Change dwarf2_attr_no_follow to be a method
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "bfd.h"
44 #include "elf-bfd.h"
45 #include "symtab.h"
46 #include "gdbtypes.h"
47 #include "objfiles.h"
48 #include "dwarf2.h"
49 #include "buildsym.h"
50 #include "demangle.h"
51 #include "gdb-demangle.h"
52 #include "filenames.h" /* for DOSish file names */
53 #include "language.h"
54 #include "complaints.h"
55 #include "dwarf2/expr.h"
56 #include "dwarf2/loc.h"
57 #include "cp-support.h"
58 #include "hashtab.h"
59 #include "command.h"
60 #include "gdbcmd.h"
61 #include "block.h"
62 #include "addrmap.h"
63 #include "typeprint.h"
64 #include "psympriv.h"
65 #include "c-lang.h"
66 #include "go-lang.h"
67 #include "valprint.h"
68 #include "gdbcore.h" /* for gnutarget */
69 #include "gdb/gdb-index.h"
70 #include "gdb_bfd.h"
71 #include "f-lang.h"
72 #include "source.h"
73 #include "build-id.h"
74 #include "namespace.h"
75 #include "gdbsupport/function-view.h"
76 #include "gdbsupport/gdb_optional.h"
77 #include "gdbsupport/underlying.h"
78 #include "gdbsupport/hash_enum.h"
79 #include "filename-seen-cache.h"
80 #include "producer.h"
81 #include <fcntl.h>
82 #include <algorithm>
83 #include <unordered_map>
84 #include "gdbsupport/selftest.h"
85 #include "rust-lang.h"
86 #include "gdbsupport/pathstuff.h"
87 #include "count-one-bits.h"
88 #include "debuginfod-support.h"
89
90 /* When == 1, print basic high level tracing messages.
91 When > 1, be more verbose.
92 This is in contrast to the low level DIE reading of dwarf_die_debug. */
93 static unsigned int dwarf_read_debug = 0;
94
95 /* When non-zero, dump DIEs after they are read in. */
96 static unsigned int dwarf_die_debug = 0;
97
98 /* When non-zero, dump line number entries as they are read in. */
99 unsigned int dwarf_line_debug = 0;
100
101 /* When true, cross-check physname against demangler. */
102 static bool check_physname = false;
103
104 /* When true, do not reject deprecated .gdb_index sections. */
105 static bool use_deprecated_index_sections = false;
106
107 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
108
109 /* The "aclass" indices for various kinds of computed DWARF symbols. */
110
111 static int dwarf2_locexpr_index;
112 static int dwarf2_loclist_index;
113 static int dwarf2_locexpr_block_index;
114 static int dwarf2_loclist_block_index;
115
116 /* An index into a (C++) symbol name component in a symbol name as
117 recorded in the mapped_index's symbol table. For each C++ symbol
118 in the symbol table, we record one entry for the start of each
119 component in the symbol in a table of name components, and then
120 sort the table, in order to be able to binary search symbol names,
121 ignoring leading namespaces, both completion and regular look up.
122 For example, for symbol "A::B::C", we'll have an entry that points
123 to "A::B::C", another that points to "B::C", and another for "C".
124 Note that function symbols in GDB index have no parameter
125 information, just the function/method names. You can convert a
126 name_component to a "const char *" using the
127 'mapped_index::symbol_name_at(offset_type)' method. */
128
129 struct name_component
130 {
131 /* Offset in the symbol name where the component starts. Stored as
132 a (32-bit) offset instead of a pointer to save memory and improve
133 locality on 64-bit architectures. */
134 offset_type name_offset;
135
136 /* The symbol's index in the symbol and constant pool tables of a
137 mapped_index. */
138 offset_type idx;
139 };
140
141 /* Base class containing bits shared by both .gdb_index and
142 .debug_name indexes. */
143
144 struct mapped_index_base
145 {
146 mapped_index_base () = default;
147 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
148
149 /* The name_component table (a sorted vector). See name_component's
150 description above. */
151 std::vector<name_component> name_components;
152
153 /* How NAME_COMPONENTS is sorted. */
154 enum case_sensitivity name_components_casing;
155
156 /* Return the number of names in the symbol table. */
157 virtual size_t symbol_name_count () const = 0;
158
159 /* Get the name of the symbol at IDX in the symbol table. */
160 virtual const char *symbol_name_at (offset_type idx) const = 0;
161
162 /* Return whether the name at IDX in the symbol table should be
163 ignored. */
164 virtual bool symbol_name_slot_invalid (offset_type idx) const
165 {
166 return false;
167 }
168
169 /* Build the symbol name component sorted vector, if we haven't
170 yet. */
171 void build_name_components ();
172
173 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
174 possible matches for LN_NO_PARAMS in the name component
175 vector. */
176 std::pair<std::vector<name_component>::const_iterator,
177 std::vector<name_component>::const_iterator>
178 find_name_components_bounds (const lookup_name_info &ln_no_params,
179 enum language lang) const;
180
181 /* Prevent deleting/destroying via a base class pointer. */
182 protected:
183 ~mapped_index_base() = default;
184 };
185
186 /* A description of the mapped index. The file format is described in
187 a comment by the code that writes the index. */
188 struct mapped_index final : public mapped_index_base
189 {
190 /* A slot/bucket in the symbol table hash. */
191 struct symbol_table_slot
192 {
193 const offset_type name;
194 const offset_type vec;
195 };
196
197 /* Index data format version. */
198 int version = 0;
199
200 /* The address table data. */
201 gdb::array_view<const gdb_byte> address_table;
202
203 /* The symbol table, implemented as a hash table. */
204 gdb::array_view<symbol_table_slot> symbol_table;
205
206 /* A pointer to the constant pool. */
207 const char *constant_pool = nullptr;
208
209 bool symbol_name_slot_invalid (offset_type idx) const override
210 {
211 const auto &bucket = this->symbol_table[idx];
212 return bucket.name == 0 && bucket.vec == 0;
213 }
214
215 /* Convenience method to get at the name of the symbol at IDX in the
216 symbol table. */
217 const char *symbol_name_at (offset_type idx) const override
218 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
219
220 size_t symbol_name_count () const override
221 { return this->symbol_table.size (); }
222 };
223
224 /* A description of the mapped .debug_names.
225 Uninitialized map has CU_COUNT 0. */
226 struct mapped_debug_names final : public mapped_index_base
227 {
228 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
229 : dwarf2_per_objfile (dwarf2_per_objfile_)
230 {}
231
232 struct dwarf2_per_objfile *dwarf2_per_objfile;
233 bfd_endian dwarf5_byte_order;
234 bool dwarf5_is_dwarf64;
235 bool augmentation_is_gdb;
236 uint8_t offset_size;
237 uint32_t cu_count = 0;
238 uint32_t tu_count, bucket_count, name_count;
239 const gdb_byte *cu_table_reordered, *tu_table_reordered;
240 const uint32_t *bucket_table_reordered, *hash_table_reordered;
241 const gdb_byte *name_table_string_offs_reordered;
242 const gdb_byte *name_table_entry_offs_reordered;
243 const gdb_byte *entry_pool;
244
245 struct index_val
246 {
247 ULONGEST dwarf_tag;
248 struct attr
249 {
250 /* Attribute name DW_IDX_*. */
251 ULONGEST dw_idx;
252
253 /* Attribute form DW_FORM_*. */
254 ULONGEST form;
255
256 /* Value if FORM is DW_FORM_implicit_const. */
257 LONGEST implicit_const;
258 };
259 std::vector<attr> attr_vec;
260 };
261
262 std::unordered_map<ULONGEST, index_val> abbrev_map;
263
264 const char *namei_to_name (uint32_t namei) const;
265
266 /* Implementation of the mapped_index_base virtual interface, for
267 the name_components cache. */
268
269 const char *symbol_name_at (offset_type idx) const override
270 { return namei_to_name (idx); }
271
272 size_t symbol_name_count () const override
273 { return this->name_count; }
274 };
275
276 /* See dwarf2read.h. */
277
278 dwarf2_per_objfile *
279 get_dwarf2_per_objfile (struct objfile *objfile)
280 {
281 return dwarf2_objfile_data_key.get (objfile);
282 }
283
284 /* Default names of the debugging sections. */
285
286 /* Note that if the debugging section has been compressed, it might
287 have a name like .zdebug_info. */
288
289 static const struct dwarf2_debug_sections dwarf2_elf_names =
290 {
291 { ".debug_info", ".zdebug_info" },
292 { ".debug_abbrev", ".zdebug_abbrev" },
293 { ".debug_line", ".zdebug_line" },
294 { ".debug_loc", ".zdebug_loc" },
295 { ".debug_loclists", ".zdebug_loclists" },
296 { ".debug_macinfo", ".zdebug_macinfo" },
297 { ".debug_macro", ".zdebug_macro" },
298 { ".debug_str", ".zdebug_str" },
299 { ".debug_str_offsets", ".zdebug_str_offsets" },
300 { ".debug_line_str", ".zdebug_line_str" },
301 { ".debug_ranges", ".zdebug_ranges" },
302 { ".debug_rnglists", ".zdebug_rnglists" },
303 { ".debug_types", ".zdebug_types" },
304 { ".debug_addr", ".zdebug_addr" },
305 { ".debug_frame", ".zdebug_frame" },
306 { ".eh_frame", NULL },
307 { ".gdb_index", ".zgdb_index" },
308 { ".debug_names", ".zdebug_names" },
309 { ".debug_aranges", ".zdebug_aranges" },
310 23
311 };
312
313 /* List of DWO/DWP sections. */
314
315 static const struct dwop_section_names
316 {
317 struct dwarf2_section_names abbrev_dwo;
318 struct dwarf2_section_names info_dwo;
319 struct dwarf2_section_names line_dwo;
320 struct dwarf2_section_names loc_dwo;
321 struct dwarf2_section_names loclists_dwo;
322 struct dwarf2_section_names macinfo_dwo;
323 struct dwarf2_section_names macro_dwo;
324 struct dwarf2_section_names str_dwo;
325 struct dwarf2_section_names str_offsets_dwo;
326 struct dwarf2_section_names types_dwo;
327 struct dwarf2_section_names cu_index;
328 struct dwarf2_section_names tu_index;
329 }
330 dwop_section_names =
331 {
332 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
333 { ".debug_info.dwo", ".zdebug_info.dwo" },
334 { ".debug_line.dwo", ".zdebug_line.dwo" },
335 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
336 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
337 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
338 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
339 { ".debug_str.dwo", ".zdebug_str.dwo" },
340 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
341 { ".debug_types.dwo", ".zdebug_types.dwo" },
342 { ".debug_cu_index", ".zdebug_cu_index" },
343 { ".debug_tu_index", ".zdebug_tu_index" },
344 };
345
346 /* local data types */
347
348 /* Type used for delaying computation of method physnames.
349 See comments for compute_delayed_physnames. */
350 struct delayed_method_info
351 {
352 /* The type to which the method is attached, i.e., its parent class. */
353 struct type *type;
354
355 /* The index of the method in the type's function fieldlists. */
356 int fnfield_index;
357
358 /* The index of the method in the fieldlist. */
359 int index;
360
361 /* The name of the DIE. */
362 const char *name;
363
364 /* The DIE associated with this method. */
365 struct die_info *die;
366 };
367
368 /* Internal state when decoding a particular compilation unit. */
369 struct dwarf2_cu
370 {
371 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
372 ~dwarf2_cu ();
373
374 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
375
376 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
377 Create the set of symtabs used by this TU, or if this TU is sharing
378 symtabs with another TU and the symtabs have already been created
379 then restore those symtabs in the line header.
380 We don't need the pc/line-number mapping for type units. */
381 void setup_type_unit_groups (struct die_info *die);
382
383 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
384 buildsym_compunit constructor. */
385 struct compunit_symtab *start_symtab (const char *name,
386 const char *comp_dir,
387 CORE_ADDR low_pc);
388
389 /* Reset the builder. */
390 void reset_builder () { m_builder.reset (); }
391
392 /* The header of the compilation unit. */
393 struct comp_unit_head header {};
394
395 /* Base address of this compilation unit. */
396 gdb::optional<CORE_ADDR> base_address;
397
398 /* The language we are debugging. */
399 enum language language = language_unknown;
400 const struct language_defn *language_defn = nullptr;
401
402 const char *producer = nullptr;
403
404 private:
405 /* The symtab builder for this CU. This is only non-NULL when full
406 symbols are being read. */
407 std::unique_ptr<buildsym_compunit> m_builder;
408
409 public:
410 /* The generic symbol table building routines have separate lists for
411 file scope symbols and all all other scopes (local scopes). So
412 we need to select the right one to pass to add_symbol_to_list().
413 We do it by keeping a pointer to the correct list in list_in_scope.
414
415 FIXME: The original dwarf code just treated the file scope as the
416 first local scope, and all other local scopes as nested local
417 scopes, and worked fine. Check to see if we really need to
418 distinguish these in buildsym.c. */
419 struct pending **list_in_scope = nullptr;
420
421 /* Hash table holding all the loaded partial DIEs
422 with partial_die->offset.SECT_OFF as hash. */
423 htab_t partial_dies = nullptr;
424
425 /* Storage for things with the same lifetime as this read-in compilation
426 unit, including partial DIEs. */
427 auto_obstack comp_unit_obstack;
428
429 /* When multiple dwarf2_cu structures are living in memory, this field
430 chains them all together, so that they can be released efficiently.
431 We will probably also want a generation counter so that most-recently-used
432 compilation units are cached... */
433 struct dwarf2_per_cu_data *read_in_chain = nullptr;
434
435 /* Backlink to our per_cu entry. */
436 struct dwarf2_per_cu_data *per_cu;
437
438 /* How many compilation units ago was this CU last referenced? */
439 int last_used = 0;
440
441 /* A hash table of DIE cu_offset for following references with
442 die_info->offset.sect_off as hash. */
443 htab_t die_hash = nullptr;
444
445 /* Full DIEs if read in. */
446 struct die_info *dies = nullptr;
447
448 /* A set of pointers to dwarf2_per_cu_data objects for compilation
449 units referenced by this one. Only set during full symbol processing;
450 partial symbol tables do not have dependencies. */
451 htab_t dependencies = nullptr;
452
453 /* Header data from the line table, during full symbol processing. */
454 struct line_header *line_header = nullptr;
455 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
456 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
457 this is the DW_TAG_compile_unit die for this CU. We'll hold on
458 to the line header as long as this DIE is being processed. See
459 process_die_scope. */
460 die_info *line_header_die_owner = nullptr;
461
462 /* A list of methods which need to have physnames computed
463 after all type information has been read. */
464 std::vector<delayed_method_info> method_list;
465
466 /* To be copied to symtab->call_site_htab. */
467 htab_t call_site_htab = nullptr;
468
469 /* Non-NULL if this CU came from a DWO file.
470 There is an invariant here that is important to remember:
471 Except for attributes copied from the top level DIE in the "main"
472 (or "stub") file in preparation for reading the DWO file
473 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
474 Either there isn't a DWO file (in which case this is NULL and the point
475 is moot), or there is and either we're not going to read it (in which
476 case this is NULL) or there is and we are reading it (in which case this
477 is non-NULL). */
478 struct dwo_unit *dwo_unit = nullptr;
479
480 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
481 Note this value comes from the Fission stub CU/TU's DIE. */
482 gdb::optional<ULONGEST> addr_base;
483
484 /* The DW_AT_rnglists_base attribute if present.
485 Note this value comes from the Fission stub CU/TU's DIE.
486 Also note that the value is zero in the non-DWO case so this value can
487 be used without needing to know whether DWO files are in use or not.
488 N.B. This does not apply to DW_AT_ranges appearing in
489 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
490 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
491 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
492 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
493 ULONGEST ranges_base = 0;
494
495 /* When reading debug info generated by older versions of rustc, we
496 have to rewrite some union types to be struct types with a
497 variant part. This rewriting must be done after the CU is fully
498 read in, because otherwise at the point of rewriting some struct
499 type might not have been fully processed. So, we keep a list of
500 all such types here and process them after expansion. */
501 std::vector<struct type *> rust_unions;
502
503 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
504 files, the value is implicitly zero. For DWARF 5 version DWO files, the
505 value is often implicit and is the size of the header of
506 .debug_str_offsets section (8 or 4, depending on the address size). */
507 gdb::optional<ULONGEST> str_offsets_base;
508
509 /* Mark used when releasing cached dies. */
510 bool mark : 1;
511
512 /* This CU references .debug_loc. See the symtab->locations_valid field.
513 This test is imperfect as there may exist optimized debug code not using
514 any location list and still facing inlining issues if handled as
515 unoptimized code. For a future better test see GCC PR other/32998. */
516 bool has_loclist : 1;
517
518 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
519 if all the producer_is_* fields are valid. This information is cached
520 because profiling CU expansion showed excessive time spent in
521 producer_is_gxx_lt_4_6. */
522 bool checked_producer : 1;
523 bool producer_is_gxx_lt_4_6 : 1;
524 bool producer_is_gcc_lt_4_3 : 1;
525 bool producer_is_icc : 1;
526 bool producer_is_icc_lt_14 : 1;
527 bool producer_is_codewarrior : 1;
528
529 /* When true, the file that we're processing is known to have
530 debugging info for C++ namespaces. GCC 3.3.x did not produce
531 this information, but later versions do. */
532
533 bool processing_has_namespace_info : 1;
534
535 struct partial_die_info *find_partial_die (sect_offset sect_off);
536
537 /* If this CU was inherited by another CU (via specification,
538 abstract_origin, etc), this is the ancestor CU. */
539 dwarf2_cu *ancestor;
540
541 /* Get the buildsym_compunit for this CU. */
542 buildsym_compunit *get_builder ()
543 {
544 /* If this CU has a builder associated with it, use that. */
545 if (m_builder != nullptr)
546 return m_builder.get ();
547
548 /* Otherwise, search ancestors for a valid builder. */
549 if (ancestor != nullptr)
550 return ancestor->get_builder ();
551
552 return nullptr;
553 }
554 };
555
556 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
557 This includes type_unit_group and quick_file_names. */
558
559 struct stmt_list_hash
560 {
561 /* The DWO unit this table is from or NULL if there is none. */
562 struct dwo_unit *dwo_unit;
563
564 /* Offset in .debug_line or .debug_line.dwo. */
565 sect_offset line_sect_off;
566 };
567
568 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
569 an object of this type. */
570
571 struct type_unit_group
572 {
573 /* dwarf2read.c's main "handle" on a TU symtab.
574 To simplify things we create an artificial CU that "includes" all the
575 type units using this stmt_list so that the rest of the code still has
576 a "per_cu" handle on the symtab. */
577 struct dwarf2_per_cu_data per_cu;
578
579 /* The TUs that share this DW_AT_stmt_list entry.
580 This is added to while parsing type units to build partial symtabs,
581 and is deleted afterwards and not used again. */
582 std::vector<signatured_type *> *tus;
583
584 /* The compunit symtab.
585 Type units in a group needn't all be defined in the same source file,
586 so we create an essentially anonymous symtab as the compunit symtab. */
587 struct compunit_symtab *compunit_symtab;
588
589 /* The data used to construct the hash key. */
590 struct stmt_list_hash hash;
591
592 /* The symbol tables for this TU (obtained from the files listed in
593 DW_AT_stmt_list).
594 WARNING: The order of entries here must match the order of entries
595 in the line header. After the first TU using this type_unit_group, the
596 line header for the subsequent TUs is recreated from this. This is done
597 because we need to use the same symtabs for each TU using the same
598 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
599 there's no guarantee the line header doesn't have duplicate entries. */
600 struct symtab **symtabs;
601 };
602
603 /* These sections are what may appear in a (real or virtual) DWO file. */
604
605 struct dwo_sections
606 {
607 struct dwarf2_section_info abbrev;
608 struct dwarf2_section_info line;
609 struct dwarf2_section_info loc;
610 struct dwarf2_section_info loclists;
611 struct dwarf2_section_info macinfo;
612 struct dwarf2_section_info macro;
613 struct dwarf2_section_info str;
614 struct dwarf2_section_info str_offsets;
615 /* In the case of a virtual DWO file, these two are unused. */
616 struct dwarf2_section_info info;
617 std::vector<dwarf2_section_info> types;
618 };
619
620 /* CUs/TUs in DWP/DWO files. */
621
622 struct dwo_unit
623 {
624 /* Backlink to the containing struct dwo_file. */
625 struct dwo_file *dwo_file;
626
627 /* The "id" that distinguishes this CU/TU.
628 .debug_info calls this "dwo_id", .debug_types calls this "signature".
629 Since signatures came first, we stick with it for consistency. */
630 ULONGEST signature;
631
632 /* The section this CU/TU lives in, in the DWO file. */
633 struct dwarf2_section_info *section;
634
635 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
636 sect_offset sect_off;
637 unsigned int length;
638
639 /* For types, offset in the type's DIE of the type defined by this TU. */
640 cu_offset type_offset_in_tu;
641 };
642
643 /* include/dwarf2.h defines the DWP section codes.
644 It defines a max value but it doesn't define a min value, which we
645 use for error checking, so provide one. */
646
647 enum dwp_v2_section_ids
648 {
649 DW_SECT_MIN = 1
650 };
651
652 /* Data for one DWO file.
653
654 This includes virtual DWO files (a virtual DWO file is a DWO file as it
655 appears in a DWP file). DWP files don't really have DWO files per se -
656 comdat folding of types "loses" the DWO file they came from, and from
657 a high level view DWP files appear to contain a mass of random types.
658 However, to maintain consistency with the non-DWP case we pretend DWP
659 files contain virtual DWO files, and we assign each TU with one virtual
660 DWO file (generally based on the line and abbrev section offsets -
661 a heuristic that seems to work in practice). */
662
663 struct dwo_file
664 {
665 dwo_file () = default;
666 DISABLE_COPY_AND_ASSIGN (dwo_file);
667
668 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
669 For virtual DWO files the name is constructed from the section offsets
670 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
671 from related CU+TUs. */
672 const char *dwo_name = nullptr;
673
674 /* The DW_AT_comp_dir attribute. */
675 const char *comp_dir = nullptr;
676
677 /* The bfd, when the file is open. Otherwise this is NULL.
678 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
679 gdb_bfd_ref_ptr dbfd;
680
681 /* The sections that make up this DWO file.
682 Remember that for virtual DWO files in DWP V2, these are virtual
683 sections (for lack of a better name). */
684 struct dwo_sections sections {};
685
686 /* The CUs in the file.
687 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
688 an extension to handle LLVM's Link Time Optimization output (where
689 multiple source files may be compiled into a single object/dwo pair). */
690 htab_up cus;
691
692 /* Table of TUs in the file.
693 Each element is a struct dwo_unit. */
694 htab_up tus;
695 };
696
697 /* These sections are what may appear in a DWP file. */
698
699 struct dwp_sections
700 {
701 /* These are used by both DWP version 1 and 2. */
702 struct dwarf2_section_info str;
703 struct dwarf2_section_info cu_index;
704 struct dwarf2_section_info tu_index;
705
706 /* These are only used by DWP version 2 files.
707 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
708 sections are referenced by section number, and are not recorded here.
709 In DWP version 2 there is at most one copy of all these sections, each
710 section being (effectively) comprised of the concatenation of all of the
711 individual sections that exist in the version 1 format.
712 To keep the code simple we treat each of these concatenated pieces as a
713 section itself (a virtual section?). */
714 struct dwarf2_section_info abbrev;
715 struct dwarf2_section_info info;
716 struct dwarf2_section_info line;
717 struct dwarf2_section_info loc;
718 struct dwarf2_section_info macinfo;
719 struct dwarf2_section_info macro;
720 struct dwarf2_section_info str_offsets;
721 struct dwarf2_section_info types;
722 };
723
724 /* These sections are what may appear in a virtual DWO file in DWP version 1.
725 A virtual DWO file is a DWO file as it appears in a DWP file. */
726
727 struct virtual_v1_dwo_sections
728 {
729 struct dwarf2_section_info abbrev;
730 struct dwarf2_section_info line;
731 struct dwarf2_section_info loc;
732 struct dwarf2_section_info macinfo;
733 struct dwarf2_section_info macro;
734 struct dwarf2_section_info str_offsets;
735 /* Each DWP hash table entry records one CU or one TU.
736 That is recorded here, and copied to dwo_unit.section. */
737 struct dwarf2_section_info info_or_types;
738 };
739
740 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
741 In version 2, the sections of the DWO files are concatenated together
742 and stored in one section of that name. Thus each ELF section contains
743 several "virtual" sections. */
744
745 struct virtual_v2_dwo_sections
746 {
747 bfd_size_type abbrev_offset;
748 bfd_size_type abbrev_size;
749
750 bfd_size_type line_offset;
751 bfd_size_type line_size;
752
753 bfd_size_type loc_offset;
754 bfd_size_type loc_size;
755
756 bfd_size_type macinfo_offset;
757 bfd_size_type macinfo_size;
758
759 bfd_size_type macro_offset;
760 bfd_size_type macro_size;
761
762 bfd_size_type str_offsets_offset;
763 bfd_size_type str_offsets_size;
764
765 /* Each DWP hash table entry records one CU or one TU.
766 That is recorded here, and copied to dwo_unit.section. */
767 bfd_size_type info_or_types_offset;
768 bfd_size_type info_or_types_size;
769 };
770
771 /* Contents of DWP hash tables. */
772
773 struct dwp_hash_table
774 {
775 uint32_t version, nr_columns;
776 uint32_t nr_units, nr_slots;
777 const gdb_byte *hash_table, *unit_table;
778 union
779 {
780 struct
781 {
782 const gdb_byte *indices;
783 } v1;
784 struct
785 {
786 /* This is indexed by column number and gives the id of the section
787 in that column. */
788 #define MAX_NR_V2_DWO_SECTIONS \
789 (1 /* .debug_info or .debug_types */ \
790 + 1 /* .debug_abbrev */ \
791 + 1 /* .debug_line */ \
792 + 1 /* .debug_loc */ \
793 + 1 /* .debug_str_offsets */ \
794 + 1 /* .debug_macro or .debug_macinfo */)
795 int section_ids[MAX_NR_V2_DWO_SECTIONS];
796 const gdb_byte *offsets;
797 const gdb_byte *sizes;
798 } v2;
799 } section_pool;
800 };
801
802 /* Data for one DWP file. */
803
804 struct dwp_file
805 {
806 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
807 : name (name_),
808 dbfd (std::move (abfd))
809 {
810 }
811
812 /* Name of the file. */
813 const char *name;
814
815 /* File format version. */
816 int version = 0;
817
818 /* The bfd. */
819 gdb_bfd_ref_ptr dbfd;
820
821 /* Section info for this file. */
822 struct dwp_sections sections {};
823
824 /* Table of CUs in the file. */
825 const struct dwp_hash_table *cus = nullptr;
826
827 /* Table of TUs in the file. */
828 const struct dwp_hash_table *tus = nullptr;
829
830 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
831 htab_up loaded_cus;
832 htab_up loaded_tus;
833
834 /* Table to map ELF section numbers to their sections.
835 This is only needed for the DWP V1 file format. */
836 unsigned int num_sections = 0;
837 asection **elf_sections = nullptr;
838 };
839
840 /* Struct used to pass misc. parameters to read_die_and_children, et
841 al. which are used for both .debug_info and .debug_types dies.
842 All parameters here are unchanging for the life of the call. This
843 struct exists to abstract away the constant parameters of die reading. */
844
845 struct die_reader_specs
846 {
847 /* The bfd of die_section. */
848 bfd* abfd;
849
850 /* The CU of the DIE we are parsing. */
851 struct dwarf2_cu *cu;
852
853 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
854 struct dwo_file *dwo_file;
855
856 /* The section the die comes from.
857 This is either .debug_info or .debug_types, or the .dwo variants. */
858 struct dwarf2_section_info *die_section;
859
860 /* die_section->buffer. */
861 const gdb_byte *buffer;
862
863 /* The end of the buffer. */
864 const gdb_byte *buffer_end;
865
866 /* The abbreviation table to use when reading the DIEs. */
867 struct abbrev_table *abbrev_table;
868 };
869
870 /* A subclass of die_reader_specs that holds storage and has complex
871 constructor and destructor behavior. */
872
873 class cutu_reader : public die_reader_specs
874 {
875 public:
876
877 cutu_reader (struct dwarf2_per_cu_data *this_cu,
878 struct abbrev_table *abbrev_table,
879 int use_existing_cu,
880 bool skip_partial);
881
882 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
883 struct dwarf2_cu *parent_cu = nullptr,
884 struct dwo_file *dwo_file = nullptr);
885
886 DISABLE_COPY_AND_ASSIGN (cutu_reader);
887
888 const gdb_byte *info_ptr = nullptr;
889 struct die_info *comp_unit_die = nullptr;
890 bool dummy_p = false;
891
892 /* Release the new CU, putting it on the chain. This cannot be done
893 for dummy CUs. */
894 void keep ();
895
896 private:
897 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
898 int use_existing_cu);
899
900 struct dwarf2_per_cu_data *m_this_cu;
901 std::unique_ptr<dwarf2_cu> m_new_cu;
902
903 /* The ordinary abbreviation table. */
904 abbrev_table_up m_abbrev_table_holder;
905
906 /* The DWO abbreviation table. */
907 abbrev_table_up m_dwo_abbrev_table;
908 };
909
910 /* When we construct a partial symbol table entry we only
911 need this much information. */
912 struct partial_die_info : public allocate_on_obstack
913 {
914 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
915
916 /* Disable assign but still keep copy ctor, which is needed
917 load_partial_dies. */
918 partial_die_info& operator=(const partial_die_info& rhs) = delete;
919
920 /* Adjust the partial die before generating a symbol for it. This
921 function may set the is_external flag or change the DIE's
922 name. */
923 void fixup (struct dwarf2_cu *cu);
924
925 /* Read a minimal amount of information into the minimal die
926 structure. */
927 const gdb_byte *read (const struct die_reader_specs *reader,
928 const struct abbrev_info &abbrev,
929 const gdb_byte *info_ptr);
930
931 /* Offset of this DIE. */
932 const sect_offset sect_off;
933
934 /* DWARF-2 tag for this DIE. */
935 const ENUM_BITFIELD(dwarf_tag) tag : 16;
936
937 /* Assorted flags describing the data found in this DIE. */
938 const unsigned int has_children : 1;
939
940 unsigned int is_external : 1;
941 unsigned int is_declaration : 1;
942 unsigned int has_type : 1;
943 unsigned int has_specification : 1;
944 unsigned int has_pc_info : 1;
945 unsigned int may_be_inlined : 1;
946
947 /* This DIE has been marked DW_AT_main_subprogram. */
948 unsigned int main_subprogram : 1;
949
950 /* Flag set if the SCOPE field of this structure has been
951 computed. */
952 unsigned int scope_set : 1;
953
954 /* Flag set if the DIE has a byte_size attribute. */
955 unsigned int has_byte_size : 1;
956
957 /* Flag set if the DIE has a DW_AT_const_value attribute. */
958 unsigned int has_const_value : 1;
959
960 /* Flag set if any of the DIE's children are template arguments. */
961 unsigned int has_template_arguments : 1;
962
963 /* Flag set if fixup has been called on this die. */
964 unsigned int fixup_called : 1;
965
966 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
967 unsigned int is_dwz : 1;
968
969 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
970 unsigned int spec_is_dwz : 1;
971
972 /* The name of this DIE. Normally the value of DW_AT_name, but
973 sometimes a default name for unnamed DIEs. */
974 const char *name = nullptr;
975
976 /* The linkage name, if present. */
977 const char *linkage_name = nullptr;
978
979 /* The scope to prepend to our children. This is generally
980 allocated on the comp_unit_obstack, so will disappear
981 when this compilation unit leaves the cache. */
982 const char *scope = nullptr;
983
984 /* Some data associated with the partial DIE. The tag determines
985 which field is live. */
986 union
987 {
988 /* The location description associated with this DIE, if any. */
989 struct dwarf_block *locdesc;
990 /* The offset of an import, for DW_TAG_imported_unit. */
991 sect_offset sect_off;
992 } d {};
993
994 /* If HAS_PC_INFO, the PC range associated with this DIE. */
995 CORE_ADDR lowpc = 0;
996 CORE_ADDR highpc = 0;
997
998 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
999 DW_AT_sibling, if any. */
1000 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1001 could return DW_AT_sibling values to its caller load_partial_dies. */
1002 const gdb_byte *sibling = nullptr;
1003
1004 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1005 DW_AT_specification (or DW_AT_abstract_origin or
1006 DW_AT_extension). */
1007 sect_offset spec_offset {};
1008
1009 /* Pointers to this DIE's parent, first child, and next sibling,
1010 if any. */
1011 struct partial_die_info *die_parent = nullptr;
1012 struct partial_die_info *die_child = nullptr;
1013 struct partial_die_info *die_sibling = nullptr;
1014
1015 friend struct partial_die_info *
1016 dwarf2_cu::find_partial_die (sect_offset sect_off);
1017
1018 private:
1019 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1020 partial_die_info (sect_offset sect_off)
1021 : partial_die_info (sect_off, DW_TAG_padding, 0)
1022 {
1023 }
1024
1025 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1026 int has_children_)
1027 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1028 {
1029 is_external = 0;
1030 is_declaration = 0;
1031 has_type = 0;
1032 has_specification = 0;
1033 has_pc_info = 0;
1034 may_be_inlined = 0;
1035 main_subprogram = 0;
1036 scope_set = 0;
1037 has_byte_size = 0;
1038 has_const_value = 0;
1039 has_template_arguments = 0;
1040 fixup_called = 0;
1041 is_dwz = 0;
1042 spec_is_dwz = 0;
1043 }
1044 };
1045
1046 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1047 but this would require a corresponding change in unpack_field_as_long
1048 and friends. */
1049 static int bits_per_byte = 8;
1050
1051 /* When reading a variant or variant part, we track a bit more
1052 information about the field, and store it in an object of this
1053 type. */
1054
1055 struct variant_field
1056 {
1057 /* If we see a DW_TAG_variant, then this will be the discriminant
1058 value. */
1059 ULONGEST discriminant_value;
1060 /* If we see a DW_TAG_variant, then this will be set if this is the
1061 default branch. */
1062 bool default_branch;
1063 /* While reading a DW_TAG_variant_part, this will be set if this
1064 field is the discriminant. */
1065 bool is_discriminant;
1066 };
1067
1068 struct nextfield
1069 {
1070 int accessibility = 0;
1071 int virtuality = 0;
1072 /* Extra information to describe a variant or variant part. */
1073 struct variant_field variant {};
1074 struct field field {};
1075 };
1076
1077 struct fnfieldlist
1078 {
1079 const char *name = nullptr;
1080 std::vector<struct fn_field> fnfields;
1081 };
1082
1083 /* The routines that read and process dies for a C struct or C++ class
1084 pass lists of data member fields and lists of member function fields
1085 in an instance of a field_info structure, as defined below. */
1086 struct field_info
1087 {
1088 /* List of data member and baseclasses fields. */
1089 std::vector<struct nextfield> fields;
1090 std::vector<struct nextfield> baseclasses;
1091
1092 /* Set if the accessibility of one of the fields is not public. */
1093 int non_public_fields = 0;
1094
1095 /* Member function fieldlist array, contains name of possibly overloaded
1096 member function, number of overloaded member functions and a pointer
1097 to the head of the member function field chain. */
1098 std::vector<struct fnfieldlist> fnfieldlists;
1099
1100 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1101 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1102 std::vector<struct decl_field> typedef_field_list;
1103
1104 /* Nested types defined by this class and the number of elements in this
1105 list. */
1106 std::vector<struct decl_field> nested_types_list;
1107
1108 /* Return the total number of fields (including baseclasses). */
1109 int nfields () const
1110 {
1111 return fields.size () + baseclasses.size ();
1112 }
1113 };
1114
1115 /* Loaded secondary compilation units are kept in memory until they
1116 have not been referenced for the processing of this many
1117 compilation units. Set this to zero to disable caching. Cache
1118 sizes of up to at least twenty will improve startup time for
1119 typical inter-CU-reference binaries, at an obvious memory cost. */
1120 static int dwarf_max_cache_age = 5;
1121 static void
1122 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1123 struct cmd_list_element *c, const char *value)
1124 {
1125 fprintf_filtered (file, _("The upper bound on the age of cached "
1126 "DWARF compilation units is %s.\n"),
1127 value);
1128 }
1129 \f
1130 /* local function prototypes */
1131
1132 static void dwarf2_find_base_address (struct die_info *die,
1133 struct dwarf2_cu *cu);
1134
1135 static dwarf2_psymtab *create_partial_symtab
1136 (struct dwarf2_per_cu_data *per_cu, const char *name);
1137
1138 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1139 const gdb_byte *info_ptr,
1140 struct die_info *type_unit_die);
1141
1142 static void dwarf2_build_psymtabs_hard
1143 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1144
1145 static void scan_partial_symbols (struct partial_die_info *,
1146 CORE_ADDR *, CORE_ADDR *,
1147 int, struct dwarf2_cu *);
1148
1149 static void add_partial_symbol (struct partial_die_info *,
1150 struct dwarf2_cu *);
1151
1152 static void add_partial_namespace (struct partial_die_info *pdi,
1153 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1154 int set_addrmap, struct dwarf2_cu *cu);
1155
1156 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1157 CORE_ADDR *highpc, int set_addrmap,
1158 struct dwarf2_cu *cu);
1159
1160 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1161 struct dwarf2_cu *cu);
1162
1163 static void add_partial_subprogram (struct partial_die_info *pdi,
1164 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1165 int need_pc, struct dwarf2_cu *cu);
1166
1167 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1168
1169 static struct partial_die_info *load_partial_dies
1170 (const struct die_reader_specs *, const gdb_byte *, int);
1171
1172 /* A pair of partial_die_info and compilation unit. */
1173 struct cu_partial_die_info
1174 {
1175 /* The compilation unit of the partial_die_info. */
1176 struct dwarf2_cu *cu;
1177 /* A partial_die_info. */
1178 struct partial_die_info *pdi;
1179
1180 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1181 : cu (cu),
1182 pdi (pdi)
1183 { /* Nothing. */ }
1184
1185 private:
1186 cu_partial_die_info () = delete;
1187 };
1188
1189 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1190 struct dwarf2_cu *);
1191
1192 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1193 struct attribute *, struct attr_abbrev *,
1194 const gdb_byte *, bool *need_reprocess);
1195
1196 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1197 struct attribute *attr);
1198
1199 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1200
1201 static sect_offset read_abbrev_offset
1202 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1203 struct dwarf2_section_info *, sect_offset);
1204
1205 static const char *read_indirect_string
1206 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1207 const struct comp_unit_head *, unsigned int *);
1208
1209 static const char *read_indirect_string_at_offset
1210 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1211
1212 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1213 const gdb_byte *,
1214 unsigned int *);
1215
1216 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1217 ULONGEST str_index);
1218
1219 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1220 ULONGEST str_index);
1221
1222 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1223
1224 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1225 struct dwarf2_cu *);
1226
1227 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1228 struct dwarf2_cu *cu);
1229
1230 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1231
1232 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1233 struct dwarf2_cu *cu);
1234
1235 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1236
1237 static struct die_info *die_specification (struct die_info *die,
1238 struct dwarf2_cu **);
1239
1240 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1241 struct dwarf2_cu *cu);
1242
1243 static void dwarf_decode_lines (struct line_header *, const char *,
1244 struct dwarf2_cu *, dwarf2_psymtab *,
1245 CORE_ADDR, int decode_mapping);
1246
1247 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1248 const char *);
1249
1250 static struct symbol *new_symbol (struct die_info *, struct type *,
1251 struct dwarf2_cu *, struct symbol * = NULL);
1252
1253 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1254 struct dwarf2_cu *);
1255
1256 static void dwarf2_const_value_attr (const struct attribute *attr,
1257 struct type *type,
1258 const char *name,
1259 struct obstack *obstack,
1260 struct dwarf2_cu *cu, LONGEST *value,
1261 const gdb_byte **bytes,
1262 struct dwarf2_locexpr_baton **baton);
1263
1264 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1265
1266 static int need_gnat_info (struct dwarf2_cu *);
1267
1268 static struct type *die_descriptive_type (struct die_info *,
1269 struct dwarf2_cu *);
1270
1271 static void set_descriptive_type (struct type *, struct die_info *,
1272 struct dwarf2_cu *);
1273
1274 static struct type *die_containing_type (struct die_info *,
1275 struct dwarf2_cu *);
1276
1277 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1278 struct dwarf2_cu *);
1279
1280 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1281
1282 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1283
1284 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1285
1286 static char *typename_concat (struct obstack *obs, const char *prefix,
1287 const char *suffix, int physname,
1288 struct dwarf2_cu *cu);
1289
1290 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1291
1292 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1293
1294 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1295
1296 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1297
1298 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1299
1300 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1301
1302 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1303 struct dwarf2_cu *, dwarf2_psymtab *);
1304
1305 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1306 values. Keep the items ordered with increasing constraints compliance. */
1307 enum pc_bounds_kind
1308 {
1309 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1310 PC_BOUNDS_NOT_PRESENT,
1311
1312 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1313 were present but they do not form a valid range of PC addresses. */
1314 PC_BOUNDS_INVALID,
1315
1316 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1317 PC_BOUNDS_RANGES,
1318
1319 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1320 PC_BOUNDS_HIGH_LOW,
1321 };
1322
1323 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1324 CORE_ADDR *, CORE_ADDR *,
1325 struct dwarf2_cu *,
1326 dwarf2_psymtab *);
1327
1328 static void get_scope_pc_bounds (struct die_info *,
1329 CORE_ADDR *, CORE_ADDR *,
1330 struct dwarf2_cu *);
1331
1332 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1333 CORE_ADDR, struct dwarf2_cu *);
1334
1335 static void dwarf2_add_field (struct field_info *, struct die_info *,
1336 struct dwarf2_cu *);
1337
1338 static void dwarf2_attach_fields_to_type (struct field_info *,
1339 struct type *, struct dwarf2_cu *);
1340
1341 static void dwarf2_add_member_fn (struct field_info *,
1342 struct die_info *, struct type *,
1343 struct dwarf2_cu *);
1344
1345 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1346 struct type *,
1347 struct dwarf2_cu *);
1348
1349 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1350
1351 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1352
1353 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1354
1355 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1356
1357 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1358
1359 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1360
1361 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1362
1363 static struct type *read_module_type (struct die_info *die,
1364 struct dwarf2_cu *cu);
1365
1366 static const char *namespace_name (struct die_info *die,
1367 int *is_anonymous, struct dwarf2_cu *);
1368
1369 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1370
1371 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1372
1373 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1374 struct dwarf2_cu *);
1375
1376 static struct die_info *read_die_and_siblings_1
1377 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1378 struct die_info *);
1379
1380 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1381 const gdb_byte *info_ptr,
1382 const gdb_byte **new_info_ptr,
1383 struct die_info *parent);
1384
1385 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1386 struct die_info **, const gdb_byte *,
1387 int);
1388
1389 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1390 struct die_info **, const gdb_byte *);
1391
1392 static void process_die (struct die_info *, struct dwarf2_cu *);
1393
1394 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1395 struct objfile *);
1396
1397 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1398
1399 static const char *dwarf2_full_name (const char *name,
1400 struct die_info *die,
1401 struct dwarf2_cu *cu);
1402
1403 static const char *dwarf2_physname (const char *name, struct die_info *die,
1404 struct dwarf2_cu *cu);
1405
1406 static struct die_info *dwarf2_extension (struct die_info *die,
1407 struct dwarf2_cu **);
1408
1409 static const char *dwarf_tag_name (unsigned int);
1410
1411 static const char *dwarf_attr_name (unsigned int);
1412
1413 static const char *dwarf_form_name (unsigned int);
1414
1415 static const char *dwarf_bool_name (unsigned int);
1416
1417 static const char *dwarf_type_encoding_name (unsigned int);
1418
1419 static struct die_info *sibling_die (struct die_info *);
1420
1421 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1422
1423 static void dump_die_for_error (struct die_info *);
1424
1425 static void dump_die_1 (struct ui_file *, int level, int max_level,
1426 struct die_info *);
1427
1428 /*static*/ void dump_die (struct die_info *, int max_level);
1429
1430 static void store_in_ref_table (struct die_info *,
1431 struct dwarf2_cu *);
1432
1433 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1434
1435 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1436
1437 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1438 const struct attribute *,
1439 struct dwarf2_cu **);
1440
1441 static struct die_info *follow_die_ref (struct die_info *,
1442 const struct attribute *,
1443 struct dwarf2_cu **);
1444
1445 static struct die_info *follow_die_sig (struct die_info *,
1446 const struct attribute *,
1447 struct dwarf2_cu **);
1448
1449 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1450 struct dwarf2_cu *);
1451
1452 static struct type *get_DW_AT_signature_type (struct die_info *,
1453 const struct attribute *,
1454 struct dwarf2_cu *);
1455
1456 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1457
1458 static void read_signatured_type (struct signatured_type *);
1459
1460 static int attr_to_dynamic_prop (const struct attribute *attr,
1461 struct die_info *die, struct dwarf2_cu *cu,
1462 struct dynamic_prop *prop, struct type *type);
1463
1464 /* memory allocation interface */
1465
1466 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1467
1468 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1469
1470 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1471
1472 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1473 struct dwarf2_loclist_baton *baton,
1474 const struct attribute *attr);
1475
1476 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1477 struct symbol *sym,
1478 struct dwarf2_cu *cu,
1479 int is_block);
1480
1481 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1482 const gdb_byte *info_ptr,
1483 struct abbrev_info *abbrev);
1484
1485 static hashval_t partial_die_hash (const void *item);
1486
1487 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1488
1489 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1490 (sect_offset sect_off, unsigned int offset_in_dwz,
1491 struct dwarf2_per_objfile *dwarf2_per_objfile);
1492
1493 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1494 struct die_info *comp_unit_die,
1495 enum language pretend_language);
1496
1497 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1498
1499 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1500
1501 static struct type *set_die_type (struct die_info *, struct type *,
1502 struct dwarf2_cu *);
1503
1504 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1505
1506 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1507
1508 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1509 enum language);
1510
1511 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1512 enum language);
1513
1514 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1515 enum language);
1516
1517 static void dwarf2_add_dependence (struct dwarf2_cu *,
1518 struct dwarf2_per_cu_data *);
1519
1520 static void dwarf2_mark (struct dwarf2_cu *);
1521
1522 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1523
1524 static struct type *get_die_type_at_offset (sect_offset,
1525 struct dwarf2_per_cu_data *);
1526
1527 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1528
1529 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1530 enum language pretend_language);
1531
1532 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1533
1534 /* Class, the destructor of which frees all allocated queue entries. This
1535 will only have work to do if an error was thrown while processing the
1536 dwarf. If no error was thrown then the queue entries should have all
1537 been processed, and freed, as we went along. */
1538
1539 class dwarf2_queue_guard
1540 {
1541 public:
1542 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1543 : m_per_objfile (per_objfile)
1544 {
1545 }
1546
1547 /* Free any entries remaining on the queue. There should only be
1548 entries left if we hit an error while processing the dwarf. */
1549 ~dwarf2_queue_guard ()
1550 {
1551 /* Ensure that no memory is allocated by the queue. */
1552 std::queue<dwarf2_queue_item> empty;
1553 std::swap (m_per_objfile->queue, empty);
1554 }
1555
1556 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1557
1558 private:
1559 dwarf2_per_objfile *m_per_objfile;
1560 };
1561
1562 dwarf2_queue_item::~dwarf2_queue_item ()
1563 {
1564 /* Anything still marked queued is likely to be in an
1565 inconsistent state, so discard it. */
1566 if (per_cu->queued)
1567 {
1568 if (per_cu->cu != NULL)
1569 free_one_cached_comp_unit (per_cu);
1570 per_cu->queued = 0;
1571 }
1572 }
1573
1574 /* The return type of find_file_and_directory. Note, the enclosed
1575 string pointers are only valid while this object is valid. */
1576
1577 struct file_and_directory
1578 {
1579 /* The filename. This is never NULL. */
1580 const char *name;
1581
1582 /* The compilation directory. NULL if not known. If we needed to
1583 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1584 points directly to the DW_AT_comp_dir string attribute owned by
1585 the obstack that owns the DIE. */
1586 const char *comp_dir;
1587
1588 /* If we needed to build a new string for comp_dir, this is what
1589 owns the storage. */
1590 std::string comp_dir_storage;
1591 };
1592
1593 static file_and_directory find_file_and_directory (struct die_info *die,
1594 struct dwarf2_cu *cu);
1595
1596 static htab_up allocate_signatured_type_table ();
1597
1598 static htab_up allocate_dwo_unit_table ();
1599
1600 static struct dwo_unit *lookup_dwo_unit_in_dwp
1601 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1602 struct dwp_file *dwp_file, const char *comp_dir,
1603 ULONGEST signature, int is_debug_types);
1604
1605 static struct dwp_file *get_dwp_file
1606 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1607
1608 static struct dwo_unit *lookup_dwo_comp_unit
1609 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1610
1611 static struct dwo_unit *lookup_dwo_type_unit
1612 (struct signatured_type *, const char *, const char *);
1613
1614 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1615
1616 /* A unique pointer to a dwo_file. */
1617
1618 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1619
1620 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1621
1622 static void check_producer (struct dwarf2_cu *cu);
1623
1624 static void free_line_header_voidp (void *arg);
1625 \f
1626 /* Various complaints about symbol reading that don't abort the process. */
1627
1628 static void
1629 dwarf2_debug_line_missing_file_complaint (void)
1630 {
1631 complaint (_(".debug_line section has line data without a file"));
1632 }
1633
1634 static void
1635 dwarf2_debug_line_missing_end_sequence_complaint (void)
1636 {
1637 complaint (_(".debug_line section has line "
1638 "program sequence without an end"));
1639 }
1640
1641 static void
1642 dwarf2_complex_location_expr_complaint (void)
1643 {
1644 complaint (_("location expression too complex"));
1645 }
1646
1647 static void
1648 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1649 int arg3)
1650 {
1651 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1652 arg1, arg2, arg3);
1653 }
1654
1655 static void
1656 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1657 {
1658 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1659 arg1, arg2);
1660 }
1661
1662 /* Hash function for line_header_hash. */
1663
1664 static hashval_t
1665 line_header_hash (const struct line_header *ofs)
1666 {
1667 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1668 }
1669
1670 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1671
1672 static hashval_t
1673 line_header_hash_voidp (const void *item)
1674 {
1675 const struct line_header *ofs = (const struct line_header *) item;
1676
1677 return line_header_hash (ofs);
1678 }
1679
1680 /* Equality function for line_header_hash. */
1681
1682 static int
1683 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1684 {
1685 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1686 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1687
1688 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1689 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1690 }
1691
1692 \f
1693
1694 /* See declaration. */
1695
1696 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1697 const dwarf2_debug_sections *names,
1698 bool can_copy_)
1699 : objfile (objfile_),
1700 can_copy (can_copy_)
1701 {
1702 if (names == NULL)
1703 names = &dwarf2_elf_names;
1704
1705 bfd *obfd = objfile->obfd;
1706
1707 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1708 locate_sections (obfd, sec, *names);
1709 }
1710
1711 dwarf2_per_objfile::~dwarf2_per_objfile ()
1712 {
1713 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1714 free_cached_comp_units ();
1715
1716 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1717 per_cu->imported_symtabs_free ();
1718
1719 for (signatured_type *sig_type : all_type_units)
1720 sig_type->per_cu.imported_symtabs_free ();
1721
1722 /* Everything else should be on the objfile obstack. */
1723 }
1724
1725 /* See declaration. */
1726
1727 void
1728 dwarf2_per_objfile::free_cached_comp_units ()
1729 {
1730 dwarf2_per_cu_data *per_cu = read_in_chain;
1731 dwarf2_per_cu_data **last_chain = &read_in_chain;
1732 while (per_cu != NULL)
1733 {
1734 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1735
1736 delete per_cu->cu;
1737 *last_chain = next_cu;
1738 per_cu = next_cu;
1739 }
1740 }
1741
1742 /* A helper class that calls free_cached_comp_units on
1743 destruction. */
1744
1745 class free_cached_comp_units
1746 {
1747 public:
1748
1749 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1750 : m_per_objfile (per_objfile)
1751 {
1752 }
1753
1754 ~free_cached_comp_units ()
1755 {
1756 m_per_objfile->free_cached_comp_units ();
1757 }
1758
1759 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1760
1761 private:
1762
1763 dwarf2_per_objfile *m_per_objfile;
1764 };
1765
1766 /* Try to locate the sections we need for DWARF 2 debugging
1767 information and return true if we have enough to do something.
1768 NAMES points to the dwarf2 section names, or is NULL if the standard
1769 ELF names are used. CAN_COPY is true for formats where symbol
1770 interposition is possible and so symbol values must follow copy
1771 relocation rules. */
1772
1773 int
1774 dwarf2_has_info (struct objfile *objfile,
1775 const struct dwarf2_debug_sections *names,
1776 bool can_copy)
1777 {
1778 if (objfile->flags & OBJF_READNEVER)
1779 return 0;
1780
1781 struct dwarf2_per_objfile *dwarf2_per_objfile
1782 = get_dwarf2_per_objfile (objfile);
1783
1784 if (dwarf2_per_objfile == NULL)
1785 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1786 names,
1787 can_copy);
1788
1789 return (!dwarf2_per_objfile->info.is_virtual
1790 && dwarf2_per_objfile->info.s.section != NULL
1791 && !dwarf2_per_objfile->abbrev.is_virtual
1792 && dwarf2_per_objfile->abbrev.s.section != NULL);
1793 }
1794
1795 /* When loading sections, we look either for uncompressed section or for
1796 compressed section names. */
1797
1798 static int
1799 section_is_p (const char *section_name,
1800 const struct dwarf2_section_names *names)
1801 {
1802 if (names->normal != NULL
1803 && strcmp (section_name, names->normal) == 0)
1804 return 1;
1805 if (names->compressed != NULL
1806 && strcmp (section_name, names->compressed) == 0)
1807 return 1;
1808 return 0;
1809 }
1810
1811 /* See declaration. */
1812
1813 void
1814 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1815 const dwarf2_debug_sections &names)
1816 {
1817 flagword aflag = bfd_section_flags (sectp);
1818
1819 if ((aflag & SEC_HAS_CONTENTS) == 0)
1820 {
1821 }
1822 else if (elf_section_data (sectp)->this_hdr.sh_size
1823 > bfd_get_file_size (abfd))
1824 {
1825 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1826 warning (_("Discarding section %s which has a section size (%s"
1827 ") larger than the file size [in module %s]"),
1828 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1829 bfd_get_filename (abfd));
1830 }
1831 else if (section_is_p (sectp->name, &names.info))
1832 {
1833 this->info.s.section = sectp;
1834 this->info.size = bfd_section_size (sectp);
1835 }
1836 else if (section_is_p (sectp->name, &names.abbrev))
1837 {
1838 this->abbrev.s.section = sectp;
1839 this->abbrev.size = bfd_section_size (sectp);
1840 }
1841 else if (section_is_p (sectp->name, &names.line))
1842 {
1843 this->line.s.section = sectp;
1844 this->line.size = bfd_section_size (sectp);
1845 }
1846 else if (section_is_p (sectp->name, &names.loc))
1847 {
1848 this->loc.s.section = sectp;
1849 this->loc.size = bfd_section_size (sectp);
1850 }
1851 else if (section_is_p (sectp->name, &names.loclists))
1852 {
1853 this->loclists.s.section = sectp;
1854 this->loclists.size = bfd_section_size (sectp);
1855 }
1856 else if (section_is_p (sectp->name, &names.macinfo))
1857 {
1858 this->macinfo.s.section = sectp;
1859 this->macinfo.size = bfd_section_size (sectp);
1860 }
1861 else if (section_is_p (sectp->name, &names.macro))
1862 {
1863 this->macro.s.section = sectp;
1864 this->macro.size = bfd_section_size (sectp);
1865 }
1866 else if (section_is_p (sectp->name, &names.str))
1867 {
1868 this->str.s.section = sectp;
1869 this->str.size = bfd_section_size (sectp);
1870 }
1871 else if (section_is_p (sectp->name, &names.str_offsets))
1872 {
1873 this->str_offsets.s.section = sectp;
1874 this->str_offsets.size = bfd_section_size (sectp);
1875 }
1876 else if (section_is_p (sectp->name, &names.line_str))
1877 {
1878 this->line_str.s.section = sectp;
1879 this->line_str.size = bfd_section_size (sectp);
1880 }
1881 else if (section_is_p (sectp->name, &names.addr))
1882 {
1883 this->addr.s.section = sectp;
1884 this->addr.size = bfd_section_size (sectp);
1885 }
1886 else if (section_is_p (sectp->name, &names.frame))
1887 {
1888 this->frame.s.section = sectp;
1889 this->frame.size = bfd_section_size (sectp);
1890 }
1891 else if (section_is_p (sectp->name, &names.eh_frame))
1892 {
1893 this->eh_frame.s.section = sectp;
1894 this->eh_frame.size = bfd_section_size (sectp);
1895 }
1896 else if (section_is_p (sectp->name, &names.ranges))
1897 {
1898 this->ranges.s.section = sectp;
1899 this->ranges.size = bfd_section_size (sectp);
1900 }
1901 else if (section_is_p (sectp->name, &names.rnglists))
1902 {
1903 this->rnglists.s.section = sectp;
1904 this->rnglists.size = bfd_section_size (sectp);
1905 }
1906 else if (section_is_p (sectp->name, &names.types))
1907 {
1908 struct dwarf2_section_info type_section;
1909
1910 memset (&type_section, 0, sizeof (type_section));
1911 type_section.s.section = sectp;
1912 type_section.size = bfd_section_size (sectp);
1913
1914 this->types.push_back (type_section);
1915 }
1916 else if (section_is_p (sectp->name, &names.gdb_index))
1917 {
1918 this->gdb_index.s.section = sectp;
1919 this->gdb_index.size = bfd_section_size (sectp);
1920 }
1921 else if (section_is_p (sectp->name, &names.debug_names))
1922 {
1923 this->debug_names.s.section = sectp;
1924 this->debug_names.size = bfd_section_size (sectp);
1925 }
1926 else if (section_is_p (sectp->name, &names.debug_aranges))
1927 {
1928 this->debug_aranges.s.section = sectp;
1929 this->debug_aranges.size = bfd_section_size (sectp);
1930 }
1931
1932 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1933 && bfd_section_vma (sectp) == 0)
1934 this->has_section_at_zero = true;
1935 }
1936
1937 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1938 SECTION_NAME. */
1939
1940 void
1941 dwarf2_get_section_info (struct objfile *objfile,
1942 enum dwarf2_section_enum sect,
1943 asection **sectp, const gdb_byte **bufp,
1944 bfd_size_type *sizep)
1945 {
1946 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1947 struct dwarf2_section_info *info;
1948
1949 /* We may see an objfile without any DWARF, in which case we just
1950 return nothing. */
1951 if (data == NULL)
1952 {
1953 *sectp = NULL;
1954 *bufp = NULL;
1955 *sizep = 0;
1956 return;
1957 }
1958 switch (sect)
1959 {
1960 case DWARF2_DEBUG_FRAME:
1961 info = &data->frame;
1962 break;
1963 case DWARF2_EH_FRAME:
1964 info = &data->eh_frame;
1965 break;
1966 default:
1967 gdb_assert_not_reached ("unexpected section");
1968 }
1969
1970 info->read (objfile);
1971
1972 *sectp = info->get_bfd_section ();
1973 *bufp = info->buffer;
1974 *sizep = info->size;
1975 }
1976
1977 /* A helper function to find the sections for a .dwz file. */
1978
1979 static void
1980 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1981 {
1982 struct dwz_file *dwz_file = (struct dwz_file *) arg;
1983
1984 /* Note that we only support the standard ELF names, because .dwz
1985 is ELF-only (at the time of writing). */
1986 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1987 {
1988 dwz_file->abbrev.s.section = sectp;
1989 dwz_file->abbrev.size = bfd_section_size (sectp);
1990 }
1991 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1992 {
1993 dwz_file->info.s.section = sectp;
1994 dwz_file->info.size = bfd_section_size (sectp);
1995 }
1996 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1997 {
1998 dwz_file->str.s.section = sectp;
1999 dwz_file->str.size = bfd_section_size (sectp);
2000 }
2001 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2002 {
2003 dwz_file->line.s.section = sectp;
2004 dwz_file->line.size = bfd_section_size (sectp);
2005 }
2006 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2007 {
2008 dwz_file->macro.s.section = sectp;
2009 dwz_file->macro.size = bfd_section_size (sectp);
2010 }
2011 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2012 {
2013 dwz_file->gdb_index.s.section = sectp;
2014 dwz_file->gdb_index.size = bfd_section_size (sectp);
2015 }
2016 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2017 {
2018 dwz_file->debug_names.s.section = sectp;
2019 dwz_file->debug_names.size = bfd_section_size (sectp);
2020 }
2021 }
2022
2023 /* See dwarf2read.h. */
2024
2025 struct dwz_file *
2026 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2027 {
2028 const char *filename;
2029 bfd_size_type buildid_len_arg;
2030 size_t buildid_len;
2031 bfd_byte *buildid;
2032
2033 if (dwarf2_per_objfile->dwz_file != NULL)
2034 return dwarf2_per_objfile->dwz_file.get ();
2035
2036 bfd_set_error (bfd_error_no_error);
2037 gdb::unique_xmalloc_ptr<char> data
2038 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2039 &buildid_len_arg, &buildid));
2040 if (data == NULL)
2041 {
2042 if (bfd_get_error () == bfd_error_no_error)
2043 return NULL;
2044 error (_("could not read '.gnu_debugaltlink' section: %s"),
2045 bfd_errmsg (bfd_get_error ()));
2046 }
2047
2048 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2049
2050 buildid_len = (size_t) buildid_len_arg;
2051
2052 filename = data.get ();
2053
2054 std::string abs_storage;
2055 if (!IS_ABSOLUTE_PATH (filename))
2056 {
2057 gdb::unique_xmalloc_ptr<char> abs
2058 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2059
2060 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2061 filename = abs_storage.c_str ();
2062 }
2063
2064 /* First try the file name given in the section. If that doesn't
2065 work, try to use the build-id instead. */
2066 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2067 if (dwz_bfd != NULL)
2068 {
2069 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2070 dwz_bfd.reset (nullptr);
2071 }
2072
2073 if (dwz_bfd == NULL)
2074 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2075
2076 if (dwz_bfd == nullptr)
2077 {
2078 gdb::unique_xmalloc_ptr<char> alt_filename;
2079 const char *origname = dwarf2_per_objfile->objfile->original_name;
2080
2081 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2082 buildid_len,
2083 origname,
2084 &alt_filename));
2085
2086 if (fd.get () >= 0)
2087 {
2088 /* File successfully retrieved from server. */
2089 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2090
2091 if (dwz_bfd == nullptr)
2092 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2093 alt_filename.get ());
2094 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2095 dwz_bfd.reset (nullptr);
2096 }
2097 }
2098
2099 if (dwz_bfd == NULL)
2100 error (_("could not find '.gnu_debugaltlink' file for %s"),
2101 objfile_name (dwarf2_per_objfile->objfile));
2102
2103 std::unique_ptr<struct dwz_file> result
2104 (new struct dwz_file (std::move (dwz_bfd)));
2105
2106 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2107 result.get ());
2108
2109 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2110 result->dwz_bfd.get ());
2111 dwarf2_per_objfile->dwz_file = std::move (result);
2112 return dwarf2_per_objfile->dwz_file.get ();
2113 }
2114 \f
2115 /* DWARF quick_symbols_functions support. */
2116
2117 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2118 unique line tables, so we maintain a separate table of all .debug_line
2119 derived entries to support the sharing.
2120 All the quick functions need is the list of file names. We discard the
2121 line_header when we're done and don't need to record it here. */
2122 struct quick_file_names
2123 {
2124 /* The data used to construct the hash key. */
2125 struct stmt_list_hash hash;
2126
2127 /* The number of entries in file_names, real_names. */
2128 unsigned int num_file_names;
2129
2130 /* The file names from the line table, after being run through
2131 file_full_name. */
2132 const char **file_names;
2133
2134 /* The file names from the line table after being run through
2135 gdb_realpath. These are computed lazily. */
2136 const char **real_names;
2137 };
2138
2139 /* When using the index (and thus not using psymtabs), each CU has an
2140 object of this type. This is used to hold information needed by
2141 the various "quick" methods. */
2142 struct dwarf2_per_cu_quick_data
2143 {
2144 /* The file table. This can be NULL if there was no file table
2145 or it's currently not read in.
2146 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2147 struct quick_file_names *file_names;
2148
2149 /* The corresponding symbol table. This is NULL if symbols for this
2150 CU have not yet been read. */
2151 struct compunit_symtab *compunit_symtab;
2152
2153 /* A temporary mark bit used when iterating over all CUs in
2154 expand_symtabs_matching. */
2155 unsigned int mark : 1;
2156
2157 /* True if we've tried to read the file table and found there isn't one.
2158 There will be no point in trying to read it again next time. */
2159 unsigned int no_file_data : 1;
2160 };
2161
2162 /* Utility hash function for a stmt_list_hash. */
2163
2164 static hashval_t
2165 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2166 {
2167 hashval_t v = 0;
2168
2169 if (stmt_list_hash->dwo_unit != NULL)
2170 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2171 v += to_underlying (stmt_list_hash->line_sect_off);
2172 return v;
2173 }
2174
2175 /* Utility equality function for a stmt_list_hash. */
2176
2177 static int
2178 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2179 const struct stmt_list_hash *rhs)
2180 {
2181 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2182 return 0;
2183 if (lhs->dwo_unit != NULL
2184 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2185 return 0;
2186
2187 return lhs->line_sect_off == rhs->line_sect_off;
2188 }
2189
2190 /* Hash function for a quick_file_names. */
2191
2192 static hashval_t
2193 hash_file_name_entry (const void *e)
2194 {
2195 const struct quick_file_names *file_data
2196 = (const struct quick_file_names *) e;
2197
2198 return hash_stmt_list_entry (&file_data->hash);
2199 }
2200
2201 /* Equality function for a quick_file_names. */
2202
2203 static int
2204 eq_file_name_entry (const void *a, const void *b)
2205 {
2206 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2207 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2208
2209 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2210 }
2211
2212 /* Delete function for a quick_file_names. */
2213
2214 static void
2215 delete_file_name_entry (void *e)
2216 {
2217 struct quick_file_names *file_data = (struct quick_file_names *) e;
2218 int i;
2219
2220 for (i = 0; i < file_data->num_file_names; ++i)
2221 {
2222 xfree ((void*) file_data->file_names[i]);
2223 if (file_data->real_names)
2224 xfree ((void*) file_data->real_names[i]);
2225 }
2226
2227 /* The space for the struct itself lives on objfile_obstack,
2228 so we don't free it here. */
2229 }
2230
2231 /* Create a quick_file_names hash table. */
2232
2233 static htab_up
2234 create_quick_file_names_table (unsigned int nr_initial_entries)
2235 {
2236 return htab_up (htab_create_alloc (nr_initial_entries,
2237 hash_file_name_entry, eq_file_name_entry,
2238 delete_file_name_entry, xcalloc, xfree));
2239 }
2240
2241 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2242 have to be created afterwards. You should call age_cached_comp_units after
2243 processing PER_CU->CU. dw2_setup must have been already called. */
2244
2245 static void
2246 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2247 {
2248 if (per_cu->is_debug_types)
2249 load_full_type_unit (per_cu);
2250 else
2251 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2252
2253 if (per_cu->cu == NULL)
2254 return; /* Dummy CU. */
2255
2256 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2257 }
2258
2259 /* Read in the symbols for PER_CU. */
2260
2261 static void
2262 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2263 {
2264 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2265
2266 /* Skip type_unit_groups, reading the type units they contain
2267 is handled elsewhere. */
2268 if (per_cu->type_unit_group_p ())
2269 return;
2270
2271 /* The destructor of dwarf2_queue_guard frees any entries left on
2272 the queue. After this point we're guaranteed to leave this function
2273 with the dwarf queue empty. */
2274 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2275
2276 if (dwarf2_per_objfile->using_index
2277 ? per_cu->v.quick->compunit_symtab == NULL
2278 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2279 {
2280 queue_comp_unit (per_cu, language_minimal);
2281 load_cu (per_cu, skip_partial);
2282
2283 /* If we just loaded a CU from a DWO, and we're working with an index
2284 that may badly handle TUs, load all the TUs in that DWO as well.
2285 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2286 if (!per_cu->is_debug_types
2287 && per_cu->cu != NULL
2288 && per_cu->cu->dwo_unit != NULL
2289 && dwarf2_per_objfile->index_table != NULL
2290 && dwarf2_per_objfile->index_table->version <= 7
2291 /* DWP files aren't supported yet. */
2292 && get_dwp_file (dwarf2_per_objfile) == NULL)
2293 queue_and_load_all_dwo_tus (per_cu);
2294 }
2295
2296 process_queue (dwarf2_per_objfile);
2297
2298 /* Age the cache, releasing compilation units that have not
2299 been used recently. */
2300 age_cached_comp_units (dwarf2_per_objfile);
2301 }
2302
2303 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2304 the objfile from which this CU came. Returns the resulting symbol
2305 table. */
2306
2307 static struct compunit_symtab *
2308 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2309 {
2310 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2311
2312 gdb_assert (dwarf2_per_objfile->using_index);
2313 if (!per_cu->v.quick->compunit_symtab)
2314 {
2315 free_cached_comp_units freer (dwarf2_per_objfile);
2316 scoped_restore decrementer = increment_reading_symtab ();
2317 dw2_do_instantiate_symtab (per_cu, skip_partial);
2318 process_cu_includes (dwarf2_per_objfile);
2319 }
2320
2321 return per_cu->v.quick->compunit_symtab;
2322 }
2323
2324 /* See declaration. */
2325
2326 dwarf2_per_cu_data *
2327 dwarf2_per_objfile::get_cutu (int index)
2328 {
2329 if (index >= this->all_comp_units.size ())
2330 {
2331 index -= this->all_comp_units.size ();
2332 gdb_assert (index < this->all_type_units.size ());
2333 return &this->all_type_units[index]->per_cu;
2334 }
2335
2336 return this->all_comp_units[index];
2337 }
2338
2339 /* See declaration. */
2340
2341 dwarf2_per_cu_data *
2342 dwarf2_per_objfile::get_cu (int index)
2343 {
2344 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2345
2346 return this->all_comp_units[index];
2347 }
2348
2349 /* See declaration. */
2350
2351 signatured_type *
2352 dwarf2_per_objfile::get_tu (int index)
2353 {
2354 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2355
2356 return this->all_type_units[index];
2357 }
2358
2359 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2360 objfile_obstack, and constructed with the specified field
2361 values. */
2362
2363 static dwarf2_per_cu_data *
2364 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2365 struct dwarf2_section_info *section,
2366 int is_dwz,
2367 sect_offset sect_off, ULONGEST length)
2368 {
2369 struct objfile *objfile = dwarf2_per_objfile->objfile;
2370 dwarf2_per_cu_data *the_cu
2371 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2372 struct dwarf2_per_cu_data);
2373 the_cu->sect_off = sect_off;
2374 the_cu->length = length;
2375 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2376 the_cu->section = section;
2377 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2378 struct dwarf2_per_cu_quick_data);
2379 the_cu->is_dwz = is_dwz;
2380 return the_cu;
2381 }
2382
2383 /* A helper for create_cus_from_index that handles a given list of
2384 CUs. */
2385
2386 static void
2387 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2388 const gdb_byte *cu_list, offset_type n_elements,
2389 struct dwarf2_section_info *section,
2390 int is_dwz)
2391 {
2392 for (offset_type i = 0; i < n_elements; i += 2)
2393 {
2394 gdb_static_assert (sizeof (ULONGEST) >= 8);
2395
2396 sect_offset sect_off
2397 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2398 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2399 cu_list += 2 * 8;
2400
2401 dwarf2_per_cu_data *per_cu
2402 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2403 sect_off, length);
2404 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2405 }
2406 }
2407
2408 /* Read the CU list from the mapped index, and use it to create all
2409 the CU objects for this objfile. */
2410
2411 static void
2412 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2413 const gdb_byte *cu_list, offset_type cu_list_elements,
2414 const gdb_byte *dwz_list, offset_type dwz_elements)
2415 {
2416 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2417 dwarf2_per_objfile->all_comp_units.reserve
2418 ((cu_list_elements + dwz_elements) / 2);
2419
2420 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2421 &dwarf2_per_objfile->info, 0);
2422
2423 if (dwz_elements == 0)
2424 return;
2425
2426 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2427 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2428 &dwz->info, 1);
2429 }
2430
2431 /* Create the signatured type hash table from the index. */
2432
2433 static void
2434 create_signatured_type_table_from_index
2435 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2436 struct dwarf2_section_info *section,
2437 const gdb_byte *bytes,
2438 offset_type elements)
2439 {
2440 struct objfile *objfile = dwarf2_per_objfile->objfile;
2441
2442 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2443 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2444
2445 htab_up sig_types_hash = allocate_signatured_type_table ();
2446
2447 for (offset_type i = 0; i < elements; i += 3)
2448 {
2449 struct signatured_type *sig_type;
2450 ULONGEST signature;
2451 void **slot;
2452 cu_offset type_offset_in_tu;
2453
2454 gdb_static_assert (sizeof (ULONGEST) >= 8);
2455 sect_offset sect_off
2456 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2457 type_offset_in_tu
2458 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2459 BFD_ENDIAN_LITTLE);
2460 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2461 bytes += 3 * 8;
2462
2463 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2464 struct signatured_type);
2465 sig_type->signature = signature;
2466 sig_type->type_offset_in_tu = type_offset_in_tu;
2467 sig_type->per_cu.is_debug_types = 1;
2468 sig_type->per_cu.section = section;
2469 sig_type->per_cu.sect_off = sect_off;
2470 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2471 sig_type->per_cu.v.quick
2472 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2473 struct dwarf2_per_cu_quick_data);
2474
2475 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2476 *slot = sig_type;
2477
2478 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2479 }
2480
2481 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2482 }
2483
2484 /* Create the signatured type hash table from .debug_names. */
2485
2486 static void
2487 create_signatured_type_table_from_debug_names
2488 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2489 const mapped_debug_names &map,
2490 struct dwarf2_section_info *section,
2491 struct dwarf2_section_info *abbrev_section)
2492 {
2493 struct objfile *objfile = dwarf2_per_objfile->objfile;
2494
2495 section->read (objfile);
2496 abbrev_section->read (objfile);
2497
2498 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2499 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2500
2501 htab_up sig_types_hash = allocate_signatured_type_table ();
2502
2503 for (uint32_t i = 0; i < map.tu_count; ++i)
2504 {
2505 struct signatured_type *sig_type;
2506 void **slot;
2507
2508 sect_offset sect_off
2509 = (sect_offset) (extract_unsigned_integer
2510 (map.tu_table_reordered + i * map.offset_size,
2511 map.offset_size,
2512 map.dwarf5_byte_order));
2513
2514 comp_unit_head cu_header;
2515 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2516 abbrev_section,
2517 section->buffer + to_underlying (sect_off),
2518 rcuh_kind::TYPE);
2519
2520 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2521 struct signatured_type);
2522 sig_type->signature = cu_header.signature;
2523 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2524 sig_type->per_cu.is_debug_types = 1;
2525 sig_type->per_cu.section = section;
2526 sig_type->per_cu.sect_off = sect_off;
2527 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2528 sig_type->per_cu.v.quick
2529 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2530 struct dwarf2_per_cu_quick_data);
2531
2532 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2533 *slot = sig_type;
2534
2535 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2536 }
2537
2538 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2539 }
2540
2541 /* Read the address map data from the mapped index, and use it to
2542 populate the objfile's psymtabs_addrmap. */
2543
2544 static void
2545 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2546 struct mapped_index *index)
2547 {
2548 struct objfile *objfile = dwarf2_per_objfile->objfile;
2549 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2550 const gdb_byte *iter, *end;
2551 struct addrmap *mutable_map;
2552 CORE_ADDR baseaddr;
2553
2554 auto_obstack temp_obstack;
2555
2556 mutable_map = addrmap_create_mutable (&temp_obstack);
2557
2558 iter = index->address_table.data ();
2559 end = iter + index->address_table.size ();
2560
2561 baseaddr = objfile->text_section_offset ();
2562
2563 while (iter < end)
2564 {
2565 ULONGEST hi, lo, cu_index;
2566 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2567 iter += 8;
2568 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2569 iter += 8;
2570 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2571 iter += 4;
2572
2573 if (lo > hi)
2574 {
2575 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2576 hex_string (lo), hex_string (hi));
2577 continue;
2578 }
2579
2580 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2581 {
2582 complaint (_(".gdb_index address table has invalid CU number %u"),
2583 (unsigned) cu_index);
2584 continue;
2585 }
2586
2587 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2588 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2589 addrmap_set_empty (mutable_map, lo, hi - 1,
2590 dwarf2_per_objfile->get_cu (cu_index));
2591 }
2592
2593 objfile->partial_symtabs->psymtabs_addrmap
2594 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2595 }
2596
2597 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2598 populate the objfile's psymtabs_addrmap. */
2599
2600 static void
2601 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2602 struct dwarf2_section_info *section)
2603 {
2604 struct objfile *objfile = dwarf2_per_objfile->objfile;
2605 bfd *abfd = objfile->obfd;
2606 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2607 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2608
2609 auto_obstack temp_obstack;
2610 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2611
2612 std::unordered_map<sect_offset,
2613 dwarf2_per_cu_data *,
2614 gdb::hash_enum<sect_offset>>
2615 debug_info_offset_to_per_cu;
2616 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2617 {
2618 const auto insertpair
2619 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2620 if (!insertpair.second)
2621 {
2622 warning (_("Section .debug_aranges in %s has duplicate "
2623 "debug_info_offset %s, ignoring .debug_aranges."),
2624 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2625 return;
2626 }
2627 }
2628
2629 section->read (objfile);
2630
2631 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2632
2633 const gdb_byte *addr = section->buffer;
2634
2635 while (addr < section->buffer + section->size)
2636 {
2637 const gdb_byte *const entry_addr = addr;
2638 unsigned int bytes_read;
2639
2640 const LONGEST entry_length = read_initial_length (abfd, addr,
2641 &bytes_read);
2642 addr += bytes_read;
2643
2644 const gdb_byte *const entry_end = addr + entry_length;
2645 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2646 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2647 if (addr + entry_length > section->buffer + section->size)
2648 {
2649 warning (_("Section .debug_aranges in %s entry at offset %s "
2650 "length %s exceeds section length %s, "
2651 "ignoring .debug_aranges."),
2652 objfile_name (objfile),
2653 plongest (entry_addr - section->buffer),
2654 plongest (bytes_read + entry_length),
2655 pulongest (section->size));
2656 return;
2657 }
2658
2659 /* The version number. */
2660 const uint16_t version = read_2_bytes (abfd, addr);
2661 addr += 2;
2662 if (version != 2)
2663 {
2664 warning (_("Section .debug_aranges in %s entry at offset %s "
2665 "has unsupported version %d, ignoring .debug_aranges."),
2666 objfile_name (objfile),
2667 plongest (entry_addr - section->buffer), version);
2668 return;
2669 }
2670
2671 const uint64_t debug_info_offset
2672 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2673 addr += offset_size;
2674 const auto per_cu_it
2675 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2676 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2677 {
2678 warning (_("Section .debug_aranges in %s entry at offset %s "
2679 "debug_info_offset %s does not exists, "
2680 "ignoring .debug_aranges."),
2681 objfile_name (objfile),
2682 plongest (entry_addr - section->buffer),
2683 pulongest (debug_info_offset));
2684 return;
2685 }
2686 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2687
2688 const uint8_t address_size = *addr++;
2689 if (address_size < 1 || address_size > 8)
2690 {
2691 warning (_("Section .debug_aranges in %s entry at offset %s "
2692 "address_size %u is invalid, ignoring .debug_aranges."),
2693 objfile_name (objfile),
2694 plongest (entry_addr - section->buffer), address_size);
2695 return;
2696 }
2697
2698 const uint8_t segment_selector_size = *addr++;
2699 if (segment_selector_size != 0)
2700 {
2701 warning (_("Section .debug_aranges in %s entry at offset %s "
2702 "segment_selector_size %u is not supported, "
2703 "ignoring .debug_aranges."),
2704 objfile_name (objfile),
2705 plongest (entry_addr - section->buffer),
2706 segment_selector_size);
2707 return;
2708 }
2709
2710 /* Must pad to an alignment boundary that is twice the address
2711 size. It is undocumented by the DWARF standard but GCC does
2712 use it. */
2713 for (size_t padding = ((-(addr - section->buffer))
2714 & (2 * address_size - 1));
2715 padding > 0; padding--)
2716 if (*addr++ != 0)
2717 {
2718 warning (_("Section .debug_aranges in %s entry at offset %s "
2719 "padding is not zero, ignoring .debug_aranges."),
2720 objfile_name (objfile),
2721 plongest (entry_addr - section->buffer));
2722 return;
2723 }
2724
2725 for (;;)
2726 {
2727 if (addr + 2 * address_size > entry_end)
2728 {
2729 warning (_("Section .debug_aranges in %s entry at offset %s "
2730 "address list is not properly terminated, "
2731 "ignoring .debug_aranges."),
2732 objfile_name (objfile),
2733 plongest (entry_addr - section->buffer));
2734 return;
2735 }
2736 ULONGEST start = extract_unsigned_integer (addr, address_size,
2737 dwarf5_byte_order);
2738 addr += address_size;
2739 ULONGEST length = extract_unsigned_integer (addr, address_size,
2740 dwarf5_byte_order);
2741 addr += address_size;
2742 if (start == 0 && length == 0)
2743 break;
2744 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2745 {
2746 /* Symbol was eliminated due to a COMDAT group. */
2747 continue;
2748 }
2749 ULONGEST end = start + length;
2750 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2751 - baseaddr);
2752 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2753 - baseaddr);
2754 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2755 }
2756 }
2757
2758 objfile->partial_symtabs->psymtabs_addrmap
2759 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2760 }
2761
2762 /* Find a slot in the mapped index INDEX for the object named NAME.
2763 If NAME is found, set *VEC_OUT to point to the CU vector in the
2764 constant pool and return true. If NAME cannot be found, return
2765 false. */
2766
2767 static bool
2768 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2769 offset_type **vec_out)
2770 {
2771 offset_type hash;
2772 offset_type slot, step;
2773 int (*cmp) (const char *, const char *);
2774
2775 gdb::unique_xmalloc_ptr<char> without_params;
2776 if (current_language->la_language == language_cplus
2777 || current_language->la_language == language_fortran
2778 || current_language->la_language == language_d)
2779 {
2780 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2781 not contain any. */
2782
2783 if (strchr (name, '(') != NULL)
2784 {
2785 without_params = cp_remove_params (name);
2786
2787 if (without_params != NULL)
2788 name = without_params.get ();
2789 }
2790 }
2791
2792 /* Index version 4 did not support case insensitive searches. But the
2793 indices for case insensitive languages are built in lowercase, therefore
2794 simulate our NAME being searched is also lowercased. */
2795 hash = mapped_index_string_hash ((index->version == 4
2796 && case_sensitivity == case_sensitive_off
2797 ? 5 : index->version),
2798 name);
2799
2800 slot = hash & (index->symbol_table.size () - 1);
2801 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2802 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2803
2804 for (;;)
2805 {
2806 const char *str;
2807
2808 const auto &bucket = index->symbol_table[slot];
2809 if (bucket.name == 0 && bucket.vec == 0)
2810 return false;
2811
2812 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2813 if (!cmp (name, str))
2814 {
2815 *vec_out = (offset_type *) (index->constant_pool
2816 + MAYBE_SWAP (bucket.vec));
2817 return true;
2818 }
2819
2820 slot = (slot + step) & (index->symbol_table.size () - 1);
2821 }
2822 }
2823
2824 /* A helper function that reads the .gdb_index from BUFFER and fills
2825 in MAP. FILENAME is the name of the file containing the data;
2826 it is used for error reporting. DEPRECATED_OK is true if it is
2827 ok to use deprecated sections.
2828
2829 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2830 out parameters that are filled in with information about the CU and
2831 TU lists in the section.
2832
2833 Returns true if all went well, false otherwise. */
2834
2835 static bool
2836 read_gdb_index_from_buffer (struct objfile *objfile,
2837 const char *filename,
2838 bool deprecated_ok,
2839 gdb::array_view<const gdb_byte> buffer,
2840 struct mapped_index *map,
2841 const gdb_byte **cu_list,
2842 offset_type *cu_list_elements,
2843 const gdb_byte **types_list,
2844 offset_type *types_list_elements)
2845 {
2846 const gdb_byte *addr = &buffer[0];
2847
2848 /* Version check. */
2849 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2850 /* Versions earlier than 3 emitted every copy of a psymbol. This
2851 causes the index to behave very poorly for certain requests. Version 3
2852 contained incomplete addrmap. So, it seems better to just ignore such
2853 indices. */
2854 if (version < 4)
2855 {
2856 static int warning_printed = 0;
2857 if (!warning_printed)
2858 {
2859 warning (_("Skipping obsolete .gdb_index section in %s."),
2860 filename);
2861 warning_printed = 1;
2862 }
2863 return 0;
2864 }
2865 /* Index version 4 uses a different hash function than index version
2866 5 and later.
2867
2868 Versions earlier than 6 did not emit psymbols for inlined
2869 functions. Using these files will cause GDB not to be able to
2870 set breakpoints on inlined functions by name, so we ignore these
2871 indices unless the user has done
2872 "set use-deprecated-index-sections on". */
2873 if (version < 6 && !deprecated_ok)
2874 {
2875 static int warning_printed = 0;
2876 if (!warning_printed)
2877 {
2878 warning (_("\
2879 Skipping deprecated .gdb_index section in %s.\n\
2880 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2881 to use the section anyway."),
2882 filename);
2883 warning_printed = 1;
2884 }
2885 return 0;
2886 }
2887 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2888 of the TU (for symbols coming from TUs),
2889 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2890 Plus gold-generated indices can have duplicate entries for global symbols,
2891 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2892 These are just performance bugs, and we can't distinguish gdb-generated
2893 indices from gold-generated ones, so issue no warning here. */
2894
2895 /* Indexes with higher version than the one supported by GDB may be no
2896 longer backward compatible. */
2897 if (version > 8)
2898 return 0;
2899
2900 map->version = version;
2901
2902 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2903
2904 int i = 0;
2905 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2906 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2907 / 8);
2908 ++i;
2909
2910 *types_list = addr + MAYBE_SWAP (metadata[i]);
2911 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2912 - MAYBE_SWAP (metadata[i]))
2913 / 8);
2914 ++i;
2915
2916 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2917 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2918 map->address_table
2919 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2920 ++i;
2921
2922 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2923 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2924 map->symbol_table
2925 = gdb::array_view<mapped_index::symbol_table_slot>
2926 ((mapped_index::symbol_table_slot *) symbol_table,
2927 (mapped_index::symbol_table_slot *) symbol_table_end);
2928
2929 ++i;
2930 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2931
2932 return 1;
2933 }
2934
2935 /* Callback types for dwarf2_read_gdb_index. */
2936
2937 typedef gdb::function_view
2938 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2939 get_gdb_index_contents_ftype;
2940 typedef gdb::function_view
2941 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2942 get_gdb_index_contents_dwz_ftype;
2943
2944 /* Read .gdb_index. If everything went ok, initialize the "quick"
2945 elements of all the CUs and return 1. Otherwise, return 0. */
2946
2947 static int
2948 dwarf2_read_gdb_index
2949 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2950 get_gdb_index_contents_ftype get_gdb_index_contents,
2951 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2952 {
2953 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2954 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2955 struct dwz_file *dwz;
2956 struct objfile *objfile = dwarf2_per_objfile->objfile;
2957
2958 gdb::array_view<const gdb_byte> main_index_contents
2959 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2960
2961 if (main_index_contents.empty ())
2962 return 0;
2963
2964 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2965 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2966 use_deprecated_index_sections,
2967 main_index_contents, map.get (), &cu_list,
2968 &cu_list_elements, &types_list,
2969 &types_list_elements))
2970 return 0;
2971
2972 /* Don't use the index if it's empty. */
2973 if (map->symbol_table.empty ())
2974 return 0;
2975
2976 /* If there is a .dwz file, read it so we can get its CU list as
2977 well. */
2978 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2979 if (dwz != NULL)
2980 {
2981 struct mapped_index dwz_map;
2982 const gdb_byte *dwz_types_ignore;
2983 offset_type dwz_types_elements_ignore;
2984
2985 gdb::array_view<const gdb_byte> dwz_index_content
2986 = get_gdb_index_contents_dwz (objfile, dwz);
2987
2988 if (dwz_index_content.empty ())
2989 return 0;
2990
2991 if (!read_gdb_index_from_buffer (objfile,
2992 bfd_get_filename (dwz->dwz_bfd.get ()),
2993 1, dwz_index_content, &dwz_map,
2994 &dwz_list, &dwz_list_elements,
2995 &dwz_types_ignore,
2996 &dwz_types_elements_ignore))
2997 {
2998 warning (_("could not read '.gdb_index' section from %s; skipping"),
2999 bfd_get_filename (dwz->dwz_bfd.get ()));
3000 return 0;
3001 }
3002 }
3003
3004 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3005 dwz_list, dwz_list_elements);
3006
3007 if (types_list_elements)
3008 {
3009 /* We can only handle a single .debug_types when we have an
3010 index. */
3011 if (dwarf2_per_objfile->types.size () != 1)
3012 return 0;
3013
3014 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3015
3016 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3017 types_list, types_list_elements);
3018 }
3019
3020 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3021
3022 dwarf2_per_objfile->index_table = std::move (map);
3023 dwarf2_per_objfile->using_index = 1;
3024 dwarf2_per_objfile->quick_file_names_table =
3025 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3026
3027 return 1;
3028 }
3029
3030 /* die_reader_func for dw2_get_file_names. */
3031
3032 static void
3033 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3034 const gdb_byte *info_ptr,
3035 struct die_info *comp_unit_die)
3036 {
3037 struct dwarf2_cu *cu = reader->cu;
3038 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3039 struct dwarf2_per_objfile *dwarf2_per_objfile
3040 = cu->per_cu->dwarf2_per_objfile;
3041 struct objfile *objfile = dwarf2_per_objfile->objfile;
3042 struct dwarf2_per_cu_data *lh_cu;
3043 struct attribute *attr;
3044 void **slot;
3045 struct quick_file_names *qfn;
3046
3047 gdb_assert (! this_cu->is_debug_types);
3048
3049 /* Our callers never want to match partial units -- instead they
3050 will match the enclosing full CU. */
3051 if (comp_unit_die->tag == DW_TAG_partial_unit)
3052 {
3053 this_cu->v.quick->no_file_data = 1;
3054 return;
3055 }
3056
3057 lh_cu = this_cu;
3058 slot = NULL;
3059
3060 line_header_up lh;
3061 sect_offset line_offset {};
3062
3063 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3064 if (attr != nullptr)
3065 {
3066 struct quick_file_names find_entry;
3067
3068 line_offset = (sect_offset) DW_UNSND (attr);
3069
3070 /* We may have already read in this line header (TU line header sharing).
3071 If we have we're done. */
3072 find_entry.hash.dwo_unit = cu->dwo_unit;
3073 find_entry.hash.line_sect_off = line_offset;
3074 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3075 &find_entry, INSERT);
3076 if (*slot != NULL)
3077 {
3078 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3079 return;
3080 }
3081
3082 lh = dwarf_decode_line_header (line_offset, cu);
3083 }
3084 if (lh == NULL)
3085 {
3086 lh_cu->v.quick->no_file_data = 1;
3087 return;
3088 }
3089
3090 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3091 qfn->hash.dwo_unit = cu->dwo_unit;
3092 qfn->hash.line_sect_off = line_offset;
3093 gdb_assert (slot != NULL);
3094 *slot = qfn;
3095
3096 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3097
3098 int offset = 0;
3099 if (strcmp (fnd.name, "<unknown>") != 0)
3100 ++offset;
3101
3102 qfn->num_file_names = offset + lh->file_names_size ();
3103 qfn->file_names =
3104 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3105 if (offset != 0)
3106 qfn->file_names[0] = xstrdup (fnd.name);
3107 for (int i = 0; i < lh->file_names_size (); ++i)
3108 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3109 fnd.comp_dir).release ();
3110 qfn->real_names = NULL;
3111
3112 lh_cu->v.quick->file_names = qfn;
3113 }
3114
3115 /* A helper for the "quick" functions which attempts to read the line
3116 table for THIS_CU. */
3117
3118 static struct quick_file_names *
3119 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3120 {
3121 /* This should never be called for TUs. */
3122 gdb_assert (! this_cu->is_debug_types);
3123 /* Nor type unit groups. */
3124 gdb_assert (! this_cu->type_unit_group_p ());
3125
3126 if (this_cu->v.quick->file_names != NULL)
3127 return this_cu->v.quick->file_names;
3128 /* If we know there is no line data, no point in looking again. */
3129 if (this_cu->v.quick->no_file_data)
3130 return NULL;
3131
3132 cutu_reader reader (this_cu);
3133 if (!reader.dummy_p)
3134 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3135
3136 if (this_cu->v.quick->no_file_data)
3137 return NULL;
3138 return this_cu->v.quick->file_names;
3139 }
3140
3141 /* A helper for the "quick" functions which computes and caches the
3142 real path for a given file name from the line table. */
3143
3144 static const char *
3145 dw2_get_real_path (struct objfile *objfile,
3146 struct quick_file_names *qfn, int index)
3147 {
3148 if (qfn->real_names == NULL)
3149 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3150 qfn->num_file_names, const char *);
3151
3152 if (qfn->real_names[index] == NULL)
3153 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3154
3155 return qfn->real_names[index];
3156 }
3157
3158 static struct symtab *
3159 dw2_find_last_source_symtab (struct objfile *objfile)
3160 {
3161 struct dwarf2_per_objfile *dwarf2_per_objfile
3162 = get_dwarf2_per_objfile (objfile);
3163 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3164 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3165
3166 if (cust == NULL)
3167 return NULL;
3168
3169 return compunit_primary_filetab (cust);
3170 }
3171
3172 /* Traversal function for dw2_forget_cached_source_info. */
3173
3174 static int
3175 dw2_free_cached_file_names (void **slot, void *info)
3176 {
3177 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3178
3179 if (file_data->real_names)
3180 {
3181 int i;
3182
3183 for (i = 0; i < file_data->num_file_names; ++i)
3184 {
3185 xfree ((void*) file_data->real_names[i]);
3186 file_data->real_names[i] = NULL;
3187 }
3188 }
3189
3190 return 1;
3191 }
3192
3193 static void
3194 dw2_forget_cached_source_info (struct objfile *objfile)
3195 {
3196 struct dwarf2_per_objfile *dwarf2_per_objfile
3197 = get_dwarf2_per_objfile (objfile);
3198
3199 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3200 dw2_free_cached_file_names, NULL);
3201 }
3202
3203 /* Helper function for dw2_map_symtabs_matching_filename that expands
3204 the symtabs and calls the iterator. */
3205
3206 static int
3207 dw2_map_expand_apply (struct objfile *objfile,
3208 struct dwarf2_per_cu_data *per_cu,
3209 const char *name, const char *real_path,
3210 gdb::function_view<bool (symtab *)> callback)
3211 {
3212 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3213
3214 /* Don't visit already-expanded CUs. */
3215 if (per_cu->v.quick->compunit_symtab)
3216 return 0;
3217
3218 /* This may expand more than one symtab, and we want to iterate over
3219 all of them. */
3220 dw2_instantiate_symtab (per_cu, false);
3221
3222 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3223 last_made, callback);
3224 }
3225
3226 /* Implementation of the map_symtabs_matching_filename method. */
3227
3228 static bool
3229 dw2_map_symtabs_matching_filename
3230 (struct objfile *objfile, const char *name, const char *real_path,
3231 gdb::function_view<bool (symtab *)> callback)
3232 {
3233 const char *name_basename = lbasename (name);
3234 struct dwarf2_per_objfile *dwarf2_per_objfile
3235 = get_dwarf2_per_objfile (objfile);
3236
3237 /* The rule is CUs specify all the files, including those used by
3238 any TU, so there's no need to scan TUs here. */
3239
3240 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3241 {
3242 /* We only need to look at symtabs not already expanded. */
3243 if (per_cu->v.quick->compunit_symtab)
3244 continue;
3245
3246 quick_file_names *file_data = dw2_get_file_names (per_cu);
3247 if (file_data == NULL)
3248 continue;
3249
3250 for (int j = 0; j < file_data->num_file_names; ++j)
3251 {
3252 const char *this_name = file_data->file_names[j];
3253 const char *this_real_name;
3254
3255 if (compare_filenames_for_search (this_name, name))
3256 {
3257 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3258 callback))
3259 return true;
3260 continue;
3261 }
3262
3263 /* Before we invoke realpath, which can get expensive when many
3264 files are involved, do a quick comparison of the basenames. */
3265 if (! basenames_may_differ
3266 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3267 continue;
3268
3269 this_real_name = dw2_get_real_path (objfile, file_data, j);
3270 if (compare_filenames_for_search (this_real_name, name))
3271 {
3272 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3273 callback))
3274 return true;
3275 continue;
3276 }
3277
3278 if (real_path != NULL)
3279 {
3280 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3281 gdb_assert (IS_ABSOLUTE_PATH (name));
3282 if (this_real_name != NULL
3283 && FILENAME_CMP (real_path, this_real_name) == 0)
3284 {
3285 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3286 callback))
3287 return true;
3288 continue;
3289 }
3290 }
3291 }
3292 }
3293
3294 return false;
3295 }
3296
3297 /* Struct used to manage iterating over all CUs looking for a symbol. */
3298
3299 struct dw2_symtab_iterator
3300 {
3301 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3302 struct dwarf2_per_objfile *dwarf2_per_objfile;
3303 /* If set, only look for symbols that match that block. Valid values are
3304 GLOBAL_BLOCK and STATIC_BLOCK. */
3305 gdb::optional<block_enum> block_index;
3306 /* The kind of symbol we're looking for. */
3307 domain_enum domain;
3308 /* The list of CUs from the index entry of the symbol,
3309 or NULL if not found. */
3310 offset_type *vec;
3311 /* The next element in VEC to look at. */
3312 int next;
3313 /* The number of elements in VEC, or zero if there is no match. */
3314 int length;
3315 /* Have we seen a global version of the symbol?
3316 If so we can ignore all further global instances.
3317 This is to work around gold/15646, inefficient gold-generated
3318 indices. */
3319 int global_seen;
3320 };
3321
3322 /* Initialize the index symtab iterator ITER. */
3323
3324 static void
3325 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3326 struct dwarf2_per_objfile *dwarf2_per_objfile,
3327 gdb::optional<block_enum> block_index,
3328 domain_enum domain,
3329 const char *name)
3330 {
3331 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3332 iter->block_index = block_index;
3333 iter->domain = domain;
3334 iter->next = 0;
3335 iter->global_seen = 0;
3336
3337 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3338
3339 /* index is NULL if OBJF_READNOW. */
3340 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3341 iter->length = MAYBE_SWAP (*iter->vec);
3342 else
3343 {
3344 iter->vec = NULL;
3345 iter->length = 0;
3346 }
3347 }
3348
3349 /* Return the next matching CU or NULL if there are no more. */
3350
3351 static struct dwarf2_per_cu_data *
3352 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3353 {
3354 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3355
3356 for ( ; iter->next < iter->length; ++iter->next)
3357 {
3358 offset_type cu_index_and_attrs =
3359 MAYBE_SWAP (iter->vec[iter->next + 1]);
3360 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3361 gdb_index_symbol_kind symbol_kind =
3362 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3363 /* Only check the symbol attributes if they're present.
3364 Indices prior to version 7 don't record them,
3365 and indices >= 7 may elide them for certain symbols
3366 (gold does this). */
3367 int attrs_valid =
3368 (dwarf2_per_objfile->index_table->version >= 7
3369 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3370
3371 /* Don't crash on bad data. */
3372 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3373 + dwarf2_per_objfile->all_type_units.size ()))
3374 {
3375 complaint (_(".gdb_index entry has bad CU index"
3376 " [in module %s]"),
3377 objfile_name (dwarf2_per_objfile->objfile));
3378 continue;
3379 }
3380
3381 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3382
3383 /* Skip if already read in. */
3384 if (per_cu->v.quick->compunit_symtab)
3385 continue;
3386
3387 /* Check static vs global. */
3388 if (attrs_valid)
3389 {
3390 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3391
3392 if (iter->block_index.has_value ())
3393 {
3394 bool want_static = *iter->block_index == STATIC_BLOCK;
3395
3396 if (is_static != want_static)
3397 continue;
3398 }
3399
3400 /* Work around gold/15646. */
3401 if (!is_static && iter->global_seen)
3402 continue;
3403 if (!is_static)
3404 iter->global_seen = 1;
3405 }
3406
3407 /* Only check the symbol's kind if it has one. */
3408 if (attrs_valid)
3409 {
3410 switch (iter->domain)
3411 {
3412 case VAR_DOMAIN:
3413 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3414 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3415 /* Some types are also in VAR_DOMAIN. */
3416 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3417 continue;
3418 break;
3419 case STRUCT_DOMAIN:
3420 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3421 continue;
3422 break;
3423 case LABEL_DOMAIN:
3424 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3425 continue;
3426 break;
3427 case MODULE_DOMAIN:
3428 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3429 continue;
3430 break;
3431 default:
3432 break;
3433 }
3434 }
3435
3436 ++iter->next;
3437 return per_cu;
3438 }
3439
3440 return NULL;
3441 }
3442
3443 static struct compunit_symtab *
3444 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3445 const char *name, domain_enum domain)
3446 {
3447 struct compunit_symtab *stab_best = NULL;
3448 struct dwarf2_per_objfile *dwarf2_per_objfile
3449 = get_dwarf2_per_objfile (objfile);
3450
3451 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3452
3453 struct dw2_symtab_iterator iter;
3454 struct dwarf2_per_cu_data *per_cu;
3455
3456 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3457
3458 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3459 {
3460 struct symbol *sym, *with_opaque = NULL;
3461 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3462 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3463 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3464
3465 sym = block_find_symbol (block, name, domain,
3466 block_find_non_opaque_type_preferred,
3467 &with_opaque);
3468
3469 /* Some caution must be observed with overloaded functions
3470 and methods, since the index will not contain any overload
3471 information (but NAME might contain it). */
3472
3473 if (sym != NULL
3474 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3475 return stab;
3476 if (with_opaque != NULL
3477 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3478 stab_best = stab;
3479
3480 /* Keep looking through other CUs. */
3481 }
3482
3483 return stab_best;
3484 }
3485
3486 static void
3487 dw2_print_stats (struct objfile *objfile)
3488 {
3489 struct dwarf2_per_objfile *dwarf2_per_objfile
3490 = get_dwarf2_per_objfile (objfile);
3491 int total = (dwarf2_per_objfile->all_comp_units.size ()
3492 + dwarf2_per_objfile->all_type_units.size ());
3493 int count = 0;
3494
3495 for (int i = 0; i < total; ++i)
3496 {
3497 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3498
3499 if (!per_cu->v.quick->compunit_symtab)
3500 ++count;
3501 }
3502 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3503 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3504 }
3505
3506 /* This dumps minimal information about the index.
3507 It is called via "mt print objfiles".
3508 One use is to verify .gdb_index has been loaded by the
3509 gdb.dwarf2/gdb-index.exp testcase. */
3510
3511 static void
3512 dw2_dump (struct objfile *objfile)
3513 {
3514 struct dwarf2_per_objfile *dwarf2_per_objfile
3515 = get_dwarf2_per_objfile (objfile);
3516
3517 gdb_assert (dwarf2_per_objfile->using_index);
3518 printf_filtered (".gdb_index:");
3519 if (dwarf2_per_objfile->index_table != NULL)
3520 {
3521 printf_filtered (" version %d\n",
3522 dwarf2_per_objfile->index_table->version);
3523 }
3524 else
3525 printf_filtered (" faked for \"readnow\"\n");
3526 printf_filtered ("\n");
3527 }
3528
3529 static void
3530 dw2_expand_symtabs_for_function (struct objfile *objfile,
3531 const char *func_name)
3532 {
3533 struct dwarf2_per_objfile *dwarf2_per_objfile
3534 = get_dwarf2_per_objfile (objfile);
3535
3536 struct dw2_symtab_iterator iter;
3537 struct dwarf2_per_cu_data *per_cu;
3538
3539 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3540
3541 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3542 dw2_instantiate_symtab (per_cu, false);
3543
3544 }
3545
3546 static void
3547 dw2_expand_all_symtabs (struct objfile *objfile)
3548 {
3549 struct dwarf2_per_objfile *dwarf2_per_objfile
3550 = get_dwarf2_per_objfile (objfile);
3551 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3552 + dwarf2_per_objfile->all_type_units.size ());
3553
3554 for (int i = 0; i < total_units; ++i)
3555 {
3556 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3557
3558 /* We don't want to directly expand a partial CU, because if we
3559 read it with the wrong language, then assertion failures can
3560 be triggered later on. See PR symtab/23010. So, tell
3561 dw2_instantiate_symtab to skip partial CUs -- any important
3562 partial CU will be read via DW_TAG_imported_unit anyway. */
3563 dw2_instantiate_symtab (per_cu, true);
3564 }
3565 }
3566
3567 static void
3568 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3569 const char *fullname)
3570 {
3571 struct dwarf2_per_objfile *dwarf2_per_objfile
3572 = get_dwarf2_per_objfile (objfile);
3573
3574 /* We don't need to consider type units here.
3575 This is only called for examining code, e.g. expand_line_sal.
3576 There can be an order of magnitude (or more) more type units
3577 than comp units, and we avoid them if we can. */
3578
3579 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3580 {
3581 /* We only need to look at symtabs not already expanded. */
3582 if (per_cu->v.quick->compunit_symtab)
3583 continue;
3584
3585 quick_file_names *file_data = dw2_get_file_names (per_cu);
3586 if (file_data == NULL)
3587 continue;
3588
3589 for (int j = 0; j < file_data->num_file_names; ++j)
3590 {
3591 const char *this_fullname = file_data->file_names[j];
3592
3593 if (filename_cmp (this_fullname, fullname) == 0)
3594 {
3595 dw2_instantiate_symtab (per_cu, false);
3596 break;
3597 }
3598 }
3599 }
3600 }
3601
3602 static void
3603 dw2_map_matching_symbols
3604 (struct objfile *objfile,
3605 const lookup_name_info &name, domain_enum domain,
3606 int global,
3607 gdb::function_view<symbol_found_callback_ftype> callback,
3608 symbol_compare_ftype *ordered_compare)
3609 {
3610 /* Currently unimplemented; used for Ada. The function can be called if the
3611 current language is Ada for a non-Ada objfile using GNU index. As Ada
3612 does not look for non-Ada symbols this function should just return. */
3613 }
3614
3615 /* Starting from a search name, return the string that finds the upper
3616 bound of all strings that start with SEARCH_NAME in a sorted name
3617 list. Returns the empty string to indicate that the upper bound is
3618 the end of the list. */
3619
3620 static std::string
3621 make_sort_after_prefix_name (const char *search_name)
3622 {
3623 /* When looking to complete "func", we find the upper bound of all
3624 symbols that start with "func" by looking for where we'd insert
3625 the closest string that would follow "func" in lexicographical
3626 order. Usually, that's "func"-with-last-character-incremented,
3627 i.e. "fund". Mind non-ASCII characters, though. Usually those
3628 will be UTF-8 multi-byte sequences, but we can't be certain.
3629 Especially mind the 0xff character, which is a valid character in
3630 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3631 rule out compilers allowing it in identifiers. Note that
3632 conveniently, strcmp/strcasecmp are specified to compare
3633 characters interpreted as unsigned char. So what we do is treat
3634 the whole string as a base 256 number composed of a sequence of
3635 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3636 to 0, and carries 1 to the following more-significant position.
3637 If the very first character in SEARCH_NAME ends up incremented
3638 and carries/overflows, then the upper bound is the end of the
3639 list. The string after the empty string is also the empty
3640 string.
3641
3642 Some examples of this operation:
3643
3644 SEARCH_NAME => "+1" RESULT
3645
3646 "abc" => "abd"
3647 "ab\xff" => "ac"
3648 "\xff" "a" "\xff" => "\xff" "b"
3649 "\xff" => ""
3650 "\xff\xff" => ""
3651 "" => ""
3652
3653 Then, with these symbols for example:
3654
3655 func
3656 func1
3657 fund
3658
3659 completing "func" looks for symbols between "func" and
3660 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3661 which finds "func" and "func1", but not "fund".
3662
3663 And with:
3664
3665 funcÿ (Latin1 'ÿ' [0xff])
3666 funcÿ1
3667 fund
3668
3669 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3670 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3671
3672 And with:
3673
3674 ÿÿ (Latin1 'ÿ' [0xff])
3675 ÿÿ1
3676
3677 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3678 the end of the list.
3679 */
3680 std::string after = search_name;
3681 while (!after.empty () && (unsigned char) after.back () == 0xff)
3682 after.pop_back ();
3683 if (!after.empty ())
3684 after.back () = (unsigned char) after.back () + 1;
3685 return after;
3686 }
3687
3688 /* See declaration. */
3689
3690 std::pair<std::vector<name_component>::const_iterator,
3691 std::vector<name_component>::const_iterator>
3692 mapped_index_base::find_name_components_bounds
3693 (const lookup_name_info &lookup_name_without_params, language lang) const
3694 {
3695 auto *name_cmp
3696 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3697
3698 const char *lang_name
3699 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3700
3701 /* Comparison function object for lower_bound that matches against a
3702 given symbol name. */
3703 auto lookup_compare_lower = [&] (const name_component &elem,
3704 const char *name)
3705 {
3706 const char *elem_qualified = this->symbol_name_at (elem.idx);
3707 const char *elem_name = elem_qualified + elem.name_offset;
3708 return name_cmp (elem_name, name) < 0;
3709 };
3710
3711 /* Comparison function object for upper_bound that matches against a
3712 given symbol name. */
3713 auto lookup_compare_upper = [&] (const char *name,
3714 const name_component &elem)
3715 {
3716 const char *elem_qualified = this->symbol_name_at (elem.idx);
3717 const char *elem_name = elem_qualified + elem.name_offset;
3718 return name_cmp (name, elem_name) < 0;
3719 };
3720
3721 auto begin = this->name_components.begin ();
3722 auto end = this->name_components.end ();
3723
3724 /* Find the lower bound. */
3725 auto lower = [&] ()
3726 {
3727 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3728 return begin;
3729 else
3730 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3731 } ();
3732
3733 /* Find the upper bound. */
3734 auto upper = [&] ()
3735 {
3736 if (lookup_name_without_params.completion_mode ())
3737 {
3738 /* In completion mode, we want UPPER to point past all
3739 symbols names that have the same prefix. I.e., with
3740 these symbols, and completing "func":
3741
3742 function << lower bound
3743 function1
3744 other_function << upper bound
3745
3746 We find the upper bound by looking for the insertion
3747 point of "func"-with-last-character-incremented,
3748 i.e. "fund". */
3749 std::string after = make_sort_after_prefix_name (lang_name);
3750 if (after.empty ())
3751 return end;
3752 return std::lower_bound (lower, end, after.c_str (),
3753 lookup_compare_lower);
3754 }
3755 else
3756 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3757 } ();
3758
3759 return {lower, upper};
3760 }
3761
3762 /* See declaration. */
3763
3764 void
3765 mapped_index_base::build_name_components ()
3766 {
3767 if (!this->name_components.empty ())
3768 return;
3769
3770 this->name_components_casing = case_sensitivity;
3771 auto *name_cmp
3772 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3773
3774 /* The code below only knows how to break apart components of C++
3775 symbol names (and other languages that use '::' as
3776 namespace/module separator) and Ada symbol names. */
3777 auto count = this->symbol_name_count ();
3778 for (offset_type idx = 0; idx < count; idx++)
3779 {
3780 if (this->symbol_name_slot_invalid (idx))
3781 continue;
3782
3783 const char *name = this->symbol_name_at (idx);
3784
3785 /* Add each name component to the name component table. */
3786 unsigned int previous_len = 0;
3787
3788 if (strstr (name, "::") != nullptr)
3789 {
3790 for (unsigned int current_len = cp_find_first_component (name);
3791 name[current_len] != '\0';
3792 current_len += cp_find_first_component (name + current_len))
3793 {
3794 gdb_assert (name[current_len] == ':');
3795 this->name_components.push_back ({previous_len, idx});
3796 /* Skip the '::'. */
3797 current_len += 2;
3798 previous_len = current_len;
3799 }
3800 }
3801 else
3802 {
3803 /* Handle the Ada encoded (aka mangled) form here. */
3804 for (const char *iter = strstr (name, "__");
3805 iter != nullptr;
3806 iter = strstr (iter, "__"))
3807 {
3808 this->name_components.push_back ({previous_len, idx});
3809 iter += 2;
3810 previous_len = iter - name;
3811 }
3812 }
3813
3814 this->name_components.push_back ({previous_len, idx});
3815 }
3816
3817 /* Sort name_components elements by name. */
3818 auto name_comp_compare = [&] (const name_component &left,
3819 const name_component &right)
3820 {
3821 const char *left_qualified = this->symbol_name_at (left.idx);
3822 const char *right_qualified = this->symbol_name_at (right.idx);
3823
3824 const char *left_name = left_qualified + left.name_offset;
3825 const char *right_name = right_qualified + right.name_offset;
3826
3827 return name_cmp (left_name, right_name) < 0;
3828 };
3829
3830 std::sort (this->name_components.begin (),
3831 this->name_components.end (),
3832 name_comp_compare);
3833 }
3834
3835 /* Helper for dw2_expand_symtabs_matching that works with a
3836 mapped_index_base instead of the containing objfile. This is split
3837 to a separate function in order to be able to unit test the
3838 name_components matching using a mock mapped_index_base. For each
3839 symbol name that matches, calls MATCH_CALLBACK, passing it the
3840 symbol's index in the mapped_index_base symbol table. */
3841
3842 static void
3843 dw2_expand_symtabs_matching_symbol
3844 (mapped_index_base &index,
3845 const lookup_name_info &lookup_name_in,
3846 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3847 enum search_domain kind,
3848 gdb::function_view<bool (offset_type)> match_callback)
3849 {
3850 lookup_name_info lookup_name_without_params
3851 = lookup_name_in.make_ignore_params ();
3852
3853 /* Build the symbol name component sorted vector, if we haven't
3854 yet. */
3855 index.build_name_components ();
3856
3857 /* The same symbol may appear more than once in the range though.
3858 E.g., if we're looking for symbols that complete "w", and we have
3859 a symbol named "w1::w2", we'll find the two name components for
3860 that same symbol in the range. To be sure we only call the
3861 callback once per symbol, we first collect the symbol name
3862 indexes that matched in a temporary vector and ignore
3863 duplicates. */
3864 std::vector<offset_type> matches;
3865
3866 struct name_and_matcher
3867 {
3868 symbol_name_matcher_ftype *matcher;
3869 const std::string &name;
3870
3871 bool operator== (const name_and_matcher &other) const
3872 {
3873 return matcher == other.matcher && name == other.name;
3874 }
3875 };
3876
3877 /* A vector holding all the different symbol name matchers, for all
3878 languages. */
3879 std::vector<name_and_matcher> matchers;
3880
3881 for (int i = 0; i < nr_languages; i++)
3882 {
3883 enum language lang_e = (enum language) i;
3884
3885 const language_defn *lang = language_def (lang_e);
3886 symbol_name_matcher_ftype *name_matcher
3887 = get_symbol_name_matcher (lang, lookup_name_without_params);
3888
3889 name_and_matcher key {
3890 name_matcher,
3891 lookup_name_without_params.language_lookup_name (lang_e)
3892 };
3893
3894 /* Don't insert the same comparison routine more than once.
3895 Note that we do this linear walk. This is not a problem in
3896 practice because the number of supported languages is
3897 low. */
3898 if (std::find (matchers.begin (), matchers.end (), key)
3899 != matchers.end ())
3900 continue;
3901 matchers.push_back (std::move (key));
3902
3903 auto bounds
3904 = index.find_name_components_bounds (lookup_name_without_params,
3905 lang_e);
3906
3907 /* Now for each symbol name in range, check to see if we have a name
3908 match, and if so, call the MATCH_CALLBACK callback. */
3909
3910 for (; bounds.first != bounds.second; ++bounds.first)
3911 {
3912 const char *qualified = index.symbol_name_at (bounds.first->idx);
3913
3914 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3915 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3916 continue;
3917
3918 matches.push_back (bounds.first->idx);
3919 }
3920 }
3921
3922 std::sort (matches.begin (), matches.end ());
3923
3924 /* Finally call the callback, once per match. */
3925 ULONGEST prev = -1;
3926 for (offset_type idx : matches)
3927 {
3928 if (prev != idx)
3929 {
3930 if (!match_callback (idx))
3931 break;
3932 prev = idx;
3933 }
3934 }
3935
3936 /* Above we use a type wider than idx's for 'prev', since 0 and
3937 (offset_type)-1 are both possible values. */
3938 static_assert (sizeof (prev) > sizeof (offset_type), "");
3939 }
3940
3941 #if GDB_SELF_TEST
3942
3943 namespace selftests { namespace dw2_expand_symtabs_matching {
3944
3945 /* A mock .gdb_index/.debug_names-like name index table, enough to
3946 exercise dw2_expand_symtabs_matching_symbol, which works with the
3947 mapped_index_base interface. Builds an index from the symbol list
3948 passed as parameter to the constructor. */
3949 class mock_mapped_index : public mapped_index_base
3950 {
3951 public:
3952 mock_mapped_index (gdb::array_view<const char *> symbols)
3953 : m_symbol_table (symbols)
3954 {}
3955
3956 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3957
3958 /* Return the number of names in the symbol table. */
3959 size_t symbol_name_count () const override
3960 {
3961 return m_symbol_table.size ();
3962 }
3963
3964 /* Get the name of the symbol at IDX in the symbol table. */
3965 const char *symbol_name_at (offset_type idx) const override
3966 {
3967 return m_symbol_table[idx];
3968 }
3969
3970 private:
3971 gdb::array_view<const char *> m_symbol_table;
3972 };
3973
3974 /* Convenience function that converts a NULL pointer to a "<null>"
3975 string, to pass to print routines. */
3976
3977 static const char *
3978 string_or_null (const char *str)
3979 {
3980 return str != NULL ? str : "<null>";
3981 }
3982
3983 /* Check if a lookup_name_info built from
3984 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3985 index. EXPECTED_LIST is the list of expected matches, in expected
3986 matching order. If no match expected, then an empty list is
3987 specified. Returns true on success. On failure prints a warning
3988 indicating the file:line that failed, and returns false. */
3989
3990 static bool
3991 check_match (const char *file, int line,
3992 mock_mapped_index &mock_index,
3993 const char *name, symbol_name_match_type match_type,
3994 bool completion_mode,
3995 std::initializer_list<const char *> expected_list)
3996 {
3997 lookup_name_info lookup_name (name, match_type, completion_mode);
3998
3999 bool matched = true;
4000
4001 auto mismatch = [&] (const char *expected_str,
4002 const char *got)
4003 {
4004 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4005 "expected=\"%s\", got=\"%s\"\n"),
4006 file, line,
4007 (match_type == symbol_name_match_type::FULL
4008 ? "FULL" : "WILD"),
4009 name, string_or_null (expected_str), string_or_null (got));
4010 matched = false;
4011 };
4012
4013 auto expected_it = expected_list.begin ();
4014 auto expected_end = expected_list.end ();
4015
4016 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4017 NULL, ALL_DOMAIN,
4018 [&] (offset_type idx)
4019 {
4020 const char *matched_name = mock_index.symbol_name_at (idx);
4021 const char *expected_str
4022 = expected_it == expected_end ? NULL : *expected_it++;
4023
4024 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4025 mismatch (expected_str, matched_name);
4026 return true;
4027 });
4028
4029 const char *expected_str
4030 = expected_it == expected_end ? NULL : *expected_it++;
4031 if (expected_str != NULL)
4032 mismatch (expected_str, NULL);
4033
4034 return matched;
4035 }
4036
4037 /* The symbols added to the mock mapped_index for testing (in
4038 canonical form). */
4039 static const char *test_symbols[] = {
4040 "function",
4041 "std::bar",
4042 "std::zfunction",
4043 "std::zfunction2",
4044 "w1::w2",
4045 "ns::foo<char*>",
4046 "ns::foo<int>",
4047 "ns::foo<long>",
4048 "ns2::tmpl<int>::foo2",
4049 "(anonymous namespace)::A::B::C",
4050
4051 /* These are used to check that the increment-last-char in the
4052 matching algorithm for completion doesn't match "t1_fund" when
4053 completing "t1_func". */
4054 "t1_func",
4055 "t1_func1",
4056 "t1_fund",
4057 "t1_fund1",
4058
4059 /* A UTF-8 name with multi-byte sequences to make sure that
4060 cp-name-parser understands this as a single identifier ("função"
4061 is "function" in PT). */
4062 u8"u8função",
4063
4064 /* \377 (0xff) is Latin1 'ÿ'. */
4065 "yfunc\377",
4066
4067 /* \377 (0xff) is Latin1 'ÿ'. */
4068 "\377",
4069 "\377\377123",
4070
4071 /* A name with all sorts of complications. Starts with "z" to make
4072 it easier for the completion tests below. */
4073 #define Z_SYM_NAME \
4074 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4075 "::tuple<(anonymous namespace)::ui*, " \
4076 "std::default_delete<(anonymous namespace)::ui>, void>"
4077
4078 Z_SYM_NAME
4079 };
4080
4081 /* Returns true if the mapped_index_base::find_name_component_bounds
4082 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4083 in completion mode. */
4084
4085 static bool
4086 check_find_bounds_finds (mapped_index_base &index,
4087 const char *search_name,
4088 gdb::array_view<const char *> expected_syms)
4089 {
4090 lookup_name_info lookup_name (search_name,
4091 symbol_name_match_type::FULL, true);
4092
4093 auto bounds = index.find_name_components_bounds (lookup_name,
4094 language_cplus);
4095
4096 size_t distance = std::distance (bounds.first, bounds.second);
4097 if (distance != expected_syms.size ())
4098 return false;
4099
4100 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4101 {
4102 auto nc_elem = bounds.first + exp_elem;
4103 const char *qualified = index.symbol_name_at (nc_elem->idx);
4104 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4105 return false;
4106 }
4107
4108 return true;
4109 }
4110
4111 /* Test the lower-level mapped_index::find_name_component_bounds
4112 method. */
4113
4114 static void
4115 test_mapped_index_find_name_component_bounds ()
4116 {
4117 mock_mapped_index mock_index (test_symbols);
4118
4119 mock_index.build_name_components ();
4120
4121 /* Test the lower-level mapped_index::find_name_component_bounds
4122 method in completion mode. */
4123 {
4124 static const char *expected_syms[] = {
4125 "t1_func",
4126 "t1_func1",
4127 };
4128
4129 SELF_CHECK (check_find_bounds_finds (mock_index,
4130 "t1_func", expected_syms));
4131 }
4132
4133 /* Check that the increment-last-char in the name matching algorithm
4134 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4135 {
4136 static const char *expected_syms1[] = {
4137 "\377",
4138 "\377\377123",
4139 };
4140 SELF_CHECK (check_find_bounds_finds (mock_index,
4141 "\377", expected_syms1));
4142
4143 static const char *expected_syms2[] = {
4144 "\377\377123",
4145 };
4146 SELF_CHECK (check_find_bounds_finds (mock_index,
4147 "\377\377", expected_syms2));
4148 }
4149 }
4150
4151 /* Test dw2_expand_symtabs_matching_symbol. */
4152
4153 static void
4154 test_dw2_expand_symtabs_matching_symbol ()
4155 {
4156 mock_mapped_index mock_index (test_symbols);
4157
4158 /* We let all tests run until the end even if some fails, for debug
4159 convenience. */
4160 bool any_mismatch = false;
4161
4162 /* Create the expected symbols list (an initializer_list). Needed
4163 because lists have commas, and we need to pass them to CHECK,
4164 which is a macro. */
4165 #define EXPECT(...) { __VA_ARGS__ }
4166
4167 /* Wrapper for check_match that passes down the current
4168 __FILE__/__LINE__. */
4169 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4170 any_mismatch |= !check_match (__FILE__, __LINE__, \
4171 mock_index, \
4172 NAME, MATCH_TYPE, COMPLETION_MODE, \
4173 EXPECTED_LIST)
4174
4175 /* Identity checks. */
4176 for (const char *sym : test_symbols)
4177 {
4178 /* Should be able to match all existing symbols. */
4179 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4180 EXPECT (sym));
4181
4182 /* Should be able to match all existing symbols with
4183 parameters. */
4184 std::string with_params = std::string (sym) + "(int)";
4185 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4186 EXPECT (sym));
4187
4188 /* Should be able to match all existing symbols with
4189 parameters and qualifiers. */
4190 with_params = std::string (sym) + " ( int ) const";
4191 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4192 EXPECT (sym));
4193
4194 /* This should really find sym, but cp-name-parser.y doesn't
4195 know about lvalue/rvalue qualifiers yet. */
4196 with_params = std::string (sym) + " ( int ) &&";
4197 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4198 {});
4199 }
4200
4201 /* Check that the name matching algorithm for completion doesn't get
4202 confused with Latin1 'ÿ' / 0xff. */
4203 {
4204 static const char str[] = "\377";
4205 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4206 EXPECT ("\377", "\377\377123"));
4207 }
4208
4209 /* Check that the increment-last-char in the matching algorithm for
4210 completion doesn't match "t1_fund" when completing "t1_func". */
4211 {
4212 static const char str[] = "t1_func";
4213 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4214 EXPECT ("t1_func", "t1_func1"));
4215 }
4216
4217 /* Check that completion mode works at each prefix of the expected
4218 symbol name. */
4219 {
4220 static const char str[] = "function(int)";
4221 size_t len = strlen (str);
4222 std::string lookup;
4223
4224 for (size_t i = 1; i < len; i++)
4225 {
4226 lookup.assign (str, i);
4227 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4228 EXPECT ("function"));
4229 }
4230 }
4231
4232 /* While "w" is a prefix of both components, the match function
4233 should still only be called once. */
4234 {
4235 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4236 EXPECT ("w1::w2"));
4237 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4238 EXPECT ("w1::w2"));
4239 }
4240
4241 /* Same, with a "complicated" symbol. */
4242 {
4243 static const char str[] = Z_SYM_NAME;
4244 size_t len = strlen (str);
4245 std::string lookup;
4246
4247 for (size_t i = 1; i < len; i++)
4248 {
4249 lookup.assign (str, i);
4250 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4251 EXPECT (Z_SYM_NAME));
4252 }
4253 }
4254
4255 /* In FULL mode, an incomplete symbol doesn't match. */
4256 {
4257 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4258 {});
4259 }
4260
4261 /* A complete symbol with parameters matches any overload, since the
4262 index has no overload info. */
4263 {
4264 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4265 EXPECT ("std::zfunction", "std::zfunction2"));
4266 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4267 EXPECT ("std::zfunction", "std::zfunction2"));
4268 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4269 EXPECT ("std::zfunction", "std::zfunction2"));
4270 }
4271
4272 /* Check that whitespace is ignored appropriately. A symbol with a
4273 template argument list. */
4274 {
4275 static const char expected[] = "ns::foo<int>";
4276 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4277 EXPECT (expected));
4278 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4279 EXPECT (expected));
4280 }
4281
4282 /* Check that whitespace is ignored appropriately. A symbol with a
4283 template argument list that includes a pointer. */
4284 {
4285 static const char expected[] = "ns::foo<char*>";
4286 /* Try both completion and non-completion modes. */
4287 static const bool completion_mode[2] = {false, true};
4288 for (size_t i = 0; i < 2; i++)
4289 {
4290 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4291 completion_mode[i], EXPECT (expected));
4292 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4293 completion_mode[i], EXPECT (expected));
4294
4295 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4296 completion_mode[i], EXPECT (expected));
4297 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4298 completion_mode[i], EXPECT (expected));
4299 }
4300 }
4301
4302 {
4303 /* Check method qualifiers are ignored. */
4304 static const char expected[] = "ns::foo<char*>";
4305 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4306 symbol_name_match_type::FULL, true, EXPECT (expected));
4307 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4308 symbol_name_match_type::FULL, true, EXPECT (expected));
4309 CHECK_MATCH ("foo < char * > ( int ) const",
4310 symbol_name_match_type::WILD, true, EXPECT (expected));
4311 CHECK_MATCH ("foo < char * > ( int ) &&",
4312 symbol_name_match_type::WILD, true, EXPECT (expected));
4313 }
4314
4315 /* Test lookup names that don't match anything. */
4316 {
4317 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4318 {});
4319
4320 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4321 {});
4322 }
4323
4324 /* Some wild matching tests, exercising "(anonymous namespace)",
4325 which should not be confused with a parameter list. */
4326 {
4327 static const char *syms[] = {
4328 "A::B::C",
4329 "B::C",
4330 "C",
4331 "A :: B :: C ( int )",
4332 "B :: C ( int )",
4333 "C ( int )",
4334 };
4335
4336 for (const char *s : syms)
4337 {
4338 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4339 EXPECT ("(anonymous namespace)::A::B::C"));
4340 }
4341 }
4342
4343 {
4344 static const char expected[] = "ns2::tmpl<int>::foo2";
4345 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4346 EXPECT (expected));
4347 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4348 EXPECT (expected));
4349 }
4350
4351 SELF_CHECK (!any_mismatch);
4352
4353 #undef EXPECT
4354 #undef CHECK_MATCH
4355 }
4356
4357 static void
4358 run_test ()
4359 {
4360 test_mapped_index_find_name_component_bounds ();
4361 test_dw2_expand_symtabs_matching_symbol ();
4362 }
4363
4364 }} // namespace selftests::dw2_expand_symtabs_matching
4365
4366 #endif /* GDB_SELF_TEST */
4367
4368 /* If FILE_MATCHER is NULL or if PER_CU has
4369 dwarf2_per_cu_quick_data::MARK set (see
4370 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4371 EXPANSION_NOTIFY on it. */
4372
4373 static void
4374 dw2_expand_symtabs_matching_one
4375 (struct dwarf2_per_cu_data *per_cu,
4376 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4377 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4378 {
4379 if (file_matcher == NULL || per_cu->v.quick->mark)
4380 {
4381 bool symtab_was_null
4382 = (per_cu->v.quick->compunit_symtab == NULL);
4383
4384 dw2_instantiate_symtab (per_cu, false);
4385
4386 if (expansion_notify != NULL
4387 && symtab_was_null
4388 && per_cu->v.quick->compunit_symtab != NULL)
4389 expansion_notify (per_cu->v.quick->compunit_symtab);
4390 }
4391 }
4392
4393 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4394 matched, to expand corresponding CUs that were marked. IDX is the
4395 index of the symbol name that matched. */
4396
4397 static void
4398 dw2_expand_marked_cus
4399 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4400 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4401 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4402 search_domain kind)
4403 {
4404 offset_type *vec, vec_len, vec_idx;
4405 bool global_seen = false;
4406 mapped_index &index = *dwarf2_per_objfile->index_table;
4407
4408 vec = (offset_type *) (index.constant_pool
4409 + MAYBE_SWAP (index.symbol_table[idx].vec));
4410 vec_len = MAYBE_SWAP (vec[0]);
4411 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4412 {
4413 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4414 /* This value is only valid for index versions >= 7. */
4415 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4416 gdb_index_symbol_kind symbol_kind =
4417 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4418 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4419 /* Only check the symbol attributes if they're present.
4420 Indices prior to version 7 don't record them,
4421 and indices >= 7 may elide them for certain symbols
4422 (gold does this). */
4423 int attrs_valid =
4424 (index.version >= 7
4425 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4426
4427 /* Work around gold/15646. */
4428 if (attrs_valid)
4429 {
4430 if (!is_static && global_seen)
4431 continue;
4432 if (!is_static)
4433 global_seen = true;
4434 }
4435
4436 /* Only check the symbol's kind if it has one. */
4437 if (attrs_valid)
4438 {
4439 switch (kind)
4440 {
4441 case VARIABLES_DOMAIN:
4442 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4443 continue;
4444 break;
4445 case FUNCTIONS_DOMAIN:
4446 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4447 continue;
4448 break;
4449 case TYPES_DOMAIN:
4450 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4451 continue;
4452 break;
4453 case MODULES_DOMAIN:
4454 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4455 continue;
4456 break;
4457 default:
4458 break;
4459 }
4460 }
4461
4462 /* Don't crash on bad data. */
4463 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4464 + dwarf2_per_objfile->all_type_units.size ()))
4465 {
4466 complaint (_(".gdb_index entry has bad CU index"
4467 " [in module %s]"),
4468 objfile_name (dwarf2_per_objfile->objfile));
4469 continue;
4470 }
4471
4472 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4473 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4474 expansion_notify);
4475 }
4476 }
4477
4478 /* If FILE_MATCHER is non-NULL, set all the
4479 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4480 that match FILE_MATCHER. */
4481
4482 static void
4483 dw_expand_symtabs_matching_file_matcher
4484 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4485 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4486 {
4487 if (file_matcher == NULL)
4488 return;
4489
4490 objfile *const objfile = dwarf2_per_objfile->objfile;
4491
4492 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4493 htab_eq_pointer,
4494 NULL, xcalloc, xfree));
4495 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4496 htab_eq_pointer,
4497 NULL, xcalloc, xfree));
4498
4499 /* The rule is CUs specify all the files, including those used by
4500 any TU, so there's no need to scan TUs here. */
4501
4502 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4503 {
4504 QUIT;
4505
4506 per_cu->v.quick->mark = 0;
4507
4508 /* We only need to look at symtabs not already expanded. */
4509 if (per_cu->v.quick->compunit_symtab)
4510 continue;
4511
4512 quick_file_names *file_data = dw2_get_file_names (per_cu);
4513 if (file_data == NULL)
4514 continue;
4515
4516 if (htab_find (visited_not_found.get (), file_data) != NULL)
4517 continue;
4518 else if (htab_find (visited_found.get (), file_data) != NULL)
4519 {
4520 per_cu->v.quick->mark = 1;
4521 continue;
4522 }
4523
4524 for (int j = 0; j < file_data->num_file_names; ++j)
4525 {
4526 const char *this_real_name;
4527
4528 if (file_matcher (file_data->file_names[j], false))
4529 {
4530 per_cu->v.quick->mark = 1;
4531 break;
4532 }
4533
4534 /* Before we invoke realpath, which can get expensive when many
4535 files are involved, do a quick comparison of the basenames. */
4536 if (!basenames_may_differ
4537 && !file_matcher (lbasename (file_data->file_names[j]),
4538 true))
4539 continue;
4540
4541 this_real_name = dw2_get_real_path (objfile, file_data, j);
4542 if (file_matcher (this_real_name, false))
4543 {
4544 per_cu->v.quick->mark = 1;
4545 break;
4546 }
4547 }
4548
4549 void **slot = htab_find_slot (per_cu->v.quick->mark
4550 ? visited_found.get ()
4551 : visited_not_found.get (),
4552 file_data, INSERT);
4553 *slot = file_data;
4554 }
4555 }
4556
4557 static void
4558 dw2_expand_symtabs_matching
4559 (struct objfile *objfile,
4560 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4561 const lookup_name_info &lookup_name,
4562 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4563 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4564 enum search_domain kind)
4565 {
4566 struct dwarf2_per_objfile *dwarf2_per_objfile
4567 = get_dwarf2_per_objfile (objfile);
4568
4569 /* index_table is NULL if OBJF_READNOW. */
4570 if (!dwarf2_per_objfile->index_table)
4571 return;
4572
4573 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4574
4575 mapped_index &index = *dwarf2_per_objfile->index_table;
4576
4577 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4578 symbol_matcher,
4579 kind, [&] (offset_type idx)
4580 {
4581 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4582 expansion_notify, kind);
4583 return true;
4584 });
4585 }
4586
4587 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4588 symtab. */
4589
4590 static struct compunit_symtab *
4591 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4592 CORE_ADDR pc)
4593 {
4594 int i;
4595
4596 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4597 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4598 return cust;
4599
4600 if (cust->includes == NULL)
4601 return NULL;
4602
4603 for (i = 0; cust->includes[i]; ++i)
4604 {
4605 struct compunit_symtab *s = cust->includes[i];
4606
4607 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4608 if (s != NULL)
4609 return s;
4610 }
4611
4612 return NULL;
4613 }
4614
4615 static struct compunit_symtab *
4616 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4617 struct bound_minimal_symbol msymbol,
4618 CORE_ADDR pc,
4619 struct obj_section *section,
4620 int warn_if_readin)
4621 {
4622 struct dwarf2_per_cu_data *data;
4623 struct compunit_symtab *result;
4624
4625 if (!objfile->partial_symtabs->psymtabs_addrmap)
4626 return NULL;
4627
4628 CORE_ADDR baseaddr = objfile->text_section_offset ();
4629 data = (struct dwarf2_per_cu_data *) addrmap_find
4630 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4631 if (!data)
4632 return NULL;
4633
4634 if (warn_if_readin && data->v.quick->compunit_symtab)
4635 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4636 paddress (get_objfile_arch (objfile), pc));
4637
4638 result
4639 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4640 false),
4641 pc);
4642 gdb_assert (result != NULL);
4643 return result;
4644 }
4645
4646 static void
4647 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4648 void *data, int need_fullname)
4649 {
4650 struct dwarf2_per_objfile *dwarf2_per_objfile
4651 = get_dwarf2_per_objfile (objfile);
4652
4653 if (!dwarf2_per_objfile->filenames_cache)
4654 {
4655 dwarf2_per_objfile->filenames_cache.emplace ();
4656
4657 htab_up visited (htab_create_alloc (10,
4658 htab_hash_pointer, htab_eq_pointer,
4659 NULL, xcalloc, xfree));
4660
4661 /* The rule is CUs specify all the files, including those used
4662 by any TU, so there's no need to scan TUs here. We can
4663 ignore file names coming from already-expanded CUs. */
4664
4665 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4666 {
4667 if (per_cu->v.quick->compunit_symtab)
4668 {
4669 void **slot = htab_find_slot (visited.get (),
4670 per_cu->v.quick->file_names,
4671 INSERT);
4672
4673 *slot = per_cu->v.quick->file_names;
4674 }
4675 }
4676
4677 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4678 {
4679 /* We only need to look at symtabs not already expanded. */
4680 if (per_cu->v.quick->compunit_symtab)
4681 continue;
4682
4683 quick_file_names *file_data = dw2_get_file_names (per_cu);
4684 if (file_data == NULL)
4685 continue;
4686
4687 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4688 if (*slot)
4689 {
4690 /* Already visited. */
4691 continue;
4692 }
4693 *slot = file_data;
4694
4695 for (int j = 0; j < file_data->num_file_names; ++j)
4696 {
4697 const char *filename = file_data->file_names[j];
4698 dwarf2_per_objfile->filenames_cache->seen (filename);
4699 }
4700 }
4701 }
4702
4703 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4704 {
4705 gdb::unique_xmalloc_ptr<char> this_real_name;
4706
4707 if (need_fullname)
4708 this_real_name = gdb_realpath (filename);
4709 (*fun) (filename, this_real_name.get (), data);
4710 });
4711 }
4712
4713 static int
4714 dw2_has_symbols (struct objfile *objfile)
4715 {
4716 return 1;
4717 }
4718
4719 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4720 {
4721 dw2_has_symbols,
4722 dw2_find_last_source_symtab,
4723 dw2_forget_cached_source_info,
4724 dw2_map_symtabs_matching_filename,
4725 dw2_lookup_symbol,
4726 dw2_print_stats,
4727 dw2_dump,
4728 dw2_expand_symtabs_for_function,
4729 dw2_expand_all_symtabs,
4730 dw2_expand_symtabs_with_fullname,
4731 dw2_map_matching_symbols,
4732 dw2_expand_symtabs_matching,
4733 dw2_find_pc_sect_compunit_symtab,
4734 NULL,
4735 dw2_map_symbol_filenames
4736 };
4737
4738 /* DWARF-5 debug_names reader. */
4739
4740 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4741 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4742
4743 /* A helper function that reads the .debug_names section in SECTION
4744 and fills in MAP. FILENAME is the name of the file containing the
4745 section; it is used for error reporting.
4746
4747 Returns true if all went well, false otherwise. */
4748
4749 static bool
4750 read_debug_names_from_section (struct objfile *objfile,
4751 const char *filename,
4752 struct dwarf2_section_info *section,
4753 mapped_debug_names &map)
4754 {
4755 if (section->empty ())
4756 return false;
4757
4758 /* Older elfutils strip versions could keep the section in the main
4759 executable while splitting it for the separate debug info file. */
4760 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4761 return false;
4762
4763 section->read (objfile);
4764
4765 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4766
4767 const gdb_byte *addr = section->buffer;
4768
4769 bfd *const abfd = section->get_bfd_owner ();
4770
4771 unsigned int bytes_read;
4772 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4773 addr += bytes_read;
4774
4775 map.dwarf5_is_dwarf64 = bytes_read != 4;
4776 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4777 if (bytes_read + length != section->size)
4778 {
4779 /* There may be multiple per-CU indices. */
4780 warning (_("Section .debug_names in %s length %s does not match "
4781 "section length %s, ignoring .debug_names."),
4782 filename, plongest (bytes_read + length),
4783 pulongest (section->size));
4784 return false;
4785 }
4786
4787 /* The version number. */
4788 uint16_t version = read_2_bytes (abfd, addr);
4789 addr += 2;
4790 if (version != 5)
4791 {
4792 warning (_("Section .debug_names in %s has unsupported version %d, "
4793 "ignoring .debug_names."),
4794 filename, version);
4795 return false;
4796 }
4797
4798 /* Padding. */
4799 uint16_t padding = read_2_bytes (abfd, addr);
4800 addr += 2;
4801 if (padding != 0)
4802 {
4803 warning (_("Section .debug_names in %s has unsupported padding %d, "
4804 "ignoring .debug_names."),
4805 filename, padding);
4806 return false;
4807 }
4808
4809 /* comp_unit_count - The number of CUs in the CU list. */
4810 map.cu_count = read_4_bytes (abfd, addr);
4811 addr += 4;
4812
4813 /* local_type_unit_count - The number of TUs in the local TU
4814 list. */
4815 map.tu_count = read_4_bytes (abfd, addr);
4816 addr += 4;
4817
4818 /* foreign_type_unit_count - The number of TUs in the foreign TU
4819 list. */
4820 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4821 addr += 4;
4822 if (foreign_tu_count != 0)
4823 {
4824 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4825 "ignoring .debug_names."),
4826 filename, static_cast<unsigned long> (foreign_tu_count));
4827 return false;
4828 }
4829
4830 /* bucket_count - The number of hash buckets in the hash lookup
4831 table. */
4832 map.bucket_count = read_4_bytes (abfd, addr);
4833 addr += 4;
4834
4835 /* name_count - The number of unique names in the index. */
4836 map.name_count = read_4_bytes (abfd, addr);
4837 addr += 4;
4838
4839 /* abbrev_table_size - The size in bytes of the abbreviations
4840 table. */
4841 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4842 addr += 4;
4843
4844 /* augmentation_string_size - The size in bytes of the augmentation
4845 string. This value is rounded up to a multiple of 4. */
4846 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4847 addr += 4;
4848 map.augmentation_is_gdb = ((augmentation_string_size
4849 == sizeof (dwarf5_augmentation))
4850 && memcmp (addr, dwarf5_augmentation,
4851 sizeof (dwarf5_augmentation)) == 0);
4852 augmentation_string_size += (-augmentation_string_size) & 3;
4853 addr += augmentation_string_size;
4854
4855 /* List of CUs */
4856 map.cu_table_reordered = addr;
4857 addr += map.cu_count * map.offset_size;
4858
4859 /* List of Local TUs */
4860 map.tu_table_reordered = addr;
4861 addr += map.tu_count * map.offset_size;
4862
4863 /* Hash Lookup Table */
4864 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4865 addr += map.bucket_count * 4;
4866 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4867 addr += map.name_count * 4;
4868
4869 /* Name Table */
4870 map.name_table_string_offs_reordered = addr;
4871 addr += map.name_count * map.offset_size;
4872 map.name_table_entry_offs_reordered = addr;
4873 addr += map.name_count * map.offset_size;
4874
4875 const gdb_byte *abbrev_table_start = addr;
4876 for (;;)
4877 {
4878 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4879 addr += bytes_read;
4880 if (index_num == 0)
4881 break;
4882
4883 const auto insertpair
4884 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4885 if (!insertpair.second)
4886 {
4887 warning (_("Section .debug_names in %s has duplicate index %s, "
4888 "ignoring .debug_names."),
4889 filename, pulongest (index_num));
4890 return false;
4891 }
4892 mapped_debug_names::index_val &indexval = insertpair.first->second;
4893 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4894 addr += bytes_read;
4895
4896 for (;;)
4897 {
4898 mapped_debug_names::index_val::attr attr;
4899 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4900 addr += bytes_read;
4901 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4902 addr += bytes_read;
4903 if (attr.form == DW_FORM_implicit_const)
4904 {
4905 attr.implicit_const = read_signed_leb128 (abfd, addr,
4906 &bytes_read);
4907 addr += bytes_read;
4908 }
4909 if (attr.dw_idx == 0 && attr.form == 0)
4910 break;
4911 indexval.attr_vec.push_back (std::move (attr));
4912 }
4913 }
4914 if (addr != abbrev_table_start + abbrev_table_size)
4915 {
4916 warning (_("Section .debug_names in %s has abbreviation_table "
4917 "of size %s vs. written as %u, ignoring .debug_names."),
4918 filename, plongest (addr - abbrev_table_start),
4919 abbrev_table_size);
4920 return false;
4921 }
4922 map.entry_pool = addr;
4923
4924 return true;
4925 }
4926
4927 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4928 list. */
4929
4930 static void
4931 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4932 const mapped_debug_names &map,
4933 dwarf2_section_info &section,
4934 bool is_dwz)
4935 {
4936 sect_offset sect_off_prev;
4937 for (uint32_t i = 0; i <= map.cu_count; ++i)
4938 {
4939 sect_offset sect_off_next;
4940 if (i < map.cu_count)
4941 {
4942 sect_off_next
4943 = (sect_offset) (extract_unsigned_integer
4944 (map.cu_table_reordered + i * map.offset_size,
4945 map.offset_size,
4946 map.dwarf5_byte_order));
4947 }
4948 else
4949 sect_off_next = (sect_offset) section.size;
4950 if (i >= 1)
4951 {
4952 const ULONGEST length = sect_off_next - sect_off_prev;
4953 dwarf2_per_cu_data *per_cu
4954 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4955 sect_off_prev, length);
4956 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4957 }
4958 sect_off_prev = sect_off_next;
4959 }
4960 }
4961
4962 /* Read the CU list from the mapped index, and use it to create all
4963 the CU objects for this dwarf2_per_objfile. */
4964
4965 static void
4966 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4967 const mapped_debug_names &map,
4968 const mapped_debug_names &dwz_map)
4969 {
4970 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4971 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4972
4973 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4974 dwarf2_per_objfile->info,
4975 false /* is_dwz */);
4976
4977 if (dwz_map.cu_count == 0)
4978 return;
4979
4980 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4981 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4982 true /* is_dwz */);
4983 }
4984
4985 /* Read .debug_names. If everything went ok, initialize the "quick"
4986 elements of all the CUs and return true. Otherwise, return false. */
4987
4988 static bool
4989 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
4990 {
4991 std::unique_ptr<mapped_debug_names> map
4992 (new mapped_debug_names (dwarf2_per_objfile));
4993 mapped_debug_names dwz_map (dwarf2_per_objfile);
4994 struct objfile *objfile = dwarf2_per_objfile->objfile;
4995
4996 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4997 &dwarf2_per_objfile->debug_names,
4998 *map))
4999 return false;
5000
5001 /* Don't use the index if it's empty. */
5002 if (map->name_count == 0)
5003 return false;
5004
5005 /* If there is a .dwz file, read it so we can get its CU list as
5006 well. */
5007 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5008 if (dwz != NULL)
5009 {
5010 if (!read_debug_names_from_section (objfile,
5011 bfd_get_filename (dwz->dwz_bfd.get ()),
5012 &dwz->debug_names, dwz_map))
5013 {
5014 warning (_("could not read '.debug_names' section from %s; skipping"),
5015 bfd_get_filename (dwz->dwz_bfd.get ()));
5016 return false;
5017 }
5018 }
5019
5020 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5021
5022 if (map->tu_count != 0)
5023 {
5024 /* We can only handle a single .debug_types when we have an
5025 index. */
5026 if (dwarf2_per_objfile->types.size () != 1)
5027 return false;
5028
5029 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5030
5031 create_signatured_type_table_from_debug_names
5032 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5033 }
5034
5035 create_addrmap_from_aranges (dwarf2_per_objfile,
5036 &dwarf2_per_objfile->debug_aranges);
5037
5038 dwarf2_per_objfile->debug_names_table = std::move (map);
5039 dwarf2_per_objfile->using_index = 1;
5040 dwarf2_per_objfile->quick_file_names_table =
5041 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5042
5043 return true;
5044 }
5045
5046 /* Type used to manage iterating over all CUs looking for a symbol for
5047 .debug_names. */
5048
5049 class dw2_debug_names_iterator
5050 {
5051 public:
5052 dw2_debug_names_iterator (const mapped_debug_names &map,
5053 gdb::optional<block_enum> block_index,
5054 domain_enum domain,
5055 const char *name)
5056 : m_map (map), m_block_index (block_index), m_domain (domain),
5057 m_addr (find_vec_in_debug_names (map, name))
5058 {}
5059
5060 dw2_debug_names_iterator (const mapped_debug_names &map,
5061 search_domain search, uint32_t namei)
5062 : m_map (map),
5063 m_search (search),
5064 m_addr (find_vec_in_debug_names (map, namei))
5065 {}
5066
5067 dw2_debug_names_iterator (const mapped_debug_names &map,
5068 block_enum block_index, domain_enum domain,
5069 uint32_t namei)
5070 : m_map (map), m_block_index (block_index), m_domain (domain),
5071 m_addr (find_vec_in_debug_names (map, namei))
5072 {}
5073
5074 /* Return the next matching CU or NULL if there are no more. */
5075 dwarf2_per_cu_data *next ();
5076
5077 private:
5078 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5079 const char *name);
5080 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5081 uint32_t namei);
5082
5083 /* The internalized form of .debug_names. */
5084 const mapped_debug_names &m_map;
5085
5086 /* If set, only look for symbols that match that block. Valid values are
5087 GLOBAL_BLOCK and STATIC_BLOCK. */
5088 const gdb::optional<block_enum> m_block_index;
5089
5090 /* The kind of symbol we're looking for. */
5091 const domain_enum m_domain = UNDEF_DOMAIN;
5092 const search_domain m_search = ALL_DOMAIN;
5093
5094 /* The list of CUs from the index entry of the symbol, or NULL if
5095 not found. */
5096 const gdb_byte *m_addr;
5097 };
5098
5099 const char *
5100 mapped_debug_names::namei_to_name (uint32_t namei) const
5101 {
5102 const ULONGEST namei_string_offs
5103 = extract_unsigned_integer ((name_table_string_offs_reordered
5104 + namei * offset_size),
5105 offset_size,
5106 dwarf5_byte_order);
5107 return read_indirect_string_at_offset (dwarf2_per_objfile,
5108 namei_string_offs);
5109 }
5110
5111 /* Find a slot in .debug_names for the object named NAME. If NAME is
5112 found, return pointer to its pool data. If NAME cannot be found,
5113 return NULL. */
5114
5115 const gdb_byte *
5116 dw2_debug_names_iterator::find_vec_in_debug_names
5117 (const mapped_debug_names &map, const char *name)
5118 {
5119 int (*cmp) (const char *, const char *);
5120
5121 gdb::unique_xmalloc_ptr<char> without_params;
5122 if (current_language->la_language == language_cplus
5123 || current_language->la_language == language_fortran
5124 || current_language->la_language == language_d)
5125 {
5126 /* NAME is already canonical. Drop any qualifiers as
5127 .debug_names does not contain any. */
5128
5129 if (strchr (name, '(') != NULL)
5130 {
5131 without_params = cp_remove_params (name);
5132 if (without_params != NULL)
5133 name = without_params.get ();
5134 }
5135 }
5136
5137 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5138
5139 const uint32_t full_hash = dwarf5_djb_hash (name);
5140 uint32_t namei
5141 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5142 (map.bucket_table_reordered
5143 + (full_hash % map.bucket_count)), 4,
5144 map.dwarf5_byte_order);
5145 if (namei == 0)
5146 return NULL;
5147 --namei;
5148 if (namei >= map.name_count)
5149 {
5150 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5151 "[in module %s]"),
5152 namei, map.name_count,
5153 objfile_name (map.dwarf2_per_objfile->objfile));
5154 return NULL;
5155 }
5156
5157 for (;;)
5158 {
5159 const uint32_t namei_full_hash
5160 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5161 (map.hash_table_reordered + namei), 4,
5162 map.dwarf5_byte_order);
5163 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5164 return NULL;
5165
5166 if (full_hash == namei_full_hash)
5167 {
5168 const char *const namei_string = map.namei_to_name (namei);
5169
5170 #if 0 /* An expensive sanity check. */
5171 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5172 {
5173 complaint (_("Wrong .debug_names hash for string at index %u "
5174 "[in module %s]"),
5175 namei, objfile_name (dwarf2_per_objfile->objfile));
5176 return NULL;
5177 }
5178 #endif
5179
5180 if (cmp (namei_string, name) == 0)
5181 {
5182 const ULONGEST namei_entry_offs
5183 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5184 + namei * map.offset_size),
5185 map.offset_size, map.dwarf5_byte_order);
5186 return map.entry_pool + namei_entry_offs;
5187 }
5188 }
5189
5190 ++namei;
5191 if (namei >= map.name_count)
5192 return NULL;
5193 }
5194 }
5195
5196 const gdb_byte *
5197 dw2_debug_names_iterator::find_vec_in_debug_names
5198 (const mapped_debug_names &map, uint32_t namei)
5199 {
5200 if (namei >= map.name_count)
5201 {
5202 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5203 "[in module %s]"),
5204 namei, map.name_count,
5205 objfile_name (map.dwarf2_per_objfile->objfile));
5206 return NULL;
5207 }
5208
5209 const ULONGEST namei_entry_offs
5210 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5211 + namei * map.offset_size),
5212 map.offset_size, map.dwarf5_byte_order);
5213 return map.entry_pool + namei_entry_offs;
5214 }
5215
5216 /* See dw2_debug_names_iterator. */
5217
5218 dwarf2_per_cu_data *
5219 dw2_debug_names_iterator::next ()
5220 {
5221 if (m_addr == NULL)
5222 return NULL;
5223
5224 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5225 struct objfile *objfile = dwarf2_per_objfile->objfile;
5226 bfd *const abfd = objfile->obfd;
5227
5228 again:
5229
5230 unsigned int bytes_read;
5231 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5232 m_addr += bytes_read;
5233 if (abbrev == 0)
5234 return NULL;
5235
5236 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5237 if (indexval_it == m_map.abbrev_map.cend ())
5238 {
5239 complaint (_("Wrong .debug_names undefined abbrev code %s "
5240 "[in module %s]"),
5241 pulongest (abbrev), objfile_name (objfile));
5242 return NULL;
5243 }
5244 const mapped_debug_names::index_val &indexval = indexval_it->second;
5245 enum class symbol_linkage {
5246 unknown,
5247 static_,
5248 extern_,
5249 } symbol_linkage_ = symbol_linkage::unknown;
5250 dwarf2_per_cu_data *per_cu = NULL;
5251 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5252 {
5253 ULONGEST ull;
5254 switch (attr.form)
5255 {
5256 case DW_FORM_implicit_const:
5257 ull = attr.implicit_const;
5258 break;
5259 case DW_FORM_flag_present:
5260 ull = 1;
5261 break;
5262 case DW_FORM_udata:
5263 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5264 m_addr += bytes_read;
5265 break;
5266 default:
5267 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5268 dwarf_form_name (attr.form),
5269 objfile_name (objfile));
5270 return NULL;
5271 }
5272 switch (attr.dw_idx)
5273 {
5274 case DW_IDX_compile_unit:
5275 /* Don't crash on bad data. */
5276 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5277 {
5278 complaint (_(".debug_names entry has bad CU index %s"
5279 " [in module %s]"),
5280 pulongest (ull),
5281 objfile_name (dwarf2_per_objfile->objfile));
5282 continue;
5283 }
5284 per_cu = dwarf2_per_objfile->get_cutu (ull);
5285 break;
5286 case DW_IDX_type_unit:
5287 /* Don't crash on bad data. */
5288 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5289 {
5290 complaint (_(".debug_names entry has bad TU index %s"
5291 " [in module %s]"),
5292 pulongest (ull),
5293 objfile_name (dwarf2_per_objfile->objfile));
5294 continue;
5295 }
5296 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5297 break;
5298 case DW_IDX_GNU_internal:
5299 if (!m_map.augmentation_is_gdb)
5300 break;
5301 symbol_linkage_ = symbol_linkage::static_;
5302 break;
5303 case DW_IDX_GNU_external:
5304 if (!m_map.augmentation_is_gdb)
5305 break;
5306 symbol_linkage_ = symbol_linkage::extern_;
5307 break;
5308 }
5309 }
5310
5311 /* Skip if already read in. */
5312 if (per_cu->v.quick->compunit_symtab)
5313 goto again;
5314
5315 /* Check static vs global. */
5316 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5317 {
5318 const bool want_static = *m_block_index == STATIC_BLOCK;
5319 const bool symbol_is_static =
5320 symbol_linkage_ == symbol_linkage::static_;
5321 if (want_static != symbol_is_static)
5322 goto again;
5323 }
5324
5325 /* Match dw2_symtab_iter_next, symbol_kind
5326 and debug_names::psymbol_tag. */
5327 switch (m_domain)
5328 {
5329 case VAR_DOMAIN:
5330 switch (indexval.dwarf_tag)
5331 {
5332 case DW_TAG_variable:
5333 case DW_TAG_subprogram:
5334 /* Some types are also in VAR_DOMAIN. */
5335 case DW_TAG_typedef:
5336 case DW_TAG_structure_type:
5337 break;
5338 default:
5339 goto again;
5340 }
5341 break;
5342 case STRUCT_DOMAIN:
5343 switch (indexval.dwarf_tag)
5344 {
5345 case DW_TAG_typedef:
5346 case DW_TAG_structure_type:
5347 break;
5348 default:
5349 goto again;
5350 }
5351 break;
5352 case LABEL_DOMAIN:
5353 switch (indexval.dwarf_tag)
5354 {
5355 case 0:
5356 case DW_TAG_variable:
5357 break;
5358 default:
5359 goto again;
5360 }
5361 break;
5362 case MODULE_DOMAIN:
5363 switch (indexval.dwarf_tag)
5364 {
5365 case DW_TAG_module:
5366 break;
5367 default:
5368 goto again;
5369 }
5370 break;
5371 default:
5372 break;
5373 }
5374
5375 /* Match dw2_expand_symtabs_matching, symbol_kind and
5376 debug_names::psymbol_tag. */
5377 switch (m_search)
5378 {
5379 case VARIABLES_DOMAIN:
5380 switch (indexval.dwarf_tag)
5381 {
5382 case DW_TAG_variable:
5383 break;
5384 default:
5385 goto again;
5386 }
5387 break;
5388 case FUNCTIONS_DOMAIN:
5389 switch (indexval.dwarf_tag)
5390 {
5391 case DW_TAG_subprogram:
5392 break;
5393 default:
5394 goto again;
5395 }
5396 break;
5397 case TYPES_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 MODULES_DOMAIN:
5408 switch (indexval.dwarf_tag)
5409 {
5410 case DW_TAG_module:
5411 break;
5412 default:
5413 goto again;
5414 }
5415 default:
5416 break;
5417 }
5418
5419 return per_cu;
5420 }
5421
5422 static struct compunit_symtab *
5423 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5424 const char *name, domain_enum domain)
5425 {
5426 struct dwarf2_per_objfile *dwarf2_per_objfile
5427 = get_dwarf2_per_objfile (objfile);
5428
5429 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5430 if (!mapp)
5431 {
5432 /* index is NULL if OBJF_READNOW. */
5433 return NULL;
5434 }
5435 const auto &map = *mapp;
5436
5437 dw2_debug_names_iterator iter (map, block_index, domain, name);
5438
5439 struct compunit_symtab *stab_best = NULL;
5440 struct dwarf2_per_cu_data *per_cu;
5441 while ((per_cu = iter.next ()) != NULL)
5442 {
5443 struct symbol *sym, *with_opaque = NULL;
5444 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5445 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5446 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5447
5448 sym = block_find_symbol (block, name, domain,
5449 block_find_non_opaque_type_preferred,
5450 &with_opaque);
5451
5452 /* Some caution must be observed with overloaded functions and
5453 methods, since the index will not contain any overload
5454 information (but NAME might contain it). */
5455
5456 if (sym != NULL
5457 && strcmp_iw (sym->search_name (), name) == 0)
5458 return stab;
5459 if (with_opaque != NULL
5460 && strcmp_iw (with_opaque->search_name (), name) == 0)
5461 stab_best = stab;
5462
5463 /* Keep looking through other CUs. */
5464 }
5465
5466 return stab_best;
5467 }
5468
5469 /* This dumps minimal information about .debug_names. It is called
5470 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5471 uses this to verify that .debug_names has been loaded. */
5472
5473 static void
5474 dw2_debug_names_dump (struct objfile *objfile)
5475 {
5476 struct dwarf2_per_objfile *dwarf2_per_objfile
5477 = get_dwarf2_per_objfile (objfile);
5478
5479 gdb_assert (dwarf2_per_objfile->using_index);
5480 printf_filtered (".debug_names:");
5481 if (dwarf2_per_objfile->debug_names_table)
5482 printf_filtered (" exists\n");
5483 else
5484 printf_filtered (" faked for \"readnow\"\n");
5485 printf_filtered ("\n");
5486 }
5487
5488 static void
5489 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5490 const char *func_name)
5491 {
5492 struct dwarf2_per_objfile *dwarf2_per_objfile
5493 = get_dwarf2_per_objfile (objfile);
5494
5495 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5496 if (dwarf2_per_objfile->debug_names_table)
5497 {
5498 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5499
5500 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5501
5502 struct dwarf2_per_cu_data *per_cu;
5503 while ((per_cu = iter.next ()) != NULL)
5504 dw2_instantiate_symtab (per_cu, false);
5505 }
5506 }
5507
5508 static void
5509 dw2_debug_names_map_matching_symbols
5510 (struct objfile *objfile,
5511 const lookup_name_info &name, domain_enum domain,
5512 int global,
5513 gdb::function_view<symbol_found_callback_ftype> callback,
5514 symbol_compare_ftype *ordered_compare)
5515 {
5516 struct dwarf2_per_objfile *dwarf2_per_objfile
5517 = get_dwarf2_per_objfile (objfile);
5518
5519 /* debug_names_table is NULL if OBJF_READNOW. */
5520 if (!dwarf2_per_objfile->debug_names_table)
5521 return;
5522
5523 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5524 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5525
5526 const char *match_name = name.ada ().lookup_name ().c_str ();
5527 auto matcher = [&] (const char *symname)
5528 {
5529 if (ordered_compare == nullptr)
5530 return true;
5531 return ordered_compare (symname, match_name) == 0;
5532 };
5533
5534 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5535 [&] (offset_type namei)
5536 {
5537 /* The name was matched, now expand corresponding CUs that were
5538 marked. */
5539 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5540
5541 struct dwarf2_per_cu_data *per_cu;
5542 while ((per_cu = iter.next ()) != NULL)
5543 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5544 return true;
5545 });
5546
5547 /* It's a shame we couldn't do this inside the
5548 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5549 that have already been expanded. Instead, this loop matches what
5550 the psymtab code does. */
5551 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5552 {
5553 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5554 if (cust != nullptr)
5555 {
5556 const struct block *block
5557 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5558 if (!iterate_over_symbols_terminated (block, name,
5559 domain, callback))
5560 break;
5561 }
5562 }
5563 }
5564
5565 static void
5566 dw2_debug_names_expand_symtabs_matching
5567 (struct objfile *objfile,
5568 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5569 const lookup_name_info &lookup_name,
5570 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5571 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5572 enum search_domain kind)
5573 {
5574 struct dwarf2_per_objfile *dwarf2_per_objfile
5575 = get_dwarf2_per_objfile (objfile);
5576
5577 /* debug_names_table is NULL if OBJF_READNOW. */
5578 if (!dwarf2_per_objfile->debug_names_table)
5579 return;
5580
5581 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5582
5583 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5584
5585 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5586 symbol_matcher,
5587 kind, [&] (offset_type namei)
5588 {
5589 /* The name was matched, now expand corresponding CUs that were
5590 marked. */
5591 dw2_debug_names_iterator iter (map, kind, namei);
5592
5593 struct dwarf2_per_cu_data *per_cu;
5594 while ((per_cu = iter.next ()) != NULL)
5595 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5596 expansion_notify);
5597 return true;
5598 });
5599 }
5600
5601 const struct quick_symbol_functions dwarf2_debug_names_functions =
5602 {
5603 dw2_has_symbols,
5604 dw2_find_last_source_symtab,
5605 dw2_forget_cached_source_info,
5606 dw2_map_symtabs_matching_filename,
5607 dw2_debug_names_lookup_symbol,
5608 dw2_print_stats,
5609 dw2_debug_names_dump,
5610 dw2_debug_names_expand_symtabs_for_function,
5611 dw2_expand_all_symtabs,
5612 dw2_expand_symtabs_with_fullname,
5613 dw2_debug_names_map_matching_symbols,
5614 dw2_debug_names_expand_symtabs_matching,
5615 dw2_find_pc_sect_compunit_symtab,
5616 NULL,
5617 dw2_map_symbol_filenames
5618 };
5619
5620 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5621 to either a dwarf2_per_objfile or dwz_file object. */
5622
5623 template <typename T>
5624 static gdb::array_view<const gdb_byte>
5625 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5626 {
5627 dwarf2_section_info *section = &section_owner->gdb_index;
5628
5629 if (section->empty ())
5630 return {};
5631
5632 /* Older elfutils strip versions could keep the section in the main
5633 executable while splitting it for the separate debug info file. */
5634 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5635 return {};
5636
5637 section->read (obj);
5638
5639 /* dwarf2_section_info::size is a bfd_size_type, while
5640 gdb::array_view works with size_t. On 32-bit hosts, with
5641 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5642 is 32-bit. So we need an explicit narrowing conversion here.
5643 This is fine, because it's impossible to allocate or mmap an
5644 array/buffer larger than what size_t can represent. */
5645 return gdb::make_array_view (section->buffer, section->size);
5646 }
5647
5648 /* Lookup the index cache for the contents of the index associated to
5649 DWARF2_OBJ. */
5650
5651 static gdb::array_view<const gdb_byte>
5652 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5653 {
5654 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5655 if (build_id == nullptr)
5656 return {};
5657
5658 return global_index_cache.lookup_gdb_index (build_id,
5659 &dwarf2_obj->index_cache_res);
5660 }
5661
5662 /* Same as the above, but for DWZ. */
5663
5664 static gdb::array_view<const gdb_byte>
5665 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5666 {
5667 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5668 if (build_id == nullptr)
5669 return {};
5670
5671 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5672 }
5673
5674 /* See symfile.h. */
5675
5676 bool
5677 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5678 {
5679 struct dwarf2_per_objfile *dwarf2_per_objfile
5680 = get_dwarf2_per_objfile (objfile);
5681
5682 /* If we're about to read full symbols, don't bother with the
5683 indices. In this case we also don't care if some other debug
5684 format is making psymtabs, because they are all about to be
5685 expanded anyway. */
5686 if ((objfile->flags & OBJF_READNOW))
5687 {
5688 dwarf2_per_objfile->using_index = 1;
5689 create_all_comp_units (dwarf2_per_objfile);
5690 create_all_type_units (dwarf2_per_objfile);
5691 dwarf2_per_objfile->quick_file_names_table
5692 = create_quick_file_names_table
5693 (dwarf2_per_objfile->all_comp_units.size ());
5694
5695 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5696 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5697 {
5698 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5699
5700 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5701 struct dwarf2_per_cu_quick_data);
5702 }
5703
5704 /* Return 1 so that gdb sees the "quick" functions. However,
5705 these functions will be no-ops because we will have expanded
5706 all symtabs. */
5707 *index_kind = dw_index_kind::GDB_INDEX;
5708 return true;
5709 }
5710
5711 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5712 {
5713 *index_kind = dw_index_kind::DEBUG_NAMES;
5714 return true;
5715 }
5716
5717 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5718 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5719 get_gdb_index_contents_from_section<dwz_file>))
5720 {
5721 *index_kind = dw_index_kind::GDB_INDEX;
5722 return true;
5723 }
5724
5725 /* ... otherwise, try to find the index in the index cache. */
5726 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5727 get_gdb_index_contents_from_cache,
5728 get_gdb_index_contents_from_cache_dwz))
5729 {
5730 global_index_cache.hit ();
5731 *index_kind = dw_index_kind::GDB_INDEX;
5732 return true;
5733 }
5734
5735 global_index_cache.miss ();
5736 return false;
5737 }
5738
5739 \f
5740
5741 /* Build a partial symbol table. */
5742
5743 void
5744 dwarf2_build_psymtabs (struct objfile *objfile)
5745 {
5746 struct dwarf2_per_objfile *dwarf2_per_objfile
5747 = get_dwarf2_per_objfile (objfile);
5748
5749 init_psymbol_list (objfile, 1024);
5750
5751 try
5752 {
5753 /* This isn't really ideal: all the data we allocate on the
5754 objfile's obstack is still uselessly kept around. However,
5755 freeing it seems unsafe. */
5756 psymtab_discarder psymtabs (objfile);
5757 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5758 psymtabs.keep ();
5759
5760 /* (maybe) store an index in the cache. */
5761 global_index_cache.store (dwarf2_per_objfile);
5762 }
5763 catch (const gdb_exception_error &except)
5764 {
5765 exception_print (gdb_stderr, except);
5766 }
5767 }
5768
5769 /* Find the base address of the compilation unit for range lists and
5770 location lists. It will normally be specified by DW_AT_low_pc.
5771 In DWARF-3 draft 4, the base address could be overridden by
5772 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5773 compilation units with discontinuous ranges. */
5774
5775 static void
5776 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5777 {
5778 struct attribute *attr;
5779
5780 cu->base_address.reset ();
5781
5782 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5783 if (attr != nullptr)
5784 cu->base_address = attr->value_as_address ();
5785 else
5786 {
5787 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5788 if (attr != nullptr)
5789 cu->base_address = attr->value_as_address ();
5790 }
5791 }
5792
5793 /* Helper function that returns the proper abbrev section for
5794 THIS_CU. */
5795
5796 static struct dwarf2_section_info *
5797 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5798 {
5799 struct dwarf2_section_info *abbrev;
5800 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5801
5802 if (this_cu->is_dwz)
5803 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5804 else
5805 abbrev = &dwarf2_per_objfile->abbrev;
5806
5807 return abbrev;
5808 }
5809
5810 /* Fetch the abbreviation table offset from a comp or type unit header. */
5811
5812 static sect_offset
5813 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5814 struct dwarf2_section_info *section,
5815 sect_offset sect_off)
5816 {
5817 bfd *abfd = section->get_bfd_owner ();
5818 const gdb_byte *info_ptr;
5819 unsigned int initial_length_size, offset_size;
5820 uint16_t version;
5821
5822 section->read (dwarf2_per_objfile->objfile);
5823 info_ptr = section->buffer + to_underlying (sect_off);
5824 read_initial_length (abfd, info_ptr, &initial_length_size);
5825 offset_size = initial_length_size == 4 ? 4 : 8;
5826 info_ptr += initial_length_size;
5827
5828 version = read_2_bytes (abfd, info_ptr);
5829 info_ptr += 2;
5830 if (version >= 5)
5831 {
5832 /* Skip unit type and address size. */
5833 info_ptr += 2;
5834 }
5835
5836 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5837 }
5838
5839 /* A partial symtab that is used only for include files. */
5840 struct dwarf2_include_psymtab : public partial_symtab
5841 {
5842 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5843 : partial_symtab (filename, objfile)
5844 {
5845 }
5846
5847 void read_symtab (struct objfile *objfile) override
5848 {
5849 expand_psymtab (objfile);
5850 }
5851
5852 void expand_psymtab (struct objfile *objfile) override
5853 {
5854 if (m_readin)
5855 return;
5856 /* It's an include file, no symbols to read for it.
5857 Everything is in the parent symtab. */
5858 read_dependencies (objfile);
5859 m_readin = true;
5860 }
5861
5862 bool readin_p () const override
5863 {
5864 return m_readin;
5865 }
5866
5867 struct compunit_symtab *get_compunit_symtab () const override
5868 {
5869 return nullptr;
5870 }
5871
5872 private:
5873
5874 bool m_readin = false;
5875 };
5876
5877 /* Allocate a new partial symtab for file named NAME and mark this new
5878 partial symtab as being an include of PST. */
5879
5880 static void
5881 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5882 struct objfile *objfile)
5883 {
5884 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5885
5886 if (!IS_ABSOLUTE_PATH (subpst->filename))
5887 {
5888 /* It shares objfile->objfile_obstack. */
5889 subpst->dirname = pst->dirname;
5890 }
5891
5892 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5893 subpst->dependencies[0] = pst;
5894 subpst->number_of_dependencies = 1;
5895 }
5896
5897 /* Read the Line Number Program data and extract the list of files
5898 included by the source file represented by PST. Build an include
5899 partial symtab for each of these included files. */
5900
5901 static void
5902 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5903 struct die_info *die,
5904 dwarf2_psymtab *pst)
5905 {
5906 line_header_up lh;
5907 struct attribute *attr;
5908
5909 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5910 if (attr != nullptr)
5911 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5912 if (lh == NULL)
5913 return; /* No linetable, so no includes. */
5914
5915 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5916 that we pass in the raw text_low here; that is ok because we're
5917 only decoding the line table to make include partial symtabs, and
5918 so the addresses aren't really used. */
5919 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5920 pst->raw_text_low (), 1);
5921 }
5922
5923 static hashval_t
5924 hash_signatured_type (const void *item)
5925 {
5926 const struct signatured_type *sig_type
5927 = (const struct signatured_type *) item;
5928
5929 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5930 return sig_type->signature;
5931 }
5932
5933 static int
5934 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5935 {
5936 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5937 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5938
5939 return lhs->signature == rhs->signature;
5940 }
5941
5942 /* Allocate a hash table for signatured types. */
5943
5944 static htab_up
5945 allocate_signatured_type_table ()
5946 {
5947 return htab_up (htab_create_alloc (41,
5948 hash_signatured_type,
5949 eq_signatured_type,
5950 NULL, xcalloc, xfree));
5951 }
5952
5953 /* A helper function to add a signatured type CU to a table. */
5954
5955 static int
5956 add_signatured_type_cu_to_table (void **slot, void *datum)
5957 {
5958 struct signatured_type *sigt = (struct signatured_type *) *slot;
5959 std::vector<signatured_type *> *all_type_units
5960 = (std::vector<signatured_type *> *) datum;
5961
5962 all_type_units->push_back (sigt);
5963
5964 return 1;
5965 }
5966
5967 /* A helper for create_debug_types_hash_table. Read types from SECTION
5968 and fill them into TYPES_HTAB. It will process only type units,
5969 therefore DW_UT_type. */
5970
5971 static void
5972 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5973 struct dwo_file *dwo_file,
5974 dwarf2_section_info *section, htab_up &types_htab,
5975 rcuh_kind section_kind)
5976 {
5977 struct objfile *objfile = dwarf2_per_objfile->objfile;
5978 struct dwarf2_section_info *abbrev_section;
5979 bfd *abfd;
5980 const gdb_byte *info_ptr, *end_ptr;
5981
5982 abbrev_section = (dwo_file != NULL
5983 ? &dwo_file->sections.abbrev
5984 : &dwarf2_per_objfile->abbrev);
5985
5986 if (dwarf_read_debug)
5987 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5988 section->get_name (),
5989 abbrev_section->get_file_name ());
5990
5991 section->read (objfile);
5992 info_ptr = section->buffer;
5993
5994 if (info_ptr == NULL)
5995 return;
5996
5997 /* We can't set abfd until now because the section may be empty or
5998 not present, in which case the bfd is unknown. */
5999 abfd = section->get_bfd_owner ();
6000
6001 /* We don't use cutu_reader here because we don't need to read
6002 any dies: the signature is in the header. */
6003
6004 end_ptr = info_ptr + section->size;
6005 while (info_ptr < end_ptr)
6006 {
6007 struct signatured_type *sig_type;
6008 struct dwo_unit *dwo_tu;
6009 void **slot;
6010 const gdb_byte *ptr = info_ptr;
6011 struct comp_unit_head header;
6012 unsigned int length;
6013
6014 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6015
6016 /* Initialize it due to a false compiler warning. */
6017 header.signature = -1;
6018 header.type_cu_offset_in_tu = (cu_offset) -1;
6019
6020 /* We need to read the type's signature in order to build the hash
6021 table, but we don't need anything else just yet. */
6022
6023 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6024 abbrev_section, ptr, section_kind);
6025
6026 length = header.get_length ();
6027
6028 /* Skip dummy type units. */
6029 if (ptr >= info_ptr + length
6030 || peek_abbrev_code (abfd, ptr) == 0
6031 || header.unit_type != DW_UT_type)
6032 {
6033 info_ptr += length;
6034 continue;
6035 }
6036
6037 if (types_htab == NULL)
6038 {
6039 if (dwo_file)
6040 types_htab = allocate_dwo_unit_table ();
6041 else
6042 types_htab = allocate_signatured_type_table ();
6043 }
6044
6045 if (dwo_file)
6046 {
6047 sig_type = NULL;
6048 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6049 struct dwo_unit);
6050 dwo_tu->dwo_file = dwo_file;
6051 dwo_tu->signature = header.signature;
6052 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6053 dwo_tu->section = section;
6054 dwo_tu->sect_off = sect_off;
6055 dwo_tu->length = length;
6056 }
6057 else
6058 {
6059 /* N.B.: type_offset is not usable if this type uses a DWO file.
6060 The real type_offset is in the DWO file. */
6061 dwo_tu = NULL;
6062 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6063 struct signatured_type);
6064 sig_type->signature = header.signature;
6065 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6066 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6067 sig_type->per_cu.is_debug_types = 1;
6068 sig_type->per_cu.section = section;
6069 sig_type->per_cu.sect_off = sect_off;
6070 sig_type->per_cu.length = length;
6071 }
6072
6073 slot = htab_find_slot (types_htab.get (),
6074 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6075 INSERT);
6076 gdb_assert (slot != NULL);
6077 if (*slot != NULL)
6078 {
6079 sect_offset dup_sect_off;
6080
6081 if (dwo_file)
6082 {
6083 const struct dwo_unit *dup_tu
6084 = (const struct dwo_unit *) *slot;
6085
6086 dup_sect_off = dup_tu->sect_off;
6087 }
6088 else
6089 {
6090 const struct signatured_type *dup_tu
6091 = (const struct signatured_type *) *slot;
6092
6093 dup_sect_off = dup_tu->per_cu.sect_off;
6094 }
6095
6096 complaint (_("debug type entry at offset %s is duplicate to"
6097 " the entry at offset %s, signature %s"),
6098 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6099 hex_string (header.signature));
6100 }
6101 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6102
6103 if (dwarf_read_debug > 1)
6104 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6105 sect_offset_str (sect_off),
6106 hex_string (header.signature));
6107
6108 info_ptr += length;
6109 }
6110 }
6111
6112 /* Create the hash table of all entries in the .debug_types
6113 (or .debug_types.dwo) section(s).
6114 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6115 otherwise it is NULL.
6116
6117 The result is a pointer to the hash table or NULL if there are no types.
6118
6119 Note: This function processes DWO files only, not DWP files. */
6120
6121 static void
6122 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6123 struct dwo_file *dwo_file,
6124 gdb::array_view<dwarf2_section_info> type_sections,
6125 htab_up &types_htab)
6126 {
6127 for (dwarf2_section_info &section : type_sections)
6128 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6129 types_htab, rcuh_kind::TYPE);
6130 }
6131
6132 /* Create the hash table of all entries in the .debug_types section,
6133 and initialize all_type_units.
6134 The result is zero if there is an error (e.g. missing .debug_types section),
6135 otherwise non-zero. */
6136
6137 static int
6138 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6139 {
6140 htab_up types_htab;
6141
6142 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6143 &dwarf2_per_objfile->info, types_htab,
6144 rcuh_kind::COMPILE);
6145 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6146 dwarf2_per_objfile->types, types_htab);
6147 if (types_htab == NULL)
6148 {
6149 dwarf2_per_objfile->signatured_types = NULL;
6150 return 0;
6151 }
6152
6153 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6154
6155 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6156 dwarf2_per_objfile->all_type_units.reserve
6157 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6158
6159 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6160 add_signatured_type_cu_to_table,
6161 &dwarf2_per_objfile->all_type_units);
6162
6163 return 1;
6164 }
6165
6166 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6167 If SLOT is non-NULL, it is the entry to use in the hash table.
6168 Otherwise we find one. */
6169
6170 static struct signatured_type *
6171 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6172 void **slot)
6173 {
6174 struct objfile *objfile = dwarf2_per_objfile->objfile;
6175
6176 if (dwarf2_per_objfile->all_type_units.size ()
6177 == dwarf2_per_objfile->all_type_units.capacity ())
6178 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6179
6180 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6181 struct signatured_type);
6182
6183 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6184 sig_type->signature = sig;
6185 sig_type->per_cu.is_debug_types = 1;
6186 if (dwarf2_per_objfile->using_index)
6187 {
6188 sig_type->per_cu.v.quick =
6189 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6190 struct dwarf2_per_cu_quick_data);
6191 }
6192
6193 if (slot == NULL)
6194 {
6195 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6196 sig_type, INSERT);
6197 }
6198 gdb_assert (*slot == NULL);
6199 *slot = sig_type;
6200 /* The rest of sig_type must be filled in by the caller. */
6201 return sig_type;
6202 }
6203
6204 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6205 Fill in SIG_ENTRY with DWO_ENTRY. */
6206
6207 static void
6208 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6209 struct signatured_type *sig_entry,
6210 struct dwo_unit *dwo_entry)
6211 {
6212 /* Make sure we're not clobbering something we don't expect to. */
6213 gdb_assert (! sig_entry->per_cu.queued);
6214 gdb_assert (sig_entry->per_cu.cu == NULL);
6215 if (dwarf2_per_objfile->using_index)
6216 {
6217 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6218 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6219 }
6220 else
6221 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6222 gdb_assert (sig_entry->signature == dwo_entry->signature);
6223 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6224 gdb_assert (sig_entry->type_unit_group == NULL);
6225 gdb_assert (sig_entry->dwo_unit == NULL);
6226
6227 sig_entry->per_cu.section = dwo_entry->section;
6228 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6229 sig_entry->per_cu.length = dwo_entry->length;
6230 sig_entry->per_cu.reading_dwo_directly = 1;
6231 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6232 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6233 sig_entry->dwo_unit = dwo_entry;
6234 }
6235
6236 /* Subroutine of lookup_signatured_type.
6237 If we haven't read the TU yet, create the signatured_type data structure
6238 for a TU to be read in directly from a DWO file, bypassing the stub.
6239 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6240 using .gdb_index, then when reading a CU we want to stay in the DWO file
6241 containing that CU. Otherwise we could end up reading several other DWO
6242 files (due to comdat folding) to process the transitive closure of all the
6243 mentioned TUs, and that can be slow. The current DWO file will have every
6244 type signature that it needs.
6245 We only do this for .gdb_index because in the psymtab case we already have
6246 to read all the DWOs to build the type unit groups. */
6247
6248 static struct signatured_type *
6249 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6250 {
6251 struct dwarf2_per_objfile *dwarf2_per_objfile
6252 = cu->per_cu->dwarf2_per_objfile;
6253 struct dwo_file *dwo_file;
6254 struct dwo_unit find_dwo_entry, *dwo_entry;
6255 struct signatured_type find_sig_entry, *sig_entry;
6256 void **slot;
6257
6258 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6259
6260 /* If TU skeletons have been removed then we may not have read in any
6261 TUs yet. */
6262 if (dwarf2_per_objfile->signatured_types == NULL)
6263 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6264
6265 /* We only ever need to read in one copy of a signatured type.
6266 Use the global signatured_types array to do our own comdat-folding
6267 of types. If this is the first time we're reading this TU, and
6268 the TU has an entry in .gdb_index, replace the recorded data from
6269 .gdb_index with this TU. */
6270
6271 find_sig_entry.signature = sig;
6272 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6273 &find_sig_entry, INSERT);
6274 sig_entry = (struct signatured_type *) *slot;
6275
6276 /* We can get here with the TU already read, *or* in the process of being
6277 read. Don't reassign the global entry to point to this DWO if that's
6278 the case. Also note that if the TU is already being read, it may not
6279 have come from a DWO, the program may be a mix of Fission-compiled
6280 code and non-Fission-compiled code. */
6281
6282 /* Have we already tried to read this TU?
6283 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6284 needn't exist in the global table yet). */
6285 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6286 return sig_entry;
6287
6288 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6289 dwo_unit of the TU itself. */
6290 dwo_file = cu->dwo_unit->dwo_file;
6291
6292 /* Ok, this is the first time we're reading this TU. */
6293 if (dwo_file->tus == NULL)
6294 return NULL;
6295 find_dwo_entry.signature = sig;
6296 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6297 &find_dwo_entry);
6298 if (dwo_entry == NULL)
6299 return NULL;
6300
6301 /* If the global table doesn't have an entry for this TU, add one. */
6302 if (sig_entry == NULL)
6303 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6304
6305 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6306 sig_entry->per_cu.tu_read = 1;
6307 return sig_entry;
6308 }
6309
6310 /* Subroutine of lookup_signatured_type.
6311 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6312 then try the DWP file. If the TU stub (skeleton) has been removed then
6313 it won't be in .gdb_index. */
6314
6315 static struct signatured_type *
6316 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6317 {
6318 struct dwarf2_per_objfile *dwarf2_per_objfile
6319 = cu->per_cu->dwarf2_per_objfile;
6320 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6321 struct dwo_unit *dwo_entry;
6322 struct signatured_type find_sig_entry, *sig_entry;
6323 void **slot;
6324
6325 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6326 gdb_assert (dwp_file != NULL);
6327
6328 /* If TU skeletons have been removed then we may not have read in any
6329 TUs yet. */
6330 if (dwarf2_per_objfile->signatured_types == NULL)
6331 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6332
6333 find_sig_entry.signature = sig;
6334 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6335 &find_sig_entry, INSERT);
6336 sig_entry = (struct signatured_type *) *slot;
6337
6338 /* Have we already tried to read this TU?
6339 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6340 needn't exist in the global table yet). */
6341 if (sig_entry != NULL)
6342 return sig_entry;
6343
6344 if (dwp_file->tus == NULL)
6345 return NULL;
6346 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6347 sig, 1 /* is_debug_types */);
6348 if (dwo_entry == NULL)
6349 return NULL;
6350
6351 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6352 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6353
6354 return sig_entry;
6355 }
6356
6357 /* Lookup a signature based type for DW_FORM_ref_sig8.
6358 Returns NULL if signature SIG is not present in the table.
6359 It is up to the caller to complain about this. */
6360
6361 static struct signatured_type *
6362 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6363 {
6364 struct dwarf2_per_objfile *dwarf2_per_objfile
6365 = cu->per_cu->dwarf2_per_objfile;
6366
6367 if (cu->dwo_unit
6368 && dwarf2_per_objfile->using_index)
6369 {
6370 /* We're in a DWO/DWP file, and we're using .gdb_index.
6371 These cases require special processing. */
6372 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6373 return lookup_dwo_signatured_type (cu, sig);
6374 else
6375 return lookup_dwp_signatured_type (cu, sig);
6376 }
6377 else
6378 {
6379 struct signatured_type find_entry, *entry;
6380
6381 if (dwarf2_per_objfile->signatured_types == NULL)
6382 return NULL;
6383 find_entry.signature = sig;
6384 entry = ((struct signatured_type *)
6385 htab_find (dwarf2_per_objfile->signatured_types.get (),
6386 &find_entry));
6387 return entry;
6388 }
6389 }
6390
6391 /* Return the address base of the compile unit, which, if exists, is stored
6392 either at the attribute DW_AT_GNU_addr_base, or DW_AT_addr_base. */
6393 static gdb::optional<ULONGEST>
6394 lookup_addr_base (struct die_info *comp_unit_die)
6395 {
6396 struct attribute *attr;
6397 attr = comp_unit_die->attr (DW_AT_addr_base);
6398 if (attr == nullptr)
6399 attr = comp_unit_die->attr (DW_AT_GNU_addr_base);
6400 if (attr == nullptr)
6401 return gdb::optional<ULONGEST> ();
6402 return DW_UNSND (attr);
6403 }
6404
6405 /* Return range lists base of the compile unit, which, if exists, is stored
6406 either at the attribute DW_AT_rnglists_base or DW_AT_GNU_ranges_base. */
6407 static ULONGEST
6408 lookup_ranges_base (struct die_info *comp_unit_die)
6409 {
6410 struct attribute *attr;
6411 attr = comp_unit_die->attr (DW_AT_rnglists_base);
6412 if (attr == nullptr)
6413 attr = comp_unit_die->attr (DW_AT_GNU_ranges_base);
6414 if (attr == nullptr)
6415 return 0;
6416 return DW_UNSND (attr);
6417 }
6418
6419 /* Low level DIE reading support. */
6420
6421 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6422
6423 static void
6424 init_cu_die_reader (struct die_reader_specs *reader,
6425 struct dwarf2_cu *cu,
6426 struct dwarf2_section_info *section,
6427 struct dwo_file *dwo_file,
6428 struct abbrev_table *abbrev_table)
6429 {
6430 gdb_assert (section->readin && section->buffer != NULL);
6431 reader->abfd = section->get_bfd_owner ();
6432 reader->cu = cu;
6433 reader->dwo_file = dwo_file;
6434 reader->die_section = section;
6435 reader->buffer = section->buffer;
6436 reader->buffer_end = section->buffer + section->size;
6437 reader->abbrev_table = abbrev_table;
6438 }
6439
6440 /* Subroutine of cutu_reader to simplify it.
6441 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6442 There's just a lot of work to do, and cutu_reader is big enough
6443 already.
6444
6445 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6446 from it to the DIE in the DWO. If NULL we are skipping the stub.
6447 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6448 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6449 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6450 STUB_COMP_DIR may be non-NULL.
6451 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6452 are filled in with the info of the DIE from the DWO file.
6453 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6454 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6455 kept around for at least as long as *RESULT_READER.
6456
6457 The result is non-zero if a valid (non-dummy) DIE was found. */
6458
6459 static int
6460 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6461 struct dwo_unit *dwo_unit,
6462 struct die_info *stub_comp_unit_die,
6463 const char *stub_comp_dir,
6464 struct die_reader_specs *result_reader,
6465 const gdb_byte **result_info_ptr,
6466 struct die_info **result_comp_unit_die,
6467 abbrev_table_up *result_dwo_abbrev_table)
6468 {
6469 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6470 struct objfile *objfile = dwarf2_per_objfile->objfile;
6471 struct dwarf2_cu *cu = this_cu->cu;
6472 bfd *abfd;
6473 const gdb_byte *begin_info_ptr, *info_ptr;
6474 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6475 int i,num_extra_attrs;
6476 struct dwarf2_section_info *dwo_abbrev_section;
6477 struct die_info *comp_unit_die;
6478
6479 /* At most one of these may be provided. */
6480 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6481
6482 /* These attributes aren't processed until later:
6483 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6484 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6485 referenced later. However, these attributes are found in the stub
6486 which we won't have later. In order to not impose this complication
6487 on the rest of the code, we read them here and copy them to the
6488 DWO CU/TU die. */
6489
6490 stmt_list = NULL;
6491 low_pc = NULL;
6492 high_pc = NULL;
6493 ranges = NULL;
6494 comp_dir = NULL;
6495
6496 if (stub_comp_unit_die != NULL)
6497 {
6498 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6499 DWO file. */
6500 if (! this_cu->is_debug_types)
6501 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6502 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6503 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6504 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6505 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6506
6507 cu->addr_base = lookup_addr_base (stub_comp_unit_die);
6508
6509 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6510 here (if needed). We need the value before we can process
6511 DW_AT_ranges. */
6512 cu->ranges_base = lookup_ranges_base (stub_comp_unit_die);
6513 }
6514 else if (stub_comp_dir != NULL)
6515 {
6516 /* Reconstruct the comp_dir attribute to simplify the code below. */
6517 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6518 comp_dir->name = DW_AT_comp_dir;
6519 comp_dir->form = DW_FORM_string;
6520 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6521 DW_STRING (comp_dir) = stub_comp_dir;
6522 }
6523
6524 /* Set up for reading the DWO CU/TU. */
6525 cu->dwo_unit = dwo_unit;
6526 dwarf2_section_info *section = dwo_unit->section;
6527 section->read (objfile);
6528 abfd = section->get_bfd_owner ();
6529 begin_info_ptr = info_ptr = (section->buffer
6530 + to_underlying (dwo_unit->sect_off));
6531 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6532
6533 if (this_cu->is_debug_types)
6534 {
6535 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6536
6537 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6538 &cu->header, section,
6539 dwo_abbrev_section,
6540 info_ptr, rcuh_kind::TYPE);
6541 /* This is not an assert because it can be caused by bad debug info. */
6542 if (sig_type->signature != cu->header.signature)
6543 {
6544 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6545 " TU at offset %s [in module %s]"),
6546 hex_string (sig_type->signature),
6547 hex_string (cu->header.signature),
6548 sect_offset_str (dwo_unit->sect_off),
6549 bfd_get_filename (abfd));
6550 }
6551 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6552 /* For DWOs coming from DWP files, we don't know the CU length
6553 nor the type's offset in the TU until now. */
6554 dwo_unit->length = cu->header.get_length ();
6555 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6556
6557 /* Establish the type offset that can be used to lookup the type.
6558 For DWO files, we don't know it until now. */
6559 sig_type->type_offset_in_section
6560 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6561 }
6562 else
6563 {
6564 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6565 &cu->header, section,
6566 dwo_abbrev_section,
6567 info_ptr, rcuh_kind::COMPILE);
6568 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6569 /* For DWOs coming from DWP files, we don't know the CU length
6570 until now. */
6571 dwo_unit->length = cu->header.get_length ();
6572 }
6573
6574 *result_dwo_abbrev_table
6575 = abbrev_table::read (objfile, dwo_abbrev_section,
6576 cu->header.abbrev_sect_off);
6577 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6578 result_dwo_abbrev_table->get ());
6579
6580 /* Read in the die, but leave space to copy over the attributes
6581 from the stub. This has the benefit of simplifying the rest of
6582 the code - all the work to maintain the illusion of a single
6583 DW_TAG_{compile,type}_unit DIE is done here. */
6584 num_extra_attrs = ((stmt_list != NULL)
6585 + (low_pc != NULL)
6586 + (high_pc != NULL)
6587 + (ranges != NULL)
6588 + (comp_dir != NULL));
6589 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6590 num_extra_attrs);
6591
6592 /* Copy over the attributes from the stub to the DIE we just read in. */
6593 comp_unit_die = *result_comp_unit_die;
6594 i = comp_unit_die->num_attrs;
6595 if (stmt_list != NULL)
6596 comp_unit_die->attrs[i++] = *stmt_list;
6597 if (low_pc != NULL)
6598 comp_unit_die->attrs[i++] = *low_pc;
6599 if (high_pc != NULL)
6600 comp_unit_die->attrs[i++] = *high_pc;
6601 if (ranges != NULL)
6602 comp_unit_die->attrs[i++] = *ranges;
6603 if (comp_dir != NULL)
6604 comp_unit_die->attrs[i++] = *comp_dir;
6605 comp_unit_die->num_attrs += num_extra_attrs;
6606
6607 if (dwarf_die_debug)
6608 {
6609 fprintf_unfiltered (gdb_stdlog,
6610 "Read die from %s@0x%x of %s:\n",
6611 section->get_name (),
6612 (unsigned) (begin_info_ptr - section->buffer),
6613 bfd_get_filename (abfd));
6614 dump_die (comp_unit_die, dwarf_die_debug);
6615 }
6616
6617 /* Skip dummy compilation units. */
6618 if (info_ptr >= begin_info_ptr + dwo_unit->length
6619 || peek_abbrev_code (abfd, info_ptr) == 0)
6620 return 0;
6621
6622 *result_info_ptr = info_ptr;
6623 return 1;
6624 }
6625
6626 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6627 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6628 signature is part of the header. */
6629 static gdb::optional<ULONGEST>
6630 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6631 {
6632 if (cu->header.version >= 5)
6633 return cu->header.signature;
6634 struct attribute *attr;
6635 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6636 if (attr == nullptr)
6637 return gdb::optional<ULONGEST> ();
6638 return DW_UNSND (attr);
6639 }
6640
6641 /* Subroutine of cutu_reader to simplify it.
6642 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6643 Returns NULL if the specified DWO unit cannot be found. */
6644
6645 static struct dwo_unit *
6646 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6647 struct die_info *comp_unit_die,
6648 const char *dwo_name)
6649 {
6650 struct dwarf2_cu *cu = this_cu->cu;
6651 struct dwo_unit *dwo_unit;
6652 const char *comp_dir;
6653
6654 gdb_assert (cu != NULL);
6655
6656 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6657 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6658 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6659
6660 if (this_cu->is_debug_types)
6661 {
6662 struct signatured_type *sig_type;
6663
6664 /* Since this_cu is the first member of struct signatured_type,
6665 we can go from a pointer to one to a pointer to the other. */
6666 sig_type = (struct signatured_type *) this_cu;
6667 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6668 }
6669 else
6670 {
6671 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6672 if (!signature.has_value ())
6673 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6674 " [in module %s]"),
6675 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6676 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6677 *signature);
6678 }
6679
6680 return dwo_unit;
6681 }
6682
6683 /* Subroutine of cutu_reader to simplify it.
6684 See it for a description of the parameters.
6685 Read a TU directly from a DWO file, bypassing the stub. */
6686
6687 void
6688 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6689 int use_existing_cu)
6690 {
6691 struct signatured_type *sig_type;
6692
6693 /* Verify we can do the following downcast, and that we have the
6694 data we need. */
6695 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6696 sig_type = (struct signatured_type *) this_cu;
6697 gdb_assert (sig_type->dwo_unit != NULL);
6698
6699 if (use_existing_cu && this_cu->cu != NULL)
6700 {
6701 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6702 /* There's no need to do the rereading_dwo_cu handling that
6703 cutu_reader does since we don't read the stub. */
6704 }
6705 else
6706 {
6707 /* If !use_existing_cu, this_cu->cu must be NULL. */
6708 gdb_assert (this_cu->cu == NULL);
6709 m_new_cu.reset (new dwarf2_cu (this_cu));
6710 }
6711
6712 /* A future optimization, if needed, would be to use an existing
6713 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6714 could share abbrev tables. */
6715
6716 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6717 NULL /* stub_comp_unit_die */,
6718 sig_type->dwo_unit->dwo_file->comp_dir,
6719 this, &info_ptr,
6720 &comp_unit_die,
6721 &m_dwo_abbrev_table) == 0)
6722 {
6723 /* Dummy die. */
6724 dummy_p = true;
6725 }
6726 }
6727
6728 /* Initialize a CU (or TU) and read its DIEs.
6729 If the CU defers to a DWO file, read the DWO file as well.
6730
6731 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6732 Otherwise the table specified in the comp unit header is read in and used.
6733 This is an optimization for when we already have the abbrev table.
6734
6735 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6736 Otherwise, a new CU is allocated with xmalloc. */
6737
6738 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6739 struct abbrev_table *abbrev_table,
6740 int use_existing_cu,
6741 bool skip_partial)
6742 : die_reader_specs {},
6743 m_this_cu (this_cu)
6744 {
6745 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6746 struct objfile *objfile = dwarf2_per_objfile->objfile;
6747 struct dwarf2_section_info *section = this_cu->section;
6748 bfd *abfd = section->get_bfd_owner ();
6749 struct dwarf2_cu *cu;
6750 const gdb_byte *begin_info_ptr;
6751 struct signatured_type *sig_type = NULL;
6752 struct dwarf2_section_info *abbrev_section;
6753 /* Non-zero if CU currently points to a DWO file and we need to
6754 reread it. When this happens we need to reread the skeleton die
6755 before we can reread the DWO file (this only applies to CUs, not TUs). */
6756 int rereading_dwo_cu = 0;
6757
6758 if (dwarf_die_debug)
6759 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6760 this_cu->is_debug_types ? "type" : "comp",
6761 sect_offset_str (this_cu->sect_off));
6762
6763 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6764 file (instead of going through the stub), short-circuit all of this. */
6765 if (this_cu->reading_dwo_directly)
6766 {
6767 /* Narrow down the scope of possibilities to have to understand. */
6768 gdb_assert (this_cu->is_debug_types);
6769 gdb_assert (abbrev_table == NULL);
6770 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6771 return;
6772 }
6773
6774 /* This is cheap if the section is already read in. */
6775 section->read (objfile);
6776
6777 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6778
6779 abbrev_section = get_abbrev_section_for_cu (this_cu);
6780
6781 if (use_existing_cu && this_cu->cu != NULL)
6782 {
6783 cu = this_cu->cu;
6784 /* If this CU is from a DWO file we need to start over, we need to
6785 refetch the attributes from the skeleton CU.
6786 This could be optimized by retrieving those attributes from when we
6787 were here the first time: the previous comp_unit_die was stored in
6788 comp_unit_obstack. But there's no data yet that we need this
6789 optimization. */
6790 if (cu->dwo_unit != NULL)
6791 rereading_dwo_cu = 1;
6792 }
6793 else
6794 {
6795 /* If !use_existing_cu, this_cu->cu must be NULL. */
6796 gdb_assert (this_cu->cu == NULL);
6797 m_new_cu.reset (new dwarf2_cu (this_cu));
6798 cu = m_new_cu.get ();
6799 }
6800
6801 /* Get the header. */
6802 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6803 {
6804 /* We already have the header, there's no need to read it in again. */
6805 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6806 }
6807 else
6808 {
6809 if (this_cu->is_debug_types)
6810 {
6811 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6812 &cu->header, section,
6813 abbrev_section, info_ptr,
6814 rcuh_kind::TYPE);
6815
6816 /* Since per_cu is the first member of struct signatured_type,
6817 we can go from a pointer to one to a pointer to the other. */
6818 sig_type = (struct signatured_type *) this_cu;
6819 gdb_assert (sig_type->signature == cu->header.signature);
6820 gdb_assert (sig_type->type_offset_in_tu
6821 == cu->header.type_cu_offset_in_tu);
6822 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6823
6824 /* LENGTH has not been set yet for type units if we're
6825 using .gdb_index. */
6826 this_cu->length = cu->header.get_length ();
6827
6828 /* Establish the type offset that can be used to lookup the type. */
6829 sig_type->type_offset_in_section =
6830 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6831
6832 this_cu->dwarf_version = cu->header.version;
6833 }
6834 else
6835 {
6836 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6837 &cu->header, section,
6838 abbrev_section,
6839 info_ptr,
6840 rcuh_kind::COMPILE);
6841
6842 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6843 gdb_assert (this_cu->length == cu->header.get_length ());
6844 this_cu->dwarf_version = cu->header.version;
6845 }
6846 }
6847
6848 /* Skip dummy compilation units. */
6849 if (info_ptr >= begin_info_ptr + this_cu->length
6850 || peek_abbrev_code (abfd, info_ptr) == 0)
6851 {
6852 dummy_p = true;
6853 return;
6854 }
6855
6856 /* If we don't have them yet, read the abbrevs for this compilation unit.
6857 And if we need to read them now, make sure they're freed when we're
6858 done. */
6859 if (abbrev_table != NULL)
6860 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6861 else
6862 {
6863 m_abbrev_table_holder
6864 = abbrev_table::read (objfile, abbrev_section,
6865 cu->header.abbrev_sect_off);
6866 abbrev_table = m_abbrev_table_holder.get ();
6867 }
6868
6869 /* Read the top level CU/TU die. */
6870 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6871 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6872
6873 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6874 {
6875 dummy_p = true;
6876 return;
6877 }
6878
6879 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6880 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6881 table from the DWO file and pass the ownership over to us. It will be
6882 referenced from READER, so we must make sure to free it after we're done
6883 with READER.
6884
6885 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6886 DWO CU, that this test will fail (the attribute will not be present). */
6887 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6888 if (dwo_name != nullptr)
6889 {
6890 struct dwo_unit *dwo_unit;
6891 struct die_info *dwo_comp_unit_die;
6892
6893 if (comp_unit_die->has_children)
6894 {
6895 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6896 " has children (offset %s) [in module %s]"),
6897 sect_offset_str (this_cu->sect_off),
6898 bfd_get_filename (abfd));
6899 }
6900 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6901 if (dwo_unit != NULL)
6902 {
6903 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6904 comp_unit_die, NULL,
6905 this, &info_ptr,
6906 &dwo_comp_unit_die,
6907 &m_dwo_abbrev_table) == 0)
6908 {
6909 /* Dummy die. */
6910 dummy_p = true;
6911 return;
6912 }
6913 comp_unit_die = dwo_comp_unit_die;
6914 }
6915 else
6916 {
6917 /* Yikes, we couldn't find the rest of the DIE, we only have
6918 the stub. A complaint has already been logged. There's
6919 not much more we can do except pass on the stub DIE to
6920 die_reader_func. We don't want to throw an error on bad
6921 debug info. */
6922 }
6923 }
6924 }
6925
6926 void
6927 cutu_reader::keep ()
6928 {
6929 /* Done, clean up. */
6930 gdb_assert (!dummy_p);
6931 if (m_new_cu != NULL)
6932 {
6933 struct dwarf2_per_objfile *dwarf2_per_objfile
6934 = m_this_cu->dwarf2_per_objfile;
6935 /* Link this CU into read_in_chain. */
6936 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6937 dwarf2_per_objfile->read_in_chain = m_this_cu;
6938 /* The chain owns it now. */
6939 m_new_cu.release ();
6940 }
6941 }
6942
6943 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6944 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6945 assumed to have already done the lookup to find the DWO file).
6946
6947 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6948 THIS_CU->is_debug_types, but nothing else.
6949
6950 We fill in THIS_CU->length.
6951
6952 THIS_CU->cu is always freed when done.
6953 This is done in order to not leave THIS_CU->cu in a state where we have
6954 to care whether it refers to the "main" CU or the DWO CU.
6955
6956 When parent_cu is passed, it is used to provide a default value for
6957 str_offsets_base and addr_base from the parent. */
6958
6959 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6960 struct dwarf2_cu *parent_cu,
6961 struct dwo_file *dwo_file)
6962 : die_reader_specs {},
6963 m_this_cu (this_cu)
6964 {
6965 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6966 struct objfile *objfile = dwarf2_per_objfile->objfile;
6967 struct dwarf2_section_info *section = this_cu->section;
6968 bfd *abfd = section->get_bfd_owner ();
6969 struct dwarf2_section_info *abbrev_section;
6970 const gdb_byte *begin_info_ptr, *info_ptr;
6971
6972 if (dwarf_die_debug)
6973 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6974 this_cu->is_debug_types ? "type" : "comp",
6975 sect_offset_str (this_cu->sect_off));
6976
6977 gdb_assert (this_cu->cu == NULL);
6978
6979 abbrev_section = (dwo_file != NULL
6980 ? &dwo_file->sections.abbrev
6981 : get_abbrev_section_for_cu (this_cu));
6982
6983 /* This is cheap if the section is already read in. */
6984 section->read (objfile);
6985
6986 m_new_cu.reset (new dwarf2_cu (this_cu));
6987
6988 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6989 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6990 &m_new_cu->header, section,
6991 abbrev_section, info_ptr,
6992 (this_cu->is_debug_types
6993 ? rcuh_kind::TYPE
6994 : rcuh_kind::COMPILE));
6995
6996 if (parent_cu != nullptr)
6997 {
6998 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6999 m_new_cu->addr_base = parent_cu->addr_base;
7000 }
7001 this_cu->length = m_new_cu->header.get_length ();
7002
7003 /* Skip dummy compilation units. */
7004 if (info_ptr >= begin_info_ptr + this_cu->length
7005 || peek_abbrev_code (abfd, info_ptr) == 0)
7006 {
7007 dummy_p = true;
7008 return;
7009 }
7010
7011 m_abbrev_table_holder
7012 = abbrev_table::read (objfile, abbrev_section,
7013 m_new_cu->header.abbrev_sect_off);
7014
7015 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7016 m_abbrev_table_holder.get ());
7017 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7018 }
7019
7020 \f
7021 /* Type Unit Groups.
7022
7023 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7024 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7025 so that all types coming from the same compilation (.o file) are grouped
7026 together. A future step could be to put the types in the same symtab as
7027 the CU the types ultimately came from. */
7028
7029 static hashval_t
7030 hash_type_unit_group (const void *item)
7031 {
7032 const struct type_unit_group *tu_group
7033 = (const struct type_unit_group *) item;
7034
7035 return hash_stmt_list_entry (&tu_group->hash);
7036 }
7037
7038 static int
7039 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7040 {
7041 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7042 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7043
7044 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7045 }
7046
7047 /* Allocate a hash table for type unit groups. */
7048
7049 static htab_up
7050 allocate_type_unit_groups_table ()
7051 {
7052 return htab_up (htab_create_alloc (3,
7053 hash_type_unit_group,
7054 eq_type_unit_group,
7055 NULL, xcalloc, xfree));
7056 }
7057
7058 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7059 partial symtabs. We combine several TUs per psymtab to not let the size
7060 of any one psymtab grow too big. */
7061 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7062 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7063
7064 /* Helper routine for get_type_unit_group.
7065 Create the type_unit_group object used to hold one or more TUs. */
7066
7067 static struct type_unit_group *
7068 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7069 {
7070 struct dwarf2_per_objfile *dwarf2_per_objfile
7071 = cu->per_cu->dwarf2_per_objfile;
7072 struct objfile *objfile = dwarf2_per_objfile->objfile;
7073 struct dwarf2_per_cu_data *per_cu;
7074 struct type_unit_group *tu_group;
7075
7076 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7077 struct type_unit_group);
7078 per_cu = &tu_group->per_cu;
7079 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7080
7081 if (dwarf2_per_objfile->using_index)
7082 {
7083 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7084 struct dwarf2_per_cu_quick_data);
7085 }
7086 else
7087 {
7088 unsigned int line_offset = to_underlying (line_offset_struct);
7089 dwarf2_psymtab *pst;
7090 std::string name;
7091
7092 /* Give the symtab a useful name for debug purposes. */
7093 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7094 name = string_printf ("<type_units_%d>",
7095 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7096 else
7097 name = string_printf ("<type_units_at_0x%x>", line_offset);
7098
7099 pst = create_partial_symtab (per_cu, name.c_str ());
7100 pst->anonymous = true;
7101 }
7102
7103 tu_group->hash.dwo_unit = cu->dwo_unit;
7104 tu_group->hash.line_sect_off = line_offset_struct;
7105
7106 return tu_group;
7107 }
7108
7109 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7110 STMT_LIST is a DW_AT_stmt_list attribute. */
7111
7112 static struct type_unit_group *
7113 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7114 {
7115 struct dwarf2_per_objfile *dwarf2_per_objfile
7116 = cu->per_cu->dwarf2_per_objfile;
7117 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7118 struct type_unit_group *tu_group;
7119 void **slot;
7120 unsigned int line_offset;
7121 struct type_unit_group type_unit_group_for_lookup;
7122
7123 if (dwarf2_per_objfile->type_unit_groups == NULL)
7124 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7125
7126 /* Do we need to create a new group, or can we use an existing one? */
7127
7128 if (stmt_list)
7129 {
7130 line_offset = DW_UNSND (stmt_list);
7131 ++tu_stats->nr_symtab_sharers;
7132 }
7133 else
7134 {
7135 /* Ugh, no stmt_list. Rare, but we have to handle it.
7136 We can do various things here like create one group per TU or
7137 spread them over multiple groups to split up the expansion work.
7138 To avoid worst case scenarios (too many groups or too large groups)
7139 we, umm, group them in bunches. */
7140 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7141 | (tu_stats->nr_stmt_less_type_units
7142 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7143 ++tu_stats->nr_stmt_less_type_units;
7144 }
7145
7146 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7147 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7148 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7149 &type_unit_group_for_lookup, INSERT);
7150 if (*slot != NULL)
7151 {
7152 tu_group = (struct type_unit_group *) *slot;
7153 gdb_assert (tu_group != NULL);
7154 }
7155 else
7156 {
7157 sect_offset line_offset_struct = (sect_offset) line_offset;
7158 tu_group = create_type_unit_group (cu, line_offset_struct);
7159 *slot = tu_group;
7160 ++tu_stats->nr_symtabs;
7161 }
7162
7163 return tu_group;
7164 }
7165 \f
7166 /* Partial symbol tables. */
7167
7168 /* Create a psymtab named NAME and assign it to PER_CU.
7169
7170 The caller must fill in the following details:
7171 dirname, textlow, texthigh. */
7172
7173 static dwarf2_psymtab *
7174 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7175 {
7176 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7177 dwarf2_psymtab *pst;
7178
7179 pst = new dwarf2_psymtab (name, objfile, 0);
7180
7181 pst->psymtabs_addrmap_supported = true;
7182
7183 /* This is the glue that links PST into GDB's symbol API. */
7184 pst->per_cu_data = per_cu;
7185 per_cu->v.psymtab = pst;
7186
7187 return pst;
7188 }
7189
7190 /* DIE reader function for process_psymtab_comp_unit. */
7191
7192 static void
7193 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7194 const gdb_byte *info_ptr,
7195 struct die_info *comp_unit_die,
7196 enum language pretend_language)
7197 {
7198 struct dwarf2_cu *cu = reader->cu;
7199 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7200 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7201 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7202 CORE_ADDR baseaddr;
7203 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7204 dwarf2_psymtab *pst;
7205 enum pc_bounds_kind cu_bounds_kind;
7206 const char *filename;
7207
7208 gdb_assert (! per_cu->is_debug_types);
7209
7210 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7211
7212 /* Allocate a new partial symbol table structure. */
7213 gdb::unique_xmalloc_ptr<char> debug_filename;
7214 static const char artificial[] = "<artificial>";
7215 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7216 if (filename == NULL)
7217 filename = "";
7218 else if (strcmp (filename, artificial) == 0)
7219 {
7220 debug_filename.reset (concat (artificial, "@",
7221 sect_offset_str (per_cu->sect_off),
7222 (char *) NULL));
7223 filename = debug_filename.get ();
7224 }
7225
7226 pst = create_partial_symtab (per_cu, filename);
7227
7228 /* This must be done before calling dwarf2_build_include_psymtabs. */
7229 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7230
7231 baseaddr = objfile->text_section_offset ();
7232
7233 dwarf2_find_base_address (comp_unit_die, cu);
7234
7235 /* Possibly set the default values of LOWPC and HIGHPC from
7236 `DW_AT_ranges'. */
7237 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7238 &best_highpc, cu, pst);
7239 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7240 {
7241 CORE_ADDR low
7242 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7243 - baseaddr);
7244 CORE_ADDR high
7245 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7246 - baseaddr - 1);
7247 /* Store the contiguous range if it is not empty; it can be
7248 empty for CUs with no code. */
7249 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7250 low, high, pst);
7251 }
7252
7253 /* Check if comp unit has_children.
7254 If so, read the rest of the partial symbols from this comp unit.
7255 If not, there's no more debug_info for this comp unit. */
7256 if (comp_unit_die->has_children)
7257 {
7258 struct partial_die_info *first_die;
7259 CORE_ADDR lowpc, highpc;
7260
7261 lowpc = ((CORE_ADDR) -1);
7262 highpc = ((CORE_ADDR) 0);
7263
7264 first_die = load_partial_dies (reader, info_ptr, 1);
7265
7266 scan_partial_symbols (first_die, &lowpc, &highpc,
7267 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7268
7269 /* If we didn't find a lowpc, set it to highpc to avoid
7270 complaints from `maint check'. */
7271 if (lowpc == ((CORE_ADDR) -1))
7272 lowpc = highpc;
7273
7274 /* If the compilation unit didn't have an explicit address range,
7275 then use the information extracted from its child dies. */
7276 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7277 {
7278 best_lowpc = lowpc;
7279 best_highpc = highpc;
7280 }
7281 }
7282 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7283 best_lowpc + baseaddr)
7284 - baseaddr);
7285 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7286 best_highpc + baseaddr)
7287 - baseaddr);
7288
7289 end_psymtab_common (objfile, pst);
7290
7291 if (!cu->per_cu->imported_symtabs_empty ())
7292 {
7293 int i;
7294 int len = cu->per_cu->imported_symtabs_size ();
7295
7296 /* Fill in 'dependencies' here; we fill in 'users' in a
7297 post-pass. */
7298 pst->number_of_dependencies = len;
7299 pst->dependencies
7300 = objfile->partial_symtabs->allocate_dependencies (len);
7301 for (i = 0; i < len; ++i)
7302 {
7303 pst->dependencies[i]
7304 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7305 }
7306
7307 cu->per_cu->imported_symtabs_free ();
7308 }
7309
7310 /* Get the list of files included in the current compilation unit,
7311 and build a psymtab for each of them. */
7312 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7313
7314 if (dwarf_read_debug)
7315 fprintf_unfiltered (gdb_stdlog,
7316 "Psymtab for %s unit @%s: %s - %s"
7317 ", %d global, %d static syms\n",
7318 per_cu->is_debug_types ? "type" : "comp",
7319 sect_offset_str (per_cu->sect_off),
7320 paddress (gdbarch, pst->text_low (objfile)),
7321 paddress (gdbarch, pst->text_high (objfile)),
7322 pst->n_global_syms, pst->n_static_syms);
7323 }
7324
7325 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7326 Process compilation unit THIS_CU for a psymtab. */
7327
7328 static void
7329 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7330 bool want_partial_unit,
7331 enum language pretend_language)
7332 {
7333 /* If this compilation unit was already read in, free the
7334 cached copy in order to read it in again. This is
7335 necessary because we skipped some symbols when we first
7336 read in the compilation unit (see load_partial_dies).
7337 This problem could be avoided, but the benefit is unclear. */
7338 if (this_cu->cu != NULL)
7339 free_one_cached_comp_unit (this_cu);
7340
7341 cutu_reader reader (this_cu, NULL, 0, false);
7342
7343 switch (reader.comp_unit_die->tag)
7344 {
7345 case DW_TAG_compile_unit:
7346 this_cu->unit_type = DW_UT_compile;
7347 break;
7348 case DW_TAG_partial_unit:
7349 this_cu->unit_type = DW_UT_partial;
7350 break;
7351 default:
7352 abort ();
7353 }
7354
7355 if (reader.dummy_p)
7356 {
7357 /* Nothing. */
7358 }
7359 else if (this_cu->is_debug_types)
7360 build_type_psymtabs_reader (&reader, reader.info_ptr,
7361 reader.comp_unit_die);
7362 else if (want_partial_unit
7363 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7364 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7365 reader.comp_unit_die,
7366 pretend_language);
7367
7368 this_cu->lang = this_cu->cu->language;
7369
7370 /* Age out any secondary CUs. */
7371 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7372 }
7373
7374 /* Reader function for build_type_psymtabs. */
7375
7376 static void
7377 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7378 const gdb_byte *info_ptr,
7379 struct die_info *type_unit_die)
7380 {
7381 struct dwarf2_per_objfile *dwarf2_per_objfile
7382 = reader->cu->per_cu->dwarf2_per_objfile;
7383 struct objfile *objfile = dwarf2_per_objfile->objfile;
7384 struct dwarf2_cu *cu = reader->cu;
7385 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7386 struct signatured_type *sig_type;
7387 struct type_unit_group *tu_group;
7388 struct attribute *attr;
7389 struct partial_die_info *first_die;
7390 CORE_ADDR lowpc, highpc;
7391 dwarf2_psymtab *pst;
7392
7393 gdb_assert (per_cu->is_debug_types);
7394 sig_type = (struct signatured_type *) per_cu;
7395
7396 if (! type_unit_die->has_children)
7397 return;
7398
7399 attr = type_unit_die->attr (DW_AT_stmt_list);
7400 tu_group = get_type_unit_group (cu, attr);
7401
7402 if (tu_group->tus == nullptr)
7403 tu_group->tus = new std::vector<signatured_type *>;
7404 tu_group->tus->push_back (sig_type);
7405
7406 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7407 pst = create_partial_symtab (per_cu, "");
7408 pst->anonymous = true;
7409
7410 first_die = load_partial_dies (reader, info_ptr, 1);
7411
7412 lowpc = (CORE_ADDR) -1;
7413 highpc = (CORE_ADDR) 0;
7414 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7415
7416 end_psymtab_common (objfile, pst);
7417 }
7418
7419 /* Struct used to sort TUs by their abbreviation table offset. */
7420
7421 struct tu_abbrev_offset
7422 {
7423 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7424 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7425 {}
7426
7427 signatured_type *sig_type;
7428 sect_offset abbrev_offset;
7429 };
7430
7431 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7432
7433 static bool
7434 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7435 const struct tu_abbrev_offset &b)
7436 {
7437 return a.abbrev_offset < b.abbrev_offset;
7438 }
7439
7440 /* Efficiently read all the type units.
7441 This does the bulk of the work for build_type_psymtabs.
7442
7443 The efficiency is because we sort TUs by the abbrev table they use and
7444 only read each abbrev table once. In one program there are 200K TUs
7445 sharing 8K abbrev tables.
7446
7447 The main purpose of this function is to support building the
7448 dwarf2_per_objfile->type_unit_groups table.
7449 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7450 can collapse the search space by grouping them by stmt_list.
7451 The savings can be significant, in the same program from above the 200K TUs
7452 share 8K stmt_list tables.
7453
7454 FUNC is expected to call get_type_unit_group, which will create the
7455 struct type_unit_group if necessary and add it to
7456 dwarf2_per_objfile->type_unit_groups. */
7457
7458 static void
7459 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7460 {
7461 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7462 abbrev_table_up abbrev_table;
7463 sect_offset abbrev_offset;
7464
7465 /* It's up to the caller to not call us multiple times. */
7466 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7467
7468 if (dwarf2_per_objfile->all_type_units.empty ())
7469 return;
7470
7471 /* TUs typically share abbrev tables, and there can be way more TUs than
7472 abbrev tables. Sort by abbrev table to reduce the number of times we
7473 read each abbrev table in.
7474 Alternatives are to punt or to maintain a cache of abbrev tables.
7475 This is simpler and efficient enough for now.
7476
7477 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7478 symtab to use). Typically TUs with the same abbrev offset have the same
7479 stmt_list value too so in practice this should work well.
7480
7481 The basic algorithm here is:
7482
7483 sort TUs by abbrev table
7484 for each TU with same abbrev table:
7485 read abbrev table if first user
7486 read TU top level DIE
7487 [IWBN if DWO skeletons had DW_AT_stmt_list]
7488 call FUNC */
7489
7490 if (dwarf_read_debug)
7491 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7492
7493 /* Sort in a separate table to maintain the order of all_type_units
7494 for .gdb_index: TU indices directly index all_type_units. */
7495 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7496 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7497
7498 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7499 sorted_by_abbrev.emplace_back
7500 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7501 sig_type->per_cu.section,
7502 sig_type->per_cu.sect_off));
7503
7504 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7505 sort_tu_by_abbrev_offset);
7506
7507 abbrev_offset = (sect_offset) ~(unsigned) 0;
7508
7509 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7510 {
7511 /* Switch to the next abbrev table if necessary. */
7512 if (abbrev_table == NULL
7513 || tu.abbrev_offset != abbrev_offset)
7514 {
7515 abbrev_offset = tu.abbrev_offset;
7516 abbrev_table =
7517 abbrev_table::read (dwarf2_per_objfile->objfile,
7518 &dwarf2_per_objfile->abbrev,
7519 abbrev_offset);
7520 ++tu_stats->nr_uniq_abbrev_tables;
7521 }
7522
7523 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7524 0, false);
7525 if (!reader.dummy_p)
7526 build_type_psymtabs_reader (&reader, reader.info_ptr,
7527 reader.comp_unit_die);
7528 }
7529 }
7530
7531 /* Print collected type unit statistics. */
7532
7533 static void
7534 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7535 {
7536 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7537
7538 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7539 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7540 dwarf2_per_objfile->all_type_units.size ());
7541 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7542 tu_stats->nr_uniq_abbrev_tables);
7543 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7544 tu_stats->nr_symtabs);
7545 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7546 tu_stats->nr_symtab_sharers);
7547 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7548 tu_stats->nr_stmt_less_type_units);
7549 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7550 tu_stats->nr_all_type_units_reallocs);
7551 }
7552
7553 /* Traversal function for build_type_psymtabs. */
7554
7555 static int
7556 build_type_psymtab_dependencies (void **slot, void *info)
7557 {
7558 struct dwarf2_per_objfile *dwarf2_per_objfile
7559 = (struct dwarf2_per_objfile *) info;
7560 struct objfile *objfile = dwarf2_per_objfile->objfile;
7561 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7562 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7563 dwarf2_psymtab *pst = per_cu->v.psymtab;
7564 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7565 int i;
7566
7567 gdb_assert (len > 0);
7568 gdb_assert (per_cu->type_unit_group_p ());
7569
7570 pst->number_of_dependencies = len;
7571 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7572 for (i = 0; i < len; ++i)
7573 {
7574 struct signatured_type *iter = tu_group->tus->at (i);
7575 gdb_assert (iter->per_cu.is_debug_types);
7576 pst->dependencies[i] = iter->per_cu.v.psymtab;
7577 iter->type_unit_group = tu_group;
7578 }
7579
7580 delete tu_group->tus;
7581 tu_group->tus = nullptr;
7582
7583 return 1;
7584 }
7585
7586 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7587 Build partial symbol tables for the .debug_types comp-units. */
7588
7589 static void
7590 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7591 {
7592 if (! create_all_type_units (dwarf2_per_objfile))
7593 return;
7594
7595 build_type_psymtabs_1 (dwarf2_per_objfile);
7596 }
7597
7598 /* Traversal function for process_skeletonless_type_unit.
7599 Read a TU in a DWO file and build partial symbols for it. */
7600
7601 static int
7602 process_skeletonless_type_unit (void **slot, void *info)
7603 {
7604 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7605 struct dwarf2_per_objfile *dwarf2_per_objfile
7606 = (struct dwarf2_per_objfile *) info;
7607 struct signatured_type find_entry, *entry;
7608
7609 /* If this TU doesn't exist in the global table, add it and read it in. */
7610
7611 if (dwarf2_per_objfile->signatured_types == NULL)
7612 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7613
7614 find_entry.signature = dwo_unit->signature;
7615 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7616 &find_entry, INSERT);
7617 /* If we've already seen this type there's nothing to do. What's happening
7618 is we're doing our own version of comdat-folding here. */
7619 if (*slot != NULL)
7620 return 1;
7621
7622 /* This does the job that create_all_type_units would have done for
7623 this TU. */
7624 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7625 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7626 *slot = entry;
7627
7628 /* This does the job that build_type_psymtabs_1 would have done. */
7629 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7630 if (!reader.dummy_p)
7631 build_type_psymtabs_reader (&reader, reader.info_ptr,
7632 reader.comp_unit_die);
7633
7634 return 1;
7635 }
7636
7637 /* Traversal function for process_skeletonless_type_units. */
7638
7639 static int
7640 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7641 {
7642 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7643
7644 if (dwo_file->tus != NULL)
7645 htab_traverse_noresize (dwo_file->tus.get (),
7646 process_skeletonless_type_unit, info);
7647
7648 return 1;
7649 }
7650
7651 /* Scan all TUs of DWO files, verifying we've processed them.
7652 This is needed in case a TU was emitted without its skeleton.
7653 Note: This can't be done until we know what all the DWO files are. */
7654
7655 static void
7656 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7657 {
7658 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7659 if (get_dwp_file (dwarf2_per_objfile) == NULL
7660 && dwarf2_per_objfile->dwo_files != NULL)
7661 {
7662 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7663 process_dwo_file_for_skeletonless_type_units,
7664 dwarf2_per_objfile);
7665 }
7666 }
7667
7668 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7669
7670 static void
7671 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7672 {
7673 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7674 {
7675 dwarf2_psymtab *pst = per_cu->v.psymtab;
7676
7677 if (pst == NULL)
7678 continue;
7679
7680 for (int j = 0; j < pst->number_of_dependencies; ++j)
7681 {
7682 /* Set the 'user' field only if it is not already set. */
7683 if (pst->dependencies[j]->user == NULL)
7684 pst->dependencies[j]->user = pst;
7685 }
7686 }
7687 }
7688
7689 /* Build the partial symbol table by doing a quick pass through the
7690 .debug_info and .debug_abbrev sections. */
7691
7692 static void
7693 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7694 {
7695 struct objfile *objfile = dwarf2_per_objfile->objfile;
7696
7697 if (dwarf_read_debug)
7698 {
7699 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7700 objfile_name (objfile));
7701 }
7702
7703 scoped_restore restore_reading_psyms
7704 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7705 true);
7706
7707 dwarf2_per_objfile->info.read (objfile);
7708
7709 /* Any cached compilation units will be linked by the per-objfile
7710 read_in_chain. Make sure to free them when we're done. */
7711 free_cached_comp_units freer (dwarf2_per_objfile);
7712
7713 build_type_psymtabs (dwarf2_per_objfile);
7714
7715 create_all_comp_units (dwarf2_per_objfile);
7716
7717 /* Create a temporary address map on a temporary obstack. We later
7718 copy this to the final obstack. */
7719 auto_obstack temp_obstack;
7720
7721 scoped_restore save_psymtabs_addrmap
7722 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7723 addrmap_create_mutable (&temp_obstack));
7724
7725 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7726 process_psymtab_comp_unit (per_cu, false, language_minimal);
7727
7728 /* This has to wait until we read the CUs, we need the list of DWOs. */
7729 process_skeletonless_type_units (dwarf2_per_objfile);
7730
7731 /* Now that all TUs have been processed we can fill in the dependencies. */
7732 if (dwarf2_per_objfile->type_unit_groups != NULL)
7733 {
7734 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7735 build_type_psymtab_dependencies, dwarf2_per_objfile);
7736 }
7737
7738 if (dwarf_read_debug)
7739 print_tu_stats (dwarf2_per_objfile);
7740
7741 set_partial_user (dwarf2_per_objfile);
7742
7743 objfile->partial_symtabs->psymtabs_addrmap
7744 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7745 objfile->partial_symtabs->obstack ());
7746 /* At this point we want to keep the address map. */
7747 save_psymtabs_addrmap.release ();
7748
7749 if (dwarf_read_debug)
7750 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7751 objfile_name (objfile));
7752 }
7753
7754 /* Load the partial DIEs for a secondary CU into memory.
7755 This is also used when rereading a primary CU with load_all_dies. */
7756
7757 static void
7758 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7759 {
7760 cutu_reader reader (this_cu, NULL, 1, false);
7761
7762 if (!reader.dummy_p)
7763 {
7764 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7765 language_minimal);
7766
7767 /* Check if comp unit has_children.
7768 If so, read the rest of the partial symbols from this comp unit.
7769 If not, there's no more debug_info for this comp unit. */
7770 if (reader.comp_unit_die->has_children)
7771 load_partial_dies (&reader, reader.info_ptr, 0);
7772
7773 reader.keep ();
7774 }
7775 }
7776
7777 static void
7778 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7779 struct dwarf2_section_info *section,
7780 struct dwarf2_section_info *abbrev_section,
7781 unsigned int is_dwz)
7782 {
7783 const gdb_byte *info_ptr;
7784 struct objfile *objfile = dwarf2_per_objfile->objfile;
7785
7786 if (dwarf_read_debug)
7787 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7788 section->get_name (),
7789 section->get_file_name ());
7790
7791 section->read (objfile);
7792
7793 info_ptr = section->buffer;
7794
7795 while (info_ptr < section->buffer + section->size)
7796 {
7797 struct dwarf2_per_cu_data *this_cu;
7798
7799 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7800
7801 comp_unit_head cu_header;
7802 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7803 abbrev_section, info_ptr,
7804 rcuh_kind::COMPILE);
7805
7806 /* Save the compilation unit for later lookup. */
7807 if (cu_header.unit_type != DW_UT_type)
7808 {
7809 this_cu = XOBNEW (&objfile->objfile_obstack,
7810 struct dwarf2_per_cu_data);
7811 memset (this_cu, 0, sizeof (*this_cu));
7812 }
7813 else
7814 {
7815 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7816 struct signatured_type);
7817 memset (sig_type, 0, sizeof (*sig_type));
7818 sig_type->signature = cu_header.signature;
7819 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7820 this_cu = &sig_type->per_cu;
7821 }
7822 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7823 this_cu->sect_off = sect_off;
7824 this_cu->length = cu_header.length + cu_header.initial_length_size;
7825 this_cu->is_dwz = is_dwz;
7826 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7827 this_cu->section = section;
7828
7829 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7830
7831 info_ptr = info_ptr + this_cu->length;
7832 }
7833 }
7834
7835 /* Create a list of all compilation units in OBJFILE.
7836 This is only done for -readnow and building partial symtabs. */
7837
7838 static void
7839 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7840 {
7841 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7842 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7843 &dwarf2_per_objfile->abbrev, 0);
7844
7845 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7846 if (dwz != NULL)
7847 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7848 1);
7849 }
7850
7851 /* Process all loaded DIEs for compilation unit CU, starting at
7852 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7853 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7854 DW_AT_ranges). See the comments of add_partial_subprogram on how
7855 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7856
7857 static void
7858 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7859 CORE_ADDR *highpc, int set_addrmap,
7860 struct dwarf2_cu *cu)
7861 {
7862 struct partial_die_info *pdi;
7863
7864 /* Now, march along the PDI's, descending into ones which have
7865 interesting children but skipping the children of the other ones,
7866 until we reach the end of the compilation unit. */
7867
7868 pdi = first_die;
7869
7870 while (pdi != NULL)
7871 {
7872 pdi->fixup (cu);
7873
7874 /* Anonymous namespaces or modules have no name but have interesting
7875 children, so we need to look at them. Ditto for anonymous
7876 enums. */
7877
7878 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7879 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7880 || pdi->tag == DW_TAG_imported_unit
7881 || pdi->tag == DW_TAG_inlined_subroutine)
7882 {
7883 switch (pdi->tag)
7884 {
7885 case DW_TAG_subprogram:
7886 case DW_TAG_inlined_subroutine:
7887 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7888 break;
7889 case DW_TAG_constant:
7890 case DW_TAG_variable:
7891 case DW_TAG_typedef:
7892 case DW_TAG_union_type:
7893 if (!pdi->is_declaration)
7894 {
7895 add_partial_symbol (pdi, cu);
7896 }
7897 break;
7898 case DW_TAG_class_type:
7899 case DW_TAG_interface_type:
7900 case DW_TAG_structure_type:
7901 if (!pdi->is_declaration)
7902 {
7903 add_partial_symbol (pdi, cu);
7904 }
7905 if ((cu->language == language_rust
7906 || cu->language == language_cplus) && pdi->has_children)
7907 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7908 set_addrmap, cu);
7909 break;
7910 case DW_TAG_enumeration_type:
7911 if (!pdi->is_declaration)
7912 add_partial_enumeration (pdi, cu);
7913 break;
7914 case DW_TAG_base_type:
7915 case DW_TAG_subrange_type:
7916 /* File scope base type definitions are added to the partial
7917 symbol table. */
7918 add_partial_symbol (pdi, cu);
7919 break;
7920 case DW_TAG_namespace:
7921 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7922 break;
7923 case DW_TAG_module:
7924 if (!pdi->is_declaration)
7925 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7926 break;
7927 case DW_TAG_imported_unit:
7928 {
7929 struct dwarf2_per_cu_data *per_cu;
7930
7931 /* For now we don't handle imported units in type units. */
7932 if (cu->per_cu->is_debug_types)
7933 {
7934 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7935 " supported in type units [in module %s]"),
7936 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7937 }
7938
7939 per_cu = dwarf2_find_containing_comp_unit
7940 (pdi->d.sect_off, pdi->is_dwz,
7941 cu->per_cu->dwarf2_per_objfile);
7942
7943 /* Go read the partial unit, if needed. */
7944 if (per_cu->v.psymtab == NULL)
7945 process_psymtab_comp_unit (per_cu, true, cu->language);
7946
7947 cu->per_cu->imported_symtabs_push (per_cu);
7948 }
7949 break;
7950 case DW_TAG_imported_declaration:
7951 add_partial_symbol (pdi, cu);
7952 break;
7953 default:
7954 break;
7955 }
7956 }
7957
7958 /* If the die has a sibling, skip to the sibling. */
7959
7960 pdi = pdi->die_sibling;
7961 }
7962 }
7963
7964 /* Functions used to compute the fully scoped name of a partial DIE.
7965
7966 Normally, this is simple. For C++, the parent DIE's fully scoped
7967 name is concatenated with "::" and the partial DIE's name.
7968 Enumerators are an exception; they use the scope of their parent
7969 enumeration type, i.e. the name of the enumeration type is not
7970 prepended to the enumerator.
7971
7972 There are two complexities. One is DW_AT_specification; in this
7973 case "parent" means the parent of the target of the specification,
7974 instead of the direct parent of the DIE. The other is compilers
7975 which do not emit DW_TAG_namespace; in this case we try to guess
7976 the fully qualified name of structure types from their members'
7977 linkage names. This must be done using the DIE's children rather
7978 than the children of any DW_AT_specification target. We only need
7979 to do this for structures at the top level, i.e. if the target of
7980 any DW_AT_specification (if any; otherwise the DIE itself) does not
7981 have a parent. */
7982
7983 /* Compute the scope prefix associated with PDI's parent, in
7984 compilation unit CU. The result will be allocated on CU's
7985 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7986 field. NULL is returned if no prefix is necessary. */
7987 static const char *
7988 partial_die_parent_scope (struct partial_die_info *pdi,
7989 struct dwarf2_cu *cu)
7990 {
7991 const char *grandparent_scope;
7992 struct partial_die_info *parent, *real_pdi;
7993
7994 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7995 then this means the parent of the specification DIE. */
7996
7997 real_pdi = pdi;
7998 while (real_pdi->has_specification)
7999 {
8000 auto res = find_partial_die (real_pdi->spec_offset,
8001 real_pdi->spec_is_dwz, cu);
8002 real_pdi = res.pdi;
8003 cu = res.cu;
8004 }
8005
8006 parent = real_pdi->die_parent;
8007 if (parent == NULL)
8008 return NULL;
8009
8010 if (parent->scope_set)
8011 return parent->scope;
8012
8013 parent->fixup (cu);
8014
8015 grandparent_scope = partial_die_parent_scope (parent, cu);
8016
8017 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8018 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8019 Work around this problem here. */
8020 if (cu->language == language_cplus
8021 && parent->tag == DW_TAG_namespace
8022 && strcmp (parent->name, "::") == 0
8023 && grandparent_scope == NULL)
8024 {
8025 parent->scope = NULL;
8026 parent->scope_set = 1;
8027 return NULL;
8028 }
8029
8030 /* Nested subroutines in Fortran get a prefix. */
8031 if (pdi->tag == DW_TAG_enumerator)
8032 /* Enumerators should not get the name of the enumeration as a prefix. */
8033 parent->scope = grandparent_scope;
8034 else if (parent->tag == DW_TAG_namespace
8035 || parent->tag == DW_TAG_module
8036 || parent->tag == DW_TAG_structure_type
8037 || parent->tag == DW_TAG_class_type
8038 || parent->tag == DW_TAG_interface_type
8039 || parent->tag == DW_TAG_union_type
8040 || parent->tag == DW_TAG_enumeration_type
8041 || (cu->language == language_fortran
8042 && parent->tag == DW_TAG_subprogram
8043 && pdi->tag == DW_TAG_subprogram))
8044 {
8045 if (grandparent_scope == NULL)
8046 parent->scope = parent->name;
8047 else
8048 parent->scope = typename_concat (&cu->comp_unit_obstack,
8049 grandparent_scope,
8050 parent->name, 0, cu);
8051 }
8052 else
8053 {
8054 /* FIXME drow/2004-04-01: What should we be doing with
8055 function-local names? For partial symbols, we should probably be
8056 ignoring them. */
8057 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8058 dwarf_tag_name (parent->tag),
8059 sect_offset_str (pdi->sect_off));
8060 parent->scope = grandparent_scope;
8061 }
8062
8063 parent->scope_set = 1;
8064 return parent->scope;
8065 }
8066
8067 /* Return the fully scoped name associated with PDI, from compilation unit
8068 CU. The result will be allocated with malloc. */
8069
8070 static gdb::unique_xmalloc_ptr<char>
8071 partial_die_full_name (struct partial_die_info *pdi,
8072 struct dwarf2_cu *cu)
8073 {
8074 const char *parent_scope;
8075
8076 /* If this is a template instantiation, we can not work out the
8077 template arguments from partial DIEs. So, unfortunately, we have
8078 to go through the full DIEs. At least any work we do building
8079 types here will be reused if full symbols are loaded later. */
8080 if (pdi->has_template_arguments)
8081 {
8082 pdi->fixup (cu);
8083
8084 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8085 {
8086 struct die_info *die;
8087 struct attribute attr;
8088 struct dwarf2_cu *ref_cu = cu;
8089
8090 /* DW_FORM_ref_addr is using section offset. */
8091 attr.name = (enum dwarf_attribute) 0;
8092 attr.form = DW_FORM_ref_addr;
8093 attr.u.unsnd = to_underlying (pdi->sect_off);
8094 die = follow_die_ref (NULL, &attr, &ref_cu);
8095
8096 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8097 }
8098 }
8099
8100 parent_scope = partial_die_parent_scope (pdi, cu);
8101 if (parent_scope == NULL)
8102 return NULL;
8103 else
8104 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8105 pdi->name, 0, cu));
8106 }
8107
8108 static void
8109 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8110 {
8111 struct dwarf2_per_objfile *dwarf2_per_objfile
8112 = cu->per_cu->dwarf2_per_objfile;
8113 struct objfile *objfile = dwarf2_per_objfile->objfile;
8114 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8115 CORE_ADDR addr = 0;
8116 const char *actual_name = NULL;
8117 CORE_ADDR baseaddr;
8118
8119 baseaddr = objfile->text_section_offset ();
8120
8121 gdb::unique_xmalloc_ptr<char> built_actual_name
8122 = partial_die_full_name (pdi, cu);
8123 if (built_actual_name != NULL)
8124 actual_name = built_actual_name.get ();
8125
8126 if (actual_name == NULL)
8127 actual_name = pdi->name;
8128
8129 switch (pdi->tag)
8130 {
8131 case DW_TAG_inlined_subroutine:
8132 case DW_TAG_subprogram:
8133 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8134 - baseaddr);
8135 if (pdi->is_external
8136 || cu->language == language_ada
8137 || (cu->language == language_fortran
8138 && pdi->die_parent != NULL
8139 && pdi->die_parent->tag == DW_TAG_subprogram))
8140 {
8141 /* Normally, only "external" DIEs are part of the global scope.
8142 But in Ada and Fortran, we want to be able to access nested
8143 procedures globally. So all Ada and Fortran subprograms are
8144 stored in the global scope. */
8145 add_psymbol_to_list (actual_name,
8146 built_actual_name != NULL,
8147 VAR_DOMAIN, LOC_BLOCK,
8148 SECT_OFF_TEXT (objfile),
8149 psymbol_placement::GLOBAL,
8150 addr,
8151 cu->language, objfile);
8152 }
8153 else
8154 {
8155 add_psymbol_to_list (actual_name,
8156 built_actual_name != NULL,
8157 VAR_DOMAIN, LOC_BLOCK,
8158 SECT_OFF_TEXT (objfile),
8159 psymbol_placement::STATIC,
8160 addr, cu->language, objfile);
8161 }
8162
8163 if (pdi->main_subprogram && actual_name != NULL)
8164 set_objfile_main_name (objfile, actual_name, cu->language);
8165 break;
8166 case DW_TAG_constant:
8167 add_psymbol_to_list (actual_name,
8168 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8169 -1, (pdi->is_external
8170 ? psymbol_placement::GLOBAL
8171 : psymbol_placement::STATIC),
8172 0, cu->language, objfile);
8173 break;
8174 case DW_TAG_variable:
8175 if (pdi->d.locdesc)
8176 addr = decode_locdesc (pdi->d.locdesc, cu);
8177
8178 if (pdi->d.locdesc
8179 && addr == 0
8180 && !dwarf2_per_objfile->has_section_at_zero)
8181 {
8182 /* A global or static variable may also have been stripped
8183 out by the linker if unused, in which case its address
8184 will be nullified; do not add such variables into partial
8185 symbol table then. */
8186 }
8187 else if (pdi->is_external)
8188 {
8189 /* Global Variable.
8190 Don't enter into the minimal symbol tables as there is
8191 a minimal symbol table entry from the ELF symbols already.
8192 Enter into partial symbol table if it has a location
8193 descriptor or a type.
8194 If the location descriptor is missing, new_symbol will create
8195 a LOC_UNRESOLVED symbol, the address of the variable will then
8196 be determined from the minimal symbol table whenever the variable
8197 is referenced.
8198 The address for the partial symbol table entry is not
8199 used by GDB, but it comes in handy for debugging partial symbol
8200 table building. */
8201
8202 if (pdi->d.locdesc || pdi->has_type)
8203 add_psymbol_to_list (actual_name,
8204 built_actual_name != NULL,
8205 VAR_DOMAIN, LOC_STATIC,
8206 SECT_OFF_TEXT (objfile),
8207 psymbol_placement::GLOBAL,
8208 addr, cu->language, objfile);
8209 }
8210 else
8211 {
8212 int has_loc = pdi->d.locdesc != NULL;
8213
8214 /* Static Variable. Skip symbols whose value we cannot know (those
8215 without location descriptors or constant values). */
8216 if (!has_loc && !pdi->has_const_value)
8217 return;
8218
8219 add_psymbol_to_list (actual_name,
8220 built_actual_name != NULL,
8221 VAR_DOMAIN, LOC_STATIC,
8222 SECT_OFF_TEXT (objfile),
8223 psymbol_placement::STATIC,
8224 has_loc ? addr : 0,
8225 cu->language, objfile);
8226 }
8227 break;
8228 case DW_TAG_typedef:
8229 case DW_TAG_base_type:
8230 case DW_TAG_subrange_type:
8231 add_psymbol_to_list (actual_name,
8232 built_actual_name != NULL,
8233 VAR_DOMAIN, LOC_TYPEDEF, -1,
8234 psymbol_placement::STATIC,
8235 0, cu->language, objfile);
8236 break;
8237 case DW_TAG_imported_declaration:
8238 case DW_TAG_namespace:
8239 add_psymbol_to_list (actual_name,
8240 built_actual_name != NULL,
8241 VAR_DOMAIN, LOC_TYPEDEF, -1,
8242 psymbol_placement::GLOBAL,
8243 0, cu->language, objfile);
8244 break;
8245 case DW_TAG_module:
8246 /* With Fortran 77 there might be a "BLOCK DATA" module
8247 available without any name. If so, we skip the module as it
8248 doesn't bring any value. */
8249 if (actual_name != nullptr)
8250 add_psymbol_to_list (actual_name,
8251 built_actual_name != NULL,
8252 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8253 psymbol_placement::GLOBAL,
8254 0, cu->language, objfile);
8255 break;
8256 case DW_TAG_class_type:
8257 case DW_TAG_interface_type:
8258 case DW_TAG_structure_type:
8259 case DW_TAG_union_type:
8260 case DW_TAG_enumeration_type:
8261 /* Skip external references. The DWARF standard says in the section
8262 about "Structure, Union, and Class Type Entries": "An incomplete
8263 structure, union or class type is represented by a structure,
8264 union or class entry that does not have a byte size attribute
8265 and that has a DW_AT_declaration attribute." */
8266 if (!pdi->has_byte_size && pdi->is_declaration)
8267 return;
8268
8269 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8270 static vs. global. */
8271 add_psymbol_to_list (actual_name,
8272 built_actual_name != NULL,
8273 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8274 cu->language == language_cplus
8275 ? psymbol_placement::GLOBAL
8276 : psymbol_placement::STATIC,
8277 0, cu->language, objfile);
8278
8279 break;
8280 case DW_TAG_enumerator:
8281 add_psymbol_to_list (actual_name,
8282 built_actual_name != NULL,
8283 VAR_DOMAIN, LOC_CONST, -1,
8284 cu->language == language_cplus
8285 ? psymbol_placement::GLOBAL
8286 : psymbol_placement::STATIC,
8287 0, cu->language, objfile);
8288 break;
8289 default:
8290 break;
8291 }
8292 }
8293
8294 /* Read a partial die corresponding to a namespace; also, add a symbol
8295 corresponding to that namespace to the symbol table. NAMESPACE is
8296 the name of the enclosing namespace. */
8297
8298 static void
8299 add_partial_namespace (struct partial_die_info *pdi,
8300 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8301 int set_addrmap, struct dwarf2_cu *cu)
8302 {
8303 /* Add a symbol for the namespace. */
8304
8305 add_partial_symbol (pdi, cu);
8306
8307 /* Now scan partial symbols in that namespace. */
8308
8309 if (pdi->has_children)
8310 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8311 }
8312
8313 /* Read a partial die corresponding to a Fortran module. */
8314
8315 static void
8316 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8317 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8318 {
8319 /* Add a symbol for the namespace. */
8320
8321 add_partial_symbol (pdi, cu);
8322
8323 /* Now scan partial symbols in that module. */
8324
8325 if (pdi->has_children)
8326 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8327 }
8328
8329 /* Read a partial die corresponding to a subprogram or an inlined
8330 subprogram and create a partial symbol for that subprogram.
8331 When the CU language allows it, this routine also defines a partial
8332 symbol for each nested subprogram that this subprogram contains.
8333 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8334 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8335
8336 PDI may also be a lexical block, in which case we simply search
8337 recursively for subprograms defined inside that lexical block.
8338 Again, this is only performed when the CU language allows this
8339 type of definitions. */
8340
8341 static void
8342 add_partial_subprogram (struct partial_die_info *pdi,
8343 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8344 int set_addrmap, struct dwarf2_cu *cu)
8345 {
8346 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8347 {
8348 if (pdi->has_pc_info)
8349 {
8350 if (pdi->lowpc < *lowpc)
8351 *lowpc = pdi->lowpc;
8352 if (pdi->highpc > *highpc)
8353 *highpc = pdi->highpc;
8354 if (set_addrmap)
8355 {
8356 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8357 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8358 CORE_ADDR baseaddr;
8359 CORE_ADDR this_highpc;
8360 CORE_ADDR this_lowpc;
8361
8362 baseaddr = objfile->text_section_offset ();
8363 this_lowpc
8364 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8365 pdi->lowpc + baseaddr)
8366 - baseaddr);
8367 this_highpc
8368 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8369 pdi->highpc + baseaddr)
8370 - baseaddr);
8371 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8372 this_lowpc, this_highpc - 1,
8373 cu->per_cu->v.psymtab);
8374 }
8375 }
8376
8377 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8378 {
8379 if (!pdi->is_declaration)
8380 /* Ignore subprogram DIEs that do not have a name, they are
8381 illegal. Do not emit a complaint at this point, we will
8382 do so when we convert this psymtab into a symtab. */
8383 if (pdi->name)
8384 add_partial_symbol (pdi, cu);
8385 }
8386 }
8387
8388 if (! pdi->has_children)
8389 return;
8390
8391 if (cu->language == language_ada || cu->language == language_fortran)
8392 {
8393 pdi = pdi->die_child;
8394 while (pdi != NULL)
8395 {
8396 pdi->fixup (cu);
8397 if (pdi->tag == DW_TAG_subprogram
8398 || pdi->tag == DW_TAG_inlined_subroutine
8399 || pdi->tag == DW_TAG_lexical_block)
8400 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8401 pdi = pdi->die_sibling;
8402 }
8403 }
8404 }
8405
8406 /* Read a partial die corresponding to an enumeration type. */
8407
8408 static void
8409 add_partial_enumeration (struct partial_die_info *enum_pdi,
8410 struct dwarf2_cu *cu)
8411 {
8412 struct partial_die_info *pdi;
8413
8414 if (enum_pdi->name != NULL)
8415 add_partial_symbol (enum_pdi, cu);
8416
8417 pdi = enum_pdi->die_child;
8418 while (pdi)
8419 {
8420 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8421 complaint (_("malformed enumerator DIE ignored"));
8422 else
8423 add_partial_symbol (pdi, cu);
8424 pdi = pdi->die_sibling;
8425 }
8426 }
8427
8428 /* Return the initial uleb128 in the die at INFO_PTR. */
8429
8430 static unsigned int
8431 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8432 {
8433 unsigned int bytes_read;
8434
8435 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8436 }
8437
8438 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8439 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8440
8441 Return the corresponding abbrev, or NULL if the number is zero (indicating
8442 an empty DIE). In either case *BYTES_READ will be set to the length of
8443 the initial number. */
8444
8445 static struct abbrev_info *
8446 peek_die_abbrev (const die_reader_specs &reader,
8447 const gdb_byte *info_ptr, unsigned int *bytes_read)
8448 {
8449 dwarf2_cu *cu = reader.cu;
8450 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8451 unsigned int abbrev_number
8452 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8453
8454 if (abbrev_number == 0)
8455 return NULL;
8456
8457 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8458 if (!abbrev)
8459 {
8460 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8461 " at offset %s [in module %s]"),
8462 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8463 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8464 }
8465
8466 return abbrev;
8467 }
8468
8469 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8470 Returns a pointer to the end of a series of DIEs, terminated by an empty
8471 DIE. Any children of the skipped DIEs will also be skipped. */
8472
8473 static const gdb_byte *
8474 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8475 {
8476 while (1)
8477 {
8478 unsigned int bytes_read;
8479 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8480
8481 if (abbrev == NULL)
8482 return info_ptr + bytes_read;
8483 else
8484 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8485 }
8486 }
8487
8488 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8489 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8490 abbrev corresponding to that skipped uleb128 should be passed in
8491 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8492 children. */
8493
8494 static const gdb_byte *
8495 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8496 struct abbrev_info *abbrev)
8497 {
8498 unsigned int bytes_read;
8499 struct attribute attr;
8500 bfd *abfd = reader->abfd;
8501 struct dwarf2_cu *cu = reader->cu;
8502 const gdb_byte *buffer = reader->buffer;
8503 const gdb_byte *buffer_end = reader->buffer_end;
8504 unsigned int form, i;
8505
8506 for (i = 0; i < abbrev->num_attrs; i++)
8507 {
8508 /* The only abbrev we care about is DW_AT_sibling. */
8509 if (abbrev->attrs[i].name == DW_AT_sibling)
8510 {
8511 bool ignored;
8512 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8513 &ignored);
8514 if (attr.form == DW_FORM_ref_addr)
8515 complaint (_("ignoring absolute DW_AT_sibling"));
8516 else
8517 {
8518 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8519 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8520
8521 if (sibling_ptr < info_ptr)
8522 complaint (_("DW_AT_sibling points backwards"));
8523 else if (sibling_ptr > reader->buffer_end)
8524 reader->die_section->overflow_complaint ();
8525 else
8526 return sibling_ptr;
8527 }
8528 }
8529
8530 /* If it isn't DW_AT_sibling, skip this attribute. */
8531 form = abbrev->attrs[i].form;
8532 skip_attribute:
8533 switch (form)
8534 {
8535 case DW_FORM_ref_addr:
8536 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8537 and later it is offset sized. */
8538 if (cu->header.version == 2)
8539 info_ptr += cu->header.addr_size;
8540 else
8541 info_ptr += cu->header.offset_size;
8542 break;
8543 case DW_FORM_GNU_ref_alt:
8544 info_ptr += cu->header.offset_size;
8545 break;
8546 case DW_FORM_addr:
8547 info_ptr += cu->header.addr_size;
8548 break;
8549 case DW_FORM_data1:
8550 case DW_FORM_ref1:
8551 case DW_FORM_flag:
8552 case DW_FORM_strx1:
8553 info_ptr += 1;
8554 break;
8555 case DW_FORM_flag_present:
8556 case DW_FORM_implicit_const:
8557 break;
8558 case DW_FORM_data2:
8559 case DW_FORM_ref2:
8560 case DW_FORM_strx2:
8561 info_ptr += 2;
8562 break;
8563 case DW_FORM_strx3:
8564 info_ptr += 3;
8565 break;
8566 case DW_FORM_data4:
8567 case DW_FORM_ref4:
8568 case DW_FORM_strx4:
8569 info_ptr += 4;
8570 break;
8571 case DW_FORM_data8:
8572 case DW_FORM_ref8:
8573 case DW_FORM_ref_sig8:
8574 info_ptr += 8;
8575 break;
8576 case DW_FORM_data16:
8577 info_ptr += 16;
8578 break;
8579 case DW_FORM_string:
8580 read_direct_string (abfd, info_ptr, &bytes_read);
8581 info_ptr += bytes_read;
8582 break;
8583 case DW_FORM_sec_offset:
8584 case DW_FORM_strp:
8585 case DW_FORM_GNU_strp_alt:
8586 info_ptr += cu->header.offset_size;
8587 break;
8588 case DW_FORM_exprloc:
8589 case DW_FORM_block:
8590 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8591 info_ptr += bytes_read;
8592 break;
8593 case DW_FORM_block1:
8594 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8595 break;
8596 case DW_FORM_block2:
8597 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8598 break;
8599 case DW_FORM_block4:
8600 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8601 break;
8602 case DW_FORM_addrx:
8603 case DW_FORM_strx:
8604 case DW_FORM_sdata:
8605 case DW_FORM_udata:
8606 case DW_FORM_ref_udata:
8607 case DW_FORM_GNU_addr_index:
8608 case DW_FORM_GNU_str_index:
8609 case DW_FORM_rnglistx:
8610 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8611 break;
8612 case DW_FORM_indirect:
8613 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8614 info_ptr += bytes_read;
8615 /* We need to continue parsing from here, so just go back to
8616 the top. */
8617 goto skip_attribute;
8618
8619 default:
8620 error (_("Dwarf Error: Cannot handle %s "
8621 "in DWARF reader [in module %s]"),
8622 dwarf_form_name (form),
8623 bfd_get_filename (abfd));
8624 }
8625 }
8626
8627 if (abbrev->has_children)
8628 return skip_children (reader, info_ptr);
8629 else
8630 return info_ptr;
8631 }
8632
8633 /* Locate ORIG_PDI's sibling.
8634 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8635
8636 static const gdb_byte *
8637 locate_pdi_sibling (const struct die_reader_specs *reader,
8638 struct partial_die_info *orig_pdi,
8639 const gdb_byte *info_ptr)
8640 {
8641 /* Do we know the sibling already? */
8642
8643 if (orig_pdi->sibling)
8644 return orig_pdi->sibling;
8645
8646 /* Are there any children to deal with? */
8647
8648 if (!orig_pdi->has_children)
8649 return info_ptr;
8650
8651 /* Skip the children the long way. */
8652
8653 return skip_children (reader, info_ptr);
8654 }
8655
8656 /* Expand this partial symbol table into a full symbol table. SELF is
8657 not NULL. */
8658
8659 void
8660 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8661 {
8662 struct dwarf2_per_objfile *dwarf2_per_objfile
8663 = get_dwarf2_per_objfile (objfile);
8664
8665 gdb_assert (!readin);
8666 /* If this psymtab is constructed from a debug-only objfile, the
8667 has_section_at_zero flag will not necessarily be correct. We
8668 can get the correct value for this flag by looking at the data
8669 associated with the (presumably stripped) associated objfile. */
8670 if (objfile->separate_debug_objfile_backlink)
8671 {
8672 struct dwarf2_per_objfile *dpo_backlink
8673 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8674
8675 dwarf2_per_objfile->has_section_at_zero
8676 = dpo_backlink->has_section_at_zero;
8677 }
8678
8679 expand_psymtab (objfile);
8680
8681 process_cu_includes (dwarf2_per_objfile);
8682 }
8683 \f
8684 /* Reading in full CUs. */
8685
8686 /* Add PER_CU to the queue. */
8687
8688 static void
8689 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8690 enum language pretend_language)
8691 {
8692 per_cu->queued = 1;
8693 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8694 }
8695
8696 /* If PER_CU is not yet queued, add it to the queue.
8697 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8698 dependency.
8699 The result is non-zero if PER_CU was queued, otherwise the result is zero
8700 meaning either PER_CU is already queued or it is already loaded.
8701
8702 N.B. There is an invariant here that if a CU is queued then it is loaded.
8703 The caller is required to load PER_CU if we return non-zero. */
8704
8705 static int
8706 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8707 struct dwarf2_per_cu_data *per_cu,
8708 enum language pretend_language)
8709 {
8710 /* We may arrive here during partial symbol reading, if we need full
8711 DIEs to process an unusual case (e.g. template arguments). Do
8712 not queue PER_CU, just tell our caller to load its DIEs. */
8713 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8714 {
8715 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8716 return 1;
8717 return 0;
8718 }
8719
8720 /* Mark the dependence relation so that we don't flush PER_CU
8721 too early. */
8722 if (dependent_cu != NULL)
8723 dwarf2_add_dependence (dependent_cu, per_cu);
8724
8725 /* If it's already on the queue, we have nothing to do. */
8726 if (per_cu->queued)
8727 return 0;
8728
8729 /* If the compilation unit is already loaded, just mark it as
8730 used. */
8731 if (per_cu->cu != NULL)
8732 {
8733 per_cu->cu->last_used = 0;
8734 return 0;
8735 }
8736
8737 /* Add it to the queue. */
8738 queue_comp_unit (per_cu, pretend_language);
8739
8740 return 1;
8741 }
8742
8743 /* Process the queue. */
8744
8745 static void
8746 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8747 {
8748 if (dwarf_read_debug)
8749 {
8750 fprintf_unfiltered (gdb_stdlog,
8751 "Expanding one or more symtabs of objfile %s ...\n",
8752 objfile_name (dwarf2_per_objfile->objfile));
8753 }
8754
8755 /* The queue starts out with one item, but following a DIE reference
8756 may load a new CU, adding it to the end of the queue. */
8757 while (!dwarf2_per_objfile->queue.empty ())
8758 {
8759 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8760
8761 if ((dwarf2_per_objfile->using_index
8762 ? !item.per_cu->v.quick->compunit_symtab
8763 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8764 /* Skip dummy CUs. */
8765 && item.per_cu->cu != NULL)
8766 {
8767 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8768 unsigned int debug_print_threshold;
8769 char buf[100];
8770
8771 if (per_cu->is_debug_types)
8772 {
8773 struct signatured_type *sig_type =
8774 (struct signatured_type *) per_cu;
8775
8776 sprintf (buf, "TU %s at offset %s",
8777 hex_string (sig_type->signature),
8778 sect_offset_str (per_cu->sect_off));
8779 /* There can be 100s of TUs.
8780 Only print them in verbose mode. */
8781 debug_print_threshold = 2;
8782 }
8783 else
8784 {
8785 sprintf (buf, "CU at offset %s",
8786 sect_offset_str (per_cu->sect_off));
8787 debug_print_threshold = 1;
8788 }
8789
8790 if (dwarf_read_debug >= debug_print_threshold)
8791 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8792
8793 if (per_cu->is_debug_types)
8794 process_full_type_unit (per_cu, item.pretend_language);
8795 else
8796 process_full_comp_unit (per_cu, item.pretend_language);
8797
8798 if (dwarf_read_debug >= debug_print_threshold)
8799 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8800 }
8801
8802 item.per_cu->queued = 0;
8803 dwarf2_per_objfile->queue.pop ();
8804 }
8805
8806 if (dwarf_read_debug)
8807 {
8808 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8809 objfile_name (dwarf2_per_objfile->objfile));
8810 }
8811 }
8812
8813 /* Read in full symbols for PST, and anything it depends on. */
8814
8815 void
8816 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8817 {
8818 if (readin)
8819 return;
8820
8821 read_dependencies (objfile);
8822
8823 dw2_do_instantiate_symtab (per_cu_data, false);
8824 gdb_assert (get_compunit_symtab () != nullptr);
8825 }
8826
8827 /* Trivial hash function for die_info: the hash value of a DIE
8828 is its offset in .debug_info for this objfile. */
8829
8830 static hashval_t
8831 die_hash (const void *item)
8832 {
8833 const struct die_info *die = (const struct die_info *) item;
8834
8835 return to_underlying (die->sect_off);
8836 }
8837
8838 /* Trivial comparison function for die_info structures: two DIEs
8839 are equal if they have the same offset. */
8840
8841 static int
8842 die_eq (const void *item_lhs, const void *item_rhs)
8843 {
8844 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8845 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8846
8847 return die_lhs->sect_off == die_rhs->sect_off;
8848 }
8849
8850 /* Load the DIEs associated with PER_CU into memory. */
8851
8852 static void
8853 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8854 bool skip_partial,
8855 enum language pretend_language)
8856 {
8857 gdb_assert (! this_cu->is_debug_types);
8858
8859 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8860 if (reader.dummy_p)
8861 return;
8862
8863 struct dwarf2_cu *cu = reader.cu;
8864 const gdb_byte *info_ptr = reader.info_ptr;
8865
8866 gdb_assert (cu->die_hash == NULL);
8867 cu->die_hash =
8868 htab_create_alloc_ex (cu->header.length / 12,
8869 die_hash,
8870 die_eq,
8871 NULL,
8872 &cu->comp_unit_obstack,
8873 hashtab_obstack_allocate,
8874 dummy_obstack_deallocate);
8875
8876 if (reader.comp_unit_die->has_children)
8877 reader.comp_unit_die->child
8878 = read_die_and_siblings (&reader, reader.info_ptr,
8879 &info_ptr, reader.comp_unit_die);
8880 cu->dies = reader.comp_unit_die;
8881 /* comp_unit_die is not stored in die_hash, no need. */
8882
8883 /* We try not to read any attributes in this function, because not
8884 all CUs needed for references have been loaded yet, and symbol
8885 table processing isn't initialized. But we have to set the CU language,
8886 or we won't be able to build types correctly.
8887 Similarly, if we do not read the producer, we can not apply
8888 producer-specific interpretation. */
8889 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8890
8891 reader.keep ();
8892 }
8893
8894 /* Add a DIE to the delayed physname list. */
8895
8896 static void
8897 add_to_method_list (struct type *type, int fnfield_index, int index,
8898 const char *name, struct die_info *die,
8899 struct dwarf2_cu *cu)
8900 {
8901 struct delayed_method_info mi;
8902 mi.type = type;
8903 mi.fnfield_index = fnfield_index;
8904 mi.index = index;
8905 mi.name = name;
8906 mi.die = die;
8907 cu->method_list.push_back (mi);
8908 }
8909
8910 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8911 "const" / "volatile". If so, decrements LEN by the length of the
8912 modifier and return true. Otherwise return false. */
8913
8914 template<size_t N>
8915 static bool
8916 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8917 {
8918 size_t mod_len = sizeof (mod) - 1;
8919 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8920 {
8921 len -= mod_len;
8922 return true;
8923 }
8924 return false;
8925 }
8926
8927 /* Compute the physnames of any methods on the CU's method list.
8928
8929 The computation of method physnames is delayed in order to avoid the
8930 (bad) condition that one of the method's formal parameters is of an as yet
8931 incomplete type. */
8932
8933 static void
8934 compute_delayed_physnames (struct dwarf2_cu *cu)
8935 {
8936 /* Only C++ delays computing physnames. */
8937 if (cu->method_list.empty ())
8938 return;
8939 gdb_assert (cu->language == language_cplus);
8940
8941 for (const delayed_method_info &mi : cu->method_list)
8942 {
8943 const char *physname;
8944 struct fn_fieldlist *fn_flp
8945 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8946 physname = dwarf2_physname (mi.name, mi.die, cu);
8947 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8948 = physname ? physname : "";
8949
8950 /* Since there's no tag to indicate whether a method is a
8951 const/volatile overload, extract that information out of the
8952 demangled name. */
8953 if (physname != NULL)
8954 {
8955 size_t len = strlen (physname);
8956
8957 while (1)
8958 {
8959 if (physname[len] == ')') /* shortcut */
8960 break;
8961 else if (check_modifier (physname, len, " const"))
8962 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8963 else if (check_modifier (physname, len, " volatile"))
8964 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8965 else
8966 break;
8967 }
8968 }
8969 }
8970
8971 /* The list is no longer needed. */
8972 cu->method_list.clear ();
8973 }
8974
8975 /* Go objects should be embedded in a DW_TAG_module DIE,
8976 and it's not clear if/how imported objects will appear.
8977 To keep Go support simple until that's worked out,
8978 go back through what we've read and create something usable.
8979 We could do this while processing each DIE, and feels kinda cleaner,
8980 but that way is more invasive.
8981 This is to, for example, allow the user to type "p var" or "b main"
8982 without having to specify the package name, and allow lookups
8983 of module.object to work in contexts that use the expression
8984 parser. */
8985
8986 static void
8987 fixup_go_packaging (struct dwarf2_cu *cu)
8988 {
8989 gdb::unique_xmalloc_ptr<char> package_name;
8990 struct pending *list;
8991 int i;
8992
8993 for (list = *cu->get_builder ()->get_global_symbols ();
8994 list != NULL;
8995 list = list->next)
8996 {
8997 for (i = 0; i < list->nsyms; ++i)
8998 {
8999 struct symbol *sym = list->symbol[i];
9000
9001 if (sym->language () == language_go
9002 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9003 {
9004 gdb::unique_xmalloc_ptr<char> this_package_name
9005 (go_symbol_package_name (sym));
9006
9007 if (this_package_name == NULL)
9008 continue;
9009 if (package_name == NULL)
9010 package_name = std::move (this_package_name);
9011 else
9012 {
9013 struct objfile *objfile
9014 = cu->per_cu->dwarf2_per_objfile->objfile;
9015 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9016 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9017 (symbol_symtab (sym) != NULL
9018 ? symtab_to_filename_for_display
9019 (symbol_symtab (sym))
9020 : objfile_name (objfile)),
9021 this_package_name.get (), package_name.get ());
9022 }
9023 }
9024 }
9025 }
9026
9027 if (package_name != NULL)
9028 {
9029 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9030 const char *saved_package_name = objfile->intern (package_name.get ());
9031 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9032 saved_package_name);
9033 struct symbol *sym;
9034
9035 sym = allocate_symbol (objfile);
9036 sym->set_language (language_go, &objfile->objfile_obstack);
9037 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9038 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9039 e.g., "main" finds the "main" module and not C's main(). */
9040 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9041 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9042 SYMBOL_TYPE (sym) = type;
9043
9044 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9045 }
9046 }
9047
9048 /* Allocate a fully-qualified name consisting of the two parts on the
9049 obstack. */
9050
9051 static const char *
9052 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9053 {
9054 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9055 }
9056
9057 /* A helper that allocates a struct discriminant_info to attach to a
9058 union type. */
9059
9060 static struct discriminant_info *
9061 alloc_discriminant_info (struct type *type, int discriminant_index,
9062 int default_index)
9063 {
9064 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9065 gdb_assert (discriminant_index == -1
9066 || (discriminant_index >= 0
9067 && discriminant_index < TYPE_NFIELDS (type)));
9068 gdb_assert (default_index == -1
9069 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9070
9071 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9072
9073 struct discriminant_info *disc
9074 = ((struct discriminant_info *)
9075 TYPE_ZALLOC (type,
9076 offsetof (struct discriminant_info, discriminants)
9077 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9078 disc->default_index = default_index;
9079 disc->discriminant_index = discriminant_index;
9080
9081 struct dynamic_prop prop;
9082 prop.kind = PROP_UNDEFINED;
9083 prop.data.baton = disc;
9084
9085 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9086
9087 return disc;
9088 }
9089
9090 /* Some versions of rustc emitted enums in an unusual way.
9091
9092 Ordinary enums were emitted as unions. The first element of each
9093 structure in the union was named "RUST$ENUM$DISR". This element
9094 held the discriminant.
9095
9096 These versions of Rust also implemented the "non-zero"
9097 optimization. When the enum had two values, and one is empty and
9098 the other holds a pointer that cannot be zero, the pointer is used
9099 as the discriminant, with a zero value meaning the empty variant.
9100 Here, the union's first member is of the form
9101 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9102 where the fieldnos are the indices of the fields that should be
9103 traversed in order to find the field (which may be several fields deep)
9104 and the variantname is the name of the variant of the case when the
9105 field is zero.
9106
9107 This function recognizes whether TYPE is of one of these forms,
9108 and, if so, smashes it to be a variant type. */
9109
9110 static void
9111 quirk_rust_enum (struct type *type, struct objfile *objfile)
9112 {
9113 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9114
9115 /* We don't need to deal with empty enums. */
9116 if (TYPE_NFIELDS (type) == 0)
9117 return;
9118
9119 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9120 if (TYPE_NFIELDS (type) == 1
9121 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9122 {
9123 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9124
9125 /* Decode the field name to find the offset of the
9126 discriminant. */
9127 ULONGEST bit_offset = 0;
9128 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9129 while (name[0] >= '0' && name[0] <= '9')
9130 {
9131 char *tail;
9132 unsigned long index = strtoul (name, &tail, 10);
9133 name = tail;
9134 if (*name != '$'
9135 || index >= TYPE_NFIELDS (field_type)
9136 || (TYPE_FIELD_LOC_KIND (field_type, index)
9137 != FIELD_LOC_KIND_BITPOS))
9138 {
9139 complaint (_("Could not parse Rust enum encoding string \"%s\""
9140 "[in module %s]"),
9141 TYPE_FIELD_NAME (type, 0),
9142 objfile_name (objfile));
9143 return;
9144 }
9145 ++name;
9146
9147 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9148 field_type = TYPE_FIELD_TYPE (field_type, index);
9149 }
9150
9151 /* Make a union to hold the variants. */
9152 struct type *union_type = alloc_type (objfile);
9153 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9154 TYPE_NFIELDS (union_type) = 3;
9155 TYPE_FIELDS (union_type)
9156 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9157 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9158 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9159
9160 /* Put the discriminant must at index 0. */
9161 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9162 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9163 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9164 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9165
9166 /* The order of fields doesn't really matter, so put the real
9167 field at index 1 and the data-less field at index 2. */
9168 struct discriminant_info *disc
9169 = alloc_discriminant_info (union_type, 0, 1);
9170 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9171 TYPE_FIELD_NAME (union_type, 1)
9172 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9173 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9174 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9175 TYPE_FIELD_NAME (union_type, 1));
9176
9177 const char *dataless_name
9178 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9179 name);
9180 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9181 dataless_name);
9182 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9183 /* NAME points into the original discriminant name, which
9184 already has the correct lifetime. */
9185 TYPE_FIELD_NAME (union_type, 2) = name;
9186 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9187 disc->discriminants[2] = 0;
9188
9189 /* Smash this type to be a structure type. We have to do this
9190 because the type has already been recorded. */
9191 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9192 TYPE_NFIELDS (type) = 1;
9193 TYPE_FIELDS (type)
9194 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9195
9196 /* Install the variant part. */
9197 TYPE_FIELD_TYPE (type, 0) = union_type;
9198 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9199 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9200 }
9201 /* A union with a single anonymous field is probably an old-style
9202 univariant enum. */
9203 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9204 {
9205 /* Smash this type to be a structure type. We have to do this
9206 because the type has already been recorded. */
9207 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9208
9209 /* Make a union to hold the variants. */
9210 struct type *union_type = alloc_type (objfile);
9211 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9212 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9213 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9214 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9215 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9216
9217 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9218 const char *variant_name
9219 = rust_last_path_segment (TYPE_NAME (field_type));
9220 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9221 TYPE_NAME (field_type)
9222 = rust_fully_qualify (&objfile->objfile_obstack,
9223 TYPE_NAME (type), variant_name);
9224
9225 /* Install the union in the outer struct type. */
9226 TYPE_NFIELDS (type) = 1;
9227 TYPE_FIELDS (type)
9228 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9229 TYPE_FIELD_TYPE (type, 0) = union_type;
9230 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9231 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9232
9233 alloc_discriminant_info (union_type, -1, 0);
9234 }
9235 else
9236 {
9237 struct type *disr_type = nullptr;
9238 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9239 {
9240 disr_type = TYPE_FIELD_TYPE (type, i);
9241
9242 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9243 {
9244 /* All fields of a true enum will be structs. */
9245 return;
9246 }
9247 else if (TYPE_NFIELDS (disr_type) == 0)
9248 {
9249 /* Could be data-less variant, so keep going. */
9250 disr_type = nullptr;
9251 }
9252 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9253 "RUST$ENUM$DISR") != 0)
9254 {
9255 /* Not a Rust enum. */
9256 return;
9257 }
9258 else
9259 {
9260 /* Found one. */
9261 break;
9262 }
9263 }
9264
9265 /* If we got here without a discriminant, then it's probably
9266 just a union. */
9267 if (disr_type == nullptr)
9268 return;
9269
9270 /* Smash this type to be a structure type. We have to do this
9271 because the type has already been recorded. */
9272 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9273
9274 /* Make a union to hold the variants. */
9275 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9276 struct type *union_type = alloc_type (objfile);
9277 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9278 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9279 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9280 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9281 TYPE_FIELDS (union_type)
9282 = (struct field *) TYPE_ZALLOC (union_type,
9283 (TYPE_NFIELDS (union_type)
9284 * sizeof (struct field)));
9285
9286 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9287 TYPE_NFIELDS (type) * sizeof (struct field));
9288
9289 /* Install the discriminant at index 0 in the union. */
9290 TYPE_FIELD (union_type, 0) = *disr_field;
9291 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9292 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9293
9294 /* Install the union in the outer struct type. */
9295 TYPE_FIELD_TYPE (type, 0) = union_type;
9296 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9297 TYPE_NFIELDS (type) = 1;
9298
9299 /* Set the size and offset of the union type. */
9300 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9301
9302 /* We need a way to find the correct discriminant given a
9303 variant name. For convenience we build a map here. */
9304 struct type *enum_type = FIELD_TYPE (*disr_field);
9305 std::unordered_map<std::string, ULONGEST> discriminant_map;
9306 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9307 {
9308 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9309 {
9310 const char *name
9311 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9312 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9313 }
9314 }
9315
9316 int n_fields = TYPE_NFIELDS (union_type);
9317 struct discriminant_info *disc
9318 = alloc_discriminant_info (union_type, 0, -1);
9319 /* Skip the discriminant here. */
9320 for (int i = 1; i < n_fields; ++i)
9321 {
9322 /* Find the final word in the name of this variant's type.
9323 That name can be used to look up the correct
9324 discriminant. */
9325 const char *variant_name
9326 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9327 i)));
9328
9329 auto iter = discriminant_map.find (variant_name);
9330 if (iter != discriminant_map.end ())
9331 disc->discriminants[i] = iter->second;
9332
9333 /* Remove the discriminant field, if it exists. */
9334 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9335 if (TYPE_NFIELDS (sub_type) > 0)
9336 {
9337 --TYPE_NFIELDS (sub_type);
9338 ++TYPE_FIELDS (sub_type);
9339 }
9340 TYPE_FIELD_NAME (union_type, i) = variant_name;
9341 TYPE_NAME (sub_type)
9342 = rust_fully_qualify (&objfile->objfile_obstack,
9343 TYPE_NAME (type), variant_name);
9344 }
9345 }
9346 }
9347
9348 /* Rewrite some Rust unions to be structures with variants parts. */
9349
9350 static void
9351 rust_union_quirks (struct dwarf2_cu *cu)
9352 {
9353 gdb_assert (cu->language == language_rust);
9354 for (type *type_ : cu->rust_unions)
9355 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9356 /* We don't need this any more. */
9357 cu->rust_unions.clear ();
9358 }
9359
9360 /* Return the symtab for PER_CU. This works properly regardless of
9361 whether we're using the index or psymtabs. */
9362
9363 static struct compunit_symtab *
9364 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9365 {
9366 return (per_cu->dwarf2_per_objfile->using_index
9367 ? per_cu->v.quick->compunit_symtab
9368 : per_cu->v.psymtab->compunit_symtab);
9369 }
9370
9371 /* A helper function for computing the list of all symbol tables
9372 included by PER_CU. */
9373
9374 static void
9375 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9376 htab_t all_children, htab_t all_type_symtabs,
9377 struct dwarf2_per_cu_data *per_cu,
9378 struct compunit_symtab *immediate_parent)
9379 {
9380 void **slot;
9381 struct compunit_symtab *cust;
9382
9383 slot = htab_find_slot (all_children, per_cu, INSERT);
9384 if (*slot != NULL)
9385 {
9386 /* This inclusion and its children have been processed. */
9387 return;
9388 }
9389
9390 *slot = per_cu;
9391 /* Only add a CU if it has a symbol table. */
9392 cust = get_compunit_symtab (per_cu);
9393 if (cust != NULL)
9394 {
9395 /* If this is a type unit only add its symbol table if we haven't
9396 seen it yet (type unit per_cu's can share symtabs). */
9397 if (per_cu->is_debug_types)
9398 {
9399 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9400 if (*slot == NULL)
9401 {
9402 *slot = cust;
9403 result->push_back (cust);
9404 if (cust->user == NULL)
9405 cust->user = immediate_parent;
9406 }
9407 }
9408 else
9409 {
9410 result->push_back (cust);
9411 if (cust->user == NULL)
9412 cust->user = immediate_parent;
9413 }
9414 }
9415
9416 if (!per_cu->imported_symtabs_empty ())
9417 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9418 {
9419 recursively_compute_inclusions (result, all_children,
9420 all_type_symtabs, ptr, cust);
9421 }
9422 }
9423
9424 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9425 PER_CU. */
9426
9427 static void
9428 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9429 {
9430 gdb_assert (! per_cu->is_debug_types);
9431
9432 if (!per_cu->imported_symtabs_empty ())
9433 {
9434 int len;
9435 std::vector<compunit_symtab *> result_symtabs;
9436 htab_t all_children, all_type_symtabs;
9437 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9438
9439 /* If we don't have a symtab, we can just skip this case. */
9440 if (cust == NULL)
9441 return;
9442
9443 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9444 NULL, xcalloc, xfree);
9445 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9446 NULL, xcalloc, xfree);
9447
9448 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9449 {
9450 recursively_compute_inclusions (&result_symtabs, all_children,
9451 all_type_symtabs, ptr, cust);
9452 }
9453
9454 /* Now we have a transitive closure of all the included symtabs. */
9455 len = result_symtabs.size ();
9456 cust->includes
9457 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9458 struct compunit_symtab *, len + 1);
9459 memcpy (cust->includes, result_symtabs.data (),
9460 len * sizeof (compunit_symtab *));
9461 cust->includes[len] = NULL;
9462
9463 htab_delete (all_children);
9464 htab_delete (all_type_symtabs);
9465 }
9466 }
9467
9468 /* Compute the 'includes' field for the symtabs of all the CUs we just
9469 read. */
9470
9471 static void
9472 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9473 {
9474 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9475 {
9476 if (! iter->is_debug_types)
9477 compute_compunit_symtab_includes (iter);
9478 }
9479
9480 dwarf2_per_objfile->just_read_cus.clear ();
9481 }
9482
9483 /* Generate full symbol information for PER_CU, whose DIEs have
9484 already been loaded into memory. */
9485
9486 static void
9487 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9488 enum language pretend_language)
9489 {
9490 struct dwarf2_cu *cu = per_cu->cu;
9491 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9492 struct objfile *objfile = dwarf2_per_objfile->objfile;
9493 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9494 CORE_ADDR lowpc, highpc;
9495 struct compunit_symtab *cust;
9496 CORE_ADDR baseaddr;
9497 struct block *static_block;
9498 CORE_ADDR addr;
9499
9500 baseaddr = objfile->text_section_offset ();
9501
9502 /* Clear the list here in case something was left over. */
9503 cu->method_list.clear ();
9504
9505 cu->language = pretend_language;
9506 cu->language_defn = language_def (cu->language);
9507
9508 /* Do line number decoding in read_file_scope () */
9509 process_die (cu->dies, cu);
9510
9511 /* For now fudge the Go package. */
9512 if (cu->language == language_go)
9513 fixup_go_packaging (cu);
9514
9515 /* Now that we have processed all the DIEs in the CU, all the types
9516 should be complete, and it should now be safe to compute all of the
9517 physnames. */
9518 compute_delayed_physnames (cu);
9519
9520 if (cu->language == language_rust)
9521 rust_union_quirks (cu);
9522
9523 /* Some compilers don't define a DW_AT_high_pc attribute for the
9524 compilation unit. If the DW_AT_high_pc is missing, synthesize
9525 it, by scanning the DIE's below the compilation unit. */
9526 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9527
9528 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9529 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9530
9531 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9532 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9533 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9534 addrmap to help ensure it has an accurate map of pc values belonging to
9535 this comp unit. */
9536 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9537
9538 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9539 SECT_OFF_TEXT (objfile),
9540 0);
9541
9542 if (cust != NULL)
9543 {
9544 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9545
9546 /* Set symtab language to language from DW_AT_language. If the
9547 compilation is from a C file generated by language preprocessors, do
9548 not set the language if it was already deduced by start_subfile. */
9549 if (!(cu->language == language_c
9550 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9551 COMPUNIT_FILETABS (cust)->language = cu->language;
9552
9553 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9554 produce DW_AT_location with location lists but it can be possibly
9555 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9556 there were bugs in prologue debug info, fixed later in GCC-4.5
9557 by "unwind info for epilogues" patch (which is not directly related).
9558
9559 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9560 needed, it would be wrong due to missing DW_AT_producer there.
9561
9562 Still one can confuse GDB by using non-standard GCC compilation
9563 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9564 */
9565 if (cu->has_loclist && gcc_4_minor >= 5)
9566 cust->locations_valid = 1;
9567
9568 if (gcc_4_minor >= 5)
9569 cust->epilogue_unwind_valid = 1;
9570
9571 cust->call_site_htab = cu->call_site_htab;
9572 }
9573
9574 if (dwarf2_per_objfile->using_index)
9575 per_cu->v.quick->compunit_symtab = cust;
9576 else
9577 {
9578 dwarf2_psymtab *pst = per_cu->v.psymtab;
9579 pst->compunit_symtab = cust;
9580 pst->readin = true;
9581 }
9582
9583 /* Push it for inclusion processing later. */
9584 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9585
9586 /* Not needed any more. */
9587 cu->reset_builder ();
9588 }
9589
9590 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9591 already been loaded into memory. */
9592
9593 static void
9594 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9595 enum language pretend_language)
9596 {
9597 struct dwarf2_cu *cu = per_cu->cu;
9598 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9599 struct objfile *objfile = dwarf2_per_objfile->objfile;
9600 struct compunit_symtab *cust;
9601 struct signatured_type *sig_type;
9602
9603 gdb_assert (per_cu->is_debug_types);
9604 sig_type = (struct signatured_type *) per_cu;
9605
9606 /* Clear the list here in case something was left over. */
9607 cu->method_list.clear ();
9608
9609 cu->language = pretend_language;
9610 cu->language_defn = language_def (cu->language);
9611
9612 /* The symbol tables are set up in read_type_unit_scope. */
9613 process_die (cu->dies, cu);
9614
9615 /* For now fudge the Go package. */
9616 if (cu->language == language_go)
9617 fixup_go_packaging (cu);
9618
9619 /* Now that we have processed all the DIEs in the CU, all the types
9620 should be complete, and it should now be safe to compute all of the
9621 physnames. */
9622 compute_delayed_physnames (cu);
9623
9624 if (cu->language == language_rust)
9625 rust_union_quirks (cu);
9626
9627 /* TUs share symbol tables.
9628 If this is the first TU to use this symtab, complete the construction
9629 of it with end_expandable_symtab. Otherwise, complete the addition of
9630 this TU's symbols to the existing symtab. */
9631 if (sig_type->type_unit_group->compunit_symtab == NULL)
9632 {
9633 buildsym_compunit *builder = cu->get_builder ();
9634 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9635 sig_type->type_unit_group->compunit_symtab = cust;
9636
9637 if (cust != NULL)
9638 {
9639 /* Set symtab language to language from DW_AT_language. If the
9640 compilation is from a C file generated by language preprocessors,
9641 do not set the language if it was already deduced by
9642 start_subfile. */
9643 if (!(cu->language == language_c
9644 && COMPUNIT_FILETABS (cust)->language != language_c))
9645 COMPUNIT_FILETABS (cust)->language = cu->language;
9646 }
9647 }
9648 else
9649 {
9650 cu->get_builder ()->augment_type_symtab ();
9651 cust = sig_type->type_unit_group->compunit_symtab;
9652 }
9653
9654 if (dwarf2_per_objfile->using_index)
9655 per_cu->v.quick->compunit_symtab = cust;
9656 else
9657 {
9658 dwarf2_psymtab *pst = per_cu->v.psymtab;
9659 pst->compunit_symtab = cust;
9660 pst->readin = true;
9661 }
9662
9663 /* Not needed any more. */
9664 cu->reset_builder ();
9665 }
9666
9667 /* Process an imported unit DIE. */
9668
9669 static void
9670 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9671 {
9672 struct attribute *attr;
9673
9674 /* For now we don't handle imported units in type units. */
9675 if (cu->per_cu->is_debug_types)
9676 {
9677 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9678 " supported in type units [in module %s]"),
9679 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9680 }
9681
9682 attr = dwarf2_attr (die, DW_AT_import, cu);
9683 if (attr != NULL)
9684 {
9685 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9686 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9687 dwarf2_per_cu_data *per_cu
9688 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9689 cu->per_cu->dwarf2_per_objfile);
9690
9691 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9692 into another compilation unit, at root level. Regard this as a hint,
9693 and ignore it. */
9694 if (die->parent && die->parent->parent == NULL
9695 && per_cu->unit_type == DW_UT_compile
9696 && per_cu->lang == language_cplus)
9697 return;
9698
9699 /* If necessary, add it to the queue and load its DIEs. */
9700 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9701 load_full_comp_unit (per_cu, false, cu->language);
9702
9703 cu->per_cu->imported_symtabs_push (per_cu);
9704 }
9705 }
9706
9707 /* RAII object that represents a process_die scope: i.e.,
9708 starts/finishes processing a DIE. */
9709 class process_die_scope
9710 {
9711 public:
9712 process_die_scope (die_info *die, dwarf2_cu *cu)
9713 : m_die (die), m_cu (cu)
9714 {
9715 /* We should only be processing DIEs not already in process. */
9716 gdb_assert (!m_die->in_process);
9717 m_die->in_process = true;
9718 }
9719
9720 ~process_die_scope ()
9721 {
9722 m_die->in_process = false;
9723
9724 /* If we're done processing the DIE for the CU that owns the line
9725 header, we don't need the line header anymore. */
9726 if (m_cu->line_header_die_owner == m_die)
9727 {
9728 delete m_cu->line_header;
9729 m_cu->line_header = NULL;
9730 m_cu->line_header_die_owner = NULL;
9731 }
9732 }
9733
9734 private:
9735 die_info *m_die;
9736 dwarf2_cu *m_cu;
9737 };
9738
9739 /* Process a die and its children. */
9740
9741 static void
9742 process_die (struct die_info *die, struct dwarf2_cu *cu)
9743 {
9744 process_die_scope scope (die, cu);
9745
9746 switch (die->tag)
9747 {
9748 case DW_TAG_padding:
9749 break;
9750 case DW_TAG_compile_unit:
9751 case DW_TAG_partial_unit:
9752 read_file_scope (die, cu);
9753 break;
9754 case DW_TAG_type_unit:
9755 read_type_unit_scope (die, cu);
9756 break;
9757 case DW_TAG_subprogram:
9758 /* Nested subprograms in Fortran get a prefix. */
9759 if (cu->language == language_fortran
9760 && die->parent != NULL
9761 && die->parent->tag == DW_TAG_subprogram)
9762 cu->processing_has_namespace_info = true;
9763 /* Fall through. */
9764 case DW_TAG_inlined_subroutine:
9765 read_func_scope (die, cu);
9766 break;
9767 case DW_TAG_lexical_block:
9768 case DW_TAG_try_block:
9769 case DW_TAG_catch_block:
9770 read_lexical_block_scope (die, cu);
9771 break;
9772 case DW_TAG_call_site:
9773 case DW_TAG_GNU_call_site:
9774 read_call_site_scope (die, cu);
9775 break;
9776 case DW_TAG_class_type:
9777 case DW_TAG_interface_type:
9778 case DW_TAG_structure_type:
9779 case DW_TAG_union_type:
9780 process_structure_scope (die, cu);
9781 break;
9782 case DW_TAG_enumeration_type:
9783 process_enumeration_scope (die, cu);
9784 break;
9785
9786 /* These dies have a type, but processing them does not create
9787 a symbol or recurse to process the children. Therefore we can
9788 read them on-demand through read_type_die. */
9789 case DW_TAG_subroutine_type:
9790 case DW_TAG_set_type:
9791 case DW_TAG_array_type:
9792 case DW_TAG_pointer_type:
9793 case DW_TAG_ptr_to_member_type:
9794 case DW_TAG_reference_type:
9795 case DW_TAG_rvalue_reference_type:
9796 case DW_TAG_string_type:
9797 break;
9798
9799 case DW_TAG_base_type:
9800 case DW_TAG_subrange_type:
9801 case DW_TAG_typedef:
9802 /* Add a typedef symbol for the type definition, if it has a
9803 DW_AT_name. */
9804 new_symbol (die, read_type_die (die, cu), cu);
9805 break;
9806 case DW_TAG_common_block:
9807 read_common_block (die, cu);
9808 break;
9809 case DW_TAG_common_inclusion:
9810 break;
9811 case DW_TAG_namespace:
9812 cu->processing_has_namespace_info = true;
9813 read_namespace (die, cu);
9814 break;
9815 case DW_TAG_module:
9816 cu->processing_has_namespace_info = true;
9817 read_module (die, cu);
9818 break;
9819 case DW_TAG_imported_declaration:
9820 cu->processing_has_namespace_info = true;
9821 if (read_namespace_alias (die, cu))
9822 break;
9823 /* The declaration is not a global namespace alias. */
9824 /* Fall through. */
9825 case DW_TAG_imported_module:
9826 cu->processing_has_namespace_info = true;
9827 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9828 || cu->language != language_fortran))
9829 complaint (_("Tag '%s' has unexpected children"),
9830 dwarf_tag_name (die->tag));
9831 read_import_statement (die, cu);
9832 break;
9833
9834 case DW_TAG_imported_unit:
9835 process_imported_unit_die (die, cu);
9836 break;
9837
9838 case DW_TAG_variable:
9839 read_variable (die, cu);
9840 break;
9841
9842 default:
9843 new_symbol (die, NULL, cu);
9844 break;
9845 }
9846 }
9847 \f
9848 /* DWARF name computation. */
9849
9850 /* A helper function for dwarf2_compute_name which determines whether DIE
9851 needs to have the name of the scope prepended to the name listed in the
9852 die. */
9853
9854 static int
9855 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9856 {
9857 struct attribute *attr;
9858
9859 switch (die->tag)
9860 {
9861 case DW_TAG_namespace:
9862 case DW_TAG_typedef:
9863 case DW_TAG_class_type:
9864 case DW_TAG_interface_type:
9865 case DW_TAG_structure_type:
9866 case DW_TAG_union_type:
9867 case DW_TAG_enumeration_type:
9868 case DW_TAG_enumerator:
9869 case DW_TAG_subprogram:
9870 case DW_TAG_inlined_subroutine:
9871 case DW_TAG_member:
9872 case DW_TAG_imported_declaration:
9873 return 1;
9874
9875 case DW_TAG_variable:
9876 case DW_TAG_constant:
9877 /* We only need to prefix "globally" visible variables. These include
9878 any variable marked with DW_AT_external or any variable that
9879 lives in a namespace. [Variables in anonymous namespaces
9880 require prefixing, but they are not DW_AT_external.] */
9881
9882 if (dwarf2_attr (die, DW_AT_specification, cu))
9883 {
9884 struct dwarf2_cu *spec_cu = cu;
9885
9886 return die_needs_namespace (die_specification (die, &spec_cu),
9887 spec_cu);
9888 }
9889
9890 attr = dwarf2_attr (die, DW_AT_external, cu);
9891 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9892 && die->parent->tag != DW_TAG_module)
9893 return 0;
9894 /* A variable in a lexical block of some kind does not need a
9895 namespace, even though in C++ such variables may be external
9896 and have a mangled name. */
9897 if (die->parent->tag == DW_TAG_lexical_block
9898 || die->parent->tag == DW_TAG_try_block
9899 || die->parent->tag == DW_TAG_catch_block
9900 || die->parent->tag == DW_TAG_subprogram)
9901 return 0;
9902 return 1;
9903
9904 default:
9905 return 0;
9906 }
9907 }
9908
9909 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9910 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9911 defined for the given DIE. */
9912
9913 static struct attribute *
9914 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9915 {
9916 struct attribute *attr;
9917
9918 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9919 if (attr == NULL)
9920 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9921
9922 return attr;
9923 }
9924
9925 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9926 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9927 defined for the given DIE. */
9928
9929 static const char *
9930 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9931 {
9932 const char *linkage_name;
9933
9934 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9935 if (linkage_name == NULL)
9936 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9937
9938 return linkage_name;
9939 }
9940
9941 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9942 compute the physname for the object, which include a method's:
9943 - formal parameters (C++),
9944 - receiver type (Go),
9945
9946 The term "physname" is a bit confusing.
9947 For C++, for example, it is the demangled name.
9948 For Go, for example, it's the mangled name.
9949
9950 For Ada, return the DIE's linkage name rather than the fully qualified
9951 name. PHYSNAME is ignored..
9952
9953 The result is allocated on the objfile_obstack and canonicalized. */
9954
9955 static const char *
9956 dwarf2_compute_name (const char *name,
9957 struct die_info *die, struct dwarf2_cu *cu,
9958 int physname)
9959 {
9960 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9961
9962 if (name == NULL)
9963 name = dwarf2_name (die, cu);
9964
9965 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9966 but otherwise compute it by typename_concat inside GDB.
9967 FIXME: Actually this is not really true, or at least not always true.
9968 It's all very confusing. compute_and_set_names doesn't try to demangle
9969 Fortran names because there is no mangling standard. So new_symbol
9970 will set the demangled name to the result of dwarf2_full_name, and it is
9971 the demangled name that GDB uses if it exists. */
9972 if (cu->language == language_ada
9973 || (cu->language == language_fortran && physname))
9974 {
9975 /* For Ada unit, we prefer the linkage name over the name, as
9976 the former contains the exported name, which the user expects
9977 to be able to reference. Ideally, we want the user to be able
9978 to reference this entity using either natural or linkage name,
9979 but we haven't started looking at this enhancement yet. */
9980 const char *linkage_name = dw2_linkage_name (die, cu);
9981
9982 if (linkage_name != NULL)
9983 return linkage_name;
9984 }
9985
9986 /* These are the only languages we know how to qualify names in. */
9987 if (name != NULL
9988 && (cu->language == language_cplus
9989 || cu->language == language_fortran || cu->language == language_d
9990 || cu->language == language_rust))
9991 {
9992 if (die_needs_namespace (die, cu))
9993 {
9994 const char *prefix;
9995 const char *canonical_name = NULL;
9996
9997 string_file buf;
9998
9999 prefix = determine_prefix (die, cu);
10000 if (*prefix != '\0')
10001 {
10002 gdb::unique_xmalloc_ptr<char> prefixed_name
10003 (typename_concat (NULL, prefix, name, physname, cu));
10004
10005 buf.puts (prefixed_name.get ());
10006 }
10007 else
10008 buf.puts (name);
10009
10010 /* Template parameters may be specified in the DIE's DW_AT_name, or
10011 as children with DW_TAG_template_type_param or
10012 DW_TAG_value_type_param. If the latter, add them to the name
10013 here. If the name already has template parameters, then
10014 skip this step; some versions of GCC emit both, and
10015 it is more efficient to use the pre-computed name.
10016
10017 Something to keep in mind about this process: it is very
10018 unlikely, or in some cases downright impossible, to produce
10019 something that will match the mangled name of a function.
10020 If the definition of the function has the same debug info,
10021 we should be able to match up with it anyway. But fallbacks
10022 using the minimal symbol, for instance to find a method
10023 implemented in a stripped copy of libstdc++, will not work.
10024 If we do not have debug info for the definition, we will have to
10025 match them up some other way.
10026
10027 When we do name matching there is a related problem with function
10028 templates; two instantiated function templates are allowed to
10029 differ only by their return types, which we do not add here. */
10030
10031 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10032 {
10033 struct attribute *attr;
10034 struct die_info *child;
10035 int first = 1;
10036
10037 die->building_fullname = 1;
10038
10039 for (child = die->child; child != NULL; child = child->sibling)
10040 {
10041 struct type *type;
10042 LONGEST value;
10043 const gdb_byte *bytes;
10044 struct dwarf2_locexpr_baton *baton;
10045 struct value *v;
10046
10047 if (child->tag != DW_TAG_template_type_param
10048 && child->tag != DW_TAG_template_value_param)
10049 continue;
10050
10051 if (first)
10052 {
10053 buf.puts ("<");
10054 first = 0;
10055 }
10056 else
10057 buf.puts (", ");
10058
10059 attr = dwarf2_attr (child, DW_AT_type, cu);
10060 if (attr == NULL)
10061 {
10062 complaint (_("template parameter missing DW_AT_type"));
10063 buf.puts ("UNKNOWN_TYPE");
10064 continue;
10065 }
10066 type = die_type (child, cu);
10067
10068 if (child->tag == DW_TAG_template_type_param)
10069 {
10070 c_print_type (type, "", &buf, -1, 0, cu->language,
10071 &type_print_raw_options);
10072 continue;
10073 }
10074
10075 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10076 if (attr == NULL)
10077 {
10078 complaint (_("template parameter missing "
10079 "DW_AT_const_value"));
10080 buf.puts ("UNKNOWN_VALUE");
10081 continue;
10082 }
10083
10084 dwarf2_const_value_attr (attr, type, name,
10085 &cu->comp_unit_obstack, cu,
10086 &value, &bytes, &baton);
10087
10088 if (TYPE_NOSIGN (type))
10089 /* GDB prints characters as NUMBER 'CHAR'. If that's
10090 changed, this can use value_print instead. */
10091 c_printchar (value, type, &buf);
10092 else
10093 {
10094 struct value_print_options opts;
10095
10096 if (baton != NULL)
10097 v = dwarf2_evaluate_loc_desc (type, NULL,
10098 baton->data,
10099 baton->size,
10100 baton->per_cu);
10101 else if (bytes != NULL)
10102 {
10103 v = allocate_value (type);
10104 memcpy (value_contents_writeable (v), bytes,
10105 TYPE_LENGTH (type));
10106 }
10107 else
10108 v = value_from_longest (type, value);
10109
10110 /* Specify decimal so that we do not depend on
10111 the radix. */
10112 get_formatted_print_options (&opts, 'd');
10113 opts.raw = 1;
10114 value_print (v, &buf, &opts);
10115 release_value (v);
10116 }
10117 }
10118
10119 die->building_fullname = 0;
10120
10121 if (!first)
10122 {
10123 /* Close the argument list, with a space if necessary
10124 (nested templates). */
10125 if (!buf.empty () && buf.string ().back () == '>')
10126 buf.puts (" >");
10127 else
10128 buf.puts (">");
10129 }
10130 }
10131
10132 /* For C++ methods, append formal parameter type
10133 information, if PHYSNAME. */
10134
10135 if (physname && die->tag == DW_TAG_subprogram
10136 && cu->language == language_cplus)
10137 {
10138 struct type *type = read_type_die (die, cu);
10139
10140 c_type_print_args (type, &buf, 1, cu->language,
10141 &type_print_raw_options);
10142
10143 if (cu->language == language_cplus)
10144 {
10145 /* Assume that an artificial first parameter is
10146 "this", but do not crash if it is not. RealView
10147 marks unnamed (and thus unused) parameters as
10148 artificial; there is no way to differentiate
10149 the two cases. */
10150 if (TYPE_NFIELDS (type) > 0
10151 && TYPE_FIELD_ARTIFICIAL (type, 0)
10152 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10153 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10154 0))))
10155 buf.puts (" const");
10156 }
10157 }
10158
10159 const std::string &intermediate_name = buf.string ();
10160
10161 if (cu->language == language_cplus)
10162 canonical_name
10163 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10164 objfile);
10165
10166 /* If we only computed INTERMEDIATE_NAME, or if
10167 INTERMEDIATE_NAME is already canonical, then we need to
10168 intern it. */
10169 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10170 name = objfile->intern (intermediate_name);
10171 else
10172 name = canonical_name;
10173 }
10174 }
10175
10176 return name;
10177 }
10178
10179 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10180 If scope qualifiers are appropriate they will be added. The result
10181 will be allocated on the storage_obstack, or NULL if the DIE does
10182 not have a name. NAME may either be from a previous call to
10183 dwarf2_name or NULL.
10184
10185 The output string will be canonicalized (if C++). */
10186
10187 static const char *
10188 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10189 {
10190 return dwarf2_compute_name (name, die, cu, 0);
10191 }
10192
10193 /* Construct a physname for the given DIE in CU. NAME may either be
10194 from a previous call to dwarf2_name or NULL. The result will be
10195 allocated on the objfile_objstack or NULL if the DIE does not have a
10196 name.
10197
10198 The output string will be canonicalized (if C++). */
10199
10200 static const char *
10201 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10202 {
10203 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10204 const char *retval, *mangled = NULL, *canon = NULL;
10205 int need_copy = 1;
10206
10207 /* In this case dwarf2_compute_name is just a shortcut not building anything
10208 on its own. */
10209 if (!die_needs_namespace (die, cu))
10210 return dwarf2_compute_name (name, die, cu, 1);
10211
10212 mangled = dw2_linkage_name (die, cu);
10213
10214 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10215 See https://github.com/rust-lang/rust/issues/32925. */
10216 if (cu->language == language_rust && mangled != NULL
10217 && strchr (mangled, '{') != NULL)
10218 mangled = NULL;
10219
10220 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10221 has computed. */
10222 gdb::unique_xmalloc_ptr<char> demangled;
10223 if (mangled != NULL)
10224 {
10225
10226 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10227 {
10228 /* Do nothing (do not demangle the symbol name). */
10229 }
10230 else if (cu->language == language_go)
10231 {
10232 /* This is a lie, but we already lie to the caller new_symbol.
10233 new_symbol assumes we return the mangled name.
10234 This just undoes that lie until things are cleaned up. */
10235 }
10236 else
10237 {
10238 /* Use DMGL_RET_DROP for C++ template functions to suppress
10239 their return type. It is easier for GDB users to search
10240 for such functions as `name(params)' than `long name(params)'.
10241 In such case the minimal symbol names do not match the full
10242 symbol names but for template functions there is never a need
10243 to look up their definition from their declaration so
10244 the only disadvantage remains the minimal symbol variant
10245 `long name(params)' does not have the proper inferior type. */
10246 demangled.reset (gdb_demangle (mangled,
10247 (DMGL_PARAMS | DMGL_ANSI
10248 | DMGL_RET_DROP)));
10249 }
10250 if (demangled)
10251 canon = demangled.get ();
10252 else
10253 {
10254 canon = mangled;
10255 need_copy = 0;
10256 }
10257 }
10258
10259 if (canon == NULL || check_physname)
10260 {
10261 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10262
10263 if (canon != NULL && strcmp (physname, canon) != 0)
10264 {
10265 /* It may not mean a bug in GDB. The compiler could also
10266 compute DW_AT_linkage_name incorrectly. But in such case
10267 GDB would need to be bug-to-bug compatible. */
10268
10269 complaint (_("Computed physname <%s> does not match demangled <%s> "
10270 "(from linkage <%s>) - DIE at %s [in module %s]"),
10271 physname, canon, mangled, sect_offset_str (die->sect_off),
10272 objfile_name (objfile));
10273
10274 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10275 is available here - over computed PHYSNAME. It is safer
10276 against both buggy GDB and buggy compilers. */
10277
10278 retval = canon;
10279 }
10280 else
10281 {
10282 retval = physname;
10283 need_copy = 0;
10284 }
10285 }
10286 else
10287 retval = canon;
10288
10289 if (need_copy)
10290 retval = objfile->intern (retval);
10291
10292 return retval;
10293 }
10294
10295 /* Inspect DIE in CU for a namespace alias. If one exists, record
10296 a new symbol for it.
10297
10298 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10299
10300 static int
10301 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10302 {
10303 struct attribute *attr;
10304
10305 /* If the die does not have a name, this is not a namespace
10306 alias. */
10307 attr = dwarf2_attr (die, DW_AT_name, cu);
10308 if (attr != NULL)
10309 {
10310 int num;
10311 struct die_info *d = die;
10312 struct dwarf2_cu *imported_cu = cu;
10313
10314 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10315 keep inspecting DIEs until we hit the underlying import. */
10316 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10317 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10318 {
10319 attr = dwarf2_attr (d, DW_AT_import, cu);
10320 if (attr == NULL)
10321 break;
10322
10323 d = follow_die_ref (d, attr, &imported_cu);
10324 if (d->tag != DW_TAG_imported_declaration)
10325 break;
10326 }
10327
10328 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10329 {
10330 complaint (_("DIE at %s has too many recursively imported "
10331 "declarations"), sect_offset_str (d->sect_off));
10332 return 0;
10333 }
10334
10335 if (attr != NULL)
10336 {
10337 struct type *type;
10338 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10339
10340 type = get_die_type_at_offset (sect_off, cu->per_cu);
10341 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10342 {
10343 /* This declaration is a global namespace alias. Add
10344 a symbol for it whose type is the aliased namespace. */
10345 new_symbol (die, type, cu);
10346 return 1;
10347 }
10348 }
10349 }
10350
10351 return 0;
10352 }
10353
10354 /* Return the using directives repository (global or local?) to use in the
10355 current context for CU.
10356
10357 For Ada, imported declarations can materialize renamings, which *may* be
10358 global. However it is impossible (for now?) in DWARF to distinguish
10359 "external" imported declarations and "static" ones. As all imported
10360 declarations seem to be static in all other languages, make them all CU-wide
10361 global only in Ada. */
10362
10363 static struct using_direct **
10364 using_directives (struct dwarf2_cu *cu)
10365 {
10366 if (cu->language == language_ada
10367 && cu->get_builder ()->outermost_context_p ())
10368 return cu->get_builder ()->get_global_using_directives ();
10369 else
10370 return cu->get_builder ()->get_local_using_directives ();
10371 }
10372
10373 /* Read the import statement specified by the given die and record it. */
10374
10375 static void
10376 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10377 {
10378 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10379 struct attribute *import_attr;
10380 struct die_info *imported_die, *child_die;
10381 struct dwarf2_cu *imported_cu;
10382 const char *imported_name;
10383 const char *imported_name_prefix;
10384 const char *canonical_name;
10385 const char *import_alias;
10386 const char *imported_declaration = NULL;
10387 const char *import_prefix;
10388 std::vector<const char *> excludes;
10389
10390 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10391 if (import_attr == NULL)
10392 {
10393 complaint (_("Tag '%s' has no DW_AT_import"),
10394 dwarf_tag_name (die->tag));
10395 return;
10396 }
10397
10398 imported_cu = cu;
10399 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10400 imported_name = dwarf2_name (imported_die, imported_cu);
10401 if (imported_name == NULL)
10402 {
10403 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10404
10405 The import in the following code:
10406 namespace A
10407 {
10408 typedef int B;
10409 }
10410
10411 int main ()
10412 {
10413 using A::B;
10414 B b;
10415 return b;
10416 }
10417
10418 ...
10419 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10420 <52> DW_AT_decl_file : 1
10421 <53> DW_AT_decl_line : 6
10422 <54> DW_AT_import : <0x75>
10423 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10424 <59> DW_AT_name : B
10425 <5b> DW_AT_decl_file : 1
10426 <5c> DW_AT_decl_line : 2
10427 <5d> DW_AT_type : <0x6e>
10428 ...
10429 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10430 <76> DW_AT_byte_size : 4
10431 <77> DW_AT_encoding : 5 (signed)
10432
10433 imports the wrong die ( 0x75 instead of 0x58 ).
10434 This case will be ignored until the gcc bug is fixed. */
10435 return;
10436 }
10437
10438 /* Figure out the local name after import. */
10439 import_alias = dwarf2_name (die, cu);
10440
10441 /* Figure out where the statement is being imported to. */
10442 import_prefix = determine_prefix (die, cu);
10443
10444 /* Figure out what the scope of the imported die is and prepend it
10445 to the name of the imported die. */
10446 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10447
10448 if (imported_die->tag != DW_TAG_namespace
10449 && imported_die->tag != DW_TAG_module)
10450 {
10451 imported_declaration = imported_name;
10452 canonical_name = imported_name_prefix;
10453 }
10454 else if (strlen (imported_name_prefix) > 0)
10455 canonical_name = obconcat (&objfile->objfile_obstack,
10456 imported_name_prefix,
10457 (cu->language == language_d ? "." : "::"),
10458 imported_name, (char *) NULL);
10459 else
10460 canonical_name = imported_name;
10461
10462 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10463 for (child_die = die->child; child_die && child_die->tag;
10464 child_die = sibling_die (child_die))
10465 {
10466 /* DWARF-4: A Fortran use statement with a “rename list” may be
10467 represented by an imported module entry with an import attribute
10468 referring to the module and owned entries corresponding to those
10469 entities that are renamed as part of being imported. */
10470
10471 if (child_die->tag != DW_TAG_imported_declaration)
10472 {
10473 complaint (_("child DW_TAG_imported_declaration expected "
10474 "- DIE at %s [in module %s]"),
10475 sect_offset_str (child_die->sect_off),
10476 objfile_name (objfile));
10477 continue;
10478 }
10479
10480 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10481 if (import_attr == NULL)
10482 {
10483 complaint (_("Tag '%s' has no DW_AT_import"),
10484 dwarf_tag_name (child_die->tag));
10485 continue;
10486 }
10487
10488 imported_cu = cu;
10489 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10490 &imported_cu);
10491 imported_name = dwarf2_name (imported_die, imported_cu);
10492 if (imported_name == NULL)
10493 {
10494 complaint (_("child DW_TAG_imported_declaration has unknown "
10495 "imported name - DIE at %s [in module %s]"),
10496 sect_offset_str (child_die->sect_off),
10497 objfile_name (objfile));
10498 continue;
10499 }
10500
10501 excludes.push_back (imported_name);
10502
10503 process_die (child_die, cu);
10504 }
10505
10506 add_using_directive (using_directives (cu),
10507 import_prefix,
10508 canonical_name,
10509 import_alias,
10510 imported_declaration,
10511 excludes,
10512 0,
10513 &objfile->objfile_obstack);
10514 }
10515
10516 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10517 types, but gives them a size of zero. Starting with version 14,
10518 ICC is compatible with GCC. */
10519
10520 static bool
10521 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10522 {
10523 if (!cu->checked_producer)
10524 check_producer (cu);
10525
10526 return cu->producer_is_icc_lt_14;
10527 }
10528
10529 /* ICC generates a DW_AT_type for C void functions. This was observed on
10530 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10531 which says that void functions should not have a DW_AT_type. */
10532
10533 static bool
10534 producer_is_icc (struct dwarf2_cu *cu)
10535 {
10536 if (!cu->checked_producer)
10537 check_producer (cu);
10538
10539 return cu->producer_is_icc;
10540 }
10541
10542 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10543 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10544 this, it was first present in GCC release 4.3.0. */
10545
10546 static bool
10547 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10548 {
10549 if (!cu->checked_producer)
10550 check_producer (cu);
10551
10552 return cu->producer_is_gcc_lt_4_3;
10553 }
10554
10555 static file_and_directory
10556 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10557 {
10558 file_and_directory res;
10559
10560 /* Find the filename. Do not use dwarf2_name here, since the filename
10561 is not a source language identifier. */
10562 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10563 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10564
10565 if (res.comp_dir == NULL
10566 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10567 && IS_ABSOLUTE_PATH (res.name))
10568 {
10569 res.comp_dir_storage = ldirname (res.name);
10570 if (!res.comp_dir_storage.empty ())
10571 res.comp_dir = res.comp_dir_storage.c_str ();
10572 }
10573 if (res.comp_dir != NULL)
10574 {
10575 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10576 directory, get rid of it. */
10577 const char *cp = strchr (res.comp_dir, ':');
10578
10579 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10580 res.comp_dir = cp + 1;
10581 }
10582
10583 if (res.name == NULL)
10584 res.name = "<unknown>";
10585
10586 return res;
10587 }
10588
10589 /* Handle DW_AT_stmt_list for a compilation unit.
10590 DIE is the DW_TAG_compile_unit die for CU.
10591 COMP_DIR is the compilation directory. LOWPC is passed to
10592 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10593
10594 static void
10595 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10596 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10597 {
10598 struct dwarf2_per_objfile *dwarf2_per_objfile
10599 = cu->per_cu->dwarf2_per_objfile;
10600 struct attribute *attr;
10601 struct line_header line_header_local;
10602 hashval_t line_header_local_hash;
10603 void **slot;
10604 int decode_mapping;
10605
10606 gdb_assert (! cu->per_cu->is_debug_types);
10607
10608 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10609 if (attr == NULL)
10610 return;
10611
10612 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10613
10614 /* The line header hash table is only created if needed (it exists to
10615 prevent redundant reading of the line table for partial_units).
10616 If we're given a partial_unit, we'll need it. If we're given a
10617 compile_unit, then use the line header hash table if it's already
10618 created, but don't create one just yet. */
10619
10620 if (dwarf2_per_objfile->line_header_hash == NULL
10621 && die->tag == DW_TAG_partial_unit)
10622 {
10623 dwarf2_per_objfile->line_header_hash
10624 .reset (htab_create_alloc (127, line_header_hash_voidp,
10625 line_header_eq_voidp,
10626 free_line_header_voidp,
10627 xcalloc, xfree));
10628 }
10629
10630 line_header_local.sect_off = line_offset;
10631 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10632 line_header_local_hash = line_header_hash (&line_header_local);
10633 if (dwarf2_per_objfile->line_header_hash != NULL)
10634 {
10635 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10636 &line_header_local,
10637 line_header_local_hash, NO_INSERT);
10638
10639 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10640 is not present in *SLOT (since if there is something in *SLOT then
10641 it will be for a partial_unit). */
10642 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10643 {
10644 gdb_assert (*slot != NULL);
10645 cu->line_header = (struct line_header *) *slot;
10646 return;
10647 }
10648 }
10649
10650 /* dwarf_decode_line_header does not yet provide sufficient information.
10651 We always have to call also dwarf_decode_lines for it. */
10652 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10653 if (lh == NULL)
10654 return;
10655
10656 cu->line_header = lh.release ();
10657 cu->line_header_die_owner = die;
10658
10659 if (dwarf2_per_objfile->line_header_hash == NULL)
10660 slot = NULL;
10661 else
10662 {
10663 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10664 &line_header_local,
10665 line_header_local_hash, INSERT);
10666 gdb_assert (slot != NULL);
10667 }
10668 if (slot != NULL && *slot == NULL)
10669 {
10670 /* This newly decoded line number information unit will be owned
10671 by line_header_hash hash table. */
10672 *slot = cu->line_header;
10673 cu->line_header_die_owner = NULL;
10674 }
10675 else
10676 {
10677 /* We cannot free any current entry in (*slot) as that struct line_header
10678 may be already used by multiple CUs. Create only temporary decoded
10679 line_header for this CU - it may happen at most once for each line
10680 number information unit. And if we're not using line_header_hash
10681 then this is what we want as well. */
10682 gdb_assert (die->tag != DW_TAG_partial_unit);
10683 }
10684 decode_mapping = (die->tag != DW_TAG_partial_unit);
10685 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10686 decode_mapping);
10687
10688 }
10689
10690 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10691
10692 static void
10693 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10694 {
10695 struct dwarf2_per_objfile *dwarf2_per_objfile
10696 = cu->per_cu->dwarf2_per_objfile;
10697 struct objfile *objfile = dwarf2_per_objfile->objfile;
10698 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10699 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10700 CORE_ADDR highpc = ((CORE_ADDR) 0);
10701 struct attribute *attr;
10702 struct die_info *child_die;
10703 CORE_ADDR baseaddr;
10704
10705 prepare_one_comp_unit (cu, die, cu->language);
10706 baseaddr = objfile->text_section_offset ();
10707
10708 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10709
10710 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10711 from finish_block. */
10712 if (lowpc == ((CORE_ADDR) -1))
10713 lowpc = highpc;
10714 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10715
10716 file_and_directory fnd = find_file_and_directory (die, cu);
10717
10718 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10719 standardised yet. As a workaround for the language detection we fall
10720 back to the DW_AT_producer string. */
10721 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10722 cu->language = language_opencl;
10723
10724 /* Similar hack for Go. */
10725 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10726 set_cu_language (DW_LANG_Go, cu);
10727
10728 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10729
10730 /* Decode line number information if present. We do this before
10731 processing child DIEs, so that the line header table is available
10732 for DW_AT_decl_file. */
10733 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10734
10735 /* Process all dies in compilation unit. */
10736 if (die->child != NULL)
10737 {
10738 child_die = die->child;
10739 while (child_die && child_die->tag)
10740 {
10741 process_die (child_die, cu);
10742 child_die = sibling_die (child_die);
10743 }
10744 }
10745
10746 /* Decode macro information, if present. Dwarf 2 macro information
10747 refers to information in the line number info statement program
10748 header, so we can only read it if we've read the header
10749 successfully. */
10750 attr = dwarf2_attr (die, DW_AT_macros, cu);
10751 if (attr == NULL)
10752 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10753 if (attr && cu->line_header)
10754 {
10755 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10756 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10757
10758 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10759 }
10760 else
10761 {
10762 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10763 if (attr && cu->line_header)
10764 {
10765 unsigned int macro_offset = DW_UNSND (attr);
10766
10767 dwarf_decode_macros (cu, macro_offset, 0);
10768 }
10769 }
10770 }
10771
10772 void
10773 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10774 {
10775 struct type_unit_group *tu_group;
10776 int first_time;
10777 struct attribute *attr;
10778 unsigned int i;
10779 struct signatured_type *sig_type;
10780
10781 gdb_assert (per_cu->is_debug_types);
10782 sig_type = (struct signatured_type *) per_cu;
10783
10784 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10785
10786 /* If we're using .gdb_index (includes -readnow) then
10787 per_cu->type_unit_group may not have been set up yet. */
10788 if (sig_type->type_unit_group == NULL)
10789 sig_type->type_unit_group = get_type_unit_group (this, attr);
10790 tu_group = sig_type->type_unit_group;
10791
10792 /* If we've already processed this stmt_list there's no real need to
10793 do it again, we could fake it and just recreate the part we need
10794 (file name,index -> symtab mapping). If data shows this optimization
10795 is useful we can do it then. */
10796 first_time = tu_group->compunit_symtab == NULL;
10797
10798 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10799 debug info. */
10800 line_header_up lh;
10801 if (attr != NULL)
10802 {
10803 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10804 lh = dwarf_decode_line_header (line_offset, this);
10805 }
10806 if (lh == NULL)
10807 {
10808 if (first_time)
10809 start_symtab ("", NULL, 0);
10810 else
10811 {
10812 gdb_assert (tu_group->symtabs == NULL);
10813 gdb_assert (m_builder == nullptr);
10814 struct compunit_symtab *cust = tu_group->compunit_symtab;
10815 m_builder.reset (new struct buildsym_compunit
10816 (COMPUNIT_OBJFILE (cust), "",
10817 COMPUNIT_DIRNAME (cust),
10818 compunit_language (cust),
10819 0, cust));
10820 }
10821 return;
10822 }
10823
10824 line_header = lh.release ();
10825 line_header_die_owner = die;
10826
10827 if (first_time)
10828 {
10829 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10830
10831 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10832 still initializing it, and our caller (a few levels up)
10833 process_full_type_unit still needs to know if this is the first
10834 time. */
10835
10836 tu_group->symtabs
10837 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10838 struct symtab *, line_header->file_names_size ());
10839
10840 auto &file_names = line_header->file_names ();
10841 for (i = 0; i < file_names.size (); ++i)
10842 {
10843 file_entry &fe = file_names[i];
10844 dwarf2_start_subfile (this, fe.name,
10845 fe.include_dir (line_header));
10846 buildsym_compunit *b = get_builder ();
10847 if (b->get_current_subfile ()->symtab == NULL)
10848 {
10849 /* NOTE: start_subfile will recognize when it's been
10850 passed a file it has already seen. So we can't
10851 assume there's a simple mapping from
10852 cu->line_header->file_names to subfiles, plus
10853 cu->line_header->file_names may contain dups. */
10854 b->get_current_subfile ()->symtab
10855 = allocate_symtab (cust, b->get_current_subfile ()->name);
10856 }
10857
10858 fe.symtab = b->get_current_subfile ()->symtab;
10859 tu_group->symtabs[i] = fe.symtab;
10860 }
10861 }
10862 else
10863 {
10864 gdb_assert (m_builder == nullptr);
10865 struct compunit_symtab *cust = tu_group->compunit_symtab;
10866 m_builder.reset (new struct buildsym_compunit
10867 (COMPUNIT_OBJFILE (cust), "",
10868 COMPUNIT_DIRNAME (cust),
10869 compunit_language (cust),
10870 0, cust));
10871
10872 auto &file_names = line_header->file_names ();
10873 for (i = 0; i < file_names.size (); ++i)
10874 {
10875 file_entry &fe = file_names[i];
10876 fe.symtab = tu_group->symtabs[i];
10877 }
10878 }
10879
10880 /* The main symtab is allocated last. Type units don't have DW_AT_name
10881 so they don't have a "real" (so to speak) symtab anyway.
10882 There is later code that will assign the main symtab to all symbols
10883 that don't have one. We need to handle the case of a symbol with a
10884 missing symtab (DW_AT_decl_file) anyway. */
10885 }
10886
10887 /* Process DW_TAG_type_unit.
10888 For TUs we want to skip the first top level sibling if it's not the
10889 actual type being defined by this TU. In this case the first top
10890 level sibling is there to provide context only. */
10891
10892 static void
10893 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10894 {
10895 struct die_info *child_die;
10896
10897 prepare_one_comp_unit (cu, die, language_minimal);
10898
10899 /* Initialize (or reinitialize) the machinery for building symtabs.
10900 We do this before processing child DIEs, so that the line header table
10901 is available for DW_AT_decl_file. */
10902 cu->setup_type_unit_groups (die);
10903
10904 if (die->child != NULL)
10905 {
10906 child_die = die->child;
10907 while (child_die && child_die->tag)
10908 {
10909 process_die (child_die, cu);
10910 child_die = sibling_die (child_die);
10911 }
10912 }
10913 }
10914 \f
10915 /* DWO/DWP files.
10916
10917 http://gcc.gnu.org/wiki/DebugFission
10918 http://gcc.gnu.org/wiki/DebugFissionDWP
10919
10920 To simplify handling of both DWO files ("object" files with the DWARF info)
10921 and DWP files (a file with the DWOs packaged up into one file), we treat
10922 DWP files as having a collection of virtual DWO files. */
10923
10924 static hashval_t
10925 hash_dwo_file (const void *item)
10926 {
10927 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10928 hashval_t hash;
10929
10930 hash = htab_hash_string (dwo_file->dwo_name);
10931 if (dwo_file->comp_dir != NULL)
10932 hash += htab_hash_string (dwo_file->comp_dir);
10933 return hash;
10934 }
10935
10936 static int
10937 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10938 {
10939 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10940 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10941
10942 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10943 return 0;
10944 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10945 return lhs->comp_dir == rhs->comp_dir;
10946 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10947 }
10948
10949 /* Allocate a hash table for DWO files. */
10950
10951 static htab_up
10952 allocate_dwo_file_hash_table ()
10953 {
10954 auto delete_dwo_file = [] (void *item)
10955 {
10956 struct dwo_file *dwo_file = (struct dwo_file *) item;
10957
10958 delete dwo_file;
10959 };
10960
10961 return htab_up (htab_create_alloc (41,
10962 hash_dwo_file,
10963 eq_dwo_file,
10964 delete_dwo_file,
10965 xcalloc, xfree));
10966 }
10967
10968 /* Lookup DWO file DWO_NAME. */
10969
10970 static void **
10971 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10972 const char *dwo_name,
10973 const char *comp_dir)
10974 {
10975 struct dwo_file find_entry;
10976 void **slot;
10977
10978 if (dwarf2_per_objfile->dwo_files == NULL)
10979 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10980
10981 find_entry.dwo_name = dwo_name;
10982 find_entry.comp_dir = comp_dir;
10983 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10984 INSERT);
10985
10986 return slot;
10987 }
10988
10989 static hashval_t
10990 hash_dwo_unit (const void *item)
10991 {
10992 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10993
10994 /* This drops the top 32 bits of the id, but is ok for a hash. */
10995 return dwo_unit->signature;
10996 }
10997
10998 static int
10999 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11000 {
11001 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11002 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11003
11004 /* The signature is assumed to be unique within the DWO file.
11005 So while object file CU dwo_id's always have the value zero,
11006 that's OK, assuming each object file DWO file has only one CU,
11007 and that's the rule for now. */
11008 return lhs->signature == rhs->signature;
11009 }
11010
11011 /* Allocate a hash table for DWO CUs,TUs.
11012 There is one of these tables for each of CUs,TUs for each DWO file. */
11013
11014 static htab_up
11015 allocate_dwo_unit_table ()
11016 {
11017 /* Start out with a pretty small number.
11018 Generally DWO files contain only one CU and maybe some TUs. */
11019 return htab_up (htab_create_alloc (3,
11020 hash_dwo_unit,
11021 eq_dwo_unit,
11022 NULL, xcalloc, xfree));
11023 }
11024
11025 /* die_reader_func for create_dwo_cu. */
11026
11027 static void
11028 create_dwo_cu_reader (const struct die_reader_specs *reader,
11029 const gdb_byte *info_ptr,
11030 struct die_info *comp_unit_die,
11031 struct dwo_file *dwo_file,
11032 struct dwo_unit *dwo_unit)
11033 {
11034 struct dwarf2_cu *cu = reader->cu;
11035 sect_offset sect_off = cu->per_cu->sect_off;
11036 struct dwarf2_section_info *section = cu->per_cu->section;
11037
11038 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11039 if (!signature.has_value ())
11040 {
11041 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11042 " its dwo_id [in module %s]"),
11043 sect_offset_str (sect_off), dwo_file->dwo_name);
11044 return;
11045 }
11046
11047 dwo_unit->dwo_file = dwo_file;
11048 dwo_unit->signature = *signature;
11049 dwo_unit->section = section;
11050 dwo_unit->sect_off = sect_off;
11051 dwo_unit->length = cu->per_cu->length;
11052
11053 if (dwarf_read_debug)
11054 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11055 sect_offset_str (sect_off),
11056 hex_string (dwo_unit->signature));
11057 }
11058
11059 /* Create the dwo_units for the CUs in a DWO_FILE.
11060 Note: This function processes DWO files only, not DWP files. */
11061
11062 static void
11063 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11064 dwarf2_cu *cu, struct dwo_file &dwo_file,
11065 dwarf2_section_info &section, htab_up &cus_htab)
11066 {
11067 struct objfile *objfile = dwarf2_per_objfile->objfile;
11068 const gdb_byte *info_ptr, *end_ptr;
11069
11070 section.read (objfile);
11071 info_ptr = section.buffer;
11072
11073 if (info_ptr == NULL)
11074 return;
11075
11076 if (dwarf_read_debug)
11077 {
11078 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11079 section.get_name (),
11080 section.get_file_name ());
11081 }
11082
11083 end_ptr = info_ptr + section.size;
11084 while (info_ptr < end_ptr)
11085 {
11086 struct dwarf2_per_cu_data per_cu;
11087 struct dwo_unit read_unit {};
11088 struct dwo_unit *dwo_unit;
11089 void **slot;
11090 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11091
11092 memset (&per_cu, 0, sizeof (per_cu));
11093 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11094 per_cu.is_debug_types = 0;
11095 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11096 per_cu.section = &section;
11097
11098 cutu_reader reader (&per_cu, cu, &dwo_file);
11099 if (!reader.dummy_p)
11100 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11101 &dwo_file, &read_unit);
11102 info_ptr += per_cu.length;
11103
11104 // If the unit could not be parsed, skip it.
11105 if (read_unit.dwo_file == NULL)
11106 continue;
11107
11108 if (cus_htab == NULL)
11109 cus_htab = allocate_dwo_unit_table ();
11110
11111 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11112 *dwo_unit = read_unit;
11113 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11114 gdb_assert (slot != NULL);
11115 if (*slot != NULL)
11116 {
11117 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11118 sect_offset dup_sect_off = dup_cu->sect_off;
11119
11120 complaint (_("debug cu entry at offset %s is duplicate to"
11121 " the entry at offset %s, signature %s"),
11122 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11123 hex_string (dwo_unit->signature));
11124 }
11125 *slot = (void *)dwo_unit;
11126 }
11127 }
11128
11129 /* DWP file .debug_{cu,tu}_index section format:
11130 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11131
11132 DWP Version 1:
11133
11134 Both index sections have the same format, and serve to map a 64-bit
11135 signature to a set of section numbers. Each section begins with a header,
11136 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11137 indexes, and a pool of 32-bit section numbers. The index sections will be
11138 aligned at 8-byte boundaries in the file.
11139
11140 The index section header consists of:
11141
11142 V, 32 bit version number
11143 -, 32 bits unused
11144 N, 32 bit number of compilation units or type units in the index
11145 M, 32 bit number of slots in the hash table
11146
11147 Numbers are recorded using the byte order of the application binary.
11148
11149 The hash table begins at offset 16 in the section, and consists of an array
11150 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11151 order of the application binary). Unused slots in the hash table are 0.
11152 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11153
11154 The parallel table begins immediately after the hash table
11155 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11156 array of 32-bit indexes (using the byte order of the application binary),
11157 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11158 table contains a 32-bit index into the pool of section numbers. For unused
11159 hash table slots, the corresponding entry in the parallel table will be 0.
11160
11161 The pool of section numbers begins immediately following the hash table
11162 (at offset 16 + 12 * M from the beginning of the section). The pool of
11163 section numbers consists of an array of 32-bit words (using the byte order
11164 of the application binary). Each item in the array is indexed starting
11165 from 0. The hash table entry provides the index of the first section
11166 number in the set. Additional section numbers in the set follow, and the
11167 set is terminated by a 0 entry (section number 0 is not used in ELF).
11168
11169 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11170 section must be the first entry in the set, and the .debug_abbrev.dwo must
11171 be the second entry. Other members of the set may follow in any order.
11172
11173 ---
11174
11175 DWP Version 2:
11176
11177 DWP Version 2 combines all the .debug_info, etc. sections into one,
11178 and the entries in the index tables are now offsets into these sections.
11179 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11180 section.
11181
11182 Index Section Contents:
11183 Header
11184 Hash Table of Signatures dwp_hash_table.hash_table
11185 Parallel Table of Indices dwp_hash_table.unit_table
11186 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11187 Table of Section Sizes dwp_hash_table.v2.sizes
11188
11189 The index section header consists of:
11190
11191 V, 32 bit version number
11192 L, 32 bit number of columns in the table of section offsets
11193 N, 32 bit number of compilation units or type units in the index
11194 M, 32 bit number of slots in the hash table
11195
11196 Numbers are recorded using the byte order of the application binary.
11197
11198 The hash table has the same format as version 1.
11199 The parallel table of indices has the same format as version 1,
11200 except that the entries are origin-1 indices into the table of sections
11201 offsets and the table of section sizes.
11202
11203 The table of offsets begins immediately following the parallel table
11204 (at offset 16 + 12 * M from the beginning of the section). The table is
11205 a two-dimensional array of 32-bit words (using the byte order of the
11206 application binary), with L columns and N+1 rows, in row-major order.
11207 Each row in the array is indexed starting from 0. The first row provides
11208 a key to the remaining rows: each column in this row provides an identifier
11209 for a debug section, and the offsets in the same column of subsequent rows
11210 refer to that section. The section identifiers are:
11211
11212 DW_SECT_INFO 1 .debug_info.dwo
11213 DW_SECT_TYPES 2 .debug_types.dwo
11214 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11215 DW_SECT_LINE 4 .debug_line.dwo
11216 DW_SECT_LOC 5 .debug_loc.dwo
11217 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11218 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11219 DW_SECT_MACRO 8 .debug_macro.dwo
11220
11221 The offsets provided by the CU and TU index sections are the base offsets
11222 for the contributions made by each CU or TU to the corresponding section
11223 in the package file. Each CU and TU header contains an abbrev_offset
11224 field, used to find the abbreviations table for that CU or TU within the
11225 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11226 be interpreted as relative to the base offset given in the index section.
11227 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11228 should be interpreted as relative to the base offset for .debug_line.dwo,
11229 and offsets into other debug sections obtained from DWARF attributes should
11230 also be interpreted as relative to the corresponding base offset.
11231
11232 The table of sizes begins immediately following the table of offsets.
11233 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11234 with L columns and N rows, in row-major order. Each row in the array is
11235 indexed starting from 1 (row 0 is shared by the two tables).
11236
11237 ---
11238
11239 Hash table lookup is handled the same in version 1 and 2:
11240
11241 We assume that N and M will not exceed 2^32 - 1.
11242 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11243
11244 Given a 64-bit compilation unit signature or a type signature S, an entry
11245 in the hash table is located as follows:
11246
11247 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11248 the low-order k bits all set to 1.
11249
11250 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11251
11252 3) If the hash table entry at index H matches the signature, use that
11253 entry. If the hash table entry at index H is unused (all zeroes),
11254 terminate the search: the signature is not present in the table.
11255
11256 4) Let H = (H + H') modulo M. Repeat at Step 3.
11257
11258 Because M > N and H' and M are relatively prime, the search is guaranteed
11259 to stop at an unused slot or find the match. */
11260
11261 /* Create a hash table to map DWO IDs to their CU/TU entry in
11262 .debug_{info,types}.dwo in DWP_FILE.
11263 Returns NULL if there isn't one.
11264 Note: This function processes DWP files only, not DWO files. */
11265
11266 static struct dwp_hash_table *
11267 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11268 struct dwp_file *dwp_file, int is_debug_types)
11269 {
11270 struct objfile *objfile = dwarf2_per_objfile->objfile;
11271 bfd *dbfd = dwp_file->dbfd.get ();
11272 const gdb_byte *index_ptr, *index_end;
11273 struct dwarf2_section_info *index;
11274 uint32_t version, nr_columns, nr_units, nr_slots;
11275 struct dwp_hash_table *htab;
11276
11277 if (is_debug_types)
11278 index = &dwp_file->sections.tu_index;
11279 else
11280 index = &dwp_file->sections.cu_index;
11281
11282 if (index->empty ())
11283 return NULL;
11284 index->read (objfile);
11285
11286 index_ptr = index->buffer;
11287 index_end = index_ptr + index->size;
11288
11289 version = read_4_bytes (dbfd, index_ptr);
11290 index_ptr += 4;
11291 if (version == 2)
11292 nr_columns = read_4_bytes (dbfd, index_ptr);
11293 else
11294 nr_columns = 0;
11295 index_ptr += 4;
11296 nr_units = read_4_bytes (dbfd, index_ptr);
11297 index_ptr += 4;
11298 nr_slots = read_4_bytes (dbfd, index_ptr);
11299 index_ptr += 4;
11300
11301 if (version != 1 && version != 2)
11302 {
11303 error (_("Dwarf Error: unsupported DWP file version (%s)"
11304 " [in module %s]"),
11305 pulongest (version), dwp_file->name);
11306 }
11307 if (nr_slots != (nr_slots & -nr_slots))
11308 {
11309 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11310 " is not power of 2 [in module %s]"),
11311 pulongest (nr_slots), dwp_file->name);
11312 }
11313
11314 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11315 htab->version = version;
11316 htab->nr_columns = nr_columns;
11317 htab->nr_units = nr_units;
11318 htab->nr_slots = nr_slots;
11319 htab->hash_table = index_ptr;
11320 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11321
11322 /* Exit early if the table is empty. */
11323 if (nr_slots == 0 || nr_units == 0
11324 || (version == 2 && nr_columns == 0))
11325 {
11326 /* All must be zero. */
11327 if (nr_slots != 0 || nr_units != 0
11328 || (version == 2 && nr_columns != 0))
11329 {
11330 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11331 " all zero [in modules %s]"),
11332 dwp_file->name);
11333 }
11334 return htab;
11335 }
11336
11337 if (version == 1)
11338 {
11339 htab->section_pool.v1.indices =
11340 htab->unit_table + sizeof (uint32_t) * nr_slots;
11341 /* It's harder to decide whether the section is too small in v1.
11342 V1 is deprecated anyway so we punt. */
11343 }
11344 else
11345 {
11346 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11347 int *ids = htab->section_pool.v2.section_ids;
11348 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11349 /* Reverse map for error checking. */
11350 int ids_seen[DW_SECT_MAX + 1];
11351 int i;
11352
11353 if (nr_columns < 2)
11354 {
11355 error (_("Dwarf Error: bad DWP hash table, too few columns"
11356 " in section table [in module %s]"),
11357 dwp_file->name);
11358 }
11359 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11360 {
11361 error (_("Dwarf Error: bad DWP hash table, too many columns"
11362 " in section table [in module %s]"),
11363 dwp_file->name);
11364 }
11365 memset (ids, 255, sizeof_ids);
11366 memset (ids_seen, 255, sizeof (ids_seen));
11367 for (i = 0; i < nr_columns; ++i)
11368 {
11369 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11370
11371 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11372 {
11373 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11374 " in section table [in module %s]"),
11375 id, dwp_file->name);
11376 }
11377 if (ids_seen[id] != -1)
11378 {
11379 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11380 " id %d in section table [in module %s]"),
11381 id, dwp_file->name);
11382 }
11383 ids_seen[id] = i;
11384 ids[i] = id;
11385 }
11386 /* Must have exactly one info or types section. */
11387 if (((ids_seen[DW_SECT_INFO] != -1)
11388 + (ids_seen[DW_SECT_TYPES] != -1))
11389 != 1)
11390 {
11391 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11392 " DWO info/types section [in module %s]"),
11393 dwp_file->name);
11394 }
11395 /* Must have an abbrev section. */
11396 if (ids_seen[DW_SECT_ABBREV] == -1)
11397 {
11398 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11399 " section [in module %s]"),
11400 dwp_file->name);
11401 }
11402 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11403 htab->section_pool.v2.sizes =
11404 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11405 * nr_units * nr_columns);
11406 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11407 * nr_units * nr_columns))
11408 > index_end)
11409 {
11410 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11411 " [in module %s]"),
11412 dwp_file->name);
11413 }
11414 }
11415
11416 return htab;
11417 }
11418
11419 /* Update SECTIONS with the data from SECTP.
11420
11421 This function is like the other "locate" section routines that are
11422 passed to bfd_map_over_sections, but in this context the sections to
11423 read comes from the DWP V1 hash table, not the full ELF section table.
11424
11425 The result is non-zero for success, or zero if an error was found. */
11426
11427 static int
11428 locate_v1_virtual_dwo_sections (asection *sectp,
11429 struct virtual_v1_dwo_sections *sections)
11430 {
11431 const struct dwop_section_names *names = &dwop_section_names;
11432
11433 if (section_is_p (sectp->name, &names->abbrev_dwo))
11434 {
11435 /* There can be only one. */
11436 if (sections->abbrev.s.section != NULL)
11437 return 0;
11438 sections->abbrev.s.section = sectp;
11439 sections->abbrev.size = bfd_section_size (sectp);
11440 }
11441 else if (section_is_p (sectp->name, &names->info_dwo)
11442 || section_is_p (sectp->name, &names->types_dwo))
11443 {
11444 /* There can be only one. */
11445 if (sections->info_or_types.s.section != NULL)
11446 return 0;
11447 sections->info_or_types.s.section = sectp;
11448 sections->info_or_types.size = bfd_section_size (sectp);
11449 }
11450 else if (section_is_p (sectp->name, &names->line_dwo))
11451 {
11452 /* There can be only one. */
11453 if (sections->line.s.section != NULL)
11454 return 0;
11455 sections->line.s.section = sectp;
11456 sections->line.size = bfd_section_size (sectp);
11457 }
11458 else if (section_is_p (sectp->name, &names->loc_dwo))
11459 {
11460 /* There can be only one. */
11461 if (sections->loc.s.section != NULL)
11462 return 0;
11463 sections->loc.s.section = sectp;
11464 sections->loc.size = bfd_section_size (sectp);
11465 }
11466 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11467 {
11468 /* There can be only one. */
11469 if (sections->macinfo.s.section != NULL)
11470 return 0;
11471 sections->macinfo.s.section = sectp;
11472 sections->macinfo.size = bfd_section_size (sectp);
11473 }
11474 else if (section_is_p (sectp->name, &names->macro_dwo))
11475 {
11476 /* There can be only one. */
11477 if (sections->macro.s.section != NULL)
11478 return 0;
11479 sections->macro.s.section = sectp;
11480 sections->macro.size = bfd_section_size (sectp);
11481 }
11482 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11483 {
11484 /* There can be only one. */
11485 if (sections->str_offsets.s.section != NULL)
11486 return 0;
11487 sections->str_offsets.s.section = sectp;
11488 sections->str_offsets.size = bfd_section_size (sectp);
11489 }
11490 else
11491 {
11492 /* No other kind of section is valid. */
11493 return 0;
11494 }
11495
11496 return 1;
11497 }
11498
11499 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11500 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11501 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11502 This is for DWP version 1 files. */
11503
11504 static struct dwo_unit *
11505 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11506 struct dwp_file *dwp_file,
11507 uint32_t unit_index,
11508 const char *comp_dir,
11509 ULONGEST signature, int is_debug_types)
11510 {
11511 struct objfile *objfile = dwarf2_per_objfile->objfile;
11512 const struct dwp_hash_table *dwp_htab =
11513 is_debug_types ? dwp_file->tus : dwp_file->cus;
11514 bfd *dbfd = dwp_file->dbfd.get ();
11515 const char *kind = is_debug_types ? "TU" : "CU";
11516 struct dwo_file *dwo_file;
11517 struct dwo_unit *dwo_unit;
11518 struct virtual_v1_dwo_sections sections;
11519 void **dwo_file_slot;
11520 int i;
11521
11522 gdb_assert (dwp_file->version == 1);
11523
11524 if (dwarf_read_debug)
11525 {
11526 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11527 kind,
11528 pulongest (unit_index), hex_string (signature),
11529 dwp_file->name);
11530 }
11531
11532 /* Fetch the sections of this DWO unit.
11533 Put a limit on the number of sections we look for so that bad data
11534 doesn't cause us to loop forever. */
11535
11536 #define MAX_NR_V1_DWO_SECTIONS \
11537 (1 /* .debug_info or .debug_types */ \
11538 + 1 /* .debug_abbrev */ \
11539 + 1 /* .debug_line */ \
11540 + 1 /* .debug_loc */ \
11541 + 1 /* .debug_str_offsets */ \
11542 + 1 /* .debug_macro or .debug_macinfo */ \
11543 + 1 /* trailing zero */)
11544
11545 memset (&sections, 0, sizeof (sections));
11546
11547 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11548 {
11549 asection *sectp;
11550 uint32_t section_nr =
11551 read_4_bytes (dbfd,
11552 dwp_htab->section_pool.v1.indices
11553 + (unit_index + i) * sizeof (uint32_t));
11554
11555 if (section_nr == 0)
11556 break;
11557 if (section_nr >= dwp_file->num_sections)
11558 {
11559 error (_("Dwarf Error: bad DWP hash table, section number too large"
11560 " [in module %s]"),
11561 dwp_file->name);
11562 }
11563
11564 sectp = dwp_file->elf_sections[section_nr];
11565 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11566 {
11567 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11568 " [in module %s]"),
11569 dwp_file->name);
11570 }
11571 }
11572
11573 if (i < 2
11574 || sections.info_or_types.empty ()
11575 || sections.abbrev.empty ())
11576 {
11577 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11578 " [in module %s]"),
11579 dwp_file->name);
11580 }
11581 if (i == MAX_NR_V1_DWO_SECTIONS)
11582 {
11583 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11584 " [in module %s]"),
11585 dwp_file->name);
11586 }
11587
11588 /* It's easier for the rest of the code if we fake a struct dwo_file and
11589 have dwo_unit "live" in that. At least for now.
11590
11591 The DWP file can be made up of a random collection of CUs and TUs.
11592 However, for each CU + set of TUs that came from the same original DWO
11593 file, we can combine them back into a virtual DWO file to save space
11594 (fewer struct dwo_file objects to allocate). Remember that for really
11595 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11596
11597 std::string virtual_dwo_name =
11598 string_printf ("virtual-dwo/%d-%d-%d-%d",
11599 sections.abbrev.get_id (),
11600 sections.line.get_id (),
11601 sections.loc.get_id (),
11602 sections.str_offsets.get_id ());
11603 /* Can we use an existing virtual DWO file? */
11604 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11605 virtual_dwo_name.c_str (),
11606 comp_dir);
11607 /* Create one if necessary. */
11608 if (*dwo_file_slot == NULL)
11609 {
11610 if (dwarf_read_debug)
11611 {
11612 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11613 virtual_dwo_name.c_str ());
11614 }
11615 dwo_file = new struct dwo_file;
11616 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11617 dwo_file->comp_dir = comp_dir;
11618 dwo_file->sections.abbrev = sections.abbrev;
11619 dwo_file->sections.line = sections.line;
11620 dwo_file->sections.loc = sections.loc;
11621 dwo_file->sections.macinfo = sections.macinfo;
11622 dwo_file->sections.macro = sections.macro;
11623 dwo_file->sections.str_offsets = sections.str_offsets;
11624 /* The "str" section is global to the entire DWP file. */
11625 dwo_file->sections.str = dwp_file->sections.str;
11626 /* The info or types section is assigned below to dwo_unit,
11627 there's no need to record it in dwo_file.
11628 Also, we can't simply record type sections in dwo_file because
11629 we record a pointer into the vector in dwo_unit. As we collect more
11630 types we'll grow the vector and eventually have to reallocate space
11631 for it, invalidating all copies of pointers into the previous
11632 contents. */
11633 *dwo_file_slot = dwo_file;
11634 }
11635 else
11636 {
11637 if (dwarf_read_debug)
11638 {
11639 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11640 virtual_dwo_name.c_str ());
11641 }
11642 dwo_file = (struct dwo_file *) *dwo_file_slot;
11643 }
11644
11645 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11646 dwo_unit->dwo_file = dwo_file;
11647 dwo_unit->signature = signature;
11648 dwo_unit->section =
11649 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11650 *dwo_unit->section = sections.info_or_types;
11651 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11652
11653 return dwo_unit;
11654 }
11655
11656 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11657 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11658 piece within that section used by a TU/CU, return a virtual section
11659 of just that piece. */
11660
11661 static struct dwarf2_section_info
11662 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11663 struct dwarf2_section_info *section,
11664 bfd_size_type offset, bfd_size_type size)
11665 {
11666 struct dwarf2_section_info result;
11667 asection *sectp;
11668
11669 gdb_assert (section != NULL);
11670 gdb_assert (!section->is_virtual);
11671
11672 memset (&result, 0, sizeof (result));
11673 result.s.containing_section = section;
11674 result.is_virtual = true;
11675
11676 if (size == 0)
11677 return result;
11678
11679 sectp = section->get_bfd_section ();
11680
11681 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11682 bounds of the real section. This is a pretty-rare event, so just
11683 flag an error (easier) instead of a warning and trying to cope. */
11684 if (sectp == NULL
11685 || offset + size > bfd_section_size (sectp))
11686 {
11687 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11688 " in section %s [in module %s]"),
11689 sectp ? bfd_section_name (sectp) : "<unknown>",
11690 objfile_name (dwarf2_per_objfile->objfile));
11691 }
11692
11693 result.virtual_offset = offset;
11694 result.size = size;
11695 return result;
11696 }
11697
11698 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11699 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11700 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11701 This is for DWP version 2 files. */
11702
11703 static struct dwo_unit *
11704 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11705 struct dwp_file *dwp_file,
11706 uint32_t unit_index,
11707 const char *comp_dir,
11708 ULONGEST signature, int is_debug_types)
11709 {
11710 struct objfile *objfile = dwarf2_per_objfile->objfile;
11711 const struct dwp_hash_table *dwp_htab =
11712 is_debug_types ? dwp_file->tus : dwp_file->cus;
11713 bfd *dbfd = dwp_file->dbfd.get ();
11714 const char *kind = is_debug_types ? "TU" : "CU";
11715 struct dwo_file *dwo_file;
11716 struct dwo_unit *dwo_unit;
11717 struct virtual_v2_dwo_sections sections;
11718 void **dwo_file_slot;
11719 int i;
11720
11721 gdb_assert (dwp_file->version == 2);
11722
11723 if (dwarf_read_debug)
11724 {
11725 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11726 kind,
11727 pulongest (unit_index), hex_string (signature),
11728 dwp_file->name);
11729 }
11730
11731 /* Fetch the section offsets of this DWO unit. */
11732
11733 memset (&sections, 0, sizeof (sections));
11734
11735 for (i = 0; i < dwp_htab->nr_columns; ++i)
11736 {
11737 uint32_t offset = read_4_bytes (dbfd,
11738 dwp_htab->section_pool.v2.offsets
11739 + (((unit_index - 1) * dwp_htab->nr_columns
11740 + i)
11741 * sizeof (uint32_t)));
11742 uint32_t size = read_4_bytes (dbfd,
11743 dwp_htab->section_pool.v2.sizes
11744 + (((unit_index - 1) * dwp_htab->nr_columns
11745 + i)
11746 * sizeof (uint32_t)));
11747
11748 switch (dwp_htab->section_pool.v2.section_ids[i])
11749 {
11750 case DW_SECT_INFO:
11751 case DW_SECT_TYPES:
11752 sections.info_or_types_offset = offset;
11753 sections.info_or_types_size = size;
11754 break;
11755 case DW_SECT_ABBREV:
11756 sections.abbrev_offset = offset;
11757 sections.abbrev_size = size;
11758 break;
11759 case DW_SECT_LINE:
11760 sections.line_offset = offset;
11761 sections.line_size = size;
11762 break;
11763 case DW_SECT_LOC:
11764 sections.loc_offset = offset;
11765 sections.loc_size = size;
11766 break;
11767 case DW_SECT_STR_OFFSETS:
11768 sections.str_offsets_offset = offset;
11769 sections.str_offsets_size = size;
11770 break;
11771 case DW_SECT_MACINFO:
11772 sections.macinfo_offset = offset;
11773 sections.macinfo_size = size;
11774 break;
11775 case DW_SECT_MACRO:
11776 sections.macro_offset = offset;
11777 sections.macro_size = size;
11778 break;
11779 }
11780 }
11781
11782 /* It's easier for the rest of the code if we fake a struct dwo_file and
11783 have dwo_unit "live" in that. At least for now.
11784
11785 The DWP file can be made up of a random collection of CUs and TUs.
11786 However, for each CU + set of TUs that came from the same original DWO
11787 file, we can combine them back into a virtual DWO file to save space
11788 (fewer struct dwo_file objects to allocate). Remember that for really
11789 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11790
11791 std::string virtual_dwo_name =
11792 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11793 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11794 (long) (sections.line_size ? sections.line_offset : 0),
11795 (long) (sections.loc_size ? sections.loc_offset : 0),
11796 (long) (sections.str_offsets_size
11797 ? sections.str_offsets_offset : 0));
11798 /* Can we use an existing virtual DWO file? */
11799 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11800 virtual_dwo_name.c_str (),
11801 comp_dir);
11802 /* Create one if necessary. */
11803 if (*dwo_file_slot == NULL)
11804 {
11805 if (dwarf_read_debug)
11806 {
11807 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11808 virtual_dwo_name.c_str ());
11809 }
11810 dwo_file = new struct dwo_file;
11811 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11812 dwo_file->comp_dir = comp_dir;
11813 dwo_file->sections.abbrev =
11814 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11815 sections.abbrev_offset, sections.abbrev_size);
11816 dwo_file->sections.line =
11817 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11818 sections.line_offset, sections.line_size);
11819 dwo_file->sections.loc =
11820 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11821 sections.loc_offset, sections.loc_size);
11822 dwo_file->sections.macinfo =
11823 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11824 sections.macinfo_offset, sections.macinfo_size);
11825 dwo_file->sections.macro =
11826 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11827 sections.macro_offset, sections.macro_size);
11828 dwo_file->sections.str_offsets =
11829 create_dwp_v2_section (dwarf2_per_objfile,
11830 &dwp_file->sections.str_offsets,
11831 sections.str_offsets_offset,
11832 sections.str_offsets_size);
11833 /* The "str" section is global to the entire DWP file. */
11834 dwo_file->sections.str = dwp_file->sections.str;
11835 /* The info or types section is assigned below to dwo_unit,
11836 there's no need to record it in dwo_file.
11837 Also, we can't simply record type sections in dwo_file because
11838 we record a pointer into the vector in dwo_unit. As we collect more
11839 types we'll grow the vector and eventually have to reallocate space
11840 for it, invalidating all copies of pointers into the previous
11841 contents. */
11842 *dwo_file_slot = dwo_file;
11843 }
11844 else
11845 {
11846 if (dwarf_read_debug)
11847 {
11848 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11849 virtual_dwo_name.c_str ());
11850 }
11851 dwo_file = (struct dwo_file *) *dwo_file_slot;
11852 }
11853
11854 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11855 dwo_unit->dwo_file = dwo_file;
11856 dwo_unit->signature = signature;
11857 dwo_unit->section =
11858 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11859 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11860 is_debug_types
11861 ? &dwp_file->sections.types
11862 : &dwp_file->sections.info,
11863 sections.info_or_types_offset,
11864 sections.info_or_types_size);
11865 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11866
11867 return dwo_unit;
11868 }
11869
11870 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11871 Returns NULL if the signature isn't found. */
11872
11873 static struct dwo_unit *
11874 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11875 struct dwp_file *dwp_file, const char *comp_dir,
11876 ULONGEST signature, int is_debug_types)
11877 {
11878 const struct dwp_hash_table *dwp_htab =
11879 is_debug_types ? dwp_file->tus : dwp_file->cus;
11880 bfd *dbfd = dwp_file->dbfd.get ();
11881 uint32_t mask = dwp_htab->nr_slots - 1;
11882 uint32_t hash = signature & mask;
11883 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11884 unsigned int i;
11885 void **slot;
11886 struct dwo_unit find_dwo_cu;
11887
11888 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11889 find_dwo_cu.signature = signature;
11890 slot = htab_find_slot (is_debug_types
11891 ? dwp_file->loaded_tus.get ()
11892 : dwp_file->loaded_cus.get (),
11893 &find_dwo_cu, INSERT);
11894
11895 if (*slot != NULL)
11896 return (struct dwo_unit *) *slot;
11897
11898 /* Use a for loop so that we don't loop forever on bad debug info. */
11899 for (i = 0; i < dwp_htab->nr_slots; ++i)
11900 {
11901 ULONGEST signature_in_table;
11902
11903 signature_in_table =
11904 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11905 if (signature_in_table == signature)
11906 {
11907 uint32_t unit_index =
11908 read_4_bytes (dbfd,
11909 dwp_htab->unit_table + hash * sizeof (uint32_t));
11910
11911 if (dwp_file->version == 1)
11912 {
11913 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11914 dwp_file, unit_index,
11915 comp_dir, signature,
11916 is_debug_types);
11917 }
11918 else
11919 {
11920 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11921 dwp_file, unit_index,
11922 comp_dir, signature,
11923 is_debug_types);
11924 }
11925 return (struct dwo_unit *) *slot;
11926 }
11927 if (signature_in_table == 0)
11928 return NULL;
11929 hash = (hash + hash2) & mask;
11930 }
11931
11932 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11933 " [in module %s]"),
11934 dwp_file->name);
11935 }
11936
11937 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11938 Open the file specified by FILE_NAME and hand it off to BFD for
11939 preliminary analysis. Return a newly initialized bfd *, which
11940 includes a canonicalized copy of FILE_NAME.
11941 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11942 SEARCH_CWD is true if the current directory is to be searched.
11943 It will be searched before debug-file-directory.
11944 If successful, the file is added to the bfd include table of the
11945 objfile's bfd (see gdb_bfd_record_inclusion).
11946 If unable to find/open the file, return NULL.
11947 NOTE: This function is derived from symfile_bfd_open. */
11948
11949 static gdb_bfd_ref_ptr
11950 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11951 const char *file_name, int is_dwp, int search_cwd)
11952 {
11953 int desc;
11954 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11955 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11956 to debug_file_directory. */
11957 const char *search_path;
11958 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11959
11960 gdb::unique_xmalloc_ptr<char> search_path_holder;
11961 if (search_cwd)
11962 {
11963 if (*debug_file_directory != '\0')
11964 {
11965 search_path_holder.reset (concat (".", dirname_separator_string,
11966 debug_file_directory,
11967 (char *) NULL));
11968 search_path = search_path_holder.get ();
11969 }
11970 else
11971 search_path = ".";
11972 }
11973 else
11974 search_path = debug_file_directory;
11975
11976 openp_flags flags = OPF_RETURN_REALPATH;
11977 if (is_dwp)
11978 flags |= OPF_SEARCH_IN_PATH;
11979
11980 gdb::unique_xmalloc_ptr<char> absolute_name;
11981 desc = openp (search_path, flags, file_name,
11982 O_RDONLY | O_BINARY, &absolute_name);
11983 if (desc < 0)
11984 return NULL;
11985
11986 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11987 gnutarget, desc));
11988 if (sym_bfd == NULL)
11989 return NULL;
11990 bfd_set_cacheable (sym_bfd.get (), 1);
11991
11992 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11993 return NULL;
11994
11995 /* Success. Record the bfd as having been included by the objfile's bfd.
11996 This is important because things like demangled_names_hash lives in the
11997 objfile's per_bfd space and may have references to things like symbol
11998 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11999 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12000
12001 return sym_bfd;
12002 }
12003
12004 /* Try to open DWO file FILE_NAME.
12005 COMP_DIR is the DW_AT_comp_dir attribute.
12006 The result is the bfd handle of the file.
12007 If there is a problem finding or opening the file, return NULL.
12008 Upon success, the canonicalized path of the file is stored in the bfd,
12009 same as symfile_bfd_open. */
12010
12011 static gdb_bfd_ref_ptr
12012 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12013 const char *file_name, const char *comp_dir)
12014 {
12015 if (IS_ABSOLUTE_PATH (file_name))
12016 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12017 0 /*is_dwp*/, 0 /*search_cwd*/);
12018
12019 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12020
12021 if (comp_dir != NULL)
12022 {
12023 gdb::unique_xmalloc_ptr<char> path_to_try
12024 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12025
12026 /* NOTE: If comp_dir is a relative path, this will also try the
12027 search path, which seems useful. */
12028 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12029 path_to_try.get (),
12030 0 /*is_dwp*/,
12031 1 /*search_cwd*/));
12032 if (abfd != NULL)
12033 return abfd;
12034 }
12035
12036 /* That didn't work, try debug-file-directory, which, despite its name,
12037 is a list of paths. */
12038
12039 if (*debug_file_directory == '\0')
12040 return NULL;
12041
12042 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12043 0 /*is_dwp*/, 1 /*search_cwd*/);
12044 }
12045
12046 /* This function is mapped across the sections and remembers the offset and
12047 size of each of the DWO debugging sections we are interested in. */
12048
12049 static void
12050 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12051 {
12052 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12053 const struct dwop_section_names *names = &dwop_section_names;
12054
12055 if (section_is_p (sectp->name, &names->abbrev_dwo))
12056 {
12057 dwo_sections->abbrev.s.section = sectp;
12058 dwo_sections->abbrev.size = bfd_section_size (sectp);
12059 }
12060 else if (section_is_p (sectp->name, &names->info_dwo))
12061 {
12062 dwo_sections->info.s.section = sectp;
12063 dwo_sections->info.size = bfd_section_size (sectp);
12064 }
12065 else if (section_is_p (sectp->name, &names->line_dwo))
12066 {
12067 dwo_sections->line.s.section = sectp;
12068 dwo_sections->line.size = bfd_section_size (sectp);
12069 }
12070 else if (section_is_p (sectp->name, &names->loc_dwo))
12071 {
12072 dwo_sections->loc.s.section = sectp;
12073 dwo_sections->loc.size = bfd_section_size (sectp);
12074 }
12075 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12076 {
12077 dwo_sections->macinfo.s.section = sectp;
12078 dwo_sections->macinfo.size = bfd_section_size (sectp);
12079 }
12080 else if (section_is_p (sectp->name, &names->macro_dwo))
12081 {
12082 dwo_sections->macro.s.section = sectp;
12083 dwo_sections->macro.size = bfd_section_size (sectp);
12084 }
12085 else if (section_is_p (sectp->name, &names->str_dwo))
12086 {
12087 dwo_sections->str.s.section = sectp;
12088 dwo_sections->str.size = bfd_section_size (sectp);
12089 }
12090 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12091 {
12092 dwo_sections->str_offsets.s.section = sectp;
12093 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12094 }
12095 else if (section_is_p (sectp->name, &names->types_dwo))
12096 {
12097 struct dwarf2_section_info type_section;
12098
12099 memset (&type_section, 0, sizeof (type_section));
12100 type_section.s.section = sectp;
12101 type_section.size = bfd_section_size (sectp);
12102 dwo_sections->types.push_back (type_section);
12103 }
12104 }
12105
12106 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12107 by PER_CU. This is for the non-DWP case.
12108 The result is NULL if DWO_NAME can't be found. */
12109
12110 static struct dwo_file *
12111 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12112 const char *dwo_name, const char *comp_dir)
12113 {
12114 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12115
12116 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12117 if (dbfd == NULL)
12118 {
12119 if (dwarf_read_debug)
12120 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12121 return NULL;
12122 }
12123
12124 dwo_file_up dwo_file (new struct dwo_file);
12125 dwo_file->dwo_name = dwo_name;
12126 dwo_file->comp_dir = comp_dir;
12127 dwo_file->dbfd = std::move (dbfd);
12128
12129 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12130 &dwo_file->sections);
12131
12132 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12133 dwo_file->sections.info, dwo_file->cus);
12134
12135 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12136 dwo_file->sections.types, dwo_file->tus);
12137
12138 if (dwarf_read_debug)
12139 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12140
12141 return dwo_file.release ();
12142 }
12143
12144 /* This function is mapped across the sections and remembers the offset and
12145 size of each of the DWP debugging sections common to version 1 and 2 that
12146 we are interested in. */
12147
12148 static void
12149 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12150 void *dwp_file_ptr)
12151 {
12152 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12153 const struct dwop_section_names *names = &dwop_section_names;
12154 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12155
12156 /* Record the ELF section number for later lookup: this is what the
12157 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12158 gdb_assert (elf_section_nr < dwp_file->num_sections);
12159 dwp_file->elf_sections[elf_section_nr] = sectp;
12160
12161 /* Look for specific sections that we need. */
12162 if (section_is_p (sectp->name, &names->str_dwo))
12163 {
12164 dwp_file->sections.str.s.section = sectp;
12165 dwp_file->sections.str.size = bfd_section_size (sectp);
12166 }
12167 else if (section_is_p (sectp->name, &names->cu_index))
12168 {
12169 dwp_file->sections.cu_index.s.section = sectp;
12170 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12171 }
12172 else if (section_is_p (sectp->name, &names->tu_index))
12173 {
12174 dwp_file->sections.tu_index.s.section = sectp;
12175 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12176 }
12177 }
12178
12179 /* This function is mapped across the sections and remembers the offset and
12180 size of each of the DWP version 2 debugging sections that we are interested
12181 in. This is split into a separate function because we don't know if we
12182 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12183
12184 static void
12185 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12186 {
12187 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12188 const struct dwop_section_names *names = &dwop_section_names;
12189 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12190
12191 /* Record the ELF section number for later lookup: this is what the
12192 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12193 gdb_assert (elf_section_nr < dwp_file->num_sections);
12194 dwp_file->elf_sections[elf_section_nr] = sectp;
12195
12196 /* Look for specific sections that we need. */
12197 if (section_is_p (sectp->name, &names->abbrev_dwo))
12198 {
12199 dwp_file->sections.abbrev.s.section = sectp;
12200 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12201 }
12202 else if (section_is_p (sectp->name, &names->info_dwo))
12203 {
12204 dwp_file->sections.info.s.section = sectp;
12205 dwp_file->sections.info.size = bfd_section_size (sectp);
12206 }
12207 else if (section_is_p (sectp->name, &names->line_dwo))
12208 {
12209 dwp_file->sections.line.s.section = sectp;
12210 dwp_file->sections.line.size = bfd_section_size (sectp);
12211 }
12212 else if (section_is_p (sectp->name, &names->loc_dwo))
12213 {
12214 dwp_file->sections.loc.s.section = sectp;
12215 dwp_file->sections.loc.size = bfd_section_size (sectp);
12216 }
12217 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12218 {
12219 dwp_file->sections.macinfo.s.section = sectp;
12220 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12221 }
12222 else if (section_is_p (sectp->name, &names->macro_dwo))
12223 {
12224 dwp_file->sections.macro.s.section = sectp;
12225 dwp_file->sections.macro.size = bfd_section_size (sectp);
12226 }
12227 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12228 {
12229 dwp_file->sections.str_offsets.s.section = sectp;
12230 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12231 }
12232 else if (section_is_p (sectp->name, &names->types_dwo))
12233 {
12234 dwp_file->sections.types.s.section = sectp;
12235 dwp_file->sections.types.size = bfd_section_size (sectp);
12236 }
12237 }
12238
12239 /* Hash function for dwp_file loaded CUs/TUs. */
12240
12241 static hashval_t
12242 hash_dwp_loaded_cutus (const void *item)
12243 {
12244 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12245
12246 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12247 return dwo_unit->signature;
12248 }
12249
12250 /* Equality function for dwp_file loaded CUs/TUs. */
12251
12252 static int
12253 eq_dwp_loaded_cutus (const void *a, const void *b)
12254 {
12255 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12256 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12257
12258 return dua->signature == dub->signature;
12259 }
12260
12261 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12262
12263 static htab_up
12264 allocate_dwp_loaded_cutus_table ()
12265 {
12266 return htab_up (htab_create_alloc (3,
12267 hash_dwp_loaded_cutus,
12268 eq_dwp_loaded_cutus,
12269 NULL, xcalloc, xfree));
12270 }
12271
12272 /* Try to open DWP file FILE_NAME.
12273 The result is the bfd handle of the file.
12274 If there is a problem finding or opening the file, return NULL.
12275 Upon success, the canonicalized path of the file is stored in the bfd,
12276 same as symfile_bfd_open. */
12277
12278 static gdb_bfd_ref_ptr
12279 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12280 const char *file_name)
12281 {
12282 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12283 1 /*is_dwp*/,
12284 1 /*search_cwd*/));
12285 if (abfd != NULL)
12286 return abfd;
12287
12288 /* Work around upstream bug 15652.
12289 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12290 [Whether that's a "bug" is debatable, but it is getting in our way.]
12291 We have no real idea where the dwp file is, because gdb's realpath-ing
12292 of the executable's path may have discarded the needed info.
12293 [IWBN if the dwp file name was recorded in the executable, akin to
12294 .gnu_debuglink, but that doesn't exist yet.]
12295 Strip the directory from FILE_NAME and search again. */
12296 if (*debug_file_directory != '\0')
12297 {
12298 /* Don't implicitly search the current directory here.
12299 If the user wants to search "." to handle this case,
12300 it must be added to debug-file-directory. */
12301 return try_open_dwop_file (dwarf2_per_objfile,
12302 lbasename (file_name), 1 /*is_dwp*/,
12303 0 /*search_cwd*/);
12304 }
12305
12306 return NULL;
12307 }
12308
12309 /* Initialize the use of the DWP file for the current objfile.
12310 By convention the name of the DWP file is ${objfile}.dwp.
12311 The result is NULL if it can't be found. */
12312
12313 static std::unique_ptr<struct dwp_file>
12314 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12315 {
12316 struct objfile *objfile = dwarf2_per_objfile->objfile;
12317
12318 /* Try to find first .dwp for the binary file before any symbolic links
12319 resolving. */
12320
12321 /* If the objfile is a debug file, find the name of the real binary
12322 file and get the name of dwp file from there. */
12323 std::string dwp_name;
12324 if (objfile->separate_debug_objfile_backlink != NULL)
12325 {
12326 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12327 const char *backlink_basename = lbasename (backlink->original_name);
12328
12329 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12330 }
12331 else
12332 dwp_name = objfile->original_name;
12333
12334 dwp_name += ".dwp";
12335
12336 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12337 if (dbfd == NULL
12338 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12339 {
12340 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12341 dwp_name = objfile_name (objfile);
12342 dwp_name += ".dwp";
12343 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12344 }
12345
12346 if (dbfd == NULL)
12347 {
12348 if (dwarf_read_debug)
12349 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12350 return std::unique_ptr<dwp_file> ();
12351 }
12352
12353 const char *name = bfd_get_filename (dbfd.get ());
12354 std::unique_ptr<struct dwp_file> dwp_file
12355 (new struct dwp_file (name, std::move (dbfd)));
12356
12357 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12358 dwp_file->elf_sections =
12359 OBSTACK_CALLOC (&objfile->objfile_obstack,
12360 dwp_file->num_sections, asection *);
12361
12362 bfd_map_over_sections (dwp_file->dbfd.get (),
12363 dwarf2_locate_common_dwp_sections,
12364 dwp_file.get ());
12365
12366 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12367 0);
12368
12369 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12370 1);
12371
12372 /* The DWP file version is stored in the hash table. Oh well. */
12373 if (dwp_file->cus && dwp_file->tus
12374 && dwp_file->cus->version != dwp_file->tus->version)
12375 {
12376 /* Technically speaking, we should try to limp along, but this is
12377 pretty bizarre. We use pulongest here because that's the established
12378 portability solution (e.g, we cannot use %u for uint32_t). */
12379 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12380 " TU version %s [in DWP file %s]"),
12381 pulongest (dwp_file->cus->version),
12382 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12383 }
12384
12385 if (dwp_file->cus)
12386 dwp_file->version = dwp_file->cus->version;
12387 else if (dwp_file->tus)
12388 dwp_file->version = dwp_file->tus->version;
12389 else
12390 dwp_file->version = 2;
12391
12392 if (dwp_file->version == 2)
12393 bfd_map_over_sections (dwp_file->dbfd.get (),
12394 dwarf2_locate_v2_dwp_sections,
12395 dwp_file.get ());
12396
12397 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12398 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12399
12400 if (dwarf_read_debug)
12401 {
12402 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12403 fprintf_unfiltered (gdb_stdlog,
12404 " %s CUs, %s TUs\n",
12405 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12406 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12407 }
12408
12409 return dwp_file;
12410 }
12411
12412 /* Wrapper around open_and_init_dwp_file, only open it once. */
12413
12414 static struct dwp_file *
12415 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12416 {
12417 if (! dwarf2_per_objfile->dwp_checked)
12418 {
12419 dwarf2_per_objfile->dwp_file
12420 = open_and_init_dwp_file (dwarf2_per_objfile);
12421 dwarf2_per_objfile->dwp_checked = 1;
12422 }
12423 return dwarf2_per_objfile->dwp_file.get ();
12424 }
12425
12426 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12427 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12428 or in the DWP file for the objfile, referenced by THIS_UNIT.
12429 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12430 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12431
12432 This is called, for example, when wanting to read a variable with a
12433 complex location. Therefore we don't want to do file i/o for every call.
12434 Therefore we don't want to look for a DWO file on every call.
12435 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12436 then we check if we've already seen DWO_NAME, and only THEN do we check
12437 for a DWO file.
12438
12439 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12440 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12441
12442 static struct dwo_unit *
12443 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12444 const char *dwo_name, const char *comp_dir,
12445 ULONGEST signature, int is_debug_types)
12446 {
12447 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12448 struct objfile *objfile = dwarf2_per_objfile->objfile;
12449 const char *kind = is_debug_types ? "TU" : "CU";
12450 void **dwo_file_slot;
12451 struct dwo_file *dwo_file;
12452 struct dwp_file *dwp_file;
12453
12454 /* First see if there's a DWP file.
12455 If we have a DWP file but didn't find the DWO inside it, don't
12456 look for the original DWO file. It makes gdb behave differently
12457 depending on whether one is debugging in the build tree. */
12458
12459 dwp_file = get_dwp_file (dwarf2_per_objfile);
12460 if (dwp_file != NULL)
12461 {
12462 const struct dwp_hash_table *dwp_htab =
12463 is_debug_types ? dwp_file->tus : dwp_file->cus;
12464
12465 if (dwp_htab != NULL)
12466 {
12467 struct dwo_unit *dwo_cutu =
12468 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12469 signature, is_debug_types);
12470
12471 if (dwo_cutu != NULL)
12472 {
12473 if (dwarf_read_debug)
12474 {
12475 fprintf_unfiltered (gdb_stdlog,
12476 "Virtual DWO %s %s found: @%s\n",
12477 kind, hex_string (signature),
12478 host_address_to_string (dwo_cutu));
12479 }
12480 return dwo_cutu;
12481 }
12482 }
12483 }
12484 else
12485 {
12486 /* No DWP file, look for the DWO file. */
12487
12488 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12489 dwo_name, comp_dir);
12490 if (*dwo_file_slot == NULL)
12491 {
12492 /* Read in the file and build a table of the CUs/TUs it contains. */
12493 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12494 }
12495 /* NOTE: This will be NULL if unable to open the file. */
12496 dwo_file = (struct dwo_file *) *dwo_file_slot;
12497
12498 if (dwo_file != NULL)
12499 {
12500 struct dwo_unit *dwo_cutu = NULL;
12501
12502 if (is_debug_types && dwo_file->tus)
12503 {
12504 struct dwo_unit find_dwo_cutu;
12505
12506 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12507 find_dwo_cutu.signature = signature;
12508 dwo_cutu
12509 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12510 &find_dwo_cutu);
12511 }
12512 else if (!is_debug_types && dwo_file->cus)
12513 {
12514 struct dwo_unit find_dwo_cutu;
12515
12516 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12517 find_dwo_cutu.signature = signature;
12518 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12519 &find_dwo_cutu);
12520 }
12521
12522 if (dwo_cutu != NULL)
12523 {
12524 if (dwarf_read_debug)
12525 {
12526 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12527 kind, dwo_name, hex_string (signature),
12528 host_address_to_string (dwo_cutu));
12529 }
12530 return dwo_cutu;
12531 }
12532 }
12533 }
12534
12535 /* We didn't find it. This could mean a dwo_id mismatch, or
12536 someone deleted the DWO/DWP file, or the search path isn't set up
12537 correctly to find the file. */
12538
12539 if (dwarf_read_debug)
12540 {
12541 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12542 kind, dwo_name, hex_string (signature));
12543 }
12544
12545 /* This is a warning and not a complaint because it can be caused by
12546 pilot error (e.g., user accidentally deleting the DWO). */
12547 {
12548 /* Print the name of the DWP file if we looked there, helps the user
12549 better diagnose the problem. */
12550 std::string dwp_text;
12551
12552 if (dwp_file != NULL)
12553 dwp_text = string_printf (" [in DWP file %s]",
12554 lbasename (dwp_file->name));
12555
12556 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12557 " [in module %s]"),
12558 kind, dwo_name, hex_string (signature),
12559 dwp_text.c_str (),
12560 this_unit->is_debug_types ? "TU" : "CU",
12561 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12562 }
12563 return NULL;
12564 }
12565
12566 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12567 See lookup_dwo_cutu_unit for details. */
12568
12569 static struct dwo_unit *
12570 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12571 const char *dwo_name, const char *comp_dir,
12572 ULONGEST signature)
12573 {
12574 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12575 }
12576
12577 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12578 See lookup_dwo_cutu_unit for details. */
12579
12580 static struct dwo_unit *
12581 lookup_dwo_type_unit (struct signatured_type *this_tu,
12582 const char *dwo_name, const char *comp_dir)
12583 {
12584 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12585 }
12586
12587 /* Traversal function for queue_and_load_all_dwo_tus. */
12588
12589 static int
12590 queue_and_load_dwo_tu (void **slot, void *info)
12591 {
12592 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12593 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12594 ULONGEST signature = dwo_unit->signature;
12595 struct signatured_type *sig_type =
12596 lookup_dwo_signatured_type (per_cu->cu, signature);
12597
12598 if (sig_type != NULL)
12599 {
12600 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12601
12602 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12603 a real dependency of PER_CU on SIG_TYPE. That is detected later
12604 while processing PER_CU. */
12605 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12606 load_full_type_unit (sig_cu);
12607 per_cu->imported_symtabs_push (sig_cu);
12608 }
12609
12610 return 1;
12611 }
12612
12613 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12614 The DWO may have the only definition of the type, though it may not be
12615 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12616 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12617
12618 static void
12619 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12620 {
12621 struct dwo_unit *dwo_unit;
12622 struct dwo_file *dwo_file;
12623
12624 gdb_assert (!per_cu->is_debug_types);
12625 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12626 gdb_assert (per_cu->cu != NULL);
12627
12628 dwo_unit = per_cu->cu->dwo_unit;
12629 gdb_assert (dwo_unit != NULL);
12630
12631 dwo_file = dwo_unit->dwo_file;
12632 if (dwo_file->tus != NULL)
12633 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12634 per_cu);
12635 }
12636
12637 /* Read in various DIEs. */
12638
12639 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12640 Inherit only the children of the DW_AT_abstract_origin DIE not being
12641 already referenced by DW_AT_abstract_origin from the children of the
12642 current DIE. */
12643
12644 static void
12645 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12646 {
12647 struct die_info *child_die;
12648 sect_offset *offsetp;
12649 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12650 struct die_info *origin_die;
12651 /* Iterator of the ORIGIN_DIE children. */
12652 struct die_info *origin_child_die;
12653 struct attribute *attr;
12654 struct dwarf2_cu *origin_cu;
12655 struct pending **origin_previous_list_in_scope;
12656
12657 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12658 if (!attr)
12659 return;
12660
12661 /* Note that following die references may follow to a die in a
12662 different cu. */
12663
12664 origin_cu = cu;
12665 origin_die = follow_die_ref (die, attr, &origin_cu);
12666
12667 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12668 symbols in. */
12669 origin_previous_list_in_scope = origin_cu->list_in_scope;
12670 origin_cu->list_in_scope = cu->list_in_scope;
12671
12672 if (die->tag != origin_die->tag
12673 && !(die->tag == DW_TAG_inlined_subroutine
12674 && origin_die->tag == DW_TAG_subprogram))
12675 complaint (_("DIE %s and its abstract origin %s have different tags"),
12676 sect_offset_str (die->sect_off),
12677 sect_offset_str (origin_die->sect_off));
12678
12679 std::vector<sect_offset> offsets;
12680
12681 for (child_die = die->child;
12682 child_die && child_die->tag;
12683 child_die = sibling_die (child_die))
12684 {
12685 struct die_info *child_origin_die;
12686 struct dwarf2_cu *child_origin_cu;
12687
12688 /* We are trying to process concrete instance entries:
12689 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12690 it's not relevant to our analysis here. i.e. detecting DIEs that are
12691 present in the abstract instance but not referenced in the concrete
12692 one. */
12693 if (child_die->tag == DW_TAG_call_site
12694 || child_die->tag == DW_TAG_GNU_call_site)
12695 continue;
12696
12697 /* For each CHILD_DIE, find the corresponding child of
12698 ORIGIN_DIE. If there is more than one layer of
12699 DW_AT_abstract_origin, follow them all; there shouldn't be,
12700 but GCC versions at least through 4.4 generate this (GCC PR
12701 40573). */
12702 child_origin_die = child_die;
12703 child_origin_cu = cu;
12704 while (1)
12705 {
12706 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12707 child_origin_cu);
12708 if (attr == NULL)
12709 break;
12710 child_origin_die = follow_die_ref (child_origin_die, attr,
12711 &child_origin_cu);
12712 }
12713
12714 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12715 counterpart may exist. */
12716 if (child_origin_die != child_die)
12717 {
12718 if (child_die->tag != child_origin_die->tag
12719 && !(child_die->tag == DW_TAG_inlined_subroutine
12720 && child_origin_die->tag == DW_TAG_subprogram))
12721 complaint (_("Child DIE %s and its abstract origin %s have "
12722 "different tags"),
12723 sect_offset_str (child_die->sect_off),
12724 sect_offset_str (child_origin_die->sect_off));
12725 if (child_origin_die->parent != origin_die)
12726 complaint (_("Child DIE %s and its abstract origin %s have "
12727 "different parents"),
12728 sect_offset_str (child_die->sect_off),
12729 sect_offset_str (child_origin_die->sect_off));
12730 else
12731 offsets.push_back (child_origin_die->sect_off);
12732 }
12733 }
12734 std::sort (offsets.begin (), offsets.end ());
12735 sect_offset *offsets_end = offsets.data () + offsets.size ();
12736 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12737 if (offsetp[-1] == *offsetp)
12738 complaint (_("Multiple children of DIE %s refer "
12739 "to DIE %s as their abstract origin"),
12740 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12741
12742 offsetp = offsets.data ();
12743 origin_child_die = origin_die->child;
12744 while (origin_child_die && origin_child_die->tag)
12745 {
12746 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12747 while (offsetp < offsets_end
12748 && *offsetp < origin_child_die->sect_off)
12749 offsetp++;
12750 if (offsetp >= offsets_end
12751 || *offsetp > origin_child_die->sect_off)
12752 {
12753 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12754 Check whether we're already processing ORIGIN_CHILD_DIE.
12755 This can happen with mutually referenced abstract_origins.
12756 PR 16581. */
12757 if (!origin_child_die->in_process)
12758 process_die (origin_child_die, origin_cu);
12759 }
12760 origin_child_die = sibling_die (origin_child_die);
12761 }
12762 origin_cu->list_in_scope = origin_previous_list_in_scope;
12763
12764 if (cu != origin_cu)
12765 compute_delayed_physnames (origin_cu);
12766 }
12767
12768 static void
12769 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12770 {
12771 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12772 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12773 struct context_stack *newobj;
12774 CORE_ADDR lowpc;
12775 CORE_ADDR highpc;
12776 struct die_info *child_die;
12777 struct attribute *attr, *call_line, *call_file;
12778 const char *name;
12779 CORE_ADDR baseaddr;
12780 struct block *block;
12781 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12782 std::vector<struct symbol *> template_args;
12783 struct template_symbol *templ_func = NULL;
12784
12785 if (inlined_func)
12786 {
12787 /* If we do not have call site information, we can't show the
12788 caller of this inlined function. That's too confusing, so
12789 only use the scope for local variables. */
12790 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12791 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12792 if (call_line == NULL || call_file == NULL)
12793 {
12794 read_lexical_block_scope (die, cu);
12795 return;
12796 }
12797 }
12798
12799 baseaddr = objfile->text_section_offset ();
12800
12801 name = dwarf2_name (die, cu);
12802
12803 /* Ignore functions with missing or empty names. These are actually
12804 illegal according to the DWARF standard. */
12805 if (name == NULL)
12806 {
12807 complaint (_("missing name for subprogram DIE at %s"),
12808 sect_offset_str (die->sect_off));
12809 return;
12810 }
12811
12812 /* Ignore functions with missing or invalid low and high pc attributes. */
12813 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12814 <= PC_BOUNDS_INVALID)
12815 {
12816 attr = dwarf2_attr (die, DW_AT_external, cu);
12817 if (!attr || !DW_UNSND (attr))
12818 complaint (_("cannot get low and high bounds "
12819 "for subprogram DIE at %s"),
12820 sect_offset_str (die->sect_off));
12821 return;
12822 }
12823
12824 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12825 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12826
12827 /* If we have any template arguments, then we must allocate a
12828 different sort of symbol. */
12829 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
12830 {
12831 if (child_die->tag == DW_TAG_template_type_param
12832 || child_die->tag == DW_TAG_template_value_param)
12833 {
12834 templ_func = allocate_template_symbol (objfile);
12835 templ_func->subclass = SYMBOL_TEMPLATE;
12836 break;
12837 }
12838 }
12839
12840 newobj = cu->get_builder ()->push_context (0, lowpc);
12841 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12842 (struct symbol *) templ_func);
12843
12844 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12845 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12846 cu->language);
12847
12848 /* If there is a location expression for DW_AT_frame_base, record
12849 it. */
12850 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12851 if (attr != nullptr)
12852 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12853
12854 /* If there is a location for the static link, record it. */
12855 newobj->static_link = NULL;
12856 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12857 if (attr != nullptr)
12858 {
12859 newobj->static_link
12860 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12861 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12862 cu->per_cu->addr_type ());
12863 }
12864
12865 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12866
12867 if (die->child != NULL)
12868 {
12869 child_die = die->child;
12870 while (child_die && child_die->tag)
12871 {
12872 if (child_die->tag == DW_TAG_template_type_param
12873 || child_die->tag == DW_TAG_template_value_param)
12874 {
12875 struct symbol *arg = new_symbol (child_die, NULL, cu);
12876
12877 if (arg != NULL)
12878 template_args.push_back (arg);
12879 }
12880 else
12881 process_die (child_die, cu);
12882 child_die = sibling_die (child_die);
12883 }
12884 }
12885
12886 inherit_abstract_dies (die, cu);
12887
12888 /* If we have a DW_AT_specification, we might need to import using
12889 directives from the context of the specification DIE. See the
12890 comment in determine_prefix. */
12891 if (cu->language == language_cplus
12892 && dwarf2_attr (die, DW_AT_specification, cu))
12893 {
12894 struct dwarf2_cu *spec_cu = cu;
12895 struct die_info *spec_die = die_specification (die, &spec_cu);
12896
12897 while (spec_die)
12898 {
12899 child_die = spec_die->child;
12900 while (child_die && child_die->tag)
12901 {
12902 if (child_die->tag == DW_TAG_imported_module)
12903 process_die (child_die, spec_cu);
12904 child_die = sibling_die (child_die);
12905 }
12906
12907 /* In some cases, GCC generates specification DIEs that
12908 themselves contain DW_AT_specification attributes. */
12909 spec_die = die_specification (spec_die, &spec_cu);
12910 }
12911 }
12912
12913 struct context_stack cstk = cu->get_builder ()->pop_context ();
12914 /* Make a block for the local symbols within. */
12915 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12916 cstk.static_link, lowpc, highpc);
12917
12918 /* For C++, set the block's scope. */
12919 if ((cu->language == language_cplus
12920 || cu->language == language_fortran
12921 || cu->language == language_d
12922 || cu->language == language_rust)
12923 && cu->processing_has_namespace_info)
12924 block_set_scope (block, determine_prefix (die, cu),
12925 &objfile->objfile_obstack);
12926
12927 /* If we have address ranges, record them. */
12928 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12929
12930 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12931
12932 /* Attach template arguments to function. */
12933 if (!template_args.empty ())
12934 {
12935 gdb_assert (templ_func != NULL);
12936
12937 templ_func->n_template_arguments = template_args.size ();
12938 templ_func->template_arguments
12939 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12940 templ_func->n_template_arguments);
12941 memcpy (templ_func->template_arguments,
12942 template_args.data (),
12943 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12944
12945 /* Make sure that the symtab is set on the new symbols. Even
12946 though they don't appear in this symtab directly, other parts
12947 of gdb assume that symbols do, and this is reasonably
12948 true. */
12949 for (symbol *sym : template_args)
12950 symbol_set_symtab (sym, symbol_symtab (templ_func));
12951 }
12952
12953 /* In C++, we can have functions nested inside functions (e.g., when
12954 a function declares a class that has methods). This means that
12955 when we finish processing a function scope, we may need to go
12956 back to building a containing block's symbol lists. */
12957 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12958 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12959
12960 /* If we've finished processing a top-level function, subsequent
12961 symbols go in the file symbol list. */
12962 if (cu->get_builder ()->outermost_context_p ())
12963 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12964 }
12965
12966 /* Process all the DIES contained within a lexical block scope. Start
12967 a new scope, process the dies, and then close the scope. */
12968
12969 static void
12970 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12971 {
12972 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12973 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12974 CORE_ADDR lowpc, highpc;
12975 struct die_info *child_die;
12976 CORE_ADDR baseaddr;
12977
12978 baseaddr = objfile->text_section_offset ();
12979
12980 /* Ignore blocks with missing or invalid low and high pc attributes. */
12981 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12982 as multiple lexical blocks? Handling children in a sane way would
12983 be nasty. Might be easier to properly extend generic blocks to
12984 describe ranges. */
12985 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12986 {
12987 case PC_BOUNDS_NOT_PRESENT:
12988 /* DW_TAG_lexical_block has no attributes, process its children as if
12989 there was no wrapping by that DW_TAG_lexical_block.
12990 GCC does no longer produces such DWARF since GCC r224161. */
12991 for (child_die = die->child;
12992 child_die != NULL && child_die->tag;
12993 child_die = sibling_die (child_die))
12994 process_die (child_die, cu);
12995 return;
12996 case PC_BOUNDS_INVALID:
12997 return;
12998 }
12999 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13000 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13001
13002 cu->get_builder ()->push_context (0, lowpc);
13003 if (die->child != NULL)
13004 {
13005 child_die = die->child;
13006 while (child_die && child_die->tag)
13007 {
13008 process_die (child_die, cu);
13009 child_die = sibling_die (child_die);
13010 }
13011 }
13012 inherit_abstract_dies (die, cu);
13013 struct context_stack cstk = cu->get_builder ()->pop_context ();
13014
13015 if (*cu->get_builder ()->get_local_symbols () != NULL
13016 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13017 {
13018 struct block *block
13019 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13020 cstk.start_addr, highpc);
13021
13022 /* Note that recording ranges after traversing children, as we
13023 do here, means that recording a parent's ranges entails
13024 walking across all its children's ranges as they appear in
13025 the address map, which is quadratic behavior.
13026
13027 It would be nicer to record the parent's ranges before
13028 traversing its children, simply overriding whatever you find
13029 there. But since we don't even decide whether to create a
13030 block until after we've traversed its children, that's hard
13031 to do. */
13032 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13033 }
13034 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13035 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13036 }
13037
13038 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13039
13040 static void
13041 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13042 {
13043 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13044 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13045 CORE_ADDR pc, baseaddr;
13046 struct attribute *attr;
13047 struct call_site *call_site, call_site_local;
13048 void **slot;
13049 int nparams;
13050 struct die_info *child_die;
13051
13052 baseaddr = objfile->text_section_offset ();
13053
13054 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13055 if (attr == NULL)
13056 {
13057 /* This was a pre-DWARF-5 GNU extension alias
13058 for DW_AT_call_return_pc. */
13059 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13060 }
13061 if (!attr)
13062 {
13063 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13064 "DIE %s [in module %s]"),
13065 sect_offset_str (die->sect_off), objfile_name (objfile));
13066 return;
13067 }
13068 pc = attr->value_as_address () + baseaddr;
13069 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13070
13071 if (cu->call_site_htab == NULL)
13072 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13073 NULL, &objfile->objfile_obstack,
13074 hashtab_obstack_allocate, NULL);
13075 call_site_local.pc = pc;
13076 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13077 if (*slot != NULL)
13078 {
13079 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13080 "DIE %s [in module %s]"),
13081 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13082 objfile_name (objfile));
13083 return;
13084 }
13085
13086 /* Count parameters at the caller. */
13087
13088 nparams = 0;
13089 for (child_die = die->child; child_die && child_die->tag;
13090 child_die = sibling_die (child_die))
13091 {
13092 if (child_die->tag != DW_TAG_call_site_parameter
13093 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13094 {
13095 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13096 "DW_TAG_call_site child DIE %s [in module %s]"),
13097 child_die->tag, sect_offset_str (child_die->sect_off),
13098 objfile_name (objfile));
13099 continue;
13100 }
13101
13102 nparams++;
13103 }
13104
13105 call_site
13106 = ((struct call_site *)
13107 obstack_alloc (&objfile->objfile_obstack,
13108 sizeof (*call_site)
13109 + (sizeof (*call_site->parameter) * (nparams - 1))));
13110 *slot = call_site;
13111 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13112 call_site->pc = pc;
13113
13114 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13115 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13116 {
13117 struct die_info *func_die;
13118
13119 /* Skip also over DW_TAG_inlined_subroutine. */
13120 for (func_die = die->parent;
13121 func_die && func_die->tag != DW_TAG_subprogram
13122 && func_die->tag != DW_TAG_subroutine_type;
13123 func_die = func_die->parent);
13124
13125 /* DW_AT_call_all_calls is a superset
13126 of DW_AT_call_all_tail_calls. */
13127 if (func_die
13128 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13129 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13130 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13131 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13132 {
13133 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13134 not complete. But keep CALL_SITE for look ups via call_site_htab,
13135 both the initial caller containing the real return address PC and
13136 the final callee containing the current PC of a chain of tail
13137 calls do not need to have the tail call list complete. But any
13138 function candidate for a virtual tail call frame searched via
13139 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13140 determined unambiguously. */
13141 }
13142 else
13143 {
13144 struct type *func_type = NULL;
13145
13146 if (func_die)
13147 func_type = get_die_type (func_die, cu);
13148 if (func_type != NULL)
13149 {
13150 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13151
13152 /* Enlist this call site to the function. */
13153 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13154 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13155 }
13156 else
13157 complaint (_("Cannot find function owning DW_TAG_call_site "
13158 "DIE %s [in module %s]"),
13159 sect_offset_str (die->sect_off), objfile_name (objfile));
13160 }
13161 }
13162
13163 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13164 if (attr == NULL)
13165 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13166 if (attr == NULL)
13167 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13168 if (attr == NULL)
13169 {
13170 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13171 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13172 }
13173 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13174 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13175 /* Keep NULL DWARF_BLOCK. */;
13176 else if (attr->form_is_block ())
13177 {
13178 struct dwarf2_locexpr_baton *dlbaton;
13179
13180 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13181 dlbaton->data = DW_BLOCK (attr)->data;
13182 dlbaton->size = DW_BLOCK (attr)->size;
13183 dlbaton->per_cu = cu->per_cu;
13184
13185 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13186 }
13187 else if (attr->form_is_ref ())
13188 {
13189 struct dwarf2_cu *target_cu = cu;
13190 struct die_info *target_die;
13191
13192 target_die = follow_die_ref (die, attr, &target_cu);
13193 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13194 if (die_is_declaration (target_die, target_cu))
13195 {
13196 const char *target_physname;
13197
13198 /* Prefer the mangled name; otherwise compute the demangled one. */
13199 target_physname = dw2_linkage_name (target_die, target_cu);
13200 if (target_physname == NULL)
13201 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13202 if (target_physname == NULL)
13203 complaint (_("DW_AT_call_target target DIE has invalid "
13204 "physname, for referencing DIE %s [in module %s]"),
13205 sect_offset_str (die->sect_off), objfile_name (objfile));
13206 else
13207 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13208 }
13209 else
13210 {
13211 CORE_ADDR lowpc;
13212
13213 /* DW_AT_entry_pc should be preferred. */
13214 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13215 <= PC_BOUNDS_INVALID)
13216 complaint (_("DW_AT_call_target target DIE has invalid "
13217 "low pc, for referencing DIE %s [in module %s]"),
13218 sect_offset_str (die->sect_off), objfile_name (objfile));
13219 else
13220 {
13221 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13222 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13223 }
13224 }
13225 }
13226 else
13227 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13228 "block nor reference, for DIE %s [in module %s]"),
13229 sect_offset_str (die->sect_off), objfile_name (objfile));
13230
13231 call_site->per_cu = cu->per_cu;
13232
13233 for (child_die = die->child;
13234 child_die && child_die->tag;
13235 child_die = sibling_die (child_die))
13236 {
13237 struct call_site_parameter *parameter;
13238 struct attribute *loc, *origin;
13239
13240 if (child_die->tag != DW_TAG_call_site_parameter
13241 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13242 {
13243 /* Already printed the complaint above. */
13244 continue;
13245 }
13246
13247 gdb_assert (call_site->parameter_count < nparams);
13248 parameter = &call_site->parameter[call_site->parameter_count];
13249
13250 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13251 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13252 register is contained in DW_AT_call_value. */
13253
13254 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13255 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13256 if (origin == NULL)
13257 {
13258 /* This was a pre-DWARF-5 GNU extension alias
13259 for DW_AT_call_parameter. */
13260 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13261 }
13262 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13263 {
13264 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13265
13266 sect_offset sect_off
13267 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13268 if (!cu->header.offset_in_cu_p (sect_off))
13269 {
13270 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13271 binding can be done only inside one CU. Such referenced DIE
13272 therefore cannot be even moved to DW_TAG_partial_unit. */
13273 complaint (_("DW_AT_call_parameter offset is not in CU for "
13274 "DW_TAG_call_site child DIE %s [in module %s]"),
13275 sect_offset_str (child_die->sect_off),
13276 objfile_name (objfile));
13277 continue;
13278 }
13279 parameter->u.param_cu_off
13280 = (cu_offset) (sect_off - cu->header.sect_off);
13281 }
13282 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13283 {
13284 complaint (_("No DW_FORM_block* DW_AT_location for "
13285 "DW_TAG_call_site child DIE %s [in module %s]"),
13286 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13287 continue;
13288 }
13289 else
13290 {
13291 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13292 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13293 if (parameter->u.dwarf_reg != -1)
13294 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13295 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13296 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13297 &parameter->u.fb_offset))
13298 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13299 else
13300 {
13301 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13302 "for DW_FORM_block* DW_AT_location is supported for "
13303 "DW_TAG_call_site child DIE %s "
13304 "[in module %s]"),
13305 sect_offset_str (child_die->sect_off),
13306 objfile_name (objfile));
13307 continue;
13308 }
13309 }
13310
13311 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13312 if (attr == NULL)
13313 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13314 if (attr == NULL || !attr->form_is_block ())
13315 {
13316 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13317 "DW_TAG_call_site child DIE %s [in module %s]"),
13318 sect_offset_str (child_die->sect_off),
13319 objfile_name (objfile));
13320 continue;
13321 }
13322 parameter->value = DW_BLOCK (attr)->data;
13323 parameter->value_size = DW_BLOCK (attr)->size;
13324
13325 /* Parameters are not pre-cleared by memset above. */
13326 parameter->data_value = NULL;
13327 parameter->data_value_size = 0;
13328 call_site->parameter_count++;
13329
13330 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13331 if (attr == NULL)
13332 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13333 if (attr != nullptr)
13334 {
13335 if (!attr->form_is_block ())
13336 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13337 "DW_TAG_call_site child DIE %s [in module %s]"),
13338 sect_offset_str (child_die->sect_off),
13339 objfile_name (objfile));
13340 else
13341 {
13342 parameter->data_value = DW_BLOCK (attr)->data;
13343 parameter->data_value_size = DW_BLOCK (attr)->size;
13344 }
13345 }
13346 }
13347 }
13348
13349 /* Helper function for read_variable. If DIE represents a virtual
13350 table, then return the type of the concrete object that is
13351 associated with the virtual table. Otherwise, return NULL. */
13352
13353 static struct type *
13354 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13355 {
13356 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13357 if (attr == NULL)
13358 return NULL;
13359
13360 /* Find the type DIE. */
13361 struct die_info *type_die = NULL;
13362 struct dwarf2_cu *type_cu = cu;
13363
13364 if (attr->form_is_ref ())
13365 type_die = follow_die_ref (die, attr, &type_cu);
13366 if (type_die == NULL)
13367 return NULL;
13368
13369 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13370 return NULL;
13371 return die_containing_type (type_die, type_cu);
13372 }
13373
13374 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13375
13376 static void
13377 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13378 {
13379 struct rust_vtable_symbol *storage = NULL;
13380
13381 if (cu->language == language_rust)
13382 {
13383 struct type *containing_type = rust_containing_type (die, cu);
13384
13385 if (containing_type != NULL)
13386 {
13387 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13388
13389 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13390 initialize_objfile_symbol (storage);
13391 storage->concrete_type = containing_type;
13392 storage->subclass = SYMBOL_RUST_VTABLE;
13393 }
13394 }
13395
13396 struct symbol *res = new_symbol (die, NULL, cu, storage);
13397 struct attribute *abstract_origin
13398 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13399 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13400 if (res == NULL && loc && abstract_origin)
13401 {
13402 /* We have a variable without a name, but with a location and an abstract
13403 origin. This may be a concrete instance of an abstract variable
13404 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13405 later. */
13406 struct dwarf2_cu *origin_cu = cu;
13407 struct die_info *origin_die
13408 = follow_die_ref (die, abstract_origin, &origin_cu);
13409 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13410 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13411 }
13412 }
13413
13414 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13415 reading .debug_rnglists.
13416 Callback's type should be:
13417 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13418 Return true if the attributes are present and valid, otherwise,
13419 return false. */
13420
13421 template <typename Callback>
13422 static bool
13423 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13424 Callback &&callback)
13425 {
13426 struct dwarf2_per_objfile *dwarf2_per_objfile
13427 = cu->per_cu->dwarf2_per_objfile;
13428 struct objfile *objfile = dwarf2_per_objfile->objfile;
13429 bfd *obfd = objfile->obfd;
13430 /* Base address selection entry. */
13431 gdb::optional<CORE_ADDR> base;
13432 const gdb_byte *buffer;
13433 CORE_ADDR baseaddr;
13434 bool overflow = false;
13435
13436 base = cu->base_address;
13437
13438 dwarf2_per_objfile->rnglists.read (objfile);
13439 if (offset >= dwarf2_per_objfile->rnglists.size)
13440 {
13441 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13442 offset);
13443 return false;
13444 }
13445 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13446
13447 baseaddr = objfile->text_section_offset ();
13448
13449 while (1)
13450 {
13451 /* Initialize it due to a false compiler warning. */
13452 CORE_ADDR range_beginning = 0, range_end = 0;
13453 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13454 + dwarf2_per_objfile->rnglists.size);
13455 unsigned int bytes_read;
13456
13457 if (buffer == buf_end)
13458 {
13459 overflow = true;
13460 break;
13461 }
13462 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13463 switch (rlet)
13464 {
13465 case DW_RLE_end_of_list:
13466 break;
13467 case DW_RLE_base_address:
13468 if (buffer + cu->header.addr_size > buf_end)
13469 {
13470 overflow = true;
13471 break;
13472 }
13473 base = cu->header.read_address (obfd, buffer, &bytes_read);
13474 buffer += bytes_read;
13475 break;
13476 case DW_RLE_start_length:
13477 if (buffer + cu->header.addr_size > buf_end)
13478 {
13479 overflow = true;
13480 break;
13481 }
13482 range_beginning = cu->header.read_address (obfd, buffer,
13483 &bytes_read);
13484 buffer += bytes_read;
13485 range_end = (range_beginning
13486 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13487 buffer += bytes_read;
13488 if (buffer > buf_end)
13489 {
13490 overflow = true;
13491 break;
13492 }
13493 break;
13494 case DW_RLE_offset_pair:
13495 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13496 buffer += bytes_read;
13497 if (buffer > buf_end)
13498 {
13499 overflow = true;
13500 break;
13501 }
13502 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13503 buffer += bytes_read;
13504 if (buffer > buf_end)
13505 {
13506 overflow = true;
13507 break;
13508 }
13509 break;
13510 case DW_RLE_start_end:
13511 if (buffer + 2 * 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 = cu->header.read_address (obfd, buffer, &bytes_read);
13520 buffer += bytes_read;
13521 break;
13522 default:
13523 complaint (_("Invalid .debug_rnglists data (no base address)"));
13524 return false;
13525 }
13526 if (rlet == DW_RLE_end_of_list || overflow)
13527 break;
13528 if (rlet == DW_RLE_base_address)
13529 continue;
13530
13531 if (!base.has_value ())
13532 {
13533 /* We have no valid base address for the ranges
13534 data. */
13535 complaint (_("Invalid .debug_rnglists data (no base address)"));
13536 return false;
13537 }
13538
13539 if (range_beginning > range_end)
13540 {
13541 /* Inverted range entries are invalid. */
13542 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13543 return false;
13544 }
13545
13546 /* Empty range entries have no effect. */
13547 if (range_beginning == range_end)
13548 continue;
13549
13550 range_beginning += *base;
13551 range_end += *base;
13552
13553 /* A not-uncommon case of bad debug info.
13554 Don't pollute the addrmap with bad data. */
13555 if (range_beginning + baseaddr == 0
13556 && !dwarf2_per_objfile->has_section_at_zero)
13557 {
13558 complaint (_(".debug_rnglists entry has start address of zero"
13559 " [in module %s]"), objfile_name (objfile));
13560 continue;
13561 }
13562
13563 callback (range_beginning, range_end);
13564 }
13565
13566 if (overflow)
13567 {
13568 complaint (_("Offset %d is not terminated "
13569 "for DW_AT_ranges attribute"),
13570 offset);
13571 return false;
13572 }
13573
13574 return true;
13575 }
13576
13577 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13578 Callback's type should be:
13579 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13580 Return 1 if the attributes are present and valid, otherwise, return 0. */
13581
13582 template <typename Callback>
13583 static int
13584 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13585 Callback &&callback)
13586 {
13587 struct dwarf2_per_objfile *dwarf2_per_objfile
13588 = cu->per_cu->dwarf2_per_objfile;
13589 struct objfile *objfile = dwarf2_per_objfile->objfile;
13590 struct comp_unit_head *cu_header = &cu->header;
13591 bfd *obfd = objfile->obfd;
13592 unsigned int addr_size = cu_header->addr_size;
13593 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13594 /* Base address selection entry. */
13595 gdb::optional<CORE_ADDR> base;
13596 unsigned int dummy;
13597 const gdb_byte *buffer;
13598 CORE_ADDR baseaddr;
13599
13600 if (cu_header->version >= 5)
13601 return dwarf2_rnglists_process (offset, cu, callback);
13602
13603 base = cu->base_address;
13604
13605 dwarf2_per_objfile->ranges.read (objfile);
13606 if (offset >= dwarf2_per_objfile->ranges.size)
13607 {
13608 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13609 offset);
13610 return 0;
13611 }
13612 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13613
13614 baseaddr = objfile->text_section_offset ();
13615
13616 while (1)
13617 {
13618 CORE_ADDR range_beginning, range_end;
13619
13620 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13621 buffer += addr_size;
13622 range_end = cu->header.read_address (obfd, buffer, &dummy);
13623 buffer += addr_size;
13624 offset += 2 * addr_size;
13625
13626 /* An end of list marker is a pair of zero addresses. */
13627 if (range_beginning == 0 && range_end == 0)
13628 /* Found the end of list entry. */
13629 break;
13630
13631 /* Each base address selection entry is a pair of 2 values.
13632 The first is the largest possible address, the second is
13633 the base address. Check for a base address here. */
13634 if ((range_beginning & mask) == mask)
13635 {
13636 /* If we found the largest possible address, then we already
13637 have the base address in range_end. */
13638 base = range_end;
13639 continue;
13640 }
13641
13642 if (!base.has_value ())
13643 {
13644 /* We have no valid base address for the ranges
13645 data. */
13646 complaint (_("Invalid .debug_ranges data (no base address)"));
13647 return 0;
13648 }
13649
13650 if (range_beginning > range_end)
13651 {
13652 /* Inverted range entries are invalid. */
13653 complaint (_("Invalid .debug_ranges data (inverted range)"));
13654 return 0;
13655 }
13656
13657 /* Empty range entries have no effect. */
13658 if (range_beginning == range_end)
13659 continue;
13660
13661 range_beginning += *base;
13662 range_end += *base;
13663
13664 /* A not-uncommon case of bad debug info.
13665 Don't pollute the addrmap with bad data. */
13666 if (range_beginning + baseaddr == 0
13667 && !dwarf2_per_objfile->has_section_at_zero)
13668 {
13669 complaint (_(".debug_ranges entry has start address of zero"
13670 " [in module %s]"), objfile_name (objfile));
13671 continue;
13672 }
13673
13674 callback (range_beginning, range_end);
13675 }
13676
13677 return 1;
13678 }
13679
13680 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13681 Return 1 if the attributes are present and valid, otherwise, return 0.
13682 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13683
13684 static int
13685 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13686 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13687 dwarf2_psymtab *ranges_pst)
13688 {
13689 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13690 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13691 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13692 int low_set = 0;
13693 CORE_ADDR low = 0;
13694 CORE_ADDR high = 0;
13695 int retval;
13696
13697 retval = dwarf2_ranges_process (offset, cu,
13698 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13699 {
13700 if (ranges_pst != NULL)
13701 {
13702 CORE_ADDR lowpc;
13703 CORE_ADDR highpc;
13704
13705 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13706 range_beginning + baseaddr)
13707 - baseaddr);
13708 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13709 range_end + baseaddr)
13710 - baseaddr);
13711 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13712 lowpc, highpc - 1, ranges_pst);
13713 }
13714
13715 /* FIXME: This is recording everything as a low-high
13716 segment of consecutive addresses. We should have a
13717 data structure for discontiguous block ranges
13718 instead. */
13719 if (! low_set)
13720 {
13721 low = range_beginning;
13722 high = range_end;
13723 low_set = 1;
13724 }
13725 else
13726 {
13727 if (range_beginning < low)
13728 low = range_beginning;
13729 if (range_end > high)
13730 high = range_end;
13731 }
13732 });
13733 if (!retval)
13734 return 0;
13735
13736 if (! low_set)
13737 /* If the first entry is an end-of-list marker, the range
13738 describes an empty scope, i.e. no instructions. */
13739 return 0;
13740
13741 if (low_return)
13742 *low_return = low;
13743 if (high_return)
13744 *high_return = high;
13745 return 1;
13746 }
13747
13748 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13749 definition for the return value. *LOWPC and *HIGHPC are set iff
13750 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13751
13752 static enum pc_bounds_kind
13753 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13754 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13755 dwarf2_psymtab *pst)
13756 {
13757 struct dwarf2_per_objfile *dwarf2_per_objfile
13758 = cu->per_cu->dwarf2_per_objfile;
13759 struct attribute *attr;
13760 struct attribute *attr_high;
13761 CORE_ADDR low = 0;
13762 CORE_ADDR high = 0;
13763 enum pc_bounds_kind ret;
13764
13765 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13766 if (attr_high)
13767 {
13768 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13769 if (attr != nullptr)
13770 {
13771 low = attr->value_as_address ();
13772 high = attr_high->value_as_address ();
13773 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13774 high += low;
13775 }
13776 else
13777 /* Found high w/o low attribute. */
13778 return PC_BOUNDS_INVALID;
13779
13780 /* Found consecutive range of addresses. */
13781 ret = PC_BOUNDS_HIGH_LOW;
13782 }
13783 else
13784 {
13785 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13786 if (attr != NULL)
13787 {
13788 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13789 We take advantage of the fact that DW_AT_ranges does not appear
13790 in DW_TAG_compile_unit of DWO files. */
13791 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13792 unsigned int ranges_offset = (DW_UNSND (attr)
13793 + (need_ranges_base
13794 ? cu->ranges_base
13795 : 0));
13796
13797 /* Value of the DW_AT_ranges attribute is the offset in the
13798 .debug_ranges section. */
13799 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13800 return PC_BOUNDS_INVALID;
13801 /* Found discontinuous range of addresses. */
13802 ret = PC_BOUNDS_RANGES;
13803 }
13804 else
13805 return PC_BOUNDS_NOT_PRESENT;
13806 }
13807
13808 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13809 if (high <= low)
13810 return PC_BOUNDS_INVALID;
13811
13812 /* When using the GNU linker, .gnu.linkonce. sections are used to
13813 eliminate duplicate copies of functions and vtables and such.
13814 The linker will arbitrarily choose one and discard the others.
13815 The AT_*_pc values for such functions refer to local labels in
13816 these sections. If the section from that file was discarded, the
13817 labels are not in the output, so the relocs get a value of 0.
13818 If this is a discarded function, mark the pc bounds as invalid,
13819 so that GDB will ignore it. */
13820 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13821 return PC_BOUNDS_INVALID;
13822
13823 *lowpc = low;
13824 if (highpc)
13825 *highpc = high;
13826 return ret;
13827 }
13828
13829 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13830 its low and high PC addresses. Do nothing if these addresses could not
13831 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13832 and HIGHPC to the high address if greater than HIGHPC. */
13833
13834 static void
13835 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13836 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13837 struct dwarf2_cu *cu)
13838 {
13839 CORE_ADDR low, high;
13840 struct die_info *child = die->child;
13841
13842 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13843 {
13844 *lowpc = std::min (*lowpc, low);
13845 *highpc = std::max (*highpc, high);
13846 }
13847
13848 /* If the language does not allow nested subprograms (either inside
13849 subprograms or lexical blocks), we're done. */
13850 if (cu->language != language_ada)
13851 return;
13852
13853 /* Check all the children of the given DIE. If it contains nested
13854 subprograms, then check their pc bounds. Likewise, we need to
13855 check lexical blocks as well, as they may also contain subprogram
13856 definitions. */
13857 while (child && child->tag)
13858 {
13859 if (child->tag == DW_TAG_subprogram
13860 || child->tag == DW_TAG_lexical_block)
13861 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13862 child = sibling_die (child);
13863 }
13864 }
13865
13866 /* Get the low and high pc's represented by the scope DIE, and store
13867 them in *LOWPC and *HIGHPC. If the correct values can't be
13868 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13869
13870 static void
13871 get_scope_pc_bounds (struct die_info *die,
13872 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13873 struct dwarf2_cu *cu)
13874 {
13875 CORE_ADDR best_low = (CORE_ADDR) -1;
13876 CORE_ADDR best_high = (CORE_ADDR) 0;
13877 CORE_ADDR current_low, current_high;
13878
13879 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13880 >= PC_BOUNDS_RANGES)
13881 {
13882 best_low = current_low;
13883 best_high = current_high;
13884 }
13885 else
13886 {
13887 struct die_info *child = die->child;
13888
13889 while (child && child->tag)
13890 {
13891 switch (child->tag) {
13892 case DW_TAG_subprogram:
13893 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13894 break;
13895 case DW_TAG_namespace:
13896 case DW_TAG_module:
13897 /* FIXME: carlton/2004-01-16: Should we do this for
13898 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13899 that current GCC's always emit the DIEs corresponding
13900 to definitions of methods of classes as children of a
13901 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13902 the DIEs giving the declarations, which could be
13903 anywhere). But I don't see any reason why the
13904 standards says that they have to be there. */
13905 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13906
13907 if (current_low != ((CORE_ADDR) -1))
13908 {
13909 best_low = std::min (best_low, current_low);
13910 best_high = std::max (best_high, current_high);
13911 }
13912 break;
13913 default:
13914 /* Ignore. */
13915 break;
13916 }
13917
13918 child = sibling_die (child);
13919 }
13920 }
13921
13922 *lowpc = best_low;
13923 *highpc = best_high;
13924 }
13925
13926 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13927 in DIE. */
13928
13929 static void
13930 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13931 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13932 {
13933 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13934 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13935 struct attribute *attr;
13936 struct attribute *attr_high;
13937
13938 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13939 if (attr_high)
13940 {
13941 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13942 if (attr != nullptr)
13943 {
13944 CORE_ADDR low = attr->value_as_address ();
13945 CORE_ADDR high = attr_high->value_as_address ();
13946
13947 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13948 high += low;
13949
13950 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13951 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13952 cu->get_builder ()->record_block_range (block, low, high - 1);
13953 }
13954 }
13955
13956 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13957 if (attr != nullptr)
13958 {
13959 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13960 We take advantage of the fact that DW_AT_ranges does not appear
13961 in DW_TAG_compile_unit of DWO files. */
13962 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13963
13964 /* The value of the DW_AT_ranges attribute is the offset of the
13965 address range list in the .debug_ranges section. */
13966 unsigned long offset = (DW_UNSND (attr)
13967 + (need_ranges_base ? cu->ranges_base : 0));
13968
13969 std::vector<blockrange> blockvec;
13970 dwarf2_ranges_process (offset, cu,
13971 [&] (CORE_ADDR start, CORE_ADDR end)
13972 {
13973 start += baseaddr;
13974 end += baseaddr;
13975 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13976 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13977 cu->get_builder ()->record_block_range (block, start, end - 1);
13978 blockvec.emplace_back (start, end);
13979 });
13980
13981 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13982 }
13983 }
13984
13985 /* Check whether the producer field indicates either of GCC < 4.6, or the
13986 Intel C/C++ compiler, and cache the result in CU. */
13987
13988 static void
13989 check_producer (struct dwarf2_cu *cu)
13990 {
13991 int major, minor;
13992
13993 if (cu->producer == NULL)
13994 {
13995 /* For unknown compilers expect their behavior is DWARF version
13996 compliant.
13997
13998 GCC started to support .debug_types sections by -gdwarf-4 since
13999 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14000 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14001 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14002 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14003 }
14004 else if (producer_is_gcc (cu->producer, &major, &minor))
14005 {
14006 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14007 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14008 }
14009 else if (producer_is_icc (cu->producer, &major, &minor))
14010 {
14011 cu->producer_is_icc = true;
14012 cu->producer_is_icc_lt_14 = major < 14;
14013 }
14014 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14015 cu->producer_is_codewarrior = true;
14016 else
14017 {
14018 /* For other non-GCC compilers, expect their behavior is DWARF version
14019 compliant. */
14020 }
14021
14022 cu->checked_producer = true;
14023 }
14024
14025 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14026 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14027 during 4.6.0 experimental. */
14028
14029 static bool
14030 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14031 {
14032 if (!cu->checked_producer)
14033 check_producer (cu);
14034
14035 return cu->producer_is_gxx_lt_4_6;
14036 }
14037
14038
14039 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14040 with incorrect is_stmt attributes. */
14041
14042 static bool
14043 producer_is_codewarrior (struct dwarf2_cu *cu)
14044 {
14045 if (!cu->checked_producer)
14046 check_producer (cu);
14047
14048 return cu->producer_is_codewarrior;
14049 }
14050
14051 /* Return the default accessibility type if it is not overridden by
14052 DW_AT_accessibility. */
14053
14054 static enum dwarf_access_attribute
14055 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14056 {
14057 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14058 {
14059 /* The default DWARF 2 accessibility for members is public, the default
14060 accessibility for inheritance is private. */
14061
14062 if (die->tag != DW_TAG_inheritance)
14063 return DW_ACCESS_public;
14064 else
14065 return DW_ACCESS_private;
14066 }
14067 else
14068 {
14069 /* DWARF 3+ defines the default accessibility a different way. The same
14070 rules apply now for DW_TAG_inheritance as for the members and it only
14071 depends on the container kind. */
14072
14073 if (die->parent->tag == DW_TAG_class_type)
14074 return DW_ACCESS_private;
14075 else
14076 return DW_ACCESS_public;
14077 }
14078 }
14079
14080 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14081 offset. If the attribute was not found return 0, otherwise return
14082 1. If it was found but could not properly be handled, set *OFFSET
14083 to 0. */
14084
14085 static int
14086 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14087 LONGEST *offset)
14088 {
14089 struct attribute *attr;
14090
14091 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14092 if (attr != NULL)
14093 {
14094 *offset = 0;
14095
14096 /* Note that we do not check for a section offset first here.
14097 This is because DW_AT_data_member_location is new in DWARF 4,
14098 so if we see it, we can assume that a constant form is really
14099 a constant and not a section offset. */
14100 if (attr->form_is_constant ())
14101 *offset = dwarf2_get_attr_constant_value (attr, 0);
14102 else if (attr->form_is_section_offset ())
14103 dwarf2_complex_location_expr_complaint ();
14104 else if (attr->form_is_block ())
14105 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14106 else
14107 dwarf2_complex_location_expr_complaint ();
14108
14109 return 1;
14110 }
14111
14112 return 0;
14113 }
14114
14115 /* Add an aggregate field to the field list. */
14116
14117 static void
14118 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14119 struct dwarf2_cu *cu)
14120 {
14121 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14122 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14123 struct nextfield *new_field;
14124 struct attribute *attr;
14125 struct field *fp;
14126 const char *fieldname = "";
14127
14128 if (die->tag == DW_TAG_inheritance)
14129 {
14130 fip->baseclasses.emplace_back ();
14131 new_field = &fip->baseclasses.back ();
14132 }
14133 else
14134 {
14135 fip->fields.emplace_back ();
14136 new_field = &fip->fields.back ();
14137 }
14138
14139 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14140 if (attr != nullptr)
14141 new_field->accessibility = DW_UNSND (attr);
14142 else
14143 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14144 if (new_field->accessibility != DW_ACCESS_public)
14145 fip->non_public_fields = 1;
14146
14147 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14148 if (attr != nullptr)
14149 new_field->virtuality = DW_UNSND (attr);
14150 else
14151 new_field->virtuality = DW_VIRTUALITY_none;
14152
14153 fp = &new_field->field;
14154
14155 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14156 {
14157 LONGEST offset;
14158
14159 /* Data member other than a C++ static data member. */
14160
14161 /* Get type of field. */
14162 fp->type = die_type (die, cu);
14163
14164 SET_FIELD_BITPOS (*fp, 0);
14165
14166 /* Get bit size of field (zero if none). */
14167 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14168 if (attr != nullptr)
14169 {
14170 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14171 }
14172 else
14173 {
14174 FIELD_BITSIZE (*fp) = 0;
14175 }
14176
14177 /* Get bit offset of field. */
14178 if (handle_data_member_location (die, cu, &offset))
14179 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14180 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14181 if (attr != nullptr)
14182 {
14183 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14184 {
14185 /* For big endian bits, the DW_AT_bit_offset gives the
14186 additional bit offset from the MSB of the containing
14187 anonymous object to the MSB of the field. We don't
14188 have to do anything special since we don't need to
14189 know the size of the anonymous object. */
14190 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14191 }
14192 else
14193 {
14194 /* For little endian bits, compute the bit offset to the
14195 MSB of the anonymous object, subtract off the number of
14196 bits from the MSB of the field to the MSB of the
14197 object, and then subtract off the number of bits of
14198 the field itself. The result is the bit offset of
14199 the LSB of the field. */
14200 int anonymous_size;
14201 int bit_offset = DW_UNSND (attr);
14202
14203 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14204 if (attr != nullptr)
14205 {
14206 /* The size of the anonymous object containing
14207 the bit field is explicit, so use the
14208 indicated size (in bytes). */
14209 anonymous_size = DW_UNSND (attr);
14210 }
14211 else
14212 {
14213 /* The size of the anonymous object containing
14214 the bit field must be inferred from the type
14215 attribute of the data member containing the
14216 bit field. */
14217 anonymous_size = TYPE_LENGTH (fp->type);
14218 }
14219 SET_FIELD_BITPOS (*fp,
14220 (FIELD_BITPOS (*fp)
14221 + anonymous_size * bits_per_byte
14222 - bit_offset - FIELD_BITSIZE (*fp)));
14223 }
14224 }
14225 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14226 if (attr != NULL)
14227 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14228 + dwarf2_get_attr_constant_value (attr, 0)));
14229
14230 /* Get name of field. */
14231 fieldname = dwarf2_name (die, cu);
14232 if (fieldname == NULL)
14233 fieldname = "";
14234
14235 /* The name is already allocated along with this objfile, so we don't
14236 need to duplicate it for the type. */
14237 fp->name = fieldname;
14238
14239 /* Change accessibility for artificial fields (e.g. virtual table
14240 pointer or virtual base class pointer) to private. */
14241 if (dwarf2_attr (die, DW_AT_artificial, cu))
14242 {
14243 FIELD_ARTIFICIAL (*fp) = 1;
14244 new_field->accessibility = DW_ACCESS_private;
14245 fip->non_public_fields = 1;
14246 }
14247 }
14248 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14249 {
14250 /* C++ static member. */
14251
14252 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14253 is a declaration, but all versions of G++ as of this writing
14254 (so through at least 3.2.1) incorrectly generate
14255 DW_TAG_variable tags. */
14256
14257 const char *physname;
14258
14259 /* Get name of field. */
14260 fieldname = dwarf2_name (die, cu);
14261 if (fieldname == NULL)
14262 return;
14263
14264 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14265 if (attr
14266 /* Only create a symbol if this is an external value.
14267 new_symbol checks this and puts the value in the global symbol
14268 table, which we want. If it is not external, new_symbol
14269 will try to put the value in cu->list_in_scope which is wrong. */
14270 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14271 {
14272 /* A static const member, not much different than an enum as far as
14273 we're concerned, except that we can support more types. */
14274 new_symbol (die, NULL, cu);
14275 }
14276
14277 /* Get physical name. */
14278 physname = dwarf2_physname (fieldname, die, cu);
14279
14280 /* The name is already allocated along with this objfile, so we don't
14281 need to duplicate it for the type. */
14282 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14283 FIELD_TYPE (*fp) = die_type (die, cu);
14284 FIELD_NAME (*fp) = fieldname;
14285 }
14286 else if (die->tag == DW_TAG_inheritance)
14287 {
14288 LONGEST offset;
14289
14290 /* C++ base class field. */
14291 if (handle_data_member_location (die, cu, &offset))
14292 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14293 FIELD_BITSIZE (*fp) = 0;
14294 FIELD_TYPE (*fp) = die_type (die, cu);
14295 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14296 }
14297 else if (die->tag == DW_TAG_variant_part)
14298 {
14299 /* process_structure_scope will treat this DIE as a union. */
14300 process_structure_scope (die, cu);
14301
14302 /* The variant part is relative to the start of the enclosing
14303 structure. */
14304 SET_FIELD_BITPOS (*fp, 0);
14305 fp->type = get_die_type (die, cu);
14306 fp->artificial = 1;
14307 fp->name = "<<variant>>";
14308
14309 /* Normally a DW_TAG_variant_part won't have a size, but our
14310 representation requires one, so set it to the maximum of the
14311 child sizes, being sure to account for the offset at which
14312 each child is seen. */
14313 if (TYPE_LENGTH (fp->type) == 0)
14314 {
14315 unsigned max = 0;
14316 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14317 {
14318 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14319 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14320 if (len > max)
14321 max = len;
14322 }
14323 TYPE_LENGTH (fp->type) = max;
14324 }
14325 }
14326 else
14327 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14328 }
14329
14330 /* Can the type given by DIE define another type? */
14331
14332 static bool
14333 type_can_define_types (const struct die_info *die)
14334 {
14335 switch (die->tag)
14336 {
14337 case DW_TAG_typedef:
14338 case DW_TAG_class_type:
14339 case DW_TAG_structure_type:
14340 case DW_TAG_union_type:
14341 case DW_TAG_enumeration_type:
14342 return true;
14343
14344 default:
14345 return false;
14346 }
14347 }
14348
14349 /* Add a type definition defined in the scope of the FIP's class. */
14350
14351 static void
14352 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14353 struct dwarf2_cu *cu)
14354 {
14355 struct decl_field fp;
14356 memset (&fp, 0, sizeof (fp));
14357
14358 gdb_assert (type_can_define_types (die));
14359
14360 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14361 fp.name = dwarf2_name (die, cu);
14362 fp.type = read_type_die (die, cu);
14363
14364 /* Save accessibility. */
14365 enum dwarf_access_attribute accessibility;
14366 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14367 if (attr != NULL)
14368 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14369 else
14370 accessibility = dwarf2_default_access_attribute (die, cu);
14371 switch (accessibility)
14372 {
14373 case DW_ACCESS_public:
14374 /* The assumed value if neither private nor protected. */
14375 break;
14376 case DW_ACCESS_private:
14377 fp.is_private = 1;
14378 break;
14379 case DW_ACCESS_protected:
14380 fp.is_protected = 1;
14381 break;
14382 default:
14383 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14384 }
14385
14386 if (die->tag == DW_TAG_typedef)
14387 fip->typedef_field_list.push_back (fp);
14388 else
14389 fip->nested_types_list.push_back (fp);
14390 }
14391
14392 /* Create the vector of fields, and attach it to the type. */
14393
14394 static void
14395 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14396 struct dwarf2_cu *cu)
14397 {
14398 int nfields = fip->nfields ();
14399
14400 /* Record the field count, allocate space for the array of fields,
14401 and create blank accessibility bitfields if necessary. */
14402 TYPE_NFIELDS (type) = nfields;
14403 TYPE_FIELDS (type) = (struct field *)
14404 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14405
14406 if (fip->non_public_fields && cu->language != language_ada)
14407 {
14408 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14409
14410 TYPE_FIELD_PRIVATE_BITS (type) =
14411 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14412 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14413
14414 TYPE_FIELD_PROTECTED_BITS (type) =
14415 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14416 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14417
14418 TYPE_FIELD_IGNORE_BITS (type) =
14419 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14420 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14421 }
14422
14423 /* If the type has baseclasses, allocate and clear a bit vector for
14424 TYPE_FIELD_VIRTUAL_BITS. */
14425 if (!fip->baseclasses.empty () && cu->language != language_ada)
14426 {
14427 int num_bytes = B_BYTES (fip->baseclasses.size ());
14428 unsigned char *pointer;
14429
14430 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14431 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14432 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14433 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14434 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14435 }
14436
14437 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14438 {
14439 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14440
14441 for (int index = 0; index < nfields; ++index)
14442 {
14443 struct nextfield &field = fip->fields[index];
14444
14445 if (field.variant.is_discriminant)
14446 di->discriminant_index = index;
14447 else if (field.variant.default_branch)
14448 di->default_index = index;
14449 else
14450 di->discriminants[index] = field.variant.discriminant_value;
14451 }
14452 }
14453
14454 /* Copy the saved-up fields into the field vector. */
14455 for (int i = 0; i < nfields; ++i)
14456 {
14457 struct nextfield &field
14458 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14459 : fip->fields[i - fip->baseclasses.size ()]);
14460
14461 TYPE_FIELD (type, i) = field.field;
14462 switch (field.accessibility)
14463 {
14464 case DW_ACCESS_private:
14465 if (cu->language != language_ada)
14466 SET_TYPE_FIELD_PRIVATE (type, i);
14467 break;
14468
14469 case DW_ACCESS_protected:
14470 if (cu->language != language_ada)
14471 SET_TYPE_FIELD_PROTECTED (type, i);
14472 break;
14473
14474 case DW_ACCESS_public:
14475 break;
14476
14477 default:
14478 /* Unknown accessibility. Complain and treat it as public. */
14479 {
14480 complaint (_("unsupported accessibility %d"),
14481 field.accessibility);
14482 }
14483 break;
14484 }
14485 if (i < fip->baseclasses.size ())
14486 {
14487 switch (field.virtuality)
14488 {
14489 case DW_VIRTUALITY_virtual:
14490 case DW_VIRTUALITY_pure_virtual:
14491 if (cu->language == language_ada)
14492 error (_("unexpected virtuality in component of Ada type"));
14493 SET_TYPE_FIELD_VIRTUAL (type, i);
14494 break;
14495 }
14496 }
14497 }
14498 }
14499
14500 /* Return true if this member function is a constructor, false
14501 otherwise. */
14502
14503 static int
14504 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14505 {
14506 const char *fieldname;
14507 const char *type_name;
14508 int len;
14509
14510 if (die->parent == NULL)
14511 return 0;
14512
14513 if (die->parent->tag != DW_TAG_structure_type
14514 && die->parent->tag != DW_TAG_union_type
14515 && die->parent->tag != DW_TAG_class_type)
14516 return 0;
14517
14518 fieldname = dwarf2_name (die, cu);
14519 type_name = dwarf2_name (die->parent, cu);
14520 if (fieldname == NULL || type_name == NULL)
14521 return 0;
14522
14523 len = strlen (fieldname);
14524 return (strncmp (fieldname, type_name, len) == 0
14525 && (type_name[len] == '\0' || type_name[len] == '<'));
14526 }
14527
14528 /* Check if the given VALUE is a recognized enum
14529 dwarf_defaulted_attribute constant according to DWARF5 spec,
14530 Table 7.24. */
14531
14532 static bool
14533 is_valid_DW_AT_defaulted (ULONGEST value)
14534 {
14535 switch (value)
14536 {
14537 case DW_DEFAULTED_no:
14538 case DW_DEFAULTED_in_class:
14539 case DW_DEFAULTED_out_of_class:
14540 return true;
14541 }
14542
14543 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14544 return false;
14545 }
14546
14547 /* Add a member function to the proper fieldlist. */
14548
14549 static void
14550 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14551 struct type *type, struct dwarf2_cu *cu)
14552 {
14553 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14554 struct attribute *attr;
14555 int i;
14556 struct fnfieldlist *flp = nullptr;
14557 struct fn_field *fnp;
14558 const char *fieldname;
14559 struct type *this_type;
14560 enum dwarf_access_attribute accessibility;
14561
14562 if (cu->language == language_ada)
14563 error (_("unexpected member function in Ada type"));
14564
14565 /* Get name of member function. */
14566 fieldname = dwarf2_name (die, cu);
14567 if (fieldname == NULL)
14568 return;
14569
14570 /* Look up member function name in fieldlist. */
14571 for (i = 0; i < fip->fnfieldlists.size (); i++)
14572 {
14573 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14574 {
14575 flp = &fip->fnfieldlists[i];
14576 break;
14577 }
14578 }
14579
14580 /* Create a new fnfieldlist if necessary. */
14581 if (flp == nullptr)
14582 {
14583 fip->fnfieldlists.emplace_back ();
14584 flp = &fip->fnfieldlists.back ();
14585 flp->name = fieldname;
14586 i = fip->fnfieldlists.size () - 1;
14587 }
14588
14589 /* Create a new member function field and add it to the vector of
14590 fnfieldlists. */
14591 flp->fnfields.emplace_back ();
14592 fnp = &flp->fnfields.back ();
14593
14594 /* Delay processing of the physname until later. */
14595 if (cu->language == language_cplus)
14596 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14597 die, cu);
14598 else
14599 {
14600 const char *physname = dwarf2_physname (fieldname, die, cu);
14601 fnp->physname = physname ? physname : "";
14602 }
14603
14604 fnp->type = alloc_type (objfile);
14605 this_type = read_type_die (die, cu);
14606 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14607 {
14608 int nparams = TYPE_NFIELDS (this_type);
14609
14610 /* TYPE is the domain of this method, and THIS_TYPE is the type
14611 of the method itself (TYPE_CODE_METHOD). */
14612 smash_to_method_type (fnp->type, type,
14613 TYPE_TARGET_TYPE (this_type),
14614 TYPE_FIELDS (this_type),
14615 TYPE_NFIELDS (this_type),
14616 TYPE_VARARGS (this_type));
14617
14618 /* Handle static member functions.
14619 Dwarf2 has no clean way to discern C++ static and non-static
14620 member functions. G++ helps GDB by marking the first
14621 parameter for non-static member functions (which is the this
14622 pointer) as artificial. We obtain this information from
14623 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14624 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14625 fnp->voffset = VOFFSET_STATIC;
14626 }
14627 else
14628 complaint (_("member function type missing for '%s'"),
14629 dwarf2_full_name (fieldname, die, cu));
14630
14631 /* Get fcontext from DW_AT_containing_type if present. */
14632 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14633 fnp->fcontext = die_containing_type (die, cu);
14634
14635 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14636 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14637
14638 /* Get accessibility. */
14639 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14640 if (attr != nullptr)
14641 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14642 else
14643 accessibility = dwarf2_default_access_attribute (die, cu);
14644 switch (accessibility)
14645 {
14646 case DW_ACCESS_private:
14647 fnp->is_private = 1;
14648 break;
14649 case DW_ACCESS_protected:
14650 fnp->is_protected = 1;
14651 break;
14652 }
14653
14654 /* Check for artificial methods. */
14655 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14656 if (attr && DW_UNSND (attr) != 0)
14657 fnp->is_artificial = 1;
14658
14659 /* Check for defaulted methods. */
14660 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14661 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14662 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14663
14664 /* Check for deleted methods. */
14665 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14666 if (attr != nullptr && DW_UNSND (attr) != 0)
14667 fnp->is_deleted = 1;
14668
14669 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14670
14671 /* Get index in virtual function table if it is a virtual member
14672 function. For older versions of GCC, this is an offset in the
14673 appropriate virtual table, as specified by DW_AT_containing_type.
14674 For everyone else, it is an expression to be evaluated relative
14675 to the object address. */
14676
14677 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14678 if (attr != nullptr)
14679 {
14680 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14681 {
14682 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14683 {
14684 /* Old-style GCC. */
14685 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14686 }
14687 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14688 || (DW_BLOCK (attr)->size > 1
14689 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14690 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14691 {
14692 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14693 if ((fnp->voffset % cu->header.addr_size) != 0)
14694 dwarf2_complex_location_expr_complaint ();
14695 else
14696 fnp->voffset /= cu->header.addr_size;
14697 fnp->voffset += 2;
14698 }
14699 else
14700 dwarf2_complex_location_expr_complaint ();
14701
14702 if (!fnp->fcontext)
14703 {
14704 /* If there is no `this' field and no DW_AT_containing_type,
14705 we cannot actually find a base class context for the
14706 vtable! */
14707 if (TYPE_NFIELDS (this_type) == 0
14708 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14709 {
14710 complaint (_("cannot determine context for virtual member "
14711 "function \"%s\" (offset %s)"),
14712 fieldname, sect_offset_str (die->sect_off));
14713 }
14714 else
14715 {
14716 fnp->fcontext
14717 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14718 }
14719 }
14720 }
14721 else if (attr->form_is_section_offset ())
14722 {
14723 dwarf2_complex_location_expr_complaint ();
14724 }
14725 else
14726 {
14727 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14728 fieldname);
14729 }
14730 }
14731 else
14732 {
14733 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14734 if (attr && DW_UNSND (attr))
14735 {
14736 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14737 complaint (_("Member function \"%s\" (offset %s) is virtual "
14738 "but the vtable offset is not specified"),
14739 fieldname, sect_offset_str (die->sect_off));
14740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14741 TYPE_CPLUS_DYNAMIC (type) = 1;
14742 }
14743 }
14744 }
14745
14746 /* Create the vector of member function fields, and attach it to the type. */
14747
14748 static void
14749 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14750 struct dwarf2_cu *cu)
14751 {
14752 if (cu->language == language_ada)
14753 error (_("unexpected member functions in Ada type"));
14754
14755 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14756 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14757 TYPE_ALLOC (type,
14758 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14759
14760 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14761 {
14762 struct fnfieldlist &nf = fip->fnfieldlists[i];
14763 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14764
14765 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14766 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14767 fn_flp->fn_fields = (struct fn_field *)
14768 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14769
14770 for (int k = 0; k < nf.fnfields.size (); ++k)
14771 fn_flp->fn_fields[k] = nf.fnfields[k];
14772 }
14773
14774 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14775 }
14776
14777 /* Returns non-zero if NAME is the name of a vtable member in CU's
14778 language, zero otherwise. */
14779 static int
14780 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14781 {
14782 static const char vptr[] = "_vptr";
14783
14784 /* Look for the C++ form of the vtable. */
14785 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14786 return 1;
14787
14788 return 0;
14789 }
14790
14791 /* GCC outputs unnamed structures that are really pointers to member
14792 functions, with the ABI-specified layout. If TYPE describes
14793 such a structure, smash it into a member function type.
14794
14795 GCC shouldn't do this; it should just output pointer to member DIEs.
14796 This is GCC PR debug/28767. */
14797
14798 static void
14799 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14800 {
14801 struct type *pfn_type, *self_type, *new_type;
14802
14803 /* Check for a structure with no name and two children. */
14804 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14805 return;
14806
14807 /* Check for __pfn and __delta members. */
14808 if (TYPE_FIELD_NAME (type, 0) == NULL
14809 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14810 || TYPE_FIELD_NAME (type, 1) == NULL
14811 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14812 return;
14813
14814 /* Find the type of the method. */
14815 pfn_type = TYPE_FIELD_TYPE (type, 0);
14816 if (pfn_type == NULL
14817 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14818 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14819 return;
14820
14821 /* Look for the "this" argument. */
14822 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14823 if (TYPE_NFIELDS (pfn_type) == 0
14824 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14825 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14826 return;
14827
14828 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14829 new_type = alloc_type (objfile);
14830 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14831 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14832 TYPE_VARARGS (pfn_type));
14833 smash_to_methodptr_type (type, new_type);
14834 }
14835
14836 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14837 appropriate error checking and issuing complaints if there is a
14838 problem. */
14839
14840 static ULONGEST
14841 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14842 {
14843 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14844
14845 if (attr == nullptr)
14846 return 0;
14847
14848 if (!attr->form_is_constant ())
14849 {
14850 complaint (_("DW_AT_alignment must have constant form"
14851 " - DIE at %s [in module %s]"),
14852 sect_offset_str (die->sect_off),
14853 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14854 return 0;
14855 }
14856
14857 ULONGEST align;
14858 if (attr->form == DW_FORM_sdata)
14859 {
14860 LONGEST val = DW_SND (attr);
14861 if (val < 0)
14862 {
14863 complaint (_("DW_AT_alignment value must not be negative"
14864 " - DIE at %s [in module %s]"),
14865 sect_offset_str (die->sect_off),
14866 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14867 return 0;
14868 }
14869 align = val;
14870 }
14871 else
14872 align = DW_UNSND (attr);
14873
14874 if (align == 0)
14875 {
14876 complaint (_("DW_AT_alignment value must not be zero"
14877 " - DIE at %s [in module %s]"),
14878 sect_offset_str (die->sect_off),
14879 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14880 return 0;
14881 }
14882 if ((align & (align - 1)) != 0)
14883 {
14884 complaint (_("DW_AT_alignment value must be a power of 2"
14885 " - DIE at %s [in module %s]"),
14886 sect_offset_str (die->sect_off),
14887 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14888 return 0;
14889 }
14890
14891 return align;
14892 }
14893
14894 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14895 the alignment for TYPE. */
14896
14897 static void
14898 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14899 struct type *type)
14900 {
14901 if (!set_type_align (type, get_alignment (cu, die)))
14902 complaint (_("DW_AT_alignment value too large"
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 }
14907
14908 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14909 constant for a type, according to DWARF5 spec, Table 5.5. */
14910
14911 static bool
14912 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14913 {
14914 switch (value)
14915 {
14916 case DW_CC_normal:
14917 case DW_CC_pass_by_reference:
14918 case DW_CC_pass_by_value:
14919 return true;
14920
14921 default:
14922 complaint (_("unrecognized DW_AT_calling_convention value "
14923 "(%s) for a type"), pulongest (value));
14924 return false;
14925 }
14926 }
14927
14928 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14929 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14930 also according to GNU-specific values (see include/dwarf2.h). */
14931
14932 static bool
14933 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14934 {
14935 switch (value)
14936 {
14937 case DW_CC_normal:
14938 case DW_CC_program:
14939 case DW_CC_nocall:
14940 return true;
14941
14942 case DW_CC_GNU_renesas_sh:
14943 case DW_CC_GNU_borland_fastcall_i386:
14944 case DW_CC_GDB_IBM_OpenCL:
14945 return true;
14946
14947 default:
14948 complaint (_("unrecognized DW_AT_calling_convention value "
14949 "(%s) for a subroutine"), pulongest (value));
14950 return false;
14951 }
14952 }
14953
14954 /* Called when we find the DIE that starts a structure or union scope
14955 (definition) to create a type for the structure or union. Fill in
14956 the type's name and general properties; the members will not be
14957 processed until process_structure_scope. A symbol table entry for
14958 the type will also not be done until process_structure_scope (assuming
14959 the type has a name).
14960
14961 NOTE: we need to call these functions regardless of whether or not the
14962 DIE has a DW_AT_name attribute, since it might be an anonymous
14963 structure or union. This gets the type entered into our set of
14964 user defined types. */
14965
14966 static struct type *
14967 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14968 {
14969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14970 struct type *type;
14971 struct attribute *attr;
14972 const char *name;
14973
14974 /* If the definition of this type lives in .debug_types, read that type.
14975 Don't follow DW_AT_specification though, that will take us back up
14976 the chain and we want to go down. */
14977 attr = die->attr (DW_AT_signature);
14978 if (attr != nullptr)
14979 {
14980 type = get_DW_AT_signature_type (die, attr, cu);
14981
14982 /* The type's CU may not be the same as CU.
14983 Ensure TYPE is recorded with CU in die_type_hash. */
14984 return set_die_type (die, type, cu);
14985 }
14986
14987 type = alloc_type (objfile);
14988 INIT_CPLUS_SPECIFIC (type);
14989
14990 name = dwarf2_name (die, cu);
14991 if (name != NULL)
14992 {
14993 if (cu->language == language_cplus
14994 || cu->language == language_d
14995 || cu->language == language_rust)
14996 {
14997 const char *full_name = dwarf2_full_name (name, die, cu);
14998
14999 /* dwarf2_full_name might have already finished building the DIE's
15000 type. If so, there is no need to continue. */
15001 if (get_die_type (die, cu) != NULL)
15002 return get_die_type (die, cu);
15003
15004 TYPE_NAME (type) = full_name;
15005 }
15006 else
15007 {
15008 /* The name is already allocated along with this objfile, so
15009 we don't need to duplicate it for the type. */
15010 TYPE_NAME (type) = name;
15011 }
15012 }
15013
15014 if (die->tag == DW_TAG_structure_type)
15015 {
15016 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15017 }
15018 else if (die->tag == DW_TAG_union_type)
15019 {
15020 TYPE_CODE (type) = TYPE_CODE_UNION;
15021 }
15022 else if (die->tag == DW_TAG_variant_part)
15023 {
15024 TYPE_CODE (type) = TYPE_CODE_UNION;
15025 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15026 }
15027 else
15028 {
15029 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15030 }
15031
15032 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15033 TYPE_DECLARED_CLASS (type) = 1;
15034
15035 /* Store the calling convention in the type if it's available in
15036 the die. Otherwise the calling convention remains set to
15037 the default value DW_CC_normal. */
15038 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15039 if (attr != nullptr
15040 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15041 {
15042 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15043 TYPE_CPLUS_CALLING_CONVENTION (type)
15044 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15045 }
15046
15047 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15048 if (attr != nullptr)
15049 {
15050 if (attr->form_is_constant ())
15051 TYPE_LENGTH (type) = DW_UNSND (attr);
15052 else
15053 {
15054 /* For the moment, dynamic type sizes are not supported
15055 by GDB's struct type. The actual size is determined
15056 on-demand when resolving the type of a given object,
15057 so set the type's length to zero for now. Otherwise,
15058 we record an expression as the length, and that expression
15059 could lead to a very large value, which could eventually
15060 lead to us trying to allocate that much memory when creating
15061 a value of that type. */
15062 TYPE_LENGTH (type) = 0;
15063 }
15064 }
15065 else
15066 {
15067 TYPE_LENGTH (type) = 0;
15068 }
15069
15070 maybe_set_alignment (cu, die, type);
15071
15072 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15073 {
15074 /* ICC<14 does not output the required DW_AT_declaration on
15075 incomplete types, but gives them a size of zero. */
15076 TYPE_STUB (type) = 1;
15077 }
15078 else
15079 TYPE_STUB_SUPPORTED (type) = 1;
15080
15081 if (die_is_declaration (die, cu))
15082 TYPE_STUB (type) = 1;
15083 else if (attr == NULL && die->child == NULL
15084 && producer_is_realview (cu->producer))
15085 /* RealView does not output the required DW_AT_declaration
15086 on incomplete types. */
15087 TYPE_STUB (type) = 1;
15088
15089 /* We need to add the type field to the die immediately so we don't
15090 infinitely recurse when dealing with pointers to the structure
15091 type within the structure itself. */
15092 set_die_type (die, type, cu);
15093
15094 /* set_die_type should be already done. */
15095 set_descriptive_type (type, die, cu);
15096
15097 return type;
15098 }
15099
15100 /* A helper for process_structure_scope that handles a single member
15101 DIE. */
15102
15103 static void
15104 handle_struct_member_die (struct die_info *child_die, struct type *type,
15105 struct field_info *fi,
15106 std::vector<struct symbol *> *template_args,
15107 struct dwarf2_cu *cu)
15108 {
15109 if (child_die->tag == DW_TAG_member
15110 || child_die->tag == DW_TAG_variable
15111 || child_die->tag == DW_TAG_variant_part)
15112 {
15113 /* NOTE: carlton/2002-11-05: A C++ static data member
15114 should be a DW_TAG_member that is a declaration, but
15115 all versions of G++ as of this writing (so through at
15116 least 3.2.1) incorrectly generate DW_TAG_variable
15117 tags for them instead. */
15118 dwarf2_add_field (fi, child_die, cu);
15119 }
15120 else if (child_die->tag == DW_TAG_subprogram)
15121 {
15122 /* Rust doesn't have member functions in the C++ sense.
15123 However, it does emit ordinary functions as children
15124 of a struct DIE. */
15125 if (cu->language == language_rust)
15126 read_func_scope (child_die, cu);
15127 else
15128 {
15129 /* C++ member function. */
15130 dwarf2_add_member_fn (fi, child_die, type, cu);
15131 }
15132 }
15133 else if (child_die->tag == DW_TAG_inheritance)
15134 {
15135 /* C++ base class field. */
15136 dwarf2_add_field (fi, child_die, cu);
15137 }
15138 else if (type_can_define_types (child_die))
15139 dwarf2_add_type_defn (fi, child_die, cu);
15140 else if (child_die->tag == DW_TAG_template_type_param
15141 || child_die->tag == DW_TAG_template_value_param)
15142 {
15143 struct symbol *arg = new_symbol (child_die, NULL, cu);
15144
15145 if (arg != NULL)
15146 template_args->push_back (arg);
15147 }
15148 else if (child_die->tag == DW_TAG_variant)
15149 {
15150 /* In a variant we want to get the discriminant and also add a
15151 field for our sole member child. */
15152 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15153
15154 for (die_info *variant_child = child_die->child;
15155 variant_child != NULL;
15156 variant_child = sibling_die (variant_child))
15157 {
15158 if (variant_child->tag == DW_TAG_member)
15159 {
15160 handle_struct_member_die (variant_child, type, fi,
15161 template_args, cu);
15162 /* Only handle the one. */
15163 break;
15164 }
15165 }
15166
15167 /* We don't handle this but we might as well report it if we see
15168 it. */
15169 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15170 complaint (_("DW_AT_discr_list is not supported yet"
15171 " - DIE at %s [in module %s]"),
15172 sect_offset_str (child_die->sect_off),
15173 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15174
15175 /* The first field was just added, so we can stash the
15176 discriminant there. */
15177 gdb_assert (!fi->fields.empty ());
15178 if (discr == NULL)
15179 fi->fields.back ().variant.default_branch = true;
15180 else
15181 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15182 }
15183 }
15184
15185 /* Finish creating a structure or union type, including filling in
15186 its members and creating a symbol for it. */
15187
15188 static void
15189 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15190 {
15191 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15192 struct die_info *child_die;
15193 struct type *type;
15194
15195 type = get_die_type (die, cu);
15196 if (type == NULL)
15197 type = read_structure_type (die, cu);
15198
15199 /* When reading a DW_TAG_variant_part, we need to notice when we
15200 read the discriminant member, so we can record it later in the
15201 discriminant_info. */
15202 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15203 sect_offset discr_offset {};
15204 bool has_template_parameters = false;
15205
15206 if (is_variant_part)
15207 {
15208 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15209 if (discr == NULL)
15210 {
15211 /* Maybe it's a univariant form, an extension we support.
15212 In this case arrange not to check the offset. */
15213 is_variant_part = false;
15214 }
15215 else if (discr->form_is_ref ())
15216 {
15217 struct dwarf2_cu *target_cu = cu;
15218 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15219
15220 discr_offset = target_die->sect_off;
15221 }
15222 else
15223 {
15224 complaint (_("DW_AT_discr does not have DIE reference form"
15225 " - DIE at %s [in module %s]"),
15226 sect_offset_str (die->sect_off),
15227 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15228 is_variant_part = false;
15229 }
15230 }
15231
15232 if (die->child != NULL && ! die_is_declaration (die, cu))
15233 {
15234 struct field_info fi;
15235 std::vector<struct symbol *> template_args;
15236
15237 child_die = die->child;
15238
15239 while (child_die && child_die->tag)
15240 {
15241 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15242
15243 if (is_variant_part && discr_offset == child_die->sect_off)
15244 fi.fields.back ().variant.is_discriminant = true;
15245
15246 child_die = sibling_die (child_die);
15247 }
15248
15249 /* Attach template arguments to type. */
15250 if (!template_args.empty ())
15251 {
15252 has_template_parameters = true;
15253 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15254 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15255 TYPE_TEMPLATE_ARGUMENTS (type)
15256 = XOBNEWVEC (&objfile->objfile_obstack,
15257 struct symbol *,
15258 TYPE_N_TEMPLATE_ARGUMENTS (type));
15259 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15260 template_args.data (),
15261 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15262 * sizeof (struct symbol *)));
15263 }
15264
15265 /* Attach fields and member functions to the type. */
15266 if (fi.nfields () > 0)
15267 dwarf2_attach_fields_to_type (&fi, type, cu);
15268 if (!fi.fnfieldlists.empty ())
15269 {
15270 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15271
15272 /* Get the type which refers to the base class (possibly this
15273 class itself) which contains the vtable pointer for the current
15274 class from the DW_AT_containing_type attribute. This use of
15275 DW_AT_containing_type is a GNU extension. */
15276
15277 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15278 {
15279 struct type *t = die_containing_type (die, cu);
15280
15281 set_type_vptr_basetype (type, t);
15282 if (type == t)
15283 {
15284 int i;
15285
15286 /* Our own class provides vtbl ptr. */
15287 for (i = TYPE_NFIELDS (t) - 1;
15288 i >= TYPE_N_BASECLASSES (t);
15289 --i)
15290 {
15291 const char *fieldname = TYPE_FIELD_NAME (t, i);
15292
15293 if (is_vtable_name (fieldname, cu))
15294 {
15295 set_type_vptr_fieldno (type, i);
15296 break;
15297 }
15298 }
15299
15300 /* Complain if virtual function table field not found. */
15301 if (i < TYPE_N_BASECLASSES (t))
15302 complaint (_("virtual function table pointer "
15303 "not found when defining class '%s'"),
15304 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15305 }
15306 else
15307 {
15308 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15309 }
15310 }
15311 else if (cu->producer
15312 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15313 {
15314 /* The IBM XLC compiler does not provide direct indication
15315 of the containing type, but the vtable pointer is
15316 always named __vfp. */
15317
15318 int i;
15319
15320 for (i = TYPE_NFIELDS (type) - 1;
15321 i >= TYPE_N_BASECLASSES (type);
15322 --i)
15323 {
15324 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15325 {
15326 set_type_vptr_fieldno (type, i);
15327 set_type_vptr_basetype (type, type);
15328 break;
15329 }
15330 }
15331 }
15332 }
15333
15334 /* Copy fi.typedef_field_list linked list elements content into the
15335 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15336 if (!fi.typedef_field_list.empty ())
15337 {
15338 int count = fi.typedef_field_list.size ();
15339
15340 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15341 TYPE_TYPEDEF_FIELD_ARRAY (type)
15342 = ((struct decl_field *)
15343 TYPE_ALLOC (type,
15344 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15345 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15346
15347 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15348 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15349 }
15350
15351 /* Copy fi.nested_types_list linked list elements content into the
15352 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15353 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15354 {
15355 int count = fi.nested_types_list.size ();
15356
15357 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15358 TYPE_NESTED_TYPES_ARRAY (type)
15359 = ((struct decl_field *)
15360 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15361 TYPE_NESTED_TYPES_COUNT (type) = count;
15362
15363 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15364 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15365 }
15366 }
15367
15368 quirk_gcc_member_function_pointer (type, objfile);
15369 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15370 cu->rust_unions.push_back (type);
15371
15372 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15373 snapshots) has been known to create a die giving a declaration
15374 for a class that has, as a child, a die giving a definition for a
15375 nested class. So we have to process our children even if the
15376 current die is a declaration. Normally, of course, a declaration
15377 won't have any children at all. */
15378
15379 child_die = die->child;
15380
15381 while (child_die != NULL && child_die->tag)
15382 {
15383 if (child_die->tag == DW_TAG_member
15384 || child_die->tag == DW_TAG_variable
15385 || child_die->tag == DW_TAG_inheritance
15386 || child_die->tag == DW_TAG_template_value_param
15387 || child_die->tag == DW_TAG_template_type_param)
15388 {
15389 /* Do nothing. */
15390 }
15391 else
15392 process_die (child_die, cu);
15393
15394 child_die = sibling_die (child_die);
15395 }
15396
15397 /* Do not consider external references. According to the DWARF standard,
15398 these DIEs are identified by the fact that they have no byte_size
15399 attribute, and a declaration attribute. */
15400 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15401 || !die_is_declaration (die, cu))
15402 {
15403 struct symbol *sym = new_symbol (die, type, cu);
15404
15405 if (has_template_parameters)
15406 {
15407 struct symtab *symtab;
15408 if (sym != nullptr)
15409 symtab = symbol_symtab (sym);
15410 else if (cu->line_header != nullptr)
15411 {
15412 /* Any related symtab will do. */
15413 symtab
15414 = cu->line_header->file_names ()[0].symtab;
15415 }
15416 else
15417 {
15418 symtab = nullptr;
15419 complaint (_("could not find suitable "
15420 "symtab for template parameter"
15421 " - DIE at %s [in module %s]"),
15422 sect_offset_str (die->sect_off),
15423 objfile_name (objfile));
15424 }
15425
15426 if (symtab != nullptr)
15427 {
15428 /* Make sure that the symtab is set on the new symbols.
15429 Even though they don't appear in this symtab directly,
15430 other parts of gdb assume that symbols do, and this is
15431 reasonably true. */
15432 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15433 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15434 }
15435 }
15436 }
15437 }
15438
15439 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15440 update TYPE using some information only available in DIE's children. */
15441
15442 static void
15443 update_enumeration_type_from_children (struct die_info *die,
15444 struct type *type,
15445 struct dwarf2_cu *cu)
15446 {
15447 struct die_info *child_die;
15448 int unsigned_enum = 1;
15449 int flag_enum = 1;
15450
15451 auto_obstack obstack;
15452
15453 for (child_die = die->child;
15454 child_die != NULL && child_die->tag;
15455 child_die = sibling_die (child_die))
15456 {
15457 struct attribute *attr;
15458 LONGEST value;
15459 const gdb_byte *bytes;
15460 struct dwarf2_locexpr_baton *baton;
15461 const char *name;
15462
15463 if (child_die->tag != DW_TAG_enumerator)
15464 continue;
15465
15466 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15467 if (attr == NULL)
15468 continue;
15469
15470 name = dwarf2_name (child_die, cu);
15471 if (name == NULL)
15472 name = "<anonymous enumerator>";
15473
15474 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15475 &value, &bytes, &baton);
15476 if (value < 0)
15477 {
15478 unsigned_enum = 0;
15479 flag_enum = 0;
15480 }
15481 else
15482 {
15483 if (count_one_bits_ll (value) >= 2)
15484 flag_enum = 0;
15485 }
15486
15487 /* If we already know that the enum type is neither unsigned, nor
15488 a flag type, no need to look at the rest of the enumerates. */
15489 if (!unsigned_enum && !flag_enum)
15490 break;
15491 }
15492
15493 if (unsigned_enum)
15494 TYPE_UNSIGNED (type) = 1;
15495 if (flag_enum)
15496 TYPE_FLAG_ENUM (type) = 1;
15497 }
15498
15499 /* Given a DW_AT_enumeration_type die, set its type. We do not
15500 complete the type's fields yet, or create any symbols. */
15501
15502 static struct type *
15503 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15504 {
15505 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15506 struct type *type;
15507 struct attribute *attr;
15508 const char *name;
15509
15510 /* If the definition of this type lives in .debug_types, read that type.
15511 Don't follow DW_AT_specification though, that will take us back up
15512 the chain and we want to go down. */
15513 attr = die->attr (DW_AT_signature);
15514 if (attr != nullptr)
15515 {
15516 type = get_DW_AT_signature_type (die, attr, cu);
15517
15518 /* The type's CU may not be the same as CU.
15519 Ensure TYPE is recorded with CU in die_type_hash. */
15520 return set_die_type (die, type, cu);
15521 }
15522
15523 type = alloc_type (objfile);
15524
15525 TYPE_CODE (type) = TYPE_CODE_ENUM;
15526 name = dwarf2_full_name (NULL, die, cu);
15527 if (name != NULL)
15528 TYPE_NAME (type) = name;
15529
15530 attr = dwarf2_attr (die, DW_AT_type, cu);
15531 if (attr != NULL)
15532 {
15533 struct type *underlying_type = die_type (die, cu);
15534
15535 TYPE_TARGET_TYPE (type) = underlying_type;
15536 }
15537
15538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15539 if (attr != nullptr)
15540 {
15541 TYPE_LENGTH (type) = DW_UNSND (attr);
15542 }
15543 else
15544 {
15545 TYPE_LENGTH (type) = 0;
15546 }
15547
15548 maybe_set_alignment (cu, die, type);
15549
15550 /* The enumeration DIE can be incomplete. In Ada, any type can be
15551 declared as private in the package spec, and then defined only
15552 inside the package body. Such types are known as Taft Amendment
15553 Types. When another package uses such a type, an incomplete DIE
15554 may be generated by the compiler. */
15555 if (die_is_declaration (die, cu))
15556 TYPE_STUB (type) = 1;
15557
15558 /* Finish the creation of this type by using the enum's children.
15559 We must call this even when the underlying type has been provided
15560 so that we can determine if we're looking at a "flag" enum. */
15561 update_enumeration_type_from_children (die, type, cu);
15562
15563 /* If this type has an underlying type that is not a stub, then we
15564 may use its attributes. We always use the "unsigned" attribute
15565 in this situation, because ordinarily we guess whether the type
15566 is unsigned -- but the guess can be wrong and the underlying type
15567 can tell us the reality. However, we defer to a local size
15568 attribute if one exists, because this lets the compiler override
15569 the underlying type if needed. */
15570 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15571 {
15572 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15573 if (TYPE_LENGTH (type) == 0)
15574 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15575 if (TYPE_RAW_ALIGN (type) == 0
15576 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15577 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15578 }
15579
15580 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15581
15582 return set_die_type (die, type, cu);
15583 }
15584
15585 /* Given a pointer to a die which begins an enumeration, process all
15586 the dies that define the members of the enumeration, and create the
15587 symbol for the enumeration type.
15588
15589 NOTE: We reverse the order of the element list. */
15590
15591 static void
15592 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15593 {
15594 struct type *this_type;
15595
15596 this_type = get_die_type (die, cu);
15597 if (this_type == NULL)
15598 this_type = read_enumeration_type (die, cu);
15599
15600 if (die->child != NULL)
15601 {
15602 struct die_info *child_die;
15603 struct symbol *sym;
15604 std::vector<struct field> fields;
15605 const char *name;
15606
15607 child_die = die->child;
15608 while (child_die && child_die->tag)
15609 {
15610 if (child_die->tag != DW_TAG_enumerator)
15611 {
15612 process_die (child_die, cu);
15613 }
15614 else
15615 {
15616 name = dwarf2_name (child_die, cu);
15617 if (name)
15618 {
15619 sym = new_symbol (child_die, this_type, cu);
15620
15621 fields.emplace_back ();
15622 struct field &field = fields.back ();
15623
15624 FIELD_NAME (field) = sym->linkage_name ();
15625 FIELD_TYPE (field) = NULL;
15626 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15627 FIELD_BITSIZE (field) = 0;
15628 }
15629 }
15630
15631 child_die = sibling_die (child_die);
15632 }
15633
15634 if (!fields.empty ())
15635 {
15636 TYPE_NFIELDS (this_type) = fields.size ();
15637 TYPE_FIELDS (this_type) = (struct field *)
15638 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15639 memcpy (TYPE_FIELDS (this_type), fields.data (),
15640 sizeof (struct field) * fields.size ());
15641 }
15642 }
15643
15644 /* If we are reading an enum from a .debug_types unit, and the enum
15645 is a declaration, and the enum is not the signatured type in the
15646 unit, then we do not want to add a symbol for it. Adding a
15647 symbol would in some cases obscure the true definition of the
15648 enum, giving users an incomplete type when the definition is
15649 actually available. Note that we do not want to do this for all
15650 enums which are just declarations, because C++0x allows forward
15651 enum declarations. */
15652 if (cu->per_cu->is_debug_types
15653 && die_is_declaration (die, cu))
15654 {
15655 struct signatured_type *sig_type;
15656
15657 sig_type = (struct signatured_type *) cu->per_cu;
15658 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15659 if (sig_type->type_offset_in_section != die->sect_off)
15660 return;
15661 }
15662
15663 new_symbol (die, this_type, cu);
15664 }
15665
15666 /* Extract all information from a DW_TAG_array_type DIE and put it in
15667 the DIE's type field. For now, this only handles one dimensional
15668 arrays. */
15669
15670 static struct type *
15671 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15672 {
15673 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15674 struct die_info *child_die;
15675 struct type *type;
15676 struct type *element_type, *range_type, *index_type;
15677 struct attribute *attr;
15678 const char *name;
15679 struct dynamic_prop *byte_stride_prop = NULL;
15680 unsigned int bit_stride = 0;
15681
15682 element_type = die_type (die, cu);
15683
15684 /* The die_type call above may have already set the type for this DIE. */
15685 type = get_die_type (die, cu);
15686 if (type)
15687 return type;
15688
15689 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15690 if (attr != NULL)
15691 {
15692 int stride_ok;
15693 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15694
15695 byte_stride_prop
15696 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15697 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15698 prop_type);
15699 if (!stride_ok)
15700 {
15701 complaint (_("unable to read array DW_AT_byte_stride "
15702 " - DIE at %s [in module %s]"),
15703 sect_offset_str (die->sect_off),
15704 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15705 /* Ignore this attribute. We will likely not be able to print
15706 arrays of this type correctly, but there is little we can do
15707 to help if we cannot read the attribute's value. */
15708 byte_stride_prop = NULL;
15709 }
15710 }
15711
15712 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15713 if (attr != NULL)
15714 bit_stride = DW_UNSND (attr);
15715
15716 /* Irix 6.2 native cc creates array types without children for
15717 arrays with unspecified length. */
15718 if (die->child == NULL)
15719 {
15720 index_type = objfile_type (objfile)->builtin_int;
15721 range_type = create_static_range_type (NULL, index_type, 0, -1);
15722 type = create_array_type_with_stride (NULL, element_type, range_type,
15723 byte_stride_prop, bit_stride);
15724 return set_die_type (die, type, cu);
15725 }
15726
15727 std::vector<struct type *> range_types;
15728 child_die = die->child;
15729 while (child_die && child_die->tag)
15730 {
15731 if (child_die->tag == DW_TAG_subrange_type)
15732 {
15733 struct type *child_type = read_type_die (child_die, cu);
15734
15735 if (child_type != NULL)
15736 {
15737 /* The range type was succesfully read. Save it for the
15738 array type creation. */
15739 range_types.push_back (child_type);
15740 }
15741 }
15742 child_die = sibling_die (child_die);
15743 }
15744
15745 /* Dwarf2 dimensions are output from left to right, create the
15746 necessary array types in backwards order. */
15747
15748 type = element_type;
15749
15750 if (read_array_order (die, cu) == DW_ORD_col_major)
15751 {
15752 int i = 0;
15753
15754 while (i < range_types.size ())
15755 type = create_array_type_with_stride (NULL, type, range_types[i++],
15756 byte_stride_prop, bit_stride);
15757 }
15758 else
15759 {
15760 size_t ndim = range_types.size ();
15761 while (ndim-- > 0)
15762 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15763 byte_stride_prop, bit_stride);
15764 }
15765
15766 /* Understand Dwarf2 support for vector types (like they occur on
15767 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15768 array type. This is not part of the Dwarf2/3 standard yet, but a
15769 custom vendor extension. The main difference between a regular
15770 array and the vector variant is that vectors are passed by value
15771 to functions. */
15772 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15773 if (attr != nullptr)
15774 make_vector_type (type);
15775
15776 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15777 implementation may choose to implement triple vectors using this
15778 attribute. */
15779 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15780 if (attr != nullptr)
15781 {
15782 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15783 TYPE_LENGTH (type) = DW_UNSND (attr);
15784 else
15785 complaint (_("DW_AT_byte_size for array type smaller "
15786 "than the total size of elements"));
15787 }
15788
15789 name = dwarf2_name (die, cu);
15790 if (name)
15791 TYPE_NAME (type) = name;
15792
15793 maybe_set_alignment (cu, die, type);
15794
15795 /* Install the type in the die. */
15796 set_die_type (die, type, cu);
15797
15798 /* set_die_type should be already done. */
15799 set_descriptive_type (type, die, cu);
15800
15801 return type;
15802 }
15803
15804 static enum dwarf_array_dim_ordering
15805 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15806 {
15807 struct attribute *attr;
15808
15809 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15810
15811 if (attr != nullptr)
15812 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15813
15814 /* GNU F77 is a special case, as at 08/2004 array type info is the
15815 opposite order to the dwarf2 specification, but data is still
15816 laid out as per normal fortran.
15817
15818 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15819 version checking. */
15820
15821 if (cu->language == language_fortran
15822 && cu->producer && strstr (cu->producer, "GNU F77"))
15823 {
15824 return DW_ORD_row_major;
15825 }
15826
15827 switch (cu->language_defn->la_array_ordering)
15828 {
15829 case array_column_major:
15830 return DW_ORD_col_major;
15831 case array_row_major:
15832 default:
15833 return DW_ORD_row_major;
15834 };
15835 }
15836
15837 /* Extract all information from a DW_TAG_set_type DIE and put it in
15838 the DIE's type field. */
15839
15840 static struct type *
15841 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15842 {
15843 struct type *domain_type, *set_type;
15844 struct attribute *attr;
15845
15846 domain_type = die_type (die, cu);
15847
15848 /* The die_type call above may have already set the type for this DIE. */
15849 set_type = get_die_type (die, cu);
15850 if (set_type)
15851 return set_type;
15852
15853 set_type = create_set_type (NULL, domain_type);
15854
15855 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15856 if (attr != nullptr)
15857 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15858
15859 maybe_set_alignment (cu, die, set_type);
15860
15861 return set_die_type (die, set_type, cu);
15862 }
15863
15864 /* A helper for read_common_block that creates a locexpr baton.
15865 SYM is the symbol which we are marking as computed.
15866 COMMON_DIE is the DIE for the common block.
15867 COMMON_LOC is the location expression attribute for the common
15868 block itself.
15869 MEMBER_LOC is the location expression attribute for the particular
15870 member of the common block that we are processing.
15871 CU is the CU from which the above come. */
15872
15873 static void
15874 mark_common_block_symbol_computed (struct symbol *sym,
15875 struct die_info *common_die,
15876 struct attribute *common_loc,
15877 struct attribute *member_loc,
15878 struct dwarf2_cu *cu)
15879 {
15880 struct dwarf2_per_objfile *dwarf2_per_objfile
15881 = cu->per_cu->dwarf2_per_objfile;
15882 struct objfile *objfile = dwarf2_per_objfile->objfile;
15883 struct dwarf2_locexpr_baton *baton;
15884 gdb_byte *ptr;
15885 unsigned int cu_off;
15886 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15887 LONGEST offset = 0;
15888
15889 gdb_assert (common_loc && member_loc);
15890 gdb_assert (common_loc->form_is_block ());
15891 gdb_assert (member_loc->form_is_block ()
15892 || member_loc->form_is_constant ());
15893
15894 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15895 baton->per_cu = cu->per_cu;
15896 gdb_assert (baton->per_cu);
15897
15898 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15899
15900 if (member_loc->form_is_constant ())
15901 {
15902 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15903 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15904 }
15905 else
15906 baton->size += DW_BLOCK (member_loc)->size;
15907
15908 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15909 baton->data = ptr;
15910
15911 *ptr++ = DW_OP_call4;
15912 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15913 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15914 ptr += 4;
15915
15916 if (member_loc->form_is_constant ())
15917 {
15918 *ptr++ = DW_OP_addr;
15919 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15920 ptr += cu->header.addr_size;
15921 }
15922 else
15923 {
15924 /* We have to copy the data here, because DW_OP_call4 will only
15925 use a DW_AT_location attribute. */
15926 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15927 ptr += DW_BLOCK (member_loc)->size;
15928 }
15929
15930 *ptr++ = DW_OP_plus;
15931 gdb_assert (ptr - baton->data == baton->size);
15932
15933 SYMBOL_LOCATION_BATON (sym) = baton;
15934 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15935 }
15936
15937 /* Create appropriate locally-scoped variables for all the
15938 DW_TAG_common_block entries. Also create a struct common_block
15939 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15940 is used to separate the common blocks name namespace from regular
15941 variable names. */
15942
15943 static void
15944 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15945 {
15946 struct attribute *attr;
15947
15948 attr = dwarf2_attr (die, DW_AT_location, cu);
15949 if (attr != nullptr)
15950 {
15951 /* Support the .debug_loc offsets. */
15952 if (attr->form_is_block ())
15953 {
15954 /* Ok. */
15955 }
15956 else if (attr->form_is_section_offset ())
15957 {
15958 dwarf2_complex_location_expr_complaint ();
15959 attr = NULL;
15960 }
15961 else
15962 {
15963 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15964 "common block member");
15965 attr = NULL;
15966 }
15967 }
15968
15969 if (die->child != NULL)
15970 {
15971 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15972 struct die_info *child_die;
15973 size_t n_entries = 0, size;
15974 struct common_block *common_block;
15975 struct symbol *sym;
15976
15977 for (child_die = die->child;
15978 child_die && child_die->tag;
15979 child_die = sibling_die (child_die))
15980 ++n_entries;
15981
15982 size = (sizeof (struct common_block)
15983 + (n_entries - 1) * sizeof (struct symbol *));
15984 common_block
15985 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15986 size);
15987 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15988 common_block->n_entries = 0;
15989
15990 for (child_die = die->child;
15991 child_die && child_die->tag;
15992 child_die = sibling_die (child_die))
15993 {
15994 /* Create the symbol in the DW_TAG_common_block block in the current
15995 symbol scope. */
15996 sym = new_symbol (child_die, NULL, cu);
15997 if (sym != NULL)
15998 {
15999 struct attribute *member_loc;
16000
16001 common_block->contents[common_block->n_entries++] = sym;
16002
16003 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16004 cu);
16005 if (member_loc)
16006 {
16007 /* GDB has handled this for a long time, but it is
16008 not specified by DWARF. It seems to have been
16009 emitted by gfortran at least as recently as:
16010 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16011 complaint (_("Variable in common block has "
16012 "DW_AT_data_member_location "
16013 "- DIE at %s [in module %s]"),
16014 sect_offset_str (child_die->sect_off),
16015 objfile_name (objfile));
16016
16017 if (member_loc->form_is_section_offset ())
16018 dwarf2_complex_location_expr_complaint ();
16019 else if (member_loc->form_is_constant ()
16020 || member_loc->form_is_block ())
16021 {
16022 if (attr != nullptr)
16023 mark_common_block_symbol_computed (sym, die, attr,
16024 member_loc, cu);
16025 }
16026 else
16027 dwarf2_complex_location_expr_complaint ();
16028 }
16029 }
16030 }
16031
16032 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16033 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16034 }
16035 }
16036
16037 /* Create a type for a C++ namespace. */
16038
16039 static struct type *
16040 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16041 {
16042 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16043 const char *previous_prefix, *name;
16044 int is_anonymous;
16045 struct type *type;
16046
16047 /* For extensions, reuse the type of the original namespace. */
16048 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16049 {
16050 struct die_info *ext_die;
16051 struct dwarf2_cu *ext_cu = cu;
16052
16053 ext_die = dwarf2_extension (die, &ext_cu);
16054 type = read_type_die (ext_die, ext_cu);
16055
16056 /* EXT_CU may not be the same as CU.
16057 Ensure TYPE is recorded with CU in die_type_hash. */
16058 return set_die_type (die, type, cu);
16059 }
16060
16061 name = namespace_name (die, &is_anonymous, cu);
16062
16063 /* Now build the name of the current namespace. */
16064
16065 previous_prefix = determine_prefix (die, cu);
16066 if (previous_prefix[0] != '\0')
16067 name = typename_concat (&objfile->objfile_obstack,
16068 previous_prefix, name, 0, cu);
16069
16070 /* Create the type. */
16071 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16072
16073 return set_die_type (die, type, cu);
16074 }
16075
16076 /* Read a namespace scope. */
16077
16078 static void
16079 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16080 {
16081 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16082 int is_anonymous;
16083
16084 /* Add a symbol associated to this if we haven't seen the namespace
16085 before. Also, add a using directive if it's an anonymous
16086 namespace. */
16087
16088 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16089 {
16090 struct type *type;
16091
16092 type = read_type_die (die, cu);
16093 new_symbol (die, type, cu);
16094
16095 namespace_name (die, &is_anonymous, cu);
16096 if (is_anonymous)
16097 {
16098 const char *previous_prefix = determine_prefix (die, cu);
16099
16100 std::vector<const char *> excludes;
16101 add_using_directive (using_directives (cu),
16102 previous_prefix, TYPE_NAME (type), NULL,
16103 NULL, excludes, 0, &objfile->objfile_obstack);
16104 }
16105 }
16106
16107 if (die->child != NULL)
16108 {
16109 struct die_info *child_die = die->child;
16110
16111 while (child_die && child_die->tag)
16112 {
16113 process_die (child_die, cu);
16114 child_die = sibling_die (child_die);
16115 }
16116 }
16117 }
16118
16119 /* Read a Fortran module as type. This DIE can be only a declaration used for
16120 imported module. Still we need that type as local Fortran "use ... only"
16121 declaration imports depend on the created type in determine_prefix. */
16122
16123 static struct type *
16124 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16125 {
16126 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16127 const char *module_name;
16128 struct type *type;
16129
16130 module_name = dwarf2_name (die, cu);
16131 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16132
16133 return set_die_type (die, type, cu);
16134 }
16135
16136 /* Read a Fortran module. */
16137
16138 static void
16139 read_module (struct die_info *die, struct dwarf2_cu *cu)
16140 {
16141 struct die_info *child_die = die->child;
16142 struct type *type;
16143
16144 type = read_type_die (die, cu);
16145 new_symbol (die, type, cu);
16146
16147 while (child_die && child_die->tag)
16148 {
16149 process_die (child_die, cu);
16150 child_die = sibling_die (child_die);
16151 }
16152 }
16153
16154 /* Return the name of the namespace represented by DIE. Set
16155 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16156 namespace. */
16157
16158 static const char *
16159 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16160 {
16161 struct die_info *current_die;
16162 const char *name = NULL;
16163
16164 /* Loop through the extensions until we find a name. */
16165
16166 for (current_die = die;
16167 current_die != NULL;
16168 current_die = dwarf2_extension (die, &cu))
16169 {
16170 /* We don't use dwarf2_name here so that we can detect the absence
16171 of a name -> anonymous namespace. */
16172 name = dwarf2_string_attr (die, DW_AT_name, cu);
16173
16174 if (name != NULL)
16175 break;
16176 }
16177
16178 /* Is it an anonymous namespace? */
16179
16180 *is_anonymous = (name == NULL);
16181 if (*is_anonymous)
16182 name = CP_ANONYMOUS_NAMESPACE_STR;
16183
16184 return name;
16185 }
16186
16187 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16188 the user defined type vector. */
16189
16190 static struct type *
16191 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16192 {
16193 struct gdbarch *gdbarch
16194 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16195 struct comp_unit_head *cu_header = &cu->header;
16196 struct type *type;
16197 struct attribute *attr_byte_size;
16198 struct attribute *attr_address_class;
16199 int byte_size, addr_class;
16200 struct type *target_type;
16201
16202 target_type = die_type (die, cu);
16203
16204 /* The die_type call above may have already set the type for this DIE. */
16205 type = get_die_type (die, cu);
16206 if (type)
16207 return type;
16208
16209 type = lookup_pointer_type (target_type);
16210
16211 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16212 if (attr_byte_size)
16213 byte_size = DW_UNSND (attr_byte_size);
16214 else
16215 byte_size = cu_header->addr_size;
16216
16217 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16218 if (attr_address_class)
16219 addr_class = DW_UNSND (attr_address_class);
16220 else
16221 addr_class = DW_ADDR_none;
16222
16223 ULONGEST alignment = get_alignment (cu, die);
16224
16225 /* If the pointer size, alignment, or address class is different
16226 than the default, create a type variant marked as such and set
16227 the length accordingly. */
16228 if (TYPE_LENGTH (type) != byte_size
16229 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16230 && alignment != TYPE_RAW_ALIGN (type))
16231 || addr_class != DW_ADDR_none)
16232 {
16233 if (gdbarch_address_class_type_flags_p (gdbarch))
16234 {
16235 int type_flags;
16236
16237 type_flags = gdbarch_address_class_type_flags
16238 (gdbarch, byte_size, addr_class);
16239 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16240 == 0);
16241 type = make_type_with_address_space (type, type_flags);
16242 }
16243 else if (TYPE_LENGTH (type) != byte_size)
16244 {
16245 complaint (_("invalid pointer size %d"), byte_size);
16246 }
16247 else if (TYPE_RAW_ALIGN (type) != alignment)
16248 {
16249 complaint (_("Invalid DW_AT_alignment"
16250 " - DIE at %s [in module %s]"),
16251 sect_offset_str (die->sect_off),
16252 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16253 }
16254 else
16255 {
16256 /* Should we also complain about unhandled address classes? */
16257 }
16258 }
16259
16260 TYPE_LENGTH (type) = byte_size;
16261 set_type_align (type, alignment);
16262 return set_die_type (die, type, cu);
16263 }
16264
16265 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16266 the user defined type vector. */
16267
16268 static struct type *
16269 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16270 {
16271 struct type *type;
16272 struct type *to_type;
16273 struct type *domain;
16274
16275 to_type = die_type (die, cu);
16276 domain = die_containing_type (die, cu);
16277
16278 /* The calls above may have already set the type for this DIE. */
16279 type = get_die_type (die, cu);
16280 if (type)
16281 return type;
16282
16283 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16284 type = lookup_methodptr_type (to_type);
16285 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16286 {
16287 struct type *new_type
16288 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16289
16290 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16291 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16292 TYPE_VARARGS (to_type));
16293 type = lookup_methodptr_type (new_type);
16294 }
16295 else
16296 type = lookup_memberptr_type (to_type, domain);
16297
16298 return set_die_type (die, type, cu);
16299 }
16300
16301 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16302 the user defined type vector. */
16303
16304 static struct type *
16305 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16306 enum type_code refcode)
16307 {
16308 struct comp_unit_head *cu_header = &cu->header;
16309 struct type *type, *target_type;
16310 struct attribute *attr;
16311
16312 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16313
16314 target_type = die_type (die, cu);
16315
16316 /* The die_type call above may have already set the type for this DIE. */
16317 type = get_die_type (die, cu);
16318 if (type)
16319 return type;
16320
16321 type = lookup_reference_type (target_type, refcode);
16322 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16323 if (attr != nullptr)
16324 {
16325 TYPE_LENGTH (type) = DW_UNSND (attr);
16326 }
16327 else
16328 {
16329 TYPE_LENGTH (type) = cu_header->addr_size;
16330 }
16331 maybe_set_alignment (cu, die, type);
16332 return set_die_type (die, type, cu);
16333 }
16334
16335 /* Add the given cv-qualifiers to the element type of the array. GCC
16336 outputs DWARF type qualifiers that apply to an array, not the
16337 element type. But GDB relies on the array element type to carry
16338 the cv-qualifiers. This mimics section 6.7.3 of the C99
16339 specification. */
16340
16341 static struct type *
16342 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16343 struct type *base_type, int cnst, int voltl)
16344 {
16345 struct type *el_type, *inner_array;
16346
16347 base_type = copy_type (base_type);
16348 inner_array = base_type;
16349
16350 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16351 {
16352 TYPE_TARGET_TYPE (inner_array) =
16353 copy_type (TYPE_TARGET_TYPE (inner_array));
16354 inner_array = TYPE_TARGET_TYPE (inner_array);
16355 }
16356
16357 el_type = TYPE_TARGET_TYPE (inner_array);
16358 cnst |= TYPE_CONST (el_type);
16359 voltl |= TYPE_VOLATILE (el_type);
16360 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16361
16362 return set_die_type (die, base_type, cu);
16363 }
16364
16365 static struct type *
16366 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16367 {
16368 struct type *base_type, *cv_type;
16369
16370 base_type = die_type (die, cu);
16371
16372 /* The die_type call above may have already set the type for this DIE. */
16373 cv_type = get_die_type (die, cu);
16374 if (cv_type)
16375 return cv_type;
16376
16377 /* In case the const qualifier is applied to an array type, the element type
16378 is so qualified, not the array type (section 6.7.3 of C99). */
16379 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16380 return add_array_cv_type (die, cu, base_type, 1, 0);
16381
16382 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16383 return set_die_type (die, cv_type, cu);
16384 }
16385
16386 static struct type *
16387 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16388 {
16389 struct type *base_type, *cv_type;
16390
16391 base_type = die_type (die, cu);
16392
16393 /* The die_type call above may have already set the type for this DIE. */
16394 cv_type = get_die_type (die, cu);
16395 if (cv_type)
16396 return cv_type;
16397
16398 /* In case the volatile qualifier is applied to an array type, the
16399 element type is so qualified, not the array type (section 6.7.3
16400 of C99). */
16401 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16402 return add_array_cv_type (die, cu, base_type, 0, 1);
16403
16404 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16405 return set_die_type (die, cv_type, cu);
16406 }
16407
16408 /* Handle DW_TAG_restrict_type. */
16409
16410 static struct type *
16411 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16412 {
16413 struct type *base_type, *cv_type;
16414
16415 base_type = die_type (die, cu);
16416
16417 /* The die_type call above may have already set the type for this DIE. */
16418 cv_type = get_die_type (die, cu);
16419 if (cv_type)
16420 return cv_type;
16421
16422 cv_type = make_restrict_type (base_type);
16423 return set_die_type (die, cv_type, cu);
16424 }
16425
16426 /* Handle DW_TAG_atomic_type. */
16427
16428 static struct type *
16429 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16430 {
16431 struct type *base_type, *cv_type;
16432
16433 base_type = die_type (die, cu);
16434
16435 /* The die_type call above may have already set the type for this DIE. */
16436 cv_type = get_die_type (die, cu);
16437 if (cv_type)
16438 return cv_type;
16439
16440 cv_type = make_atomic_type (base_type);
16441 return set_die_type (die, cv_type, cu);
16442 }
16443
16444 /* Extract all information from a DW_TAG_string_type DIE and add to
16445 the user defined type vector. It isn't really a user defined type,
16446 but it behaves like one, with other DIE's using an AT_user_def_type
16447 attribute to reference it. */
16448
16449 static struct type *
16450 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16451 {
16452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16453 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16454 struct type *type, *range_type, *index_type, *char_type;
16455 struct attribute *attr;
16456 struct dynamic_prop prop;
16457 bool length_is_constant = true;
16458 LONGEST length;
16459
16460 /* There are a couple of places where bit sizes might be made use of
16461 when parsing a DW_TAG_string_type, however, no producer that we know
16462 of make use of these. Handling bit sizes that are a multiple of the
16463 byte size is easy enough, but what about other bit sizes? Lets deal
16464 with that problem when we have to. Warn about these attributes being
16465 unsupported, then parse the type and ignore them like we always
16466 have. */
16467 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16468 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16469 {
16470 static bool warning_printed = false;
16471 if (!warning_printed)
16472 {
16473 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16474 "currently supported on DW_TAG_string_type."));
16475 warning_printed = true;
16476 }
16477 }
16478
16479 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16480 if (attr != nullptr && !attr->form_is_constant ())
16481 {
16482 /* The string length describes the location at which the length of
16483 the string can be found. The size of the length field can be
16484 specified with one of the attributes below. */
16485 struct type *prop_type;
16486 struct attribute *len
16487 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16488 if (len == nullptr)
16489 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16490 if (len != nullptr && len->form_is_constant ())
16491 {
16492 /* Pass 0 as the default as we know this attribute is constant
16493 and the default value will not be returned. */
16494 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16495 prop_type = cu->per_cu->int_type (sz, true);
16496 }
16497 else
16498 {
16499 /* If the size is not specified then we assume it is the size of
16500 an address on this target. */
16501 prop_type = cu->per_cu->addr_sized_int_type (true);
16502 }
16503
16504 /* Convert the attribute into a dynamic property. */
16505 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16506 length = 1;
16507 else
16508 length_is_constant = false;
16509 }
16510 else if (attr != nullptr)
16511 {
16512 /* This DW_AT_string_length just contains the length with no
16513 indirection. There's no need to create a dynamic property in this
16514 case. Pass 0 for the default value as we know it will not be
16515 returned in this case. */
16516 length = dwarf2_get_attr_constant_value (attr, 0);
16517 }
16518 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16519 {
16520 /* We don't currently support non-constant byte sizes for strings. */
16521 length = dwarf2_get_attr_constant_value (attr, 1);
16522 }
16523 else
16524 {
16525 /* Use 1 as a fallback length if we have nothing else. */
16526 length = 1;
16527 }
16528
16529 index_type = objfile_type (objfile)->builtin_int;
16530 if (length_is_constant)
16531 range_type = create_static_range_type (NULL, index_type, 1, length);
16532 else
16533 {
16534 struct dynamic_prop low_bound;
16535
16536 low_bound.kind = PROP_CONST;
16537 low_bound.data.const_val = 1;
16538 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16539 }
16540 char_type = language_string_char_type (cu->language_defn, gdbarch);
16541 type = create_string_type (NULL, char_type, range_type);
16542
16543 return set_die_type (die, type, cu);
16544 }
16545
16546 /* Assuming that DIE corresponds to a function, returns nonzero
16547 if the function is prototyped. */
16548
16549 static int
16550 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16551 {
16552 struct attribute *attr;
16553
16554 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16555 if (attr && (DW_UNSND (attr) != 0))
16556 return 1;
16557
16558 /* The DWARF standard implies that the DW_AT_prototyped attribute
16559 is only meaningful for C, but the concept also extends to other
16560 languages that allow unprototyped functions (Eg: Objective C).
16561 For all other languages, assume that functions are always
16562 prototyped. */
16563 if (cu->language != language_c
16564 && cu->language != language_objc
16565 && cu->language != language_opencl)
16566 return 1;
16567
16568 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16569 prototyped and unprototyped functions; default to prototyped,
16570 since that is more common in modern code (and RealView warns
16571 about unprototyped functions). */
16572 if (producer_is_realview (cu->producer))
16573 return 1;
16574
16575 return 0;
16576 }
16577
16578 /* Handle DIES due to C code like:
16579
16580 struct foo
16581 {
16582 int (*funcp)(int a, long l);
16583 int b;
16584 };
16585
16586 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16587
16588 static struct type *
16589 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16590 {
16591 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16592 struct type *type; /* Type that this function returns. */
16593 struct type *ftype; /* Function that returns above type. */
16594 struct attribute *attr;
16595
16596 type = die_type (die, cu);
16597
16598 /* The die_type call above may have already set the type for this DIE. */
16599 ftype = get_die_type (die, cu);
16600 if (ftype)
16601 return ftype;
16602
16603 ftype = lookup_function_type (type);
16604
16605 if (prototyped_function_p (die, cu))
16606 TYPE_PROTOTYPED (ftype) = 1;
16607
16608 /* Store the calling convention in the type if it's available in
16609 the subroutine die. Otherwise set the calling convention to
16610 the default value DW_CC_normal. */
16611 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16612 if (attr != nullptr
16613 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16614 TYPE_CALLING_CONVENTION (ftype)
16615 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16616 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16617 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16618 else
16619 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16620
16621 /* Record whether the function returns normally to its caller or not
16622 if the DWARF producer set that information. */
16623 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16624 if (attr && (DW_UNSND (attr) != 0))
16625 TYPE_NO_RETURN (ftype) = 1;
16626
16627 /* We need to add the subroutine type to the die immediately so
16628 we don't infinitely recurse when dealing with parameters
16629 declared as the same subroutine type. */
16630 set_die_type (die, ftype, cu);
16631
16632 if (die->child != NULL)
16633 {
16634 struct type *void_type = objfile_type (objfile)->builtin_void;
16635 struct die_info *child_die;
16636 int nparams, iparams;
16637
16638 /* Count the number of parameters.
16639 FIXME: GDB currently ignores vararg functions, but knows about
16640 vararg member functions. */
16641 nparams = 0;
16642 child_die = die->child;
16643 while (child_die && child_die->tag)
16644 {
16645 if (child_die->tag == DW_TAG_formal_parameter)
16646 nparams++;
16647 else if (child_die->tag == DW_TAG_unspecified_parameters)
16648 TYPE_VARARGS (ftype) = 1;
16649 child_die = sibling_die (child_die);
16650 }
16651
16652 /* Allocate storage for parameters and fill them in. */
16653 TYPE_NFIELDS (ftype) = nparams;
16654 TYPE_FIELDS (ftype) = (struct field *)
16655 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16656
16657 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16658 even if we error out during the parameters reading below. */
16659 for (iparams = 0; iparams < nparams; iparams++)
16660 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16661
16662 iparams = 0;
16663 child_die = die->child;
16664 while (child_die && child_die->tag)
16665 {
16666 if (child_die->tag == DW_TAG_formal_parameter)
16667 {
16668 struct type *arg_type;
16669
16670 /* DWARF version 2 has no clean way to discern C++
16671 static and non-static member functions. G++ helps
16672 GDB by marking the first parameter for non-static
16673 member functions (which is the this pointer) as
16674 artificial. We pass this information to
16675 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16676
16677 DWARF version 3 added DW_AT_object_pointer, which GCC
16678 4.5 does not yet generate. */
16679 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16680 if (attr != nullptr)
16681 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16682 else
16683 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16684 arg_type = die_type (child_die, cu);
16685
16686 /* RealView does not mark THIS as const, which the testsuite
16687 expects. GCC marks THIS as const in method definitions,
16688 but not in the class specifications (GCC PR 43053). */
16689 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16690 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16691 {
16692 int is_this = 0;
16693 struct dwarf2_cu *arg_cu = cu;
16694 const char *name = dwarf2_name (child_die, cu);
16695
16696 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16697 if (attr != nullptr)
16698 {
16699 /* If the compiler emits this, use it. */
16700 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16701 is_this = 1;
16702 }
16703 else if (name && strcmp (name, "this") == 0)
16704 /* Function definitions will have the argument names. */
16705 is_this = 1;
16706 else if (name == NULL && iparams == 0)
16707 /* Declarations may not have the names, so like
16708 elsewhere in GDB, assume an artificial first
16709 argument is "this". */
16710 is_this = 1;
16711
16712 if (is_this)
16713 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16714 arg_type, 0);
16715 }
16716
16717 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16718 iparams++;
16719 }
16720 child_die = sibling_die (child_die);
16721 }
16722 }
16723
16724 return ftype;
16725 }
16726
16727 static struct type *
16728 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16729 {
16730 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16731 const char *name = NULL;
16732 struct type *this_type, *target_type;
16733
16734 name = dwarf2_full_name (NULL, die, cu);
16735 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16736 TYPE_TARGET_STUB (this_type) = 1;
16737 set_die_type (die, this_type, cu);
16738 target_type = die_type (die, cu);
16739 if (target_type != this_type)
16740 TYPE_TARGET_TYPE (this_type) = target_type;
16741 else
16742 {
16743 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16744 spec and cause infinite loops in GDB. */
16745 complaint (_("Self-referential DW_TAG_typedef "
16746 "- DIE at %s [in module %s]"),
16747 sect_offset_str (die->sect_off), objfile_name (objfile));
16748 TYPE_TARGET_TYPE (this_type) = NULL;
16749 }
16750 if (name == NULL)
16751 {
16752 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16753 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16754 Handle these by just returning the target type, rather than
16755 constructing an anonymous typedef type and trying to handle this
16756 elsewhere. */
16757 set_die_type (die, target_type, cu);
16758 return target_type;
16759 }
16760 return this_type;
16761 }
16762
16763 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16764 (which may be different from NAME) to the architecture back-end to allow
16765 it to guess the correct format if necessary. */
16766
16767 static struct type *
16768 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16769 const char *name_hint, enum bfd_endian byte_order)
16770 {
16771 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16772 const struct floatformat **format;
16773 struct type *type;
16774
16775 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16776 if (format)
16777 type = init_float_type (objfile, bits, name, format, byte_order);
16778 else
16779 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16780
16781 return type;
16782 }
16783
16784 /* Allocate an integer type of size BITS and name NAME. */
16785
16786 static struct type *
16787 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16788 int bits, int unsigned_p, const char *name)
16789 {
16790 struct type *type;
16791
16792 /* Versions of Intel's C Compiler generate an integer type called "void"
16793 instead of using DW_TAG_unspecified_type. This has been seen on
16794 at least versions 14, 17, and 18. */
16795 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16796 && strcmp (name, "void") == 0)
16797 type = objfile_type (objfile)->builtin_void;
16798 else
16799 type = init_integer_type (objfile, bits, unsigned_p, name);
16800
16801 return type;
16802 }
16803
16804 /* Initialise and return a floating point type of size BITS suitable for
16805 use as a component of a complex number. The NAME_HINT is passed through
16806 when initialising the floating point type and is the name of the complex
16807 type.
16808
16809 As DWARF doesn't currently provide an explicit name for the components
16810 of a complex number, but it can be helpful to have these components
16811 named, we try to select a suitable name based on the size of the
16812 component. */
16813 static struct type *
16814 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16815 struct objfile *objfile,
16816 int bits, const char *name_hint,
16817 enum bfd_endian byte_order)
16818 {
16819 gdbarch *gdbarch = get_objfile_arch (objfile);
16820 struct type *tt = nullptr;
16821
16822 /* Try to find a suitable floating point builtin type of size BITS.
16823 We're going to use the name of this type as the name for the complex
16824 target type that we are about to create. */
16825 switch (cu->language)
16826 {
16827 case language_fortran:
16828 switch (bits)
16829 {
16830 case 32:
16831 tt = builtin_f_type (gdbarch)->builtin_real;
16832 break;
16833 case 64:
16834 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16835 break;
16836 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16837 case 128:
16838 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16839 break;
16840 }
16841 break;
16842 default:
16843 switch (bits)
16844 {
16845 case 32:
16846 tt = builtin_type (gdbarch)->builtin_float;
16847 break;
16848 case 64:
16849 tt = builtin_type (gdbarch)->builtin_double;
16850 break;
16851 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16852 case 128:
16853 tt = builtin_type (gdbarch)->builtin_long_double;
16854 break;
16855 }
16856 break;
16857 }
16858
16859 /* If the type we found doesn't match the size we were looking for, then
16860 pretend we didn't find a type at all, the complex target type we
16861 create will then be nameless. */
16862 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16863 tt = nullptr;
16864
16865 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16866 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16867 }
16868
16869 /* Find a representation of a given base type and install
16870 it in the TYPE field of the die. */
16871
16872 static struct type *
16873 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16874 {
16875 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16876 struct type *type;
16877 struct attribute *attr;
16878 int encoding = 0, bits = 0;
16879 const char *name;
16880 gdbarch *arch;
16881
16882 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16883 if (attr != nullptr)
16884 encoding = DW_UNSND (attr);
16885 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16886 if (attr != nullptr)
16887 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16888 name = dwarf2_name (die, cu);
16889 if (!name)
16890 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16891
16892 arch = get_objfile_arch (objfile);
16893 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16894
16895 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16896 if (attr)
16897 {
16898 int endianity = DW_UNSND (attr);
16899
16900 switch (endianity)
16901 {
16902 case DW_END_big:
16903 byte_order = BFD_ENDIAN_BIG;
16904 break;
16905 case DW_END_little:
16906 byte_order = BFD_ENDIAN_LITTLE;
16907 break;
16908 default:
16909 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16910 break;
16911 }
16912 }
16913
16914 switch (encoding)
16915 {
16916 case DW_ATE_address:
16917 /* Turn DW_ATE_address into a void * pointer. */
16918 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16919 type = init_pointer_type (objfile, bits, name, type);
16920 break;
16921 case DW_ATE_boolean:
16922 type = init_boolean_type (objfile, bits, 1, name);
16923 break;
16924 case DW_ATE_complex_float:
16925 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16926 byte_order);
16927 type = init_complex_type (objfile, name, type);
16928 break;
16929 case DW_ATE_decimal_float:
16930 type = init_decfloat_type (objfile, bits, name);
16931 break;
16932 case DW_ATE_float:
16933 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16934 break;
16935 case DW_ATE_signed:
16936 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16937 break;
16938 case DW_ATE_unsigned:
16939 if (cu->language == language_fortran
16940 && name
16941 && startswith (name, "character("))
16942 type = init_character_type (objfile, bits, 1, name);
16943 else
16944 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16945 break;
16946 case DW_ATE_signed_char:
16947 if (cu->language == language_ada || cu->language == language_m2
16948 || cu->language == language_pascal
16949 || cu->language == language_fortran)
16950 type = init_character_type (objfile, bits, 0, name);
16951 else
16952 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16953 break;
16954 case DW_ATE_unsigned_char:
16955 if (cu->language == language_ada || cu->language == language_m2
16956 || cu->language == language_pascal
16957 || cu->language == language_fortran
16958 || cu->language == language_rust)
16959 type = init_character_type (objfile, bits, 1, name);
16960 else
16961 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16962 break;
16963 case DW_ATE_UTF:
16964 {
16965 if (bits == 16)
16966 type = builtin_type (arch)->builtin_char16;
16967 else if (bits == 32)
16968 type = builtin_type (arch)->builtin_char32;
16969 else
16970 {
16971 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16972 bits);
16973 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16974 }
16975 return set_die_type (die, type, cu);
16976 }
16977 break;
16978
16979 default:
16980 complaint (_("unsupported DW_AT_encoding: '%s'"),
16981 dwarf_type_encoding_name (encoding));
16982 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16983 break;
16984 }
16985
16986 if (name && strcmp (name, "char") == 0)
16987 TYPE_NOSIGN (type) = 1;
16988
16989 maybe_set_alignment (cu, die, type);
16990
16991 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16992
16993 return set_die_type (die, type, cu);
16994 }
16995
16996 /* Parse dwarf attribute if it's a block, reference or constant and put the
16997 resulting value of the attribute into struct bound_prop.
16998 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16999
17000 static int
17001 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17002 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17003 struct type *default_type)
17004 {
17005 struct dwarf2_property_baton *baton;
17006 struct obstack *obstack
17007 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17008
17009 gdb_assert (default_type != NULL);
17010
17011 if (attr == NULL || prop == NULL)
17012 return 0;
17013
17014 if (attr->form_is_block ())
17015 {
17016 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17017 baton->property_type = default_type;
17018 baton->locexpr.per_cu = cu->per_cu;
17019 baton->locexpr.size = DW_BLOCK (attr)->size;
17020 baton->locexpr.data = DW_BLOCK (attr)->data;
17021 switch (attr->name)
17022 {
17023 case DW_AT_string_length:
17024 baton->locexpr.is_reference = true;
17025 break;
17026 default:
17027 baton->locexpr.is_reference = false;
17028 break;
17029 }
17030 prop->data.baton = baton;
17031 prop->kind = PROP_LOCEXPR;
17032 gdb_assert (prop->data.baton != NULL);
17033 }
17034 else if (attr->form_is_ref ())
17035 {
17036 struct dwarf2_cu *target_cu = cu;
17037 struct die_info *target_die;
17038 struct attribute *target_attr;
17039
17040 target_die = follow_die_ref (die, attr, &target_cu);
17041 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17042 if (target_attr == NULL)
17043 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17044 target_cu);
17045 if (target_attr == NULL)
17046 return 0;
17047
17048 switch (target_attr->name)
17049 {
17050 case DW_AT_location:
17051 if (target_attr->form_is_section_offset ())
17052 {
17053 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17054 baton->property_type = die_type (target_die, target_cu);
17055 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17056 prop->data.baton = baton;
17057 prop->kind = PROP_LOCLIST;
17058 gdb_assert (prop->data.baton != NULL);
17059 }
17060 else if (target_attr->form_is_block ())
17061 {
17062 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17063 baton->property_type = die_type (target_die, target_cu);
17064 baton->locexpr.per_cu = cu->per_cu;
17065 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17066 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17067 baton->locexpr.is_reference = true;
17068 prop->data.baton = baton;
17069 prop->kind = PROP_LOCEXPR;
17070 gdb_assert (prop->data.baton != NULL);
17071 }
17072 else
17073 {
17074 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17075 "dynamic property");
17076 return 0;
17077 }
17078 break;
17079 case DW_AT_data_member_location:
17080 {
17081 LONGEST offset;
17082
17083 if (!handle_data_member_location (target_die, target_cu,
17084 &offset))
17085 return 0;
17086
17087 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17088 baton->property_type = read_type_die (target_die->parent,
17089 target_cu);
17090 baton->offset_info.offset = offset;
17091 baton->offset_info.type = die_type (target_die, target_cu);
17092 prop->data.baton = baton;
17093 prop->kind = PROP_ADDR_OFFSET;
17094 break;
17095 }
17096 }
17097 }
17098 else if (attr->form_is_constant ())
17099 {
17100 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17101 prop->kind = PROP_CONST;
17102 }
17103 else
17104 {
17105 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17106 dwarf2_name (die, cu));
17107 return 0;
17108 }
17109
17110 return 1;
17111 }
17112
17113 /* See read.h. */
17114
17115 struct type *
17116 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17117 {
17118 struct objfile *objfile = dwarf2_per_objfile->objfile;
17119 struct type *int_type;
17120
17121 /* Helper macro to examine the various builtin types. */
17122 #define TRY_TYPE(F) \
17123 int_type = (unsigned_p \
17124 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17125 : objfile_type (objfile)->builtin_ ## F); \
17126 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17127 return int_type
17128
17129 TRY_TYPE (char);
17130 TRY_TYPE (short);
17131 TRY_TYPE (int);
17132 TRY_TYPE (long);
17133 TRY_TYPE (long_long);
17134
17135 #undef TRY_TYPE
17136
17137 gdb_assert_not_reached ("unable to find suitable integer type");
17138 }
17139
17140 /* See read.h. */
17141
17142 struct type *
17143 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17144 {
17145 int addr_size = this->addr_size ();
17146 return int_type (addr_size, unsigned_p);
17147 }
17148
17149 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17150 present (which is valid) then compute the default type based on the
17151 compilation units address size. */
17152
17153 static struct type *
17154 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17155 {
17156 struct type *index_type = die_type (die, cu);
17157
17158 /* Dwarf-2 specifications explicitly allows to create subrange types
17159 without specifying a base type.
17160 In that case, the base type must be set to the type of
17161 the lower bound, upper bound or count, in that order, if any of these
17162 three attributes references an object that has a type.
17163 If no base type is found, the Dwarf-2 specifications say that
17164 a signed integer type of size equal to the size of an address should
17165 be used.
17166 For the following C code: `extern char gdb_int [];'
17167 GCC produces an empty range DIE.
17168 FIXME: muller/2010-05-28: Possible references to object for low bound,
17169 high bound or count are not yet handled by this code. */
17170 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17171 index_type = cu->per_cu->addr_sized_int_type (false);
17172
17173 return index_type;
17174 }
17175
17176 /* Read the given DW_AT_subrange DIE. */
17177
17178 static struct type *
17179 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17180 {
17181 struct type *base_type, *orig_base_type;
17182 struct type *range_type;
17183 struct attribute *attr;
17184 struct dynamic_prop low, high;
17185 int low_default_is_valid;
17186 int high_bound_is_count = 0;
17187 const char *name;
17188 ULONGEST negative_mask;
17189
17190 orig_base_type = read_subrange_index_type (die, cu);
17191
17192 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17193 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17194 creating the range type, but we use the result of check_typedef
17195 when examining properties of the type. */
17196 base_type = check_typedef (orig_base_type);
17197
17198 /* The die_type call above may have already set the type for this DIE. */
17199 range_type = get_die_type (die, cu);
17200 if (range_type)
17201 return range_type;
17202
17203 low.kind = PROP_CONST;
17204 high.kind = PROP_CONST;
17205 high.data.const_val = 0;
17206
17207 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17208 omitting DW_AT_lower_bound. */
17209 switch (cu->language)
17210 {
17211 case language_c:
17212 case language_cplus:
17213 low.data.const_val = 0;
17214 low_default_is_valid = 1;
17215 break;
17216 case language_fortran:
17217 low.data.const_val = 1;
17218 low_default_is_valid = 1;
17219 break;
17220 case language_d:
17221 case language_objc:
17222 case language_rust:
17223 low.data.const_val = 0;
17224 low_default_is_valid = (cu->header.version >= 4);
17225 break;
17226 case language_ada:
17227 case language_m2:
17228 case language_pascal:
17229 low.data.const_val = 1;
17230 low_default_is_valid = (cu->header.version >= 4);
17231 break;
17232 default:
17233 low.data.const_val = 0;
17234 low_default_is_valid = 0;
17235 break;
17236 }
17237
17238 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17239 if (attr != nullptr)
17240 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17241 else if (!low_default_is_valid)
17242 complaint (_("Missing DW_AT_lower_bound "
17243 "- DIE at %s [in module %s]"),
17244 sect_offset_str (die->sect_off),
17245 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17246
17247 struct attribute *attr_ub, *attr_count;
17248 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17249 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17250 {
17251 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17252 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17253 {
17254 /* If bounds are constant do the final calculation here. */
17255 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17256 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17257 else
17258 high_bound_is_count = 1;
17259 }
17260 else
17261 {
17262 if (attr_ub != NULL)
17263 complaint (_("Unresolved DW_AT_upper_bound "
17264 "- DIE at %s [in module %s]"),
17265 sect_offset_str (die->sect_off),
17266 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17267 if (attr_count != NULL)
17268 complaint (_("Unresolved DW_AT_count "
17269 "- DIE at %s [in module %s]"),
17270 sect_offset_str (die->sect_off),
17271 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17272 }
17273 }
17274
17275 LONGEST bias = 0;
17276 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17277 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17278 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17279
17280 /* Normally, the DWARF producers are expected to use a signed
17281 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17282 But this is unfortunately not always the case, as witnessed
17283 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17284 is used instead. To work around that ambiguity, we treat
17285 the bounds as signed, and thus sign-extend their values, when
17286 the base type is signed. */
17287 negative_mask =
17288 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17289 if (low.kind == PROP_CONST
17290 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17291 low.data.const_val |= negative_mask;
17292 if (high.kind == PROP_CONST
17293 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17294 high.data.const_val |= negative_mask;
17295
17296 /* Check for bit and byte strides. */
17297 struct dynamic_prop byte_stride_prop;
17298 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17299 if (attr_byte_stride != nullptr)
17300 {
17301 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17302 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17303 prop_type);
17304 }
17305
17306 struct dynamic_prop bit_stride_prop;
17307 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17308 if (attr_bit_stride != nullptr)
17309 {
17310 /* It only makes sense to have either a bit or byte stride. */
17311 if (attr_byte_stride != nullptr)
17312 {
17313 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17314 "- DIE at %s [in module %s]"),
17315 sect_offset_str (die->sect_off),
17316 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17317 attr_bit_stride = nullptr;
17318 }
17319 else
17320 {
17321 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17322 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17323 prop_type);
17324 }
17325 }
17326
17327 if (attr_byte_stride != nullptr
17328 || attr_bit_stride != nullptr)
17329 {
17330 bool byte_stride_p = (attr_byte_stride != nullptr);
17331 struct dynamic_prop *stride
17332 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17333
17334 range_type
17335 = create_range_type_with_stride (NULL, orig_base_type, &low,
17336 &high, bias, stride, byte_stride_p);
17337 }
17338 else
17339 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17340
17341 if (high_bound_is_count)
17342 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17343
17344 /* Ada expects an empty array on no boundary attributes. */
17345 if (attr == NULL && cu->language != language_ada)
17346 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17347
17348 name = dwarf2_name (die, cu);
17349 if (name)
17350 TYPE_NAME (range_type) = name;
17351
17352 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17353 if (attr != nullptr)
17354 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17355
17356 maybe_set_alignment (cu, die, range_type);
17357
17358 set_die_type (die, range_type, cu);
17359
17360 /* set_die_type should be already done. */
17361 set_descriptive_type (range_type, die, cu);
17362
17363 return range_type;
17364 }
17365
17366 static struct type *
17367 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17368 {
17369 struct type *type;
17370
17371 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17372 NULL);
17373 TYPE_NAME (type) = dwarf2_name (die, cu);
17374
17375 /* In Ada, an unspecified type is typically used when the description
17376 of the type is deferred to a different unit. When encountering
17377 such a type, we treat it as a stub, and try to resolve it later on,
17378 when needed. */
17379 if (cu->language == language_ada)
17380 TYPE_STUB (type) = 1;
17381
17382 return set_die_type (die, type, cu);
17383 }
17384
17385 /* Read a single die and all its descendents. Set the die's sibling
17386 field to NULL; set other fields in the die correctly, and set all
17387 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17388 location of the info_ptr after reading all of those dies. PARENT
17389 is the parent of the die in question. */
17390
17391 static struct die_info *
17392 read_die_and_children (const struct die_reader_specs *reader,
17393 const gdb_byte *info_ptr,
17394 const gdb_byte **new_info_ptr,
17395 struct die_info *parent)
17396 {
17397 struct die_info *die;
17398 const gdb_byte *cur_ptr;
17399
17400 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17401 if (die == NULL)
17402 {
17403 *new_info_ptr = cur_ptr;
17404 return NULL;
17405 }
17406 store_in_ref_table (die, reader->cu);
17407
17408 if (die->has_children)
17409 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17410 else
17411 {
17412 die->child = NULL;
17413 *new_info_ptr = cur_ptr;
17414 }
17415
17416 die->sibling = NULL;
17417 die->parent = parent;
17418 return die;
17419 }
17420
17421 /* Read a die, all of its descendents, and all of its siblings; set
17422 all of the fields of all of the dies correctly. Arguments are as
17423 in read_die_and_children. */
17424
17425 static struct die_info *
17426 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17427 const gdb_byte *info_ptr,
17428 const gdb_byte **new_info_ptr,
17429 struct die_info *parent)
17430 {
17431 struct die_info *first_die, *last_sibling;
17432 const gdb_byte *cur_ptr;
17433
17434 cur_ptr = info_ptr;
17435 first_die = last_sibling = NULL;
17436
17437 while (1)
17438 {
17439 struct die_info *die
17440 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17441
17442 if (die == NULL)
17443 {
17444 *new_info_ptr = cur_ptr;
17445 return first_die;
17446 }
17447
17448 if (!first_die)
17449 first_die = die;
17450 else
17451 last_sibling->sibling = die;
17452
17453 last_sibling = die;
17454 }
17455 }
17456
17457 /* Read a die, all of its descendents, and all of its siblings; set
17458 all of the fields of all of the dies correctly. Arguments are as
17459 in read_die_and_children.
17460 This the main entry point for reading a DIE and all its children. */
17461
17462 static struct die_info *
17463 read_die_and_siblings (const struct die_reader_specs *reader,
17464 const gdb_byte *info_ptr,
17465 const gdb_byte **new_info_ptr,
17466 struct die_info *parent)
17467 {
17468 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17469 new_info_ptr, parent);
17470
17471 if (dwarf_die_debug)
17472 {
17473 fprintf_unfiltered (gdb_stdlog,
17474 "Read die from %s@0x%x of %s:\n",
17475 reader->die_section->get_name (),
17476 (unsigned) (info_ptr - reader->die_section->buffer),
17477 bfd_get_filename (reader->abfd));
17478 dump_die (die, dwarf_die_debug);
17479 }
17480
17481 return die;
17482 }
17483
17484 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17485 attributes.
17486 The caller is responsible for filling in the extra attributes
17487 and updating (*DIEP)->num_attrs.
17488 Set DIEP to point to a newly allocated die with its information,
17489 except for its child, sibling, and parent fields. */
17490
17491 static const gdb_byte *
17492 read_full_die_1 (const struct die_reader_specs *reader,
17493 struct die_info **diep, const gdb_byte *info_ptr,
17494 int num_extra_attrs)
17495 {
17496 unsigned int abbrev_number, bytes_read, i;
17497 struct abbrev_info *abbrev;
17498 struct die_info *die;
17499 struct dwarf2_cu *cu = reader->cu;
17500 bfd *abfd = reader->abfd;
17501
17502 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17503 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17504 info_ptr += bytes_read;
17505 if (!abbrev_number)
17506 {
17507 *diep = NULL;
17508 return info_ptr;
17509 }
17510
17511 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17512 if (!abbrev)
17513 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17514 abbrev_number,
17515 bfd_get_filename (abfd));
17516
17517 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17518 die->sect_off = sect_off;
17519 die->tag = abbrev->tag;
17520 die->abbrev = abbrev_number;
17521 die->has_children = abbrev->has_children;
17522
17523 /* Make the result usable.
17524 The caller needs to update num_attrs after adding the extra
17525 attributes. */
17526 die->num_attrs = abbrev->num_attrs;
17527
17528 std::vector<int> indexes_that_need_reprocess;
17529 for (i = 0; i < abbrev->num_attrs; ++i)
17530 {
17531 bool need_reprocess;
17532 info_ptr =
17533 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17534 info_ptr, &need_reprocess);
17535 if (need_reprocess)
17536 indexes_that_need_reprocess.push_back (i);
17537 }
17538
17539 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17540 if (attr != nullptr)
17541 cu->str_offsets_base = DW_UNSND (attr);
17542
17543 auto maybe_addr_base = lookup_addr_base(die);
17544 if (maybe_addr_base.has_value ())
17545 cu->addr_base = *maybe_addr_base;
17546 for (int index : indexes_that_need_reprocess)
17547 read_attribute_reprocess (reader, &die->attrs[index]);
17548 *diep = die;
17549 return info_ptr;
17550 }
17551
17552 /* Read a die and all its attributes.
17553 Set DIEP to point to a newly allocated die with its information,
17554 except for its child, sibling, and parent fields. */
17555
17556 static const gdb_byte *
17557 read_full_die (const struct die_reader_specs *reader,
17558 struct die_info **diep, const gdb_byte *info_ptr)
17559 {
17560 const gdb_byte *result;
17561
17562 result = read_full_die_1 (reader, diep, info_ptr, 0);
17563
17564 if (dwarf_die_debug)
17565 {
17566 fprintf_unfiltered (gdb_stdlog,
17567 "Read die from %s@0x%x of %s:\n",
17568 reader->die_section->get_name (),
17569 (unsigned) (info_ptr - reader->die_section->buffer),
17570 bfd_get_filename (reader->abfd));
17571 dump_die (*diep, dwarf_die_debug);
17572 }
17573
17574 return result;
17575 }
17576 \f
17577
17578 /* Returns nonzero if TAG represents a type that we might generate a partial
17579 symbol for. */
17580
17581 static int
17582 is_type_tag_for_partial (int tag)
17583 {
17584 switch (tag)
17585 {
17586 #if 0
17587 /* Some types that would be reasonable to generate partial symbols for,
17588 that we don't at present. */
17589 case DW_TAG_array_type:
17590 case DW_TAG_file_type:
17591 case DW_TAG_ptr_to_member_type:
17592 case DW_TAG_set_type:
17593 case DW_TAG_string_type:
17594 case DW_TAG_subroutine_type:
17595 #endif
17596 case DW_TAG_base_type:
17597 case DW_TAG_class_type:
17598 case DW_TAG_interface_type:
17599 case DW_TAG_enumeration_type:
17600 case DW_TAG_structure_type:
17601 case DW_TAG_subrange_type:
17602 case DW_TAG_typedef:
17603 case DW_TAG_union_type:
17604 return 1;
17605 default:
17606 return 0;
17607 }
17608 }
17609
17610 /* Load all DIEs that are interesting for partial symbols into memory. */
17611
17612 static struct partial_die_info *
17613 load_partial_dies (const struct die_reader_specs *reader,
17614 const gdb_byte *info_ptr, int building_psymtab)
17615 {
17616 struct dwarf2_cu *cu = reader->cu;
17617 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17618 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17619 unsigned int bytes_read;
17620 unsigned int load_all = 0;
17621 int nesting_level = 1;
17622
17623 parent_die = NULL;
17624 last_die = NULL;
17625
17626 gdb_assert (cu->per_cu != NULL);
17627 if (cu->per_cu->load_all_dies)
17628 load_all = 1;
17629
17630 cu->partial_dies
17631 = htab_create_alloc_ex (cu->header.length / 12,
17632 partial_die_hash,
17633 partial_die_eq,
17634 NULL,
17635 &cu->comp_unit_obstack,
17636 hashtab_obstack_allocate,
17637 dummy_obstack_deallocate);
17638
17639 while (1)
17640 {
17641 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17642
17643 /* A NULL abbrev means the end of a series of children. */
17644 if (abbrev == NULL)
17645 {
17646 if (--nesting_level == 0)
17647 return first_die;
17648
17649 info_ptr += bytes_read;
17650 last_die = parent_die;
17651 parent_die = parent_die->die_parent;
17652 continue;
17653 }
17654
17655 /* Check for template arguments. We never save these; if
17656 they're seen, we just mark the parent, and go on our way. */
17657 if (parent_die != NULL
17658 && cu->language == language_cplus
17659 && (abbrev->tag == DW_TAG_template_type_param
17660 || abbrev->tag == DW_TAG_template_value_param))
17661 {
17662 parent_die->has_template_arguments = 1;
17663
17664 if (!load_all)
17665 {
17666 /* We don't need a partial DIE for the template argument. */
17667 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17668 continue;
17669 }
17670 }
17671
17672 /* We only recurse into c++ subprograms looking for template arguments.
17673 Skip their other children. */
17674 if (!load_all
17675 && cu->language == language_cplus
17676 && parent_die != NULL
17677 && parent_die->tag == DW_TAG_subprogram)
17678 {
17679 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17680 continue;
17681 }
17682
17683 /* Check whether this DIE is interesting enough to save. Normally
17684 we would not be interested in members here, but there may be
17685 later variables referencing them via DW_AT_specification (for
17686 static members). */
17687 if (!load_all
17688 && !is_type_tag_for_partial (abbrev->tag)
17689 && abbrev->tag != DW_TAG_constant
17690 && abbrev->tag != DW_TAG_enumerator
17691 && abbrev->tag != DW_TAG_subprogram
17692 && abbrev->tag != DW_TAG_inlined_subroutine
17693 && abbrev->tag != DW_TAG_lexical_block
17694 && abbrev->tag != DW_TAG_variable
17695 && abbrev->tag != DW_TAG_namespace
17696 && abbrev->tag != DW_TAG_module
17697 && abbrev->tag != DW_TAG_member
17698 && abbrev->tag != DW_TAG_imported_unit
17699 && abbrev->tag != DW_TAG_imported_declaration)
17700 {
17701 /* Otherwise we skip to the next sibling, if any. */
17702 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17703 continue;
17704 }
17705
17706 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17707 abbrev);
17708
17709 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17710
17711 /* This two-pass algorithm for processing partial symbols has a
17712 high cost in cache pressure. Thus, handle some simple cases
17713 here which cover the majority of C partial symbols. DIEs
17714 which neither have specification tags in them, nor could have
17715 specification tags elsewhere pointing at them, can simply be
17716 processed and discarded.
17717
17718 This segment is also optional; scan_partial_symbols and
17719 add_partial_symbol will handle these DIEs if we chain
17720 them in normally. When compilers which do not emit large
17721 quantities of duplicate debug information are more common,
17722 this code can probably be removed. */
17723
17724 /* Any complete simple types at the top level (pretty much all
17725 of them, for a language without namespaces), can be processed
17726 directly. */
17727 if (parent_die == NULL
17728 && pdi.has_specification == 0
17729 && pdi.is_declaration == 0
17730 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17731 || pdi.tag == DW_TAG_base_type
17732 || pdi.tag == DW_TAG_subrange_type))
17733 {
17734 if (building_psymtab && pdi.name != NULL)
17735 add_psymbol_to_list (pdi.name, false,
17736 VAR_DOMAIN, LOC_TYPEDEF, -1,
17737 psymbol_placement::STATIC,
17738 0, cu->language, objfile);
17739 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17740 continue;
17741 }
17742
17743 /* The exception for DW_TAG_typedef with has_children above is
17744 a workaround of GCC PR debug/47510. In the case of this complaint
17745 type_name_or_error will error on such types later.
17746
17747 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17748 it could not find the child DIEs referenced later, this is checked
17749 above. In correct DWARF DW_TAG_typedef should have no children. */
17750
17751 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17752 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17753 "- DIE at %s [in module %s]"),
17754 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17755
17756 /* If we're at the second level, and we're an enumerator, and
17757 our parent has no specification (meaning possibly lives in a
17758 namespace elsewhere), then we can add the partial symbol now
17759 instead of queueing it. */
17760 if (pdi.tag == DW_TAG_enumerator
17761 && parent_die != NULL
17762 && parent_die->die_parent == NULL
17763 && parent_die->tag == DW_TAG_enumeration_type
17764 && parent_die->has_specification == 0)
17765 {
17766 if (pdi.name == NULL)
17767 complaint (_("malformed enumerator DIE ignored"));
17768 else if (building_psymtab)
17769 add_psymbol_to_list (pdi.name, false,
17770 VAR_DOMAIN, LOC_CONST, -1,
17771 cu->language == language_cplus
17772 ? psymbol_placement::GLOBAL
17773 : psymbol_placement::STATIC,
17774 0, cu->language, objfile);
17775
17776 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17777 continue;
17778 }
17779
17780 struct partial_die_info *part_die
17781 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17782
17783 /* We'll save this DIE so link it in. */
17784 part_die->die_parent = parent_die;
17785 part_die->die_sibling = NULL;
17786 part_die->die_child = NULL;
17787
17788 if (last_die && last_die == parent_die)
17789 last_die->die_child = part_die;
17790 else if (last_die)
17791 last_die->die_sibling = part_die;
17792
17793 last_die = part_die;
17794
17795 if (first_die == NULL)
17796 first_die = part_die;
17797
17798 /* Maybe add the DIE to the hash table. Not all DIEs that we
17799 find interesting need to be in the hash table, because we
17800 also have the parent/sibling/child chains; only those that we
17801 might refer to by offset later during partial symbol reading.
17802
17803 For now this means things that might have be the target of a
17804 DW_AT_specification, DW_AT_abstract_origin, or
17805 DW_AT_extension. DW_AT_extension will refer only to
17806 namespaces; DW_AT_abstract_origin refers to functions (and
17807 many things under the function DIE, but we do not recurse
17808 into function DIEs during partial symbol reading) and
17809 possibly variables as well; DW_AT_specification refers to
17810 declarations. Declarations ought to have the DW_AT_declaration
17811 flag. It happens that GCC forgets to put it in sometimes, but
17812 only for functions, not for types.
17813
17814 Adding more things than necessary to the hash table is harmless
17815 except for the performance cost. Adding too few will result in
17816 wasted time in find_partial_die, when we reread the compilation
17817 unit with load_all_dies set. */
17818
17819 if (load_all
17820 || abbrev->tag == DW_TAG_constant
17821 || abbrev->tag == DW_TAG_subprogram
17822 || abbrev->tag == DW_TAG_variable
17823 || abbrev->tag == DW_TAG_namespace
17824 || part_die->is_declaration)
17825 {
17826 void **slot;
17827
17828 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17829 to_underlying (part_die->sect_off),
17830 INSERT);
17831 *slot = part_die;
17832 }
17833
17834 /* For some DIEs we want to follow their children (if any). For C
17835 we have no reason to follow the children of structures; for other
17836 languages we have to, so that we can get at method physnames
17837 to infer fully qualified class names, for DW_AT_specification,
17838 and for C++ template arguments. For C++, we also look one level
17839 inside functions to find template arguments (if the name of the
17840 function does not already contain the template arguments).
17841
17842 For Ada and Fortran, we need to scan the children of subprograms
17843 and lexical blocks as well because these languages allow the
17844 definition of nested entities that could be interesting for the
17845 debugger, such as nested subprograms for instance. */
17846 if (last_die->has_children
17847 && (load_all
17848 || last_die->tag == DW_TAG_namespace
17849 || last_die->tag == DW_TAG_module
17850 || last_die->tag == DW_TAG_enumeration_type
17851 || (cu->language == language_cplus
17852 && last_die->tag == DW_TAG_subprogram
17853 && (last_die->name == NULL
17854 || strchr (last_die->name, '<') == NULL))
17855 || (cu->language != language_c
17856 && (last_die->tag == DW_TAG_class_type
17857 || last_die->tag == DW_TAG_interface_type
17858 || last_die->tag == DW_TAG_structure_type
17859 || last_die->tag == DW_TAG_union_type))
17860 || ((cu->language == language_ada
17861 || cu->language == language_fortran)
17862 && (last_die->tag == DW_TAG_subprogram
17863 || last_die->tag == DW_TAG_lexical_block))))
17864 {
17865 nesting_level++;
17866 parent_die = last_die;
17867 continue;
17868 }
17869
17870 /* Otherwise we skip to the next sibling, if any. */
17871 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17872
17873 /* Back to the top, do it again. */
17874 }
17875 }
17876
17877 partial_die_info::partial_die_info (sect_offset sect_off_,
17878 struct abbrev_info *abbrev)
17879 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17880 {
17881 }
17882
17883 /* Read a minimal amount of information into the minimal die structure.
17884 INFO_PTR should point just after the initial uleb128 of a DIE. */
17885
17886 const gdb_byte *
17887 partial_die_info::read (const struct die_reader_specs *reader,
17888 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17889 {
17890 struct dwarf2_cu *cu = reader->cu;
17891 struct dwarf2_per_objfile *dwarf2_per_objfile
17892 = cu->per_cu->dwarf2_per_objfile;
17893 unsigned int i;
17894 int has_low_pc_attr = 0;
17895 int has_high_pc_attr = 0;
17896 int high_pc_relative = 0;
17897
17898 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17899 for (i = 0; i < abbrev.num_attrs; ++i)
17900 {
17901 bool need_reprocess;
17902 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17903 info_ptr, &need_reprocess);
17904 /* String and address offsets that need to do the reprocessing have
17905 already been read at this point, so there is no need to wait until
17906 the loop terminates to do the reprocessing. */
17907 if (need_reprocess)
17908 read_attribute_reprocess (reader, &attr_vec[i]);
17909 attribute &attr = attr_vec[i];
17910 /* Store the data if it is of an attribute we want to keep in a
17911 partial symbol table. */
17912 switch (attr.name)
17913 {
17914 case DW_AT_name:
17915 switch (tag)
17916 {
17917 case DW_TAG_compile_unit:
17918 case DW_TAG_partial_unit:
17919 case DW_TAG_type_unit:
17920 /* Compilation units have a DW_AT_name that is a filename, not
17921 a source language identifier. */
17922 case DW_TAG_enumeration_type:
17923 case DW_TAG_enumerator:
17924 /* These tags always have simple identifiers already; no need
17925 to canonicalize them. */
17926 name = DW_STRING (&attr);
17927 break;
17928 default:
17929 {
17930 struct objfile *objfile = dwarf2_per_objfile->objfile;
17931
17932 name
17933 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17934 }
17935 break;
17936 }
17937 break;
17938 case DW_AT_linkage_name:
17939 case DW_AT_MIPS_linkage_name:
17940 /* Note that both forms of linkage name might appear. We
17941 assume they will be the same, and we only store the last
17942 one we see. */
17943 linkage_name = DW_STRING (&attr);
17944 break;
17945 case DW_AT_low_pc:
17946 has_low_pc_attr = 1;
17947 lowpc = attr.value_as_address ();
17948 break;
17949 case DW_AT_high_pc:
17950 has_high_pc_attr = 1;
17951 highpc = attr.value_as_address ();
17952 if (cu->header.version >= 4 && attr.form_is_constant ())
17953 high_pc_relative = 1;
17954 break;
17955 case DW_AT_location:
17956 /* Support the .debug_loc offsets. */
17957 if (attr.form_is_block ())
17958 {
17959 d.locdesc = DW_BLOCK (&attr);
17960 }
17961 else if (attr.form_is_section_offset ())
17962 {
17963 dwarf2_complex_location_expr_complaint ();
17964 }
17965 else
17966 {
17967 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17968 "partial symbol information");
17969 }
17970 break;
17971 case DW_AT_external:
17972 is_external = DW_UNSND (&attr);
17973 break;
17974 case DW_AT_declaration:
17975 is_declaration = DW_UNSND (&attr);
17976 break;
17977 case DW_AT_type:
17978 has_type = 1;
17979 break;
17980 case DW_AT_abstract_origin:
17981 case DW_AT_specification:
17982 case DW_AT_extension:
17983 has_specification = 1;
17984 spec_offset = dwarf2_get_ref_die_offset (&attr);
17985 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17986 || cu->per_cu->is_dwz);
17987 break;
17988 case DW_AT_sibling:
17989 /* Ignore absolute siblings, they might point outside of
17990 the current compile unit. */
17991 if (attr.form == DW_FORM_ref_addr)
17992 complaint (_("ignoring absolute DW_AT_sibling"));
17993 else
17994 {
17995 const gdb_byte *buffer = reader->buffer;
17996 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17997 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17998
17999 if (sibling_ptr < info_ptr)
18000 complaint (_("DW_AT_sibling points backwards"));
18001 else if (sibling_ptr > reader->buffer_end)
18002 reader->die_section->overflow_complaint ();
18003 else
18004 sibling = sibling_ptr;
18005 }
18006 break;
18007 case DW_AT_byte_size:
18008 has_byte_size = 1;
18009 break;
18010 case DW_AT_const_value:
18011 has_const_value = 1;
18012 break;
18013 case DW_AT_calling_convention:
18014 /* DWARF doesn't provide a way to identify a program's source-level
18015 entry point. DW_AT_calling_convention attributes are only meant
18016 to describe functions' calling conventions.
18017
18018 However, because it's a necessary piece of information in
18019 Fortran, and before DWARF 4 DW_CC_program was the only
18020 piece of debugging information whose definition refers to
18021 a 'main program' at all, several compilers marked Fortran
18022 main programs with DW_CC_program --- even when those
18023 functions use the standard calling conventions.
18024
18025 Although DWARF now specifies a way to provide this
18026 information, we support this practice for backward
18027 compatibility. */
18028 if (DW_UNSND (&attr) == DW_CC_program
18029 && cu->language == language_fortran)
18030 main_subprogram = 1;
18031 break;
18032 case DW_AT_inline:
18033 if (DW_UNSND (&attr) == DW_INL_inlined
18034 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18035 may_be_inlined = 1;
18036 break;
18037
18038 case DW_AT_import:
18039 if (tag == DW_TAG_imported_unit)
18040 {
18041 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18042 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18043 || cu->per_cu->is_dwz);
18044 }
18045 break;
18046
18047 case DW_AT_main_subprogram:
18048 main_subprogram = DW_UNSND (&attr);
18049 break;
18050
18051 case DW_AT_ranges:
18052 {
18053 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18054 but that requires a full DIE, so instead we just
18055 reimplement it. */
18056 int need_ranges_base = tag != DW_TAG_compile_unit;
18057 unsigned int ranges_offset = (DW_UNSND (&attr)
18058 + (need_ranges_base
18059 ? cu->ranges_base
18060 : 0));
18061
18062 /* Value of the DW_AT_ranges attribute is the offset in the
18063 .debug_ranges section. */
18064 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18065 nullptr))
18066 has_pc_info = 1;
18067 }
18068 break;
18069
18070 default:
18071 break;
18072 }
18073 }
18074
18075 /* For Ada, if both the name and the linkage name appear, we prefer
18076 the latter. This lets "catch exception" work better, regardless
18077 of the order in which the name and linkage name were emitted.
18078 Really, though, this is just a workaround for the fact that gdb
18079 doesn't store both the name and the linkage name. */
18080 if (cu->language == language_ada && linkage_name != nullptr)
18081 name = linkage_name;
18082
18083 if (high_pc_relative)
18084 highpc += lowpc;
18085
18086 if (has_low_pc_attr && has_high_pc_attr)
18087 {
18088 /* When using the GNU linker, .gnu.linkonce. sections are used to
18089 eliminate duplicate copies of functions and vtables and such.
18090 The linker will arbitrarily choose one and discard the others.
18091 The AT_*_pc values for such functions refer to local labels in
18092 these sections. If the section from that file was discarded, the
18093 labels are not in the output, so the relocs get a value of 0.
18094 If this is a discarded function, mark the pc bounds as invalid,
18095 so that GDB will ignore it. */
18096 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18097 {
18098 struct objfile *objfile = dwarf2_per_objfile->objfile;
18099 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18100
18101 complaint (_("DW_AT_low_pc %s is zero "
18102 "for DIE at %s [in module %s]"),
18103 paddress (gdbarch, lowpc),
18104 sect_offset_str (sect_off),
18105 objfile_name (objfile));
18106 }
18107 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18108 else if (lowpc >= highpc)
18109 {
18110 struct objfile *objfile = dwarf2_per_objfile->objfile;
18111 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18112
18113 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18114 "for DIE at %s [in module %s]"),
18115 paddress (gdbarch, lowpc),
18116 paddress (gdbarch, highpc),
18117 sect_offset_str (sect_off),
18118 objfile_name (objfile));
18119 }
18120 else
18121 has_pc_info = 1;
18122 }
18123
18124 return info_ptr;
18125 }
18126
18127 /* Find a cached partial DIE at OFFSET in CU. */
18128
18129 struct partial_die_info *
18130 dwarf2_cu::find_partial_die (sect_offset sect_off)
18131 {
18132 struct partial_die_info *lookup_die = NULL;
18133 struct partial_die_info part_die (sect_off);
18134
18135 lookup_die = ((struct partial_die_info *)
18136 htab_find_with_hash (partial_dies, &part_die,
18137 to_underlying (sect_off)));
18138
18139 return lookup_die;
18140 }
18141
18142 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18143 except in the case of .debug_types DIEs which do not reference
18144 outside their CU (they do however referencing other types via
18145 DW_FORM_ref_sig8). */
18146
18147 static const struct cu_partial_die_info
18148 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18149 {
18150 struct dwarf2_per_objfile *dwarf2_per_objfile
18151 = cu->per_cu->dwarf2_per_objfile;
18152 struct objfile *objfile = dwarf2_per_objfile->objfile;
18153 struct dwarf2_per_cu_data *per_cu = NULL;
18154 struct partial_die_info *pd = NULL;
18155
18156 if (offset_in_dwz == cu->per_cu->is_dwz
18157 && cu->header.offset_in_cu_p (sect_off))
18158 {
18159 pd = cu->find_partial_die (sect_off);
18160 if (pd != NULL)
18161 return { cu, pd };
18162 /* We missed recording what we needed.
18163 Load all dies and try again. */
18164 per_cu = cu->per_cu;
18165 }
18166 else
18167 {
18168 /* TUs don't reference other CUs/TUs (except via type signatures). */
18169 if (cu->per_cu->is_debug_types)
18170 {
18171 error (_("Dwarf Error: Type Unit at offset %s contains"
18172 " external reference to offset %s [in module %s].\n"),
18173 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18174 bfd_get_filename (objfile->obfd));
18175 }
18176 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18177 dwarf2_per_objfile);
18178
18179 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18180 load_partial_comp_unit (per_cu);
18181
18182 per_cu->cu->last_used = 0;
18183 pd = per_cu->cu->find_partial_die (sect_off);
18184 }
18185
18186 /* If we didn't find it, and not all dies have been loaded,
18187 load them all and try again. */
18188
18189 if (pd == NULL && per_cu->load_all_dies == 0)
18190 {
18191 per_cu->load_all_dies = 1;
18192
18193 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18194 THIS_CU->cu may already be in use. So we can't just free it and
18195 replace its DIEs with the ones we read in. Instead, we leave those
18196 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18197 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18198 set. */
18199 load_partial_comp_unit (per_cu);
18200
18201 pd = per_cu->cu->find_partial_die (sect_off);
18202 }
18203
18204 if (pd == NULL)
18205 internal_error (__FILE__, __LINE__,
18206 _("could not find partial DIE %s "
18207 "in cache [from module %s]\n"),
18208 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18209 return { per_cu->cu, pd };
18210 }
18211
18212 /* See if we can figure out if the class lives in a namespace. We do
18213 this by looking for a member function; its demangled name will
18214 contain namespace info, if there is any. */
18215
18216 static void
18217 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18218 struct dwarf2_cu *cu)
18219 {
18220 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18221 what template types look like, because the demangler
18222 frequently doesn't give the same name as the debug info. We
18223 could fix this by only using the demangled name to get the
18224 prefix (but see comment in read_structure_type). */
18225
18226 struct partial_die_info *real_pdi;
18227 struct partial_die_info *child_pdi;
18228
18229 /* If this DIE (this DIE's specification, if any) has a parent, then
18230 we should not do this. We'll prepend the parent's fully qualified
18231 name when we create the partial symbol. */
18232
18233 real_pdi = struct_pdi;
18234 while (real_pdi->has_specification)
18235 {
18236 auto res = find_partial_die (real_pdi->spec_offset,
18237 real_pdi->spec_is_dwz, cu);
18238 real_pdi = res.pdi;
18239 cu = res.cu;
18240 }
18241
18242 if (real_pdi->die_parent != NULL)
18243 return;
18244
18245 for (child_pdi = struct_pdi->die_child;
18246 child_pdi != NULL;
18247 child_pdi = child_pdi->die_sibling)
18248 {
18249 if (child_pdi->tag == DW_TAG_subprogram
18250 && child_pdi->linkage_name != NULL)
18251 {
18252 gdb::unique_xmalloc_ptr<char> actual_class_name
18253 (language_class_name_from_physname (cu->language_defn,
18254 child_pdi->linkage_name));
18255 if (actual_class_name != NULL)
18256 {
18257 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18258 struct_pdi->name = objfile->intern (actual_class_name.get ());
18259 }
18260 break;
18261 }
18262 }
18263 }
18264
18265 void
18266 partial_die_info::fixup (struct dwarf2_cu *cu)
18267 {
18268 /* Once we've fixed up a die, there's no point in doing so again.
18269 This also avoids a memory leak if we were to call
18270 guess_partial_die_structure_name multiple times. */
18271 if (fixup_called)
18272 return;
18273
18274 /* If we found a reference attribute and the DIE has no name, try
18275 to find a name in the referred to DIE. */
18276
18277 if (name == NULL && has_specification)
18278 {
18279 struct partial_die_info *spec_die;
18280
18281 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18282 spec_die = res.pdi;
18283 cu = res.cu;
18284
18285 spec_die->fixup (cu);
18286
18287 if (spec_die->name)
18288 {
18289 name = spec_die->name;
18290
18291 /* Copy DW_AT_external attribute if it is set. */
18292 if (spec_die->is_external)
18293 is_external = spec_die->is_external;
18294 }
18295 }
18296
18297 /* Set default names for some unnamed DIEs. */
18298
18299 if (name == NULL && tag == DW_TAG_namespace)
18300 name = CP_ANONYMOUS_NAMESPACE_STR;
18301
18302 /* If there is no parent die to provide a namespace, and there are
18303 children, see if we can determine the namespace from their linkage
18304 name. */
18305 if (cu->language == language_cplus
18306 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18307 && die_parent == NULL
18308 && has_children
18309 && (tag == DW_TAG_class_type
18310 || tag == DW_TAG_structure_type
18311 || tag == DW_TAG_union_type))
18312 guess_partial_die_structure_name (this, cu);
18313
18314 /* GCC might emit a nameless struct or union that has a linkage
18315 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18316 if (name == NULL
18317 && (tag == DW_TAG_class_type
18318 || tag == DW_TAG_interface_type
18319 || tag == DW_TAG_structure_type
18320 || tag == DW_TAG_union_type)
18321 && linkage_name != NULL)
18322 {
18323 gdb::unique_xmalloc_ptr<char> demangled
18324 (gdb_demangle (linkage_name, DMGL_TYPES));
18325 if (demangled != nullptr)
18326 {
18327 const char *base;
18328
18329 /* Strip any leading namespaces/classes, keep only the base name.
18330 DW_AT_name for named DIEs does not contain the prefixes. */
18331 base = strrchr (demangled.get (), ':');
18332 if (base && base > demangled.get () && base[-1] == ':')
18333 base++;
18334 else
18335 base = demangled.get ();
18336
18337 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18338 name = objfile->intern (base);
18339 }
18340 }
18341
18342 fixup_called = 1;
18343 }
18344
18345 /* Process the attributes that had to be skipped in the first round. These
18346 attributes are the ones that need str_offsets_base or addr_base attributes.
18347 They could not have been processed in the first round, because at the time
18348 the values of str_offsets_base or addr_base may not have been known. */
18349 void read_attribute_reprocess (const struct die_reader_specs *reader,
18350 struct attribute *attr)
18351 {
18352 struct dwarf2_cu *cu = reader->cu;
18353 switch (attr->form)
18354 {
18355 case DW_FORM_addrx:
18356 case DW_FORM_GNU_addr_index:
18357 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18358 break;
18359 case DW_FORM_strx:
18360 case DW_FORM_strx1:
18361 case DW_FORM_strx2:
18362 case DW_FORM_strx3:
18363 case DW_FORM_strx4:
18364 case DW_FORM_GNU_str_index:
18365 {
18366 unsigned int str_index = DW_UNSND (attr);
18367 if (reader->dwo_file != NULL)
18368 {
18369 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18370 DW_STRING_IS_CANONICAL (attr) = 0;
18371 }
18372 else
18373 {
18374 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18375 DW_STRING_IS_CANONICAL (attr) = 0;
18376 }
18377 break;
18378 }
18379 default:
18380 gdb_assert_not_reached (_("Unexpected DWARF form."));
18381 }
18382 }
18383
18384 /* Read an attribute value described by an attribute form. */
18385
18386 static const gdb_byte *
18387 read_attribute_value (const struct die_reader_specs *reader,
18388 struct attribute *attr, unsigned form,
18389 LONGEST implicit_const, const gdb_byte *info_ptr,
18390 bool *need_reprocess)
18391 {
18392 struct dwarf2_cu *cu = reader->cu;
18393 struct dwarf2_per_objfile *dwarf2_per_objfile
18394 = cu->per_cu->dwarf2_per_objfile;
18395 struct objfile *objfile = dwarf2_per_objfile->objfile;
18396 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18397 bfd *abfd = reader->abfd;
18398 struct comp_unit_head *cu_header = &cu->header;
18399 unsigned int bytes_read;
18400 struct dwarf_block *blk;
18401 *need_reprocess = false;
18402
18403 attr->form = (enum dwarf_form) form;
18404 switch (form)
18405 {
18406 case DW_FORM_ref_addr:
18407 if (cu->header.version == 2)
18408 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18409 &bytes_read);
18410 else
18411 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18412 &bytes_read);
18413 info_ptr += bytes_read;
18414 break;
18415 case DW_FORM_GNU_ref_alt:
18416 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18417 info_ptr += bytes_read;
18418 break;
18419 case DW_FORM_addr:
18420 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18421 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18422 info_ptr += bytes_read;
18423 break;
18424 case DW_FORM_block2:
18425 blk = dwarf_alloc_block (cu);
18426 blk->size = read_2_bytes (abfd, info_ptr);
18427 info_ptr += 2;
18428 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18429 info_ptr += blk->size;
18430 DW_BLOCK (attr) = blk;
18431 break;
18432 case DW_FORM_block4:
18433 blk = dwarf_alloc_block (cu);
18434 blk->size = read_4_bytes (abfd, info_ptr);
18435 info_ptr += 4;
18436 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18437 info_ptr += blk->size;
18438 DW_BLOCK (attr) = blk;
18439 break;
18440 case DW_FORM_data2:
18441 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18442 info_ptr += 2;
18443 break;
18444 case DW_FORM_data4:
18445 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18446 info_ptr += 4;
18447 break;
18448 case DW_FORM_data8:
18449 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18450 info_ptr += 8;
18451 break;
18452 case DW_FORM_data16:
18453 blk = dwarf_alloc_block (cu);
18454 blk->size = 16;
18455 blk->data = read_n_bytes (abfd, info_ptr, 16);
18456 info_ptr += 16;
18457 DW_BLOCK (attr) = blk;
18458 break;
18459 case DW_FORM_sec_offset:
18460 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18461 info_ptr += bytes_read;
18462 break;
18463 case DW_FORM_string:
18464 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18465 DW_STRING_IS_CANONICAL (attr) = 0;
18466 info_ptr += bytes_read;
18467 break;
18468 case DW_FORM_strp:
18469 if (!cu->per_cu->is_dwz)
18470 {
18471 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18472 abfd, info_ptr, cu_header,
18473 &bytes_read);
18474 DW_STRING_IS_CANONICAL (attr) = 0;
18475 info_ptr += bytes_read;
18476 break;
18477 }
18478 /* FALLTHROUGH */
18479 case DW_FORM_line_strp:
18480 if (!cu->per_cu->is_dwz)
18481 {
18482 DW_STRING (attr)
18483 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18484 &bytes_read);
18485 DW_STRING_IS_CANONICAL (attr) = 0;
18486 info_ptr += bytes_read;
18487 break;
18488 }
18489 /* FALLTHROUGH */
18490 case DW_FORM_GNU_strp_alt:
18491 {
18492 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18493 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18494 &bytes_read);
18495
18496 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18497 DW_STRING_IS_CANONICAL (attr) = 0;
18498 info_ptr += bytes_read;
18499 }
18500 break;
18501 case DW_FORM_exprloc:
18502 case DW_FORM_block:
18503 blk = dwarf_alloc_block (cu);
18504 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18505 info_ptr += bytes_read;
18506 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18507 info_ptr += blk->size;
18508 DW_BLOCK (attr) = blk;
18509 break;
18510 case DW_FORM_block1:
18511 blk = dwarf_alloc_block (cu);
18512 blk->size = read_1_byte (abfd, info_ptr);
18513 info_ptr += 1;
18514 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18515 info_ptr += blk->size;
18516 DW_BLOCK (attr) = blk;
18517 break;
18518 case DW_FORM_data1:
18519 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18520 info_ptr += 1;
18521 break;
18522 case DW_FORM_flag:
18523 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18524 info_ptr += 1;
18525 break;
18526 case DW_FORM_flag_present:
18527 DW_UNSND (attr) = 1;
18528 break;
18529 case DW_FORM_sdata:
18530 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18531 info_ptr += bytes_read;
18532 break;
18533 case DW_FORM_udata:
18534 case DW_FORM_rnglistx:
18535 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18536 info_ptr += bytes_read;
18537 break;
18538 case DW_FORM_ref1:
18539 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18540 + read_1_byte (abfd, info_ptr));
18541 info_ptr += 1;
18542 break;
18543 case DW_FORM_ref2:
18544 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18545 + read_2_bytes (abfd, info_ptr));
18546 info_ptr += 2;
18547 break;
18548 case DW_FORM_ref4:
18549 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18550 + read_4_bytes (abfd, info_ptr));
18551 info_ptr += 4;
18552 break;
18553 case DW_FORM_ref8:
18554 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18555 + read_8_bytes (abfd, info_ptr));
18556 info_ptr += 8;
18557 break;
18558 case DW_FORM_ref_sig8:
18559 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18560 info_ptr += 8;
18561 break;
18562 case DW_FORM_ref_udata:
18563 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18564 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18565 info_ptr += bytes_read;
18566 break;
18567 case DW_FORM_indirect:
18568 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18569 info_ptr += bytes_read;
18570 if (form == DW_FORM_implicit_const)
18571 {
18572 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18573 info_ptr += bytes_read;
18574 }
18575 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18576 info_ptr, need_reprocess);
18577 break;
18578 case DW_FORM_implicit_const:
18579 DW_SND (attr) = implicit_const;
18580 break;
18581 case DW_FORM_addrx:
18582 case DW_FORM_GNU_addr_index:
18583 *need_reprocess = true;
18584 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18585 info_ptr += bytes_read;
18586 break;
18587 case DW_FORM_strx:
18588 case DW_FORM_strx1:
18589 case DW_FORM_strx2:
18590 case DW_FORM_strx3:
18591 case DW_FORM_strx4:
18592 case DW_FORM_GNU_str_index:
18593 {
18594 ULONGEST str_index;
18595 if (form == DW_FORM_strx1)
18596 {
18597 str_index = read_1_byte (abfd, info_ptr);
18598 info_ptr += 1;
18599 }
18600 else if (form == DW_FORM_strx2)
18601 {
18602 str_index = read_2_bytes (abfd, info_ptr);
18603 info_ptr += 2;
18604 }
18605 else if (form == DW_FORM_strx3)
18606 {
18607 str_index = read_3_bytes (abfd, info_ptr);
18608 info_ptr += 3;
18609 }
18610 else if (form == DW_FORM_strx4)
18611 {
18612 str_index = read_4_bytes (abfd, info_ptr);
18613 info_ptr += 4;
18614 }
18615 else
18616 {
18617 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18618 info_ptr += bytes_read;
18619 }
18620 *need_reprocess = true;
18621 DW_UNSND (attr) = str_index;
18622 }
18623 break;
18624 default:
18625 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18626 dwarf_form_name (form),
18627 bfd_get_filename (abfd));
18628 }
18629
18630 /* Super hack. */
18631 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18632 attr->form = DW_FORM_GNU_ref_alt;
18633
18634 /* We have seen instances where the compiler tried to emit a byte
18635 size attribute of -1 which ended up being encoded as an unsigned
18636 0xffffffff. Although 0xffffffff is technically a valid size value,
18637 an object of this size seems pretty unlikely so we can relatively
18638 safely treat these cases as if the size attribute was invalid and
18639 treat them as zero by default. */
18640 if (attr->name == DW_AT_byte_size
18641 && form == DW_FORM_data4
18642 && DW_UNSND (attr) >= 0xffffffff)
18643 {
18644 complaint
18645 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18646 hex_string (DW_UNSND (attr)));
18647 DW_UNSND (attr) = 0;
18648 }
18649
18650 return info_ptr;
18651 }
18652
18653 /* Read an attribute described by an abbreviated attribute. */
18654
18655 static const gdb_byte *
18656 read_attribute (const struct die_reader_specs *reader,
18657 struct attribute *attr, struct attr_abbrev *abbrev,
18658 const gdb_byte *info_ptr, bool *need_reprocess)
18659 {
18660 attr->name = abbrev->name;
18661 return read_attribute_value (reader, attr, abbrev->form,
18662 abbrev->implicit_const, info_ptr,
18663 need_reprocess);
18664 }
18665
18666 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18667
18668 static const char *
18669 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18670 LONGEST str_offset)
18671 {
18672 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18673 str_offset, "DW_FORM_strp");
18674 }
18675
18676 /* Return pointer to string at .debug_str offset as read from BUF.
18677 BUF is assumed to be in a compilation unit described by CU_HEADER.
18678 Return *BYTES_READ_PTR count of bytes read from BUF. */
18679
18680 static const char *
18681 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18682 const gdb_byte *buf,
18683 const struct comp_unit_head *cu_header,
18684 unsigned int *bytes_read_ptr)
18685 {
18686 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18687
18688 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18689 }
18690
18691 /* See read.h. */
18692
18693 const char *
18694 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18695 const struct comp_unit_head *cu_header,
18696 unsigned int *bytes_read_ptr)
18697 {
18698 bfd *abfd = objfile->obfd;
18699 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18700
18701 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18702 }
18703
18704 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18705 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18706 ADDR_SIZE is the size of addresses from the CU header. */
18707
18708 static CORE_ADDR
18709 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18710 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18711 int addr_size)
18712 {
18713 struct objfile *objfile = dwarf2_per_objfile->objfile;
18714 bfd *abfd = objfile->obfd;
18715 const gdb_byte *info_ptr;
18716 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18717
18718 dwarf2_per_objfile->addr.read (objfile);
18719 if (dwarf2_per_objfile->addr.buffer == NULL)
18720 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18721 objfile_name (objfile));
18722 if (addr_base_or_zero + addr_index * addr_size
18723 >= dwarf2_per_objfile->addr.size)
18724 error (_("DW_FORM_addr_index pointing outside of "
18725 ".debug_addr section [in module %s]"),
18726 objfile_name (objfile));
18727 info_ptr = (dwarf2_per_objfile->addr.buffer
18728 + addr_base_or_zero + addr_index * addr_size);
18729 if (addr_size == 4)
18730 return bfd_get_32 (abfd, info_ptr);
18731 else
18732 return bfd_get_64 (abfd, info_ptr);
18733 }
18734
18735 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18736
18737 static CORE_ADDR
18738 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18739 {
18740 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18741 cu->addr_base, cu->header.addr_size);
18742 }
18743
18744 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18745
18746 static CORE_ADDR
18747 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18748 unsigned int *bytes_read)
18749 {
18750 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18751 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18752
18753 return read_addr_index (cu, addr_index);
18754 }
18755
18756 /* See read.h. */
18757
18758 CORE_ADDR
18759 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18760 {
18761 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18762 struct dwarf2_cu *cu = per_cu->cu;
18763 gdb::optional<ULONGEST> addr_base;
18764 int addr_size;
18765
18766 /* We need addr_base and addr_size.
18767 If we don't have PER_CU->cu, we have to get it.
18768 Nasty, but the alternative is storing the needed info in PER_CU,
18769 which at this point doesn't seem justified: it's not clear how frequently
18770 it would get used and it would increase the size of every PER_CU.
18771 Entry points like dwarf2_per_cu_addr_size do a similar thing
18772 so we're not in uncharted territory here.
18773 Alas we need to be a bit more complicated as addr_base is contained
18774 in the DIE.
18775
18776 We don't need to read the entire CU(/TU).
18777 We just need the header and top level die.
18778
18779 IWBN to use the aging mechanism to let us lazily later discard the CU.
18780 For now we skip this optimization. */
18781
18782 if (cu != NULL)
18783 {
18784 addr_base = cu->addr_base;
18785 addr_size = cu->header.addr_size;
18786 }
18787 else
18788 {
18789 cutu_reader reader (per_cu, NULL, 0, false);
18790 addr_base = reader.cu->addr_base;
18791 addr_size = reader.cu->header.addr_size;
18792 }
18793
18794 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18795 addr_size);
18796 }
18797
18798 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18799 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18800 DWO file. */
18801
18802 static const char *
18803 read_str_index (struct dwarf2_cu *cu,
18804 struct dwarf2_section_info *str_section,
18805 struct dwarf2_section_info *str_offsets_section,
18806 ULONGEST str_offsets_base, ULONGEST str_index)
18807 {
18808 struct dwarf2_per_objfile *dwarf2_per_objfile
18809 = cu->per_cu->dwarf2_per_objfile;
18810 struct objfile *objfile = dwarf2_per_objfile->objfile;
18811 const char *objf_name = objfile_name (objfile);
18812 bfd *abfd = objfile->obfd;
18813 const gdb_byte *info_ptr;
18814 ULONGEST str_offset;
18815 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18816
18817 str_section->read (objfile);
18818 str_offsets_section->read (objfile);
18819 if (str_section->buffer == NULL)
18820 error (_("%s used without %s section"
18821 " in CU at offset %s [in module %s]"),
18822 form_name, str_section->get_name (),
18823 sect_offset_str (cu->header.sect_off), objf_name);
18824 if (str_offsets_section->buffer == NULL)
18825 error (_("%s used without %s section"
18826 " in CU at offset %s [in module %s]"),
18827 form_name, str_section->get_name (),
18828 sect_offset_str (cu->header.sect_off), objf_name);
18829 info_ptr = (str_offsets_section->buffer
18830 + str_offsets_base
18831 + str_index * cu->header.offset_size);
18832 if (cu->header.offset_size == 4)
18833 str_offset = bfd_get_32 (abfd, info_ptr);
18834 else
18835 str_offset = bfd_get_64 (abfd, info_ptr);
18836 if (str_offset >= str_section->size)
18837 error (_("Offset from %s pointing outside of"
18838 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18839 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18840 return (const char *) (str_section->buffer + str_offset);
18841 }
18842
18843 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18844
18845 static const char *
18846 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18847 {
18848 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18849 ? reader->cu->header.addr_size : 0;
18850 return read_str_index (reader->cu,
18851 &reader->dwo_file->sections.str,
18852 &reader->dwo_file->sections.str_offsets,
18853 str_offsets_base, str_index);
18854 }
18855
18856 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18857
18858 static const char *
18859 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18860 {
18861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18862 const char *objf_name = objfile_name (objfile);
18863 static const char form_name[] = "DW_FORM_GNU_str_index";
18864 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18865
18866 if (!cu->str_offsets_base.has_value ())
18867 error (_("%s used in Fission stub without %s"
18868 " in CU at offset 0x%lx [in module %s]"),
18869 form_name, str_offsets_attr_name,
18870 (long) cu->header.offset_size, objf_name);
18871
18872 return read_str_index (cu,
18873 &cu->per_cu->dwarf2_per_objfile->str,
18874 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18875 *cu->str_offsets_base, str_index);
18876 }
18877
18878 /* Return the length of an LEB128 number in BUF. */
18879
18880 static int
18881 leb128_size (const gdb_byte *buf)
18882 {
18883 const gdb_byte *begin = buf;
18884 gdb_byte byte;
18885
18886 while (1)
18887 {
18888 byte = *buf++;
18889 if ((byte & 128) == 0)
18890 return buf - begin;
18891 }
18892 }
18893
18894 static void
18895 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18896 {
18897 switch (lang)
18898 {
18899 case DW_LANG_C89:
18900 case DW_LANG_C99:
18901 case DW_LANG_C11:
18902 case DW_LANG_C:
18903 case DW_LANG_UPC:
18904 cu->language = language_c;
18905 break;
18906 case DW_LANG_Java:
18907 case DW_LANG_C_plus_plus:
18908 case DW_LANG_C_plus_plus_11:
18909 case DW_LANG_C_plus_plus_14:
18910 cu->language = language_cplus;
18911 break;
18912 case DW_LANG_D:
18913 cu->language = language_d;
18914 break;
18915 case DW_LANG_Fortran77:
18916 case DW_LANG_Fortran90:
18917 case DW_LANG_Fortran95:
18918 case DW_LANG_Fortran03:
18919 case DW_LANG_Fortran08:
18920 cu->language = language_fortran;
18921 break;
18922 case DW_LANG_Go:
18923 cu->language = language_go;
18924 break;
18925 case DW_LANG_Mips_Assembler:
18926 cu->language = language_asm;
18927 break;
18928 case DW_LANG_Ada83:
18929 case DW_LANG_Ada95:
18930 cu->language = language_ada;
18931 break;
18932 case DW_LANG_Modula2:
18933 cu->language = language_m2;
18934 break;
18935 case DW_LANG_Pascal83:
18936 cu->language = language_pascal;
18937 break;
18938 case DW_LANG_ObjC:
18939 cu->language = language_objc;
18940 break;
18941 case DW_LANG_Rust:
18942 case DW_LANG_Rust_old:
18943 cu->language = language_rust;
18944 break;
18945 case DW_LANG_Cobol74:
18946 case DW_LANG_Cobol85:
18947 default:
18948 cu->language = language_minimal;
18949 break;
18950 }
18951 cu->language_defn = language_def (cu->language);
18952 }
18953
18954 /* Return the named attribute or NULL if not there. */
18955
18956 static struct attribute *
18957 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18958 {
18959 for (;;)
18960 {
18961 unsigned int i;
18962 struct attribute *spec = NULL;
18963
18964 for (i = 0; i < die->num_attrs; ++i)
18965 {
18966 if (die->attrs[i].name == name)
18967 return &die->attrs[i];
18968 if (die->attrs[i].name == DW_AT_specification
18969 || die->attrs[i].name == DW_AT_abstract_origin)
18970 spec = &die->attrs[i];
18971 }
18972
18973 if (!spec)
18974 break;
18975
18976 die = follow_die_ref (die, spec, &cu);
18977 }
18978
18979 return NULL;
18980 }
18981
18982 /* Return the string associated with a string-typed attribute, or NULL if it
18983 is either not found or is of an incorrect type. */
18984
18985 static const char *
18986 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18987 {
18988 struct attribute *attr;
18989 const char *str = NULL;
18990
18991 attr = dwarf2_attr (die, name, cu);
18992
18993 if (attr != NULL)
18994 {
18995 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18996 || attr->form == DW_FORM_string
18997 || attr->form == DW_FORM_strx
18998 || attr->form == DW_FORM_strx1
18999 || attr->form == DW_FORM_strx2
19000 || attr->form == DW_FORM_strx3
19001 || attr->form == DW_FORM_strx4
19002 || attr->form == DW_FORM_GNU_str_index
19003 || attr->form == DW_FORM_GNU_strp_alt)
19004 str = DW_STRING (attr);
19005 else
19006 complaint (_("string type expected for attribute %s for "
19007 "DIE at %s in module %s"),
19008 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19009 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
19010 }
19011
19012 return str;
19013 }
19014
19015 /* Return the dwo name or NULL if not present. If present, it is in either
19016 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19017 static const char *
19018 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19019 {
19020 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19021 if (dwo_name == nullptr)
19022 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19023 return dwo_name;
19024 }
19025
19026 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19027 and holds a non-zero value. This function should only be used for
19028 DW_FORM_flag or DW_FORM_flag_present attributes. */
19029
19030 static int
19031 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19032 {
19033 struct attribute *attr = dwarf2_attr (die, name, cu);
19034
19035 return (attr && DW_UNSND (attr));
19036 }
19037
19038 static int
19039 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19040 {
19041 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19042 which value is non-zero. However, we have to be careful with
19043 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19044 (via dwarf2_flag_true_p) follows this attribute. So we may
19045 end up accidently finding a declaration attribute that belongs
19046 to a different DIE referenced by the specification attribute,
19047 even though the given DIE does not have a declaration attribute. */
19048 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19049 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19050 }
19051
19052 /* Return the die giving the specification for DIE, if there is
19053 one. *SPEC_CU is the CU containing DIE on input, and the CU
19054 containing the return value on output. If there is no
19055 specification, but there is an abstract origin, that is
19056 returned. */
19057
19058 static struct die_info *
19059 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19060 {
19061 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19062 *spec_cu);
19063
19064 if (spec_attr == NULL)
19065 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19066
19067 if (spec_attr == NULL)
19068 return NULL;
19069 else
19070 return follow_die_ref (die, spec_attr, spec_cu);
19071 }
19072
19073 /* Stub for free_line_header to match void * callback types. */
19074
19075 static void
19076 free_line_header_voidp (void *arg)
19077 {
19078 struct line_header *lh = (struct line_header *) arg;
19079
19080 delete lh;
19081 }
19082
19083 /* A convenience function to find the proper .debug_line section for a CU. */
19084
19085 static struct dwarf2_section_info *
19086 get_debug_line_section (struct dwarf2_cu *cu)
19087 {
19088 struct dwarf2_section_info *section;
19089 struct dwarf2_per_objfile *dwarf2_per_objfile
19090 = cu->per_cu->dwarf2_per_objfile;
19091
19092 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19093 DWO file. */
19094 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19095 section = &cu->dwo_unit->dwo_file->sections.line;
19096 else if (cu->per_cu->is_dwz)
19097 {
19098 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19099
19100 section = &dwz->line;
19101 }
19102 else
19103 section = &dwarf2_per_objfile->line;
19104
19105 return section;
19106 }
19107
19108 /* Read the statement program header starting at OFFSET in
19109 .debug_line, or .debug_line.dwo. Return a pointer
19110 to a struct line_header, allocated using xmalloc.
19111 Returns NULL if there is a problem reading the header, e.g., if it
19112 has a version we don't understand.
19113
19114 NOTE: the strings in the include directory and file name tables of
19115 the returned object point into the dwarf line section buffer,
19116 and must not be freed. */
19117
19118 static line_header_up
19119 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19120 {
19121 struct dwarf2_section_info *section;
19122 struct dwarf2_per_objfile *dwarf2_per_objfile
19123 = cu->per_cu->dwarf2_per_objfile;
19124
19125 section = get_debug_line_section (cu);
19126 section->read (dwarf2_per_objfile->objfile);
19127 if (section->buffer == NULL)
19128 {
19129 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19130 complaint (_("missing .debug_line.dwo section"));
19131 else
19132 complaint (_("missing .debug_line section"));
19133 return 0;
19134 }
19135
19136 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19137 dwarf2_per_objfile, section,
19138 &cu->header);
19139 }
19140
19141 /* Subroutine of dwarf_decode_lines to simplify it.
19142 Return the file name of the psymtab for the given file_entry.
19143 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19144 If space for the result is malloc'd, *NAME_HOLDER will be set.
19145 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19146
19147 static const char *
19148 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19149 const dwarf2_psymtab *pst,
19150 const char *comp_dir,
19151 gdb::unique_xmalloc_ptr<char> *name_holder)
19152 {
19153 const char *include_name = fe.name;
19154 const char *include_name_to_compare = include_name;
19155 const char *pst_filename;
19156 int file_is_pst;
19157
19158 const char *dir_name = fe.include_dir (lh);
19159
19160 gdb::unique_xmalloc_ptr<char> hold_compare;
19161 if (!IS_ABSOLUTE_PATH (include_name)
19162 && (dir_name != NULL || comp_dir != NULL))
19163 {
19164 /* Avoid creating a duplicate psymtab for PST.
19165 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19166 Before we do the comparison, however, we need to account
19167 for DIR_NAME and COMP_DIR.
19168 First prepend dir_name (if non-NULL). If we still don't
19169 have an absolute path prepend comp_dir (if non-NULL).
19170 However, the directory we record in the include-file's
19171 psymtab does not contain COMP_DIR (to match the
19172 corresponding symtab(s)).
19173
19174 Example:
19175
19176 bash$ cd /tmp
19177 bash$ gcc -g ./hello.c
19178 include_name = "hello.c"
19179 dir_name = "."
19180 DW_AT_comp_dir = comp_dir = "/tmp"
19181 DW_AT_name = "./hello.c"
19182
19183 */
19184
19185 if (dir_name != NULL)
19186 {
19187 name_holder->reset (concat (dir_name, SLASH_STRING,
19188 include_name, (char *) NULL));
19189 include_name = name_holder->get ();
19190 include_name_to_compare = include_name;
19191 }
19192 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19193 {
19194 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19195 include_name, (char *) NULL));
19196 include_name_to_compare = hold_compare.get ();
19197 }
19198 }
19199
19200 pst_filename = pst->filename;
19201 gdb::unique_xmalloc_ptr<char> copied_name;
19202 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19203 {
19204 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19205 pst_filename, (char *) NULL));
19206 pst_filename = copied_name.get ();
19207 }
19208
19209 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19210
19211 if (file_is_pst)
19212 return NULL;
19213 return include_name;
19214 }
19215
19216 /* State machine to track the state of the line number program. */
19217
19218 class lnp_state_machine
19219 {
19220 public:
19221 /* Initialize a machine state for the start of a line number
19222 program. */
19223 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19224 bool record_lines_p);
19225
19226 file_entry *current_file ()
19227 {
19228 /* lh->file_names is 0-based, but the file name numbers in the
19229 statement program are 1-based. */
19230 return m_line_header->file_name_at (m_file);
19231 }
19232
19233 /* Record the line in the state machine. END_SEQUENCE is true if
19234 we're processing the end of a sequence. */
19235 void record_line (bool end_sequence);
19236
19237 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19238 nop-out rest of the lines in this sequence. */
19239 void check_line_address (struct dwarf2_cu *cu,
19240 const gdb_byte *line_ptr,
19241 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19242
19243 void handle_set_discriminator (unsigned int discriminator)
19244 {
19245 m_discriminator = discriminator;
19246 m_line_has_non_zero_discriminator |= discriminator != 0;
19247 }
19248
19249 /* Handle DW_LNE_set_address. */
19250 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19251 {
19252 m_op_index = 0;
19253 address += baseaddr;
19254 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19255 }
19256
19257 /* Handle DW_LNS_advance_pc. */
19258 void handle_advance_pc (CORE_ADDR adjust);
19259
19260 /* Handle a special opcode. */
19261 void handle_special_opcode (unsigned char op_code);
19262
19263 /* Handle DW_LNS_advance_line. */
19264 void handle_advance_line (int line_delta)
19265 {
19266 advance_line (line_delta);
19267 }
19268
19269 /* Handle DW_LNS_set_file. */
19270 void handle_set_file (file_name_index file);
19271
19272 /* Handle DW_LNS_negate_stmt. */
19273 void handle_negate_stmt ()
19274 {
19275 m_is_stmt = !m_is_stmt;
19276 }
19277
19278 /* Handle DW_LNS_const_add_pc. */
19279 void handle_const_add_pc ();
19280
19281 /* Handle DW_LNS_fixed_advance_pc. */
19282 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19283 {
19284 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19285 m_op_index = 0;
19286 }
19287
19288 /* Handle DW_LNS_copy. */
19289 void handle_copy ()
19290 {
19291 record_line (false);
19292 m_discriminator = 0;
19293 }
19294
19295 /* Handle DW_LNE_end_sequence. */
19296 void handle_end_sequence ()
19297 {
19298 m_currently_recording_lines = true;
19299 }
19300
19301 private:
19302 /* Advance the line by LINE_DELTA. */
19303 void advance_line (int line_delta)
19304 {
19305 m_line += line_delta;
19306
19307 if (line_delta != 0)
19308 m_line_has_non_zero_discriminator = m_discriminator != 0;
19309 }
19310
19311 struct dwarf2_cu *m_cu;
19312
19313 gdbarch *m_gdbarch;
19314
19315 /* True if we're recording lines.
19316 Otherwise we're building partial symtabs and are just interested in
19317 finding include files mentioned by the line number program. */
19318 bool m_record_lines_p;
19319
19320 /* The line number header. */
19321 line_header *m_line_header;
19322
19323 /* These are part of the standard DWARF line number state machine,
19324 and initialized according to the DWARF spec. */
19325
19326 unsigned char m_op_index = 0;
19327 /* The line table index of the current file. */
19328 file_name_index m_file = 1;
19329 unsigned int m_line = 1;
19330
19331 /* These are initialized in the constructor. */
19332
19333 CORE_ADDR m_address;
19334 bool m_is_stmt;
19335 unsigned int m_discriminator;
19336
19337 /* Additional bits of state we need to track. */
19338
19339 /* The last file that we called dwarf2_start_subfile for.
19340 This is only used for TLLs. */
19341 unsigned int m_last_file = 0;
19342 /* The last file a line number was recorded for. */
19343 struct subfile *m_last_subfile = NULL;
19344
19345 /* When true, record the lines we decode. */
19346 bool m_currently_recording_lines = false;
19347
19348 /* The last line number that was recorded, used to coalesce
19349 consecutive entries for the same line. This can happen, for
19350 example, when discriminators are present. PR 17276. */
19351 unsigned int m_last_line = 0;
19352 bool m_line_has_non_zero_discriminator = false;
19353 };
19354
19355 void
19356 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19357 {
19358 CORE_ADDR addr_adj = (((m_op_index + adjust)
19359 / m_line_header->maximum_ops_per_instruction)
19360 * m_line_header->minimum_instruction_length);
19361 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19362 m_op_index = ((m_op_index + adjust)
19363 % m_line_header->maximum_ops_per_instruction);
19364 }
19365
19366 void
19367 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19368 {
19369 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19370 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19371 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19372 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19373 / m_line_header->maximum_ops_per_instruction)
19374 * m_line_header->minimum_instruction_length);
19375 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19376 m_op_index = ((m_op_index + adj_opcode_d)
19377 % m_line_header->maximum_ops_per_instruction);
19378
19379 int line_delta = m_line_header->line_base + adj_opcode_r;
19380 advance_line (line_delta);
19381 record_line (false);
19382 m_discriminator = 0;
19383 }
19384
19385 void
19386 lnp_state_machine::handle_set_file (file_name_index file)
19387 {
19388 m_file = file;
19389
19390 const file_entry *fe = current_file ();
19391 if (fe == NULL)
19392 dwarf2_debug_line_missing_file_complaint ();
19393 else if (m_record_lines_p)
19394 {
19395 const char *dir = fe->include_dir (m_line_header);
19396
19397 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19398 m_line_has_non_zero_discriminator = m_discriminator != 0;
19399 dwarf2_start_subfile (m_cu, fe->name, dir);
19400 }
19401 }
19402
19403 void
19404 lnp_state_machine::handle_const_add_pc ()
19405 {
19406 CORE_ADDR adjust
19407 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19408
19409 CORE_ADDR addr_adj
19410 = (((m_op_index + adjust)
19411 / m_line_header->maximum_ops_per_instruction)
19412 * m_line_header->minimum_instruction_length);
19413
19414 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19415 m_op_index = ((m_op_index + adjust)
19416 % m_line_header->maximum_ops_per_instruction);
19417 }
19418
19419 /* Return non-zero if we should add LINE to the line number table.
19420 LINE is the line to add, LAST_LINE is the last line that was added,
19421 LAST_SUBFILE is the subfile for LAST_LINE.
19422 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19423 had a non-zero discriminator.
19424
19425 We have to be careful in the presence of discriminators.
19426 E.g., for this line:
19427
19428 for (i = 0; i < 100000; i++);
19429
19430 clang can emit four line number entries for that one line,
19431 each with a different discriminator.
19432 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19433
19434 However, we want gdb to coalesce all four entries into one.
19435 Otherwise the user could stepi into the middle of the line and
19436 gdb would get confused about whether the pc really was in the
19437 middle of the line.
19438
19439 Things are further complicated by the fact that two consecutive
19440 line number entries for the same line is a heuristic used by gcc
19441 to denote the end of the prologue. So we can't just discard duplicate
19442 entries, we have to be selective about it. The heuristic we use is
19443 that we only collapse consecutive entries for the same line if at least
19444 one of those entries has a non-zero discriminator. PR 17276.
19445
19446 Note: Addresses in the line number state machine can never go backwards
19447 within one sequence, thus this coalescing is ok. */
19448
19449 static int
19450 dwarf_record_line_p (struct dwarf2_cu *cu,
19451 unsigned int line, unsigned int last_line,
19452 int line_has_non_zero_discriminator,
19453 struct subfile *last_subfile)
19454 {
19455 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19456 return 1;
19457 if (line != last_line)
19458 return 1;
19459 /* Same line for the same file that we've seen already.
19460 As a last check, for pr 17276, only record the line if the line
19461 has never had a non-zero discriminator. */
19462 if (!line_has_non_zero_discriminator)
19463 return 1;
19464 return 0;
19465 }
19466
19467 /* Use the CU's builder to record line number LINE beginning at
19468 address ADDRESS in the line table of subfile SUBFILE. */
19469
19470 static void
19471 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19472 unsigned int line, CORE_ADDR address, bool is_stmt,
19473 struct dwarf2_cu *cu)
19474 {
19475 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19476
19477 if (dwarf_line_debug)
19478 {
19479 fprintf_unfiltered (gdb_stdlog,
19480 "Recording line %u, file %s, address %s\n",
19481 line, lbasename (subfile->name),
19482 paddress (gdbarch, address));
19483 }
19484
19485 if (cu != nullptr)
19486 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19487 }
19488
19489 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19490 Mark the end of a set of line number records.
19491 The arguments are the same as for dwarf_record_line_1.
19492 If SUBFILE is NULL the request is ignored. */
19493
19494 static void
19495 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19496 CORE_ADDR address, struct dwarf2_cu *cu)
19497 {
19498 if (subfile == NULL)
19499 return;
19500
19501 if (dwarf_line_debug)
19502 {
19503 fprintf_unfiltered (gdb_stdlog,
19504 "Finishing current line, file %s, address %s\n",
19505 lbasename (subfile->name),
19506 paddress (gdbarch, address));
19507 }
19508
19509 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19510 }
19511
19512 void
19513 lnp_state_machine::record_line (bool end_sequence)
19514 {
19515 if (dwarf_line_debug)
19516 {
19517 fprintf_unfiltered (gdb_stdlog,
19518 "Processing actual line %u: file %u,"
19519 " address %s, is_stmt %u, discrim %u%s\n",
19520 m_line, m_file,
19521 paddress (m_gdbarch, m_address),
19522 m_is_stmt, m_discriminator,
19523 (end_sequence ? "\t(end sequence)" : ""));
19524 }
19525
19526 file_entry *fe = current_file ();
19527
19528 if (fe == NULL)
19529 dwarf2_debug_line_missing_file_complaint ();
19530 /* For now we ignore lines not starting on an instruction boundary.
19531 But not when processing end_sequence for compatibility with the
19532 previous version of the code. */
19533 else if (m_op_index == 0 || end_sequence)
19534 {
19535 fe->included_p = 1;
19536 if (m_record_lines_p)
19537 {
19538 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19539 || end_sequence)
19540 {
19541 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19542 m_currently_recording_lines ? m_cu : nullptr);
19543 }
19544
19545 if (!end_sequence)
19546 {
19547 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19548
19549 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19550 m_line_has_non_zero_discriminator,
19551 m_last_subfile))
19552 {
19553 buildsym_compunit *builder = m_cu->get_builder ();
19554 dwarf_record_line_1 (m_gdbarch,
19555 builder->get_current_subfile (),
19556 m_line, m_address, is_stmt,
19557 m_currently_recording_lines ? m_cu : nullptr);
19558 }
19559 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19560 m_last_line = m_line;
19561 }
19562 }
19563 }
19564 }
19565
19566 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19567 line_header *lh, bool record_lines_p)
19568 {
19569 m_cu = cu;
19570 m_gdbarch = arch;
19571 m_record_lines_p = record_lines_p;
19572 m_line_header = lh;
19573
19574 m_currently_recording_lines = true;
19575
19576 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19577 was a line entry for it so that the backend has a chance to adjust it
19578 and also record it in case it needs it. This is currently used by MIPS
19579 code, cf. `mips_adjust_dwarf2_line'. */
19580 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19581 m_is_stmt = lh->default_is_stmt;
19582 m_discriminator = 0;
19583 }
19584
19585 void
19586 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19587 const gdb_byte *line_ptr,
19588 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19589 {
19590 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19591 the pc range of the CU. However, we restrict the test to only ADDRESS
19592 values of zero to preserve GDB's previous behaviour which is to handle
19593 the specific case of a function being GC'd by the linker. */
19594
19595 if (address == 0 && address < unrelocated_lowpc)
19596 {
19597 /* This line table is for a function which has been
19598 GCd by the linker. Ignore it. PR gdb/12528 */
19599
19600 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19601 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19602
19603 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19604 line_offset, objfile_name (objfile));
19605 m_currently_recording_lines = false;
19606 /* Note: m_currently_recording_lines is left as false until we see
19607 DW_LNE_end_sequence. */
19608 }
19609 }
19610
19611 /* Subroutine of dwarf_decode_lines to simplify it.
19612 Process the line number information in LH.
19613 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19614 program in order to set included_p for every referenced header. */
19615
19616 static void
19617 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19618 const int decode_for_pst_p, CORE_ADDR lowpc)
19619 {
19620 const gdb_byte *line_ptr, *extended_end;
19621 const gdb_byte *line_end;
19622 unsigned int bytes_read, extended_len;
19623 unsigned char op_code, extended_op;
19624 CORE_ADDR baseaddr;
19625 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19626 bfd *abfd = objfile->obfd;
19627 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19628 /* True if we're recording line info (as opposed to building partial
19629 symtabs and just interested in finding include files mentioned by
19630 the line number program). */
19631 bool record_lines_p = !decode_for_pst_p;
19632
19633 baseaddr = objfile->text_section_offset ();
19634
19635 line_ptr = lh->statement_program_start;
19636 line_end = lh->statement_program_end;
19637
19638 /* Read the statement sequences until there's nothing left. */
19639 while (line_ptr < line_end)
19640 {
19641 /* The DWARF line number program state machine. Reset the state
19642 machine at the start of each sequence. */
19643 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19644 bool end_sequence = false;
19645
19646 if (record_lines_p)
19647 {
19648 /* Start a subfile for the current file of the state
19649 machine. */
19650 const file_entry *fe = state_machine.current_file ();
19651
19652 if (fe != NULL)
19653 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19654 }
19655
19656 /* Decode the table. */
19657 while (line_ptr < line_end && !end_sequence)
19658 {
19659 op_code = read_1_byte (abfd, line_ptr);
19660 line_ptr += 1;
19661
19662 if (op_code >= lh->opcode_base)
19663 {
19664 /* Special opcode. */
19665 state_machine.handle_special_opcode (op_code);
19666 }
19667 else switch (op_code)
19668 {
19669 case DW_LNS_extended_op:
19670 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19671 &bytes_read);
19672 line_ptr += bytes_read;
19673 extended_end = line_ptr + extended_len;
19674 extended_op = read_1_byte (abfd, line_ptr);
19675 line_ptr += 1;
19676 switch (extended_op)
19677 {
19678 case DW_LNE_end_sequence:
19679 state_machine.handle_end_sequence ();
19680 end_sequence = true;
19681 break;
19682 case DW_LNE_set_address:
19683 {
19684 CORE_ADDR address
19685 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19686 line_ptr += bytes_read;
19687
19688 state_machine.check_line_address (cu, line_ptr,
19689 lowpc - baseaddr, address);
19690 state_machine.handle_set_address (baseaddr, address);
19691 }
19692 break;
19693 case DW_LNE_define_file:
19694 {
19695 const char *cur_file;
19696 unsigned int mod_time, length;
19697 dir_index dindex;
19698
19699 cur_file = read_direct_string (abfd, line_ptr,
19700 &bytes_read);
19701 line_ptr += bytes_read;
19702 dindex = (dir_index)
19703 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19704 line_ptr += bytes_read;
19705 mod_time =
19706 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19707 line_ptr += bytes_read;
19708 length =
19709 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19710 line_ptr += bytes_read;
19711 lh->add_file_name (cur_file, dindex, mod_time, length);
19712 }
19713 break;
19714 case DW_LNE_set_discriminator:
19715 {
19716 /* The discriminator is not interesting to the
19717 debugger; just ignore it. We still need to
19718 check its value though:
19719 if there are consecutive entries for the same
19720 (non-prologue) line we want to coalesce them.
19721 PR 17276. */
19722 unsigned int discr
19723 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19724 line_ptr += bytes_read;
19725
19726 state_machine.handle_set_discriminator (discr);
19727 }
19728 break;
19729 default:
19730 complaint (_("mangled .debug_line section"));
19731 return;
19732 }
19733 /* Make sure that we parsed the extended op correctly. If e.g.
19734 we expected a different address size than the producer used,
19735 we may have read the wrong number of bytes. */
19736 if (line_ptr != extended_end)
19737 {
19738 complaint (_("mangled .debug_line section"));
19739 return;
19740 }
19741 break;
19742 case DW_LNS_copy:
19743 state_machine.handle_copy ();
19744 break;
19745 case DW_LNS_advance_pc:
19746 {
19747 CORE_ADDR adjust
19748 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19749 line_ptr += bytes_read;
19750
19751 state_machine.handle_advance_pc (adjust);
19752 }
19753 break;
19754 case DW_LNS_advance_line:
19755 {
19756 int line_delta
19757 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19758 line_ptr += bytes_read;
19759
19760 state_machine.handle_advance_line (line_delta);
19761 }
19762 break;
19763 case DW_LNS_set_file:
19764 {
19765 file_name_index file
19766 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19767 &bytes_read);
19768 line_ptr += bytes_read;
19769
19770 state_machine.handle_set_file (file);
19771 }
19772 break;
19773 case DW_LNS_set_column:
19774 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19775 line_ptr += bytes_read;
19776 break;
19777 case DW_LNS_negate_stmt:
19778 state_machine.handle_negate_stmt ();
19779 break;
19780 case DW_LNS_set_basic_block:
19781 break;
19782 /* Add to the address register of the state machine the
19783 address increment value corresponding to special opcode
19784 255. I.e., this value is scaled by the minimum
19785 instruction length since special opcode 255 would have
19786 scaled the increment. */
19787 case DW_LNS_const_add_pc:
19788 state_machine.handle_const_add_pc ();
19789 break;
19790 case DW_LNS_fixed_advance_pc:
19791 {
19792 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19793 line_ptr += 2;
19794
19795 state_machine.handle_fixed_advance_pc (addr_adj);
19796 }
19797 break;
19798 default:
19799 {
19800 /* Unknown standard opcode, ignore it. */
19801 int i;
19802
19803 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19804 {
19805 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19806 line_ptr += bytes_read;
19807 }
19808 }
19809 }
19810 }
19811
19812 if (!end_sequence)
19813 dwarf2_debug_line_missing_end_sequence_complaint ();
19814
19815 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19816 in which case we still finish recording the last line). */
19817 state_machine.record_line (true);
19818 }
19819 }
19820
19821 /* Decode the Line Number Program (LNP) for the given line_header
19822 structure and CU. The actual information extracted and the type
19823 of structures created from the LNP depends on the value of PST.
19824
19825 1. If PST is NULL, then this procedure uses the data from the program
19826 to create all necessary symbol tables, and their linetables.
19827
19828 2. If PST is not NULL, this procedure reads the program to determine
19829 the list of files included by the unit represented by PST, and
19830 builds all the associated partial symbol tables.
19831
19832 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19833 It is used for relative paths in the line table.
19834 NOTE: When processing partial symtabs (pst != NULL),
19835 comp_dir == pst->dirname.
19836
19837 NOTE: It is important that psymtabs have the same file name (via strcmp)
19838 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19839 symtab we don't use it in the name of the psymtabs we create.
19840 E.g. expand_line_sal requires this when finding psymtabs to expand.
19841 A good testcase for this is mb-inline.exp.
19842
19843 LOWPC is the lowest address in CU (or 0 if not known).
19844
19845 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19846 for its PC<->lines mapping information. Otherwise only the filename
19847 table is read in. */
19848
19849 static void
19850 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19851 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19852 CORE_ADDR lowpc, int decode_mapping)
19853 {
19854 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19855 const int decode_for_pst_p = (pst != NULL);
19856
19857 if (decode_mapping)
19858 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19859
19860 if (decode_for_pst_p)
19861 {
19862 /* Now that we're done scanning the Line Header Program, we can
19863 create the psymtab of each included file. */
19864 for (auto &file_entry : lh->file_names ())
19865 if (file_entry.included_p == 1)
19866 {
19867 gdb::unique_xmalloc_ptr<char> name_holder;
19868 const char *include_name =
19869 psymtab_include_file_name (lh, file_entry, pst,
19870 comp_dir, &name_holder);
19871 if (include_name != NULL)
19872 dwarf2_create_include_psymtab (include_name, pst, objfile);
19873 }
19874 }
19875 else
19876 {
19877 /* Make sure a symtab is created for every file, even files
19878 which contain only variables (i.e. no code with associated
19879 line numbers). */
19880 buildsym_compunit *builder = cu->get_builder ();
19881 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19882
19883 for (auto &fe : lh->file_names ())
19884 {
19885 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19886 if (builder->get_current_subfile ()->symtab == NULL)
19887 {
19888 builder->get_current_subfile ()->symtab
19889 = allocate_symtab (cust,
19890 builder->get_current_subfile ()->name);
19891 }
19892 fe.symtab = builder->get_current_subfile ()->symtab;
19893 }
19894 }
19895 }
19896
19897 /* Start a subfile for DWARF. FILENAME is the name of the file and
19898 DIRNAME the name of the source directory which contains FILENAME
19899 or NULL if not known.
19900 This routine tries to keep line numbers from identical absolute and
19901 relative file names in a common subfile.
19902
19903 Using the `list' example from the GDB testsuite, which resides in
19904 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19905 of /srcdir/list0.c yields the following debugging information for list0.c:
19906
19907 DW_AT_name: /srcdir/list0.c
19908 DW_AT_comp_dir: /compdir
19909 files.files[0].name: list0.h
19910 files.files[0].dir: /srcdir
19911 files.files[1].name: list0.c
19912 files.files[1].dir: /srcdir
19913
19914 The line number information for list0.c has to end up in a single
19915 subfile, so that `break /srcdir/list0.c:1' works as expected.
19916 start_subfile will ensure that this happens provided that we pass the
19917 concatenation of files.files[1].dir and files.files[1].name as the
19918 subfile's name. */
19919
19920 static void
19921 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19922 const char *dirname)
19923 {
19924 gdb::unique_xmalloc_ptr<char> copy;
19925
19926 /* In order not to lose the line information directory,
19927 we concatenate it to the filename when it makes sense.
19928 Note that the Dwarf3 standard says (speaking of filenames in line
19929 information): ``The directory index is ignored for file names
19930 that represent full path names''. Thus ignoring dirname in the
19931 `else' branch below isn't an issue. */
19932
19933 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19934 {
19935 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19936 filename = copy.get ();
19937 }
19938
19939 cu->get_builder ()->start_subfile (filename);
19940 }
19941
19942 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19943 buildsym_compunit constructor. */
19944
19945 struct compunit_symtab *
19946 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19947 CORE_ADDR low_pc)
19948 {
19949 gdb_assert (m_builder == nullptr);
19950
19951 m_builder.reset (new struct buildsym_compunit
19952 (per_cu->dwarf2_per_objfile->objfile,
19953 name, comp_dir, language, low_pc));
19954
19955 list_in_scope = get_builder ()->get_file_symbols ();
19956
19957 get_builder ()->record_debugformat ("DWARF 2");
19958 get_builder ()->record_producer (producer);
19959
19960 processing_has_namespace_info = false;
19961
19962 return get_builder ()->get_compunit_symtab ();
19963 }
19964
19965 static void
19966 var_decode_location (struct attribute *attr, struct symbol *sym,
19967 struct dwarf2_cu *cu)
19968 {
19969 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19970 struct comp_unit_head *cu_header = &cu->header;
19971
19972 /* NOTE drow/2003-01-30: There used to be a comment and some special
19973 code here to turn a symbol with DW_AT_external and a
19974 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19975 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19976 with some versions of binutils) where shared libraries could have
19977 relocations against symbols in their debug information - the
19978 minimal symbol would have the right address, but the debug info
19979 would not. It's no longer necessary, because we will explicitly
19980 apply relocations when we read in the debug information now. */
19981
19982 /* A DW_AT_location attribute with no contents indicates that a
19983 variable has been optimized away. */
19984 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
19985 {
19986 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19987 return;
19988 }
19989
19990 /* Handle one degenerate form of location expression specially, to
19991 preserve GDB's previous behavior when section offsets are
19992 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19993 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
19994
19995 if (attr->form_is_block ()
19996 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19997 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19998 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19999 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20000 && (DW_BLOCK (attr)->size
20001 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20002 {
20003 unsigned int dummy;
20004
20005 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20006 SET_SYMBOL_VALUE_ADDRESS
20007 (sym, cu->header.read_address (objfile->obfd,
20008 DW_BLOCK (attr)->data + 1,
20009 &dummy));
20010 else
20011 SET_SYMBOL_VALUE_ADDRESS
20012 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20013 &dummy));
20014 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20015 fixup_symbol_section (sym, objfile);
20016 SET_SYMBOL_VALUE_ADDRESS
20017 (sym,
20018 SYMBOL_VALUE_ADDRESS (sym)
20019 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20020 return;
20021 }
20022
20023 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20024 expression evaluator, and use LOC_COMPUTED only when necessary
20025 (i.e. when the value of a register or memory location is
20026 referenced, or a thread-local block, etc.). Then again, it might
20027 not be worthwhile. I'm assuming that it isn't unless performance
20028 or memory numbers show me otherwise. */
20029
20030 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20031
20032 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20033 cu->has_loclist = true;
20034 }
20035
20036 /* Given a pointer to a DWARF information entry, figure out if we need
20037 to make a symbol table entry for it, and if so, create a new entry
20038 and return a pointer to it.
20039 If TYPE is NULL, determine symbol type from the die, otherwise
20040 used the passed type.
20041 If SPACE is not NULL, use it to hold the new symbol. If it is
20042 NULL, allocate a new symbol on the objfile's obstack. */
20043
20044 static struct symbol *
20045 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20046 struct symbol *space)
20047 {
20048 struct dwarf2_per_objfile *dwarf2_per_objfile
20049 = cu->per_cu->dwarf2_per_objfile;
20050 struct objfile *objfile = dwarf2_per_objfile->objfile;
20051 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20052 struct symbol *sym = NULL;
20053 const char *name;
20054 struct attribute *attr = NULL;
20055 struct attribute *attr2 = NULL;
20056 CORE_ADDR baseaddr;
20057 struct pending **list_to_add = NULL;
20058
20059 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20060
20061 baseaddr = objfile->text_section_offset ();
20062
20063 name = dwarf2_name (die, cu);
20064 if (name)
20065 {
20066 const char *linkagename;
20067 int suppress_add = 0;
20068
20069 if (space)
20070 sym = space;
20071 else
20072 sym = allocate_symbol (objfile);
20073 OBJSTAT (objfile, n_syms++);
20074
20075 /* Cache this symbol's name and the name's demangled form (if any). */
20076 sym->set_language (cu->language, &objfile->objfile_obstack);
20077 linkagename = dwarf2_physname (name, die, cu);
20078 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20079
20080 /* Fortran does not have mangling standard and the mangling does differ
20081 between gfortran, iFort etc. */
20082 if (cu->language == language_fortran
20083 && symbol_get_demangled_name (sym) == NULL)
20084 symbol_set_demangled_name (sym,
20085 dwarf2_full_name (name, die, cu),
20086 NULL);
20087
20088 /* Default assumptions.
20089 Use the passed type or decode it from the die. */
20090 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20091 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20092 if (type != NULL)
20093 SYMBOL_TYPE (sym) = type;
20094 else
20095 SYMBOL_TYPE (sym) = die_type (die, cu);
20096 attr = dwarf2_attr (die,
20097 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20098 cu);
20099 if (attr != nullptr)
20100 {
20101 SYMBOL_LINE (sym) = DW_UNSND (attr);
20102 }
20103
20104 attr = dwarf2_attr (die,
20105 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20106 cu);
20107 if (attr != nullptr)
20108 {
20109 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20110 struct file_entry *fe;
20111
20112 if (cu->line_header != NULL)
20113 fe = cu->line_header->file_name_at (file_index);
20114 else
20115 fe = NULL;
20116
20117 if (fe == NULL)
20118 complaint (_("file index out of range"));
20119 else
20120 symbol_set_symtab (sym, fe->symtab);
20121 }
20122
20123 switch (die->tag)
20124 {
20125 case DW_TAG_label:
20126 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20127 if (attr != nullptr)
20128 {
20129 CORE_ADDR addr;
20130
20131 addr = attr->value_as_address ();
20132 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20133 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20134 }
20135 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20136 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20137 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20138 add_symbol_to_list (sym, cu->list_in_scope);
20139 break;
20140 case DW_TAG_subprogram:
20141 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20142 finish_block. */
20143 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20144 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20145 if ((attr2 && (DW_UNSND (attr2) != 0))
20146 || cu->language == language_ada
20147 || cu->language == language_fortran)
20148 {
20149 /* Subprograms marked external are stored as a global symbol.
20150 Ada and Fortran subprograms, whether marked external or
20151 not, are always stored as a global symbol, because we want
20152 to be able to access them globally. For instance, we want
20153 to be able to break on a nested subprogram without having
20154 to specify the context. */
20155 list_to_add = cu->get_builder ()->get_global_symbols ();
20156 }
20157 else
20158 {
20159 list_to_add = cu->list_in_scope;
20160 }
20161 break;
20162 case DW_TAG_inlined_subroutine:
20163 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20164 finish_block. */
20165 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20166 SYMBOL_INLINED (sym) = 1;
20167 list_to_add = cu->list_in_scope;
20168 break;
20169 case DW_TAG_template_value_param:
20170 suppress_add = 1;
20171 /* Fall through. */
20172 case DW_TAG_constant:
20173 case DW_TAG_variable:
20174 case DW_TAG_member:
20175 /* Compilation with minimal debug info may result in
20176 variables with missing type entries. Change the
20177 misleading `void' type to something sensible. */
20178 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20179 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20180
20181 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20182 /* In the case of DW_TAG_member, we should only be called for
20183 static const members. */
20184 if (die->tag == DW_TAG_member)
20185 {
20186 /* dwarf2_add_field uses die_is_declaration,
20187 so we do the same. */
20188 gdb_assert (die_is_declaration (die, cu));
20189 gdb_assert (attr);
20190 }
20191 if (attr != nullptr)
20192 {
20193 dwarf2_const_value (attr, sym, cu);
20194 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20195 if (!suppress_add)
20196 {
20197 if (attr2 && (DW_UNSND (attr2) != 0))
20198 list_to_add = cu->get_builder ()->get_global_symbols ();
20199 else
20200 list_to_add = cu->list_in_scope;
20201 }
20202 break;
20203 }
20204 attr = dwarf2_attr (die, DW_AT_location, cu);
20205 if (attr != nullptr)
20206 {
20207 var_decode_location (attr, sym, cu);
20208 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20209
20210 /* Fortran explicitly imports any global symbols to the local
20211 scope by DW_TAG_common_block. */
20212 if (cu->language == language_fortran && die->parent
20213 && die->parent->tag == DW_TAG_common_block)
20214 attr2 = NULL;
20215
20216 if (SYMBOL_CLASS (sym) == LOC_STATIC
20217 && SYMBOL_VALUE_ADDRESS (sym) == 0
20218 && !dwarf2_per_objfile->has_section_at_zero)
20219 {
20220 /* When a static variable is eliminated by the linker,
20221 the corresponding debug information is not stripped
20222 out, but the variable address is set to null;
20223 do not add such variables into symbol table. */
20224 }
20225 else if (attr2 && (DW_UNSND (attr2) != 0))
20226 {
20227 if (SYMBOL_CLASS (sym) == LOC_STATIC
20228 && (objfile->flags & OBJF_MAINLINE) == 0
20229 && dwarf2_per_objfile->can_copy)
20230 {
20231 /* A global static variable might be subject to
20232 copy relocation. We first check for a local
20233 minsym, though, because maybe the symbol was
20234 marked hidden, in which case this would not
20235 apply. */
20236 bound_minimal_symbol found
20237 = (lookup_minimal_symbol_linkage
20238 (sym->linkage_name (), objfile));
20239 if (found.minsym != nullptr)
20240 sym->maybe_copied = 1;
20241 }
20242
20243 /* A variable with DW_AT_external is never static,
20244 but it may be block-scoped. */
20245 list_to_add
20246 = ((cu->list_in_scope
20247 == cu->get_builder ()->get_file_symbols ())
20248 ? cu->get_builder ()->get_global_symbols ()
20249 : cu->list_in_scope);
20250 }
20251 else
20252 list_to_add = cu->list_in_scope;
20253 }
20254 else
20255 {
20256 /* We do not know the address of this symbol.
20257 If it is an external symbol and we have type information
20258 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20259 The address of the variable will then be determined from
20260 the minimal symbol table whenever the variable is
20261 referenced. */
20262 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20263
20264 /* Fortran explicitly imports any global symbols to the local
20265 scope by DW_TAG_common_block. */
20266 if (cu->language == language_fortran && die->parent
20267 && die->parent->tag == DW_TAG_common_block)
20268 {
20269 /* SYMBOL_CLASS doesn't matter here because
20270 read_common_block is going to reset it. */
20271 if (!suppress_add)
20272 list_to_add = cu->list_in_scope;
20273 }
20274 else if (attr2 && (DW_UNSND (attr2) != 0)
20275 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20276 {
20277 /* A variable with DW_AT_external is never static, but it
20278 may be block-scoped. */
20279 list_to_add
20280 = ((cu->list_in_scope
20281 == cu->get_builder ()->get_file_symbols ())
20282 ? cu->get_builder ()->get_global_symbols ()
20283 : cu->list_in_scope);
20284
20285 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20286 }
20287 else if (!die_is_declaration (die, cu))
20288 {
20289 /* Use the default LOC_OPTIMIZED_OUT class. */
20290 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20291 if (!suppress_add)
20292 list_to_add = cu->list_in_scope;
20293 }
20294 }
20295 break;
20296 case DW_TAG_formal_parameter:
20297 {
20298 /* If we are inside a function, mark this as an argument. If
20299 not, we might be looking at an argument to an inlined function
20300 when we do not have enough information to show inlined frames;
20301 pretend it's a local variable in that case so that the user can
20302 still see it. */
20303 struct context_stack *curr
20304 = cu->get_builder ()->get_current_context_stack ();
20305 if (curr != nullptr && curr->name != nullptr)
20306 SYMBOL_IS_ARGUMENT (sym) = 1;
20307 attr = dwarf2_attr (die, DW_AT_location, cu);
20308 if (attr != nullptr)
20309 {
20310 var_decode_location (attr, sym, cu);
20311 }
20312 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20313 if (attr != nullptr)
20314 {
20315 dwarf2_const_value (attr, sym, cu);
20316 }
20317
20318 list_to_add = cu->list_in_scope;
20319 }
20320 break;
20321 case DW_TAG_unspecified_parameters:
20322 /* From varargs functions; gdb doesn't seem to have any
20323 interest in this information, so just ignore it for now.
20324 (FIXME?) */
20325 break;
20326 case DW_TAG_template_type_param:
20327 suppress_add = 1;
20328 /* Fall through. */
20329 case DW_TAG_class_type:
20330 case DW_TAG_interface_type:
20331 case DW_TAG_structure_type:
20332 case DW_TAG_union_type:
20333 case DW_TAG_set_type:
20334 case DW_TAG_enumeration_type:
20335 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20336 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20337
20338 {
20339 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20340 really ever be static objects: otherwise, if you try
20341 to, say, break of a class's method and you're in a file
20342 which doesn't mention that class, it won't work unless
20343 the check for all static symbols in lookup_symbol_aux
20344 saves you. See the OtherFileClass tests in
20345 gdb.c++/namespace.exp. */
20346
20347 if (!suppress_add)
20348 {
20349 buildsym_compunit *builder = cu->get_builder ();
20350 list_to_add
20351 = (cu->list_in_scope == builder->get_file_symbols ()
20352 && cu->language == language_cplus
20353 ? builder->get_global_symbols ()
20354 : cu->list_in_scope);
20355
20356 /* The semantics of C++ state that "struct foo {
20357 ... }" also defines a typedef for "foo". */
20358 if (cu->language == language_cplus
20359 || cu->language == language_ada
20360 || cu->language == language_d
20361 || cu->language == language_rust)
20362 {
20363 /* The symbol's name is already allocated along
20364 with this objfile, so we don't need to
20365 duplicate it for the type. */
20366 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20367 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20368 }
20369 }
20370 }
20371 break;
20372 case DW_TAG_typedef:
20373 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20374 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20375 list_to_add = cu->list_in_scope;
20376 break;
20377 case DW_TAG_base_type:
20378 case DW_TAG_subrange_type:
20379 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20380 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20381 list_to_add = cu->list_in_scope;
20382 break;
20383 case DW_TAG_enumerator:
20384 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20385 if (attr != nullptr)
20386 {
20387 dwarf2_const_value (attr, sym, cu);
20388 }
20389 {
20390 /* NOTE: carlton/2003-11-10: See comment above in the
20391 DW_TAG_class_type, etc. block. */
20392
20393 list_to_add
20394 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20395 && cu->language == language_cplus
20396 ? cu->get_builder ()->get_global_symbols ()
20397 : cu->list_in_scope);
20398 }
20399 break;
20400 case DW_TAG_imported_declaration:
20401 case DW_TAG_namespace:
20402 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20403 list_to_add = cu->get_builder ()->get_global_symbols ();
20404 break;
20405 case DW_TAG_module:
20406 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20407 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20408 list_to_add = cu->get_builder ()->get_global_symbols ();
20409 break;
20410 case DW_TAG_common_block:
20411 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20412 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20413 add_symbol_to_list (sym, cu->list_in_scope);
20414 break;
20415 default:
20416 /* Not a tag we recognize. Hopefully we aren't processing
20417 trash data, but since we must specifically ignore things
20418 we don't recognize, there is nothing else we should do at
20419 this point. */
20420 complaint (_("unsupported tag: '%s'"),
20421 dwarf_tag_name (die->tag));
20422 break;
20423 }
20424
20425 if (suppress_add)
20426 {
20427 sym->hash_next = objfile->template_symbols;
20428 objfile->template_symbols = sym;
20429 list_to_add = NULL;
20430 }
20431
20432 if (list_to_add != NULL)
20433 add_symbol_to_list (sym, list_to_add);
20434
20435 /* For the benefit of old versions of GCC, check for anonymous
20436 namespaces based on the demangled name. */
20437 if (!cu->processing_has_namespace_info
20438 && cu->language == language_cplus)
20439 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20440 }
20441 return (sym);
20442 }
20443
20444 /* Given an attr with a DW_FORM_dataN value in host byte order,
20445 zero-extend it as appropriate for the symbol's type. The DWARF
20446 standard (v4) is not entirely clear about the meaning of using
20447 DW_FORM_dataN for a constant with a signed type, where the type is
20448 wider than the data. The conclusion of a discussion on the DWARF
20449 list was that this is unspecified. We choose to always zero-extend
20450 because that is the interpretation long in use by GCC. */
20451
20452 static gdb_byte *
20453 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20454 struct dwarf2_cu *cu, LONGEST *value, int bits)
20455 {
20456 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20457 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20458 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20459 LONGEST l = DW_UNSND (attr);
20460
20461 if (bits < sizeof (*value) * 8)
20462 {
20463 l &= ((LONGEST) 1 << bits) - 1;
20464 *value = l;
20465 }
20466 else if (bits == sizeof (*value) * 8)
20467 *value = l;
20468 else
20469 {
20470 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20471 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20472 return bytes;
20473 }
20474
20475 return NULL;
20476 }
20477
20478 /* Read a constant value from an attribute. Either set *VALUE, or if
20479 the value does not fit in *VALUE, set *BYTES - either already
20480 allocated on the objfile obstack, or newly allocated on OBSTACK,
20481 or, set *BATON, if we translated the constant to a location
20482 expression. */
20483
20484 static void
20485 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20486 const char *name, struct obstack *obstack,
20487 struct dwarf2_cu *cu,
20488 LONGEST *value, const gdb_byte **bytes,
20489 struct dwarf2_locexpr_baton **baton)
20490 {
20491 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20492 struct comp_unit_head *cu_header = &cu->header;
20493 struct dwarf_block *blk;
20494 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20495 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20496
20497 *value = 0;
20498 *bytes = NULL;
20499 *baton = NULL;
20500
20501 switch (attr->form)
20502 {
20503 case DW_FORM_addr:
20504 case DW_FORM_addrx:
20505 case DW_FORM_GNU_addr_index:
20506 {
20507 gdb_byte *data;
20508
20509 if (TYPE_LENGTH (type) != cu_header->addr_size)
20510 dwarf2_const_value_length_mismatch_complaint (name,
20511 cu_header->addr_size,
20512 TYPE_LENGTH (type));
20513 /* Symbols of this form are reasonably rare, so we just
20514 piggyback on the existing location code rather than writing
20515 a new implementation of symbol_computed_ops. */
20516 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20517 (*baton)->per_cu = cu->per_cu;
20518 gdb_assert ((*baton)->per_cu);
20519
20520 (*baton)->size = 2 + cu_header->addr_size;
20521 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20522 (*baton)->data = data;
20523
20524 data[0] = DW_OP_addr;
20525 store_unsigned_integer (&data[1], cu_header->addr_size,
20526 byte_order, DW_ADDR (attr));
20527 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20528 }
20529 break;
20530 case DW_FORM_string:
20531 case DW_FORM_strp:
20532 case DW_FORM_strx:
20533 case DW_FORM_GNU_str_index:
20534 case DW_FORM_GNU_strp_alt:
20535 /* DW_STRING is already allocated on the objfile obstack, point
20536 directly to it. */
20537 *bytes = (const gdb_byte *) DW_STRING (attr);
20538 break;
20539 case DW_FORM_block1:
20540 case DW_FORM_block2:
20541 case DW_FORM_block4:
20542 case DW_FORM_block:
20543 case DW_FORM_exprloc:
20544 case DW_FORM_data16:
20545 blk = DW_BLOCK (attr);
20546 if (TYPE_LENGTH (type) != blk->size)
20547 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20548 TYPE_LENGTH (type));
20549 *bytes = blk->data;
20550 break;
20551
20552 /* The DW_AT_const_value attributes are supposed to carry the
20553 symbol's value "represented as it would be on the target
20554 architecture." By the time we get here, it's already been
20555 converted to host endianness, so we just need to sign- or
20556 zero-extend it as appropriate. */
20557 case DW_FORM_data1:
20558 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20559 break;
20560 case DW_FORM_data2:
20561 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20562 break;
20563 case DW_FORM_data4:
20564 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20565 break;
20566 case DW_FORM_data8:
20567 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20568 break;
20569
20570 case DW_FORM_sdata:
20571 case DW_FORM_implicit_const:
20572 *value = DW_SND (attr);
20573 break;
20574
20575 case DW_FORM_udata:
20576 *value = DW_UNSND (attr);
20577 break;
20578
20579 default:
20580 complaint (_("unsupported const value attribute form: '%s'"),
20581 dwarf_form_name (attr->form));
20582 *value = 0;
20583 break;
20584 }
20585 }
20586
20587
20588 /* Copy constant value from an attribute to a symbol. */
20589
20590 static void
20591 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20592 struct dwarf2_cu *cu)
20593 {
20594 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20595 LONGEST value;
20596 const gdb_byte *bytes;
20597 struct dwarf2_locexpr_baton *baton;
20598
20599 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20600 sym->print_name (),
20601 &objfile->objfile_obstack, cu,
20602 &value, &bytes, &baton);
20603
20604 if (baton != NULL)
20605 {
20606 SYMBOL_LOCATION_BATON (sym) = baton;
20607 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20608 }
20609 else if (bytes != NULL)
20610 {
20611 SYMBOL_VALUE_BYTES (sym) = bytes;
20612 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20613 }
20614 else
20615 {
20616 SYMBOL_VALUE (sym) = value;
20617 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20618 }
20619 }
20620
20621 /* Return the type of the die in question using its DW_AT_type attribute. */
20622
20623 static struct type *
20624 die_type (struct die_info *die, struct dwarf2_cu *cu)
20625 {
20626 struct attribute *type_attr;
20627
20628 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20629 if (!type_attr)
20630 {
20631 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20632 /* A missing DW_AT_type represents a void type. */
20633 return objfile_type (objfile)->builtin_void;
20634 }
20635
20636 return lookup_die_type (die, type_attr, cu);
20637 }
20638
20639 /* True iff CU's producer generates GNAT Ada auxiliary information
20640 that allows to find parallel types through that information instead
20641 of having to do expensive parallel lookups by type name. */
20642
20643 static int
20644 need_gnat_info (struct dwarf2_cu *cu)
20645 {
20646 /* Assume that the Ada compiler was GNAT, which always produces
20647 the auxiliary information. */
20648 return (cu->language == language_ada);
20649 }
20650
20651 /* Return the auxiliary type of the die in question using its
20652 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20653 attribute is not present. */
20654
20655 static struct type *
20656 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20657 {
20658 struct attribute *type_attr;
20659
20660 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20661 if (!type_attr)
20662 return NULL;
20663
20664 return lookup_die_type (die, type_attr, cu);
20665 }
20666
20667 /* If DIE has a descriptive_type attribute, then set the TYPE's
20668 descriptive type accordingly. */
20669
20670 static void
20671 set_descriptive_type (struct type *type, struct die_info *die,
20672 struct dwarf2_cu *cu)
20673 {
20674 struct type *descriptive_type = die_descriptive_type (die, cu);
20675
20676 if (descriptive_type)
20677 {
20678 ALLOCATE_GNAT_AUX_TYPE (type);
20679 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20680 }
20681 }
20682
20683 /* Return the containing type of the die in question using its
20684 DW_AT_containing_type attribute. */
20685
20686 static struct type *
20687 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20688 {
20689 struct attribute *type_attr;
20690 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20691
20692 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20693 if (!type_attr)
20694 error (_("Dwarf Error: Problem turning containing type into gdb type "
20695 "[in module %s]"), objfile_name (objfile));
20696
20697 return lookup_die_type (die, type_attr, cu);
20698 }
20699
20700 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20701
20702 static struct type *
20703 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20704 {
20705 struct dwarf2_per_objfile *dwarf2_per_objfile
20706 = cu->per_cu->dwarf2_per_objfile;
20707 struct objfile *objfile = dwarf2_per_objfile->objfile;
20708 char *saved;
20709
20710 std::string message
20711 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20712 objfile_name (objfile),
20713 sect_offset_str (cu->header.sect_off),
20714 sect_offset_str (die->sect_off));
20715 saved = obstack_strdup (&objfile->objfile_obstack, message);
20716
20717 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20718 }
20719
20720 /* Look up the type of DIE in CU using its type attribute ATTR.
20721 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20722 DW_AT_containing_type.
20723 If there is no type substitute an error marker. */
20724
20725 static struct type *
20726 lookup_die_type (struct die_info *die, const struct attribute *attr,
20727 struct dwarf2_cu *cu)
20728 {
20729 struct dwarf2_per_objfile *dwarf2_per_objfile
20730 = cu->per_cu->dwarf2_per_objfile;
20731 struct objfile *objfile = dwarf2_per_objfile->objfile;
20732 struct type *this_type;
20733
20734 gdb_assert (attr->name == DW_AT_type
20735 || attr->name == DW_AT_GNAT_descriptive_type
20736 || attr->name == DW_AT_containing_type);
20737
20738 /* First see if we have it cached. */
20739
20740 if (attr->form == DW_FORM_GNU_ref_alt)
20741 {
20742 struct dwarf2_per_cu_data *per_cu;
20743 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20744
20745 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20746 dwarf2_per_objfile);
20747 this_type = get_die_type_at_offset (sect_off, per_cu);
20748 }
20749 else if (attr->form_is_ref ())
20750 {
20751 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20752
20753 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20754 }
20755 else if (attr->form == DW_FORM_ref_sig8)
20756 {
20757 ULONGEST signature = DW_SIGNATURE (attr);
20758
20759 return get_signatured_type (die, signature, cu);
20760 }
20761 else
20762 {
20763 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20764 " at %s [in module %s]"),
20765 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20766 objfile_name (objfile));
20767 return build_error_marker_type (cu, die);
20768 }
20769
20770 /* If not cached we need to read it in. */
20771
20772 if (this_type == NULL)
20773 {
20774 struct die_info *type_die = NULL;
20775 struct dwarf2_cu *type_cu = cu;
20776
20777 if (attr->form_is_ref ())
20778 type_die = follow_die_ref (die, attr, &type_cu);
20779 if (type_die == NULL)
20780 return build_error_marker_type (cu, die);
20781 /* If we find the type now, it's probably because the type came
20782 from an inter-CU reference and the type's CU got expanded before
20783 ours. */
20784 this_type = read_type_die (type_die, type_cu);
20785 }
20786
20787 /* If we still don't have a type use an error marker. */
20788
20789 if (this_type == NULL)
20790 return build_error_marker_type (cu, die);
20791
20792 return this_type;
20793 }
20794
20795 /* Return the type in DIE, CU.
20796 Returns NULL for invalid types.
20797
20798 This first does a lookup in die_type_hash,
20799 and only reads the die in if necessary.
20800
20801 NOTE: This can be called when reading in partial or full symbols. */
20802
20803 static struct type *
20804 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20805 {
20806 struct type *this_type;
20807
20808 this_type = get_die_type (die, cu);
20809 if (this_type)
20810 return this_type;
20811
20812 return read_type_die_1 (die, cu);
20813 }
20814
20815 /* Read the type in DIE, CU.
20816 Returns NULL for invalid types. */
20817
20818 static struct type *
20819 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20820 {
20821 struct type *this_type = NULL;
20822
20823 switch (die->tag)
20824 {
20825 case DW_TAG_class_type:
20826 case DW_TAG_interface_type:
20827 case DW_TAG_structure_type:
20828 case DW_TAG_union_type:
20829 this_type = read_structure_type (die, cu);
20830 break;
20831 case DW_TAG_enumeration_type:
20832 this_type = read_enumeration_type (die, cu);
20833 break;
20834 case DW_TAG_subprogram:
20835 case DW_TAG_subroutine_type:
20836 case DW_TAG_inlined_subroutine:
20837 this_type = read_subroutine_type (die, cu);
20838 break;
20839 case DW_TAG_array_type:
20840 this_type = read_array_type (die, cu);
20841 break;
20842 case DW_TAG_set_type:
20843 this_type = read_set_type (die, cu);
20844 break;
20845 case DW_TAG_pointer_type:
20846 this_type = read_tag_pointer_type (die, cu);
20847 break;
20848 case DW_TAG_ptr_to_member_type:
20849 this_type = read_tag_ptr_to_member_type (die, cu);
20850 break;
20851 case DW_TAG_reference_type:
20852 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20853 break;
20854 case DW_TAG_rvalue_reference_type:
20855 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20856 break;
20857 case DW_TAG_const_type:
20858 this_type = read_tag_const_type (die, cu);
20859 break;
20860 case DW_TAG_volatile_type:
20861 this_type = read_tag_volatile_type (die, cu);
20862 break;
20863 case DW_TAG_restrict_type:
20864 this_type = read_tag_restrict_type (die, cu);
20865 break;
20866 case DW_TAG_string_type:
20867 this_type = read_tag_string_type (die, cu);
20868 break;
20869 case DW_TAG_typedef:
20870 this_type = read_typedef (die, cu);
20871 break;
20872 case DW_TAG_subrange_type:
20873 this_type = read_subrange_type (die, cu);
20874 break;
20875 case DW_TAG_base_type:
20876 this_type = read_base_type (die, cu);
20877 break;
20878 case DW_TAG_unspecified_type:
20879 this_type = read_unspecified_type (die, cu);
20880 break;
20881 case DW_TAG_namespace:
20882 this_type = read_namespace_type (die, cu);
20883 break;
20884 case DW_TAG_module:
20885 this_type = read_module_type (die, cu);
20886 break;
20887 case DW_TAG_atomic_type:
20888 this_type = read_tag_atomic_type (die, cu);
20889 break;
20890 default:
20891 complaint (_("unexpected tag in read_type_die: '%s'"),
20892 dwarf_tag_name (die->tag));
20893 break;
20894 }
20895
20896 return this_type;
20897 }
20898
20899 /* See if we can figure out if the class lives in a namespace. We do
20900 this by looking for a member function; its demangled name will
20901 contain namespace info, if there is any.
20902 Return the computed name or NULL.
20903 Space for the result is allocated on the objfile's obstack.
20904 This is the full-die version of guess_partial_die_structure_name.
20905 In this case we know DIE has no useful parent. */
20906
20907 static const char *
20908 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20909 {
20910 struct die_info *spec_die;
20911 struct dwarf2_cu *spec_cu;
20912 struct die_info *child;
20913 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20914
20915 spec_cu = cu;
20916 spec_die = die_specification (die, &spec_cu);
20917 if (spec_die != NULL)
20918 {
20919 die = spec_die;
20920 cu = spec_cu;
20921 }
20922
20923 for (child = die->child;
20924 child != NULL;
20925 child = child->sibling)
20926 {
20927 if (child->tag == DW_TAG_subprogram)
20928 {
20929 const char *linkage_name = dw2_linkage_name (child, cu);
20930
20931 if (linkage_name != NULL)
20932 {
20933 gdb::unique_xmalloc_ptr<char> actual_name
20934 (language_class_name_from_physname (cu->language_defn,
20935 linkage_name));
20936 const char *name = NULL;
20937
20938 if (actual_name != NULL)
20939 {
20940 const char *die_name = dwarf2_name (die, cu);
20941
20942 if (die_name != NULL
20943 && strcmp (die_name, actual_name.get ()) != 0)
20944 {
20945 /* Strip off the class name from the full name.
20946 We want the prefix. */
20947 int die_name_len = strlen (die_name);
20948 int actual_name_len = strlen (actual_name.get ());
20949 const char *ptr = actual_name.get ();
20950
20951 /* Test for '::' as a sanity check. */
20952 if (actual_name_len > die_name_len + 2
20953 && ptr[actual_name_len - die_name_len - 1] == ':')
20954 name = obstack_strndup (
20955 &objfile->per_bfd->storage_obstack,
20956 ptr, actual_name_len - die_name_len - 2);
20957 }
20958 }
20959 return name;
20960 }
20961 }
20962 }
20963
20964 return NULL;
20965 }
20966
20967 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20968 prefix part in such case. See
20969 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20970
20971 static const char *
20972 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20973 {
20974 struct attribute *attr;
20975 const char *base;
20976
20977 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20978 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20979 return NULL;
20980
20981 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20982 return NULL;
20983
20984 attr = dw2_linkage_name_attr (die, cu);
20985 if (attr == NULL || DW_STRING (attr) == NULL)
20986 return NULL;
20987
20988 /* dwarf2_name had to be already called. */
20989 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20990
20991 /* Strip the base name, keep any leading namespaces/classes. */
20992 base = strrchr (DW_STRING (attr), ':');
20993 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20994 return "";
20995
20996 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20997 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20998 DW_STRING (attr),
20999 &base[-1] - DW_STRING (attr));
21000 }
21001
21002 /* Return the name of the namespace/class that DIE is defined within,
21003 or "" if we can't tell. The caller should not xfree the result.
21004
21005 For example, if we're within the method foo() in the following
21006 code:
21007
21008 namespace N {
21009 class C {
21010 void foo () {
21011 }
21012 };
21013 }
21014
21015 then determine_prefix on foo's die will return "N::C". */
21016
21017 static const char *
21018 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21019 {
21020 struct dwarf2_per_objfile *dwarf2_per_objfile
21021 = cu->per_cu->dwarf2_per_objfile;
21022 struct die_info *parent, *spec_die;
21023 struct dwarf2_cu *spec_cu;
21024 struct type *parent_type;
21025 const char *retval;
21026
21027 if (cu->language != language_cplus
21028 && cu->language != language_fortran && cu->language != language_d
21029 && cu->language != language_rust)
21030 return "";
21031
21032 retval = anonymous_struct_prefix (die, cu);
21033 if (retval)
21034 return retval;
21035
21036 /* We have to be careful in the presence of DW_AT_specification.
21037 For example, with GCC 3.4, given the code
21038
21039 namespace N {
21040 void foo() {
21041 // Definition of N::foo.
21042 }
21043 }
21044
21045 then we'll have a tree of DIEs like this:
21046
21047 1: DW_TAG_compile_unit
21048 2: DW_TAG_namespace // N
21049 3: DW_TAG_subprogram // declaration of N::foo
21050 4: DW_TAG_subprogram // definition of N::foo
21051 DW_AT_specification // refers to die #3
21052
21053 Thus, when processing die #4, we have to pretend that we're in
21054 the context of its DW_AT_specification, namely the contex of die
21055 #3. */
21056 spec_cu = cu;
21057 spec_die = die_specification (die, &spec_cu);
21058 if (spec_die == NULL)
21059 parent = die->parent;
21060 else
21061 {
21062 parent = spec_die->parent;
21063 cu = spec_cu;
21064 }
21065
21066 if (parent == NULL)
21067 return "";
21068 else if (parent->building_fullname)
21069 {
21070 const char *name;
21071 const char *parent_name;
21072
21073 /* It has been seen on RealView 2.2 built binaries,
21074 DW_TAG_template_type_param types actually _defined_ as
21075 children of the parent class:
21076
21077 enum E {};
21078 template class <class Enum> Class{};
21079 Class<enum E> class_e;
21080
21081 1: DW_TAG_class_type (Class)
21082 2: DW_TAG_enumeration_type (E)
21083 3: DW_TAG_enumerator (enum1:0)
21084 3: DW_TAG_enumerator (enum2:1)
21085 ...
21086 2: DW_TAG_template_type_param
21087 DW_AT_type DW_FORM_ref_udata (E)
21088
21089 Besides being broken debug info, it can put GDB into an
21090 infinite loop. Consider:
21091
21092 When we're building the full name for Class<E>, we'll start
21093 at Class, and go look over its template type parameters,
21094 finding E. We'll then try to build the full name of E, and
21095 reach here. We're now trying to build the full name of E,
21096 and look over the parent DIE for containing scope. In the
21097 broken case, if we followed the parent DIE of E, we'd again
21098 find Class, and once again go look at its template type
21099 arguments, etc., etc. Simply don't consider such parent die
21100 as source-level parent of this die (it can't be, the language
21101 doesn't allow it), and break the loop here. */
21102 name = dwarf2_name (die, cu);
21103 parent_name = dwarf2_name (parent, cu);
21104 complaint (_("template param type '%s' defined within parent '%s'"),
21105 name ? name : "<unknown>",
21106 parent_name ? parent_name : "<unknown>");
21107 return "";
21108 }
21109 else
21110 switch (parent->tag)
21111 {
21112 case DW_TAG_namespace:
21113 parent_type = read_type_die (parent, cu);
21114 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21115 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21116 Work around this problem here. */
21117 if (cu->language == language_cplus
21118 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21119 return "";
21120 /* We give a name to even anonymous namespaces. */
21121 return TYPE_NAME (parent_type);
21122 case DW_TAG_class_type:
21123 case DW_TAG_interface_type:
21124 case DW_TAG_structure_type:
21125 case DW_TAG_union_type:
21126 case DW_TAG_module:
21127 parent_type = read_type_die (parent, cu);
21128 if (TYPE_NAME (parent_type) != NULL)
21129 return TYPE_NAME (parent_type);
21130 else
21131 /* An anonymous structure is only allowed non-static data
21132 members; no typedefs, no member functions, et cetera.
21133 So it does not need a prefix. */
21134 return "";
21135 case DW_TAG_compile_unit:
21136 case DW_TAG_partial_unit:
21137 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21138 if (cu->language == language_cplus
21139 && !dwarf2_per_objfile->types.empty ()
21140 && die->child != NULL
21141 && (die->tag == DW_TAG_class_type
21142 || die->tag == DW_TAG_structure_type
21143 || die->tag == DW_TAG_union_type))
21144 {
21145 const char *name = guess_full_die_structure_name (die, cu);
21146 if (name != NULL)
21147 return name;
21148 }
21149 return "";
21150 case DW_TAG_subprogram:
21151 /* Nested subroutines in Fortran get a prefix with the name
21152 of the parent's subroutine. */
21153 if (cu->language == language_fortran)
21154 {
21155 if ((die->tag == DW_TAG_subprogram)
21156 && (dwarf2_name (parent, cu) != NULL))
21157 return dwarf2_name (parent, cu);
21158 }
21159 return determine_prefix (parent, cu);
21160 case DW_TAG_enumeration_type:
21161 parent_type = read_type_die (parent, cu);
21162 if (TYPE_DECLARED_CLASS (parent_type))
21163 {
21164 if (TYPE_NAME (parent_type) != NULL)
21165 return TYPE_NAME (parent_type);
21166 return "";
21167 }
21168 /* Fall through. */
21169 default:
21170 return determine_prefix (parent, cu);
21171 }
21172 }
21173
21174 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21175 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21176 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21177 an obconcat, otherwise allocate storage for the result. The CU argument is
21178 used to determine the language and hence, the appropriate separator. */
21179
21180 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21181
21182 static char *
21183 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21184 int physname, struct dwarf2_cu *cu)
21185 {
21186 const char *lead = "";
21187 const char *sep;
21188
21189 if (suffix == NULL || suffix[0] == '\0'
21190 || prefix == NULL || prefix[0] == '\0')
21191 sep = "";
21192 else if (cu->language == language_d)
21193 {
21194 /* For D, the 'main' function could be defined in any module, but it
21195 should never be prefixed. */
21196 if (strcmp (suffix, "D main") == 0)
21197 {
21198 prefix = "";
21199 sep = "";
21200 }
21201 else
21202 sep = ".";
21203 }
21204 else if (cu->language == language_fortran && physname)
21205 {
21206 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21207 DW_AT_MIPS_linkage_name is preferred and used instead. */
21208
21209 lead = "__";
21210 sep = "_MOD_";
21211 }
21212 else
21213 sep = "::";
21214
21215 if (prefix == NULL)
21216 prefix = "";
21217 if (suffix == NULL)
21218 suffix = "";
21219
21220 if (obs == NULL)
21221 {
21222 char *retval
21223 = ((char *)
21224 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21225
21226 strcpy (retval, lead);
21227 strcat (retval, prefix);
21228 strcat (retval, sep);
21229 strcat (retval, suffix);
21230 return retval;
21231 }
21232 else
21233 {
21234 /* We have an obstack. */
21235 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21236 }
21237 }
21238
21239 /* Return sibling of die, NULL if no sibling. */
21240
21241 static struct die_info *
21242 sibling_die (struct die_info *die)
21243 {
21244 return die->sibling;
21245 }
21246
21247 /* Get name of a die, return NULL if not found. */
21248
21249 static const char *
21250 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21251 struct objfile *objfile)
21252 {
21253 if (name && cu->language == language_cplus)
21254 {
21255 std::string canon_name = cp_canonicalize_string (name);
21256
21257 if (!canon_name.empty ())
21258 {
21259 if (canon_name != name)
21260 name = objfile->intern (canon_name);
21261 }
21262 }
21263
21264 return name;
21265 }
21266
21267 /* Get name of a die, return NULL if not found.
21268 Anonymous namespaces are converted to their magic string. */
21269
21270 static const char *
21271 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21272 {
21273 struct attribute *attr;
21274 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21275
21276 attr = dwarf2_attr (die, DW_AT_name, cu);
21277 if ((!attr || !DW_STRING (attr))
21278 && die->tag != DW_TAG_namespace
21279 && die->tag != DW_TAG_class_type
21280 && die->tag != DW_TAG_interface_type
21281 && die->tag != DW_TAG_structure_type
21282 && die->tag != DW_TAG_union_type)
21283 return NULL;
21284
21285 switch (die->tag)
21286 {
21287 case DW_TAG_compile_unit:
21288 case DW_TAG_partial_unit:
21289 /* Compilation units have a DW_AT_name that is a filename, not
21290 a source language identifier. */
21291 case DW_TAG_enumeration_type:
21292 case DW_TAG_enumerator:
21293 /* These tags always have simple identifiers already; no need
21294 to canonicalize them. */
21295 return DW_STRING (attr);
21296
21297 case DW_TAG_namespace:
21298 if (attr != NULL && DW_STRING (attr) != NULL)
21299 return DW_STRING (attr);
21300 return CP_ANONYMOUS_NAMESPACE_STR;
21301
21302 case DW_TAG_class_type:
21303 case DW_TAG_interface_type:
21304 case DW_TAG_structure_type:
21305 case DW_TAG_union_type:
21306 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21307 structures or unions. These were of the form "._%d" in GCC 4.1,
21308 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21309 and GCC 4.4. We work around this problem by ignoring these. */
21310 if (attr && DW_STRING (attr)
21311 && (startswith (DW_STRING (attr), "._")
21312 || startswith (DW_STRING (attr), "<anonymous")))
21313 return NULL;
21314
21315 /* GCC might emit a nameless typedef that has a linkage name. See
21316 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21317 if (!attr || DW_STRING (attr) == NULL)
21318 {
21319 attr = dw2_linkage_name_attr (die, cu);
21320 if (attr == NULL || DW_STRING (attr) == NULL)
21321 return NULL;
21322
21323 /* Avoid demangling DW_STRING (attr) the second time on a second
21324 call for the same DIE. */
21325 if (!DW_STRING_IS_CANONICAL (attr))
21326 {
21327 gdb::unique_xmalloc_ptr<char> demangled
21328 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21329 if (demangled == nullptr)
21330 return nullptr;
21331
21332 DW_STRING (attr) = objfile->intern (demangled.get ());
21333 DW_STRING_IS_CANONICAL (attr) = 1;
21334 }
21335
21336 /* Strip any leading namespaces/classes, keep only the base name.
21337 DW_AT_name for named DIEs does not contain the prefixes. */
21338 const char *base = strrchr (DW_STRING (attr), ':');
21339 if (base && base > DW_STRING (attr) && base[-1] == ':')
21340 return &base[1];
21341 else
21342 return DW_STRING (attr);
21343 }
21344 break;
21345
21346 default:
21347 break;
21348 }
21349
21350 if (!DW_STRING_IS_CANONICAL (attr))
21351 {
21352 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21353 objfile);
21354 DW_STRING_IS_CANONICAL (attr) = 1;
21355 }
21356 return DW_STRING (attr);
21357 }
21358
21359 /* Return the die that this die in an extension of, or NULL if there
21360 is none. *EXT_CU is the CU containing DIE on input, and the CU
21361 containing the return value on output. */
21362
21363 static struct die_info *
21364 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21365 {
21366 struct attribute *attr;
21367
21368 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21369 if (attr == NULL)
21370 return NULL;
21371
21372 return follow_die_ref (die, attr, ext_cu);
21373 }
21374
21375 /* A convenience function that returns an "unknown" DWARF name,
21376 including the value of V. STR is the name of the entity being
21377 printed, e.g., "TAG". */
21378
21379 static const char *
21380 dwarf_unknown (const char *str, unsigned v)
21381 {
21382 char *cell = get_print_cell ();
21383 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21384 return cell;
21385 }
21386
21387 /* Convert a DIE tag into its string name. */
21388
21389 static const char *
21390 dwarf_tag_name (unsigned tag)
21391 {
21392 const char *name = get_DW_TAG_name (tag);
21393
21394 if (name == NULL)
21395 return dwarf_unknown ("TAG", tag);
21396
21397 return name;
21398 }
21399
21400 /* Convert a DWARF attribute code into its string name. */
21401
21402 static const char *
21403 dwarf_attr_name (unsigned attr)
21404 {
21405 const char *name;
21406
21407 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21408 if (attr == DW_AT_MIPS_fde)
21409 return "DW_AT_MIPS_fde";
21410 #else
21411 if (attr == DW_AT_HP_block_index)
21412 return "DW_AT_HP_block_index";
21413 #endif
21414
21415 name = get_DW_AT_name (attr);
21416
21417 if (name == NULL)
21418 return dwarf_unknown ("AT", attr);
21419
21420 return name;
21421 }
21422
21423 /* Convert a DWARF value form code into its string name. */
21424
21425 static const char *
21426 dwarf_form_name (unsigned form)
21427 {
21428 const char *name = get_DW_FORM_name (form);
21429
21430 if (name == NULL)
21431 return dwarf_unknown ("FORM", form);
21432
21433 return name;
21434 }
21435
21436 static const char *
21437 dwarf_bool_name (unsigned mybool)
21438 {
21439 if (mybool)
21440 return "TRUE";
21441 else
21442 return "FALSE";
21443 }
21444
21445 /* Convert a DWARF type code into its string name. */
21446
21447 static const char *
21448 dwarf_type_encoding_name (unsigned enc)
21449 {
21450 const char *name = get_DW_ATE_name (enc);
21451
21452 if (name == NULL)
21453 return dwarf_unknown ("ATE", enc);
21454
21455 return name;
21456 }
21457
21458 static void
21459 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21460 {
21461 unsigned int i;
21462
21463 print_spaces (indent, f);
21464 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21465 dwarf_tag_name (die->tag), die->abbrev,
21466 sect_offset_str (die->sect_off));
21467
21468 if (die->parent != NULL)
21469 {
21470 print_spaces (indent, f);
21471 fprintf_unfiltered (f, " parent at offset: %s\n",
21472 sect_offset_str (die->parent->sect_off));
21473 }
21474
21475 print_spaces (indent, f);
21476 fprintf_unfiltered (f, " has children: %s\n",
21477 dwarf_bool_name (die->child != NULL));
21478
21479 print_spaces (indent, f);
21480 fprintf_unfiltered (f, " attributes:\n");
21481
21482 for (i = 0; i < die->num_attrs; ++i)
21483 {
21484 print_spaces (indent, f);
21485 fprintf_unfiltered (f, " %s (%s) ",
21486 dwarf_attr_name (die->attrs[i].name),
21487 dwarf_form_name (die->attrs[i].form));
21488
21489 switch (die->attrs[i].form)
21490 {
21491 case DW_FORM_addr:
21492 case DW_FORM_addrx:
21493 case DW_FORM_GNU_addr_index:
21494 fprintf_unfiltered (f, "address: ");
21495 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21496 break;
21497 case DW_FORM_block2:
21498 case DW_FORM_block4:
21499 case DW_FORM_block:
21500 case DW_FORM_block1:
21501 fprintf_unfiltered (f, "block: size %s",
21502 pulongest (DW_BLOCK (&die->attrs[i])->size));
21503 break;
21504 case DW_FORM_exprloc:
21505 fprintf_unfiltered (f, "expression: size %s",
21506 pulongest (DW_BLOCK (&die->attrs[i])->size));
21507 break;
21508 case DW_FORM_data16:
21509 fprintf_unfiltered (f, "constant of 16 bytes");
21510 break;
21511 case DW_FORM_ref_addr:
21512 fprintf_unfiltered (f, "ref address: ");
21513 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21514 break;
21515 case DW_FORM_GNU_ref_alt:
21516 fprintf_unfiltered (f, "alt ref address: ");
21517 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21518 break;
21519 case DW_FORM_ref1:
21520 case DW_FORM_ref2:
21521 case DW_FORM_ref4:
21522 case DW_FORM_ref8:
21523 case DW_FORM_ref_udata:
21524 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21525 (long) (DW_UNSND (&die->attrs[i])));
21526 break;
21527 case DW_FORM_data1:
21528 case DW_FORM_data2:
21529 case DW_FORM_data4:
21530 case DW_FORM_data8:
21531 case DW_FORM_udata:
21532 case DW_FORM_sdata:
21533 fprintf_unfiltered (f, "constant: %s",
21534 pulongest (DW_UNSND (&die->attrs[i])));
21535 break;
21536 case DW_FORM_sec_offset:
21537 fprintf_unfiltered (f, "section offset: %s",
21538 pulongest (DW_UNSND (&die->attrs[i])));
21539 break;
21540 case DW_FORM_ref_sig8:
21541 fprintf_unfiltered (f, "signature: %s",
21542 hex_string (DW_SIGNATURE (&die->attrs[i])));
21543 break;
21544 case DW_FORM_string:
21545 case DW_FORM_strp:
21546 case DW_FORM_line_strp:
21547 case DW_FORM_strx:
21548 case DW_FORM_GNU_str_index:
21549 case DW_FORM_GNU_strp_alt:
21550 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21551 DW_STRING (&die->attrs[i])
21552 ? DW_STRING (&die->attrs[i]) : "",
21553 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21554 break;
21555 case DW_FORM_flag:
21556 if (DW_UNSND (&die->attrs[i]))
21557 fprintf_unfiltered (f, "flag: TRUE");
21558 else
21559 fprintf_unfiltered (f, "flag: FALSE");
21560 break;
21561 case DW_FORM_flag_present:
21562 fprintf_unfiltered (f, "flag: TRUE");
21563 break;
21564 case DW_FORM_indirect:
21565 /* The reader will have reduced the indirect form to
21566 the "base form" so this form should not occur. */
21567 fprintf_unfiltered (f,
21568 "unexpected attribute form: DW_FORM_indirect");
21569 break;
21570 case DW_FORM_implicit_const:
21571 fprintf_unfiltered (f, "constant: %s",
21572 plongest (DW_SND (&die->attrs[i])));
21573 break;
21574 default:
21575 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21576 die->attrs[i].form);
21577 break;
21578 }
21579 fprintf_unfiltered (f, "\n");
21580 }
21581 }
21582
21583 static void
21584 dump_die_for_error (struct die_info *die)
21585 {
21586 dump_die_shallow (gdb_stderr, 0, die);
21587 }
21588
21589 static void
21590 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21591 {
21592 int indent = level * 4;
21593
21594 gdb_assert (die != NULL);
21595
21596 if (level >= max_level)
21597 return;
21598
21599 dump_die_shallow (f, indent, die);
21600
21601 if (die->child != NULL)
21602 {
21603 print_spaces (indent, f);
21604 fprintf_unfiltered (f, " Children:");
21605 if (level + 1 < max_level)
21606 {
21607 fprintf_unfiltered (f, "\n");
21608 dump_die_1 (f, level + 1, max_level, die->child);
21609 }
21610 else
21611 {
21612 fprintf_unfiltered (f,
21613 " [not printed, max nesting level reached]\n");
21614 }
21615 }
21616
21617 if (die->sibling != NULL && level > 0)
21618 {
21619 dump_die_1 (f, level, max_level, die->sibling);
21620 }
21621 }
21622
21623 /* This is called from the pdie macro in gdbinit.in.
21624 It's not static so gcc will keep a copy callable from gdb. */
21625
21626 void
21627 dump_die (struct die_info *die, int max_level)
21628 {
21629 dump_die_1 (gdb_stdlog, 0, max_level, die);
21630 }
21631
21632 static void
21633 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21634 {
21635 void **slot;
21636
21637 slot = htab_find_slot_with_hash (cu->die_hash, die,
21638 to_underlying (die->sect_off),
21639 INSERT);
21640
21641 *slot = die;
21642 }
21643
21644 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21645 required kind. */
21646
21647 static sect_offset
21648 dwarf2_get_ref_die_offset (const struct attribute *attr)
21649 {
21650 if (attr->form_is_ref ())
21651 return (sect_offset) DW_UNSND (attr);
21652
21653 complaint (_("unsupported die ref attribute form: '%s'"),
21654 dwarf_form_name (attr->form));
21655 return {};
21656 }
21657
21658 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21659 * the value held by the attribute is not constant. */
21660
21661 static LONGEST
21662 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21663 {
21664 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21665 return DW_SND (attr);
21666 else if (attr->form == DW_FORM_udata
21667 || attr->form == DW_FORM_data1
21668 || attr->form == DW_FORM_data2
21669 || attr->form == DW_FORM_data4
21670 || attr->form == DW_FORM_data8)
21671 return DW_UNSND (attr);
21672 else
21673 {
21674 /* For DW_FORM_data16 see attribute::form_is_constant. */
21675 complaint (_("Attribute value is not a constant (%s)"),
21676 dwarf_form_name (attr->form));
21677 return default_value;
21678 }
21679 }
21680
21681 /* Follow reference or signature attribute ATTR of SRC_DIE.
21682 On entry *REF_CU is the CU of SRC_DIE.
21683 On exit *REF_CU is the CU of the result. */
21684
21685 static struct die_info *
21686 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21687 struct dwarf2_cu **ref_cu)
21688 {
21689 struct die_info *die;
21690
21691 if (attr->form_is_ref ())
21692 die = follow_die_ref (src_die, attr, ref_cu);
21693 else if (attr->form == DW_FORM_ref_sig8)
21694 die = follow_die_sig (src_die, attr, ref_cu);
21695 else
21696 {
21697 dump_die_for_error (src_die);
21698 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21699 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21700 }
21701
21702 return die;
21703 }
21704
21705 /* Follow reference OFFSET.
21706 On entry *REF_CU is the CU of the source die referencing OFFSET.
21707 On exit *REF_CU is the CU of the result.
21708 Returns NULL if OFFSET is invalid. */
21709
21710 static struct die_info *
21711 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21712 struct dwarf2_cu **ref_cu)
21713 {
21714 struct die_info temp_die;
21715 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21716 struct dwarf2_per_objfile *dwarf2_per_objfile
21717 = cu->per_cu->dwarf2_per_objfile;
21718
21719 gdb_assert (cu->per_cu != NULL);
21720
21721 target_cu = cu;
21722
21723 if (cu->per_cu->is_debug_types)
21724 {
21725 /* .debug_types CUs cannot reference anything outside their CU.
21726 If they need to, they have to reference a signatured type via
21727 DW_FORM_ref_sig8. */
21728 if (!cu->header.offset_in_cu_p (sect_off))
21729 return NULL;
21730 }
21731 else if (offset_in_dwz != cu->per_cu->is_dwz
21732 || !cu->header.offset_in_cu_p (sect_off))
21733 {
21734 struct dwarf2_per_cu_data *per_cu;
21735
21736 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21737 dwarf2_per_objfile);
21738
21739 /* If necessary, add it to the queue and load its DIEs. */
21740 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21741 load_full_comp_unit (per_cu, false, cu->language);
21742
21743 target_cu = per_cu->cu;
21744 }
21745 else if (cu->dies == NULL)
21746 {
21747 /* We're loading full DIEs during partial symbol reading. */
21748 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21749 load_full_comp_unit (cu->per_cu, false, language_minimal);
21750 }
21751
21752 *ref_cu = target_cu;
21753 temp_die.sect_off = sect_off;
21754
21755 if (target_cu != cu)
21756 target_cu->ancestor = cu;
21757
21758 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21759 &temp_die,
21760 to_underlying (sect_off));
21761 }
21762
21763 /* Follow reference attribute ATTR of SRC_DIE.
21764 On entry *REF_CU is the CU of SRC_DIE.
21765 On exit *REF_CU is the CU of the result. */
21766
21767 static struct die_info *
21768 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21769 struct dwarf2_cu **ref_cu)
21770 {
21771 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21772 struct dwarf2_cu *cu = *ref_cu;
21773 struct die_info *die;
21774
21775 die = follow_die_offset (sect_off,
21776 (attr->form == DW_FORM_GNU_ref_alt
21777 || cu->per_cu->is_dwz),
21778 ref_cu);
21779 if (!die)
21780 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21781 "at %s [in module %s]"),
21782 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21783 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21784
21785 return die;
21786 }
21787
21788 /* See read.h. */
21789
21790 struct dwarf2_locexpr_baton
21791 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21792 dwarf2_per_cu_data *per_cu,
21793 CORE_ADDR (*get_frame_pc) (void *baton),
21794 void *baton, bool resolve_abstract_p)
21795 {
21796 struct dwarf2_cu *cu;
21797 struct die_info *die;
21798 struct attribute *attr;
21799 struct dwarf2_locexpr_baton retval;
21800 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21801 struct objfile *objfile = dwarf2_per_objfile->objfile;
21802
21803 if (per_cu->cu == NULL)
21804 load_cu (per_cu, false);
21805 cu = per_cu->cu;
21806 if (cu == NULL)
21807 {
21808 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21809 Instead just throw an error, not much else we can do. */
21810 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21811 sect_offset_str (sect_off), objfile_name (objfile));
21812 }
21813
21814 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21815 if (!die)
21816 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21817 sect_offset_str (sect_off), objfile_name (objfile));
21818
21819 attr = dwarf2_attr (die, DW_AT_location, cu);
21820 if (!attr && resolve_abstract_p
21821 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21822 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21823 {
21824 CORE_ADDR pc = (*get_frame_pc) (baton);
21825 CORE_ADDR baseaddr = objfile->text_section_offset ();
21826 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21827
21828 for (const auto &cand_off
21829 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21830 {
21831 struct dwarf2_cu *cand_cu = cu;
21832 struct die_info *cand
21833 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21834 if (!cand
21835 || !cand->parent
21836 || cand->parent->tag != DW_TAG_subprogram)
21837 continue;
21838
21839 CORE_ADDR pc_low, pc_high;
21840 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21841 if (pc_low == ((CORE_ADDR) -1))
21842 continue;
21843 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21844 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21845 if (!(pc_low <= pc && pc < pc_high))
21846 continue;
21847
21848 die = cand;
21849 attr = dwarf2_attr (die, DW_AT_location, cu);
21850 break;
21851 }
21852 }
21853
21854 if (!attr)
21855 {
21856 /* DWARF: "If there is no such attribute, then there is no effect.".
21857 DATA is ignored if SIZE is 0. */
21858
21859 retval.data = NULL;
21860 retval.size = 0;
21861 }
21862 else if (attr->form_is_section_offset ())
21863 {
21864 struct dwarf2_loclist_baton loclist_baton;
21865 CORE_ADDR pc = (*get_frame_pc) (baton);
21866 size_t size;
21867
21868 fill_in_loclist_baton (cu, &loclist_baton, attr);
21869
21870 retval.data = dwarf2_find_location_expression (&loclist_baton,
21871 &size, pc);
21872 retval.size = size;
21873 }
21874 else
21875 {
21876 if (!attr->form_is_block ())
21877 error (_("Dwarf Error: DIE at %s referenced in module %s "
21878 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21879 sect_offset_str (sect_off), objfile_name (objfile));
21880
21881 retval.data = DW_BLOCK (attr)->data;
21882 retval.size = DW_BLOCK (attr)->size;
21883 }
21884 retval.per_cu = cu->per_cu;
21885
21886 age_cached_comp_units (dwarf2_per_objfile);
21887
21888 return retval;
21889 }
21890
21891 /* See read.h. */
21892
21893 struct dwarf2_locexpr_baton
21894 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21895 dwarf2_per_cu_data *per_cu,
21896 CORE_ADDR (*get_frame_pc) (void *baton),
21897 void *baton)
21898 {
21899 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21900
21901 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21902 }
21903
21904 /* Write a constant of a given type as target-ordered bytes into
21905 OBSTACK. */
21906
21907 static const gdb_byte *
21908 write_constant_as_bytes (struct obstack *obstack,
21909 enum bfd_endian byte_order,
21910 struct type *type,
21911 ULONGEST value,
21912 LONGEST *len)
21913 {
21914 gdb_byte *result;
21915
21916 *len = TYPE_LENGTH (type);
21917 result = (gdb_byte *) obstack_alloc (obstack, *len);
21918 store_unsigned_integer (result, *len, byte_order, value);
21919
21920 return result;
21921 }
21922
21923 /* See read.h. */
21924
21925 const gdb_byte *
21926 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21927 dwarf2_per_cu_data *per_cu,
21928 obstack *obstack,
21929 LONGEST *len)
21930 {
21931 struct dwarf2_cu *cu;
21932 struct die_info *die;
21933 struct attribute *attr;
21934 const gdb_byte *result = NULL;
21935 struct type *type;
21936 LONGEST value;
21937 enum bfd_endian byte_order;
21938 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21939
21940 if (per_cu->cu == NULL)
21941 load_cu (per_cu, false);
21942 cu = per_cu->cu;
21943 if (cu == NULL)
21944 {
21945 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21946 Instead just throw an error, not much else we can do. */
21947 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21948 sect_offset_str (sect_off), objfile_name (objfile));
21949 }
21950
21951 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21952 if (!die)
21953 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21954 sect_offset_str (sect_off), objfile_name (objfile));
21955
21956 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21957 if (attr == NULL)
21958 return NULL;
21959
21960 byte_order = (bfd_big_endian (objfile->obfd)
21961 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21962
21963 switch (attr->form)
21964 {
21965 case DW_FORM_addr:
21966 case DW_FORM_addrx:
21967 case DW_FORM_GNU_addr_index:
21968 {
21969 gdb_byte *tem;
21970
21971 *len = cu->header.addr_size;
21972 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21973 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21974 result = tem;
21975 }
21976 break;
21977 case DW_FORM_string:
21978 case DW_FORM_strp:
21979 case DW_FORM_strx:
21980 case DW_FORM_GNU_str_index:
21981 case DW_FORM_GNU_strp_alt:
21982 /* DW_STRING is already allocated on the objfile obstack, point
21983 directly to it. */
21984 result = (const gdb_byte *) DW_STRING (attr);
21985 *len = strlen (DW_STRING (attr));
21986 break;
21987 case DW_FORM_block1:
21988 case DW_FORM_block2:
21989 case DW_FORM_block4:
21990 case DW_FORM_block:
21991 case DW_FORM_exprloc:
21992 case DW_FORM_data16:
21993 result = DW_BLOCK (attr)->data;
21994 *len = DW_BLOCK (attr)->size;
21995 break;
21996
21997 /* The DW_AT_const_value attributes are supposed to carry the
21998 symbol's value "represented as it would be on the target
21999 architecture." By the time we get here, it's already been
22000 converted to host endianness, so we just need to sign- or
22001 zero-extend it as appropriate. */
22002 case DW_FORM_data1:
22003 type = die_type (die, cu);
22004 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22005 if (result == NULL)
22006 result = write_constant_as_bytes (obstack, byte_order,
22007 type, value, len);
22008 break;
22009 case DW_FORM_data2:
22010 type = die_type (die, cu);
22011 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22012 if (result == NULL)
22013 result = write_constant_as_bytes (obstack, byte_order,
22014 type, value, len);
22015 break;
22016 case DW_FORM_data4:
22017 type = die_type (die, cu);
22018 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22019 if (result == NULL)
22020 result = write_constant_as_bytes (obstack, byte_order,
22021 type, value, len);
22022 break;
22023 case DW_FORM_data8:
22024 type = die_type (die, cu);
22025 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22026 if (result == NULL)
22027 result = write_constant_as_bytes (obstack, byte_order,
22028 type, value, len);
22029 break;
22030
22031 case DW_FORM_sdata:
22032 case DW_FORM_implicit_const:
22033 type = die_type (die, cu);
22034 result = write_constant_as_bytes (obstack, byte_order,
22035 type, DW_SND (attr), len);
22036 break;
22037
22038 case DW_FORM_udata:
22039 type = die_type (die, cu);
22040 result = write_constant_as_bytes (obstack, byte_order,
22041 type, DW_UNSND (attr), len);
22042 break;
22043
22044 default:
22045 complaint (_("unsupported const value attribute form: '%s'"),
22046 dwarf_form_name (attr->form));
22047 break;
22048 }
22049
22050 return result;
22051 }
22052
22053 /* See read.h. */
22054
22055 struct type *
22056 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22057 dwarf2_per_cu_data *per_cu)
22058 {
22059 struct dwarf2_cu *cu;
22060 struct die_info *die;
22061
22062 if (per_cu->cu == NULL)
22063 load_cu (per_cu, false);
22064 cu = per_cu->cu;
22065 if (!cu)
22066 return NULL;
22067
22068 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22069 if (!die)
22070 return NULL;
22071
22072 return die_type (die, cu);
22073 }
22074
22075 /* See read.h. */
22076
22077 struct type *
22078 dwarf2_get_die_type (cu_offset die_offset,
22079 struct dwarf2_per_cu_data *per_cu)
22080 {
22081 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22082 return get_die_type_at_offset (die_offset_sect, per_cu);
22083 }
22084
22085 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22086 On entry *REF_CU is the CU of SRC_DIE.
22087 On exit *REF_CU is the CU of the result.
22088 Returns NULL if the referenced DIE isn't found. */
22089
22090 static struct die_info *
22091 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22092 struct dwarf2_cu **ref_cu)
22093 {
22094 struct die_info temp_die;
22095 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22096 struct die_info *die;
22097
22098 /* While it might be nice to assert sig_type->type == NULL here,
22099 we can get here for DW_AT_imported_declaration where we need
22100 the DIE not the type. */
22101
22102 /* If necessary, add it to the queue and load its DIEs. */
22103
22104 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22105 read_signatured_type (sig_type);
22106
22107 sig_cu = sig_type->per_cu.cu;
22108 gdb_assert (sig_cu != NULL);
22109 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22110 temp_die.sect_off = sig_type->type_offset_in_section;
22111 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22112 to_underlying (temp_die.sect_off));
22113 if (die)
22114 {
22115 struct dwarf2_per_objfile *dwarf2_per_objfile
22116 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22117
22118 /* For .gdb_index version 7 keep track of included TUs.
22119 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22120 if (dwarf2_per_objfile->index_table != NULL
22121 && dwarf2_per_objfile->index_table->version <= 7)
22122 {
22123 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22124 }
22125
22126 *ref_cu = sig_cu;
22127 if (sig_cu != cu)
22128 sig_cu->ancestor = cu;
22129
22130 return die;
22131 }
22132
22133 return NULL;
22134 }
22135
22136 /* Follow signatured type referenced by ATTR in SRC_DIE.
22137 On entry *REF_CU is the CU of SRC_DIE.
22138 On exit *REF_CU is the CU of the result.
22139 The result is the DIE of the type.
22140 If the referenced type cannot be found an error is thrown. */
22141
22142 static struct die_info *
22143 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22144 struct dwarf2_cu **ref_cu)
22145 {
22146 ULONGEST signature = DW_SIGNATURE (attr);
22147 struct signatured_type *sig_type;
22148 struct die_info *die;
22149
22150 gdb_assert (attr->form == DW_FORM_ref_sig8);
22151
22152 sig_type = lookup_signatured_type (*ref_cu, signature);
22153 /* sig_type will be NULL if the signatured type is missing from
22154 the debug info. */
22155 if (sig_type == NULL)
22156 {
22157 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22158 " from DIE at %s [in module %s]"),
22159 hex_string (signature), sect_offset_str (src_die->sect_off),
22160 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22161 }
22162
22163 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22164 if (die == NULL)
22165 {
22166 dump_die_for_error (src_die);
22167 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22168 " from DIE at %s [in module %s]"),
22169 hex_string (signature), sect_offset_str (src_die->sect_off),
22170 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22171 }
22172
22173 return die;
22174 }
22175
22176 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22177 reading in and processing the type unit if necessary. */
22178
22179 static struct type *
22180 get_signatured_type (struct die_info *die, ULONGEST signature,
22181 struct dwarf2_cu *cu)
22182 {
22183 struct dwarf2_per_objfile *dwarf2_per_objfile
22184 = cu->per_cu->dwarf2_per_objfile;
22185 struct signatured_type *sig_type;
22186 struct dwarf2_cu *type_cu;
22187 struct die_info *type_die;
22188 struct type *type;
22189
22190 sig_type = lookup_signatured_type (cu, signature);
22191 /* sig_type will be NULL if the signatured type is missing from
22192 the debug info. */
22193 if (sig_type == NULL)
22194 {
22195 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22196 " from DIE at %s [in module %s]"),
22197 hex_string (signature), sect_offset_str (die->sect_off),
22198 objfile_name (dwarf2_per_objfile->objfile));
22199 return build_error_marker_type (cu, die);
22200 }
22201
22202 /* If we already know the type we're done. */
22203 if (sig_type->type != NULL)
22204 return sig_type->type;
22205
22206 type_cu = cu;
22207 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22208 if (type_die != NULL)
22209 {
22210 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22211 is created. This is important, for example, because for c++ classes
22212 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22213 type = read_type_die (type_die, type_cu);
22214 if (type == NULL)
22215 {
22216 complaint (_("Dwarf Error: Cannot build signatured type %s"
22217 " referenced from DIE at %s [in module %s]"),
22218 hex_string (signature), sect_offset_str (die->sect_off),
22219 objfile_name (dwarf2_per_objfile->objfile));
22220 type = build_error_marker_type (cu, die);
22221 }
22222 }
22223 else
22224 {
22225 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22226 " from DIE at %s [in module %s]"),
22227 hex_string (signature), sect_offset_str (die->sect_off),
22228 objfile_name (dwarf2_per_objfile->objfile));
22229 type = build_error_marker_type (cu, die);
22230 }
22231 sig_type->type = type;
22232
22233 return type;
22234 }
22235
22236 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22237 reading in and processing the type unit if necessary. */
22238
22239 static struct type *
22240 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22241 struct dwarf2_cu *cu) /* ARI: editCase function */
22242 {
22243 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22244 if (attr->form_is_ref ())
22245 {
22246 struct dwarf2_cu *type_cu = cu;
22247 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22248
22249 return read_type_die (type_die, type_cu);
22250 }
22251 else if (attr->form == DW_FORM_ref_sig8)
22252 {
22253 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22254 }
22255 else
22256 {
22257 struct dwarf2_per_objfile *dwarf2_per_objfile
22258 = cu->per_cu->dwarf2_per_objfile;
22259
22260 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22261 " at %s [in module %s]"),
22262 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22263 objfile_name (dwarf2_per_objfile->objfile));
22264 return build_error_marker_type (cu, die);
22265 }
22266 }
22267
22268 /* Load the DIEs associated with type unit PER_CU into memory. */
22269
22270 static void
22271 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22272 {
22273 struct signatured_type *sig_type;
22274
22275 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22276 gdb_assert (! per_cu->type_unit_group_p ());
22277
22278 /* We have the per_cu, but we need the signatured_type.
22279 Fortunately this is an easy translation. */
22280 gdb_assert (per_cu->is_debug_types);
22281 sig_type = (struct signatured_type *) per_cu;
22282
22283 gdb_assert (per_cu->cu == NULL);
22284
22285 read_signatured_type (sig_type);
22286
22287 gdb_assert (per_cu->cu != NULL);
22288 }
22289
22290 /* Read in a signatured type and build its CU and DIEs.
22291 If the type is a stub for the real type in a DWO file,
22292 read in the real type from the DWO file as well. */
22293
22294 static void
22295 read_signatured_type (struct signatured_type *sig_type)
22296 {
22297 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22298
22299 gdb_assert (per_cu->is_debug_types);
22300 gdb_assert (per_cu->cu == NULL);
22301
22302 cutu_reader reader (per_cu, NULL, 0, false);
22303
22304 if (!reader.dummy_p)
22305 {
22306 struct dwarf2_cu *cu = reader.cu;
22307 const gdb_byte *info_ptr = reader.info_ptr;
22308
22309 gdb_assert (cu->die_hash == NULL);
22310 cu->die_hash =
22311 htab_create_alloc_ex (cu->header.length / 12,
22312 die_hash,
22313 die_eq,
22314 NULL,
22315 &cu->comp_unit_obstack,
22316 hashtab_obstack_allocate,
22317 dummy_obstack_deallocate);
22318
22319 if (reader.comp_unit_die->has_children)
22320 reader.comp_unit_die->child
22321 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22322 reader.comp_unit_die);
22323 cu->dies = reader.comp_unit_die;
22324 /* comp_unit_die is not stored in die_hash, no need. */
22325
22326 /* We try not to read any attributes in this function, because
22327 not all CUs needed for references have been loaded yet, and
22328 symbol table processing isn't initialized. But we have to
22329 set the CU language, or we won't be able to build types
22330 correctly. Similarly, if we do not read the producer, we can
22331 not apply producer-specific interpretation. */
22332 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22333
22334 reader.keep ();
22335 }
22336
22337 sig_type->per_cu.tu_read = 1;
22338 }
22339
22340 /* Decode simple location descriptions.
22341 Given a pointer to a dwarf block that defines a location, compute
22342 the location and return the value.
22343
22344 NOTE drow/2003-11-18: This function is called in two situations
22345 now: for the address of static or global variables (partial symbols
22346 only) and for offsets into structures which are expected to be
22347 (more or less) constant. The partial symbol case should go away,
22348 and only the constant case should remain. That will let this
22349 function complain more accurately. A few special modes are allowed
22350 without complaint for global variables (for instance, global
22351 register values and thread-local values).
22352
22353 A location description containing no operations indicates that the
22354 object is optimized out. The return value is 0 for that case.
22355 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22356 callers will only want a very basic result and this can become a
22357 complaint.
22358
22359 Note that stack[0] is unused except as a default error return. */
22360
22361 static CORE_ADDR
22362 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22363 {
22364 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22365 size_t i;
22366 size_t size = blk->size;
22367 const gdb_byte *data = blk->data;
22368 CORE_ADDR stack[64];
22369 int stacki;
22370 unsigned int bytes_read, unsnd;
22371 gdb_byte op;
22372
22373 i = 0;
22374 stacki = 0;
22375 stack[stacki] = 0;
22376 stack[++stacki] = 0;
22377
22378 while (i < size)
22379 {
22380 op = data[i++];
22381 switch (op)
22382 {
22383 case DW_OP_lit0:
22384 case DW_OP_lit1:
22385 case DW_OP_lit2:
22386 case DW_OP_lit3:
22387 case DW_OP_lit4:
22388 case DW_OP_lit5:
22389 case DW_OP_lit6:
22390 case DW_OP_lit7:
22391 case DW_OP_lit8:
22392 case DW_OP_lit9:
22393 case DW_OP_lit10:
22394 case DW_OP_lit11:
22395 case DW_OP_lit12:
22396 case DW_OP_lit13:
22397 case DW_OP_lit14:
22398 case DW_OP_lit15:
22399 case DW_OP_lit16:
22400 case DW_OP_lit17:
22401 case DW_OP_lit18:
22402 case DW_OP_lit19:
22403 case DW_OP_lit20:
22404 case DW_OP_lit21:
22405 case DW_OP_lit22:
22406 case DW_OP_lit23:
22407 case DW_OP_lit24:
22408 case DW_OP_lit25:
22409 case DW_OP_lit26:
22410 case DW_OP_lit27:
22411 case DW_OP_lit28:
22412 case DW_OP_lit29:
22413 case DW_OP_lit30:
22414 case DW_OP_lit31:
22415 stack[++stacki] = op - DW_OP_lit0;
22416 break;
22417
22418 case DW_OP_reg0:
22419 case DW_OP_reg1:
22420 case DW_OP_reg2:
22421 case DW_OP_reg3:
22422 case DW_OP_reg4:
22423 case DW_OP_reg5:
22424 case DW_OP_reg6:
22425 case DW_OP_reg7:
22426 case DW_OP_reg8:
22427 case DW_OP_reg9:
22428 case DW_OP_reg10:
22429 case DW_OP_reg11:
22430 case DW_OP_reg12:
22431 case DW_OP_reg13:
22432 case DW_OP_reg14:
22433 case DW_OP_reg15:
22434 case DW_OP_reg16:
22435 case DW_OP_reg17:
22436 case DW_OP_reg18:
22437 case DW_OP_reg19:
22438 case DW_OP_reg20:
22439 case DW_OP_reg21:
22440 case DW_OP_reg22:
22441 case DW_OP_reg23:
22442 case DW_OP_reg24:
22443 case DW_OP_reg25:
22444 case DW_OP_reg26:
22445 case DW_OP_reg27:
22446 case DW_OP_reg28:
22447 case DW_OP_reg29:
22448 case DW_OP_reg30:
22449 case DW_OP_reg31:
22450 stack[++stacki] = op - DW_OP_reg0;
22451 if (i < size)
22452 dwarf2_complex_location_expr_complaint ();
22453 break;
22454
22455 case DW_OP_regx:
22456 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22457 i += bytes_read;
22458 stack[++stacki] = unsnd;
22459 if (i < size)
22460 dwarf2_complex_location_expr_complaint ();
22461 break;
22462
22463 case DW_OP_addr:
22464 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22465 &bytes_read);
22466 i += bytes_read;
22467 break;
22468
22469 case DW_OP_const1u:
22470 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22471 i += 1;
22472 break;
22473
22474 case DW_OP_const1s:
22475 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22476 i += 1;
22477 break;
22478
22479 case DW_OP_const2u:
22480 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22481 i += 2;
22482 break;
22483
22484 case DW_OP_const2s:
22485 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22486 i += 2;
22487 break;
22488
22489 case DW_OP_const4u:
22490 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22491 i += 4;
22492 break;
22493
22494 case DW_OP_const4s:
22495 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22496 i += 4;
22497 break;
22498
22499 case DW_OP_const8u:
22500 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22501 i += 8;
22502 break;
22503
22504 case DW_OP_constu:
22505 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22506 &bytes_read);
22507 i += bytes_read;
22508 break;
22509
22510 case DW_OP_consts:
22511 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22512 i += bytes_read;
22513 break;
22514
22515 case DW_OP_dup:
22516 stack[stacki + 1] = stack[stacki];
22517 stacki++;
22518 break;
22519
22520 case DW_OP_plus:
22521 stack[stacki - 1] += stack[stacki];
22522 stacki--;
22523 break;
22524
22525 case DW_OP_plus_uconst:
22526 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22527 &bytes_read);
22528 i += bytes_read;
22529 break;
22530
22531 case DW_OP_minus:
22532 stack[stacki - 1] -= stack[stacki];
22533 stacki--;
22534 break;
22535
22536 case DW_OP_deref:
22537 /* If we're not the last op, then we definitely can't encode
22538 this using GDB's address_class enum. This is valid for partial
22539 global symbols, although the variable's address will be bogus
22540 in the psymtab. */
22541 if (i < size)
22542 dwarf2_complex_location_expr_complaint ();
22543 break;
22544
22545 case DW_OP_GNU_push_tls_address:
22546 case DW_OP_form_tls_address:
22547 /* The top of the stack has the offset from the beginning
22548 of the thread control block at which the variable is located. */
22549 /* Nothing should follow this operator, so the top of stack would
22550 be returned. */
22551 /* This is valid for partial global symbols, but the variable's
22552 address will be bogus in the psymtab. Make it always at least
22553 non-zero to not look as a variable garbage collected by linker
22554 which have DW_OP_addr 0. */
22555 if (i < size)
22556 dwarf2_complex_location_expr_complaint ();
22557 stack[stacki]++;
22558 break;
22559
22560 case DW_OP_GNU_uninit:
22561 break;
22562
22563 case DW_OP_addrx:
22564 case DW_OP_GNU_addr_index:
22565 case DW_OP_GNU_const_index:
22566 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22567 &bytes_read);
22568 i += bytes_read;
22569 break;
22570
22571 default:
22572 {
22573 const char *name = get_DW_OP_name (op);
22574
22575 if (name)
22576 complaint (_("unsupported stack op: '%s'"),
22577 name);
22578 else
22579 complaint (_("unsupported stack op: '%02x'"),
22580 op);
22581 }
22582
22583 return (stack[stacki]);
22584 }
22585
22586 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22587 outside of the allocated space. Also enforce minimum>0. */
22588 if (stacki >= ARRAY_SIZE (stack) - 1)
22589 {
22590 complaint (_("location description stack overflow"));
22591 return 0;
22592 }
22593
22594 if (stacki <= 0)
22595 {
22596 complaint (_("location description stack underflow"));
22597 return 0;
22598 }
22599 }
22600 return (stack[stacki]);
22601 }
22602
22603 /* memory allocation interface */
22604
22605 static struct dwarf_block *
22606 dwarf_alloc_block (struct dwarf2_cu *cu)
22607 {
22608 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22609 }
22610
22611 static struct die_info *
22612 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22613 {
22614 struct die_info *die;
22615 size_t size = sizeof (struct die_info);
22616
22617 if (num_attrs > 1)
22618 size += (num_attrs - 1) * sizeof (struct attribute);
22619
22620 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22621 memset (die, 0, sizeof (struct die_info));
22622 return (die);
22623 }
22624
22625 \f
22626
22627 /* Macro support. */
22628
22629 /* An overload of dwarf_decode_macros that finds the correct section
22630 and ensures it is read in before calling the other overload. */
22631
22632 static void
22633 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22634 int section_is_gnu)
22635 {
22636 struct dwarf2_per_objfile *dwarf2_per_objfile
22637 = cu->per_cu->dwarf2_per_objfile;
22638 struct objfile *objfile = dwarf2_per_objfile->objfile;
22639 const struct line_header *lh = cu->line_header;
22640 unsigned int offset_size = cu->header.offset_size;
22641 struct dwarf2_section_info *section;
22642 const char *section_name;
22643
22644 if (cu->dwo_unit != nullptr)
22645 {
22646 if (section_is_gnu)
22647 {
22648 section = &cu->dwo_unit->dwo_file->sections.macro;
22649 section_name = ".debug_macro.dwo";
22650 }
22651 else
22652 {
22653 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22654 section_name = ".debug_macinfo.dwo";
22655 }
22656 }
22657 else
22658 {
22659 if (section_is_gnu)
22660 {
22661 section = &dwarf2_per_objfile->macro;
22662 section_name = ".debug_macro";
22663 }
22664 else
22665 {
22666 section = &dwarf2_per_objfile->macinfo;
22667 section_name = ".debug_macinfo";
22668 }
22669 }
22670
22671 section->read (objfile);
22672 if (section->buffer == nullptr)
22673 {
22674 complaint (_("missing %s section"), section_name);
22675 return;
22676 }
22677
22678 buildsym_compunit *builder = cu->get_builder ();
22679
22680 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22681 offset_size, offset, section_is_gnu);
22682 }
22683
22684 /* Return the .debug_loc section to use for CU.
22685 For DWO files use .debug_loc.dwo. */
22686
22687 static struct dwarf2_section_info *
22688 cu_debug_loc_section (struct dwarf2_cu *cu)
22689 {
22690 struct dwarf2_per_objfile *dwarf2_per_objfile
22691 = cu->per_cu->dwarf2_per_objfile;
22692
22693 if (cu->dwo_unit)
22694 {
22695 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22696
22697 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22698 }
22699 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22700 : &dwarf2_per_objfile->loc);
22701 }
22702
22703 /* A helper function that fills in a dwarf2_loclist_baton. */
22704
22705 static void
22706 fill_in_loclist_baton (struct dwarf2_cu *cu,
22707 struct dwarf2_loclist_baton *baton,
22708 const struct attribute *attr)
22709 {
22710 struct dwarf2_per_objfile *dwarf2_per_objfile
22711 = cu->per_cu->dwarf2_per_objfile;
22712 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22713
22714 section->read (dwarf2_per_objfile->objfile);
22715
22716 baton->per_cu = cu->per_cu;
22717 gdb_assert (baton->per_cu);
22718 /* We don't know how long the location list is, but make sure we
22719 don't run off the edge of the section. */
22720 baton->size = section->size - DW_UNSND (attr);
22721 baton->data = section->buffer + DW_UNSND (attr);
22722 if (cu->base_address.has_value ())
22723 baton->base_address = *cu->base_address;
22724 else
22725 baton->base_address = 0;
22726 baton->from_dwo = cu->dwo_unit != NULL;
22727 }
22728
22729 static void
22730 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22731 struct dwarf2_cu *cu, int is_block)
22732 {
22733 struct dwarf2_per_objfile *dwarf2_per_objfile
22734 = cu->per_cu->dwarf2_per_objfile;
22735 struct objfile *objfile = dwarf2_per_objfile->objfile;
22736 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22737
22738 if (attr->form_is_section_offset ()
22739 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22740 the section. If so, fall through to the complaint in the
22741 other branch. */
22742 && DW_UNSND (attr) < section->get_size (objfile))
22743 {
22744 struct dwarf2_loclist_baton *baton;
22745
22746 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22747
22748 fill_in_loclist_baton (cu, baton, attr);
22749
22750 if (!cu->base_address.has_value ())
22751 complaint (_("Location list used without "
22752 "specifying the CU base address."));
22753
22754 SYMBOL_ACLASS_INDEX (sym) = (is_block
22755 ? dwarf2_loclist_block_index
22756 : dwarf2_loclist_index);
22757 SYMBOL_LOCATION_BATON (sym) = baton;
22758 }
22759 else
22760 {
22761 struct dwarf2_locexpr_baton *baton;
22762
22763 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22764 baton->per_cu = cu->per_cu;
22765 gdb_assert (baton->per_cu);
22766
22767 if (attr->form_is_block ())
22768 {
22769 /* Note that we're just copying the block's data pointer
22770 here, not the actual data. We're still pointing into the
22771 info_buffer for SYM's objfile; right now we never release
22772 that buffer, but when we do clean up properly this may
22773 need to change. */
22774 baton->size = DW_BLOCK (attr)->size;
22775 baton->data = DW_BLOCK (attr)->data;
22776 }
22777 else
22778 {
22779 dwarf2_invalid_attrib_class_complaint ("location description",
22780 sym->natural_name ());
22781 baton->size = 0;
22782 }
22783
22784 SYMBOL_ACLASS_INDEX (sym) = (is_block
22785 ? dwarf2_locexpr_block_index
22786 : dwarf2_locexpr_index);
22787 SYMBOL_LOCATION_BATON (sym) = baton;
22788 }
22789 }
22790
22791 /* See read.h. */
22792
22793 struct objfile *
22794 dwarf2_per_cu_data::objfile () const
22795 {
22796 struct objfile *objfile = dwarf2_per_objfile->objfile;
22797
22798 /* Return the master objfile, so that we can report and look up the
22799 correct file containing this variable. */
22800 if (objfile->separate_debug_objfile_backlink)
22801 objfile = objfile->separate_debug_objfile_backlink;
22802
22803 return objfile;
22804 }
22805
22806 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22807 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22808 CU_HEADERP first. */
22809
22810 static const struct comp_unit_head *
22811 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22812 const struct dwarf2_per_cu_data *per_cu)
22813 {
22814 const gdb_byte *info_ptr;
22815
22816 if (per_cu->cu)
22817 return &per_cu->cu->header;
22818
22819 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22820
22821 memset (cu_headerp, 0, sizeof (*cu_headerp));
22822 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22823 rcuh_kind::COMPILE);
22824
22825 return cu_headerp;
22826 }
22827
22828 /* See read.h. */
22829
22830 int
22831 dwarf2_per_cu_data::addr_size () const
22832 {
22833 struct comp_unit_head cu_header_local;
22834 const struct comp_unit_head *cu_headerp;
22835
22836 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22837
22838 return cu_headerp->addr_size;
22839 }
22840
22841 /* See read.h. */
22842
22843 int
22844 dwarf2_per_cu_data::offset_size () const
22845 {
22846 struct comp_unit_head cu_header_local;
22847 const struct comp_unit_head *cu_headerp;
22848
22849 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22850
22851 return cu_headerp->offset_size;
22852 }
22853
22854 /* See read.h. */
22855
22856 int
22857 dwarf2_per_cu_data::ref_addr_size () const
22858 {
22859 struct comp_unit_head cu_header_local;
22860 const struct comp_unit_head *cu_headerp;
22861
22862 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22863
22864 if (cu_headerp->version == 2)
22865 return cu_headerp->addr_size;
22866 else
22867 return cu_headerp->offset_size;
22868 }
22869
22870 /* See read.h. */
22871
22872 CORE_ADDR
22873 dwarf2_per_cu_data::text_offset () const
22874 {
22875 struct objfile *objfile = dwarf2_per_objfile->objfile;
22876
22877 return objfile->text_section_offset ();
22878 }
22879
22880 /* See read.h. */
22881
22882 struct type *
22883 dwarf2_per_cu_data::addr_type () const
22884 {
22885 struct objfile *objfile = dwarf2_per_objfile->objfile;
22886 struct type *void_type = objfile_type (objfile)->builtin_void;
22887 struct type *addr_type = lookup_pointer_type (void_type);
22888 int addr_size = this->addr_size ();
22889
22890 if (TYPE_LENGTH (addr_type) == addr_size)
22891 return addr_type;
22892
22893 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22894 return addr_type;
22895 }
22896
22897 /* A helper function for dwarf2_find_containing_comp_unit that returns
22898 the index of the result, and that searches a vector. It will
22899 return a result even if the offset in question does not actually
22900 occur in any CU. This is separate so that it can be unit
22901 tested. */
22902
22903 static int
22904 dwarf2_find_containing_comp_unit
22905 (sect_offset sect_off,
22906 unsigned int offset_in_dwz,
22907 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22908 {
22909 int low, high;
22910
22911 low = 0;
22912 high = all_comp_units.size () - 1;
22913 while (high > low)
22914 {
22915 struct dwarf2_per_cu_data *mid_cu;
22916 int mid = low + (high - low) / 2;
22917
22918 mid_cu = all_comp_units[mid];
22919 if (mid_cu->is_dwz > offset_in_dwz
22920 || (mid_cu->is_dwz == offset_in_dwz
22921 && mid_cu->sect_off + mid_cu->length > sect_off))
22922 high = mid;
22923 else
22924 low = mid + 1;
22925 }
22926 gdb_assert (low == high);
22927 return low;
22928 }
22929
22930 /* Locate the .debug_info compilation unit from CU's objfile which contains
22931 the DIE at OFFSET. Raises an error on failure. */
22932
22933 static struct dwarf2_per_cu_data *
22934 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22935 unsigned int offset_in_dwz,
22936 struct dwarf2_per_objfile *dwarf2_per_objfile)
22937 {
22938 int low
22939 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22940 dwarf2_per_objfile->all_comp_units);
22941 struct dwarf2_per_cu_data *this_cu
22942 = dwarf2_per_objfile->all_comp_units[low];
22943
22944 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22945 {
22946 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22947 error (_("Dwarf Error: could not find partial DIE containing "
22948 "offset %s [in module %s]"),
22949 sect_offset_str (sect_off),
22950 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22951
22952 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22953 <= sect_off);
22954 return dwarf2_per_objfile->all_comp_units[low-1];
22955 }
22956 else
22957 {
22958 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22959 && sect_off >= this_cu->sect_off + this_cu->length)
22960 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22961 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22962 return this_cu;
22963 }
22964 }
22965
22966 #if GDB_SELF_TEST
22967
22968 namespace selftests {
22969 namespace find_containing_comp_unit {
22970
22971 static void
22972 run_test ()
22973 {
22974 struct dwarf2_per_cu_data one {};
22975 struct dwarf2_per_cu_data two {};
22976 struct dwarf2_per_cu_data three {};
22977 struct dwarf2_per_cu_data four {};
22978
22979 one.length = 5;
22980 two.sect_off = sect_offset (one.length);
22981 two.length = 7;
22982
22983 three.length = 5;
22984 three.is_dwz = 1;
22985 four.sect_off = sect_offset (three.length);
22986 four.length = 7;
22987 four.is_dwz = 1;
22988
22989 std::vector<dwarf2_per_cu_data *> units;
22990 units.push_back (&one);
22991 units.push_back (&two);
22992 units.push_back (&three);
22993 units.push_back (&four);
22994
22995 int result;
22996
22997 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22998 SELF_CHECK (units[result] == &one);
22999 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23000 SELF_CHECK (units[result] == &one);
23001 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23002 SELF_CHECK (units[result] == &two);
23003
23004 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23005 SELF_CHECK (units[result] == &three);
23006 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23007 SELF_CHECK (units[result] == &three);
23008 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23009 SELF_CHECK (units[result] == &four);
23010 }
23011
23012 }
23013 }
23014
23015 #endif /* GDB_SELF_TEST */
23016
23017 /* Initialize dwarf2_cu CU, owned by PER_CU. */
23018
23019 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23020 : per_cu (per_cu_),
23021 mark (false),
23022 has_loclist (false),
23023 checked_producer (false),
23024 producer_is_gxx_lt_4_6 (false),
23025 producer_is_gcc_lt_4_3 (false),
23026 producer_is_icc (false),
23027 producer_is_icc_lt_14 (false),
23028 producer_is_codewarrior (false),
23029 processing_has_namespace_info (false)
23030 {
23031 per_cu->cu = this;
23032 }
23033
23034 /* Destroy a dwarf2_cu. */
23035
23036 dwarf2_cu::~dwarf2_cu ()
23037 {
23038 per_cu->cu = NULL;
23039 }
23040
23041 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23042
23043 static void
23044 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23045 enum language pretend_language)
23046 {
23047 struct attribute *attr;
23048
23049 /* Set the language we're debugging. */
23050 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23051 if (attr != nullptr)
23052 set_cu_language (DW_UNSND (attr), cu);
23053 else
23054 {
23055 cu->language = pretend_language;
23056 cu->language_defn = language_def (cu->language);
23057 }
23058
23059 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23060 }
23061
23062 /* Increase the age counter on each cached compilation unit, and free
23063 any that are too old. */
23064
23065 static void
23066 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23067 {
23068 struct dwarf2_per_cu_data *per_cu, **last_chain;
23069
23070 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23071 per_cu = dwarf2_per_objfile->read_in_chain;
23072 while (per_cu != NULL)
23073 {
23074 per_cu->cu->last_used ++;
23075 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23076 dwarf2_mark (per_cu->cu);
23077 per_cu = per_cu->cu->read_in_chain;
23078 }
23079
23080 per_cu = dwarf2_per_objfile->read_in_chain;
23081 last_chain = &dwarf2_per_objfile->read_in_chain;
23082 while (per_cu != NULL)
23083 {
23084 struct dwarf2_per_cu_data *next_cu;
23085
23086 next_cu = per_cu->cu->read_in_chain;
23087
23088 if (!per_cu->cu->mark)
23089 {
23090 delete per_cu->cu;
23091 *last_chain = next_cu;
23092 }
23093 else
23094 last_chain = &per_cu->cu->read_in_chain;
23095
23096 per_cu = next_cu;
23097 }
23098 }
23099
23100 /* Remove a single compilation unit from the cache. */
23101
23102 static void
23103 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23104 {
23105 struct dwarf2_per_cu_data *per_cu, **last_chain;
23106 struct dwarf2_per_objfile *dwarf2_per_objfile
23107 = target_per_cu->dwarf2_per_objfile;
23108
23109 per_cu = dwarf2_per_objfile->read_in_chain;
23110 last_chain = &dwarf2_per_objfile->read_in_chain;
23111 while (per_cu != NULL)
23112 {
23113 struct dwarf2_per_cu_data *next_cu;
23114
23115 next_cu = per_cu->cu->read_in_chain;
23116
23117 if (per_cu == target_per_cu)
23118 {
23119 delete per_cu->cu;
23120 per_cu->cu = NULL;
23121 *last_chain = next_cu;
23122 break;
23123 }
23124 else
23125 last_chain = &per_cu->cu->read_in_chain;
23126
23127 per_cu = next_cu;
23128 }
23129 }
23130
23131 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23132 We store these in a hash table separate from the DIEs, and preserve them
23133 when the DIEs are flushed out of cache.
23134
23135 The CU "per_cu" pointer is needed because offset alone is not enough to
23136 uniquely identify the type. A file may have multiple .debug_types sections,
23137 or the type may come from a DWO file. Furthermore, while it's more logical
23138 to use per_cu->section+offset, with Fission the section with the data is in
23139 the DWO file but we don't know that section at the point we need it.
23140 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23141 because we can enter the lookup routine, get_die_type_at_offset, from
23142 outside this file, and thus won't necessarily have PER_CU->cu.
23143 Fortunately, PER_CU is stable for the life of the objfile. */
23144
23145 struct dwarf2_per_cu_offset_and_type
23146 {
23147 const struct dwarf2_per_cu_data *per_cu;
23148 sect_offset sect_off;
23149 struct type *type;
23150 };
23151
23152 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23153
23154 static hashval_t
23155 per_cu_offset_and_type_hash (const void *item)
23156 {
23157 const struct dwarf2_per_cu_offset_and_type *ofs
23158 = (const struct dwarf2_per_cu_offset_and_type *) item;
23159
23160 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23161 }
23162
23163 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23164
23165 static int
23166 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23167 {
23168 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23169 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23170 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23171 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23172
23173 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23174 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23175 }
23176
23177 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23178 table if necessary. For convenience, return TYPE.
23179
23180 The DIEs reading must have careful ordering to:
23181 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23182 reading current DIE.
23183 * Not trying to dereference contents of still incompletely read in types
23184 while reading in other DIEs.
23185 * Enable referencing still incompletely read in types just by a pointer to
23186 the type without accessing its fields.
23187
23188 Therefore caller should follow these rules:
23189 * Try to fetch any prerequisite types we may need to build this DIE type
23190 before building the type and calling set_die_type.
23191 * After building type call set_die_type for current DIE as soon as
23192 possible before fetching more types to complete the current type.
23193 * Make the type as complete as possible before fetching more types. */
23194
23195 static struct type *
23196 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23197 {
23198 struct dwarf2_per_objfile *dwarf2_per_objfile
23199 = cu->per_cu->dwarf2_per_objfile;
23200 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23201 struct objfile *objfile = dwarf2_per_objfile->objfile;
23202 struct attribute *attr;
23203 struct dynamic_prop prop;
23204
23205 /* For Ada types, make sure that the gnat-specific data is always
23206 initialized (if not already set). There are a few types where
23207 we should not be doing so, because the type-specific area is
23208 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23209 where the type-specific area is used to store the floatformat).
23210 But this is not a problem, because the gnat-specific information
23211 is actually not needed for these types. */
23212 if (need_gnat_info (cu)
23213 && TYPE_CODE (type) != TYPE_CODE_FUNC
23214 && TYPE_CODE (type) != TYPE_CODE_FLT
23215 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23216 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23217 && TYPE_CODE (type) != TYPE_CODE_METHOD
23218 && !HAVE_GNAT_AUX_INFO (type))
23219 INIT_GNAT_SPECIFIC (type);
23220
23221 /* Read DW_AT_allocated and set in type. */
23222 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23223 if (attr != NULL && attr->form_is_block ())
23224 {
23225 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23226 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23227 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23228 }
23229 else if (attr != NULL)
23230 {
23231 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23232 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23233 sect_offset_str (die->sect_off));
23234 }
23235
23236 /* Read DW_AT_associated and set in type. */
23237 attr = dwarf2_attr (die, DW_AT_associated, cu);
23238 if (attr != NULL && attr->form_is_block ())
23239 {
23240 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23241 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23242 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23243 }
23244 else if (attr != NULL)
23245 {
23246 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23247 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23248 sect_offset_str (die->sect_off));
23249 }
23250
23251 /* Read DW_AT_data_location and set in type. */
23252 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23253 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23254 cu->per_cu->addr_type ()))
23255 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23256
23257 if (dwarf2_per_objfile->die_type_hash == NULL)
23258 dwarf2_per_objfile->die_type_hash
23259 = htab_up (htab_create_alloc (127,
23260 per_cu_offset_and_type_hash,
23261 per_cu_offset_and_type_eq,
23262 NULL, xcalloc, xfree));
23263
23264 ofs.per_cu = cu->per_cu;
23265 ofs.sect_off = die->sect_off;
23266 ofs.type = type;
23267 slot = (struct dwarf2_per_cu_offset_and_type **)
23268 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23269 if (*slot)
23270 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23271 sect_offset_str (die->sect_off));
23272 *slot = XOBNEW (&objfile->objfile_obstack,
23273 struct dwarf2_per_cu_offset_and_type);
23274 **slot = ofs;
23275 return type;
23276 }
23277
23278 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23279 or return NULL if the die does not have a saved type. */
23280
23281 static struct type *
23282 get_die_type_at_offset (sect_offset sect_off,
23283 struct dwarf2_per_cu_data *per_cu)
23284 {
23285 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23286 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23287
23288 if (dwarf2_per_objfile->die_type_hash == NULL)
23289 return NULL;
23290
23291 ofs.per_cu = per_cu;
23292 ofs.sect_off = sect_off;
23293 slot = ((struct dwarf2_per_cu_offset_and_type *)
23294 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23295 if (slot)
23296 return slot->type;
23297 else
23298 return NULL;
23299 }
23300
23301 /* Look up the type for DIE in CU in die_type_hash,
23302 or return NULL if DIE does not have a saved type. */
23303
23304 static struct type *
23305 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23306 {
23307 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23308 }
23309
23310 /* Add a dependence relationship from CU to REF_PER_CU. */
23311
23312 static void
23313 dwarf2_add_dependence (struct dwarf2_cu *cu,
23314 struct dwarf2_per_cu_data *ref_per_cu)
23315 {
23316 void **slot;
23317
23318 if (cu->dependencies == NULL)
23319 cu->dependencies
23320 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23321 NULL, &cu->comp_unit_obstack,
23322 hashtab_obstack_allocate,
23323 dummy_obstack_deallocate);
23324
23325 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23326 if (*slot == NULL)
23327 *slot = ref_per_cu;
23328 }
23329
23330 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23331 Set the mark field in every compilation unit in the
23332 cache that we must keep because we are keeping CU. */
23333
23334 static int
23335 dwarf2_mark_helper (void **slot, void *data)
23336 {
23337 struct dwarf2_per_cu_data *per_cu;
23338
23339 per_cu = (struct dwarf2_per_cu_data *) *slot;
23340
23341 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23342 reading of the chain. As such dependencies remain valid it is not much
23343 useful to track and undo them during QUIT cleanups. */
23344 if (per_cu->cu == NULL)
23345 return 1;
23346
23347 if (per_cu->cu->mark)
23348 return 1;
23349 per_cu->cu->mark = true;
23350
23351 if (per_cu->cu->dependencies != NULL)
23352 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23353
23354 return 1;
23355 }
23356
23357 /* Set the mark field in CU and in every other compilation unit in the
23358 cache that we must keep because we are keeping CU. */
23359
23360 static void
23361 dwarf2_mark (struct dwarf2_cu *cu)
23362 {
23363 if (cu->mark)
23364 return;
23365 cu->mark = true;
23366 if (cu->dependencies != NULL)
23367 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23368 }
23369
23370 static void
23371 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23372 {
23373 while (per_cu)
23374 {
23375 per_cu->cu->mark = false;
23376 per_cu = per_cu->cu->read_in_chain;
23377 }
23378 }
23379
23380 /* Trivial hash function for partial_die_info: the hash value of a DIE
23381 is its offset in .debug_info for this objfile. */
23382
23383 static hashval_t
23384 partial_die_hash (const void *item)
23385 {
23386 const struct partial_die_info *part_die
23387 = (const struct partial_die_info *) item;
23388
23389 return to_underlying (part_die->sect_off);
23390 }
23391
23392 /* Trivial comparison function for partial_die_info structures: two DIEs
23393 are equal if they have the same offset. */
23394
23395 static int
23396 partial_die_eq (const void *item_lhs, const void *item_rhs)
23397 {
23398 const struct partial_die_info *part_die_lhs
23399 = (const struct partial_die_info *) item_lhs;
23400 const struct partial_die_info *part_die_rhs
23401 = (const struct partial_die_info *) item_rhs;
23402
23403 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23404 }
23405
23406 struct cmd_list_element *set_dwarf_cmdlist;
23407 struct cmd_list_element *show_dwarf_cmdlist;
23408
23409 static void
23410 set_dwarf_cmd (const char *args, int from_tty)
23411 {
23412 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23413 gdb_stdout);
23414 }
23415
23416 static void
23417 show_dwarf_cmd (const char *args, int from_tty)
23418 {
23419 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23420 }
23421
23422 static void
23423 show_check_physname (struct ui_file *file, int from_tty,
23424 struct cmd_list_element *c, const char *value)
23425 {
23426 fprintf_filtered (file,
23427 _("Whether to check \"physname\" is %s.\n"),
23428 value);
23429 }
23430
23431 void _initialize_dwarf2_read ();
23432 void
23433 _initialize_dwarf2_read ()
23434 {
23435 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23436 Set DWARF specific variables.\n\
23437 Configure DWARF variables such as the cache size."),
23438 &set_dwarf_cmdlist, "maintenance set dwarf ",
23439 0/*allow-unknown*/, &maintenance_set_cmdlist);
23440
23441 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23442 Show DWARF specific variables.\n\
23443 Show DWARF variables such as the cache size."),
23444 &show_dwarf_cmdlist, "maintenance show dwarf ",
23445 0/*allow-unknown*/, &maintenance_show_cmdlist);
23446
23447 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23448 &dwarf_max_cache_age, _("\
23449 Set the upper bound on the age of cached DWARF compilation units."), _("\
23450 Show the upper bound on the age of cached DWARF compilation units."), _("\
23451 A higher limit means that cached compilation units will be stored\n\
23452 in memory longer, and more total memory will be used. Zero disables\n\
23453 caching, which can slow down startup."),
23454 NULL,
23455 show_dwarf_max_cache_age,
23456 &set_dwarf_cmdlist,
23457 &show_dwarf_cmdlist);
23458
23459 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23460 Set debugging of the DWARF reader."), _("\
23461 Show debugging of the DWARF reader."), _("\
23462 When enabled (non-zero), debugging messages are printed during DWARF\n\
23463 reading and symtab expansion. A value of 1 (one) provides basic\n\
23464 information. A value greater than 1 provides more verbose information."),
23465 NULL,
23466 NULL,
23467 &setdebuglist, &showdebuglist);
23468
23469 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23470 Set debugging of the DWARF DIE reader."), _("\
23471 Show debugging of the DWARF DIE reader."), _("\
23472 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23473 The value is the maximum depth to print."),
23474 NULL,
23475 NULL,
23476 &setdebuglist, &showdebuglist);
23477
23478 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23479 Set debugging of the dwarf line reader."), _("\
23480 Show debugging of the dwarf line reader."), _("\
23481 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23482 A value of 1 (one) provides basic information.\n\
23483 A value greater than 1 provides more verbose information."),
23484 NULL,
23485 NULL,
23486 &setdebuglist, &showdebuglist);
23487
23488 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23489 Set cross-checking of \"physname\" code against demangler."), _("\
23490 Show cross-checking of \"physname\" code against demangler."), _("\
23491 When enabled, GDB's internal \"physname\" code is checked against\n\
23492 the demangler."),
23493 NULL, show_check_physname,
23494 &setdebuglist, &showdebuglist);
23495
23496 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23497 no_class, &use_deprecated_index_sections, _("\
23498 Set whether to use deprecated gdb_index sections."), _("\
23499 Show whether to use deprecated gdb_index sections."), _("\
23500 When enabled, deprecated .gdb_index sections are used anyway.\n\
23501 Normally they are ignored either because of a missing feature or\n\
23502 performance issue.\n\
23503 Warning: This option must be enabled before gdb reads the file."),
23504 NULL,
23505 NULL,
23506 &setlist, &showlist);
23507
23508 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23509 &dwarf2_locexpr_funcs);
23510 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23511 &dwarf2_loclist_funcs);
23512
23513 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23514 &dwarf2_block_frame_base_locexpr_funcs);
23515 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23516 &dwarf2_block_frame_base_loclist_funcs);
23517
23518 #if GDB_SELF_TEST
23519 selftests::register_test ("dw2_expand_symtabs_matching",
23520 selftests::dw2_expand_symtabs_matching::run_test);
23521 selftests::register_test ("dwarf2_find_containing_comp_unit",
23522 selftests::find_containing_comp_unit::run_test);
23523 #endif
23524 }
This page took 0.48137 seconds and 5 git commands to generate.