Change two more functions to be methods on die_info
[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 void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1420
1421 static void dump_die_for_error (struct die_info *);
1422
1423 static void dump_die_1 (struct ui_file *, int level, int max_level,
1424 struct die_info *);
1425
1426 /*static*/ void dump_die (struct die_info *, int max_level);
1427
1428 static void store_in_ref_table (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
1432
1433 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
1434
1435 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1436 const struct attribute *,
1437 struct dwarf2_cu **);
1438
1439 static struct die_info *follow_die_ref (struct die_info *,
1440 const struct attribute *,
1441 struct dwarf2_cu **);
1442
1443 static struct die_info *follow_die_sig (struct die_info *,
1444 const struct attribute *,
1445 struct dwarf2_cu **);
1446
1447 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1448 struct dwarf2_cu *);
1449
1450 static struct type *get_DW_AT_signature_type (struct die_info *,
1451 const struct attribute *,
1452 struct dwarf2_cu *);
1453
1454 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1455
1456 static void read_signatured_type (struct signatured_type *);
1457
1458 static int attr_to_dynamic_prop (const struct attribute *attr,
1459 struct die_info *die, struct dwarf2_cu *cu,
1460 struct dynamic_prop *prop, struct type *type);
1461
1462 /* memory allocation interface */
1463
1464 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1465
1466 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1467
1468 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1469
1470 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1471 struct dwarf2_loclist_baton *baton,
1472 const struct attribute *attr);
1473
1474 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1475 struct symbol *sym,
1476 struct dwarf2_cu *cu,
1477 int is_block);
1478
1479 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1480 const gdb_byte *info_ptr,
1481 struct abbrev_info *abbrev);
1482
1483 static hashval_t partial_die_hash (const void *item);
1484
1485 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1486
1487 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1488 (sect_offset sect_off, unsigned int offset_in_dwz,
1489 struct dwarf2_per_objfile *dwarf2_per_objfile);
1490
1491 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1492 struct die_info *comp_unit_die,
1493 enum language pretend_language);
1494
1495 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1496
1497 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1498
1499 static struct type *set_die_type (struct die_info *, struct type *,
1500 struct dwarf2_cu *);
1501
1502 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1503
1504 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1505
1506 static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
1507 enum language);
1508
1509 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1510 enum language);
1511
1512 static void process_full_type_unit (struct dwarf2_per_cu_data *,
1513 enum language);
1514
1515 static void dwarf2_add_dependence (struct dwarf2_cu *,
1516 struct dwarf2_per_cu_data *);
1517
1518 static void dwarf2_mark (struct dwarf2_cu *);
1519
1520 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1521
1522 static struct type *get_die_type_at_offset (sect_offset,
1523 struct dwarf2_per_cu_data *);
1524
1525 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1526
1527 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1528 enum language pretend_language);
1529
1530 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1531
1532 /* Class, the destructor of which frees all allocated queue entries. This
1533 will only have work to do if an error was thrown while processing the
1534 dwarf. If no error was thrown then the queue entries should have all
1535 been processed, and freed, as we went along. */
1536
1537 class dwarf2_queue_guard
1538 {
1539 public:
1540 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1541 : m_per_objfile (per_objfile)
1542 {
1543 }
1544
1545 /* Free any entries remaining on the queue. There should only be
1546 entries left if we hit an error while processing the dwarf. */
1547 ~dwarf2_queue_guard ()
1548 {
1549 /* Ensure that no memory is allocated by the queue. */
1550 std::queue<dwarf2_queue_item> empty;
1551 std::swap (m_per_objfile->queue, empty);
1552 }
1553
1554 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1555
1556 private:
1557 dwarf2_per_objfile *m_per_objfile;
1558 };
1559
1560 dwarf2_queue_item::~dwarf2_queue_item ()
1561 {
1562 /* Anything still marked queued is likely to be in an
1563 inconsistent state, so discard it. */
1564 if (per_cu->queued)
1565 {
1566 if (per_cu->cu != NULL)
1567 free_one_cached_comp_unit (per_cu);
1568 per_cu->queued = 0;
1569 }
1570 }
1571
1572 /* The return type of find_file_and_directory. Note, the enclosed
1573 string pointers are only valid while this object is valid. */
1574
1575 struct file_and_directory
1576 {
1577 /* The filename. This is never NULL. */
1578 const char *name;
1579
1580 /* The compilation directory. NULL if not known. If we needed to
1581 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1582 points directly to the DW_AT_comp_dir string attribute owned by
1583 the obstack that owns the DIE. */
1584 const char *comp_dir;
1585
1586 /* If we needed to build a new string for comp_dir, this is what
1587 owns the storage. */
1588 std::string comp_dir_storage;
1589 };
1590
1591 static file_and_directory find_file_and_directory (struct die_info *die,
1592 struct dwarf2_cu *cu);
1593
1594 static htab_up allocate_signatured_type_table ();
1595
1596 static htab_up allocate_dwo_unit_table ();
1597
1598 static struct dwo_unit *lookup_dwo_unit_in_dwp
1599 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1600 struct dwp_file *dwp_file, const char *comp_dir,
1601 ULONGEST signature, int is_debug_types);
1602
1603 static struct dwp_file *get_dwp_file
1604 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1605
1606 static struct dwo_unit *lookup_dwo_comp_unit
1607 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1608
1609 static struct dwo_unit *lookup_dwo_type_unit
1610 (struct signatured_type *, const char *, const char *);
1611
1612 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1613
1614 /* A unique pointer to a dwo_file. */
1615
1616 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1617
1618 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1619
1620 static void check_producer (struct dwarf2_cu *cu);
1621
1622 static void free_line_header_voidp (void *arg);
1623 \f
1624 /* Various complaints about symbol reading that don't abort the process. */
1625
1626 static void
1627 dwarf2_debug_line_missing_file_complaint (void)
1628 {
1629 complaint (_(".debug_line section has line data without a file"));
1630 }
1631
1632 static void
1633 dwarf2_debug_line_missing_end_sequence_complaint (void)
1634 {
1635 complaint (_(".debug_line section has line "
1636 "program sequence without an end"));
1637 }
1638
1639 static void
1640 dwarf2_complex_location_expr_complaint (void)
1641 {
1642 complaint (_("location expression too complex"));
1643 }
1644
1645 static void
1646 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1647 int arg3)
1648 {
1649 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1650 arg1, arg2, arg3);
1651 }
1652
1653 static void
1654 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1655 {
1656 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1657 arg1, arg2);
1658 }
1659
1660 /* Hash function for line_header_hash. */
1661
1662 static hashval_t
1663 line_header_hash (const struct line_header *ofs)
1664 {
1665 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1666 }
1667
1668 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1669
1670 static hashval_t
1671 line_header_hash_voidp (const void *item)
1672 {
1673 const struct line_header *ofs = (const struct line_header *) item;
1674
1675 return line_header_hash (ofs);
1676 }
1677
1678 /* Equality function for line_header_hash. */
1679
1680 static int
1681 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1682 {
1683 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1684 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1685
1686 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1687 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1688 }
1689
1690 \f
1691
1692 /* See declaration. */
1693
1694 dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
1695 const dwarf2_debug_sections *names,
1696 bool can_copy_)
1697 : objfile (objfile_),
1698 can_copy (can_copy_)
1699 {
1700 if (names == NULL)
1701 names = &dwarf2_elf_names;
1702
1703 bfd *obfd = objfile->obfd;
1704
1705 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1706 locate_sections (obfd, sec, *names);
1707 }
1708
1709 dwarf2_per_objfile::~dwarf2_per_objfile ()
1710 {
1711 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1712 free_cached_comp_units ();
1713
1714 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1715 per_cu->imported_symtabs_free ();
1716
1717 for (signatured_type *sig_type : all_type_units)
1718 sig_type->per_cu.imported_symtabs_free ();
1719
1720 /* Everything else should be on the objfile obstack. */
1721 }
1722
1723 /* See declaration. */
1724
1725 void
1726 dwarf2_per_objfile::free_cached_comp_units ()
1727 {
1728 dwarf2_per_cu_data *per_cu = read_in_chain;
1729 dwarf2_per_cu_data **last_chain = &read_in_chain;
1730 while (per_cu != NULL)
1731 {
1732 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1733
1734 delete per_cu->cu;
1735 *last_chain = next_cu;
1736 per_cu = next_cu;
1737 }
1738 }
1739
1740 /* A helper class that calls free_cached_comp_units on
1741 destruction. */
1742
1743 class free_cached_comp_units
1744 {
1745 public:
1746
1747 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1748 : m_per_objfile (per_objfile)
1749 {
1750 }
1751
1752 ~free_cached_comp_units ()
1753 {
1754 m_per_objfile->free_cached_comp_units ();
1755 }
1756
1757 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1758
1759 private:
1760
1761 dwarf2_per_objfile *m_per_objfile;
1762 };
1763
1764 /* Try to locate the sections we need for DWARF 2 debugging
1765 information and return true if we have enough to do something.
1766 NAMES points to the dwarf2 section names, or is NULL if the standard
1767 ELF names are used. CAN_COPY is true for formats where symbol
1768 interposition is possible and so symbol values must follow copy
1769 relocation rules. */
1770
1771 int
1772 dwarf2_has_info (struct objfile *objfile,
1773 const struct dwarf2_debug_sections *names,
1774 bool can_copy)
1775 {
1776 if (objfile->flags & OBJF_READNEVER)
1777 return 0;
1778
1779 struct dwarf2_per_objfile *dwarf2_per_objfile
1780 = get_dwarf2_per_objfile (objfile);
1781
1782 if (dwarf2_per_objfile == NULL)
1783 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
1784 names,
1785 can_copy);
1786
1787 return (!dwarf2_per_objfile->info.is_virtual
1788 && dwarf2_per_objfile->info.s.section != NULL
1789 && !dwarf2_per_objfile->abbrev.is_virtual
1790 && dwarf2_per_objfile->abbrev.s.section != NULL);
1791 }
1792
1793 /* When loading sections, we look either for uncompressed section or for
1794 compressed section names. */
1795
1796 static int
1797 section_is_p (const char *section_name,
1798 const struct dwarf2_section_names *names)
1799 {
1800 if (names->normal != NULL
1801 && strcmp (section_name, names->normal) == 0)
1802 return 1;
1803 if (names->compressed != NULL
1804 && strcmp (section_name, names->compressed) == 0)
1805 return 1;
1806 return 0;
1807 }
1808
1809 /* See declaration. */
1810
1811 void
1812 dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1813 const dwarf2_debug_sections &names)
1814 {
1815 flagword aflag = bfd_section_flags (sectp);
1816
1817 if ((aflag & SEC_HAS_CONTENTS) == 0)
1818 {
1819 }
1820 else if (elf_section_data (sectp)->this_hdr.sh_size
1821 > bfd_get_file_size (abfd))
1822 {
1823 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1824 warning (_("Discarding section %s which has a section size (%s"
1825 ") larger than the file size [in module %s]"),
1826 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1827 bfd_get_filename (abfd));
1828 }
1829 else if (section_is_p (sectp->name, &names.info))
1830 {
1831 this->info.s.section = sectp;
1832 this->info.size = bfd_section_size (sectp);
1833 }
1834 else if (section_is_p (sectp->name, &names.abbrev))
1835 {
1836 this->abbrev.s.section = sectp;
1837 this->abbrev.size = bfd_section_size (sectp);
1838 }
1839 else if (section_is_p (sectp->name, &names.line))
1840 {
1841 this->line.s.section = sectp;
1842 this->line.size = bfd_section_size (sectp);
1843 }
1844 else if (section_is_p (sectp->name, &names.loc))
1845 {
1846 this->loc.s.section = sectp;
1847 this->loc.size = bfd_section_size (sectp);
1848 }
1849 else if (section_is_p (sectp->name, &names.loclists))
1850 {
1851 this->loclists.s.section = sectp;
1852 this->loclists.size = bfd_section_size (sectp);
1853 }
1854 else if (section_is_p (sectp->name, &names.macinfo))
1855 {
1856 this->macinfo.s.section = sectp;
1857 this->macinfo.size = bfd_section_size (sectp);
1858 }
1859 else if (section_is_p (sectp->name, &names.macro))
1860 {
1861 this->macro.s.section = sectp;
1862 this->macro.size = bfd_section_size (sectp);
1863 }
1864 else if (section_is_p (sectp->name, &names.str))
1865 {
1866 this->str.s.section = sectp;
1867 this->str.size = bfd_section_size (sectp);
1868 }
1869 else if (section_is_p (sectp->name, &names.str_offsets))
1870 {
1871 this->str_offsets.s.section = sectp;
1872 this->str_offsets.size = bfd_section_size (sectp);
1873 }
1874 else if (section_is_p (sectp->name, &names.line_str))
1875 {
1876 this->line_str.s.section = sectp;
1877 this->line_str.size = bfd_section_size (sectp);
1878 }
1879 else if (section_is_p (sectp->name, &names.addr))
1880 {
1881 this->addr.s.section = sectp;
1882 this->addr.size = bfd_section_size (sectp);
1883 }
1884 else if (section_is_p (sectp->name, &names.frame))
1885 {
1886 this->frame.s.section = sectp;
1887 this->frame.size = bfd_section_size (sectp);
1888 }
1889 else if (section_is_p (sectp->name, &names.eh_frame))
1890 {
1891 this->eh_frame.s.section = sectp;
1892 this->eh_frame.size = bfd_section_size (sectp);
1893 }
1894 else if (section_is_p (sectp->name, &names.ranges))
1895 {
1896 this->ranges.s.section = sectp;
1897 this->ranges.size = bfd_section_size (sectp);
1898 }
1899 else if (section_is_p (sectp->name, &names.rnglists))
1900 {
1901 this->rnglists.s.section = sectp;
1902 this->rnglists.size = bfd_section_size (sectp);
1903 }
1904 else if (section_is_p (sectp->name, &names.types))
1905 {
1906 struct dwarf2_section_info type_section;
1907
1908 memset (&type_section, 0, sizeof (type_section));
1909 type_section.s.section = sectp;
1910 type_section.size = bfd_section_size (sectp);
1911
1912 this->types.push_back (type_section);
1913 }
1914 else if (section_is_p (sectp->name, &names.gdb_index))
1915 {
1916 this->gdb_index.s.section = sectp;
1917 this->gdb_index.size = bfd_section_size (sectp);
1918 }
1919 else if (section_is_p (sectp->name, &names.debug_names))
1920 {
1921 this->debug_names.s.section = sectp;
1922 this->debug_names.size = bfd_section_size (sectp);
1923 }
1924 else if (section_is_p (sectp->name, &names.debug_aranges))
1925 {
1926 this->debug_aranges.s.section = sectp;
1927 this->debug_aranges.size = bfd_section_size (sectp);
1928 }
1929
1930 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1931 && bfd_section_vma (sectp) == 0)
1932 this->has_section_at_zero = true;
1933 }
1934
1935 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1936 SECTION_NAME. */
1937
1938 void
1939 dwarf2_get_section_info (struct objfile *objfile,
1940 enum dwarf2_section_enum sect,
1941 asection **sectp, const gdb_byte **bufp,
1942 bfd_size_type *sizep)
1943 {
1944 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
1945 struct dwarf2_section_info *info;
1946
1947 /* We may see an objfile without any DWARF, in which case we just
1948 return nothing. */
1949 if (data == NULL)
1950 {
1951 *sectp = NULL;
1952 *bufp = NULL;
1953 *sizep = 0;
1954 return;
1955 }
1956 switch (sect)
1957 {
1958 case DWARF2_DEBUG_FRAME:
1959 info = &data->frame;
1960 break;
1961 case DWARF2_EH_FRAME:
1962 info = &data->eh_frame;
1963 break;
1964 default:
1965 gdb_assert_not_reached ("unexpected section");
1966 }
1967
1968 info->read (objfile);
1969
1970 *sectp = info->get_bfd_section ();
1971 *bufp = info->buffer;
1972 *sizep = info->size;
1973 }
1974
1975 /* A helper function to find the sections for a .dwz file. */
1976
1977 static void
1978 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1979 {
1980 struct dwz_file *dwz_file = (struct dwz_file *) arg;
1981
1982 /* Note that we only support the standard ELF names, because .dwz
1983 is ELF-only (at the time of writing). */
1984 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1985 {
1986 dwz_file->abbrev.s.section = sectp;
1987 dwz_file->abbrev.size = bfd_section_size (sectp);
1988 }
1989 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1990 {
1991 dwz_file->info.s.section = sectp;
1992 dwz_file->info.size = bfd_section_size (sectp);
1993 }
1994 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1995 {
1996 dwz_file->str.s.section = sectp;
1997 dwz_file->str.size = bfd_section_size (sectp);
1998 }
1999 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2000 {
2001 dwz_file->line.s.section = sectp;
2002 dwz_file->line.size = bfd_section_size (sectp);
2003 }
2004 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2005 {
2006 dwz_file->macro.s.section = sectp;
2007 dwz_file->macro.size = bfd_section_size (sectp);
2008 }
2009 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2010 {
2011 dwz_file->gdb_index.s.section = sectp;
2012 dwz_file->gdb_index.size = bfd_section_size (sectp);
2013 }
2014 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2015 {
2016 dwz_file->debug_names.s.section = sectp;
2017 dwz_file->debug_names.size = bfd_section_size (sectp);
2018 }
2019 }
2020
2021 /* See dwarf2read.h. */
2022
2023 struct dwz_file *
2024 dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
2025 {
2026 const char *filename;
2027 bfd_size_type buildid_len_arg;
2028 size_t buildid_len;
2029 bfd_byte *buildid;
2030
2031 if (dwarf2_per_objfile->dwz_file != NULL)
2032 return dwarf2_per_objfile->dwz_file.get ();
2033
2034 bfd_set_error (bfd_error_no_error);
2035 gdb::unique_xmalloc_ptr<char> data
2036 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2037 &buildid_len_arg, &buildid));
2038 if (data == NULL)
2039 {
2040 if (bfd_get_error () == bfd_error_no_error)
2041 return NULL;
2042 error (_("could not read '.gnu_debugaltlink' section: %s"),
2043 bfd_errmsg (bfd_get_error ()));
2044 }
2045
2046 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2047
2048 buildid_len = (size_t) buildid_len_arg;
2049
2050 filename = data.get ();
2051
2052 std::string abs_storage;
2053 if (!IS_ABSOLUTE_PATH (filename))
2054 {
2055 gdb::unique_xmalloc_ptr<char> abs
2056 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
2057
2058 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2059 filename = abs_storage.c_str ();
2060 }
2061
2062 /* First try the file name given in the section. If that doesn't
2063 work, try to use the build-id instead. */
2064 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
2065 if (dwz_bfd != NULL)
2066 {
2067 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2068 dwz_bfd.reset (nullptr);
2069 }
2070
2071 if (dwz_bfd == NULL)
2072 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2073
2074 if (dwz_bfd == nullptr)
2075 {
2076 gdb::unique_xmalloc_ptr<char> alt_filename;
2077 const char *origname = dwarf2_per_objfile->objfile->original_name;
2078
2079 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2080 buildid_len,
2081 origname,
2082 &alt_filename));
2083
2084 if (fd.get () >= 0)
2085 {
2086 /* File successfully retrieved from server. */
2087 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2088
2089 if (dwz_bfd == nullptr)
2090 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2091 alt_filename.get ());
2092 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2093 dwz_bfd.reset (nullptr);
2094 }
2095 }
2096
2097 if (dwz_bfd == NULL)
2098 error (_("could not find '.gnu_debugaltlink' file for %s"),
2099 objfile_name (dwarf2_per_objfile->objfile));
2100
2101 std::unique_ptr<struct dwz_file> result
2102 (new struct dwz_file (std::move (dwz_bfd)));
2103
2104 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2105 result.get ());
2106
2107 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2108 result->dwz_bfd.get ());
2109 dwarf2_per_objfile->dwz_file = std::move (result);
2110 return dwarf2_per_objfile->dwz_file.get ();
2111 }
2112 \f
2113 /* DWARF quick_symbols_functions support. */
2114
2115 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2116 unique line tables, so we maintain a separate table of all .debug_line
2117 derived entries to support the sharing.
2118 All the quick functions need is the list of file names. We discard the
2119 line_header when we're done and don't need to record it here. */
2120 struct quick_file_names
2121 {
2122 /* The data used to construct the hash key. */
2123 struct stmt_list_hash hash;
2124
2125 /* The number of entries in file_names, real_names. */
2126 unsigned int num_file_names;
2127
2128 /* The file names from the line table, after being run through
2129 file_full_name. */
2130 const char **file_names;
2131
2132 /* The file names from the line table after being run through
2133 gdb_realpath. These are computed lazily. */
2134 const char **real_names;
2135 };
2136
2137 /* When using the index (and thus not using psymtabs), each CU has an
2138 object of this type. This is used to hold information needed by
2139 the various "quick" methods. */
2140 struct dwarf2_per_cu_quick_data
2141 {
2142 /* The file table. This can be NULL if there was no file table
2143 or it's currently not read in.
2144 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2145 struct quick_file_names *file_names;
2146
2147 /* The corresponding symbol table. This is NULL if symbols for this
2148 CU have not yet been read. */
2149 struct compunit_symtab *compunit_symtab;
2150
2151 /* A temporary mark bit used when iterating over all CUs in
2152 expand_symtabs_matching. */
2153 unsigned int mark : 1;
2154
2155 /* True if we've tried to read the file table and found there isn't one.
2156 There will be no point in trying to read it again next time. */
2157 unsigned int no_file_data : 1;
2158 };
2159
2160 /* Utility hash function for a stmt_list_hash. */
2161
2162 static hashval_t
2163 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2164 {
2165 hashval_t v = 0;
2166
2167 if (stmt_list_hash->dwo_unit != NULL)
2168 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2169 v += to_underlying (stmt_list_hash->line_sect_off);
2170 return v;
2171 }
2172
2173 /* Utility equality function for a stmt_list_hash. */
2174
2175 static int
2176 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2177 const struct stmt_list_hash *rhs)
2178 {
2179 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2180 return 0;
2181 if (lhs->dwo_unit != NULL
2182 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2183 return 0;
2184
2185 return lhs->line_sect_off == rhs->line_sect_off;
2186 }
2187
2188 /* Hash function for a quick_file_names. */
2189
2190 static hashval_t
2191 hash_file_name_entry (const void *e)
2192 {
2193 const struct quick_file_names *file_data
2194 = (const struct quick_file_names *) e;
2195
2196 return hash_stmt_list_entry (&file_data->hash);
2197 }
2198
2199 /* Equality function for a quick_file_names. */
2200
2201 static int
2202 eq_file_name_entry (const void *a, const void *b)
2203 {
2204 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2205 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2206
2207 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2208 }
2209
2210 /* Delete function for a quick_file_names. */
2211
2212 static void
2213 delete_file_name_entry (void *e)
2214 {
2215 struct quick_file_names *file_data = (struct quick_file_names *) e;
2216 int i;
2217
2218 for (i = 0; i < file_data->num_file_names; ++i)
2219 {
2220 xfree ((void*) file_data->file_names[i]);
2221 if (file_data->real_names)
2222 xfree ((void*) file_data->real_names[i]);
2223 }
2224
2225 /* The space for the struct itself lives on objfile_obstack,
2226 so we don't free it here. */
2227 }
2228
2229 /* Create a quick_file_names hash table. */
2230
2231 static htab_up
2232 create_quick_file_names_table (unsigned int nr_initial_entries)
2233 {
2234 return htab_up (htab_create_alloc (nr_initial_entries,
2235 hash_file_name_entry, eq_file_name_entry,
2236 delete_file_name_entry, xcalloc, xfree));
2237 }
2238
2239 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2240 have to be created afterwards. You should call age_cached_comp_units after
2241 processing PER_CU->CU. dw2_setup must have been already called. */
2242
2243 static void
2244 load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2245 {
2246 if (per_cu->is_debug_types)
2247 load_full_type_unit (per_cu);
2248 else
2249 load_full_comp_unit (per_cu, skip_partial, language_minimal);
2250
2251 if (per_cu->cu == NULL)
2252 return; /* Dummy CU. */
2253
2254 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2255 }
2256
2257 /* Read in the symbols for PER_CU. */
2258
2259 static void
2260 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2261 {
2262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2263
2264 /* Skip type_unit_groups, reading the type units they contain
2265 is handled elsewhere. */
2266 if (per_cu->type_unit_group_p ())
2267 return;
2268
2269 /* The destructor of dwarf2_queue_guard frees any entries left on
2270 the queue. After this point we're guaranteed to leave this function
2271 with the dwarf queue empty. */
2272 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2273
2274 if (dwarf2_per_objfile->using_index
2275 ? per_cu->v.quick->compunit_symtab == NULL
2276 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2277 {
2278 queue_comp_unit (per_cu, language_minimal);
2279 load_cu (per_cu, skip_partial);
2280
2281 /* If we just loaded a CU from a DWO, and we're working with an index
2282 that may badly handle TUs, load all the TUs in that DWO as well.
2283 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2284 if (!per_cu->is_debug_types
2285 && per_cu->cu != NULL
2286 && per_cu->cu->dwo_unit != NULL
2287 && dwarf2_per_objfile->index_table != NULL
2288 && dwarf2_per_objfile->index_table->version <= 7
2289 /* DWP files aren't supported yet. */
2290 && get_dwp_file (dwarf2_per_objfile) == NULL)
2291 queue_and_load_all_dwo_tus (per_cu);
2292 }
2293
2294 process_queue (dwarf2_per_objfile);
2295
2296 /* Age the cache, releasing compilation units that have not
2297 been used recently. */
2298 age_cached_comp_units (dwarf2_per_objfile);
2299 }
2300
2301 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2302 the objfile from which this CU came. Returns the resulting symbol
2303 table. */
2304
2305 static struct compunit_symtab *
2306 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
2307 {
2308 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2309
2310 gdb_assert (dwarf2_per_objfile->using_index);
2311 if (!per_cu->v.quick->compunit_symtab)
2312 {
2313 free_cached_comp_units freer (dwarf2_per_objfile);
2314 scoped_restore decrementer = increment_reading_symtab ();
2315 dw2_do_instantiate_symtab (per_cu, skip_partial);
2316 process_cu_includes (dwarf2_per_objfile);
2317 }
2318
2319 return per_cu->v.quick->compunit_symtab;
2320 }
2321
2322 /* See declaration. */
2323
2324 dwarf2_per_cu_data *
2325 dwarf2_per_objfile::get_cutu (int index)
2326 {
2327 if (index >= this->all_comp_units.size ())
2328 {
2329 index -= this->all_comp_units.size ();
2330 gdb_assert (index < this->all_type_units.size ());
2331 return &this->all_type_units[index]->per_cu;
2332 }
2333
2334 return this->all_comp_units[index];
2335 }
2336
2337 /* See declaration. */
2338
2339 dwarf2_per_cu_data *
2340 dwarf2_per_objfile::get_cu (int index)
2341 {
2342 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2343
2344 return this->all_comp_units[index];
2345 }
2346
2347 /* See declaration. */
2348
2349 signatured_type *
2350 dwarf2_per_objfile::get_tu (int index)
2351 {
2352 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2353
2354 return this->all_type_units[index];
2355 }
2356
2357 /* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2358 objfile_obstack, and constructed with the specified field
2359 values. */
2360
2361 static dwarf2_per_cu_data *
2362 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2363 struct dwarf2_section_info *section,
2364 int is_dwz,
2365 sect_offset sect_off, ULONGEST length)
2366 {
2367 struct objfile *objfile = dwarf2_per_objfile->objfile;
2368 dwarf2_per_cu_data *the_cu
2369 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2370 struct dwarf2_per_cu_data);
2371 the_cu->sect_off = sect_off;
2372 the_cu->length = length;
2373 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2374 the_cu->section = section;
2375 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2376 struct dwarf2_per_cu_quick_data);
2377 the_cu->is_dwz = is_dwz;
2378 return the_cu;
2379 }
2380
2381 /* A helper for create_cus_from_index that handles a given list of
2382 CUs. */
2383
2384 static void
2385 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2386 const gdb_byte *cu_list, offset_type n_elements,
2387 struct dwarf2_section_info *section,
2388 int is_dwz)
2389 {
2390 for (offset_type i = 0; i < n_elements; i += 2)
2391 {
2392 gdb_static_assert (sizeof (ULONGEST) >= 8);
2393
2394 sect_offset sect_off
2395 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2396 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2397 cu_list += 2 * 8;
2398
2399 dwarf2_per_cu_data *per_cu
2400 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2401 sect_off, length);
2402 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
2403 }
2404 }
2405
2406 /* Read the CU list from the mapped index, and use it to create all
2407 the CU objects for this objfile. */
2408
2409 static void
2410 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2411 const gdb_byte *cu_list, offset_type cu_list_elements,
2412 const gdb_byte *dwz_list, offset_type dwz_elements)
2413 {
2414 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2415 dwarf2_per_objfile->all_comp_units.reserve
2416 ((cu_list_elements + dwz_elements) / 2);
2417
2418 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2419 &dwarf2_per_objfile->info, 0);
2420
2421 if (dwz_elements == 0)
2422 return;
2423
2424 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2425 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2426 &dwz->info, 1);
2427 }
2428
2429 /* Create the signatured type hash table from the index. */
2430
2431 static void
2432 create_signatured_type_table_from_index
2433 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2434 struct dwarf2_section_info *section,
2435 const gdb_byte *bytes,
2436 offset_type elements)
2437 {
2438 struct objfile *objfile = dwarf2_per_objfile->objfile;
2439
2440 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2441 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
2442
2443 htab_up sig_types_hash = allocate_signatured_type_table ();
2444
2445 for (offset_type i = 0; i < elements; i += 3)
2446 {
2447 struct signatured_type *sig_type;
2448 ULONGEST signature;
2449 void **slot;
2450 cu_offset type_offset_in_tu;
2451
2452 gdb_static_assert (sizeof (ULONGEST) >= 8);
2453 sect_offset sect_off
2454 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2455 type_offset_in_tu
2456 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2457 BFD_ENDIAN_LITTLE);
2458 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2459 bytes += 3 * 8;
2460
2461 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2462 struct signatured_type);
2463 sig_type->signature = signature;
2464 sig_type->type_offset_in_tu = type_offset_in_tu;
2465 sig_type->per_cu.is_debug_types = 1;
2466 sig_type->per_cu.section = section;
2467 sig_type->per_cu.sect_off = sect_off;
2468 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2469 sig_type->per_cu.v.quick
2470 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2471 struct dwarf2_per_cu_quick_data);
2472
2473 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2474 *slot = sig_type;
2475
2476 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2477 }
2478
2479 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2480 }
2481
2482 /* Create the signatured type hash table from .debug_names. */
2483
2484 static void
2485 create_signatured_type_table_from_debug_names
2486 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2487 const mapped_debug_names &map,
2488 struct dwarf2_section_info *section,
2489 struct dwarf2_section_info *abbrev_section)
2490 {
2491 struct objfile *objfile = dwarf2_per_objfile->objfile;
2492
2493 section->read (objfile);
2494 abbrev_section->read (objfile);
2495
2496 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2497 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
2498
2499 htab_up sig_types_hash = allocate_signatured_type_table ();
2500
2501 for (uint32_t i = 0; i < map.tu_count; ++i)
2502 {
2503 struct signatured_type *sig_type;
2504 void **slot;
2505
2506 sect_offset sect_off
2507 = (sect_offset) (extract_unsigned_integer
2508 (map.tu_table_reordered + i * map.offset_size,
2509 map.offset_size,
2510 map.dwarf5_byte_order));
2511
2512 comp_unit_head cu_header;
2513 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2514 abbrev_section,
2515 section->buffer + to_underlying (sect_off),
2516 rcuh_kind::TYPE);
2517
2518 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2519 struct signatured_type);
2520 sig_type->signature = cu_header.signature;
2521 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2522 sig_type->per_cu.is_debug_types = 1;
2523 sig_type->per_cu.section = section;
2524 sig_type->per_cu.sect_off = sect_off;
2525 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2526 sig_type->per_cu.v.quick
2527 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2528 struct dwarf2_per_cu_quick_data);
2529
2530 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2531 *slot = sig_type;
2532
2533 dwarf2_per_objfile->all_type_units.push_back (sig_type);
2534 }
2535
2536 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
2537 }
2538
2539 /* Read the address map data from the mapped index, and use it to
2540 populate the objfile's psymtabs_addrmap. */
2541
2542 static void
2543 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2544 struct mapped_index *index)
2545 {
2546 struct objfile *objfile = dwarf2_per_objfile->objfile;
2547 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2548 const gdb_byte *iter, *end;
2549 struct addrmap *mutable_map;
2550 CORE_ADDR baseaddr;
2551
2552 auto_obstack temp_obstack;
2553
2554 mutable_map = addrmap_create_mutable (&temp_obstack);
2555
2556 iter = index->address_table.data ();
2557 end = iter + index->address_table.size ();
2558
2559 baseaddr = objfile->text_section_offset ();
2560
2561 while (iter < end)
2562 {
2563 ULONGEST hi, lo, cu_index;
2564 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2565 iter += 8;
2566 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2567 iter += 8;
2568 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2569 iter += 4;
2570
2571 if (lo > hi)
2572 {
2573 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2574 hex_string (lo), hex_string (hi));
2575 continue;
2576 }
2577
2578 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
2579 {
2580 complaint (_(".gdb_index address table has invalid CU number %u"),
2581 (unsigned) cu_index);
2582 continue;
2583 }
2584
2585 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2586 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2587 addrmap_set_empty (mutable_map, lo, hi - 1,
2588 dwarf2_per_objfile->get_cu (cu_index));
2589 }
2590
2591 objfile->partial_symtabs->psymtabs_addrmap
2592 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2593 }
2594
2595 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2596 populate the objfile's psymtabs_addrmap. */
2597
2598 static void
2599 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2600 struct dwarf2_section_info *section)
2601 {
2602 struct objfile *objfile = dwarf2_per_objfile->objfile;
2603 bfd *abfd = objfile->obfd;
2604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2605 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2606
2607 auto_obstack temp_obstack;
2608 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2609
2610 std::unordered_map<sect_offset,
2611 dwarf2_per_cu_data *,
2612 gdb::hash_enum<sect_offset>>
2613 debug_info_offset_to_per_cu;
2614 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
2615 {
2616 const auto insertpair
2617 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2618 if (!insertpair.second)
2619 {
2620 warning (_("Section .debug_aranges in %s has duplicate "
2621 "debug_info_offset %s, ignoring .debug_aranges."),
2622 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2623 return;
2624 }
2625 }
2626
2627 section->read (objfile);
2628
2629 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2630
2631 const gdb_byte *addr = section->buffer;
2632
2633 while (addr < section->buffer + section->size)
2634 {
2635 const gdb_byte *const entry_addr = addr;
2636 unsigned int bytes_read;
2637
2638 const LONGEST entry_length = read_initial_length (abfd, addr,
2639 &bytes_read);
2640 addr += bytes_read;
2641
2642 const gdb_byte *const entry_end = addr + entry_length;
2643 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2644 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2645 if (addr + entry_length > section->buffer + section->size)
2646 {
2647 warning (_("Section .debug_aranges in %s entry at offset %s "
2648 "length %s exceeds section length %s, "
2649 "ignoring .debug_aranges."),
2650 objfile_name (objfile),
2651 plongest (entry_addr - section->buffer),
2652 plongest (bytes_read + entry_length),
2653 pulongest (section->size));
2654 return;
2655 }
2656
2657 /* The version number. */
2658 const uint16_t version = read_2_bytes (abfd, addr);
2659 addr += 2;
2660 if (version != 2)
2661 {
2662 warning (_("Section .debug_aranges in %s entry at offset %s "
2663 "has unsupported version %d, ignoring .debug_aranges."),
2664 objfile_name (objfile),
2665 plongest (entry_addr - section->buffer), version);
2666 return;
2667 }
2668
2669 const uint64_t debug_info_offset
2670 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2671 addr += offset_size;
2672 const auto per_cu_it
2673 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2674 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2675 {
2676 warning (_("Section .debug_aranges in %s entry at offset %s "
2677 "debug_info_offset %s does not exists, "
2678 "ignoring .debug_aranges."),
2679 objfile_name (objfile),
2680 plongest (entry_addr - section->buffer),
2681 pulongest (debug_info_offset));
2682 return;
2683 }
2684 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2685
2686 const uint8_t address_size = *addr++;
2687 if (address_size < 1 || address_size > 8)
2688 {
2689 warning (_("Section .debug_aranges in %s entry at offset %s "
2690 "address_size %u is invalid, ignoring .debug_aranges."),
2691 objfile_name (objfile),
2692 plongest (entry_addr - section->buffer), address_size);
2693 return;
2694 }
2695
2696 const uint8_t segment_selector_size = *addr++;
2697 if (segment_selector_size != 0)
2698 {
2699 warning (_("Section .debug_aranges in %s entry at offset %s "
2700 "segment_selector_size %u is not supported, "
2701 "ignoring .debug_aranges."),
2702 objfile_name (objfile),
2703 plongest (entry_addr - section->buffer),
2704 segment_selector_size);
2705 return;
2706 }
2707
2708 /* Must pad to an alignment boundary that is twice the address
2709 size. It is undocumented by the DWARF standard but GCC does
2710 use it. */
2711 for (size_t padding = ((-(addr - section->buffer))
2712 & (2 * address_size - 1));
2713 padding > 0; padding--)
2714 if (*addr++ != 0)
2715 {
2716 warning (_("Section .debug_aranges in %s entry at offset %s "
2717 "padding is not zero, ignoring .debug_aranges."),
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer));
2720 return;
2721 }
2722
2723 for (;;)
2724 {
2725 if (addr + 2 * address_size > entry_end)
2726 {
2727 warning (_("Section .debug_aranges in %s entry at offset %s "
2728 "address list is not properly terminated, "
2729 "ignoring .debug_aranges."),
2730 objfile_name (objfile),
2731 plongest (entry_addr - section->buffer));
2732 return;
2733 }
2734 ULONGEST start = extract_unsigned_integer (addr, address_size,
2735 dwarf5_byte_order);
2736 addr += address_size;
2737 ULONGEST length = extract_unsigned_integer (addr, address_size,
2738 dwarf5_byte_order);
2739 addr += address_size;
2740 if (start == 0 && length == 0)
2741 break;
2742 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2743 {
2744 /* Symbol was eliminated due to a COMDAT group. */
2745 continue;
2746 }
2747 ULONGEST end = start + length;
2748 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2749 - baseaddr);
2750 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2751 - baseaddr);
2752 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2753 }
2754 }
2755
2756 objfile->partial_symtabs->psymtabs_addrmap
2757 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2758 }
2759
2760 /* Find a slot in the mapped index INDEX for the object named NAME.
2761 If NAME is found, set *VEC_OUT to point to the CU vector in the
2762 constant pool and return true. If NAME cannot be found, return
2763 false. */
2764
2765 static bool
2766 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2767 offset_type **vec_out)
2768 {
2769 offset_type hash;
2770 offset_type slot, step;
2771 int (*cmp) (const char *, const char *);
2772
2773 gdb::unique_xmalloc_ptr<char> without_params;
2774 if (current_language->la_language == language_cplus
2775 || current_language->la_language == language_fortran
2776 || current_language->la_language == language_d)
2777 {
2778 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2779 not contain any. */
2780
2781 if (strchr (name, '(') != NULL)
2782 {
2783 without_params = cp_remove_params (name);
2784
2785 if (without_params != NULL)
2786 name = without_params.get ();
2787 }
2788 }
2789
2790 /* Index version 4 did not support case insensitive searches. But the
2791 indices for case insensitive languages are built in lowercase, therefore
2792 simulate our NAME being searched is also lowercased. */
2793 hash = mapped_index_string_hash ((index->version == 4
2794 && case_sensitivity == case_sensitive_off
2795 ? 5 : index->version),
2796 name);
2797
2798 slot = hash & (index->symbol_table.size () - 1);
2799 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2800 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2801
2802 for (;;)
2803 {
2804 const char *str;
2805
2806 const auto &bucket = index->symbol_table[slot];
2807 if (bucket.name == 0 && bucket.vec == 0)
2808 return false;
2809
2810 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2811 if (!cmp (name, str))
2812 {
2813 *vec_out = (offset_type *) (index->constant_pool
2814 + MAYBE_SWAP (bucket.vec));
2815 return true;
2816 }
2817
2818 slot = (slot + step) & (index->symbol_table.size () - 1);
2819 }
2820 }
2821
2822 /* A helper function that reads the .gdb_index from BUFFER and fills
2823 in MAP. FILENAME is the name of the file containing the data;
2824 it is used for error reporting. DEPRECATED_OK is true if it is
2825 ok to use deprecated sections.
2826
2827 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2828 out parameters that are filled in with information about the CU and
2829 TU lists in the section.
2830
2831 Returns true if all went well, false otherwise. */
2832
2833 static bool
2834 read_gdb_index_from_buffer (struct objfile *objfile,
2835 const char *filename,
2836 bool deprecated_ok,
2837 gdb::array_view<const gdb_byte> buffer,
2838 struct mapped_index *map,
2839 const gdb_byte **cu_list,
2840 offset_type *cu_list_elements,
2841 const gdb_byte **types_list,
2842 offset_type *types_list_elements)
2843 {
2844 const gdb_byte *addr = &buffer[0];
2845
2846 /* Version check. */
2847 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2848 /* Versions earlier than 3 emitted every copy of a psymbol. This
2849 causes the index to behave very poorly for certain requests. Version 3
2850 contained incomplete addrmap. So, it seems better to just ignore such
2851 indices. */
2852 if (version < 4)
2853 {
2854 static int warning_printed = 0;
2855 if (!warning_printed)
2856 {
2857 warning (_("Skipping obsolete .gdb_index section in %s."),
2858 filename);
2859 warning_printed = 1;
2860 }
2861 return 0;
2862 }
2863 /* Index version 4 uses a different hash function than index version
2864 5 and later.
2865
2866 Versions earlier than 6 did not emit psymbols for inlined
2867 functions. Using these files will cause GDB not to be able to
2868 set breakpoints on inlined functions by name, so we ignore these
2869 indices unless the user has done
2870 "set use-deprecated-index-sections on". */
2871 if (version < 6 && !deprecated_ok)
2872 {
2873 static int warning_printed = 0;
2874 if (!warning_printed)
2875 {
2876 warning (_("\
2877 Skipping deprecated .gdb_index section in %s.\n\
2878 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2879 to use the section anyway."),
2880 filename);
2881 warning_printed = 1;
2882 }
2883 return 0;
2884 }
2885 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2886 of the TU (for symbols coming from TUs),
2887 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2888 Plus gold-generated indices can have duplicate entries for global symbols,
2889 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2890 These are just performance bugs, and we can't distinguish gdb-generated
2891 indices from gold-generated ones, so issue no warning here. */
2892
2893 /* Indexes with higher version than the one supported by GDB may be no
2894 longer backward compatible. */
2895 if (version > 8)
2896 return 0;
2897
2898 map->version = version;
2899
2900 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2901
2902 int i = 0;
2903 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2904 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2905 / 8);
2906 ++i;
2907
2908 *types_list = addr + MAYBE_SWAP (metadata[i]);
2909 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2910 - MAYBE_SWAP (metadata[i]))
2911 / 8);
2912 ++i;
2913
2914 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2915 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2916 map->address_table
2917 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
2918 ++i;
2919
2920 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2921 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2922 map->symbol_table
2923 = gdb::array_view<mapped_index::symbol_table_slot>
2924 ((mapped_index::symbol_table_slot *) symbol_table,
2925 (mapped_index::symbol_table_slot *) symbol_table_end);
2926
2927 ++i;
2928 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
2929
2930 return 1;
2931 }
2932
2933 /* Callback types for dwarf2_read_gdb_index. */
2934
2935 typedef gdb::function_view
2936 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2937 get_gdb_index_contents_ftype;
2938 typedef gdb::function_view
2939 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2940 get_gdb_index_contents_dwz_ftype;
2941
2942 /* Read .gdb_index. If everything went ok, initialize the "quick"
2943 elements of all the CUs and return 1. Otherwise, return 0. */
2944
2945 static int
2946 dwarf2_read_gdb_index
2947 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2948 get_gdb_index_contents_ftype get_gdb_index_contents,
2949 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2950 {
2951 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2952 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
2953 struct dwz_file *dwz;
2954 struct objfile *objfile = dwarf2_per_objfile->objfile;
2955
2956 gdb::array_view<const gdb_byte> main_index_contents
2957 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2958
2959 if (main_index_contents.empty ())
2960 return 0;
2961
2962 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
2963 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2964 use_deprecated_index_sections,
2965 main_index_contents, map.get (), &cu_list,
2966 &cu_list_elements, &types_list,
2967 &types_list_elements))
2968 return 0;
2969
2970 /* Don't use the index if it's empty. */
2971 if (map->symbol_table.empty ())
2972 return 0;
2973
2974 /* If there is a .dwz file, read it so we can get its CU list as
2975 well. */
2976 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2977 if (dwz != NULL)
2978 {
2979 struct mapped_index dwz_map;
2980 const gdb_byte *dwz_types_ignore;
2981 offset_type dwz_types_elements_ignore;
2982
2983 gdb::array_view<const gdb_byte> dwz_index_content
2984 = get_gdb_index_contents_dwz (objfile, dwz);
2985
2986 if (dwz_index_content.empty ())
2987 return 0;
2988
2989 if (!read_gdb_index_from_buffer (objfile,
2990 bfd_get_filename (dwz->dwz_bfd.get ()),
2991 1, dwz_index_content, &dwz_map,
2992 &dwz_list, &dwz_list_elements,
2993 &dwz_types_ignore,
2994 &dwz_types_elements_ignore))
2995 {
2996 warning (_("could not read '.gdb_index' section from %s; skipping"),
2997 bfd_get_filename (dwz->dwz_bfd.get ()));
2998 return 0;
2999 }
3000 }
3001
3002 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3003 dwz_list, dwz_list_elements);
3004
3005 if (types_list_elements)
3006 {
3007 /* We can only handle a single .debug_types when we have an
3008 index. */
3009 if (dwarf2_per_objfile->types.size () != 1)
3010 return 0;
3011
3012 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
3013
3014 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3015 types_list, types_list_elements);
3016 }
3017
3018 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3019
3020 dwarf2_per_objfile->index_table = std::move (map);
3021 dwarf2_per_objfile->using_index = 1;
3022 dwarf2_per_objfile->quick_file_names_table =
3023 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
3024
3025 return 1;
3026 }
3027
3028 /* die_reader_func for dw2_get_file_names. */
3029
3030 static void
3031 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3032 const gdb_byte *info_ptr,
3033 struct die_info *comp_unit_die)
3034 {
3035 struct dwarf2_cu *cu = reader->cu;
3036 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3037 struct dwarf2_per_objfile *dwarf2_per_objfile
3038 = cu->per_cu->dwarf2_per_objfile;
3039 struct objfile *objfile = dwarf2_per_objfile->objfile;
3040 struct dwarf2_per_cu_data *lh_cu;
3041 struct attribute *attr;
3042 void **slot;
3043 struct quick_file_names *qfn;
3044
3045 gdb_assert (! this_cu->is_debug_types);
3046
3047 /* Our callers never want to match partial units -- instead they
3048 will match the enclosing full CU. */
3049 if (comp_unit_die->tag == DW_TAG_partial_unit)
3050 {
3051 this_cu->v.quick->no_file_data = 1;
3052 return;
3053 }
3054
3055 lh_cu = this_cu;
3056 slot = NULL;
3057
3058 line_header_up lh;
3059 sect_offset line_offset {};
3060
3061 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3062 if (attr != nullptr)
3063 {
3064 struct quick_file_names find_entry;
3065
3066 line_offset = (sect_offset) DW_UNSND (attr);
3067
3068 /* We may have already read in this line header (TU line header sharing).
3069 If we have we're done. */
3070 find_entry.hash.dwo_unit = cu->dwo_unit;
3071 find_entry.hash.line_sect_off = line_offset;
3072 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
3073 &find_entry, INSERT);
3074 if (*slot != NULL)
3075 {
3076 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3077 return;
3078 }
3079
3080 lh = dwarf_decode_line_header (line_offset, cu);
3081 }
3082 if (lh == NULL)
3083 {
3084 lh_cu->v.quick->no_file_data = 1;
3085 return;
3086 }
3087
3088 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
3089 qfn->hash.dwo_unit = cu->dwo_unit;
3090 qfn->hash.line_sect_off = line_offset;
3091 gdb_assert (slot != NULL);
3092 *slot = qfn;
3093
3094 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3095
3096 int offset = 0;
3097 if (strcmp (fnd.name, "<unknown>") != 0)
3098 ++offset;
3099
3100 qfn->num_file_names = offset + lh->file_names_size ();
3101 qfn->file_names =
3102 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3103 if (offset != 0)
3104 qfn->file_names[0] = xstrdup (fnd.name);
3105 for (int i = 0; i < lh->file_names_size (); ++i)
3106 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3107 fnd.comp_dir).release ();
3108 qfn->real_names = NULL;
3109
3110 lh_cu->v.quick->file_names = qfn;
3111 }
3112
3113 /* A helper for the "quick" functions which attempts to read the line
3114 table for THIS_CU. */
3115
3116 static struct quick_file_names *
3117 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
3118 {
3119 /* This should never be called for TUs. */
3120 gdb_assert (! this_cu->is_debug_types);
3121 /* Nor type unit groups. */
3122 gdb_assert (! this_cu->type_unit_group_p ());
3123
3124 if (this_cu->v.quick->file_names != NULL)
3125 return this_cu->v.quick->file_names;
3126 /* If we know there is no line data, no point in looking again. */
3127 if (this_cu->v.quick->no_file_data)
3128 return NULL;
3129
3130 cutu_reader reader (this_cu);
3131 if (!reader.dummy_p)
3132 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3133
3134 if (this_cu->v.quick->no_file_data)
3135 return NULL;
3136 return this_cu->v.quick->file_names;
3137 }
3138
3139 /* A helper for the "quick" functions which computes and caches the
3140 real path for a given file name from the line table. */
3141
3142 static const char *
3143 dw2_get_real_path (struct objfile *objfile,
3144 struct quick_file_names *qfn, int index)
3145 {
3146 if (qfn->real_names == NULL)
3147 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
3148 qfn->num_file_names, const char *);
3149
3150 if (qfn->real_names[index] == NULL)
3151 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3152
3153 return qfn->real_names[index];
3154 }
3155
3156 static struct symtab *
3157 dw2_find_last_source_symtab (struct objfile *objfile)
3158 {
3159 struct dwarf2_per_objfile *dwarf2_per_objfile
3160 = get_dwarf2_per_objfile (objfile);
3161 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
3162 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
3163
3164 if (cust == NULL)
3165 return NULL;
3166
3167 return compunit_primary_filetab (cust);
3168 }
3169
3170 /* Traversal function for dw2_forget_cached_source_info. */
3171
3172 static int
3173 dw2_free_cached_file_names (void **slot, void *info)
3174 {
3175 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3176
3177 if (file_data->real_names)
3178 {
3179 int i;
3180
3181 for (i = 0; i < file_data->num_file_names; ++i)
3182 {
3183 xfree ((void*) file_data->real_names[i]);
3184 file_data->real_names[i] = NULL;
3185 }
3186 }
3187
3188 return 1;
3189 }
3190
3191 static void
3192 dw2_forget_cached_source_info (struct objfile *objfile)
3193 {
3194 struct dwarf2_per_objfile *dwarf2_per_objfile
3195 = get_dwarf2_per_objfile (objfile);
3196
3197 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
3198 dw2_free_cached_file_names, NULL);
3199 }
3200
3201 /* Helper function for dw2_map_symtabs_matching_filename that expands
3202 the symtabs and calls the iterator. */
3203
3204 static int
3205 dw2_map_expand_apply (struct objfile *objfile,
3206 struct dwarf2_per_cu_data *per_cu,
3207 const char *name, const char *real_path,
3208 gdb::function_view<bool (symtab *)> callback)
3209 {
3210 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3211
3212 /* Don't visit already-expanded CUs. */
3213 if (per_cu->v.quick->compunit_symtab)
3214 return 0;
3215
3216 /* This may expand more than one symtab, and we want to iterate over
3217 all of them. */
3218 dw2_instantiate_symtab (per_cu, false);
3219
3220 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3221 last_made, callback);
3222 }
3223
3224 /* Implementation of the map_symtabs_matching_filename method. */
3225
3226 static bool
3227 dw2_map_symtabs_matching_filename
3228 (struct objfile *objfile, const char *name, const char *real_path,
3229 gdb::function_view<bool (symtab *)> callback)
3230 {
3231 const char *name_basename = lbasename (name);
3232 struct dwarf2_per_objfile *dwarf2_per_objfile
3233 = get_dwarf2_per_objfile (objfile);
3234
3235 /* The rule is CUs specify all the files, including those used by
3236 any TU, so there's no need to scan TUs here. */
3237
3238 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3239 {
3240 /* We only need to look at symtabs not already expanded. */
3241 if (per_cu->v.quick->compunit_symtab)
3242 continue;
3243
3244 quick_file_names *file_data = dw2_get_file_names (per_cu);
3245 if (file_data == NULL)
3246 continue;
3247
3248 for (int j = 0; j < file_data->num_file_names; ++j)
3249 {
3250 const char *this_name = file_data->file_names[j];
3251 const char *this_real_name;
3252
3253 if (compare_filenames_for_search (this_name, name))
3254 {
3255 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3256 callback))
3257 return true;
3258 continue;
3259 }
3260
3261 /* Before we invoke realpath, which can get expensive when many
3262 files are involved, do a quick comparison of the basenames. */
3263 if (! basenames_may_differ
3264 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3265 continue;
3266
3267 this_real_name = dw2_get_real_path (objfile, file_data, j);
3268 if (compare_filenames_for_search (this_real_name, name))
3269 {
3270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3271 callback))
3272 return true;
3273 continue;
3274 }
3275
3276 if (real_path != NULL)
3277 {
3278 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3279 gdb_assert (IS_ABSOLUTE_PATH (name));
3280 if (this_real_name != NULL
3281 && FILENAME_CMP (real_path, this_real_name) == 0)
3282 {
3283 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3284 callback))
3285 return true;
3286 continue;
3287 }
3288 }
3289 }
3290 }
3291
3292 return false;
3293 }
3294
3295 /* Struct used to manage iterating over all CUs looking for a symbol. */
3296
3297 struct dw2_symtab_iterator
3298 {
3299 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3300 struct dwarf2_per_objfile *dwarf2_per_objfile;
3301 /* If set, only look for symbols that match that block. Valid values are
3302 GLOBAL_BLOCK and STATIC_BLOCK. */
3303 gdb::optional<block_enum> block_index;
3304 /* The kind of symbol we're looking for. */
3305 domain_enum domain;
3306 /* The list of CUs from the index entry of the symbol,
3307 or NULL if not found. */
3308 offset_type *vec;
3309 /* The next element in VEC to look at. */
3310 int next;
3311 /* The number of elements in VEC, or zero if there is no match. */
3312 int length;
3313 /* Have we seen a global version of the symbol?
3314 If so we can ignore all further global instances.
3315 This is to work around gold/15646, inefficient gold-generated
3316 indices. */
3317 int global_seen;
3318 };
3319
3320 /* Initialize the index symtab iterator ITER. */
3321
3322 static void
3323 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3324 struct dwarf2_per_objfile *dwarf2_per_objfile,
3325 gdb::optional<block_enum> block_index,
3326 domain_enum domain,
3327 const char *name)
3328 {
3329 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3330 iter->block_index = block_index;
3331 iter->domain = domain;
3332 iter->next = 0;
3333 iter->global_seen = 0;
3334
3335 mapped_index *index = dwarf2_per_objfile->index_table.get ();
3336
3337 /* index is NULL if OBJF_READNOW. */
3338 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3339 iter->length = MAYBE_SWAP (*iter->vec);
3340 else
3341 {
3342 iter->vec = NULL;
3343 iter->length = 0;
3344 }
3345 }
3346
3347 /* Return the next matching CU or NULL if there are no more. */
3348
3349 static struct dwarf2_per_cu_data *
3350 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3351 {
3352 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3353
3354 for ( ; iter->next < iter->length; ++iter->next)
3355 {
3356 offset_type cu_index_and_attrs =
3357 MAYBE_SWAP (iter->vec[iter->next + 1]);
3358 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3359 gdb_index_symbol_kind symbol_kind =
3360 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3361 /* Only check the symbol attributes if they're present.
3362 Indices prior to version 7 don't record them,
3363 and indices >= 7 may elide them for certain symbols
3364 (gold does this). */
3365 int attrs_valid =
3366 (dwarf2_per_objfile->index_table->version >= 7
3367 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3368
3369 /* Don't crash on bad data. */
3370 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
3371 + dwarf2_per_objfile->all_type_units.size ()))
3372 {
3373 complaint (_(".gdb_index entry has bad CU index"
3374 " [in module %s]"),
3375 objfile_name (dwarf2_per_objfile->objfile));
3376 continue;
3377 }
3378
3379 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3380
3381 /* Skip if already read in. */
3382 if (per_cu->v.quick->compunit_symtab)
3383 continue;
3384
3385 /* Check static vs global. */
3386 if (attrs_valid)
3387 {
3388 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3389
3390 if (iter->block_index.has_value ())
3391 {
3392 bool want_static = *iter->block_index == STATIC_BLOCK;
3393
3394 if (is_static != want_static)
3395 continue;
3396 }
3397
3398 /* Work around gold/15646. */
3399 if (!is_static && iter->global_seen)
3400 continue;
3401 if (!is_static)
3402 iter->global_seen = 1;
3403 }
3404
3405 /* Only check the symbol's kind if it has one. */
3406 if (attrs_valid)
3407 {
3408 switch (iter->domain)
3409 {
3410 case VAR_DOMAIN:
3411 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3412 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3413 /* Some types are also in VAR_DOMAIN. */
3414 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3415 continue;
3416 break;
3417 case STRUCT_DOMAIN:
3418 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3419 continue;
3420 break;
3421 case LABEL_DOMAIN:
3422 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3423 continue;
3424 break;
3425 case MODULE_DOMAIN:
3426 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3427 continue;
3428 break;
3429 default:
3430 break;
3431 }
3432 }
3433
3434 ++iter->next;
3435 return per_cu;
3436 }
3437
3438 return NULL;
3439 }
3440
3441 static struct compunit_symtab *
3442 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3443 const char *name, domain_enum domain)
3444 {
3445 struct compunit_symtab *stab_best = NULL;
3446 struct dwarf2_per_objfile *dwarf2_per_objfile
3447 = get_dwarf2_per_objfile (objfile);
3448
3449 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3450
3451 struct dw2_symtab_iterator iter;
3452 struct dwarf2_per_cu_data *per_cu;
3453
3454 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3455
3456 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3457 {
3458 struct symbol *sym, *with_opaque = NULL;
3459 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
3460 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3461 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3462
3463 sym = block_find_symbol (block, name, domain,
3464 block_find_non_opaque_type_preferred,
3465 &with_opaque);
3466
3467 /* Some caution must be observed with overloaded functions
3468 and methods, since the index will not contain any overload
3469 information (but NAME might contain it). */
3470
3471 if (sym != NULL
3472 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3473 return stab;
3474 if (with_opaque != NULL
3475 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3476 stab_best = stab;
3477
3478 /* Keep looking through other CUs. */
3479 }
3480
3481 return stab_best;
3482 }
3483
3484 static void
3485 dw2_print_stats (struct objfile *objfile)
3486 {
3487 struct dwarf2_per_objfile *dwarf2_per_objfile
3488 = get_dwarf2_per_objfile (objfile);
3489 int total = (dwarf2_per_objfile->all_comp_units.size ()
3490 + dwarf2_per_objfile->all_type_units.size ());
3491 int count = 0;
3492
3493 for (int i = 0; i < total; ++i)
3494 {
3495 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3496
3497 if (!per_cu->v.quick->compunit_symtab)
3498 ++count;
3499 }
3500 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3501 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3502 }
3503
3504 /* This dumps minimal information about the index.
3505 It is called via "mt print objfiles".
3506 One use is to verify .gdb_index has been loaded by the
3507 gdb.dwarf2/gdb-index.exp testcase. */
3508
3509 static void
3510 dw2_dump (struct objfile *objfile)
3511 {
3512 struct dwarf2_per_objfile *dwarf2_per_objfile
3513 = get_dwarf2_per_objfile (objfile);
3514
3515 gdb_assert (dwarf2_per_objfile->using_index);
3516 printf_filtered (".gdb_index:");
3517 if (dwarf2_per_objfile->index_table != NULL)
3518 {
3519 printf_filtered (" version %d\n",
3520 dwarf2_per_objfile->index_table->version);
3521 }
3522 else
3523 printf_filtered (" faked for \"readnow\"\n");
3524 printf_filtered ("\n");
3525 }
3526
3527 static void
3528 dw2_expand_symtabs_for_function (struct objfile *objfile,
3529 const char *func_name)
3530 {
3531 struct dwarf2_per_objfile *dwarf2_per_objfile
3532 = get_dwarf2_per_objfile (objfile);
3533
3534 struct dw2_symtab_iterator iter;
3535 struct dwarf2_per_cu_data *per_cu;
3536
3537 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3538
3539 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3540 dw2_instantiate_symtab (per_cu, false);
3541
3542 }
3543
3544 static void
3545 dw2_expand_all_symtabs (struct objfile *objfile)
3546 {
3547 struct dwarf2_per_objfile *dwarf2_per_objfile
3548 = get_dwarf2_per_objfile (objfile);
3549 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
3550 + dwarf2_per_objfile->all_type_units.size ());
3551
3552 for (int i = 0; i < total_units; ++i)
3553 {
3554 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
3555
3556 /* We don't want to directly expand a partial CU, because if we
3557 read it with the wrong language, then assertion failures can
3558 be triggered later on. See PR symtab/23010. So, tell
3559 dw2_instantiate_symtab to skip partial CUs -- any important
3560 partial CU will be read via DW_TAG_imported_unit anyway. */
3561 dw2_instantiate_symtab (per_cu, true);
3562 }
3563 }
3564
3565 static void
3566 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3567 const char *fullname)
3568 {
3569 struct dwarf2_per_objfile *dwarf2_per_objfile
3570 = get_dwarf2_per_objfile (objfile);
3571
3572 /* We don't need to consider type units here.
3573 This is only called for examining code, e.g. expand_line_sal.
3574 There can be an order of magnitude (or more) more type units
3575 than comp units, and we avoid them if we can. */
3576
3577 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3578 {
3579 /* We only need to look at symtabs not already expanded. */
3580 if (per_cu->v.quick->compunit_symtab)
3581 continue;
3582
3583 quick_file_names *file_data = dw2_get_file_names (per_cu);
3584 if (file_data == NULL)
3585 continue;
3586
3587 for (int j = 0; j < file_data->num_file_names; ++j)
3588 {
3589 const char *this_fullname = file_data->file_names[j];
3590
3591 if (filename_cmp (this_fullname, fullname) == 0)
3592 {
3593 dw2_instantiate_symtab (per_cu, false);
3594 break;
3595 }
3596 }
3597 }
3598 }
3599
3600 static void
3601 dw2_map_matching_symbols
3602 (struct objfile *objfile,
3603 const lookup_name_info &name, domain_enum domain,
3604 int global,
3605 gdb::function_view<symbol_found_callback_ftype> callback,
3606 symbol_compare_ftype *ordered_compare)
3607 {
3608 /* Currently unimplemented; used for Ada. The function can be called if the
3609 current language is Ada for a non-Ada objfile using GNU index. As Ada
3610 does not look for non-Ada symbols this function should just return. */
3611 }
3612
3613 /* Starting from a search name, return the string that finds the upper
3614 bound of all strings that start with SEARCH_NAME in a sorted name
3615 list. Returns the empty string to indicate that the upper bound is
3616 the end of the list. */
3617
3618 static std::string
3619 make_sort_after_prefix_name (const char *search_name)
3620 {
3621 /* When looking to complete "func", we find the upper bound of all
3622 symbols that start with "func" by looking for where we'd insert
3623 the closest string that would follow "func" in lexicographical
3624 order. Usually, that's "func"-with-last-character-incremented,
3625 i.e. "fund". Mind non-ASCII characters, though. Usually those
3626 will be UTF-8 multi-byte sequences, but we can't be certain.
3627 Especially mind the 0xff character, which is a valid character in
3628 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3629 rule out compilers allowing it in identifiers. Note that
3630 conveniently, strcmp/strcasecmp are specified to compare
3631 characters interpreted as unsigned char. So what we do is treat
3632 the whole string as a base 256 number composed of a sequence of
3633 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3634 to 0, and carries 1 to the following more-significant position.
3635 If the very first character in SEARCH_NAME ends up incremented
3636 and carries/overflows, then the upper bound is the end of the
3637 list. The string after the empty string is also the empty
3638 string.
3639
3640 Some examples of this operation:
3641
3642 SEARCH_NAME => "+1" RESULT
3643
3644 "abc" => "abd"
3645 "ab\xff" => "ac"
3646 "\xff" "a" "\xff" => "\xff" "b"
3647 "\xff" => ""
3648 "\xff\xff" => ""
3649 "" => ""
3650
3651 Then, with these symbols for example:
3652
3653 func
3654 func1
3655 fund
3656
3657 completing "func" looks for symbols between "func" and
3658 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3659 which finds "func" and "func1", but not "fund".
3660
3661 And with:
3662
3663 funcÿ (Latin1 'ÿ' [0xff])
3664 funcÿ1
3665 fund
3666
3667 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3668 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3669
3670 And with:
3671
3672 ÿÿ (Latin1 'ÿ' [0xff])
3673 ÿÿ1
3674
3675 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3676 the end of the list.
3677 */
3678 std::string after = search_name;
3679 while (!after.empty () && (unsigned char) after.back () == 0xff)
3680 after.pop_back ();
3681 if (!after.empty ())
3682 after.back () = (unsigned char) after.back () + 1;
3683 return after;
3684 }
3685
3686 /* See declaration. */
3687
3688 std::pair<std::vector<name_component>::const_iterator,
3689 std::vector<name_component>::const_iterator>
3690 mapped_index_base::find_name_components_bounds
3691 (const lookup_name_info &lookup_name_without_params, language lang) const
3692 {
3693 auto *name_cmp
3694 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3695
3696 const char *lang_name
3697 = lookup_name_without_params.language_lookup_name (lang).c_str ();
3698
3699 /* Comparison function object for lower_bound that matches against a
3700 given symbol name. */
3701 auto lookup_compare_lower = [&] (const name_component &elem,
3702 const char *name)
3703 {
3704 const char *elem_qualified = this->symbol_name_at (elem.idx);
3705 const char *elem_name = elem_qualified + elem.name_offset;
3706 return name_cmp (elem_name, name) < 0;
3707 };
3708
3709 /* Comparison function object for upper_bound that matches against a
3710 given symbol name. */
3711 auto lookup_compare_upper = [&] (const char *name,
3712 const name_component &elem)
3713 {
3714 const char *elem_qualified = this->symbol_name_at (elem.idx);
3715 const char *elem_name = elem_qualified + elem.name_offset;
3716 return name_cmp (name, elem_name) < 0;
3717 };
3718
3719 auto begin = this->name_components.begin ();
3720 auto end = this->name_components.end ();
3721
3722 /* Find the lower bound. */
3723 auto lower = [&] ()
3724 {
3725 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3726 return begin;
3727 else
3728 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3729 } ();
3730
3731 /* Find the upper bound. */
3732 auto upper = [&] ()
3733 {
3734 if (lookup_name_without_params.completion_mode ())
3735 {
3736 /* In completion mode, we want UPPER to point past all
3737 symbols names that have the same prefix. I.e., with
3738 these symbols, and completing "func":
3739
3740 function << lower bound
3741 function1
3742 other_function << upper bound
3743
3744 We find the upper bound by looking for the insertion
3745 point of "func"-with-last-character-incremented,
3746 i.e. "fund". */
3747 std::string after = make_sort_after_prefix_name (lang_name);
3748 if (after.empty ())
3749 return end;
3750 return std::lower_bound (lower, end, after.c_str (),
3751 lookup_compare_lower);
3752 }
3753 else
3754 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3755 } ();
3756
3757 return {lower, upper};
3758 }
3759
3760 /* See declaration. */
3761
3762 void
3763 mapped_index_base::build_name_components ()
3764 {
3765 if (!this->name_components.empty ())
3766 return;
3767
3768 this->name_components_casing = case_sensitivity;
3769 auto *name_cmp
3770 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3771
3772 /* The code below only knows how to break apart components of C++
3773 symbol names (and other languages that use '::' as
3774 namespace/module separator) and Ada symbol names. */
3775 auto count = this->symbol_name_count ();
3776 for (offset_type idx = 0; idx < count; idx++)
3777 {
3778 if (this->symbol_name_slot_invalid (idx))
3779 continue;
3780
3781 const char *name = this->symbol_name_at (idx);
3782
3783 /* Add each name component to the name component table. */
3784 unsigned int previous_len = 0;
3785
3786 if (strstr (name, "::") != nullptr)
3787 {
3788 for (unsigned int current_len = cp_find_first_component (name);
3789 name[current_len] != '\0';
3790 current_len += cp_find_first_component (name + current_len))
3791 {
3792 gdb_assert (name[current_len] == ':');
3793 this->name_components.push_back ({previous_len, idx});
3794 /* Skip the '::'. */
3795 current_len += 2;
3796 previous_len = current_len;
3797 }
3798 }
3799 else
3800 {
3801 /* Handle the Ada encoded (aka mangled) form here. */
3802 for (const char *iter = strstr (name, "__");
3803 iter != nullptr;
3804 iter = strstr (iter, "__"))
3805 {
3806 this->name_components.push_back ({previous_len, idx});
3807 iter += 2;
3808 previous_len = iter - name;
3809 }
3810 }
3811
3812 this->name_components.push_back ({previous_len, idx});
3813 }
3814
3815 /* Sort name_components elements by name. */
3816 auto name_comp_compare = [&] (const name_component &left,
3817 const name_component &right)
3818 {
3819 const char *left_qualified = this->symbol_name_at (left.idx);
3820 const char *right_qualified = this->symbol_name_at (right.idx);
3821
3822 const char *left_name = left_qualified + left.name_offset;
3823 const char *right_name = right_qualified + right.name_offset;
3824
3825 return name_cmp (left_name, right_name) < 0;
3826 };
3827
3828 std::sort (this->name_components.begin (),
3829 this->name_components.end (),
3830 name_comp_compare);
3831 }
3832
3833 /* Helper for dw2_expand_symtabs_matching that works with a
3834 mapped_index_base instead of the containing objfile. This is split
3835 to a separate function in order to be able to unit test the
3836 name_components matching using a mock mapped_index_base. For each
3837 symbol name that matches, calls MATCH_CALLBACK, passing it the
3838 symbol's index in the mapped_index_base symbol table. */
3839
3840 static void
3841 dw2_expand_symtabs_matching_symbol
3842 (mapped_index_base &index,
3843 const lookup_name_info &lookup_name_in,
3844 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3845 enum search_domain kind,
3846 gdb::function_view<bool (offset_type)> match_callback)
3847 {
3848 lookup_name_info lookup_name_without_params
3849 = lookup_name_in.make_ignore_params ();
3850
3851 /* Build the symbol name component sorted vector, if we haven't
3852 yet. */
3853 index.build_name_components ();
3854
3855 /* The same symbol may appear more than once in the range though.
3856 E.g., if we're looking for symbols that complete "w", and we have
3857 a symbol named "w1::w2", we'll find the two name components for
3858 that same symbol in the range. To be sure we only call the
3859 callback once per symbol, we first collect the symbol name
3860 indexes that matched in a temporary vector and ignore
3861 duplicates. */
3862 std::vector<offset_type> matches;
3863
3864 struct name_and_matcher
3865 {
3866 symbol_name_matcher_ftype *matcher;
3867 const std::string &name;
3868
3869 bool operator== (const name_and_matcher &other) const
3870 {
3871 return matcher == other.matcher && name == other.name;
3872 }
3873 };
3874
3875 /* A vector holding all the different symbol name matchers, for all
3876 languages. */
3877 std::vector<name_and_matcher> matchers;
3878
3879 for (int i = 0; i < nr_languages; i++)
3880 {
3881 enum language lang_e = (enum language) i;
3882
3883 const language_defn *lang = language_def (lang_e);
3884 symbol_name_matcher_ftype *name_matcher
3885 = get_symbol_name_matcher (lang, lookup_name_without_params);
3886
3887 name_and_matcher key {
3888 name_matcher,
3889 lookup_name_without_params.language_lookup_name (lang_e)
3890 };
3891
3892 /* Don't insert the same comparison routine more than once.
3893 Note that we do this linear walk. This is not a problem in
3894 practice because the number of supported languages is
3895 low. */
3896 if (std::find (matchers.begin (), matchers.end (), key)
3897 != matchers.end ())
3898 continue;
3899 matchers.push_back (std::move (key));
3900
3901 auto bounds
3902 = index.find_name_components_bounds (lookup_name_without_params,
3903 lang_e);
3904
3905 /* Now for each symbol name in range, check to see if we have a name
3906 match, and if so, call the MATCH_CALLBACK callback. */
3907
3908 for (; bounds.first != bounds.second; ++bounds.first)
3909 {
3910 const char *qualified = index.symbol_name_at (bounds.first->idx);
3911
3912 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3913 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3914 continue;
3915
3916 matches.push_back (bounds.first->idx);
3917 }
3918 }
3919
3920 std::sort (matches.begin (), matches.end ());
3921
3922 /* Finally call the callback, once per match. */
3923 ULONGEST prev = -1;
3924 for (offset_type idx : matches)
3925 {
3926 if (prev != idx)
3927 {
3928 if (!match_callback (idx))
3929 break;
3930 prev = idx;
3931 }
3932 }
3933
3934 /* Above we use a type wider than idx's for 'prev', since 0 and
3935 (offset_type)-1 are both possible values. */
3936 static_assert (sizeof (prev) > sizeof (offset_type), "");
3937 }
3938
3939 #if GDB_SELF_TEST
3940
3941 namespace selftests { namespace dw2_expand_symtabs_matching {
3942
3943 /* A mock .gdb_index/.debug_names-like name index table, enough to
3944 exercise dw2_expand_symtabs_matching_symbol, which works with the
3945 mapped_index_base interface. Builds an index from the symbol list
3946 passed as parameter to the constructor. */
3947 class mock_mapped_index : public mapped_index_base
3948 {
3949 public:
3950 mock_mapped_index (gdb::array_view<const char *> symbols)
3951 : m_symbol_table (symbols)
3952 {}
3953
3954 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3955
3956 /* Return the number of names in the symbol table. */
3957 size_t symbol_name_count () const override
3958 {
3959 return m_symbol_table.size ();
3960 }
3961
3962 /* Get the name of the symbol at IDX in the symbol table. */
3963 const char *symbol_name_at (offset_type idx) const override
3964 {
3965 return m_symbol_table[idx];
3966 }
3967
3968 private:
3969 gdb::array_view<const char *> m_symbol_table;
3970 };
3971
3972 /* Convenience function that converts a NULL pointer to a "<null>"
3973 string, to pass to print routines. */
3974
3975 static const char *
3976 string_or_null (const char *str)
3977 {
3978 return str != NULL ? str : "<null>";
3979 }
3980
3981 /* Check if a lookup_name_info built from
3982 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3983 index. EXPECTED_LIST is the list of expected matches, in expected
3984 matching order. If no match expected, then an empty list is
3985 specified. Returns true on success. On failure prints a warning
3986 indicating the file:line that failed, and returns false. */
3987
3988 static bool
3989 check_match (const char *file, int line,
3990 mock_mapped_index &mock_index,
3991 const char *name, symbol_name_match_type match_type,
3992 bool completion_mode,
3993 std::initializer_list<const char *> expected_list)
3994 {
3995 lookup_name_info lookup_name (name, match_type, completion_mode);
3996
3997 bool matched = true;
3998
3999 auto mismatch = [&] (const char *expected_str,
4000 const char *got)
4001 {
4002 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4003 "expected=\"%s\", got=\"%s\"\n"),
4004 file, line,
4005 (match_type == symbol_name_match_type::FULL
4006 ? "FULL" : "WILD"),
4007 name, string_or_null (expected_str), string_or_null (got));
4008 matched = false;
4009 };
4010
4011 auto expected_it = expected_list.begin ();
4012 auto expected_end = expected_list.end ();
4013
4014 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4015 NULL, ALL_DOMAIN,
4016 [&] (offset_type idx)
4017 {
4018 const char *matched_name = mock_index.symbol_name_at (idx);
4019 const char *expected_str
4020 = expected_it == expected_end ? NULL : *expected_it++;
4021
4022 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4023 mismatch (expected_str, matched_name);
4024 return true;
4025 });
4026
4027 const char *expected_str
4028 = expected_it == expected_end ? NULL : *expected_it++;
4029 if (expected_str != NULL)
4030 mismatch (expected_str, NULL);
4031
4032 return matched;
4033 }
4034
4035 /* The symbols added to the mock mapped_index for testing (in
4036 canonical form). */
4037 static const char *test_symbols[] = {
4038 "function",
4039 "std::bar",
4040 "std::zfunction",
4041 "std::zfunction2",
4042 "w1::w2",
4043 "ns::foo<char*>",
4044 "ns::foo<int>",
4045 "ns::foo<long>",
4046 "ns2::tmpl<int>::foo2",
4047 "(anonymous namespace)::A::B::C",
4048
4049 /* These are used to check that the increment-last-char in the
4050 matching algorithm for completion doesn't match "t1_fund" when
4051 completing "t1_func". */
4052 "t1_func",
4053 "t1_func1",
4054 "t1_fund",
4055 "t1_fund1",
4056
4057 /* A UTF-8 name with multi-byte sequences to make sure that
4058 cp-name-parser understands this as a single identifier ("função"
4059 is "function" in PT). */
4060 u8"u8função",
4061
4062 /* \377 (0xff) is Latin1 'ÿ'. */
4063 "yfunc\377",
4064
4065 /* \377 (0xff) is Latin1 'ÿ'. */
4066 "\377",
4067 "\377\377123",
4068
4069 /* A name with all sorts of complications. Starts with "z" to make
4070 it easier for the completion tests below. */
4071 #define Z_SYM_NAME \
4072 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4073 "::tuple<(anonymous namespace)::ui*, " \
4074 "std::default_delete<(anonymous namespace)::ui>, void>"
4075
4076 Z_SYM_NAME
4077 };
4078
4079 /* Returns true if the mapped_index_base::find_name_component_bounds
4080 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4081 in completion mode. */
4082
4083 static bool
4084 check_find_bounds_finds (mapped_index_base &index,
4085 const char *search_name,
4086 gdb::array_view<const char *> expected_syms)
4087 {
4088 lookup_name_info lookup_name (search_name,
4089 symbol_name_match_type::FULL, true);
4090
4091 auto bounds = index.find_name_components_bounds (lookup_name,
4092 language_cplus);
4093
4094 size_t distance = std::distance (bounds.first, bounds.second);
4095 if (distance != expected_syms.size ())
4096 return false;
4097
4098 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4099 {
4100 auto nc_elem = bounds.first + exp_elem;
4101 const char *qualified = index.symbol_name_at (nc_elem->idx);
4102 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4103 return false;
4104 }
4105
4106 return true;
4107 }
4108
4109 /* Test the lower-level mapped_index::find_name_component_bounds
4110 method. */
4111
4112 static void
4113 test_mapped_index_find_name_component_bounds ()
4114 {
4115 mock_mapped_index mock_index (test_symbols);
4116
4117 mock_index.build_name_components ();
4118
4119 /* Test the lower-level mapped_index::find_name_component_bounds
4120 method in completion mode. */
4121 {
4122 static const char *expected_syms[] = {
4123 "t1_func",
4124 "t1_func1",
4125 };
4126
4127 SELF_CHECK (check_find_bounds_finds (mock_index,
4128 "t1_func", expected_syms));
4129 }
4130
4131 /* Check that the increment-last-char in the name matching algorithm
4132 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4133 {
4134 static const char *expected_syms1[] = {
4135 "\377",
4136 "\377\377123",
4137 };
4138 SELF_CHECK (check_find_bounds_finds (mock_index,
4139 "\377", expected_syms1));
4140
4141 static const char *expected_syms2[] = {
4142 "\377\377123",
4143 };
4144 SELF_CHECK (check_find_bounds_finds (mock_index,
4145 "\377\377", expected_syms2));
4146 }
4147 }
4148
4149 /* Test dw2_expand_symtabs_matching_symbol. */
4150
4151 static void
4152 test_dw2_expand_symtabs_matching_symbol ()
4153 {
4154 mock_mapped_index mock_index (test_symbols);
4155
4156 /* We let all tests run until the end even if some fails, for debug
4157 convenience. */
4158 bool any_mismatch = false;
4159
4160 /* Create the expected symbols list (an initializer_list). Needed
4161 because lists have commas, and we need to pass them to CHECK,
4162 which is a macro. */
4163 #define EXPECT(...) { __VA_ARGS__ }
4164
4165 /* Wrapper for check_match that passes down the current
4166 __FILE__/__LINE__. */
4167 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4168 any_mismatch |= !check_match (__FILE__, __LINE__, \
4169 mock_index, \
4170 NAME, MATCH_TYPE, COMPLETION_MODE, \
4171 EXPECTED_LIST)
4172
4173 /* Identity checks. */
4174 for (const char *sym : test_symbols)
4175 {
4176 /* Should be able to match all existing symbols. */
4177 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4178 EXPECT (sym));
4179
4180 /* Should be able to match all existing symbols with
4181 parameters. */
4182 std::string with_params = std::string (sym) + "(int)";
4183 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4184 EXPECT (sym));
4185
4186 /* Should be able to match all existing symbols with
4187 parameters and qualifiers. */
4188 with_params = std::string (sym) + " ( int ) const";
4189 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4190 EXPECT (sym));
4191
4192 /* This should really find sym, but cp-name-parser.y doesn't
4193 know about lvalue/rvalue qualifiers yet. */
4194 with_params = std::string (sym) + " ( int ) &&";
4195 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4196 {});
4197 }
4198
4199 /* Check that the name matching algorithm for completion doesn't get
4200 confused with Latin1 'ÿ' / 0xff. */
4201 {
4202 static const char str[] = "\377";
4203 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4204 EXPECT ("\377", "\377\377123"));
4205 }
4206
4207 /* Check that the increment-last-char in the matching algorithm for
4208 completion doesn't match "t1_fund" when completing "t1_func". */
4209 {
4210 static const char str[] = "t1_func";
4211 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4212 EXPECT ("t1_func", "t1_func1"));
4213 }
4214
4215 /* Check that completion mode works at each prefix of the expected
4216 symbol name. */
4217 {
4218 static const char str[] = "function(int)";
4219 size_t len = strlen (str);
4220 std::string lookup;
4221
4222 for (size_t i = 1; i < len; i++)
4223 {
4224 lookup.assign (str, i);
4225 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4226 EXPECT ("function"));
4227 }
4228 }
4229
4230 /* While "w" is a prefix of both components, the match function
4231 should still only be called once. */
4232 {
4233 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4234 EXPECT ("w1::w2"));
4235 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4236 EXPECT ("w1::w2"));
4237 }
4238
4239 /* Same, with a "complicated" symbol. */
4240 {
4241 static const char str[] = Z_SYM_NAME;
4242 size_t len = strlen (str);
4243 std::string lookup;
4244
4245 for (size_t i = 1; i < len; i++)
4246 {
4247 lookup.assign (str, i);
4248 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4249 EXPECT (Z_SYM_NAME));
4250 }
4251 }
4252
4253 /* In FULL mode, an incomplete symbol doesn't match. */
4254 {
4255 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4256 {});
4257 }
4258
4259 /* A complete symbol with parameters matches any overload, since the
4260 index has no overload info. */
4261 {
4262 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4263 EXPECT ("std::zfunction", "std::zfunction2"));
4264 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4265 EXPECT ("std::zfunction", "std::zfunction2"));
4266 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4267 EXPECT ("std::zfunction", "std::zfunction2"));
4268 }
4269
4270 /* Check that whitespace is ignored appropriately. A symbol with a
4271 template argument list. */
4272 {
4273 static const char expected[] = "ns::foo<int>";
4274 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4275 EXPECT (expected));
4276 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4277 EXPECT (expected));
4278 }
4279
4280 /* Check that whitespace is ignored appropriately. A symbol with a
4281 template argument list that includes a pointer. */
4282 {
4283 static const char expected[] = "ns::foo<char*>";
4284 /* Try both completion and non-completion modes. */
4285 static const bool completion_mode[2] = {false, true};
4286 for (size_t i = 0; i < 2; i++)
4287 {
4288 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4289 completion_mode[i], EXPECT (expected));
4290 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4291 completion_mode[i], EXPECT (expected));
4292
4293 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4294 completion_mode[i], EXPECT (expected));
4295 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4296 completion_mode[i], EXPECT (expected));
4297 }
4298 }
4299
4300 {
4301 /* Check method qualifiers are ignored. */
4302 static const char expected[] = "ns::foo<char*>";
4303 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4304 symbol_name_match_type::FULL, true, EXPECT (expected));
4305 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4306 symbol_name_match_type::FULL, true, EXPECT (expected));
4307 CHECK_MATCH ("foo < char * > ( int ) const",
4308 symbol_name_match_type::WILD, true, EXPECT (expected));
4309 CHECK_MATCH ("foo < char * > ( int ) &&",
4310 symbol_name_match_type::WILD, true, EXPECT (expected));
4311 }
4312
4313 /* Test lookup names that don't match anything. */
4314 {
4315 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4316 {});
4317
4318 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4319 {});
4320 }
4321
4322 /* Some wild matching tests, exercising "(anonymous namespace)",
4323 which should not be confused with a parameter list. */
4324 {
4325 static const char *syms[] = {
4326 "A::B::C",
4327 "B::C",
4328 "C",
4329 "A :: B :: C ( int )",
4330 "B :: C ( int )",
4331 "C ( int )",
4332 };
4333
4334 for (const char *s : syms)
4335 {
4336 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4337 EXPECT ("(anonymous namespace)::A::B::C"));
4338 }
4339 }
4340
4341 {
4342 static const char expected[] = "ns2::tmpl<int>::foo2";
4343 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4344 EXPECT (expected));
4345 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4346 EXPECT (expected));
4347 }
4348
4349 SELF_CHECK (!any_mismatch);
4350
4351 #undef EXPECT
4352 #undef CHECK_MATCH
4353 }
4354
4355 static void
4356 run_test ()
4357 {
4358 test_mapped_index_find_name_component_bounds ();
4359 test_dw2_expand_symtabs_matching_symbol ();
4360 }
4361
4362 }} // namespace selftests::dw2_expand_symtabs_matching
4363
4364 #endif /* GDB_SELF_TEST */
4365
4366 /* If FILE_MATCHER is NULL or if PER_CU has
4367 dwarf2_per_cu_quick_data::MARK set (see
4368 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4369 EXPANSION_NOTIFY on it. */
4370
4371 static void
4372 dw2_expand_symtabs_matching_one
4373 (struct dwarf2_per_cu_data *per_cu,
4374 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4375 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4376 {
4377 if (file_matcher == NULL || per_cu->v.quick->mark)
4378 {
4379 bool symtab_was_null
4380 = (per_cu->v.quick->compunit_symtab == NULL);
4381
4382 dw2_instantiate_symtab (per_cu, false);
4383
4384 if (expansion_notify != NULL
4385 && symtab_was_null
4386 && per_cu->v.quick->compunit_symtab != NULL)
4387 expansion_notify (per_cu->v.quick->compunit_symtab);
4388 }
4389 }
4390
4391 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4392 matched, to expand corresponding CUs that were marked. IDX is the
4393 index of the symbol name that matched. */
4394
4395 static void
4396 dw2_expand_marked_cus
4397 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4398 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4399 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4400 search_domain kind)
4401 {
4402 offset_type *vec, vec_len, vec_idx;
4403 bool global_seen = false;
4404 mapped_index &index = *dwarf2_per_objfile->index_table;
4405
4406 vec = (offset_type *) (index.constant_pool
4407 + MAYBE_SWAP (index.symbol_table[idx].vec));
4408 vec_len = MAYBE_SWAP (vec[0]);
4409 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4410 {
4411 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4412 /* This value is only valid for index versions >= 7. */
4413 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4414 gdb_index_symbol_kind symbol_kind =
4415 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4416 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4417 /* Only check the symbol attributes if they're present.
4418 Indices prior to version 7 don't record them,
4419 and indices >= 7 may elide them for certain symbols
4420 (gold does this). */
4421 int attrs_valid =
4422 (index.version >= 7
4423 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4424
4425 /* Work around gold/15646. */
4426 if (attrs_valid)
4427 {
4428 if (!is_static && global_seen)
4429 continue;
4430 if (!is_static)
4431 global_seen = true;
4432 }
4433
4434 /* Only check the symbol's kind if it has one. */
4435 if (attrs_valid)
4436 {
4437 switch (kind)
4438 {
4439 case VARIABLES_DOMAIN:
4440 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4441 continue;
4442 break;
4443 case FUNCTIONS_DOMAIN:
4444 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4445 continue;
4446 break;
4447 case TYPES_DOMAIN:
4448 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4449 continue;
4450 break;
4451 case MODULES_DOMAIN:
4452 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4453 continue;
4454 break;
4455 default:
4456 break;
4457 }
4458 }
4459
4460 /* Don't crash on bad data. */
4461 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
4462 + dwarf2_per_objfile->all_type_units.size ()))
4463 {
4464 complaint (_(".gdb_index entry has bad CU index"
4465 " [in module %s]"),
4466 objfile_name (dwarf2_per_objfile->objfile));
4467 continue;
4468 }
4469
4470 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4471 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4472 expansion_notify);
4473 }
4474 }
4475
4476 /* If FILE_MATCHER is non-NULL, set all the
4477 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4478 that match FILE_MATCHER. */
4479
4480 static void
4481 dw_expand_symtabs_matching_file_matcher
4482 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4483 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4484 {
4485 if (file_matcher == NULL)
4486 return;
4487
4488 objfile *const objfile = dwarf2_per_objfile->objfile;
4489
4490 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4491 htab_eq_pointer,
4492 NULL, xcalloc, xfree));
4493 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4494 htab_eq_pointer,
4495 NULL, xcalloc, xfree));
4496
4497 /* The rule is CUs specify all the files, including those used by
4498 any TU, so there's no need to scan TUs here. */
4499
4500 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4501 {
4502 QUIT;
4503
4504 per_cu->v.quick->mark = 0;
4505
4506 /* We only need to look at symtabs not already expanded. */
4507 if (per_cu->v.quick->compunit_symtab)
4508 continue;
4509
4510 quick_file_names *file_data = dw2_get_file_names (per_cu);
4511 if (file_data == NULL)
4512 continue;
4513
4514 if (htab_find (visited_not_found.get (), file_data) != NULL)
4515 continue;
4516 else if (htab_find (visited_found.get (), file_data) != NULL)
4517 {
4518 per_cu->v.quick->mark = 1;
4519 continue;
4520 }
4521
4522 for (int j = 0; j < file_data->num_file_names; ++j)
4523 {
4524 const char *this_real_name;
4525
4526 if (file_matcher (file_data->file_names[j], false))
4527 {
4528 per_cu->v.quick->mark = 1;
4529 break;
4530 }
4531
4532 /* Before we invoke realpath, which can get expensive when many
4533 files are involved, do a quick comparison of the basenames. */
4534 if (!basenames_may_differ
4535 && !file_matcher (lbasename (file_data->file_names[j]),
4536 true))
4537 continue;
4538
4539 this_real_name = dw2_get_real_path (objfile, file_data, j);
4540 if (file_matcher (this_real_name, false))
4541 {
4542 per_cu->v.quick->mark = 1;
4543 break;
4544 }
4545 }
4546
4547 void **slot = htab_find_slot (per_cu->v.quick->mark
4548 ? visited_found.get ()
4549 : visited_not_found.get (),
4550 file_data, INSERT);
4551 *slot = file_data;
4552 }
4553 }
4554
4555 static void
4556 dw2_expand_symtabs_matching
4557 (struct objfile *objfile,
4558 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4559 const lookup_name_info &lookup_name,
4560 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4561 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4562 enum search_domain kind)
4563 {
4564 struct dwarf2_per_objfile *dwarf2_per_objfile
4565 = get_dwarf2_per_objfile (objfile);
4566
4567 /* index_table is NULL if OBJF_READNOW. */
4568 if (!dwarf2_per_objfile->index_table)
4569 return;
4570
4571 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4572
4573 mapped_index &index = *dwarf2_per_objfile->index_table;
4574
4575 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4576 symbol_matcher,
4577 kind, [&] (offset_type idx)
4578 {
4579 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4580 expansion_notify, kind);
4581 return true;
4582 });
4583 }
4584
4585 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4586 symtab. */
4587
4588 static struct compunit_symtab *
4589 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4590 CORE_ADDR pc)
4591 {
4592 int i;
4593
4594 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4595 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4596 return cust;
4597
4598 if (cust->includes == NULL)
4599 return NULL;
4600
4601 for (i = 0; cust->includes[i]; ++i)
4602 {
4603 struct compunit_symtab *s = cust->includes[i];
4604
4605 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4606 if (s != NULL)
4607 return s;
4608 }
4609
4610 return NULL;
4611 }
4612
4613 static struct compunit_symtab *
4614 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4615 struct bound_minimal_symbol msymbol,
4616 CORE_ADDR pc,
4617 struct obj_section *section,
4618 int warn_if_readin)
4619 {
4620 struct dwarf2_per_cu_data *data;
4621 struct compunit_symtab *result;
4622
4623 if (!objfile->partial_symtabs->psymtabs_addrmap)
4624 return NULL;
4625
4626 CORE_ADDR baseaddr = objfile->text_section_offset ();
4627 data = (struct dwarf2_per_cu_data *) addrmap_find
4628 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4629 if (!data)
4630 return NULL;
4631
4632 if (warn_if_readin && data->v.quick->compunit_symtab)
4633 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4634 paddress (get_objfile_arch (objfile), pc));
4635
4636 result
4637 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4638 false),
4639 pc);
4640 gdb_assert (result != NULL);
4641 return result;
4642 }
4643
4644 static void
4645 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4646 void *data, int need_fullname)
4647 {
4648 struct dwarf2_per_objfile *dwarf2_per_objfile
4649 = get_dwarf2_per_objfile (objfile);
4650
4651 if (!dwarf2_per_objfile->filenames_cache)
4652 {
4653 dwarf2_per_objfile->filenames_cache.emplace ();
4654
4655 htab_up visited (htab_create_alloc (10,
4656 htab_hash_pointer, htab_eq_pointer,
4657 NULL, xcalloc, xfree));
4658
4659 /* The rule is CUs specify all the files, including those used
4660 by any TU, so there's no need to scan TUs here. We can
4661 ignore file names coming from already-expanded CUs. */
4662
4663 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4664 {
4665 if (per_cu->v.quick->compunit_symtab)
4666 {
4667 void **slot = htab_find_slot (visited.get (),
4668 per_cu->v.quick->file_names,
4669 INSERT);
4670
4671 *slot = per_cu->v.quick->file_names;
4672 }
4673 }
4674
4675 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4676 {
4677 /* We only need to look at symtabs not already expanded. */
4678 if (per_cu->v.quick->compunit_symtab)
4679 continue;
4680
4681 quick_file_names *file_data = dw2_get_file_names (per_cu);
4682 if (file_data == NULL)
4683 continue;
4684
4685 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4686 if (*slot)
4687 {
4688 /* Already visited. */
4689 continue;
4690 }
4691 *slot = file_data;
4692
4693 for (int j = 0; j < file_data->num_file_names; ++j)
4694 {
4695 const char *filename = file_data->file_names[j];
4696 dwarf2_per_objfile->filenames_cache->seen (filename);
4697 }
4698 }
4699 }
4700
4701 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4702 {
4703 gdb::unique_xmalloc_ptr<char> this_real_name;
4704
4705 if (need_fullname)
4706 this_real_name = gdb_realpath (filename);
4707 (*fun) (filename, this_real_name.get (), data);
4708 });
4709 }
4710
4711 static int
4712 dw2_has_symbols (struct objfile *objfile)
4713 {
4714 return 1;
4715 }
4716
4717 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4718 {
4719 dw2_has_symbols,
4720 dw2_find_last_source_symtab,
4721 dw2_forget_cached_source_info,
4722 dw2_map_symtabs_matching_filename,
4723 dw2_lookup_symbol,
4724 dw2_print_stats,
4725 dw2_dump,
4726 dw2_expand_symtabs_for_function,
4727 dw2_expand_all_symtabs,
4728 dw2_expand_symtabs_with_fullname,
4729 dw2_map_matching_symbols,
4730 dw2_expand_symtabs_matching,
4731 dw2_find_pc_sect_compunit_symtab,
4732 NULL,
4733 dw2_map_symbol_filenames
4734 };
4735
4736 /* DWARF-5 debug_names reader. */
4737
4738 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4739 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4740
4741 /* A helper function that reads the .debug_names section in SECTION
4742 and fills in MAP. FILENAME is the name of the file containing the
4743 section; it is used for error reporting.
4744
4745 Returns true if all went well, false otherwise. */
4746
4747 static bool
4748 read_debug_names_from_section (struct objfile *objfile,
4749 const char *filename,
4750 struct dwarf2_section_info *section,
4751 mapped_debug_names &map)
4752 {
4753 if (section->empty ())
4754 return false;
4755
4756 /* Older elfutils strip versions could keep the section in the main
4757 executable while splitting it for the separate debug info file. */
4758 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4759 return false;
4760
4761 section->read (objfile);
4762
4763 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4764
4765 const gdb_byte *addr = section->buffer;
4766
4767 bfd *const abfd = section->get_bfd_owner ();
4768
4769 unsigned int bytes_read;
4770 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4771 addr += bytes_read;
4772
4773 map.dwarf5_is_dwarf64 = bytes_read != 4;
4774 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4775 if (bytes_read + length != section->size)
4776 {
4777 /* There may be multiple per-CU indices. */
4778 warning (_("Section .debug_names in %s length %s does not match "
4779 "section length %s, ignoring .debug_names."),
4780 filename, plongest (bytes_read + length),
4781 pulongest (section->size));
4782 return false;
4783 }
4784
4785 /* The version number. */
4786 uint16_t version = read_2_bytes (abfd, addr);
4787 addr += 2;
4788 if (version != 5)
4789 {
4790 warning (_("Section .debug_names in %s has unsupported version %d, "
4791 "ignoring .debug_names."),
4792 filename, version);
4793 return false;
4794 }
4795
4796 /* Padding. */
4797 uint16_t padding = read_2_bytes (abfd, addr);
4798 addr += 2;
4799 if (padding != 0)
4800 {
4801 warning (_("Section .debug_names in %s has unsupported padding %d, "
4802 "ignoring .debug_names."),
4803 filename, padding);
4804 return false;
4805 }
4806
4807 /* comp_unit_count - The number of CUs in the CU list. */
4808 map.cu_count = read_4_bytes (abfd, addr);
4809 addr += 4;
4810
4811 /* local_type_unit_count - The number of TUs in the local TU
4812 list. */
4813 map.tu_count = read_4_bytes (abfd, addr);
4814 addr += 4;
4815
4816 /* foreign_type_unit_count - The number of TUs in the foreign TU
4817 list. */
4818 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4819 addr += 4;
4820 if (foreign_tu_count != 0)
4821 {
4822 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4823 "ignoring .debug_names."),
4824 filename, static_cast<unsigned long> (foreign_tu_count));
4825 return false;
4826 }
4827
4828 /* bucket_count - The number of hash buckets in the hash lookup
4829 table. */
4830 map.bucket_count = read_4_bytes (abfd, addr);
4831 addr += 4;
4832
4833 /* name_count - The number of unique names in the index. */
4834 map.name_count = read_4_bytes (abfd, addr);
4835 addr += 4;
4836
4837 /* abbrev_table_size - The size in bytes of the abbreviations
4838 table. */
4839 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4840 addr += 4;
4841
4842 /* augmentation_string_size - The size in bytes of the augmentation
4843 string. This value is rounded up to a multiple of 4. */
4844 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4845 addr += 4;
4846 map.augmentation_is_gdb = ((augmentation_string_size
4847 == sizeof (dwarf5_augmentation))
4848 && memcmp (addr, dwarf5_augmentation,
4849 sizeof (dwarf5_augmentation)) == 0);
4850 augmentation_string_size += (-augmentation_string_size) & 3;
4851 addr += augmentation_string_size;
4852
4853 /* List of CUs */
4854 map.cu_table_reordered = addr;
4855 addr += map.cu_count * map.offset_size;
4856
4857 /* List of Local TUs */
4858 map.tu_table_reordered = addr;
4859 addr += map.tu_count * map.offset_size;
4860
4861 /* Hash Lookup Table */
4862 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4863 addr += map.bucket_count * 4;
4864 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4865 addr += map.name_count * 4;
4866
4867 /* Name Table */
4868 map.name_table_string_offs_reordered = addr;
4869 addr += map.name_count * map.offset_size;
4870 map.name_table_entry_offs_reordered = addr;
4871 addr += map.name_count * map.offset_size;
4872
4873 const gdb_byte *abbrev_table_start = addr;
4874 for (;;)
4875 {
4876 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4877 addr += bytes_read;
4878 if (index_num == 0)
4879 break;
4880
4881 const auto insertpair
4882 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4883 if (!insertpair.second)
4884 {
4885 warning (_("Section .debug_names in %s has duplicate index %s, "
4886 "ignoring .debug_names."),
4887 filename, pulongest (index_num));
4888 return false;
4889 }
4890 mapped_debug_names::index_val &indexval = insertpair.first->second;
4891 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4892 addr += bytes_read;
4893
4894 for (;;)
4895 {
4896 mapped_debug_names::index_val::attr attr;
4897 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4898 addr += bytes_read;
4899 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4900 addr += bytes_read;
4901 if (attr.form == DW_FORM_implicit_const)
4902 {
4903 attr.implicit_const = read_signed_leb128 (abfd, addr,
4904 &bytes_read);
4905 addr += bytes_read;
4906 }
4907 if (attr.dw_idx == 0 && attr.form == 0)
4908 break;
4909 indexval.attr_vec.push_back (std::move (attr));
4910 }
4911 }
4912 if (addr != abbrev_table_start + abbrev_table_size)
4913 {
4914 warning (_("Section .debug_names in %s has abbreviation_table "
4915 "of size %s vs. written as %u, ignoring .debug_names."),
4916 filename, plongest (addr - abbrev_table_start),
4917 abbrev_table_size);
4918 return false;
4919 }
4920 map.entry_pool = addr;
4921
4922 return true;
4923 }
4924
4925 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4926 list. */
4927
4928 static void
4929 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4930 const mapped_debug_names &map,
4931 dwarf2_section_info &section,
4932 bool is_dwz)
4933 {
4934 sect_offset sect_off_prev;
4935 for (uint32_t i = 0; i <= map.cu_count; ++i)
4936 {
4937 sect_offset sect_off_next;
4938 if (i < map.cu_count)
4939 {
4940 sect_off_next
4941 = (sect_offset) (extract_unsigned_integer
4942 (map.cu_table_reordered + i * map.offset_size,
4943 map.offset_size,
4944 map.dwarf5_byte_order));
4945 }
4946 else
4947 sect_off_next = (sect_offset) section.size;
4948 if (i >= 1)
4949 {
4950 const ULONGEST length = sect_off_next - sect_off_prev;
4951 dwarf2_per_cu_data *per_cu
4952 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
4953 sect_off_prev, length);
4954 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
4955 }
4956 sect_off_prev = sect_off_next;
4957 }
4958 }
4959
4960 /* Read the CU list from the mapped index, and use it to create all
4961 the CU objects for this dwarf2_per_objfile. */
4962
4963 static void
4964 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
4965 const mapped_debug_names &map,
4966 const mapped_debug_names &dwz_map)
4967 {
4968 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4969 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
4970
4971 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4972 dwarf2_per_objfile->info,
4973 false /* is_dwz */);
4974
4975 if (dwz_map.cu_count == 0)
4976 return;
4977
4978 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4979 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
4980 true /* is_dwz */);
4981 }
4982
4983 /* Read .debug_names. If everything went ok, initialize the "quick"
4984 elements of all the CUs and return true. Otherwise, return false. */
4985
4986 static bool
4987 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
4988 {
4989 std::unique_ptr<mapped_debug_names> map
4990 (new mapped_debug_names (dwarf2_per_objfile));
4991 mapped_debug_names dwz_map (dwarf2_per_objfile);
4992 struct objfile *objfile = dwarf2_per_objfile->objfile;
4993
4994 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
4995 &dwarf2_per_objfile->debug_names,
4996 *map))
4997 return false;
4998
4999 /* Don't use the index if it's empty. */
5000 if (map->name_count == 0)
5001 return false;
5002
5003 /* If there is a .dwz file, read it so we can get its CU list as
5004 well. */
5005 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5006 if (dwz != NULL)
5007 {
5008 if (!read_debug_names_from_section (objfile,
5009 bfd_get_filename (dwz->dwz_bfd.get ()),
5010 &dwz->debug_names, dwz_map))
5011 {
5012 warning (_("could not read '.debug_names' section from %s; skipping"),
5013 bfd_get_filename (dwz->dwz_bfd.get ()));
5014 return false;
5015 }
5016 }
5017
5018 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5019
5020 if (map->tu_count != 0)
5021 {
5022 /* We can only handle a single .debug_types when we have an
5023 index. */
5024 if (dwarf2_per_objfile->types.size () != 1)
5025 return false;
5026
5027 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
5028
5029 create_signatured_type_table_from_debug_names
5030 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
5031 }
5032
5033 create_addrmap_from_aranges (dwarf2_per_objfile,
5034 &dwarf2_per_objfile->debug_aranges);
5035
5036 dwarf2_per_objfile->debug_names_table = std::move (map);
5037 dwarf2_per_objfile->using_index = 1;
5038 dwarf2_per_objfile->quick_file_names_table =
5039 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
5040
5041 return true;
5042 }
5043
5044 /* Type used to manage iterating over all CUs looking for a symbol for
5045 .debug_names. */
5046
5047 class dw2_debug_names_iterator
5048 {
5049 public:
5050 dw2_debug_names_iterator (const mapped_debug_names &map,
5051 gdb::optional<block_enum> block_index,
5052 domain_enum domain,
5053 const char *name)
5054 : m_map (map), m_block_index (block_index), m_domain (domain),
5055 m_addr (find_vec_in_debug_names (map, name))
5056 {}
5057
5058 dw2_debug_names_iterator (const mapped_debug_names &map,
5059 search_domain search, uint32_t namei)
5060 : m_map (map),
5061 m_search (search),
5062 m_addr (find_vec_in_debug_names (map, namei))
5063 {}
5064
5065 dw2_debug_names_iterator (const mapped_debug_names &map,
5066 block_enum block_index, domain_enum domain,
5067 uint32_t namei)
5068 : m_map (map), m_block_index (block_index), m_domain (domain),
5069 m_addr (find_vec_in_debug_names (map, namei))
5070 {}
5071
5072 /* Return the next matching CU or NULL if there are no more. */
5073 dwarf2_per_cu_data *next ();
5074
5075 private:
5076 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5077 const char *name);
5078 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5079 uint32_t namei);
5080
5081 /* The internalized form of .debug_names. */
5082 const mapped_debug_names &m_map;
5083
5084 /* If set, only look for symbols that match that block. Valid values are
5085 GLOBAL_BLOCK and STATIC_BLOCK. */
5086 const gdb::optional<block_enum> m_block_index;
5087
5088 /* The kind of symbol we're looking for. */
5089 const domain_enum m_domain = UNDEF_DOMAIN;
5090 const search_domain m_search = ALL_DOMAIN;
5091
5092 /* The list of CUs from the index entry of the symbol, or NULL if
5093 not found. */
5094 const gdb_byte *m_addr;
5095 };
5096
5097 const char *
5098 mapped_debug_names::namei_to_name (uint32_t namei) const
5099 {
5100 const ULONGEST namei_string_offs
5101 = extract_unsigned_integer ((name_table_string_offs_reordered
5102 + namei * offset_size),
5103 offset_size,
5104 dwarf5_byte_order);
5105 return read_indirect_string_at_offset (dwarf2_per_objfile,
5106 namei_string_offs);
5107 }
5108
5109 /* Find a slot in .debug_names for the object named NAME. If NAME is
5110 found, return pointer to its pool data. If NAME cannot be found,
5111 return NULL. */
5112
5113 const gdb_byte *
5114 dw2_debug_names_iterator::find_vec_in_debug_names
5115 (const mapped_debug_names &map, const char *name)
5116 {
5117 int (*cmp) (const char *, const char *);
5118
5119 gdb::unique_xmalloc_ptr<char> without_params;
5120 if (current_language->la_language == language_cplus
5121 || current_language->la_language == language_fortran
5122 || current_language->la_language == language_d)
5123 {
5124 /* NAME is already canonical. Drop any qualifiers as
5125 .debug_names does not contain any. */
5126
5127 if (strchr (name, '(') != NULL)
5128 {
5129 without_params = cp_remove_params (name);
5130 if (without_params != NULL)
5131 name = without_params.get ();
5132 }
5133 }
5134
5135 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5136
5137 const uint32_t full_hash = dwarf5_djb_hash (name);
5138 uint32_t namei
5139 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5140 (map.bucket_table_reordered
5141 + (full_hash % map.bucket_count)), 4,
5142 map.dwarf5_byte_order);
5143 if (namei == 0)
5144 return NULL;
5145 --namei;
5146 if (namei >= map.name_count)
5147 {
5148 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5149 "[in module %s]"),
5150 namei, map.name_count,
5151 objfile_name (map.dwarf2_per_objfile->objfile));
5152 return NULL;
5153 }
5154
5155 for (;;)
5156 {
5157 const uint32_t namei_full_hash
5158 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5159 (map.hash_table_reordered + namei), 4,
5160 map.dwarf5_byte_order);
5161 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5162 return NULL;
5163
5164 if (full_hash == namei_full_hash)
5165 {
5166 const char *const namei_string = map.namei_to_name (namei);
5167
5168 #if 0 /* An expensive sanity check. */
5169 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5170 {
5171 complaint (_("Wrong .debug_names hash for string at index %u "
5172 "[in module %s]"),
5173 namei, objfile_name (dwarf2_per_objfile->objfile));
5174 return NULL;
5175 }
5176 #endif
5177
5178 if (cmp (namei_string, name) == 0)
5179 {
5180 const ULONGEST namei_entry_offs
5181 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5182 + namei * map.offset_size),
5183 map.offset_size, map.dwarf5_byte_order);
5184 return map.entry_pool + namei_entry_offs;
5185 }
5186 }
5187
5188 ++namei;
5189 if (namei >= map.name_count)
5190 return NULL;
5191 }
5192 }
5193
5194 const gdb_byte *
5195 dw2_debug_names_iterator::find_vec_in_debug_names
5196 (const mapped_debug_names &map, uint32_t namei)
5197 {
5198 if (namei >= map.name_count)
5199 {
5200 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5201 "[in module %s]"),
5202 namei, map.name_count,
5203 objfile_name (map.dwarf2_per_objfile->objfile));
5204 return NULL;
5205 }
5206
5207 const ULONGEST namei_entry_offs
5208 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5209 + namei * map.offset_size),
5210 map.offset_size, map.dwarf5_byte_order);
5211 return map.entry_pool + namei_entry_offs;
5212 }
5213
5214 /* See dw2_debug_names_iterator. */
5215
5216 dwarf2_per_cu_data *
5217 dw2_debug_names_iterator::next ()
5218 {
5219 if (m_addr == NULL)
5220 return NULL;
5221
5222 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5223 struct objfile *objfile = dwarf2_per_objfile->objfile;
5224 bfd *const abfd = objfile->obfd;
5225
5226 again:
5227
5228 unsigned int bytes_read;
5229 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5230 m_addr += bytes_read;
5231 if (abbrev == 0)
5232 return NULL;
5233
5234 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5235 if (indexval_it == m_map.abbrev_map.cend ())
5236 {
5237 complaint (_("Wrong .debug_names undefined abbrev code %s "
5238 "[in module %s]"),
5239 pulongest (abbrev), objfile_name (objfile));
5240 return NULL;
5241 }
5242 const mapped_debug_names::index_val &indexval = indexval_it->second;
5243 enum class symbol_linkage {
5244 unknown,
5245 static_,
5246 extern_,
5247 } symbol_linkage_ = symbol_linkage::unknown;
5248 dwarf2_per_cu_data *per_cu = NULL;
5249 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5250 {
5251 ULONGEST ull;
5252 switch (attr.form)
5253 {
5254 case DW_FORM_implicit_const:
5255 ull = attr.implicit_const;
5256 break;
5257 case DW_FORM_flag_present:
5258 ull = 1;
5259 break;
5260 case DW_FORM_udata:
5261 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5262 m_addr += bytes_read;
5263 break;
5264 default:
5265 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5266 dwarf_form_name (attr.form),
5267 objfile_name (objfile));
5268 return NULL;
5269 }
5270 switch (attr.dw_idx)
5271 {
5272 case DW_IDX_compile_unit:
5273 /* Don't crash on bad data. */
5274 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
5275 {
5276 complaint (_(".debug_names entry has bad CU index %s"
5277 " [in module %s]"),
5278 pulongest (ull),
5279 objfile_name (dwarf2_per_objfile->objfile));
5280 continue;
5281 }
5282 per_cu = dwarf2_per_objfile->get_cutu (ull);
5283 break;
5284 case DW_IDX_type_unit:
5285 /* Don't crash on bad data. */
5286 if (ull >= dwarf2_per_objfile->all_type_units.size ())
5287 {
5288 complaint (_(".debug_names entry has bad TU index %s"
5289 " [in module %s]"),
5290 pulongest (ull),
5291 objfile_name (dwarf2_per_objfile->objfile));
5292 continue;
5293 }
5294 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
5295 break;
5296 case DW_IDX_GNU_internal:
5297 if (!m_map.augmentation_is_gdb)
5298 break;
5299 symbol_linkage_ = symbol_linkage::static_;
5300 break;
5301 case DW_IDX_GNU_external:
5302 if (!m_map.augmentation_is_gdb)
5303 break;
5304 symbol_linkage_ = symbol_linkage::extern_;
5305 break;
5306 }
5307 }
5308
5309 /* Skip if already read in. */
5310 if (per_cu->v.quick->compunit_symtab)
5311 goto again;
5312
5313 /* Check static vs global. */
5314 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5315 {
5316 const bool want_static = *m_block_index == STATIC_BLOCK;
5317 const bool symbol_is_static =
5318 symbol_linkage_ == symbol_linkage::static_;
5319 if (want_static != symbol_is_static)
5320 goto again;
5321 }
5322
5323 /* Match dw2_symtab_iter_next, symbol_kind
5324 and debug_names::psymbol_tag. */
5325 switch (m_domain)
5326 {
5327 case VAR_DOMAIN:
5328 switch (indexval.dwarf_tag)
5329 {
5330 case DW_TAG_variable:
5331 case DW_TAG_subprogram:
5332 /* Some types are also in VAR_DOMAIN. */
5333 case DW_TAG_typedef:
5334 case DW_TAG_structure_type:
5335 break;
5336 default:
5337 goto again;
5338 }
5339 break;
5340 case STRUCT_DOMAIN:
5341 switch (indexval.dwarf_tag)
5342 {
5343 case DW_TAG_typedef:
5344 case DW_TAG_structure_type:
5345 break;
5346 default:
5347 goto again;
5348 }
5349 break;
5350 case LABEL_DOMAIN:
5351 switch (indexval.dwarf_tag)
5352 {
5353 case 0:
5354 case DW_TAG_variable:
5355 break;
5356 default:
5357 goto again;
5358 }
5359 break;
5360 case MODULE_DOMAIN:
5361 switch (indexval.dwarf_tag)
5362 {
5363 case DW_TAG_module:
5364 break;
5365 default:
5366 goto again;
5367 }
5368 break;
5369 default:
5370 break;
5371 }
5372
5373 /* Match dw2_expand_symtabs_matching, symbol_kind and
5374 debug_names::psymbol_tag. */
5375 switch (m_search)
5376 {
5377 case VARIABLES_DOMAIN:
5378 switch (indexval.dwarf_tag)
5379 {
5380 case DW_TAG_variable:
5381 break;
5382 default:
5383 goto again;
5384 }
5385 break;
5386 case FUNCTIONS_DOMAIN:
5387 switch (indexval.dwarf_tag)
5388 {
5389 case DW_TAG_subprogram:
5390 break;
5391 default:
5392 goto again;
5393 }
5394 break;
5395 case TYPES_DOMAIN:
5396 switch (indexval.dwarf_tag)
5397 {
5398 case DW_TAG_typedef:
5399 case DW_TAG_structure_type:
5400 break;
5401 default:
5402 goto again;
5403 }
5404 break;
5405 case MODULES_DOMAIN:
5406 switch (indexval.dwarf_tag)
5407 {
5408 case DW_TAG_module:
5409 break;
5410 default:
5411 goto again;
5412 }
5413 default:
5414 break;
5415 }
5416
5417 return per_cu;
5418 }
5419
5420 static struct compunit_symtab *
5421 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5422 const char *name, domain_enum domain)
5423 {
5424 struct dwarf2_per_objfile *dwarf2_per_objfile
5425 = get_dwarf2_per_objfile (objfile);
5426
5427 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5428 if (!mapp)
5429 {
5430 /* index is NULL if OBJF_READNOW. */
5431 return NULL;
5432 }
5433 const auto &map = *mapp;
5434
5435 dw2_debug_names_iterator iter (map, block_index, domain, name);
5436
5437 struct compunit_symtab *stab_best = NULL;
5438 struct dwarf2_per_cu_data *per_cu;
5439 while ((per_cu = iter.next ()) != NULL)
5440 {
5441 struct symbol *sym, *with_opaque = NULL;
5442 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
5443 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5444 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5445
5446 sym = block_find_symbol (block, name, domain,
5447 block_find_non_opaque_type_preferred,
5448 &with_opaque);
5449
5450 /* Some caution must be observed with overloaded functions and
5451 methods, since the index will not contain any overload
5452 information (but NAME might contain it). */
5453
5454 if (sym != NULL
5455 && strcmp_iw (sym->search_name (), name) == 0)
5456 return stab;
5457 if (with_opaque != NULL
5458 && strcmp_iw (with_opaque->search_name (), name) == 0)
5459 stab_best = stab;
5460
5461 /* Keep looking through other CUs. */
5462 }
5463
5464 return stab_best;
5465 }
5466
5467 /* This dumps minimal information about .debug_names. It is called
5468 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5469 uses this to verify that .debug_names has been loaded. */
5470
5471 static void
5472 dw2_debug_names_dump (struct objfile *objfile)
5473 {
5474 struct dwarf2_per_objfile *dwarf2_per_objfile
5475 = get_dwarf2_per_objfile (objfile);
5476
5477 gdb_assert (dwarf2_per_objfile->using_index);
5478 printf_filtered (".debug_names:");
5479 if (dwarf2_per_objfile->debug_names_table)
5480 printf_filtered (" exists\n");
5481 else
5482 printf_filtered (" faked for \"readnow\"\n");
5483 printf_filtered ("\n");
5484 }
5485
5486 static void
5487 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5488 const char *func_name)
5489 {
5490 struct dwarf2_per_objfile *dwarf2_per_objfile
5491 = get_dwarf2_per_objfile (objfile);
5492
5493 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5494 if (dwarf2_per_objfile->debug_names_table)
5495 {
5496 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5497
5498 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5499
5500 struct dwarf2_per_cu_data *per_cu;
5501 while ((per_cu = iter.next ()) != NULL)
5502 dw2_instantiate_symtab (per_cu, false);
5503 }
5504 }
5505
5506 static void
5507 dw2_debug_names_map_matching_symbols
5508 (struct objfile *objfile,
5509 const lookup_name_info &name, domain_enum domain,
5510 int global,
5511 gdb::function_view<symbol_found_callback_ftype> callback,
5512 symbol_compare_ftype *ordered_compare)
5513 {
5514 struct dwarf2_per_objfile *dwarf2_per_objfile
5515 = get_dwarf2_per_objfile (objfile);
5516
5517 /* debug_names_table is NULL if OBJF_READNOW. */
5518 if (!dwarf2_per_objfile->debug_names_table)
5519 return;
5520
5521 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5522 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5523
5524 const char *match_name = name.ada ().lookup_name ().c_str ();
5525 auto matcher = [&] (const char *symname)
5526 {
5527 if (ordered_compare == nullptr)
5528 return true;
5529 return ordered_compare (symname, match_name) == 0;
5530 };
5531
5532 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5533 [&] (offset_type namei)
5534 {
5535 /* The name was matched, now expand corresponding CUs that were
5536 marked. */
5537 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5538
5539 struct dwarf2_per_cu_data *per_cu;
5540 while ((per_cu = iter.next ()) != NULL)
5541 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5542 return true;
5543 });
5544
5545 /* It's a shame we couldn't do this inside the
5546 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5547 that have already been expanded. Instead, this loop matches what
5548 the psymtab code does. */
5549 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5550 {
5551 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5552 if (cust != nullptr)
5553 {
5554 const struct block *block
5555 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5556 if (!iterate_over_symbols_terminated (block, name,
5557 domain, callback))
5558 break;
5559 }
5560 }
5561 }
5562
5563 static void
5564 dw2_debug_names_expand_symtabs_matching
5565 (struct objfile *objfile,
5566 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5567 const lookup_name_info &lookup_name,
5568 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5569 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5570 enum search_domain kind)
5571 {
5572 struct dwarf2_per_objfile *dwarf2_per_objfile
5573 = get_dwarf2_per_objfile (objfile);
5574
5575 /* debug_names_table is NULL if OBJF_READNOW. */
5576 if (!dwarf2_per_objfile->debug_names_table)
5577 return;
5578
5579 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5580
5581 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5582
5583 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5584 symbol_matcher,
5585 kind, [&] (offset_type namei)
5586 {
5587 /* The name was matched, now expand corresponding CUs that were
5588 marked. */
5589 dw2_debug_names_iterator iter (map, kind, namei);
5590
5591 struct dwarf2_per_cu_data *per_cu;
5592 while ((per_cu = iter.next ()) != NULL)
5593 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5594 expansion_notify);
5595 return true;
5596 });
5597 }
5598
5599 const struct quick_symbol_functions dwarf2_debug_names_functions =
5600 {
5601 dw2_has_symbols,
5602 dw2_find_last_source_symtab,
5603 dw2_forget_cached_source_info,
5604 dw2_map_symtabs_matching_filename,
5605 dw2_debug_names_lookup_symbol,
5606 dw2_print_stats,
5607 dw2_debug_names_dump,
5608 dw2_debug_names_expand_symtabs_for_function,
5609 dw2_expand_all_symtabs,
5610 dw2_expand_symtabs_with_fullname,
5611 dw2_debug_names_map_matching_symbols,
5612 dw2_debug_names_expand_symtabs_matching,
5613 dw2_find_pc_sect_compunit_symtab,
5614 NULL,
5615 dw2_map_symbol_filenames
5616 };
5617
5618 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5619 to either a dwarf2_per_objfile or dwz_file object. */
5620
5621 template <typename T>
5622 static gdb::array_view<const gdb_byte>
5623 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5624 {
5625 dwarf2_section_info *section = &section_owner->gdb_index;
5626
5627 if (section->empty ())
5628 return {};
5629
5630 /* Older elfutils strip versions could keep the section in the main
5631 executable while splitting it for the separate debug info file. */
5632 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5633 return {};
5634
5635 section->read (obj);
5636
5637 /* dwarf2_section_info::size is a bfd_size_type, while
5638 gdb::array_view works with size_t. On 32-bit hosts, with
5639 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5640 is 32-bit. So we need an explicit narrowing conversion here.
5641 This is fine, because it's impossible to allocate or mmap an
5642 array/buffer larger than what size_t can represent. */
5643 return gdb::make_array_view (section->buffer, section->size);
5644 }
5645
5646 /* Lookup the index cache for the contents of the index associated to
5647 DWARF2_OBJ. */
5648
5649 static gdb::array_view<const gdb_byte>
5650 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5651 {
5652 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5653 if (build_id == nullptr)
5654 return {};
5655
5656 return global_index_cache.lookup_gdb_index (build_id,
5657 &dwarf2_obj->index_cache_res);
5658 }
5659
5660 /* Same as the above, but for DWZ. */
5661
5662 static gdb::array_view<const gdb_byte>
5663 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5664 {
5665 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5666 if (build_id == nullptr)
5667 return {};
5668
5669 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5670 }
5671
5672 /* See symfile.h. */
5673
5674 bool
5675 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5676 {
5677 struct dwarf2_per_objfile *dwarf2_per_objfile
5678 = get_dwarf2_per_objfile (objfile);
5679
5680 /* If we're about to read full symbols, don't bother with the
5681 indices. In this case we also don't care if some other debug
5682 format is making psymtabs, because they are all about to be
5683 expanded anyway. */
5684 if ((objfile->flags & OBJF_READNOW))
5685 {
5686 dwarf2_per_objfile->using_index = 1;
5687 create_all_comp_units (dwarf2_per_objfile);
5688 create_all_type_units (dwarf2_per_objfile);
5689 dwarf2_per_objfile->quick_file_names_table
5690 = create_quick_file_names_table
5691 (dwarf2_per_objfile->all_comp_units.size ());
5692
5693 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
5694 + dwarf2_per_objfile->all_type_units.size ()); ++i)
5695 {
5696 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
5697
5698 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5699 struct dwarf2_per_cu_quick_data);
5700 }
5701
5702 /* Return 1 so that gdb sees the "quick" functions. However,
5703 these functions will be no-ops because we will have expanded
5704 all symtabs. */
5705 *index_kind = dw_index_kind::GDB_INDEX;
5706 return true;
5707 }
5708
5709 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5710 {
5711 *index_kind = dw_index_kind::DEBUG_NAMES;
5712 return true;
5713 }
5714
5715 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5716 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5717 get_gdb_index_contents_from_section<dwz_file>))
5718 {
5719 *index_kind = dw_index_kind::GDB_INDEX;
5720 return true;
5721 }
5722
5723 /* ... otherwise, try to find the index in the index cache. */
5724 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5725 get_gdb_index_contents_from_cache,
5726 get_gdb_index_contents_from_cache_dwz))
5727 {
5728 global_index_cache.hit ();
5729 *index_kind = dw_index_kind::GDB_INDEX;
5730 return true;
5731 }
5732
5733 global_index_cache.miss ();
5734 return false;
5735 }
5736
5737 \f
5738
5739 /* Build a partial symbol table. */
5740
5741 void
5742 dwarf2_build_psymtabs (struct objfile *objfile)
5743 {
5744 struct dwarf2_per_objfile *dwarf2_per_objfile
5745 = get_dwarf2_per_objfile (objfile);
5746
5747 init_psymbol_list (objfile, 1024);
5748
5749 try
5750 {
5751 /* This isn't really ideal: all the data we allocate on the
5752 objfile's obstack is still uselessly kept around. However,
5753 freeing it seems unsafe. */
5754 psymtab_discarder psymtabs (objfile);
5755 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5756 psymtabs.keep ();
5757
5758 /* (maybe) store an index in the cache. */
5759 global_index_cache.store (dwarf2_per_objfile);
5760 }
5761 catch (const gdb_exception_error &except)
5762 {
5763 exception_print (gdb_stderr, except);
5764 }
5765 }
5766
5767 /* Find the base address of the compilation unit for range lists and
5768 location lists. It will normally be specified by DW_AT_low_pc.
5769 In DWARF-3 draft 4, the base address could be overridden by
5770 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5771 compilation units with discontinuous ranges. */
5772
5773 static void
5774 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5775 {
5776 struct attribute *attr;
5777
5778 cu->base_address.reset ();
5779
5780 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5781 if (attr != nullptr)
5782 cu->base_address = attr->value_as_address ();
5783 else
5784 {
5785 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5786 if (attr != nullptr)
5787 cu->base_address = attr->value_as_address ();
5788 }
5789 }
5790
5791 /* Helper function that returns the proper abbrev section for
5792 THIS_CU. */
5793
5794 static struct dwarf2_section_info *
5795 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5796 {
5797 struct dwarf2_section_info *abbrev;
5798 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
5799
5800 if (this_cu->is_dwz)
5801 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
5802 else
5803 abbrev = &dwarf2_per_objfile->abbrev;
5804
5805 return abbrev;
5806 }
5807
5808 /* Fetch the abbreviation table offset from a comp or type unit header. */
5809
5810 static sect_offset
5811 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5812 struct dwarf2_section_info *section,
5813 sect_offset sect_off)
5814 {
5815 bfd *abfd = section->get_bfd_owner ();
5816 const gdb_byte *info_ptr;
5817 unsigned int initial_length_size, offset_size;
5818 uint16_t version;
5819
5820 section->read (dwarf2_per_objfile->objfile);
5821 info_ptr = section->buffer + to_underlying (sect_off);
5822 read_initial_length (abfd, info_ptr, &initial_length_size);
5823 offset_size = initial_length_size == 4 ? 4 : 8;
5824 info_ptr += initial_length_size;
5825
5826 version = read_2_bytes (abfd, info_ptr);
5827 info_ptr += 2;
5828 if (version >= 5)
5829 {
5830 /* Skip unit type and address size. */
5831 info_ptr += 2;
5832 }
5833
5834 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5835 }
5836
5837 /* A partial symtab that is used only for include files. */
5838 struct dwarf2_include_psymtab : public partial_symtab
5839 {
5840 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5841 : partial_symtab (filename, objfile)
5842 {
5843 }
5844
5845 void read_symtab (struct objfile *objfile) override
5846 {
5847 expand_psymtab (objfile);
5848 }
5849
5850 void expand_psymtab (struct objfile *objfile) override
5851 {
5852 if (m_readin)
5853 return;
5854 /* It's an include file, no symbols to read for it.
5855 Everything is in the parent symtab. */
5856 read_dependencies (objfile);
5857 m_readin = true;
5858 }
5859
5860 bool readin_p () const override
5861 {
5862 return m_readin;
5863 }
5864
5865 struct compunit_symtab *get_compunit_symtab () const override
5866 {
5867 return nullptr;
5868 }
5869
5870 private:
5871
5872 bool m_readin = false;
5873 };
5874
5875 /* Allocate a new partial symtab for file named NAME and mark this new
5876 partial symtab as being an include of PST. */
5877
5878 static void
5879 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
5880 struct objfile *objfile)
5881 {
5882 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
5883
5884 if (!IS_ABSOLUTE_PATH (subpst->filename))
5885 {
5886 /* It shares objfile->objfile_obstack. */
5887 subpst->dirname = pst->dirname;
5888 }
5889
5890 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
5891 subpst->dependencies[0] = pst;
5892 subpst->number_of_dependencies = 1;
5893 }
5894
5895 /* Read the Line Number Program data and extract the list of files
5896 included by the source file represented by PST. Build an include
5897 partial symtab for each of these included files. */
5898
5899 static void
5900 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
5901 struct die_info *die,
5902 dwarf2_psymtab *pst)
5903 {
5904 line_header_up lh;
5905 struct attribute *attr;
5906
5907 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5908 if (attr != nullptr)
5909 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
5910 if (lh == NULL)
5911 return; /* No linetable, so no includes. */
5912
5913 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5914 that we pass in the raw text_low here; that is ok because we're
5915 only decoding the line table to make include partial symtabs, and
5916 so the addresses aren't really used. */
5917 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
5918 pst->raw_text_low (), 1);
5919 }
5920
5921 static hashval_t
5922 hash_signatured_type (const void *item)
5923 {
5924 const struct signatured_type *sig_type
5925 = (const struct signatured_type *) item;
5926
5927 /* This drops the top 32 bits of the signature, but is ok for a hash. */
5928 return sig_type->signature;
5929 }
5930
5931 static int
5932 eq_signatured_type (const void *item_lhs, const void *item_rhs)
5933 {
5934 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5935 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
5936
5937 return lhs->signature == rhs->signature;
5938 }
5939
5940 /* Allocate a hash table for signatured types. */
5941
5942 static htab_up
5943 allocate_signatured_type_table ()
5944 {
5945 return htab_up (htab_create_alloc (41,
5946 hash_signatured_type,
5947 eq_signatured_type,
5948 NULL, xcalloc, xfree));
5949 }
5950
5951 /* A helper function to add a signatured type CU to a table. */
5952
5953 static int
5954 add_signatured_type_cu_to_table (void **slot, void *datum)
5955 {
5956 struct signatured_type *sigt = (struct signatured_type *) *slot;
5957 std::vector<signatured_type *> *all_type_units
5958 = (std::vector<signatured_type *> *) datum;
5959
5960 all_type_units->push_back (sigt);
5961
5962 return 1;
5963 }
5964
5965 /* A helper for create_debug_types_hash_table. Read types from SECTION
5966 and fill them into TYPES_HTAB. It will process only type units,
5967 therefore DW_UT_type. */
5968
5969 static void
5970 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5971 struct dwo_file *dwo_file,
5972 dwarf2_section_info *section, htab_up &types_htab,
5973 rcuh_kind section_kind)
5974 {
5975 struct objfile *objfile = dwarf2_per_objfile->objfile;
5976 struct dwarf2_section_info *abbrev_section;
5977 bfd *abfd;
5978 const gdb_byte *info_ptr, *end_ptr;
5979
5980 abbrev_section = (dwo_file != NULL
5981 ? &dwo_file->sections.abbrev
5982 : &dwarf2_per_objfile->abbrev);
5983
5984 if (dwarf_read_debug)
5985 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
5986 section->get_name (),
5987 abbrev_section->get_file_name ());
5988
5989 section->read (objfile);
5990 info_ptr = section->buffer;
5991
5992 if (info_ptr == NULL)
5993 return;
5994
5995 /* We can't set abfd until now because the section may be empty or
5996 not present, in which case the bfd is unknown. */
5997 abfd = section->get_bfd_owner ();
5998
5999 /* We don't use cutu_reader here because we don't need to read
6000 any dies: the signature is in the header. */
6001
6002 end_ptr = info_ptr + section->size;
6003 while (info_ptr < end_ptr)
6004 {
6005 struct signatured_type *sig_type;
6006 struct dwo_unit *dwo_tu;
6007 void **slot;
6008 const gdb_byte *ptr = info_ptr;
6009 struct comp_unit_head header;
6010 unsigned int length;
6011
6012 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6013
6014 /* Initialize it due to a false compiler warning. */
6015 header.signature = -1;
6016 header.type_cu_offset_in_tu = (cu_offset) -1;
6017
6018 /* We need to read the type's signature in order to build the hash
6019 table, but we don't need anything else just yet. */
6020
6021 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6022 abbrev_section, ptr, section_kind);
6023
6024 length = header.get_length ();
6025
6026 /* Skip dummy type units. */
6027 if (ptr >= info_ptr + length
6028 || peek_abbrev_code (abfd, ptr) == 0
6029 || header.unit_type != DW_UT_type)
6030 {
6031 info_ptr += length;
6032 continue;
6033 }
6034
6035 if (types_htab == NULL)
6036 {
6037 if (dwo_file)
6038 types_htab = allocate_dwo_unit_table ();
6039 else
6040 types_htab = allocate_signatured_type_table ();
6041 }
6042
6043 if (dwo_file)
6044 {
6045 sig_type = NULL;
6046 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6047 struct dwo_unit);
6048 dwo_tu->dwo_file = dwo_file;
6049 dwo_tu->signature = header.signature;
6050 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6051 dwo_tu->section = section;
6052 dwo_tu->sect_off = sect_off;
6053 dwo_tu->length = length;
6054 }
6055 else
6056 {
6057 /* N.B.: type_offset is not usable if this type uses a DWO file.
6058 The real type_offset is in the DWO file. */
6059 dwo_tu = NULL;
6060 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6061 struct signatured_type);
6062 sig_type->signature = header.signature;
6063 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6064 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6065 sig_type->per_cu.is_debug_types = 1;
6066 sig_type->per_cu.section = section;
6067 sig_type->per_cu.sect_off = sect_off;
6068 sig_type->per_cu.length = length;
6069 }
6070
6071 slot = htab_find_slot (types_htab.get (),
6072 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6073 INSERT);
6074 gdb_assert (slot != NULL);
6075 if (*slot != NULL)
6076 {
6077 sect_offset dup_sect_off;
6078
6079 if (dwo_file)
6080 {
6081 const struct dwo_unit *dup_tu
6082 = (const struct dwo_unit *) *slot;
6083
6084 dup_sect_off = dup_tu->sect_off;
6085 }
6086 else
6087 {
6088 const struct signatured_type *dup_tu
6089 = (const struct signatured_type *) *slot;
6090
6091 dup_sect_off = dup_tu->per_cu.sect_off;
6092 }
6093
6094 complaint (_("debug type entry at offset %s is duplicate to"
6095 " the entry at offset %s, signature %s"),
6096 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6097 hex_string (header.signature));
6098 }
6099 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6100
6101 if (dwarf_read_debug > 1)
6102 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6103 sect_offset_str (sect_off),
6104 hex_string (header.signature));
6105
6106 info_ptr += length;
6107 }
6108 }
6109
6110 /* Create the hash table of all entries in the .debug_types
6111 (or .debug_types.dwo) section(s).
6112 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6113 otherwise it is NULL.
6114
6115 The result is a pointer to the hash table or NULL if there are no types.
6116
6117 Note: This function processes DWO files only, not DWP files. */
6118
6119 static void
6120 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6121 struct dwo_file *dwo_file,
6122 gdb::array_view<dwarf2_section_info> type_sections,
6123 htab_up &types_htab)
6124 {
6125 for (dwarf2_section_info &section : type_sections)
6126 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6127 types_htab, rcuh_kind::TYPE);
6128 }
6129
6130 /* Create the hash table of all entries in the .debug_types section,
6131 and initialize all_type_units.
6132 The result is zero if there is an error (e.g. missing .debug_types section),
6133 otherwise non-zero. */
6134
6135 static int
6136 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6137 {
6138 htab_up types_htab;
6139
6140 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6141 &dwarf2_per_objfile->info, types_htab,
6142 rcuh_kind::COMPILE);
6143 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6144 dwarf2_per_objfile->types, types_htab);
6145 if (types_htab == NULL)
6146 {
6147 dwarf2_per_objfile->signatured_types = NULL;
6148 return 0;
6149 }
6150
6151 dwarf2_per_objfile->signatured_types = std::move (types_htab);
6152
6153 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6154 dwarf2_per_objfile->all_type_units.reserve
6155 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
6156
6157 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6158 add_signatured_type_cu_to_table,
6159 &dwarf2_per_objfile->all_type_units);
6160
6161 return 1;
6162 }
6163
6164 /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6165 If SLOT is non-NULL, it is the entry to use in the hash table.
6166 Otherwise we find one. */
6167
6168 static struct signatured_type *
6169 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6170 void **slot)
6171 {
6172 struct objfile *objfile = dwarf2_per_objfile->objfile;
6173
6174 if (dwarf2_per_objfile->all_type_units.size ()
6175 == dwarf2_per_objfile->all_type_units.capacity ())
6176 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6177
6178 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6179 struct signatured_type);
6180
6181 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6182 sig_type->signature = sig;
6183 sig_type->per_cu.is_debug_types = 1;
6184 if (dwarf2_per_objfile->using_index)
6185 {
6186 sig_type->per_cu.v.quick =
6187 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6188 struct dwarf2_per_cu_quick_data);
6189 }
6190
6191 if (slot == NULL)
6192 {
6193 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6194 sig_type, INSERT);
6195 }
6196 gdb_assert (*slot == NULL);
6197 *slot = sig_type;
6198 /* The rest of sig_type must be filled in by the caller. */
6199 return sig_type;
6200 }
6201
6202 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6203 Fill in SIG_ENTRY with DWO_ENTRY. */
6204
6205 static void
6206 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6207 struct signatured_type *sig_entry,
6208 struct dwo_unit *dwo_entry)
6209 {
6210 /* Make sure we're not clobbering something we don't expect to. */
6211 gdb_assert (! sig_entry->per_cu.queued);
6212 gdb_assert (sig_entry->per_cu.cu == NULL);
6213 if (dwarf2_per_objfile->using_index)
6214 {
6215 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6216 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6217 }
6218 else
6219 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6220 gdb_assert (sig_entry->signature == dwo_entry->signature);
6221 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6222 gdb_assert (sig_entry->type_unit_group == NULL);
6223 gdb_assert (sig_entry->dwo_unit == NULL);
6224
6225 sig_entry->per_cu.section = dwo_entry->section;
6226 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6227 sig_entry->per_cu.length = dwo_entry->length;
6228 sig_entry->per_cu.reading_dwo_directly = 1;
6229 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6230 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6231 sig_entry->dwo_unit = dwo_entry;
6232 }
6233
6234 /* Subroutine of lookup_signatured_type.
6235 If we haven't read the TU yet, create the signatured_type data structure
6236 for a TU to be read in directly from a DWO file, bypassing the stub.
6237 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6238 using .gdb_index, then when reading a CU we want to stay in the DWO file
6239 containing that CU. Otherwise we could end up reading several other DWO
6240 files (due to comdat folding) to process the transitive closure of all the
6241 mentioned TUs, and that can be slow. The current DWO file will have every
6242 type signature that it needs.
6243 We only do this for .gdb_index because in the psymtab case we already have
6244 to read all the DWOs to build the type unit groups. */
6245
6246 static struct signatured_type *
6247 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6248 {
6249 struct dwarf2_per_objfile *dwarf2_per_objfile
6250 = cu->per_cu->dwarf2_per_objfile;
6251 struct dwo_file *dwo_file;
6252 struct dwo_unit find_dwo_entry, *dwo_entry;
6253 struct signatured_type find_sig_entry, *sig_entry;
6254 void **slot;
6255
6256 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6257
6258 /* If TU skeletons have been removed then we may not have read in any
6259 TUs yet. */
6260 if (dwarf2_per_objfile->signatured_types == NULL)
6261 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6262
6263 /* We only ever need to read in one copy of a signatured type.
6264 Use the global signatured_types array to do our own comdat-folding
6265 of types. If this is the first time we're reading this TU, and
6266 the TU has an entry in .gdb_index, replace the recorded data from
6267 .gdb_index with this TU. */
6268
6269 find_sig_entry.signature = sig;
6270 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6271 &find_sig_entry, INSERT);
6272 sig_entry = (struct signatured_type *) *slot;
6273
6274 /* We can get here with the TU already read, *or* in the process of being
6275 read. Don't reassign the global entry to point to this DWO if that's
6276 the case. Also note that if the TU is already being read, it may not
6277 have come from a DWO, the program may be a mix of Fission-compiled
6278 code and non-Fission-compiled code. */
6279
6280 /* Have we already tried to read this TU?
6281 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6282 needn't exist in the global table yet). */
6283 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6284 return sig_entry;
6285
6286 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6287 dwo_unit of the TU itself. */
6288 dwo_file = cu->dwo_unit->dwo_file;
6289
6290 /* Ok, this is the first time we're reading this TU. */
6291 if (dwo_file->tus == NULL)
6292 return NULL;
6293 find_dwo_entry.signature = sig;
6294 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6295 &find_dwo_entry);
6296 if (dwo_entry == NULL)
6297 return NULL;
6298
6299 /* If the global table doesn't have an entry for this TU, add one. */
6300 if (sig_entry == NULL)
6301 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6302
6303 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6304 sig_entry->per_cu.tu_read = 1;
6305 return sig_entry;
6306 }
6307
6308 /* Subroutine of lookup_signatured_type.
6309 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6310 then try the DWP file. If the TU stub (skeleton) has been removed then
6311 it won't be in .gdb_index. */
6312
6313 static struct signatured_type *
6314 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6315 {
6316 struct dwarf2_per_objfile *dwarf2_per_objfile
6317 = cu->per_cu->dwarf2_per_objfile;
6318 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6319 struct dwo_unit *dwo_entry;
6320 struct signatured_type find_sig_entry, *sig_entry;
6321 void **slot;
6322
6323 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6324 gdb_assert (dwp_file != NULL);
6325
6326 /* If TU skeletons have been removed then we may not have read in any
6327 TUs yet. */
6328 if (dwarf2_per_objfile->signatured_types == NULL)
6329 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6330
6331 find_sig_entry.signature = sig;
6332 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6333 &find_sig_entry, INSERT);
6334 sig_entry = (struct signatured_type *) *slot;
6335
6336 /* Have we already tried to read this TU?
6337 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6338 needn't exist in the global table yet). */
6339 if (sig_entry != NULL)
6340 return sig_entry;
6341
6342 if (dwp_file->tus == NULL)
6343 return NULL;
6344 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6345 sig, 1 /* is_debug_types */);
6346 if (dwo_entry == NULL)
6347 return NULL;
6348
6349 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6350 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6351
6352 return sig_entry;
6353 }
6354
6355 /* Lookup a signature based type for DW_FORM_ref_sig8.
6356 Returns NULL if signature SIG is not present in the table.
6357 It is up to the caller to complain about this. */
6358
6359 static struct signatured_type *
6360 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6361 {
6362 struct dwarf2_per_objfile *dwarf2_per_objfile
6363 = cu->per_cu->dwarf2_per_objfile;
6364
6365 if (cu->dwo_unit
6366 && dwarf2_per_objfile->using_index)
6367 {
6368 /* We're in a DWO/DWP file, and we're using .gdb_index.
6369 These cases require special processing. */
6370 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6371 return lookup_dwo_signatured_type (cu, sig);
6372 else
6373 return lookup_dwp_signatured_type (cu, sig);
6374 }
6375 else
6376 {
6377 struct signatured_type find_entry, *entry;
6378
6379 if (dwarf2_per_objfile->signatured_types == NULL)
6380 return NULL;
6381 find_entry.signature = sig;
6382 entry = ((struct signatured_type *)
6383 htab_find (dwarf2_per_objfile->signatured_types.get (),
6384 &find_entry));
6385 return entry;
6386 }
6387 }
6388
6389 /* Low level DIE reading support. */
6390
6391 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6392
6393 static void
6394 init_cu_die_reader (struct die_reader_specs *reader,
6395 struct dwarf2_cu *cu,
6396 struct dwarf2_section_info *section,
6397 struct dwo_file *dwo_file,
6398 struct abbrev_table *abbrev_table)
6399 {
6400 gdb_assert (section->readin && section->buffer != NULL);
6401 reader->abfd = section->get_bfd_owner ();
6402 reader->cu = cu;
6403 reader->dwo_file = dwo_file;
6404 reader->die_section = section;
6405 reader->buffer = section->buffer;
6406 reader->buffer_end = section->buffer + section->size;
6407 reader->abbrev_table = abbrev_table;
6408 }
6409
6410 /* Subroutine of cutu_reader to simplify it.
6411 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6412 There's just a lot of work to do, and cutu_reader is big enough
6413 already.
6414
6415 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6416 from it to the DIE in the DWO. If NULL we are skipping the stub.
6417 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6418 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6419 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6420 STUB_COMP_DIR may be non-NULL.
6421 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6422 are filled in with the info of the DIE from the DWO file.
6423 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6424 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6425 kept around for at least as long as *RESULT_READER.
6426
6427 The result is non-zero if a valid (non-dummy) DIE was found. */
6428
6429 static int
6430 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6431 struct dwo_unit *dwo_unit,
6432 struct die_info *stub_comp_unit_die,
6433 const char *stub_comp_dir,
6434 struct die_reader_specs *result_reader,
6435 const gdb_byte **result_info_ptr,
6436 struct die_info **result_comp_unit_die,
6437 abbrev_table_up *result_dwo_abbrev_table)
6438 {
6439 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6440 struct objfile *objfile = dwarf2_per_objfile->objfile;
6441 struct dwarf2_cu *cu = this_cu->cu;
6442 bfd *abfd;
6443 const gdb_byte *begin_info_ptr, *info_ptr;
6444 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6445 int i,num_extra_attrs;
6446 struct dwarf2_section_info *dwo_abbrev_section;
6447 struct die_info *comp_unit_die;
6448
6449 /* At most one of these may be provided. */
6450 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6451
6452 /* These attributes aren't processed until later:
6453 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6454 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6455 referenced later. However, these attributes are found in the stub
6456 which we won't have later. In order to not impose this complication
6457 on the rest of the code, we read them here and copy them to the
6458 DWO CU/TU die. */
6459
6460 stmt_list = NULL;
6461 low_pc = NULL;
6462 high_pc = NULL;
6463 ranges = NULL;
6464 comp_dir = NULL;
6465
6466 if (stub_comp_unit_die != NULL)
6467 {
6468 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6469 DWO file. */
6470 if (! this_cu->is_debug_types)
6471 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6472 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6473 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6474 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6475 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6476
6477 cu->addr_base = stub_comp_unit_die->addr_base ();
6478
6479 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6480 here (if needed). We need the value before we can process
6481 DW_AT_ranges. */
6482 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6483 }
6484 else if (stub_comp_dir != NULL)
6485 {
6486 /* Reconstruct the comp_dir attribute to simplify the code below. */
6487 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6488 comp_dir->name = DW_AT_comp_dir;
6489 comp_dir->form = DW_FORM_string;
6490 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6491 DW_STRING (comp_dir) = stub_comp_dir;
6492 }
6493
6494 /* Set up for reading the DWO CU/TU. */
6495 cu->dwo_unit = dwo_unit;
6496 dwarf2_section_info *section = dwo_unit->section;
6497 section->read (objfile);
6498 abfd = section->get_bfd_owner ();
6499 begin_info_ptr = info_ptr = (section->buffer
6500 + to_underlying (dwo_unit->sect_off));
6501 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6502
6503 if (this_cu->is_debug_types)
6504 {
6505 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6506
6507 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6508 &cu->header, section,
6509 dwo_abbrev_section,
6510 info_ptr, rcuh_kind::TYPE);
6511 /* This is not an assert because it can be caused by bad debug info. */
6512 if (sig_type->signature != cu->header.signature)
6513 {
6514 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6515 " TU at offset %s [in module %s]"),
6516 hex_string (sig_type->signature),
6517 hex_string (cu->header.signature),
6518 sect_offset_str (dwo_unit->sect_off),
6519 bfd_get_filename (abfd));
6520 }
6521 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6522 /* For DWOs coming from DWP files, we don't know the CU length
6523 nor the type's offset in the TU until now. */
6524 dwo_unit->length = cu->header.get_length ();
6525 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6526
6527 /* Establish the type offset that can be used to lookup the type.
6528 For DWO files, we don't know it until now. */
6529 sig_type->type_offset_in_section
6530 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6531 }
6532 else
6533 {
6534 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6535 &cu->header, section,
6536 dwo_abbrev_section,
6537 info_ptr, rcuh_kind::COMPILE);
6538 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6539 /* For DWOs coming from DWP files, we don't know the CU length
6540 until now. */
6541 dwo_unit->length = cu->header.get_length ();
6542 }
6543
6544 *result_dwo_abbrev_table
6545 = abbrev_table::read (objfile, dwo_abbrev_section,
6546 cu->header.abbrev_sect_off);
6547 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6548 result_dwo_abbrev_table->get ());
6549
6550 /* Read in the die, but leave space to copy over the attributes
6551 from the stub. This has the benefit of simplifying the rest of
6552 the code - all the work to maintain the illusion of a single
6553 DW_TAG_{compile,type}_unit DIE is done here. */
6554 num_extra_attrs = ((stmt_list != NULL)
6555 + (low_pc != NULL)
6556 + (high_pc != NULL)
6557 + (ranges != NULL)
6558 + (comp_dir != NULL));
6559 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6560 num_extra_attrs);
6561
6562 /* Copy over the attributes from the stub to the DIE we just read in. */
6563 comp_unit_die = *result_comp_unit_die;
6564 i = comp_unit_die->num_attrs;
6565 if (stmt_list != NULL)
6566 comp_unit_die->attrs[i++] = *stmt_list;
6567 if (low_pc != NULL)
6568 comp_unit_die->attrs[i++] = *low_pc;
6569 if (high_pc != NULL)
6570 comp_unit_die->attrs[i++] = *high_pc;
6571 if (ranges != NULL)
6572 comp_unit_die->attrs[i++] = *ranges;
6573 if (comp_dir != NULL)
6574 comp_unit_die->attrs[i++] = *comp_dir;
6575 comp_unit_die->num_attrs += num_extra_attrs;
6576
6577 if (dwarf_die_debug)
6578 {
6579 fprintf_unfiltered (gdb_stdlog,
6580 "Read die from %s@0x%x of %s:\n",
6581 section->get_name (),
6582 (unsigned) (begin_info_ptr - section->buffer),
6583 bfd_get_filename (abfd));
6584 dump_die (comp_unit_die, dwarf_die_debug);
6585 }
6586
6587 /* Skip dummy compilation units. */
6588 if (info_ptr >= begin_info_ptr + dwo_unit->length
6589 || peek_abbrev_code (abfd, info_ptr) == 0)
6590 return 0;
6591
6592 *result_info_ptr = info_ptr;
6593 return 1;
6594 }
6595
6596 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6597 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6598 signature is part of the header. */
6599 static gdb::optional<ULONGEST>
6600 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6601 {
6602 if (cu->header.version >= 5)
6603 return cu->header.signature;
6604 struct attribute *attr;
6605 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6606 if (attr == nullptr)
6607 return gdb::optional<ULONGEST> ();
6608 return DW_UNSND (attr);
6609 }
6610
6611 /* Subroutine of cutu_reader to simplify it.
6612 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6613 Returns NULL if the specified DWO unit cannot be found. */
6614
6615 static struct dwo_unit *
6616 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6617 struct die_info *comp_unit_die,
6618 const char *dwo_name)
6619 {
6620 struct dwarf2_cu *cu = this_cu->cu;
6621 struct dwo_unit *dwo_unit;
6622 const char *comp_dir;
6623
6624 gdb_assert (cu != NULL);
6625
6626 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6627 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6628 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6629
6630 if (this_cu->is_debug_types)
6631 {
6632 struct signatured_type *sig_type;
6633
6634 /* Since this_cu is the first member of struct signatured_type,
6635 we can go from a pointer to one to a pointer to the other. */
6636 sig_type = (struct signatured_type *) this_cu;
6637 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6638 }
6639 else
6640 {
6641 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6642 if (!signature.has_value ())
6643 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6644 " [in module %s]"),
6645 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
6646 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6647 *signature);
6648 }
6649
6650 return dwo_unit;
6651 }
6652
6653 /* Subroutine of cutu_reader to simplify it.
6654 See it for a description of the parameters.
6655 Read a TU directly from a DWO file, bypassing the stub. */
6656
6657 void
6658 cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6659 int use_existing_cu)
6660 {
6661 struct signatured_type *sig_type;
6662
6663 /* Verify we can do the following downcast, and that we have the
6664 data we need. */
6665 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6666 sig_type = (struct signatured_type *) this_cu;
6667 gdb_assert (sig_type->dwo_unit != NULL);
6668
6669 if (use_existing_cu && this_cu->cu != NULL)
6670 {
6671 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6672 /* There's no need to do the rereading_dwo_cu handling that
6673 cutu_reader does since we don't read the stub. */
6674 }
6675 else
6676 {
6677 /* If !use_existing_cu, this_cu->cu must be NULL. */
6678 gdb_assert (this_cu->cu == NULL);
6679 m_new_cu.reset (new dwarf2_cu (this_cu));
6680 }
6681
6682 /* A future optimization, if needed, would be to use an existing
6683 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6684 could share abbrev tables. */
6685
6686 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6687 NULL /* stub_comp_unit_die */,
6688 sig_type->dwo_unit->dwo_file->comp_dir,
6689 this, &info_ptr,
6690 &comp_unit_die,
6691 &m_dwo_abbrev_table) == 0)
6692 {
6693 /* Dummy die. */
6694 dummy_p = true;
6695 }
6696 }
6697
6698 /* Initialize a CU (or TU) and read its DIEs.
6699 If the CU defers to a DWO file, read the DWO file as well.
6700
6701 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6702 Otherwise the table specified in the comp unit header is read in and used.
6703 This is an optimization for when we already have the abbrev table.
6704
6705 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6706 Otherwise, a new CU is allocated with xmalloc. */
6707
6708 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6709 struct abbrev_table *abbrev_table,
6710 int use_existing_cu,
6711 bool skip_partial)
6712 : die_reader_specs {},
6713 m_this_cu (this_cu)
6714 {
6715 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6716 struct objfile *objfile = dwarf2_per_objfile->objfile;
6717 struct dwarf2_section_info *section = this_cu->section;
6718 bfd *abfd = section->get_bfd_owner ();
6719 struct dwarf2_cu *cu;
6720 const gdb_byte *begin_info_ptr;
6721 struct signatured_type *sig_type = NULL;
6722 struct dwarf2_section_info *abbrev_section;
6723 /* Non-zero if CU currently points to a DWO file and we need to
6724 reread it. When this happens we need to reread the skeleton die
6725 before we can reread the DWO file (this only applies to CUs, not TUs). */
6726 int rereading_dwo_cu = 0;
6727
6728 if (dwarf_die_debug)
6729 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6730 this_cu->is_debug_types ? "type" : "comp",
6731 sect_offset_str (this_cu->sect_off));
6732
6733 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6734 file (instead of going through the stub), short-circuit all of this. */
6735 if (this_cu->reading_dwo_directly)
6736 {
6737 /* Narrow down the scope of possibilities to have to understand. */
6738 gdb_assert (this_cu->is_debug_types);
6739 gdb_assert (abbrev_table == NULL);
6740 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
6741 return;
6742 }
6743
6744 /* This is cheap if the section is already read in. */
6745 section->read (objfile);
6746
6747 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6748
6749 abbrev_section = get_abbrev_section_for_cu (this_cu);
6750
6751 if (use_existing_cu && this_cu->cu != NULL)
6752 {
6753 cu = this_cu->cu;
6754 /* If this CU is from a DWO file we need to start over, we need to
6755 refetch the attributes from the skeleton CU.
6756 This could be optimized by retrieving those attributes from when we
6757 were here the first time: the previous comp_unit_die was stored in
6758 comp_unit_obstack. But there's no data yet that we need this
6759 optimization. */
6760 if (cu->dwo_unit != NULL)
6761 rereading_dwo_cu = 1;
6762 }
6763 else
6764 {
6765 /* If !use_existing_cu, this_cu->cu must be NULL. */
6766 gdb_assert (this_cu->cu == NULL);
6767 m_new_cu.reset (new dwarf2_cu (this_cu));
6768 cu = m_new_cu.get ();
6769 }
6770
6771 /* Get the header. */
6772 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6773 {
6774 /* We already have the header, there's no need to read it in again. */
6775 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6776 }
6777 else
6778 {
6779 if (this_cu->is_debug_types)
6780 {
6781 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6782 &cu->header, section,
6783 abbrev_section, info_ptr,
6784 rcuh_kind::TYPE);
6785
6786 /* Since per_cu is the first member of struct signatured_type,
6787 we can go from a pointer to one to a pointer to the other. */
6788 sig_type = (struct signatured_type *) this_cu;
6789 gdb_assert (sig_type->signature == cu->header.signature);
6790 gdb_assert (sig_type->type_offset_in_tu
6791 == cu->header.type_cu_offset_in_tu);
6792 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6793
6794 /* LENGTH has not been set yet for type units if we're
6795 using .gdb_index. */
6796 this_cu->length = cu->header.get_length ();
6797
6798 /* Establish the type offset that can be used to lookup the type. */
6799 sig_type->type_offset_in_section =
6800 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6801
6802 this_cu->dwarf_version = cu->header.version;
6803 }
6804 else
6805 {
6806 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6807 &cu->header, section,
6808 abbrev_section,
6809 info_ptr,
6810 rcuh_kind::COMPILE);
6811
6812 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6813 gdb_assert (this_cu->length == cu->header.get_length ());
6814 this_cu->dwarf_version = cu->header.version;
6815 }
6816 }
6817
6818 /* Skip dummy compilation units. */
6819 if (info_ptr >= begin_info_ptr + this_cu->length
6820 || peek_abbrev_code (abfd, info_ptr) == 0)
6821 {
6822 dummy_p = true;
6823 return;
6824 }
6825
6826 /* If we don't have them yet, read the abbrevs for this compilation unit.
6827 And if we need to read them now, make sure they're freed when we're
6828 done. */
6829 if (abbrev_table != NULL)
6830 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6831 else
6832 {
6833 m_abbrev_table_holder
6834 = abbrev_table::read (objfile, abbrev_section,
6835 cu->header.abbrev_sect_off);
6836 abbrev_table = m_abbrev_table_holder.get ();
6837 }
6838
6839 /* Read the top level CU/TU die. */
6840 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6841 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6842
6843 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6844 {
6845 dummy_p = true;
6846 return;
6847 }
6848
6849 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6850 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6851 table from the DWO file and pass the ownership over to us. It will be
6852 referenced from READER, so we must make sure to free it after we're done
6853 with READER.
6854
6855 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6856 DWO CU, that this test will fail (the attribute will not be present). */
6857 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6858 if (dwo_name != nullptr)
6859 {
6860 struct dwo_unit *dwo_unit;
6861 struct die_info *dwo_comp_unit_die;
6862
6863 if (comp_unit_die->has_children)
6864 {
6865 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6866 " has children (offset %s) [in module %s]"),
6867 sect_offset_str (this_cu->sect_off),
6868 bfd_get_filename (abfd));
6869 }
6870 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6871 if (dwo_unit != NULL)
6872 {
6873 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
6874 comp_unit_die, NULL,
6875 this, &info_ptr,
6876 &dwo_comp_unit_die,
6877 &m_dwo_abbrev_table) == 0)
6878 {
6879 /* Dummy die. */
6880 dummy_p = true;
6881 return;
6882 }
6883 comp_unit_die = dwo_comp_unit_die;
6884 }
6885 else
6886 {
6887 /* Yikes, we couldn't find the rest of the DIE, we only have
6888 the stub. A complaint has already been logged. There's
6889 not much more we can do except pass on the stub DIE to
6890 die_reader_func. We don't want to throw an error on bad
6891 debug info. */
6892 }
6893 }
6894 }
6895
6896 void
6897 cutu_reader::keep ()
6898 {
6899 /* Done, clean up. */
6900 gdb_assert (!dummy_p);
6901 if (m_new_cu != NULL)
6902 {
6903 struct dwarf2_per_objfile *dwarf2_per_objfile
6904 = m_this_cu->dwarf2_per_objfile;
6905 /* Link this CU into read_in_chain. */
6906 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6907 dwarf2_per_objfile->read_in_chain = m_this_cu;
6908 /* The chain owns it now. */
6909 m_new_cu.release ();
6910 }
6911 }
6912
6913 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6914 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6915 assumed to have already done the lookup to find the DWO file).
6916
6917 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
6918 THIS_CU->is_debug_types, but nothing else.
6919
6920 We fill in THIS_CU->length.
6921
6922 THIS_CU->cu is always freed when done.
6923 This is done in order to not leave THIS_CU->cu in a state where we have
6924 to care whether it refers to the "main" CU or the DWO CU.
6925
6926 When parent_cu is passed, it is used to provide a default value for
6927 str_offsets_base and addr_base from the parent. */
6928
6929 cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6930 struct dwarf2_cu *parent_cu,
6931 struct dwo_file *dwo_file)
6932 : die_reader_specs {},
6933 m_this_cu (this_cu)
6934 {
6935 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6936 struct objfile *objfile = dwarf2_per_objfile->objfile;
6937 struct dwarf2_section_info *section = this_cu->section;
6938 bfd *abfd = section->get_bfd_owner ();
6939 struct dwarf2_section_info *abbrev_section;
6940 const gdb_byte *begin_info_ptr, *info_ptr;
6941
6942 if (dwarf_die_debug)
6943 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6944 this_cu->is_debug_types ? "type" : "comp",
6945 sect_offset_str (this_cu->sect_off));
6946
6947 gdb_assert (this_cu->cu == NULL);
6948
6949 abbrev_section = (dwo_file != NULL
6950 ? &dwo_file->sections.abbrev
6951 : get_abbrev_section_for_cu (this_cu));
6952
6953 /* This is cheap if the section is already read in. */
6954 section->read (objfile);
6955
6956 m_new_cu.reset (new dwarf2_cu (this_cu));
6957
6958 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6959 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6960 &m_new_cu->header, section,
6961 abbrev_section, info_ptr,
6962 (this_cu->is_debug_types
6963 ? rcuh_kind::TYPE
6964 : rcuh_kind::COMPILE));
6965
6966 if (parent_cu != nullptr)
6967 {
6968 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6969 m_new_cu->addr_base = parent_cu->addr_base;
6970 }
6971 this_cu->length = m_new_cu->header.get_length ();
6972
6973 /* Skip dummy compilation units. */
6974 if (info_ptr >= begin_info_ptr + this_cu->length
6975 || peek_abbrev_code (abfd, info_ptr) == 0)
6976 {
6977 dummy_p = true;
6978 return;
6979 }
6980
6981 m_abbrev_table_holder
6982 = abbrev_table::read (objfile, abbrev_section,
6983 m_new_cu->header.abbrev_sect_off);
6984
6985 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
6986 m_abbrev_table_holder.get ());
6987 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6988 }
6989
6990 \f
6991 /* Type Unit Groups.
6992
6993 Type Unit Groups are a way to collapse the set of all TUs (type units) into
6994 a more manageable set. The grouping is done by DW_AT_stmt_list entry
6995 so that all types coming from the same compilation (.o file) are grouped
6996 together. A future step could be to put the types in the same symtab as
6997 the CU the types ultimately came from. */
6998
6999 static hashval_t
7000 hash_type_unit_group (const void *item)
7001 {
7002 const struct type_unit_group *tu_group
7003 = (const struct type_unit_group *) item;
7004
7005 return hash_stmt_list_entry (&tu_group->hash);
7006 }
7007
7008 static int
7009 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7010 {
7011 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7012 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7013
7014 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7015 }
7016
7017 /* Allocate a hash table for type unit groups. */
7018
7019 static htab_up
7020 allocate_type_unit_groups_table ()
7021 {
7022 return htab_up (htab_create_alloc (3,
7023 hash_type_unit_group,
7024 eq_type_unit_group,
7025 NULL, xcalloc, xfree));
7026 }
7027
7028 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7029 partial symtabs. We combine several TUs per psymtab to not let the size
7030 of any one psymtab grow too big. */
7031 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7032 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7033
7034 /* Helper routine for get_type_unit_group.
7035 Create the type_unit_group object used to hold one or more TUs. */
7036
7037 static struct type_unit_group *
7038 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7039 {
7040 struct dwarf2_per_objfile *dwarf2_per_objfile
7041 = cu->per_cu->dwarf2_per_objfile;
7042 struct objfile *objfile = dwarf2_per_objfile->objfile;
7043 struct dwarf2_per_cu_data *per_cu;
7044 struct type_unit_group *tu_group;
7045
7046 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7047 struct type_unit_group);
7048 per_cu = &tu_group->per_cu;
7049 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7050
7051 if (dwarf2_per_objfile->using_index)
7052 {
7053 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7054 struct dwarf2_per_cu_quick_data);
7055 }
7056 else
7057 {
7058 unsigned int line_offset = to_underlying (line_offset_struct);
7059 dwarf2_psymtab *pst;
7060 std::string name;
7061
7062 /* Give the symtab a useful name for debug purposes. */
7063 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7064 name = string_printf ("<type_units_%d>",
7065 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7066 else
7067 name = string_printf ("<type_units_at_0x%x>", line_offset);
7068
7069 pst = create_partial_symtab (per_cu, name.c_str ());
7070 pst->anonymous = true;
7071 }
7072
7073 tu_group->hash.dwo_unit = cu->dwo_unit;
7074 tu_group->hash.line_sect_off = line_offset_struct;
7075
7076 return tu_group;
7077 }
7078
7079 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7080 STMT_LIST is a DW_AT_stmt_list attribute. */
7081
7082 static struct type_unit_group *
7083 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7084 {
7085 struct dwarf2_per_objfile *dwarf2_per_objfile
7086 = cu->per_cu->dwarf2_per_objfile;
7087 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7088 struct type_unit_group *tu_group;
7089 void **slot;
7090 unsigned int line_offset;
7091 struct type_unit_group type_unit_group_for_lookup;
7092
7093 if (dwarf2_per_objfile->type_unit_groups == NULL)
7094 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
7095
7096 /* Do we need to create a new group, or can we use an existing one? */
7097
7098 if (stmt_list)
7099 {
7100 line_offset = DW_UNSND (stmt_list);
7101 ++tu_stats->nr_symtab_sharers;
7102 }
7103 else
7104 {
7105 /* Ugh, no stmt_list. Rare, but we have to handle it.
7106 We can do various things here like create one group per TU or
7107 spread them over multiple groups to split up the expansion work.
7108 To avoid worst case scenarios (too many groups or too large groups)
7109 we, umm, group them in bunches. */
7110 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7111 | (tu_stats->nr_stmt_less_type_units
7112 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7113 ++tu_stats->nr_stmt_less_type_units;
7114 }
7115
7116 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7117 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7118 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
7119 &type_unit_group_for_lookup, INSERT);
7120 if (*slot != NULL)
7121 {
7122 tu_group = (struct type_unit_group *) *slot;
7123 gdb_assert (tu_group != NULL);
7124 }
7125 else
7126 {
7127 sect_offset line_offset_struct = (sect_offset) line_offset;
7128 tu_group = create_type_unit_group (cu, line_offset_struct);
7129 *slot = tu_group;
7130 ++tu_stats->nr_symtabs;
7131 }
7132
7133 return tu_group;
7134 }
7135 \f
7136 /* Partial symbol tables. */
7137
7138 /* Create a psymtab named NAME and assign it to PER_CU.
7139
7140 The caller must fill in the following details:
7141 dirname, textlow, texthigh. */
7142
7143 static dwarf2_psymtab *
7144 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7145 {
7146 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
7147 dwarf2_psymtab *pst;
7148
7149 pst = new dwarf2_psymtab (name, objfile, 0);
7150
7151 pst->psymtabs_addrmap_supported = true;
7152
7153 /* This is the glue that links PST into GDB's symbol API. */
7154 pst->per_cu_data = per_cu;
7155 per_cu->v.psymtab = pst;
7156
7157 return pst;
7158 }
7159
7160 /* DIE reader function for process_psymtab_comp_unit. */
7161
7162 static void
7163 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7164 const gdb_byte *info_ptr,
7165 struct die_info *comp_unit_die,
7166 enum language pretend_language)
7167 {
7168 struct dwarf2_cu *cu = reader->cu;
7169 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
7170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7171 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7172 CORE_ADDR baseaddr;
7173 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7174 dwarf2_psymtab *pst;
7175 enum pc_bounds_kind cu_bounds_kind;
7176 const char *filename;
7177
7178 gdb_assert (! per_cu->is_debug_types);
7179
7180 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7181
7182 /* Allocate a new partial symbol table structure. */
7183 gdb::unique_xmalloc_ptr<char> debug_filename;
7184 static const char artificial[] = "<artificial>";
7185 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7186 if (filename == NULL)
7187 filename = "";
7188 else if (strcmp (filename, artificial) == 0)
7189 {
7190 debug_filename.reset (concat (artificial, "@",
7191 sect_offset_str (per_cu->sect_off),
7192 (char *) NULL));
7193 filename = debug_filename.get ();
7194 }
7195
7196 pst = create_partial_symtab (per_cu, filename);
7197
7198 /* This must be done before calling dwarf2_build_include_psymtabs. */
7199 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7200
7201 baseaddr = objfile->text_section_offset ();
7202
7203 dwarf2_find_base_address (comp_unit_die, cu);
7204
7205 /* Possibly set the default values of LOWPC and HIGHPC from
7206 `DW_AT_ranges'. */
7207 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7208 &best_highpc, cu, pst);
7209 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7210 {
7211 CORE_ADDR low
7212 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7213 - baseaddr);
7214 CORE_ADDR high
7215 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7216 - baseaddr - 1);
7217 /* Store the contiguous range if it is not empty; it can be
7218 empty for CUs with no code. */
7219 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7220 low, high, pst);
7221 }
7222
7223 /* Check if comp unit has_children.
7224 If so, read the rest of the partial symbols from this comp unit.
7225 If not, there's no more debug_info for this comp unit. */
7226 if (comp_unit_die->has_children)
7227 {
7228 struct partial_die_info *first_die;
7229 CORE_ADDR lowpc, highpc;
7230
7231 lowpc = ((CORE_ADDR) -1);
7232 highpc = ((CORE_ADDR) 0);
7233
7234 first_die = load_partial_dies (reader, info_ptr, 1);
7235
7236 scan_partial_symbols (first_die, &lowpc, &highpc,
7237 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7238
7239 /* If we didn't find a lowpc, set it to highpc to avoid
7240 complaints from `maint check'. */
7241 if (lowpc == ((CORE_ADDR) -1))
7242 lowpc = highpc;
7243
7244 /* If the compilation unit didn't have an explicit address range,
7245 then use the information extracted from its child dies. */
7246 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7247 {
7248 best_lowpc = lowpc;
7249 best_highpc = highpc;
7250 }
7251 }
7252 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7253 best_lowpc + baseaddr)
7254 - baseaddr);
7255 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7256 best_highpc + baseaddr)
7257 - baseaddr);
7258
7259 end_psymtab_common (objfile, pst);
7260
7261 if (!cu->per_cu->imported_symtabs_empty ())
7262 {
7263 int i;
7264 int len = cu->per_cu->imported_symtabs_size ();
7265
7266 /* Fill in 'dependencies' here; we fill in 'users' in a
7267 post-pass. */
7268 pst->number_of_dependencies = len;
7269 pst->dependencies
7270 = objfile->partial_symtabs->allocate_dependencies (len);
7271 for (i = 0; i < len; ++i)
7272 {
7273 pst->dependencies[i]
7274 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7275 }
7276
7277 cu->per_cu->imported_symtabs_free ();
7278 }
7279
7280 /* Get the list of files included in the current compilation unit,
7281 and build a psymtab for each of them. */
7282 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7283
7284 if (dwarf_read_debug)
7285 fprintf_unfiltered (gdb_stdlog,
7286 "Psymtab for %s unit @%s: %s - %s"
7287 ", %d global, %d static syms\n",
7288 per_cu->is_debug_types ? "type" : "comp",
7289 sect_offset_str (per_cu->sect_off),
7290 paddress (gdbarch, pst->text_low (objfile)),
7291 paddress (gdbarch, pst->text_high (objfile)),
7292 pst->n_global_syms, pst->n_static_syms);
7293 }
7294
7295 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7296 Process compilation unit THIS_CU for a psymtab. */
7297
7298 static void
7299 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
7300 bool want_partial_unit,
7301 enum language pretend_language)
7302 {
7303 /* If this compilation unit was already read in, free the
7304 cached copy in order to read it in again. This is
7305 necessary because we skipped some symbols when we first
7306 read in the compilation unit (see load_partial_dies).
7307 This problem could be avoided, but the benefit is unclear. */
7308 if (this_cu->cu != NULL)
7309 free_one_cached_comp_unit (this_cu);
7310
7311 cutu_reader reader (this_cu, NULL, 0, false);
7312
7313 switch (reader.comp_unit_die->tag)
7314 {
7315 case DW_TAG_compile_unit:
7316 this_cu->unit_type = DW_UT_compile;
7317 break;
7318 case DW_TAG_partial_unit:
7319 this_cu->unit_type = DW_UT_partial;
7320 break;
7321 default:
7322 abort ();
7323 }
7324
7325 if (reader.dummy_p)
7326 {
7327 /* Nothing. */
7328 }
7329 else if (this_cu->is_debug_types)
7330 build_type_psymtabs_reader (&reader, reader.info_ptr,
7331 reader.comp_unit_die);
7332 else if (want_partial_unit
7333 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7334 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7335 reader.comp_unit_die,
7336 pretend_language);
7337
7338 this_cu->lang = this_cu->cu->language;
7339
7340 /* Age out any secondary CUs. */
7341 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7342 }
7343
7344 /* Reader function for build_type_psymtabs. */
7345
7346 static void
7347 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7348 const gdb_byte *info_ptr,
7349 struct die_info *type_unit_die)
7350 {
7351 struct dwarf2_per_objfile *dwarf2_per_objfile
7352 = reader->cu->per_cu->dwarf2_per_objfile;
7353 struct objfile *objfile = dwarf2_per_objfile->objfile;
7354 struct dwarf2_cu *cu = reader->cu;
7355 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7356 struct signatured_type *sig_type;
7357 struct type_unit_group *tu_group;
7358 struct attribute *attr;
7359 struct partial_die_info *first_die;
7360 CORE_ADDR lowpc, highpc;
7361 dwarf2_psymtab *pst;
7362
7363 gdb_assert (per_cu->is_debug_types);
7364 sig_type = (struct signatured_type *) per_cu;
7365
7366 if (! type_unit_die->has_children)
7367 return;
7368
7369 attr = type_unit_die->attr (DW_AT_stmt_list);
7370 tu_group = get_type_unit_group (cu, attr);
7371
7372 if (tu_group->tus == nullptr)
7373 tu_group->tus = new std::vector<signatured_type *>;
7374 tu_group->tus->push_back (sig_type);
7375
7376 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7377 pst = create_partial_symtab (per_cu, "");
7378 pst->anonymous = true;
7379
7380 first_die = load_partial_dies (reader, info_ptr, 1);
7381
7382 lowpc = (CORE_ADDR) -1;
7383 highpc = (CORE_ADDR) 0;
7384 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7385
7386 end_psymtab_common (objfile, pst);
7387 }
7388
7389 /* Struct used to sort TUs by their abbreviation table offset. */
7390
7391 struct tu_abbrev_offset
7392 {
7393 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7394 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7395 {}
7396
7397 signatured_type *sig_type;
7398 sect_offset abbrev_offset;
7399 };
7400
7401 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7402
7403 static bool
7404 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7405 const struct tu_abbrev_offset &b)
7406 {
7407 return a.abbrev_offset < b.abbrev_offset;
7408 }
7409
7410 /* Efficiently read all the type units.
7411 This does the bulk of the work for build_type_psymtabs.
7412
7413 The efficiency is because we sort TUs by the abbrev table they use and
7414 only read each abbrev table once. In one program there are 200K TUs
7415 sharing 8K abbrev tables.
7416
7417 The main purpose of this function is to support building the
7418 dwarf2_per_objfile->type_unit_groups table.
7419 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7420 can collapse the search space by grouping them by stmt_list.
7421 The savings can be significant, in the same program from above the 200K TUs
7422 share 8K stmt_list tables.
7423
7424 FUNC is expected to call get_type_unit_group, which will create the
7425 struct type_unit_group if necessary and add it to
7426 dwarf2_per_objfile->type_unit_groups. */
7427
7428 static void
7429 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7430 {
7431 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7432 abbrev_table_up abbrev_table;
7433 sect_offset abbrev_offset;
7434
7435 /* It's up to the caller to not call us multiple times. */
7436 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7437
7438 if (dwarf2_per_objfile->all_type_units.empty ())
7439 return;
7440
7441 /* TUs typically share abbrev tables, and there can be way more TUs than
7442 abbrev tables. Sort by abbrev table to reduce the number of times we
7443 read each abbrev table in.
7444 Alternatives are to punt or to maintain a cache of abbrev tables.
7445 This is simpler and efficient enough for now.
7446
7447 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7448 symtab to use). Typically TUs with the same abbrev offset have the same
7449 stmt_list value too so in practice this should work well.
7450
7451 The basic algorithm here is:
7452
7453 sort TUs by abbrev table
7454 for each TU with same abbrev table:
7455 read abbrev table if first user
7456 read TU top level DIE
7457 [IWBN if DWO skeletons had DW_AT_stmt_list]
7458 call FUNC */
7459
7460 if (dwarf_read_debug)
7461 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7462
7463 /* Sort in a separate table to maintain the order of all_type_units
7464 for .gdb_index: TU indices directly index all_type_units. */
7465 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7466 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7467
7468 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7469 sorted_by_abbrev.emplace_back
7470 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7471 sig_type->per_cu.section,
7472 sig_type->per_cu.sect_off));
7473
7474 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7475 sort_tu_by_abbrev_offset);
7476
7477 abbrev_offset = (sect_offset) ~(unsigned) 0;
7478
7479 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7480 {
7481 /* Switch to the next abbrev table if necessary. */
7482 if (abbrev_table == NULL
7483 || tu.abbrev_offset != abbrev_offset)
7484 {
7485 abbrev_offset = tu.abbrev_offset;
7486 abbrev_table =
7487 abbrev_table::read (dwarf2_per_objfile->objfile,
7488 &dwarf2_per_objfile->abbrev,
7489 abbrev_offset);
7490 ++tu_stats->nr_uniq_abbrev_tables;
7491 }
7492
7493 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
7494 0, false);
7495 if (!reader.dummy_p)
7496 build_type_psymtabs_reader (&reader, reader.info_ptr,
7497 reader.comp_unit_die);
7498 }
7499 }
7500
7501 /* Print collected type unit statistics. */
7502
7503 static void
7504 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7505 {
7506 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7507
7508 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7509 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7510 dwarf2_per_objfile->all_type_units.size ());
7511 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7512 tu_stats->nr_uniq_abbrev_tables);
7513 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7514 tu_stats->nr_symtabs);
7515 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7516 tu_stats->nr_symtab_sharers);
7517 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7518 tu_stats->nr_stmt_less_type_units);
7519 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7520 tu_stats->nr_all_type_units_reallocs);
7521 }
7522
7523 /* Traversal function for build_type_psymtabs. */
7524
7525 static int
7526 build_type_psymtab_dependencies (void **slot, void *info)
7527 {
7528 struct dwarf2_per_objfile *dwarf2_per_objfile
7529 = (struct dwarf2_per_objfile *) info;
7530 struct objfile *objfile = dwarf2_per_objfile->objfile;
7531 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7532 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7533 dwarf2_psymtab *pst = per_cu->v.psymtab;
7534 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7535 int i;
7536
7537 gdb_assert (len > 0);
7538 gdb_assert (per_cu->type_unit_group_p ());
7539
7540 pst->number_of_dependencies = len;
7541 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7542 for (i = 0; i < len; ++i)
7543 {
7544 struct signatured_type *iter = tu_group->tus->at (i);
7545 gdb_assert (iter->per_cu.is_debug_types);
7546 pst->dependencies[i] = iter->per_cu.v.psymtab;
7547 iter->type_unit_group = tu_group;
7548 }
7549
7550 delete tu_group->tus;
7551 tu_group->tus = nullptr;
7552
7553 return 1;
7554 }
7555
7556 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7557 Build partial symbol tables for the .debug_types comp-units. */
7558
7559 static void
7560 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7561 {
7562 if (! create_all_type_units (dwarf2_per_objfile))
7563 return;
7564
7565 build_type_psymtabs_1 (dwarf2_per_objfile);
7566 }
7567
7568 /* Traversal function for process_skeletonless_type_unit.
7569 Read a TU in a DWO file and build partial symbols for it. */
7570
7571 static int
7572 process_skeletonless_type_unit (void **slot, void *info)
7573 {
7574 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7575 struct dwarf2_per_objfile *dwarf2_per_objfile
7576 = (struct dwarf2_per_objfile *) info;
7577 struct signatured_type find_entry, *entry;
7578
7579 /* If this TU doesn't exist in the global table, add it and read it in. */
7580
7581 if (dwarf2_per_objfile->signatured_types == NULL)
7582 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
7583
7584 find_entry.signature = dwo_unit->signature;
7585 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7586 &find_entry, INSERT);
7587 /* If we've already seen this type there's nothing to do. What's happening
7588 is we're doing our own version of comdat-folding here. */
7589 if (*slot != NULL)
7590 return 1;
7591
7592 /* This does the job that create_all_type_units would have done for
7593 this TU. */
7594 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7595 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7596 *slot = entry;
7597
7598 /* This does the job that build_type_psymtabs_1 would have done. */
7599 cutu_reader reader (&entry->per_cu, NULL, 0, false);
7600 if (!reader.dummy_p)
7601 build_type_psymtabs_reader (&reader, reader.info_ptr,
7602 reader.comp_unit_die);
7603
7604 return 1;
7605 }
7606
7607 /* Traversal function for process_skeletonless_type_units. */
7608
7609 static int
7610 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7611 {
7612 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7613
7614 if (dwo_file->tus != NULL)
7615 htab_traverse_noresize (dwo_file->tus.get (),
7616 process_skeletonless_type_unit, info);
7617
7618 return 1;
7619 }
7620
7621 /* Scan all TUs of DWO files, verifying we've processed them.
7622 This is needed in case a TU was emitted without its skeleton.
7623 Note: This can't be done until we know what all the DWO files are. */
7624
7625 static void
7626 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7627 {
7628 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7629 if (get_dwp_file (dwarf2_per_objfile) == NULL
7630 && dwarf2_per_objfile->dwo_files != NULL)
7631 {
7632 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
7633 process_dwo_file_for_skeletonless_type_units,
7634 dwarf2_per_objfile);
7635 }
7636 }
7637
7638 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7639
7640 static void
7641 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7642 {
7643 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7644 {
7645 dwarf2_psymtab *pst = per_cu->v.psymtab;
7646
7647 if (pst == NULL)
7648 continue;
7649
7650 for (int j = 0; j < pst->number_of_dependencies; ++j)
7651 {
7652 /* Set the 'user' field only if it is not already set. */
7653 if (pst->dependencies[j]->user == NULL)
7654 pst->dependencies[j]->user = pst;
7655 }
7656 }
7657 }
7658
7659 /* Build the partial symbol table by doing a quick pass through the
7660 .debug_info and .debug_abbrev sections. */
7661
7662 static void
7663 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7664 {
7665 struct objfile *objfile = dwarf2_per_objfile->objfile;
7666
7667 if (dwarf_read_debug)
7668 {
7669 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7670 objfile_name (objfile));
7671 }
7672
7673 scoped_restore restore_reading_psyms
7674 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7675 true);
7676
7677 dwarf2_per_objfile->info.read (objfile);
7678
7679 /* Any cached compilation units will be linked by the per-objfile
7680 read_in_chain. Make sure to free them when we're done. */
7681 free_cached_comp_units freer (dwarf2_per_objfile);
7682
7683 build_type_psymtabs (dwarf2_per_objfile);
7684
7685 create_all_comp_units (dwarf2_per_objfile);
7686
7687 /* Create a temporary address map on a temporary obstack. We later
7688 copy this to the final obstack. */
7689 auto_obstack temp_obstack;
7690
7691 scoped_restore save_psymtabs_addrmap
7692 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7693 addrmap_create_mutable (&temp_obstack));
7694
7695 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
7696 process_psymtab_comp_unit (per_cu, false, language_minimal);
7697
7698 /* This has to wait until we read the CUs, we need the list of DWOs. */
7699 process_skeletonless_type_units (dwarf2_per_objfile);
7700
7701 /* Now that all TUs have been processed we can fill in the dependencies. */
7702 if (dwarf2_per_objfile->type_unit_groups != NULL)
7703 {
7704 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
7705 build_type_psymtab_dependencies, dwarf2_per_objfile);
7706 }
7707
7708 if (dwarf_read_debug)
7709 print_tu_stats (dwarf2_per_objfile);
7710
7711 set_partial_user (dwarf2_per_objfile);
7712
7713 objfile->partial_symtabs->psymtabs_addrmap
7714 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7715 objfile->partial_symtabs->obstack ());
7716 /* At this point we want to keep the address map. */
7717 save_psymtabs_addrmap.release ();
7718
7719 if (dwarf_read_debug)
7720 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7721 objfile_name (objfile));
7722 }
7723
7724 /* Load the partial DIEs for a secondary CU into memory.
7725 This is also used when rereading a primary CU with load_all_dies. */
7726
7727 static void
7728 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7729 {
7730 cutu_reader reader (this_cu, NULL, 1, false);
7731
7732 if (!reader.dummy_p)
7733 {
7734 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7735 language_minimal);
7736
7737 /* Check if comp unit has_children.
7738 If so, read the rest of the partial symbols from this comp unit.
7739 If not, there's no more debug_info for this comp unit. */
7740 if (reader.comp_unit_die->has_children)
7741 load_partial_dies (&reader, reader.info_ptr, 0);
7742
7743 reader.keep ();
7744 }
7745 }
7746
7747 static void
7748 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
7749 struct dwarf2_section_info *section,
7750 struct dwarf2_section_info *abbrev_section,
7751 unsigned int is_dwz)
7752 {
7753 const gdb_byte *info_ptr;
7754 struct objfile *objfile = dwarf2_per_objfile->objfile;
7755
7756 if (dwarf_read_debug)
7757 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
7758 section->get_name (),
7759 section->get_file_name ());
7760
7761 section->read (objfile);
7762
7763 info_ptr = section->buffer;
7764
7765 while (info_ptr < section->buffer + section->size)
7766 {
7767 struct dwarf2_per_cu_data *this_cu;
7768
7769 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7770
7771 comp_unit_head cu_header;
7772 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7773 abbrev_section, info_ptr,
7774 rcuh_kind::COMPILE);
7775
7776 /* Save the compilation unit for later lookup. */
7777 if (cu_header.unit_type != DW_UT_type)
7778 {
7779 this_cu = XOBNEW (&objfile->objfile_obstack,
7780 struct dwarf2_per_cu_data);
7781 memset (this_cu, 0, sizeof (*this_cu));
7782 }
7783 else
7784 {
7785 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7786 struct signatured_type);
7787 memset (sig_type, 0, sizeof (*sig_type));
7788 sig_type->signature = cu_header.signature;
7789 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7790 this_cu = &sig_type->per_cu;
7791 }
7792 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7793 this_cu->sect_off = sect_off;
7794 this_cu->length = cu_header.length + cu_header.initial_length_size;
7795 this_cu->is_dwz = is_dwz;
7796 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7797 this_cu->section = section;
7798
7799 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
7800
7801 info_ptr = info_ptr + this_cu->length;
7802 }
7803 }
7804
7805 /* Create a list of all compilation units in OBJFILE.
7806 This is only done for -readnow and building partial symtabs. */
7807
7808 static void
7809 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7810 {
7811 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
7812 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
7813 &dwarf2_per_objfile->abbrev, 0);
7814
7815 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
7816 if (dwz != NULL)
7817 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
7818 1);
7819 }
7820
7821 /* Process all loaded DIEs for compilation unit CU, starting at
7822 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7823 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7824 DW_AT_ranges). See the comments of add_partial_subprogram on how
7825 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7826
7827 static void
7828 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7829 CORE_ADDR *highpc, int set_addrmap,
7830 struct dwarf2_cu *cu)
7831 {
7832 struct partial_die_info *pdi;
7833
7834 /* Now, march along the PDI's, descending into ones which have
7835 interesting children but skipping the children of the other ones,
7836 until we reach the end of the compilation unit. */
7837
7838 pdi = first_die;
7839
7840 while (pdi != NULL)
7841 {
7842 pdi->fixup (cu);
7843
7844 /* Anonymous namespaces or modules have no name but have interesting
7845 children, so we need to look at them. Ditto for anonymous
7846 enums. */
7847
7848 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
7849 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7850 || pdi->tag == DW_TAG_imported_unit
7851 || pdi->tag == DW_TAG_inlined_subroutine)
7852 {
7853 switch (pdi->tag)
7854 {
7855 case DW_TAG_subprogram:
7856 case DW_TAG_inlined_subroutine:
7857 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7858 break;
7859 case DW_TAG_constant:
7860 case DW_TAG_variable:
7861 case DW_TAG_typedef:
7862 case DW_TAG_union_type:
7863 if (!pdi->is_declaration)
7864 {
7865 add_partial_symbol (pdi, cu);
7866 }
7867 break;
7868 case DW_TAG_class_type:
7869 case DW_TAG_interface_type:
7870 case DW_TAG_structure_type:
7871 if (!pdi->is_declaration)
7872 {
7873 add_partial_symbol (pdi, cu);
7874 }
7875 if ((cu->language == language_rust
7876 || cu->language == language_cplus) && pdi->has_children)
7877 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7878 set_addrmap, cu);
7879 break;
7880 case DW_TAG_enumeration_type:
7881 if (!pdi->is_declaration)
7882 add_partial_enumeration (pdi, cu);
7883 break;
7884 case DW_TAG_base_type:
7885 case DW_TAG_subrange_type:
7886 /* File scope base type definitions are added to the partial
7887 symbol table. */
7888 add_partial_symbol (pdi, cu);
7889 break;
7890 case DW_TAG_namespace:
7891 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7892 break;
7893 case DW_TAG_module:
7894 if (!pdi->is_declaration)
7895 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7896 break;
7897 case DW_TAG_imported_unit:
7898 {
7899 struct dwarf2_per_cu_data *per_cu;
7900
7901 /* For now we don't handle imported units in type units. */
7902 if (cu->per_cu->is_debug_types)
7903 {
7904 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7905 " supported in type units [in module %s]"),
7906 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7907 }
7908
7909 per_cu = dwarf2_find_containing_comp_unit
7910 (pdi->d.sect_off, pdi->is_dwz,
7911 cu->per_cu->dwarf2_per_objfile);
7912
7913 /* Go read the partial unit, if needed. */
7914 if (per_cu->v.psymtab == NULL)
7915 process_psymtab_comp_unit (per_cu, true, cu->language);
7916
7917 cu->per_cu->imported_symtabs_push (per_cu);
7918 }
7919 break;
7920 case DW_TAG_imported_declaration:
7921 add_partial_symbol (pdi, cu);
7922 break;
7923 default:
7924 break;
7925 }
7926 }
7927
7928 /* If the die has a sibling, skip to the sibling. */
7929
7930 pdi = pdi->die_sibling;
7931 }
7932 }
7933
7934 /* Functions used to compute the fully scoped name of a partial DIE.
7935
7936 Normally, this is simple. For C++, the parent DIE's fully scoped
7937 name is concatenated with "::" and the partial DIE's name.
7938 Enumerators are an exception; they use the scope of their parent
7939 enumeration type, i.e. the name of the enumeration type is not
7940 prepended to the enumerator.
7941
7942 There are two complexities. One is DW_AT_specification; in this
7943 case "parent" means the parent of the target of the specification,
7944 instead of the direct parent of the DIE. The other is compilers
7945 which do not emit DW_TAG_namespace; in this case we try to guess
7946 the fully qualified name of structure types from their members'
7947 linkage names. This must be done using the DIE's children rather
7948 than the children of any DW_AT_specification target. We only need
7949 to do this for structures at the top level, i.e. if the target of
7950 any DW_AT_specification (if any; otherwise the DIE itself) does not
7951 have a parent. */
7952
7953 /* Compute the scope prefix associated with PDI's parent, in
7954 compilation unit CU. The result will be allocated on CU's
7955 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7956 field. NULL is returned if no prefix is necessary. */
7957 static const char *
7958 partial_die_parent_scope (struct partial_die_info *pdi,
7959 struct dwarf2_cu *cu)
7960 {
7961 const char *grandparent_scope;
7962 struct partial_die_info *parent, *real_pdi;
7963
7964 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7965 then this means the parent of the specification DIE. */
7966
7967 real_pdi = pdi;
7968 while (real_pdi->has_specification)
7969 {
7970 auto res = find_partial_die (real_pdi->spec_offset,
7971 real_pdi->spec_is_dwz, cu);
7972 real_pdi = res.pdi;
7973 cu = res.cu;
7974 }
7975
7976 parent = real_pdi->die_parent;
7977 if (parent == NULL)
7978 return NULL;
7979
7980 if (parent->scope_set)
7981 return parent->scope;
7982
7983 parent->fixup (cu);
7984
7985 grandparent_scope = partial_die_parent_scope (parent, cu);
7986
7987 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
7988 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
7989 Work around this problem here. */
7990 if (cu->language == language_cplus
7991 && parent->tag == DW_TAG_namespace
7992 && strcmp (parent->name, "::") == 0
7993 && grandparent_scope == NULL)
7994 {
7995 parent->scope = NULL;
7996 parent->scope_set = 1;
7997 return NULL;
7998 }
7999
8000 /* Nested subroutines in Fortran get a prefix. */
8001 if (pdi->tag == DW_TAG_enumerator)
8002 /* Enumerators should not get the name of the enumeration as a prefix. */
8003 parent->scope = grandparent_scope;
8004 else if (parent->tag == DW_TAG_namespace
8005 || parent->tag == DW_TAG_module
8006 || parent->tag == DW_TAG_structure_type
8007 || parent->tag == DW_TAG_class_type
8008 || parent->tag == DW_TAG_interface_type
8009 || parent->tag == DW_TAG_union_type
8010 || parent->tag == DW_TAG_enumeration_type
8011 || (cu->language == language_fortran
8012 && parent->tag == DW_TAG_subprogram
8013 && pdi->tag == DW_TAG_subprogram))
8014 {
8015 if (grandparent_scope == NULL)
8016 parent->scope = parent->name;
8017 else
8018 parent->scope = typename_concat (&cu->comp_unit_obstack,
8019 grandparent_scope,
8020 parent->name, 0, cu);
8021 }
8022 else
8023 {
8024 /* FIXME drow/2004-04-01: What should we be doing with
8025 function-local names? For partial symbols, we should probably be
8026 ignoring them. */
8027 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8028 dwarf_tag_name (parent->tag),
8029 sect_offset_str (pdi->sect_off));
8030 parent->scope = grandparent_scope;
8031 }
8032
8033 parent->scope_set = 1;
8034 return parent->scope;
8035 }
8036
8037 /* Return the fully scoped name associated with PDI, from compilation unit
8038 CU. The result will be allocated with malloc. */
8039
8040 static gdb::unique_xmalloc_ptr<char>
8041 partial_die_full_name (struct partial_die_info *pdi,
8042 struct dwarf2_cu *cu)
8043 {
8044 const char *parent_scope;
8045
8046 /* If this is a template instantiation, we can not work out the
8047 template arguments from partial DIEs. So, unfortunately, we have
8048 to go through the full DIEs. At least any work we do building
8049 types here will be reused if full symbols are loaded later. */
8050 if (pdi->has_template_arguments)
8051 {
8052 pdi->fixup (cu);
8053
8054 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8055 {
8056 struct die_info *die;
8057 struct attribute attr;
8058 struct dwarf2_cu *ref_cu = cu;
8059
8060 /* DW_FORM_ref_addr is using section offset. */
8061 attr.name = (enum dwarf_attribute) 0;
8062 attr.form = DW_FORM_ref_addr;
8063 attr.u.unsnd = to_underlying (pdi->sect_off);
8064 die = follow_die_ref (NULL, &attr, &ref_cu);
8065
8066 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8067 }
8068 }
8069
8070 parent_scope = partial_die_parent_scope (pdi, cu);
8071 if (parent_scope == NULL)
8072 return NULL;
8073 else
8074 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8075 pdi->name, 0, cu));
8076 }
8077
8078 static void
8079 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8080 {
8081 struct dwarf2_per_objfile *dwarf2_per_objfile
8082 = cu->per_cu->dwarf2_per_objfile;
8083 struct objfile *objfile = dwarf2_per_objfile->objfile;
8084 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8085 CORE_ADDR addr = 0;
8086 const char *actual_name = NULL;
8087 CORE_ADDR baseaddr;
8088
8089 baseaddr = objfile->text_section_offset ();
8090
8091 gdb::unique_xmalloc_ptr<char> built_actual_name
8092 = partial_die_full_name (pdi, cu);
8093 if (built_actual_name != NULL)
8094 actual_name = built_actual_name.get ();
8095
8096 if (actual_name == NULL)
8097 actual_name = pdi->name;
8098
8099 switch (pdi->tag)
8100 {
8101 case DW_TAG_inlined_subroutine:
8102 case DW_TAG_subprogram:
8103 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8104 - baseaddr);
8105 if (pdi->is_external
8106 || cu->language == language_ada
8107 || (cu->language == language_fortran
8108 && pdi->die_parent != NULL
8109 && pdi->die_parent->tag == DW_TAG_subprogram))
8110 {
8111 /* Normally, only "external" DIEs are part of the global scope.
8112 But in Ada and Fortran, we want to be able to access nested
8113 procedures globally. So all Ada and Fortran subprograms are
8114 stored in the global scope. */
8115 add_psymbol_to_list (actual_name,
8116 built_actual_name != NULL,
8117 VAR_DOMAIN, LOC_BLOCK,
8118 SECT_OFF_TEXT (objfile),
8119 psymbol_placement::GLOBAL,
8120 addr,
8121 cu->language, objfile);
8122 }
8123 else
8124 {
8125 add_psymbol_to_list (actual_name,
8126 built_actual_name != NULL,
8127 VAR_DOMAIN, LOC_BLOCK,
8128 SECT_OFF_TEXT (objfile),
8129 psymbol_placement::STATIC,
8130 addr, cu->language, objfile);
8131 }
8132
8133 if (pdi->main_subprogram && actual_name != NULL)
8134 set_objfile_main_name (objfile, actual_name, cu->language);
8135 break;
8136 case DW_TAG_constant:
8137 add_psymbol_to_list (actual_name,
8138 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8139 -1, (pdi->is_external
8140 ? psymbol_placement::GLOBAL
8141 : psymbol_placement::STATIC),
8142 0, cu->language, objfile);
8143 break;
8144 case DW_TAG_variable:
8145 if (pdi->d.locdesc)
8146 addr = decode_locdesc (pdi->d.locdesc, cu);
8147
8148 if (pdi->d.locdesc
8149 && addr == 0
8150 && !dwarf2_per_objfile->has_section_at_zero)
8151 {
8152 /* A global or static variable may also have been stripped
8153 out by the linker if unused, in which case its address
8154 will be nullified; do not add such variables into partial
8155 symbol table then. */
8156 }
8157 else if (pdi->is_external)
8158 {
8159 /* Global Variable.
8160 Don't enter into the minimal symbol tables as there is
8161 a minimal symbol table entry from the ELF symbols already.
8162 Enter into partial symbol table if it has a location
8163 descriptor or a type.
8164 If the location descriptor is missing, new_symbol will create
8165 a LOC_UNRESOLVED symbol, the address of the variable will then
8166 be determined from the minimal symbol table whenever the variable
8167 is referenced.
8168 The address for the partial symbol table entry is not
8169 used by GDB, but it comes in handy for debugging partial symbol
8170 table building. */
8171
8172 if (pdi->d.locdesc || pdi->has_type)
8173 add_psymbol_to_list (actual_name,
8174 built_actual_name != NULL,
8175 VAR_DOMAIN, LOC_STATIC,
8176 SECT_OFF_TEXT (objfile),
8177 psymbol_placement::GLOBAL,
8178 addr, cu->language, objfile);
8179 }
8180 else
8181 {
8182 int has_loc = pdi->d.locdesc != NULL;
8183
8184 /* Static Variable. Skip symbols whose value we cannot know (those
8185 without location descriptors or constant values). */
8186 if (!has_loc && !pdi->has_const_value)
8187 return;
8188
8189 add_psymbol_to_list (actual_name,
8190 built_actual_name != NULL,
8191 VAR_DOMAIN, LOC_STATIC,
8192 SECT_OFF_TEXT (objfile),
8193 psymbol_placement::STATIC,
8194 has_loc ? addr : 0,
8195 cu->language, objfile);
8196 }
8197 break;
8198 case DW_TAG_typedef:
8199 case DW_TAG_base_type:
8200 case DW_TAG_subrange_type:
8201 add_psymbol_to_list (actual_name,
8202 built_actual_name != NULL,
8203 VAR_DOMAIN, LOC_TYPEDEF, -1,
8204 psymbol_placement::STATIC,
8205 0, cu->language, objfile);
8206 break;
8207 case DW_TAG_imported_declaration:
8208 case DW_TAG_namespace:
8209 add_psymbol_to_list (actual_name,
8210 built_actual_name != NULL,
8211 VAR_DOMAIN, LOC_TYPEDEF, -1,
8212 psymbol_placement::GLOBAL,
8213 0, cu->language, objfile);
8214 break;
8215 case DW_TAG_module:
8216 /* With Fortran 77 there might be a "BLOCK DATA" module
8217 available without any name. If so, we skip the module as it
8218 doesn't bring any value. */
8219 if (actual_name != nullptr)
8220 add_psymbol_to_list (actual_name,
8221 built_actual_name != NULL,
8222 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8223 psymbol_placement::GLOBAL,
8224 0, cu->language, objfile);
8225 break;
8226 case DW_TAG_class_type:
8227 case DW_TAG_interface_type:
8228 case DW_TAG_structure_type:
8229 case DW_TAG_union_type:
8230 case DW_TAG_enumeration_type:
8231 /* Skip external references. The DWARF standard says in the section
8232 about "Structure, Union, and Class Type Entries": "An incomplete
8233 structure, union or class type is represented by a structure,
8234 union or class entry that does not have a byte size attribute
8235 and that has a DW_AT_declaration attribute." */
8236 if (!pdi->has_byte_size && pdi->is_declaration)
8237 return;
8238
8239 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8240 static vs. global. */
8241 add_psymbol_to_list (actual_name,
8242 built_actual_name != NULL,
8243 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
8244 cu->language == language_cplus
8245 ? psymbol_placement::GLOBAL
8246 : psymbol_placement::STATIC,
8247 0, cu->language, objfile);
8248
8249 break;
8250 case DW_TAG_enumerator:
8251 add_psymbol_to_list (actual_name,
8252 built_actual_name != NULL,
8253 VAR_DOMAIN, LOC_CONST, -1,
8254 cu->language == language_cplus
8255 ? psymbol_placement::GLOBAL
8256 : psymbol_placement::STATIC,
8257 0, cu->language, objfile);
8258 break;
8259 default:
8260 break;
8261 }
8262 }
8263
8264 /* Read a partial die corresponding to a namespace; also, add a symbol
8265 corresponding to that namespace to the symbol table. NAMESPACE is
8266 the name of the enclosing namespace. */
8267
8268 static void
8269 add_partial_namespace (struct partial_die_info *pdi,
8270 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8271 int set_addrmap, struct dwarf2_cu *cu)
8272 {
8273 /* Add a symbol for the namespace. */
8274
8275 add_partial_symbol (pdi, cu);
8276
8277 /* Now scan partial symbols in that namespace. */
8278
8279 if (pdi->has_children)
8280 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8281 }
8282
8283 /* Read a partial die corresponding to a Fortran module. */
8284
8285 static void
8286 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8287 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8288 {
8289 /* Add a symbol for the namespace. */
8290
8291 add_partial_symbol (pdi, cu);
8292
8293 /* Now scan partial symbols in that module. */
8294
8295 if (pdi->has_children)
8296 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8297 }
8298
8299 /* Read a partial die corresponding to a subprogram or an inlined
8300 subprogram and create a partial symbol for that subprogram.
8301 When the CU language allows it, this routine also defines a partial
8302 symbol for each nested subprogram that this subprogram contains.
8303 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8304 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8305
8306 PDI may also be a lexical block, in which case we simply search
8307 recursively for subprograms defined inside that lexical block.
8308 Again, this is only performed when the CU language allows this
8309 type of definitions. */
8310
8311 static void
8312 add_partial_subprogram (struct partial_die_info *pdi,
8313 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8314 int set_addrmap, struct dwarf2_cu *cu)
8315 {
8316 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8317 {
8318 if (pdi->has_pc_info)
8319 {
8320 if (pdi->lowpc < *lowpc)
8321 *lowpc = pdi->lowpc;
8322 if (pdi->highpc > *highpc)
8323 *highpc = pdi->highpc;
8324 if (set_addrmap)
8325 {
8326 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
8327 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8328 CORE_ADDR baseaddr;
8329 CORE_ADDR this_highpc;
8330 CORE_ADDR this_lowpc;
8331
8332 baseaddr = objfile->text_section_offset ();
8333 this_lowpc
8334 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8335 pdi->lowpc + baseaddr)
8336 - baseaddr);
8337 this_highpc
8338 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8339 pdi->highpc + baseaddr)
8340 - baseaddr);
8341 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8342 this_lowpc, this_highpc - 1,
8343 cu->per_cu->v.psymtab);
8344 }
8345 }
8346
8347 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8348 {
8349 if (!pdi->is_declaration)
8350 /* Ignore subprogram DIEs that do not have a name, they are
8351 illegal. Do not emit a complaint at this point, we will
8352 do so when we convert this psymtab into a symtab. */
8353 if (pdi->name)
8354 add_partial_symbol (pdi, cu);
8355 }
8356 }
8357
8358 if (! pdi->has_children)
8359 return;
8360
8361 if (cu->language == language_ada || cu->language == language_fortran)
8362 {
8363 pdi = pdi->die_child;
8364 while (pdi != NULL)
8365 {
8366 pdi->fixup (cu);
8367 if (pdi->tag == DW_TAG_subprogram
8368 || pdi->tag == DW_TAG_inlined_subroutine
8369 || pdi->tag == DW_TAG_lexical_block)
8370 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8371 pdi = pdi->die_sibling;
8372 }
8373 }
8374 }
8375
8376 /* Read a partial die corresponding to an enumeration type. */
8377
8378 static void
8379 add_partial_enumeration (struct partial_die_info *enum_pdi,
8380 struct dwarf2_cu *cu)
8381 {
8382 struct partial_die_info *pdi;
8383
8384 if (enum_pdi->name != NULL)
8385 add_partial_symbol (enum_pdi, cu);
8386
8387 pdi = enum_pdi->die_child;
8388 while (pdi)
8389 {
8390 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8391 complaint (_("malformed enumerator DIE ignored"));
8392 else
8393 add_partial_symbol (pdi, cu);
8394 pdi = pdi->die_sibling;
8395 }
8396 }
8397
8398 /* Return the initial uleb128 in the die at INFO_PTR. */
8399
8400 static unsigned int
8401 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8402 {
8403 unsigned int bytes_read;
8404
8405 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8406 }
8407
8408 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8409 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8410
8411 Return the corresponding abbrev, or NULL if the number is zero (indicating
8412 an empty DIE). In either case *BYTES_READ will be set to the length of
8413 the initial number. */
8414
8415 static struct abbrev_info *
8416 peek_die_abbrev (const die_reader_specs &reader,
8417 const gdb_byte *info_ptr, unsigned int *bytes_read)
8418 {
8419 dwarf2_cu *cu = reader.cu;
8420 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
8421 unsigned int abbrev_number
8422 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8423
8424 if (abbrev_number == 0)
8425 return NULL;
8426
8427 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8428 if (!abbrev)
8429 {
8430 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8431 " at offset %s [in module %s]"),
8432 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8433 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8434 }
8435
8436 return abbrev;
8437 }
8438
8439 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8440 Returns a pointer to the end of a series of DIEs, terminated by an empty
8441 DIE. Any children of the skipped DIEs will also be skipped. */
8442
8443 static const gdb_byte *
8444 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8445 {
8446 while (1)
8447 {
8448 unsigned int bytes_read;
8449 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8450
8451 if (abbrev == NULL)
8452 return info_ptr + bytes_read;
8453 else
8454 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8455 }
8456 }
8457
8458 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8459 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8460 abbrev corresponding to that skipped uleb128 should be passed in
8461 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8462 children. */
8463
8464 static const gdb_byte *
8465 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8466 struct abbrev_info *abbrev)
8467 {
8468 unsigned int bytes_read;
8469 struct attribute attr;
8470 bfd *abfd = reader->abfd;
8471 struct dwarf2_cu *cu = reader->cu;
8472 const gdb_byte *buffer = reader->buffer;
8473 const gdb_byte *buffer_end = reader->buffer_end;
8474 unsigned int form, i;
8475
8476 for (i = 0; i < abbrev->num_attrs; i++)
8477 {
8478 /* The only abbrev we care about is DW_AT_sibling. */
8479 if (abbrev->attrs[i].name == DW_AT_sibling)
8480 {
8481 bool ignored;
8482 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8483 &ignored);
8484 if (attr.form == DW_FORM_ref_addr)
8485 complaint (_("ignoring absolute DW_AT_sibling"));
8486 else
8487 {
8488 sect_offset off = dwarf2_get_ref_die_offset (&attr);
8489 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8490
8491 if (sibling_ptr < info_ptr)
8492 complaint (_("DW_AT_sibling points backwards"));
8493 else if (sibling_ptr > reader->buffer_end)
8494 reader->die_section->overflow_complaint ();
8495 else
8496 return sibling_ptr;
8497 }
8498 }
8499
8500 /* If it isn't DW_AT_sibling, skip this attribute. */
8501 form = abbrev->attrs[i].form;
8502 skip_attribute:
8503 switch (form)
8504 {
8505 case DW_FORM_ref_addr:
8506 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8507 and later it is offset sized. */
8508 if (cu->header.version == 2)
8509 info_ptr += cu->header.addr_size;
8510 else
8511 info_ptr += cu->header.offset_size;
8512 break;
8513 case DW_FORM_GNU_ref_alt:
8514 info_ptr += cu->header.offset_size;
8515 break;
8516 case DW_FORM_addr:
8517 info_ptr += cu->header.addr_size;
8518 break;
8519 case DW_FORM_data1:
8520 case DW_FORM_ref1:
8521 case DW_FORM_flag:
8522 case DW_FORM_strx1:
8523 info_ptr += 1;
8524 break;
8525 case DW_FORM_flag_present:
8526 case DW_FORM_implicit_const:
8527 break;
8528 case DW_FORM_data2:
8529 case DW_FORM_ref2:
8530 case DW_FORM_strx2:
8531 info_ptr += 2;
8532 break;
8533 case DW_FORM_strx3:
8534 info_ptr += 3;
8535 break;
8536 case DW_FORM_data4:
8537 case DW_FORM_ref4:
8538 case DW_FORM_strx4:
8539 info_ptr += 4;
8540 break;
8541 case DW_FORM_data8:
8542 case DW_FORM_ref8:
8543 case DW_FORM_ref_sig8:
8544 info_ptr += 8;
8545 break;
8546 case DW_FORM_data16:
8547 info_ptr += 16;
8548 break;
8549 case DW_FORM_string:
8550 read_direct_string (abfd, info_ptr, &bytes_read);
8551 info_ptr += bytes_read;
8552 break;
8553 case DW_FORM_sec_offset:
8554 case DW_FORM_strp:
8555 case DW_FORM_GNU_strp_alt:
8556 info_ptr += cu->header.offset_size;
8557 break;
8558 case DW_FORM_exprloc:
8559 case DW_FORM_block:
8560 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8561 info_ptr += bytes_read;
8562 break;
8563 case DW_FORM_block1:
8564 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8565 break;
8566 case DW_FORM_block2:
8567 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8568 break;
8569 case DW_FORM_block4:
8570 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8571 break;
8572 case DW_FORM_addrx:
8573 case DW_FORM_strx:
8574 case DW_FORM_sdata:
8575 case DW_FORM_udata:
8576 case DW_FORM_ref_udata:
8577 case DW_FORM_GNU_addr_index:
8578 case DW_FORM_GNU_str_index:
8579 case DW_FORM_rnglistx:
8580 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8581 break;
8582 case DW_FORM_indirect:
8583 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8584 info_ptr += bytes_read;
8585 /* We need to continue parsing from here, so just go back to
8586 the top. */
8587 goto skip_attribute;
8588
8589 default:
8590 error (_("Dwarf Error: Cannot handle %s "
8591 "in DWARF reader [in module %s]"),
8592 dwarf_form_name (form),
8593 bfd_get_filename (abfd));
8594 }
8595 }
8596
8597 if (abbrev->has_children)
8598 return skip_children (reader, info_ptr);
8599 else
8600 return info_ptr;
8601 }
8602
8603 /* Locate ORIG_PDI's sibling.
8604 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8605
8606 static const gdb_byte *
8607 locate_pdi_sibling (const struct die_reader_specs *reader,
8608 struct partial_die_info *orig_pdi,
8609 const gdb_byte *info_ptr)
8610 {
8611 /* Do we know the sibling already? */
8612
8613 if (orig_pdi->sibling)
8614 return orig_pdi->sibling;
8615
8616 /* Are there any children to deal with? */
8617
8618 if (!orig_pdi->has_children)
8619 return info_ptr;
8620
8621 /* Skip the children the long way. */
8622
8623 return skip_children (reader, info_ptr);
8624 }
8625
8626 /* Expand this partial symbol table into a full symbol table. SELF is
8627 not NULL. */
8628
8629 void
8630 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8631 {
8632 struct dwarf2_per_objfile *dwarf2_per_objfile
8633 = get_dwarf2_per_objfile (objfile);
8634
8635 gdb_assert (!readin);
8636 /* If this psymtab is constructed from a debug-only objfile, the
8637 has_section_at_zero flag will not necessarily be correct. We
8638 can get the correct value for this flag by looking at the data
8639 associated with the (presumably stripped) associated objfile. */
8640 if (objfile->separate_debug_objfile_backlink)
8641 {
8642 struct dwarf2_per_objfile *dpo_backlink
8643 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8644
8645 dwarf2_per_objfile->has_section_at_zero
8646 = dpo_backlink->has_section_at_zero;
8647 }
8648
8649 expand_psymtab (objfile);
8650
8651 process_cu_includes (dwarf2_per_objfile);
8652 }
8653 \f
8654 /* Reading in full CUs. */
8655
8656 /* Add PER_CU to the queue. */
8657
8658 static void
8659 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8660 enum language pretend_language)
8661 {
8662 per_cu->queued = 1;
8663 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
8664 }
8665
8666 /* If PER_CU is not yet queued, add it to the queue.
8667 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8668 dependency.
8669 The result is non-zero if PER_CU was queued, otherwise the result is zero
8670 meaning either PER_CU is already queued or it is already loaded.
8671
8672 N.B. There is an invariant here that if a CU is queued then it is loaded.
8673 The caller is required to load PER_CU if we return non-zero. */
8674
8675 static int
8676 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8677 struct dwarf2_per_cu_data *per_cu,
8678 enum language pretend_language)
8679 {
8680 /* We may arrive here during partial symbol reading, if we need full
8681 DIEs to process an unusual case (e.g. template arguments). Do
8682 not queue PER_CU, just tell our caller to load its DIEs. */
8683 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
8684 {
8685 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8686 return 1;
8687 return 0;
8688 }
8689
8690 /* Mark the dependence relation so that we don't flush PER_CU
8691 too early. */
8692 if (dependent_cu != NULL)
8693 dwarf2_add_dependence (dependent_cu, per_cu);
8694
8695 /* If it's already on the queue, we have nothing to do. */
8696 if (per_cu->queued)
8697 return 0;
8698
8699 /* If the compilation unit is already loaded, just mark it as
8700 used. */
8701 if (per_cu->cu != NULL)
8702 {
8703 per_cu->cu->last_used = 0;
8704 return 0;
8705 }
8706
8707 /* Add it to the queue. */
8708 queue_comp_unit (per_cu, pretend_language);
8709
8710 return 1;
8711 }
8712
8713 /* Process the queue. */
8714
8715 static void
8716 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8717 {
8718 if (dwarf_read_debug)
8719 {
8720 fprintf_unfiltered (gdb_stdlog,
8721 "Expanding one or more symtabs of objfile %s ...\n",
8722 objfile_name (dwarf2_per_objfile->objfile));
8723 }
8724
8725 /* The queue starts out with one item, but following a DIE reference
8726 may load a new CU, adding it to the end of the queue. */
8727 while (!dwarf2_per_objfile->queue.empty ())
8728 {
8729 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8730
8731 if ((dwarf2_per_objfile->using_index
8732 ? !item.per_cu->v.quick->compunit_symtab
8733 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
8734 /* Skip dummy CUs. */
8735 && item.per_cu->cu != NULL)
8736 {
8737 struct dwarf2_per_cu_data *per_cu = item.per_cu;
8738 unsigned int debug_print_threshold;
8739 char buf[100];
8740
8741 if (per_cu->is_debug_types)
8742 {
8743 struct signatured_type *sig_type =
8744 (struct signatured_type *) per_cu;
8745
8746 sprintf (buf, "TU %s at offset %s",
8747 hex_string (sig_type->signature),
8748 sect_offset_str (per_cu->sect_off));
8749 /* There can be 100s of TUs.
8750 Only print them in verbose mode. */
8751 debug_print_threshold = 2;
8752 }
8753 else
8754 {
8755 sprintf (buf, "CU at offset %s",
8756 sect_offset_str (per_cu->sect_off));
8757 debug_print_threshold = 1;
8758 }
8759
8760 if (dwarf_read_debug >= debug_print_threshold)
8761 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
8762
8763 if (per_cu->is_debug_types)
8764 process_full_type_unit (per_cu, item.pretend_language);
8765 else
8766 process_full_comp_unit (per_cu, item.pretend_language);
8767
8768 if (dwarf_read_debug >= debug_print_threshold)
8769 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
8770 }
8771
8772 item.per_cu->queued = 0;
8773 dwarf2_per_objfile->queue.pop ();
8774 }
8775
8776 if (dwarf_read_debug)
8777 {
8778 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
8779 objfile_name (dwarf2_per_objfile->objfile));
8780 }
8781 }
8782
8783 /* Read in full symbols for PST, and anything it depends on. */
8784
8785 void
8786 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8787 {
8788 if (readin)
8789 return;
8790
8791 read_dependencies (objfile);
8792
8793 dw2_do_instantiate_symtab (per_cu_data, false);
8794 gdb_assert (get_compunit_symtab () != nullptr);
8795 }
8796
8797 /* Trivial hash function for die_info: the hash value of a DIE
8798 is its offset in .debug_info for this objfile. */
8799
8800 static hashval_t
8801 die_hash (const void *item)
8802 {
8803 const struct die_info *die = (const struct die_info *) item;
8804
8805 return to_underlying (die->sect_off);
8806 }
8807
8808 /* Trivial comparison function for die_info structures: two DIEs
8809 are equal if they have the same offset. */
8810
8811 static int
8812 die_eq (const void *item_lhs, const void *item_rhs)
8813 {
8814 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8815 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8816
8817 return die_lhs->sect_off == die_rhs->sect_off;
8818 }
8819
8820 /* Load the DIEs associated with PER_CU into memory. */
8821
8822 static void
8823 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8824 bool skip_partial,
8825 enum language pretend_language)
8826 {
8827 gdb_assert (! this_cu->is_debug_types);
8828
8829 cutu_reader reader (this_cu, NULL, 1, skip_partial);
8830 if (reader.dummy_p)
8831 return;
8832
8833 struct dwarf2_cu *cu = reader.cu;
8834 const gdb_byte *info_ptr = reader.info_ptr;
8835
8836 gdb_assert (cu->die_hash == NULL);
8837 cu->die_hash =
8838 htab_create_alloc_ex (cu->header.length / 12,
8839 die_hash,
8840 die_eq,
8841 NULL,
8842 &cu->comp_unit_obstack,
8843 hashtab_obstack_allocate,
8844 dummy_obstack_deallocate);
8845
8846 if (reader.comp_unit_die->has_children)
8847 reader.comp_unit_die->child
8848 = read_die_and_siblings (&reader, reader.info_ptr,
8849 &info_ptr, reader.comp_unit_die);
8850 cu->dies = reader.comp_unit_die;
8851 /* comp_unit_die is not stored in die_hash, no need. */
8852
8853 /* We try not to read any attributes in this function, because not
8854 all CUs needed for references have been loaded yet, and symbol
8855 table processing isn't initialized. But we have to set the CU language,
8856 or we won't be able to build types correctly.
8857 Similarly, if we do not read the producer, we can not apply
8858 producer-specific interpretation. */
8859 prepare_one_comp_unit (cu, cu->dies, pretend_language);
8860
8861 reader.keep ();
8862 }
8863
8864 /* Add a DIE to the delayed physname list. */
8865
8866 static void
8867 add_to_method_list (struct type *type, int fnfield_index, int index,
8868 const char *name, struct die_info *die,
8869 struct dwarf2_cu *cu)
8870 {
8871 struct delayed_method_info mi;
8872 mi.type = type;
8873 mi.fnfield_index = fnfield_index;
8874 mi.index = index;
8875 mi.name = name;
8876 mi.die = die;
8877 cu->method_list.push_back (mi);
8878 }
8879
8880 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8881 "const" / "volatile". If so, decrements LEN by the length of the
8882 modifier and return true. Otherwise return false. */
8883
8884 template<size_t N>
8885 static bool
8886 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8887 {
8888 size_t mod_len = sizeof (mod) - 1;
8889 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8890 {
8891 len -= mod_len;
8892 return true;
8893 }
8894 return false;
8895 }
8896
8897 /* Compute the physnames of any methods on the CU's method list.
8898
8899 The computation of method physnames is delayed in order to avoid the
8900 (bad) condition that one of the method's formal parameters is of an as yet
8901 incomplete type. */
8902
8903 static void
8904 compute_delayed_physnames (struct dwarf2_cu *cu)
8905 {
8906 /* Only C++ delays computing physnames. */
8907 if (cu->method_list.empty ())
8908 return;
8909 gdb_assert (cu->language == language_cplus);
8910
8911 for (const delayed_method_info &mi : cu->method_list)
8912 {
8913 const char *physname;
8914 struct fn_fieldlist *fn_flp
8915 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8916 physname = dwarf2_physname (mi.name, mi.die, cu);
8917 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
8918 = physname ? physname : "";
8919
8920 /* Since there's no tag to indicate whether a method is a
8921 const/volatile overload, extract that information out of the
8922 demangled name. */
8923 if (physname != NULL)
8924 {
8925 size_t len = strlen (physname);
8926
8927 while (1)
8928 {
8929 if (physname[len] == ')') /* shortcut */
8930 break;
8931 else if (check_modifier (physname, len, " const"))
8932 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
8933 else if (check_modifier (physname, len, " volatile"))
8934 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
8935 else
8936 break;
8937 }
8938 }
8939 }
8940
8941 /* The list is no longer needed. */
8942 cu->method_list.clear ();
8943 }
8944
8945 /* Go objects should be embedded in a DW_TAG_module DIE,
8946 and it's not clear if/how imported objects will appear.
8947 To keep Go support simple until that's worked out,
8948 go back through what we've read and create something usable.
8949 We could do this while processing each DIE, and feels kinda cleaner,
8950 but that way is more invasive.
8951 This is to, for example, allow the user to type "p var" or "b main"
8952 without having to specify the package name, and allow lookups
8953 of module.object to work in contexts that use the expression
8954 parser. */
8955
8956 static void
8957 fixup_go_packaging (struct dwarf2_cu *cu)
8958 {
8959 gdb::unique_xmalloc_ptr<char> package_name;
8960 struct pending *list;
8961 int i;
8962
8963 for (list = *cu->get_builder ()->get_global_symbols ();
8964 list != NULL;
8965 list = list->next)
8966 {
8967 for (i = 0; i < list->nsyms; ++i)
8968 {
8969 struct symbol *sym = list->symbol[i];
8970
8971 if (sym->language () == language_go
8972 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8973 {
8974 gdb::unique_xmalloc_ptr<char> this_package_name
8975 (go_symbol_package_name (sym));
8976
8977 if (this_package_name == NULL)
8978 continue;
8979 if (package_name == NULL)
8980 package_name = std::move (this_package_name);
8981 else
8982 {
8983 struct objfile *objfile
8984 = cu->per_cu->dwarf2_per_objfile->objfile;
8985 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
8986 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
8987 (symbol_symtab (sym) != NULL
8988 ? symtab_to_filename_for_display
8989 (symbol_symtab (sym))
8990 : objfile_name (objfile)),
8991 this_package_name.get (), package_name.get ());
8992 }
8993 }
8994 }
8995 }
8996
8997 if (package_name != NULL)
8998 {
8999 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9000 const char *saved_package_name = objfile->intern (package_name.get ());
9001 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9002 saved_package_name);
9003 struct symbol *sym;
9004
9005 sym = allocate_symbol (objfile);
9006 sym->set_language (language_go, &objfile->objfile_obstack);
9007 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9008 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9009 e.g., "main" finds the "main" module and not C's main(). */
9010 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9011 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9012 SYMBOL_TYPE (sym) = type;
9013
9014 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9015 }
9016 }
9017
9018 /* Allocate a fully-qualified name consisting of the two parts on the
9019 obstack. */
9020
9021 static const char *
9022 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9023 {
9024 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9025 }
9026
9027 /* A helper that allocates a struct discriminant_info to attach to a
9028 union type. */
9029
9030 static struct discriminant_info *
9031 alloc_discriminant_info (struct type *type, int discriminant_index,
9032 int default_index)
9033 {
9034 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9035 gdb_assert (discriminant_index == -1
9036 || (discriminant_index >= 0
9037 && discriminant_index < TYPE_NFIELDS (type)));
9038 gdb_assert (default_index == -1
9039 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
9040
9041 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9042
9043 struct discriminant_info *disc
9044 = ((struct discriminant_info *)
9045 TYPE_ZALLOC (type,
9046 offsetof (struct discriminant_info, discriminants)
9047 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9048 disc->default_index = default_index;
9049 disc->discriminant_index = discriminant_index;
9050
9051 struct dynamic_prop prop;
9052 prop.kind = PROP_UNDEFINED;
9053 prop.data.baton = disc;
9054
9055 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9056
9057 return disc;
9058 }
9059
9060 /* Some versions of rustc emitted enums in an unusual way.
9061
9062 Ordinary enums were emitted as unions. The first element of each
9063 structure in the union was named "RUST$ENUM$DISR". This element
9064 held the discriminant.
9065
9066 These versions of Rust also implemented the "non-zero"
9067 optimization. When the enum had two values, and one is empty and
9068 the other holds a pointer that cannot be zero, the pointer is used
9069 as the discriminant, with a zero value meaning the empty variant.
9070 Here, the union's first member is of the form
9071 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9072 where the fieldnos are the indices of the fields that should be
9073 traversed in order to find the field (which may be several fields deep)
9074 and the variantname is the name of the variant of the case when the
9075 field is zero.
9076
9077 This function recognizes whether TYPE is of one of these forms,
9078 and, if so, smashes it to be a variant type. */
9079
9080 static void
9081 quirk_rust_enum (struct type *type, struct objfile *objfile)
9082 {
9083 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9084
9085 /* We don't need to deal with empty enums. */
9086 if (TYPE_NFIELDS (type) == 0)
9087 return;
9088
9089 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9090 if (TYPE_NFIELDS (type) == 1
9091 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9092 {
9093 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9094
9095 /* Decode the field name to find the offset of the
9096 discriminant. */
9097 ULONGEST bit_offset = 0;
9098 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9099 while (name[0] >= '0' && name[0] <= '9')
9100 {
9101 char *tail;
9102 unsigned long index = strtoul (name, &tail, 10);
9103 name = tail;
9104 if (*name != '$'
9105 || index >= TYPE_NFIELDS (field_type)
9106 || (TYPE_FIELD_LOC_KIND (field_type, index)
9107 != FIELD_LOC_KIND_BITPOS))
9108 {
9109 complaint (_("Could not parse Rust enum encoding string \"%s\""
9110 "[in module %s]"),
9111 TYPE_FIELD_NAME (type, 0),
9112 objfile_name (objfile));
9113 return;
9114 }
9115 ++name;
9116
9117 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9118 field_type = TYPE_FIELD_TYPE (field_type, index);
9119 }
9120
9121 /* Make a union to hold the variants. */
9122 struct type *union_type = alloc_type (objfile);
9123 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9124 TYPE_NFIELDS (union_type) = 3;
9125 TYPE_FIELDS (union_type)
9126 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9127 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9128 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9129
9130 /* Put the discriminant must at index 0. */
9131 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9132 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9133 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9134 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9135
9136 /* The order of fields doesn't really matter, so put the real
9137 field at index 1 and the data-less field at index 2. */
9138 struct discriminant_info *disc
9139 = alloc_discriminant_info (union_type, 0, 1);
9140 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9141 TYPE_FIELD_NAME (union_type, 1)
9142 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9143 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9144 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9145 TYPE_FIELD_NAME (union_type, 1));
9146
9147 const char *dataless_name
9148 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9149 name);
9150 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9151 dataless_name);
9152 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9153 /* NAME points into the original discriminant name, which
9154 already has the correct lifetime. */
9155 TYPE_FIELD_NAME (union_type, 2) = name;
9156 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9157 disc->discriminants[2] = 0;
9158
9159 /* Smash this type to be a structure type. We have to do this
9160 because the type has already been recorded. */
9161 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9162 TYPE_NFIELDS (type) = 1;
9163 TYPE_FIELDS (type)
9164 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9165
9166 /* Install the variant part. */
9167 TYPE_FIELD_TYPE (type, 0) = union_type;
9168 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9169 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9170 }
9171 /* A union with a single anonymous field is probably an old-style
9172 univariant enum. */
9173 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9174 {
9175 /* Smash this type to be a structure type. We have to do this
9176 because the type has already been recorded. */
9177 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9178
9179 /* Make a union to hold the variants. */
9180 struct type *union_type = alloc_type (objfile);
9181 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9182 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9183 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9184 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9185 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9186
9187 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9188 const char *variant_name
9189 = rust_last_path_segment (TYPE_NAME (field_type));
9190 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9191 TYPE_NAME (field_type)
9192 = rust_fully_qualify (&objfile->objfile_obstack,
9193 TYPE_NAME (type), variant_name);
9194
9195 /* Install the union in the outer struct type. */
9196 TYPE_NFIELDS (type) = 1;
9197 TYPE_FIELDS (type)
9198 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9199 TYPE_FIELD_TYPE (type, 0) = union_type;
9200 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9201 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9202
9203 alloc_discriminant_info (union_type, -1, 0);
9204 }
9205 else
9206 {
9207 struct type *disr_type = nullptr;
9208 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9209 {
9210 disr_type = TYPE_FIELD_TYPE (type, i);
9211
9212 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9213 {
9214 /* All fields of a true enum will be structs. */
9215 return;
9216 }
9217 else if (TYPE_NFIELDS (disr_type) == 0)
9218 {
9219 /* Could be data-less variant, so keep going. */
9220 disr_type = nullptr;
9221 }
9222 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9223 "RUST$ENUM$DISR") != 0)
9224 {
9225 /* Not a Rust enum. */
9226 return;
9227 }
9228 else
9229 {
9230 /* Found one. */
9231 break;
9232 }
9233 }
9234
9235 /* If we got here without a discriminant, then it's probably
9236 just a union. */
9237 if (disr_type == nullptr)
9238 return;
9239
9240 /* Smash this type to be a structure type. We have to do this
9241 because the type has already been recorded. */
9242 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9243
9244 /* Make a union to hold the variants. */
9245 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9246 struct type *union_type = alloc_type (objfile);
9247 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9248 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9249 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
9250 set_type_align (union_type, TYPE_RAW_ALIGN (type));
9251 TYPE_FIELDS (union_type)
9252 = (struct field *) TYPE_ZALLOC (union_type,
9253 (TYPE_NFIELDS (union_type)
9254 * sizeof (struct field)));
9255
9256 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9257 TYPE_NFIELDS (type) * sizeof (struct field));
9258
9259 /* Install the discriminant at index 0 in the union. */
9260 TYPE_FIELD (union_type, 0) = *disr_field;
9261 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9262 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9263
9264 /* Install the union in the outer struct type. */
9265 TYPE_FIELD_TYPE (type, 0) = union_type;
9266 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9267 TYPE_NFIELDS (type) = 1;
9268
9269 /* Set the size and offset of the union type. */
9270 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9271
9272 /* We need a way to find the correct discriminant given a
9273 variant name. For convenience we build a map here. */
9274 struct type *enum_type = FIELD_TYPE (*disr_field);
9275 std::unordered_map<std::string, ULONGEST> discriminant_map;
9276 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9277 {
9278 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9279 {
9280 const char *name
9281 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9282 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9283 }
9284 }
9285
9286 int n_fields = TYPE_NFIELDS (union_type);
9287 struct discriminant_info *disc
9288 = alloc_discriminant_info (union_type, 0, -1);
9289 /* Skip the discriminant here. */
9290 for (int i = 1; i < n_fields; ++i)
9291 {
9292 /* Find the final word in the name of this variant's type.
9293 That name can be used to look up the correct
9294 discriminant. */
9295 const char *variant_name
9296 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9297 i)));
9298
9299 auto iter = discriminant_map.find (variant_name);
9300 if (iter != discriminant_map.end ())
9301 disc->discriminants[i] = iter->second;
9302
9303 /* Remove the discriminant field, if it exists. */
9304 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
9305 if (TYPE_NFIELDS (sub_type) > 0)
9306 {
9307 --TYPE_NFIELDS (sub_type);
9308 ++TYPE_FIELDS (sub_type);
9309 }
9310 TYPE_FIELD_NAME (union_type, i) = variant_name;
9311 TYPE_NAME (sub_type)
9312 = rust_fully_qualify (&objfile->objfile_obstack,
9313 TYPE_NAME (type), variant_name);
9314 }
9315 }
9316 }
9317
9318 /* Rewrite some Rust unions to be structures with variants parts. */
9319
9320 static void
9321 rust_union_quirks (struct dwarf2_cu *cu)
9322 {
9323 gdb_assert (cu->language == language_rust);
9324 for (type *type_ : cu->rust_unions)
9325 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
9326 /* We don't need this any more. */
9327 cu->rust_unions.clear ();
9328 }
9329
9330 /* Return the symtab for PER_CU. This works properly regardless of
9331 whether we're using the index or psymtabs. */
9332
9333 static struct compunit_symtab *
9334 get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
9335 {
9336 return (per_cu->dwarf2_per_objfile->using_index
9337 ? per_cu->v.quick->compunit_symtab
9338 : per_cu->v.psymtab->compunit_symtab);
9339 }
9340
9341 /* A helper function for computing the list of all symbol tables
9342 included by PER_CU. */
9343
9344 static void
9345 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9346 htab_t all_children, htab_t all_type_symtabs,
9347 struct dwarf2_per_cu_data *per_cu,
9348 struct compunit_symtab *immediate_parent)
9349 {
9350 void **slot;
9351 struct compunit_symtab *cust;
9352
9353 slot = htab_find_slot (all_children, per_cu, INSERT);
9354 if (*slot != NULL)
9355 {
9356 /* This inclusion and its children have been processed. */
9357 return;
9358 }
9359
9360 *slot = per_cu;
9361 /* Only add a CU if it has a symbol table. */
9362 cust = get_compunit_symtab (per_cu);
9363 if (cust != NULL)
9364 {
9365 /* If this is a type unit only add its symbol table if we haven't
9366 seen it yet (type unit per_cu's can share symtabs). */
9367 if (per_cu->is_debug_types)
9368 {
9369 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9370 if (*slot == NULL)
9371 {
9372 *slot = cust;
9373 result->push_back (cust);
9374 if (cust->user == NULL)
9375 cust->user = immediate_parent;
9376 }
9377 }
9378 else
9379 {
9380 result->push_back (cust);
9381 if (cust->user == NULL)
9382 cust->user = immediate_parent;
9383 }
9384 }
9385
9386 if (!per_cu->imported_symtabs_empty ())
9387 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9388 {
9389 recursively_compute_inclusions (result, all_children,
9390 all_type_symtabs, ptr, cust);
9391 }
9392 }
9393
9394 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9395 PER_CU. */
9396
9397 static void
9398 compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
9399 {
9400 gdb_assert (! per_cu->is_debug_types);
9401
9402 if (!per_cu->imported_symtabs_empty ())
9403 {
9404 int len;
9405 std::vector<compunit_symtab *> result_symtabs;
9406 htab_t all_children, all_type_symtabs;
9407 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
9408
9409 /* If we don't have a symtab, we can just skip this case. */
9410 if (cust == NULL)
9411 return;
9412
9413 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9414 NULL, xcalloc, xfree);
9415 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9416 NULL, xcalloc, xfree);
9417
9418 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9419 {
9420 recursively_compute_inclusions (&result_symtabs, all_children,
9421 all_type_symtabs, ptr, cust);
9422 }
9423
9424 /* Now we have a transitive closure of all the included symtabs. */
9425 len = result_symtabs.size ();
9426 cust->includes
9427 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9428 struct compunit_symtab *, len + 1);
9429 memcpy (cust->includes, result_symtabs.data (),
9430 len * sizeof (compunit_symtab *));
9431 cust->includes[len] = NULL;
9432
9433 htab_delete (all_children);
9434 htab_delete (all_type_symtabs);
9435 }
9436 }
9437
9438 /* Compute the 'includes' field for the symtabs of all the CUs we just
9439 read. */
9440
9441 static void
9442 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9443 {
9444 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
9445 {
9446 if (! iter->is_debug_types)
9447 compute_compunit_symtab_includes (iter);
9448 }
9449
9450 dwarf2_per_objfile->just_read_cus.clear ();
9451 }
9452
9453 /* Generate full symbol information for PER_CU, whose DIEs have
9454 already been loaded into memory. */
9455
9456 static void
9457 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9458 enum language pretend_language)
9459 {
9460 struct dwarf2_cu *cu = per_cu->cu;
9461 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9462 struct objfile *objfile = dwarf2_per_objfile->objfile;
9463 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9464 CORE_ADDR lowpc, highpc;
9465 struct compunit_symtab *cust;
9466 CORE_ADDR baseaddr;
9467 struct block *static_block;
9468 CORE_ADDR addr;
9469
9470 baseaddr = objfile->text_section_offset ();
9471
9472 /* Clear the list here in case something was left over. */
9473 cu->method_list.clear ();
9474
9475 cu->language = pretend_language;
9476 cu->language_defn = language_def (cu->language);
9477
9478 /* Do line number decoding in read_file_scope () */
9479 process_die (cu->dies, cu);
9480
9481 /* For now fudge the Go package. */
9482 if (cu->language == language_go)
9483 fixup_go_packaging (cu);
9484
9485 /* Now that we have processed all the DIEs in the CU, all the types
9486 should be complete, and it should now be safe to compute all of the
9487 physnames. */
9488 compute_delayed_physnames (cu);
9489
9490 if (cu->language == language_rust)
9491 rust_union_quirks (cu);
9492
9493 /* Some compilers don't define a DW_AT_high_pc attribute for the
9494 compilation unit. If the DW_AT_high_pc is missing, synthesize
9495 it, by scanning the DIE's below the compilation unit. */
9496 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9497
9498 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9499 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9500
9501 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9502 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9503 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9504 addrmap to help ensure it has an accurate map of pc values belonging to
9505 this comp unit. */
9506 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9507
9508 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9509 SECT_OFF_TEXT (objfile),
9510 0);
9511
9512 if (cust != NULL)
9513 {
9514 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9515
9516 /* Set symtab language to language from DW_AT_language. If the
9517 compilation is from a C file generated by language preprocessors, do
9518 not set the language if it was already deduced by start_subfile. */
9519 if (!(cu->language == language_c
9520 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9521 COMPUNIT_FILETABS (cust)->language = cu->language;
9522
9523 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9524 produce DW_AT_location with location lists but it can be possibly
9525 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9526 there were bugs in prologue debug info, fixed later in GCC-4.5
9527 by "unwind info for epilogues" patch (which is not directly related).
9528
9529 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9530 needed, it would be wrong due to missing DW_AT_producer there.
9531
9532 Still one can confuse GDB by using non-standard GCC compilation
9533 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9534 */
9535 if (cu->has_loclist && gcc_4_minor >= 5)
9536 cust->locations_valid = 1;
9537
9538 if (gcc_4_minor >= 5)
9539 cust->epilogue_unwind_valid = 1;
9540
9541 cust->call_site_htab = cu->call_site_htab;
9542 }
9543
9544 if (dwarf2_per_objfile->using_index)
9545 per_cu->v.quick->compunit_symtab = cust;
9546 else
9547 {
9548 dwarf2_psymtab *pst = per_cu->v.psymtab;
9549 pst->compunit_symtab = cust;
9550 pst->readin = true;
9551 }
9552
9553 /* Push it for inclusion processing later. */
9554 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
9555
9556 /* Not needed any more. */
9557 cu->reset_builder ();
9558 }
9559
9560 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9561 already been loaded into memory. */
9562
9563 static void
9564 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9565 enum language pretend_language)
9566 {
9567 struct dwarf2_cu *cu = per_cu->cu;
9568 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9569 struct objfile *objfile = dwarf2_per_objfile->objfile;
9570 struct compunit_symtab *cust;
9571 struct signatured_type *sig_type;
9572
9573 gdb_assert (per_cu->is_debug_types);
9574 sig_type = (struct signatured_type *) per_cu;
9575
9576 /* Clear the list here in case something was left over. */
9577 cu->method_list.clear ();
9578
9579 cu->language = pretend_language;
9580 cu->language_defn = language_def (cu->language);
9581
9582 /* The symbol tables are set up in read_type_unit_scope. */
9583 process_die (cu->dies, cu);
9584
9585 /* For now fudge the Go package. */
9586 if (cu->language == language_go)
9587 fixup_go_packaging (cu);
9588
9589 /* Now that we have processed all the DIEs in the CU, all the types
9590 should be complete, and it should now be safe to compute all of the
9591 physnames. */
9592 compute_delayed_physnames (cu);
9593
9594 if (cu->language == language_rust)
9595 rust_union_quirks (cu);
9596
9597 /* TUs share symbol tables.
9598 If this is the first TU to use this symtab, complete the construction
9599 of it with end_expandable_symtab. Otherwise, complete the addition of
9600 this TU's symbols to the existing symtab. */
9601 if (sig_type->type_unit_group->compunit_symtab == NULL)
9602 {
9603 buildsym_compunit *builder = cu->get_builder ();
9604 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9605 sig_type->type_unit_group->compunit_symtab = cust;
9606
9607 if (cust != NULL)
9608 {
9609 /* Set symtab language to language from DW_AT_language. If the
9610 compilation is from a C file generated by language preprocessors,
9611 do not set the language if it was already deduced by
9612 start_subfile. */
9613 if (!(cu->language == language_c
9614 && COMPUNIT_FILETABS (cust)->language != language_c))
9615 COMPUNIT_FILETABS (cust)->language = cu->language;
9616 }
9617 }
9618 else
9619 {
9620 cu->get_builder ()->augment_type_symtab ();
9621 cust = sig_type->type_unit_group->compunit_symtab;
9622 }
9623
9624 if (dwarf2_per_objfile->using_index)
9625 per_cu->v.quick->compunit_symtab = cust;
9626 else
9627 {
9628 dwarf2_psymtab *pst = per_cu->v.psymtab;
9629 pst->compunit_symtab = cust;
9630 pst->readin = true;
9631 }
9632
9633 /* Not needed any more. */
9634 cu->reset_builder ();
9635 }
9636
9637 /* Process an imported unit DIE. */
9638
9639 static void
9640 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9641 {
9642 struct attribute *attr;
9643
9644 /* For now we don't handle imported units in type units. */
9645 if (cu->per_cu->is_debug_types)
9646 {
9647 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9648 " supported in type units [in module %s]"),
9649 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
9650 }
9651
9652 attr = dwarf2_attr (die, DW_AT_import, cu);
9653 if (attr != NULL)
9654 {
9655 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
9656 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9657 dwarf2_per_cu_data *per_cu
9658 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
9659 cu->per_cu->dwarf2_per_objfile);
9660
9661 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9662 into another compilation unit, at root level. Regard this as a hint,
9663 and ignore it. */
9664 if (die->parent && die->parent->parent == NULL
9665 && per_cu->unit_type == DW_UT_compile
9666 && per_cu->lang == language_cplus)
9667 return;
9668
9669 /* If necessary, add it to the queue and load its DIEs. */
9670 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9671 load_full_comp_unit (per_cu, false, cu->language);
9672
9673 cu->per_cu->imported_symtabs_push (per_cu);
9674 }
9675 }
9676
9677 /* RAII object that represents a process_die scope: i.e.,
9678 starts/finishes processing a DIE. */
9679 class process_die_scope
9680 {
9681 public:
9682 process_die_scope (die_info *die, dwarf2_cu *cu)
9683 : m_die (die), m_cu (cu)
9684 {
9685 /* We should only be processing DIEs not already in process. */
9686 gdb_assert (!m_die->in_process);
9687 m_die->in_process = true;
9688 }
9689
9690 ~process_die_scope ()
9691 {
9692 m_die->in_process = false;
9693
9694 /* If we're done processing the DIE for the CU that owns the line
9695 header, we don't need the line header anymore. */
9696 if (m_cu->line_header_die_owner == m_die)
9697 {
9698 delete m_cu->line_header;
9699 m_cu->line_header = NULL;
9700 m_cu->line_header_die_owner = NULL;
9701 }
9702 }
9703
9704 private:
9705 die_info *m_die;
9706 dwarf2_cu *m_cu;
9707 };
9708
9709 /* Process a die and its children. */
9710
9711 static void
9712 process_die (struct die_info *die, struct dwarf2_cu *cu)
9713 {
9714 process_die_scope scope (die, cu);
9715
9716 switch (die->tag)
9717 {
9718 case DW_TAG_padding:
9719 break;
9720 case DW_TAG_compile_unit:
9721 case DW_TAG_partial_unit:
9722 read_file_scope (die, cu);
9723 break;
9724 case DW_TAG_type_unit:
9725 read_type_unit_scope (die, cu);
9726 break;
9727 case DW_TAG_subprogram:
9728 /* Nested subprograms in Fortran get a prefix. */
9729 if (cu->language == language_fortran
9730 && die->parent != NULL
9731 && die->parent->tag == DW_TAG_subprogram)
9732 cu->processing_has_namespace_info = true;
9733 /* Fall through. */
9734 case DW_TAG_inlined_subroutine:
9735 read_func_scope (die, cu);
9736 break;
9737 case DW_TAG_lexical_block:
9738 case DW_TAG_try_block:
9739 case DW_TAG_catch_block:
9740 read_lexical_block_scope (die, cu);
9741 break;
9742 case DW_TAG_call_site:
9743 case DW_TAG_GNU_call_site:
9744 read_call_site_scope (die, cu);
9745 break;
9746 case DW_TAG_class_type:
9747 case DW_TAG_interface_type:
9748 case DW_TAG_structure_type:
9749 case DW_TAG_union_type:
9750 process_structure_scope (die, cu);
9751 break;
9752 case DW_TAG_enumeration_type:
9753 process_enumeration_scope (die, cu);
9754 break;
9755
9756 /* These dies have a type, but processing them does not create
9757 a symbol or recurse to process the children. Therefore we can
9758 read them on-demand through read_type_die. */
9759 case DW_TAG_subroutine_type:
9760 case DW_TAG_set_type:
9761 case DW_TAG_array_type:
9762 case DW_TAG_pointer_type:
9763 case DW_TAG_ptr_to_member_type:
9764 case DW_TAG_reference_type:
9765 case DW_TAG_rvalue_reference_type:
9766 case DW_TAG_string_type:
9767 break;
9768
9769 case DW_TAG_base_type:
9770 case DW_TAG_subrange_type:
9771 case DW_TAG_typedef:
9772 /* Add a typedef symbol for the type definition, if it has a
9773 DW_AT_name. */
9774 new_symbol (die, read_type_die (die, cu), cu);
9775 break;
9776 case DW_TAG_common_block:
9777 read_common_block (die, cu);
9778 break;
9779 case DW_TAG_common_inclusion:
9780 break;
9781 case DW_TAG_namespace:
9782 cu->processing_has_namespace_info = true;
9783 read_namespace (die, cu);
9784 break;
9785 case DW_TAG_module:
9786 cu->processing_has_namespace_info = true;
9787 read_module (die, cu);
9788 break;
9789 case DW_TAG_imported_declaration:
9790 cu->processing_has_namespace_info = true;
9791 if (read_namespace_alias (die, cu))
9792 break;
9793 /* The declaration is not a global namespace alias. */
9794 /* Fall through. */
9795 case DW_TAG_imported_module:
9796 cu->processing_has_namespace_info = true;
9797 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9798 || cu->language != language_fortran))
9799 complaint (_("Tag '%s' has unexpected children"),
9800 dwarf_tag_name (die->tag));
9801 read_import_statement (die, cu);
9802 break;
9803
9804 case DW_TAG_imported_unit:
9805 process_imported_unit_die (die, cu);
9806 break;
9807
9808 case DW_TAG_variable:
9809 read_variable (die, cu);
9810 break;
9811
9812 default:
9813 new_symbol (die, NULL, cu);
9814 break;
9815 }
9816 }
9817 \f
9818 /* DWARF name computation. */
9819
9820 /* A helper function for dwarf2_compute_name which determines whether DIE
9821 needs to have the name of the scope prepended to the name listed in the
9822 die. */
9823
9824 static int
9825 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9826 {
9827 struct attribute *attr;
9828
9829 switch (die->tag)
9830 {
9831 case DW_TAG_namespace:
9832 case DW_TAG_typedef:
9833 case DW_TAG_class_type:
9834 case DW_TAG_interface_type:
9835 case DW_TAG_structure_type:
9836 case DW_TAG_union_type:
9837 case DW_TAG_enumeration_type:
9838 case DW_TAG_enumerator:
9839 case DW_TAG_subprogram:
9840 case DW_TAG_inlined_subroutine:
9841 case DW_TAG_member:
9842 case DW_TAG_imported_declaration:
9843 return 1;
9844
9845 case DW_TAG_variable:
9846 case DW_TAG_constant:
9847 /* We only need to prefix "globally" visible variables. These include
9848 any variable marked with DW_AT_external or any variable that
9849 lives in a namespace. [Variables in anonymous namespaces
9850 require prefixing, but they are not DW_AT_external.] */
9851
9852 if (dwarf2_attr (die, DW_AT_specification, cu))
9853 {
9854 struct dwarf2_cu *spec_cu = cu;
9855
9856 return die_needs_namespace (die_specification (die, &spec_cu),
9857 spec_cu);
9858 }
9859
9860 attr = dwarf2_attr (die, DW_AT_external, cu);
9861 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9862 && die->parent->tag != DW_TAG_module)
9863 return 0;
9864 /* A variable in a lexical block of some kind does not need a
9865 namespace, even though in C++ such variables may be external
9866 and have a mangled name. */
9867 if (die->parent->tag == DW_TAG_lexical_block
9868 || die->parent->tag == DW_TAG_try_block
9869 || die->parent->tag == DW_TAG_catch_block
9870 || die->parent->tag == DW_TAG_subprogram)
9871 return 0;
9872 return 1;
9873
9874 default:
9875 return 0;
9876 }
9877 }
9878
9879 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9880 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9881 defined for the given DIE. */
9882
9883 static struct attribute *
9884 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9885 {
9886 struct attribute *attr;
9887
9888 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9889 if (attr == NULL)
9890 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9891
9892 return attr;
9893 }
9894
9895 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9896 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9897 defined for the given DIE. */
9898
9899 static const char *
9900 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9901 {
9902 const char *linkage_name;
9903
9904 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9905 if (linkage_name == NULL)
9906 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9907
9908 return linkage_name;
9909 }
9910
9911 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
9912 compute the physname for the object, which include a method's:
9913 - formal parameters (C++),
9914 - receiver type (Go),
9915
9916 The term "physname" is a bit confusing.
9917 For C++, for example, it is the demangled name.
9918 For Go, for example, it's the mangled name.
9919
9920 For Ada, return the DIE's linkage name rather than the fully qualified
9921 name. PHYSNAME is ignored..
9922
9923 The result is allocated on the objfile_obstack and canonicalized. */
9924
9925 static const char *
9926 dwarf2_compute_name (const char *name,
9927 struct die_info *die, struct dwarf2_cu *cu,
9928 int physname)
9929 {
9930 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9931
9932 if (name == NULL)
9933 name = dwarf2_name (die, cu);
9934
9935 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9936 but otherwise compute it by typename_concat inside GDB.
9937 FIXME: Actually this is not really true, or at least not always true.
9938 It's all very confusing. compute_and_set_names doesn't try to demangle
9939 Fortran names because there is no mangling standard. So new_symbol
9940 will set the demangled name to the result of dwarf2_full_name, and it is
9941 the demangled name that GDB uses if it exists. */
9942 if (cu->language == language_ada
9943 || (cu->language == language_fortran && physname))
9944 {
9945 /* For Ada unit, we prefer the linkage name over the name, as
9946 the former contains the exported name, which the user expects
9947 to be able to reference. Ideally, we want the user to be able
9948 to reference this entity using either natural or linkage name,
9949 but we haven't started looking at this enhancement yet. */
9950 const char *linkage_name = dw2_linkage_name (die, cu);
9951
9952 if (linkage_name != NULL)
9953 return linkage_name;
9954 }
9955
9956 /* These are the only languages we know how to qualify names in. */
9957 if (name != NULL
9958 && (cu->language == language_cplus
9959 || cu->language == language_fortran || cu->language == language_d
9960 || cu->language == language_rust))
9961 {
9962 if (die_needs_namespace (die, cu))
9963 {
9964 const char *prefix;
9965 const char *canonical_name = NULL;
9966
9967 string_file buf;
9968
9969 prefix = determine_prefix (die, cu);
9970 if (*prefix != '\0')
9971 {
9972 gdb::unique_xmalloc_ptr<char> prefixed_name
9973 (typename_concat (NULL, prefix, name, physname, cu));
9974
9975 buf.puts (prefixed_name.get ());
9976 }
9977 else
9978 buf.puts (name);
9979
9980 /* Template parameters may be specified in the DIE's DW_AT_name, or
9981 as children with DW_TAG_template_type_param or
9982 DW_TAG_value_type_param. If the latter, add them to the name
9983 here. If the name already has template parameters, then
9984 skip this step; some versions of GCC emit both, and
9985 it is more efficient to use the pre-computed name.
9986
9987 Something to keep in mind about this process: it is very
9988 unlikely, or in some cases downright impossible, to produce
9989 something that will match the mangled name of a function.
9990 If the definition of the function has the same debug info,
9991 we should be able to match up with it anyway. But fallbacks
9992 using the minimal symbol, for instance to find a method
9993 implemented in a stripped copy of libstdc++, will not work.
9994 If we do not have debug info for the definition, we will have to
9995 match them up some other way.
9996
9997 When we do name matching there is a related problem with function
9998 templates; two instantiated function templates are allowed to
9999 differ only by their return types, which we do not add here. */
10000
10001 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10002 {
10003 struct attribute *attr;
10004 struct die_info *child;
10005 int first = 1;
10006
10007 die->building_fullname = 1;
10008
10009 for (child = die->child; child != NULL; child = child->sibling)
10010 {
10011 struct type *type;
10012 LONGEST value;
10013 const gdb_byte *bytes;
10014 struct dwarf2_locexpr_baton *baton;
10015 struct value *v;
10016
10017 if (child->tag != DW_TAG_template_type_param
10018 && child->tag != DW_TAG_template_value_param)
10019 continue;
10020
10021 if (first)
10022 {
10023 buf.puts ("<");
10024 first = 0;
10025 }
10026 else
10027 buf.puts (", ");
10028
10029 attr = dwarf2_attr (child, DW_AT_type, cu);
10030 if (attr == NULL)
10031 {
10032 complaint (_("template parameter missing DW_AT_type"));
10033 buf.puts ("UNKNOWN_TYPE");
10034 continue;
10035 }
10036 type = die_type (child, cu);
10037
10038 if (child->tag == DW_TAG_template_type_param)
10039 {
10040 c_print_type (type, "", &buf, -1, 0, cu->language,
10041 &type_print_raw_options);
10042 continue;
10043 }
10044
10045 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10046 if (attr == NULL)
10047 {
10048 complaint (_("template parameter missing "
10049 "DW_AT_const_value"));
10050 buf.puts ("UNKNOWN_VALUE");
10051 continue;
10052 }
10053
10054 dwarf2_const_value_attr (attr, type, name,
10055 &cu->comp_unit_obstack, cu,
10056 &value, &bytes, &baton);
10057
10058 if (TYPE_NOSIGN (type))
10059 /* GDB prints characters as NUMBER 'CHAR'. If that's
10060 changed, this can use value_print instead. */
10061 c_printchar (value, type, &buf);
10062 else
10063 {
10064 struct value_print_options opts;
10065
10066 if (baton != NULL)
10067 v = dwarf2_evaluate_loc_desc (type, NULL,
10068 baton->data,
10069 baton->size,
10070 baton->per_cu);
10071 else if (bytes != NULL)
10072 {
10073 v = allocate_value (type);
10074 memcpy (value_contents_writeable (v), bytes,
10075 TYPE_LENGTH (type));
10076 }
10077 else
10078 v = value_from_longest (type, value);
10079
10080 /* Specify decimal so that we do not depend on
10081 the radix. */
10082 get_formatted_print_options (&opts, 'd');
10083 opts.raw = 1;
10084 value_print (v, &buf, &opts);
10085 release_value (v);
10086 }
10087 }
10088
10089 die->building_fullname = 0;
10090
10091 if (!first)
10092 {
10093 /* Close the argument list, with a space if necessary
10094 (nested templates). */
10095 if (!buf.empty () && buf.string ().back () == '>')
10096 buf.puts (" >");
10097 else
10098 buf.puts (">");
10099 }
10100 }
10101
10102 /* For C++ methods, append formal parameter type
10103 information, if PHYSNAME. */
10104
10105 if (physname && die->tag == DW_TAG_subprogram
10106 && cu->language == language_cplus)
10107 {
10108 struct type *type = read_type_die (die, cu);
10109
10110 c_type_print_args (type, &buf, 1, cu->language,
10111 &type_print_raw_options);
10112
10113 if (cu->language == language_cplus)
10114 {
10115 /* Assume that an artificial first parameter is
10116 "this", but do not crash if it is not. RealView
10117 marks unnamed (and thus unused) parameters as
10118 artificial; there is no way to differentiate
10119 the two cases. */
10120 if (TYPE_NFIELDS (type) > 0
10121 && TYPE_FIELD_ARTIFICIAL (type, 0)
10122 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
10123 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10124 0))))
10125 buf.puts (" const");
10126 }
10127 }
10128
10129 const std::string &intermediate_name = buf.string ();
10130
10131 if (cu->language == language_cplus)
10132 canonical_name
10133 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10134 objfile);
10135
10136 /* If we only computed INTERMEDIATE_NAME, or if
10137 INTERMEDIATE_NAME is already canonical, then we need to
10138 intern it. */
10139 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10140 name = objfile->intern (intermediate_name);
10141 else
10142 name = canonical_name;
10143 }
10144 }
10145
10146 return name;
10147 }
10148
10149 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10150 If scope qualifiers are appropriate they will be added. The result
10151 will be allocated on the storage_obstack, or NULL if the DIE does
10152 not have a name. NAME may either be from a previous call to
10153 dwarf2_name or NULL.
10154
10155 The output string will be canonicalized (if C++). */
10156
10157 static const char *
10158 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10159 {
10160 return dwarf2_compute_name (name, die, cu, 0);
10161 }
10162
10163 /* Construct a physname for the given DIE in CU. NAME may either be
10164 from a previous call to dwarf2_name or NULL. The result will be
10165 allocated on the objfile_objstack or NULL if the DIE does not have a
10166 name.
10167
10168 The output string will be canonicalized (if C++). */
10169
10170 static const char *
10171 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10172 {
10173 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10174 const char *retval, *mangled = NULL, *canon = NULL;
10175 int need_copy = 1;
10176
10177 /* In this case dwarf2_compute_name is just a shortcut not building anything
10178 on its own. */
10179 if (!die_needs_namespace (die, cu))
10180 return dwarf2_compute_name (name, die, cu, 1);
10181
10182 mangled = dw2_linkage_name (die, cu);
10183
10184 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10185 See https://github.com/rust-lang/rust/issues/32925. */
10186 if (cu->language == language_rust && mangled != NULL
10187 && strchr (mangled, '{') != NULL)
10188 mangled = NULL;
10189
10190 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10191 has computed. */
10192 gdb::unique_xmalloc_ptr<char> demangled;
10193 if (mangled != NULL)
10194 {
10195
10196 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10197 {
10198 /* Do nothing (do not demangle the symbol name). */
10199 }
10200 else if (cu->language == language_go)
10201 {
10202 /* This is a lie, but we already lie to the caller new_symbol.
10203 new_symbol assumes we return the mangled name.
10204 This just undoes that lie until things are cleaned up. */
10205 }
10206 else
10207 {
10208 /* Use DMGL_RET_DROP for C++ template functions to suppress
10209 their return type. It is easier for GDB users to search
10210 for such functions as `name(params)' than `long name(params)'.
10211 In such case the minimal symbol names do not match the full
10212 symbol names but for template functions there is never a need
10213 to look up their definition from their declaration so
10214 the only disadvantage remains the minimal symbol variant
10215 `long name(params)' does not have the proper inferior type. */
10216 demangled.reset (gdb_demangle (mangled,
10217 (DMGL_PARAMS | DMGL_ANSI
10218 | DMGL_RET_DROP)));
10219 }
10220 if (demangled)
10221 canon = demangled.get ();
10222 else
10223 {
10224 canon = mangled;
10225 need_copy = 0;
10226 }
10227 }
10228
10229 if (canon == NULL || check_physname)
10230 {
10231 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10232
10233 if (canon != NULL && strcmp (physname, canon) != 0)
10234 {
10235 /* It may not mean a bug in GDB. The compiler could also
10236 compute DW_AT_linkage_name incorrectly. But in such case
10237 GDB would need to be bug-to-bug compatible. */
10238
10239 complaint (_("Computed physname <%s> does not match demangled <%s> "
10240 "(from linkage <%s>) - DIE at %s [in module %s]"),
10241 physname, canon, mangled, sect_offset_str (die->sect_off),
10242 objfile_name (objfile));
10243
10244 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10245 is available here - over computed PHYSNAME. It is safer
10246 against both buggy GDB and buggy compilers. */
10247
10248 retval = canon;
10249 }
10250 else
10251 {
10252 retval = physname;
10253 need_copy = 0;
10254 }
10255 }
10256 else
10257 retval = canon;
10258
10259 if (need_copy)
10260 retval = objfile->intern (retval);
10261
10262 return retval;
10263 }
10264
10265 /* Inspect DIE in CU for a namespace alias. If one exists, record
10266 a new symbol for it.
10267
10268 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10269
10270 static int
10271 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10272 {
10273 struct attribute *attr;
10274
10275 /* If the die does not have a name, this is not a namespace
10276 alias. */
10277 attr = dwarf2_attr (die, DW_AT_name, cu);
10278 if (attr != NULL)
10279 {
10280 int num;
10281 struct die_info *d = die;
10282 struct dwarf2_cu *imported_cu = cu;
10283
10284 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10285 keep inspecting DIEs until we hit the underlying import. */
10286 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10287 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10288 {
10289 attr = dwarf2_attr (d, DW_AT_import, cu);
10290 if (attr == NULL)
10291 break;
10292
10293 d = follow_die_ref (d, attr, &imported_cu);
10294 if (d->tag != DW_TAG_imported_declaration)
10295 break;
10296 }
10297
10298 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10299 {
10300 complaint (_("DIE at %s has too many recursively imported "
10301 "declarations"), sect_offset_str (d->sect_off));
10302 return 0;
10303 }
10304
10305 if (attr != NULL)
10306 {
10307 struct type *type;
10308 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10309
10310 type = get_die_type_at_offset (sect_off, cu->per_cu);
10311 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10312 {
10313 /* This declaration is a global namespace alias. Add
10314 a symbol for it whose type is the aliased namespace. */
10315 new_symbol (die, type, cu);
10316 return 1;
10317 }
10318 }
10319 }
10320
10321 return 0;
10322 }
10323
10324 /* Return the using directives repository (global or local?) to use in the
10325 current context for CU.
10326
10327 For Ada, imported declarations can materialize renamings, which *may* be
10328 global. However it is impossible (for now?) in DWARF to distinguish
10329 "external" imported declarations and "static" ones. As all imported
10330 declarations seem to be static in all other languages, make them all CU-wide
10331 global only in Ada. */
10332
10333 static struct using_direct **
10334 using_directives (struct dwarf2_cu *cu)
10335 {
10336 if (cu->language == language_ada
10337 && cu->get_builder ()->outermost_context_p ())
10338 return cu->get_builder ()->get_global_using_directives ();
10339 else
10340 return cu->get_builder ()->get_local_using_directives ();
10341 }
10342
10343 /* Read the import statement specified by the given die and record it. */
10344
10345 static void
10346 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10347 {
10348 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
10349 struct attribute *import_attr;
10350 struct die_info *imported_die, *child_die;
10351 struct dwarf2_cu *imported_cu;
10352 const char *imported_name;
10353 const char *imported_name_prefix;
10354 const char *canonical_name;
10355 const char *import_alias;
10356 const char *imported_declaration = NULL;
10357 const char *import_prefix;
10358 std::vector<const char *> excludes;
10359
10360 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10361 if (import_attr == NULL)
10362 {
10363 complaint (_("Tag '%s' has no DW_AT_import"),
10364 dwarf_tag_name (die->tag));
10365 return;
10366 }
10367
10368 imported_cu = cu;
10369 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10370 imported_name = dwarf2_name (imported_die, imported_cu);
10371 if (imported_name == NULL)
10372 {
10373 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10374
10375 The import in the following code:
10376 namespace A
10377 {
10378 typedef int B;
10379 }
10380
10381 int main ()
10382 {
10383 using A::B;
10384 B b;
10385 return b;
10386 }
10387
10388 ...
10389 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10390 <52> DW_AT_decl_file : 1
10391 <53> DW_AT_decl_line : 6
10392 <54> DW_AT_import : <0x75>
10393 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10394 <59> DW_AT_name : B
10395 <5b> DW_AT_decl_file : 1
10396 <5c> DW_AT_decl_line : 2
10397 <5d> DW_AT_type : <0x6e>
10398 ...
10399 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10400 <76> DW_AT_byte_size : 4
10401 <77> DW_AT_encoding : 5 (signed)
10402
10403 imports the wrong die ( 0x75 instead of 0x58 ).
10404 This case will be ignored until the gcc bug is fixed. */
10405 return;
10406 }
10407
10408 /* Figure out the local name after import. */
10409 import_alias = dwarf2_name (die, cu);
10410
10411 /* Figure out where the statement is being imported to. */
10412 import_prefix = determine_prefix (die, cu);
10413
10414 /* Figure out what the scope of the imported die is and prepend it
10415 to the name of the imported die. */
10416 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10417
10418 if (imported_die->tag != DW_TAG_namespace
10419 && imported_die->tag != DW_TAG_module)
10420 {
10421 imported_declaration = imported_name;
10422 canonical_name = imported_name_prefix;
10423 }
10424 else if (strlen (imported_name_prefix) > 0)
10425 canonical_name = obconcat (&objfile->objfile_obstack,
10426 imported_name_prefix,
10427 (cu->language == language_d ? "." : "::"),
10428 imported_name, (char *) NULL);
10429 else
10430 canonical_name = imported_name;
10431
10432 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10433 for (child_die = die->child; child_die && child_die->tag;
10434 child_die = child_die->sibling)
10435 {
10436 /* DWARF-4: A Fortran use statement with a “rename list” may be
10437 represented by an imported module entry with an import attribute
10438 referring to the module and owned entries corresponding to those
10439 entities that are renamed as part of being imported. */
10440
10441 if (child_die->tag != DW_TAG_imported_declaration)
10442 {
10443 complaint (_("child DW_TAG_imported_declaration expected "
10444 "- DIE at %s [in module %s]"),
10445 sect_offset_str (child_die->sect_off),
10446 objfile_name (objfile));
10447 continue;
10448 }
10449
10450 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10451 if (import_attr == NULL)
10452 {
10453 complaint (_("Tag '%s' has no DW_AT_import"),
10454 dwarf_tag_name (child_die->tag));
10455 continue;
10456 }
10457
10458 imported_cu = cu;
10459 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10460 &imported_cu);
10461 imported_name = dwarf2_name (imported_die, imported_cu);
10462 if (imported_name == NULL)
10463 {
10464 complaint (_("child DW_TAG_imported_declaration has unknown "
10465 "imported name - DIE at %s [in module %s]"),
10466 sect_offset_str (child_die->sect_off),
10467 objfile_name (objfile));
10468 continue;
10469 }
10470
10471 excludes.push_back (imported_name);
10472
10473 process_die (child_die, cu);
10474 }
10475
10476 add_using_directive (using_directives (cu),
10477 import_prefix,
10478 canonical_name,
10479 import_alias,
10480 imported_declaration,
10481 excludes,
10482 0,
10483 &objfile->objfile_obstack);
10484 }
10485
10486 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10487 types, but gives them a size of zero. Starting with version 14,
10488 ICC is compatible with GCC. */
10489
10490 static bool
10491 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10492 {
10493 if (!cu->checked_producer)
10494 check_producer (cu);
10495
10496 return cu->producer_is_icc_lt_14;
10497 }
10498
10499 /* ICC generates a DW_AT_type for C void functions. This was observed on
10500 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10501 which says that void functions should not have a DW_AT_type. */
10502
10503 static bool
10504 producer_is_icc (struct dwarf2_cu *cu)
10505 {
10506 if (!cu->checked_producer)
10507 check_producer (cu);
10508
10509 return cu->producer_is_icc;
10510 }
10511
10512 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10513 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10514 this, it was first present in GCC release 4.3.0. */
10515
10516 static bool
10517 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10518 {
10519 if (!cu->checked_producer)
10520 check_producer (cu);
10521
10522 return cu->producer_is_gcc_lt_4_3;
10523 }
10524
10525 static file_and_directory
10526 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10527 {
10528 file_and_directory res;
10529
10530 /* Find the filename. Do not use dwarf2_name here, since the filename
10531 is not a source language identifier. */
10532 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10533 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10534
10535 if (res.comp_dir == NULL
10536 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10537 && IS_ABSOLUTE_PATH (res.name))
10538 {
10539 res.comp_dir_storage = ldirname (res.name);
10540 if (!res.comp_dir_storage.empty ())
10541 res.comp_dir = res.comp_dir_storage.c_str ();
10542 }
10543 if (res.comp_dir != NULL)
10544 {
10545 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10546 directory, get rid of it. */
10547 const char *cp = strchr (res.comp_dir, ':');
10548
10549 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10550 res.comp_dir = cp + 1;
10551 }
10552
10553 if (res.name == NULL)
10554 res.name = "<unknown>";
10555
10556 return res;
10557 }
10558
10559 /* Handle DW_AT_stmt_list for a compilation unit.
10560 DIE is the DW_TAG_compile_unit die for CU.
10561 COMP_DIR is the compilation directory. LOWPC is passed to
10562 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10563
10564 static void
10565 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10566 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10567 {
10568 struct dwarf2_per_objfile *dwarf2_per_objfile
10569 = cu->per_cu->dwarf2_per_objfile;
10570 struct attribute *attr;
10571 struct line_header line_header_local;
10572 hashval_t line_header_local_hash;
10573 void **slot;
10574 int decode_mapping;
10575
10576 gdb_assert (! cu->per_cu->is_debug_types);
10577
10578 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10579 if (attr == NULL)
10580 return;
10581
10582 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10583
10584 /* The line header hash table is only created if needed (it exists to
10585 prevent redundant reading of the line table for partial_units).
10586 If we're given a partial_unit, we'll need it. If we're given a
10587 compile_unit, then use the line header hash table if it's already
10588 created, but don't create one just yet. */
10589
10590 if (dwarf2_per_objfile->line_header_hash == NULL
10591 && die->tag == DW_TAG_partial_unit)
10592 {
10593 dwarf2_per_objfile->line_header_hash
10594 .reset (htab_create_alloc (127, line_header_hash_voidp,
10595 line_header_eq_voidp,
10596 free_line_header_voidp,
10597 xcalloc, xfree));
10598 }
10599
10600 line_header_local.sect_off = line_offset;
10601 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10602 line_header_local_hash = line_header_hash (&line_header_local);
10603 if (dwarf2_per_objfile->line_header_hash != NULL)
10604 {
10605 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10606 &line_header_local,
10607 line_header_local_hash, NO_INSERT);
10608
10609 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10610 is not present in *SLOT (since if there is something in *SLOT then
10611 it will be for a partial_unit). */
10612 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10613 {
10614 gdb_assert (*slot != NULL);
10615 cu->line_header = (struct line_header *) *slot;
10616 return;
10617 }
10618 }
10619
10620 /* dwarf_decode_line_header does not yet provide sufficient information.
10621 We always have to call also dwarf_decode_lines for it. */
10622 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10623 if (lh == NULL)
10624 return;
10625
10626 cu->line_header = lh.release ();
10627 cu->line_header_die_owner = die;
10628
10629 if (dwarf2_per_objfile->line_header_hash == NULL)
10630 slot = NULL;
10631 else
10632 {
10633 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
10634 &line_header_local,
10635 line_header_local_hash, INSERT);
10636 gdb_assert (slot != NULL);
10637 }
10638 if (slot != NULL && *slot == NULL)
10639 {
10640 /* This newly decoded line number information unit will be owned
10641 by line_header_hash hash table. */
10642 *slot = cu->line_header;
10643 cu->line_header_die_owner = NULL;
10644 }
10645 else
10646 {
10647 /* We cannot free any current entry in (*slot) as that struct line_header
10648 may be already used by multiple CUs. Create only temporary decoded
10649 line_header for this CU - it may happen at most once for each line
10650 number information unit. And if we're not using line_header_hash
10651 then this is what we want as well. */
10652 gdb_assert (die->tag != DW_TAG_partial_unit);
10653 }
10654 decode_mapping = (die->tag != DW_TAG_partial_unit);
10655 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10656 decode_mapping);
10657
10658 }
10659
10660 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10661
10662 static void
10663 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10664 {
10665 struct dwarf2_per_objfile *dwarf2_per_objfile
10666 = cu->per_cu->dwarf2_per_objfile;
10667 struct objfile *objfile = dwarf2_per_objfile->objfile;
10668 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10669 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10670 CORE_ADDR highpc = ((CORE_ADDR) 0);
10671 struct attribute *attr;
10672 struct die_info *child_die;
10673 CORE_ADDR baseaddr;
10674
10675 prepare_one_comp_unit (cu, die, cu->language);
10676 baseaddr = objfile->text_section_offset ();
10677
10678 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10679
10680 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10681 from finish_block. */
10682 if (lowpc == ((CORE_ADDR) -1))
10683 lowpc = highpc;
10684 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10685
10686 file_and_directory fnd = find_file_and_directory (die, cu);
10687
10688 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10689 standardised yet. As a workaround for the language detection we fall
10690 back to the DW_AT_producer string. */
10691 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10692 cu->language = language_opencl;
10693
10694 /* Similar hack for Go. */
10695 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10696 set_cu_language (DW_LANG_Go, cu);
10697
10698 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10699
10700 /* Decode line number information if present. We do this before
10701 processing child DIEs, so that the line header table is available
10702 for DW_AT_decl_file. */
10703 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10704
10705 /* Process all dies in compilation unit. */
10706 if (die->child != NULL)
10707 {
10708 child_die = die->child;
10709 while (child_die && child_die->tag)
10710 {
10711 process_die (child_die, cu);
10712 child_die = child_die->sibling;
10713 }
10714 }
10715
10716 /* Decode macro information, if present. Dwarf 2 macro information
10717 refers to information in the line number info statement program
10718 header, so we can only read it if we've read the header
10719 successfully. */
10720 attr = dwarf2_attr (die, DW_AT_macros, cu);
10721 if (attr == NULL)
10722 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10723 if (attr && cu->line_header)
10724 {
10725 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10726 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10727
10728 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
10729 }
10730 else
10731 {
10732 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10733 if (attr && cu->line_header)
10734 {
10735 unsigned int macro_offset = DW_UNSND (attr);
10736
10737 dwarf_decode_macros (cu, macro_offset, 0);
10738 }
10739 }
10740 }
10741
10742 void
10743 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10744 {
10745 struct type_unit_group *tu_group;
10746 int first_time;
10747 struct attribute *attr;
10748 unsigned int i;
10749 struct signatured_type *sig_type;
10750
10751 gdb_assert (per_cu->is_debug_types);
10752 sig_type = (struct signatured_type *) per_cu;
10753
10754 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10755
10756 /* If we're using .gdb_index (includes -readnow) then
10757 per_cu->type_unit_group may not have been set up yet. */
10758 if (sig_type->type_unit_group == NULL)
10759 sig_type->type_unit_group = get_type_unit_group (this, attr);
10760 tu_group = sig_type->type_unit_group;
10761
10762 /* If we've already processed this stmt_list there's no real need to
10763 do it again, we could fake it and just recreate the part we need
10764 (file name,index -> symtab mapping). If data shows this optimization
10765 is useful we can do it then. */
10766 first_time = tu_group->compunit_symtab == NULL;
10767
10768 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10769 debug info. */
10770 line_header_up lh;
10771 if (attr != NULL)
10772 {
10773 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10774 lh = dwarf_decode_line_header (line_offset, this);
10775 }
10776 if (lh == NULL)
10777 {
10778 if (first_time)
10779 start_symtab ("", NULL, 0);
10780 else
10781 {
10782 gdb_assert (tu_group->symtabs == NULL);
10783 gdb_assert (m_builder == nullptr);
10784 struct compunit_symtab *cust = tu_group->compunit_symtab;
10785 m_builder.reset (new struct buildsym_compunit
10786 (COMPUNIT_OBJFILE (cust), "",
10787 COMPUNIT_DIRNAME (cust),
10788 compunit_language (cust),
10789 0, cust));
10790 }
10791 return;
10792 }
10793
10794 line_header = lh.release ();
10795 line_header_die_owner = die;
10796
10797 if (first_time)
10798 {
10799 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
10800
10801 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10802 still initializing it, and our caller (a few levels up)
10803 process_full_type_unit still needs to know if this is the first
10804 time. */
10805
10806 tu_group->symtabs
10807 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10808 struct symtab *, line_header->file_names_size ());
10809
10810 auto &file_names = line_header->file_names ();
10811 for (i = 0; i < file_names.size (); ++i)
10812 {
10813 file_entry &fe = file_names[i];
10814 dwarf2_start_subfile (this, fe.name,
10815 fe.include_dir (line_header));
10816 buildsym_compunit *b = get_builder ();
10817 if (b->get_current_subfile ()->symtab == NULL)
10818 {
10819 /* NOTE: start_subfile will recognize when it's been
10820 passed a file it has already seen. So we can't
10821 assume there's a simple mapping from
10822 cu->line_header->file_names to subfiles, plus
10823 cu->line_header->file_names may contain dups. */
10824 b->get_current_subfile ()->symtab
10825 = allocate_symtab (cust, b->get_current_subfile ()->name);
10826 }
10827
10828 fe.symtab = b->get_current_subfile ()->symtab;
10829 tu_group->symtabs[i] = fe.symtab;
10830 }
10831 }
10832 else
10833 {
10834 gdb_assert (m_builder == nullptr);
10835 struct compunit_symtab *cust = tu_group->compunit_symtab;
10836 m_builder.reset (new struct buildsym_compunit
10837 (COMPUNIT_OBJFILE (cust), "",
10838 COMPUNIT_DIRNAME (cust),
10839 compunit_language (cust),
10840 0, cust));
10841
10842 auto &file_names = line_header->file_names ();
10843 for (i = 0; i < file_names.size (); ++i)
10844 {
10845 file_entry &fe = file_names[i];
10846 fe.symtab = tu_group->symtabs[i];
10847 }
10848 }
10849
10850 /* The main symtab is allocated last. Type units don't have DW_AT_name
10851 so they don't have a "real" (so to speak) symtab anyway.
10852 There is later code that will assign the main symtab to all symbols
10853 that don't have one. We need to handle the case of a symbol with a
10854 missing symtab (DW_AT_decl_file) anyway. */
10855 }
10856
10857 /* Process DW_TAG_type_unit.
10858 For TUs we want to skip the first top level sibling if it's not the
10859 actual type being defined by this TU. In this case the first top
10860 level sibling is there to provide context only. */
10861
10862 static void
10863 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10864 {
10865 struct die_info *child_die;
10866
10867 prepare_one_comp_unit (cu, die, language_minimal);
10868
10869 /* Initialize (or reinitialize) the machinery for building symtabs.
10870 We do this before processing child DIEs, so that the line header table
10871 is available for DW_AT_decl_file. */
10872 cu->setup_type_unit_groups (die);
10873
10874 if (die->child != NULL)
10875 {
10876 child_die = die->child;
10877 while (child_die && child_die->tag)
10878 {
10879 process_die (child_die, cu);
10880 child_die = child_die->sibling;
10881 }
10882 }
10883 }
10884 \f
10885 /* DWO/DWP files.
10886
10887 http://gcc.gnu.org/wiki/DebugFission
10888 http://gcc.gnu.org/wiki/DebugFissionDWP
10889
10890 To simplify handling of both DWO files ("object" files with the DWARF info)
10891 and DWP files (a file with the DWOs packaged up into one file), we treat
10892 DWP files as having a collection of virtual DWO files. */
10893
10894 static hashval_t
10895 hash_dwo_file (const void *item)
10896 {
10897 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
10898 hashval_t hash;
10899
10900 hash = htab_hash_string (dwo_file->dwo_name);
10901 if (dwo_file->comp_dir != NULL)
10902 hash += htab_hash_string (dwo_file->comp_dir);
10903 return hash;
10904 }
10905
10906 static int
10907 eq_dwo_file (const void *item_lhs, const void *item_rhs)
10908 {
10909 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10910 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
10911
10912 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10913 return 0;
10914 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10915 return lhs->comp_dir == rhs->comp_dir;
10916 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
10917 }
10918
10919 /* Allocate a hash table for DWO files. */
10920
10921 static htab_up
10922 allocate_dwo_file_hash_table ()
10923 {
10924 auto delete_dwo_file = [] (void *item)
10925 {
10926 struct dwo_file *dwo_file = (struct dwo_file *) item;
10927
10928 delete dwo_file;
10929 };
10930
10931 return htab_up (htab_create_alloc (41,
10932 hash_dwo_file,
10933 eq_dwo_file,
10934 delete_dwo_file,
10935 xcalloc, xfree));
10936 }
10937
10938 /* Lookup DWO file DWO_NAME. */
10939
10940 static void **
10941 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10942 const char *dwo_name,
10943 const char *comp_dir)
10944 {
10945 struct dwo_file find_entry;
10946 void **slot;
10947
10948 if (dwarf2_per_objfile->dwo_files == NULL)
10949 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
10950
10951 find_entry.dwo_name = dwo_name;
10952 find_entry.comp_dir = comp_dir;
10953 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10954 INSERT);
10955
10956 return slot;
10957 }
10958
10959 static hashval_t
10960 hash_dwo_unit (const void *item)
10961 {
10962 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
10963
10964 /* This drops the top 32 bits of the id, but is ok for a hash. */
10965 return dwo_unit->signature;
10966 }
10967
10968 static int
10969 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10970 {
10971 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10972 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
10973
10974 /* The signature is assumed to be unique within the DWO file.
10975 So while object file CU dwo_id's always have the value zero,
10976 that's OK, assuming each object file DWO file has only one CU,
10977 and that's the rule for now. */
10978 return lhs->signature == rhs->signature;
10979 }
10980
10981 /* Allocate a hash table for DWO CUs,TUs.
10982 There is one of these tables for each of CUs,TUs for each DWO file. */
10983
10984 static htab_up
10985 allocate_dwo_unit_table ()
10986 {
10987 /* Start out with a pretty small number.
10988 Generally DWO files contain only one CU and maybe some TUs. */
10989 return htab_up (htab_create_alloc (3,
10990 hash_dwo_unit,
10991 eq_dwo_unit,
10992 NULL, xcalloc, xfree));
10993 }
10994
10995 /* die_reader_func for create_dwo_cu. */
10996
10997 static void
10998 create_dwo_cu_reader (const struct die_reader_specs *reader,
10999 const gdb_byte *info_ptr,
11000 struct die_info *comp_unit_die,
11001 struct dwo_file *dwo_file,
11002 struct dwo_unit *dwo_unit)
11003 {
11004 struct dwarf2_cu *cu = reader->cu;
11005 sect_offset sect_off = cu->per_cu->sect_off;
11006 struct dwarf2_section_info *section = cu->per_cu->section;
11007
11008 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11009 if (!signature.has_value ())
11010 {
11011 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11012 " its dwo_id [in module %s]"),
11013 sect_offset_str (sect_off), dwo_file->dwo_name);
11014 return;
11015 }
11016
11017 dwo_unit->dwo_file = dwo_file;
11018 dwo_unit->signature = *signature;
11019 dwo_unit->section = section;
11020 dwo_unit->sect_off = sect_off;
11021 dwo_unit->length = cu->per_cu->length;
11022
11023 if (dwarf_read_debug)
11024 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11025 sect_offset_str (sect_off),
11026 hex_string (dwo_unit->signature));
11027 }
11028
11029 /* Create the dwo_units for the CUs in a DWO_FILE.
11030 Note: This function processes DWO files only, not DWP files. */
11031
11032 static void
11033 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11034 dwarf2_cu *cu, struct dwo_file &dwo_file,
11035 dwarf2_section_info &section, htab_up &cus_htab)
11036 {
11037 struct objfile *objfile = dwarf2_per_objfile->objfile;
11038 const gdb_byte *info_ptr, *end_ptr;
11039
11040 section.read (objfile);
11041 info_ptr = section.buffer;
11042
11043 if (info_ptr == NULL)
11044 return;
11045
11046 if (dwarf_read_debug)
11047 {
11048 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11049 section.get_name (),
11050 section.get_file_name ());
11051 }
11052
11053 end_ptr = info_ptr + section.size;
11054 while (info_ptr < end_ptr)
11055 {
11056 struct dwarf2_per_cu_data per_cu;
11057 struct dwo_unit read_unit {};
11058 struct dwo_unit *dwo_unit;
11059 void **slot;
11060 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11061
11062 memset (&per_cu, 0, sizeof (per_cu));
11063 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11064 per_cu.is_debug_types = 0;
11065 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11066 per_cu.section = &section;
11067
11068 cutu_reader reader (&per_cu, cu, &dwo_file);
11069 if (!reader.dummy_p)
11070 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11071 &dwo_file, &read_unit);
11072 info_ptr += per_cu.length;
11073
11074 // If the unit could not be parsed, skip it.
11075 if (read_unit.dwo_file == NULL)
11076 continue;
11077
11078 if (cus_htab == NULL)
11079 cus_htab = allocate_dwo_unit_table ();
11080
11081 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11082 *dwo_unit = read_unit;
11083 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11084 gdb_assert (slot != NULL);
11085 if (*slot != NULL)
11086 {
11087 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11088 sect_offset dup_sect_off = dup_cu->sect_off;
11089
11090 complaint (_("debug cu entry at offset %s is duplicate to"
11091 " the entry at offset %s, signature %s"),
11092 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11093 hex_string (dwo_unit->signature));
11094 }
11095 *slot = (void *)dwo_unit;
11096 }
11097 }
11098
11099 /* DWP file .debug_{cu,tu}_index section format:
11100 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11101
11102 DWP Version 1:
11103
11104 Both index sections have the same format, and serve to map a 64-bit
11105 signature to a set of section numbers. Each section begins with a header,
11106 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11107 indexes, and a pool of 32-bit section numbers. The index sections will be
11108 aligned at 8-byte boundaries in the file.
11109
11110 The index section header consists of:
11111
11112 V, 32 bit version number
11113 -, 32 bits unused
11114 N, 32 bit number of compilation units or type units in the index
11115 M, 32 bit number of slots in the hash table
11116
11117 Numbers are recorded using the byte order of the application binary.
11118
11119 The hash table begins at offset 16 in the section, and consists of an array
11120 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11121 order of the application binary). Unused slots in the hash table are 0.
11122 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11123
11124 The parallel table begins immediately after the hash table
11125 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11126 array of 32-bit indexes (using the byte order of the application binary),
11127 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11128 table contains a 32-bit index into the pool of section numbers. For unused
11129 hash table slots, the corresponding entry in the parallel table will be 0.
11130
11131 The pool of section numbers begins immediately following the hash table
11132 (at offset 16 + 12 * M from the beginning of the section). The pool of
11133 section numbers consists of an array of 32-bit words (using the byte order
11134 of the application binary). Each item in the array is indexed starting
11135 from 0. The hash table entry provides the index of the first section
11136 number in the set. Additional section numbers in the set follow, and the
11137 set is terminated by a 0 entry (section number 0 is not used in ELF).
11138
11139 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11140 section must be the first entry in the set, and the .debug_abbrev.dwo must
11141 be the second entry. Other members of the set may follow in any order.
11142
11143 ---
11144
11145 DWP Version 2:
11146
11147 DWP Version 2 combines all the .debug_info, etc. sections into one,
11148 and the entries in the index tables are now offsets into these sections.
11149 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11150 section.
11151
11152 Index Section Contents:
11153 Header
11154 Hash Table of Signatures dwp_hash_table.hash_table
11155 Parallel Table of Indices dwp_hash_table.unit_table
11156 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11157 Table of Section Sizes dwp_hash_table.v2.sizes
11158
11159 The index section header consists of:
11160
11161 V, 32 bit version number
11162 L, 32 bit number of columns in the table of section offsets
11163 N, 32 bit number of compilation units or type units in the index
11164 M, 32 bit number of slots in the hash table
11165
11166 Numbers are recorded using the byte order of the application binary.
11167
11168 The hash table has the same format as version 1.
11169 The parallel table of indices has the same format as version 1,
11170 except that the entries are origin-1 indices into the table of sections
11171 offsets and the table of section sizes.
11172
11173 The table of offsets begins immediately following the parallel table
11174 (at offset 16 + 12 * M from the beginning of the section). The table is
11175 a two-dimensional array of 32-bit words (using the byte order of the
11176 application binary), with L columns and N+1 rows, in row-major order.
11177 Each row in the array is indexed starting from 0. The first row provides
11178 a key to the remaining rows: each column in this row provides an identifier
11179 for a debug section, and the offsets in the same column of subsequent rows
11180 refer to that section. The section identifiers are:
11181
11182 DW_SECT_INFO 1 .debug_info.dwo
11183 DW_SECT_TYPES 2 .debug_types.dwo
11184 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11185 DW_SECT_LINE 4 .debug_line.dwo
11186 DW_SECT_LOC 5 .debug_loc.dwo
11187 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11188 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11189 DW_SECT_MACRO 8 .debug_macro.dwo
11190
11191 The offsets provided by the CU and TU index sections are the base offsets
11192 for the contributions made by each CU or TU to the corresponding section
11193 in the package file. Each CU and TU header contains an abbrev_offset
11194 field, used to find the abbreviations table for that CU or TU within the
11195 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11196 be interpreted as relative to the base offset given in the index section.
11197 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11198 should be interpreted as relative to the base offset for .debug_line.dwo,
11199 and offsets into other debug sections obtained from DWARF attributes should
11200 also be interpreted as relative to the corresponding base offset.
11201
11202 The table of sizes begins immediately following the table of offsets.
11203 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11204 with L columns and N rows, in row-major order. Each row in the array is
11205 indexed starting from 1 (row 0 is shared by the two tables).
11206
11207 ---
11208
11209 Hash table lookup is handled the same in version 1 and 2:
11210
11211 We assume that N and M will not exceed 2^32 - 1.
11212 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11213
11214 Given a 64-bit compilation unit signature or a type signature S, an entry
11215 in the hash table is located as follows:
11216
11217 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11218 the low-order k bits all set to 1.
11219
11220 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11221
11222 3) If the hash table entry at index H matches the signature, use that
11223 entry. If the hash table entry at index H is unused (all zeroes),
11224 terminate the search: the signature is not present in the table.
11225
11226 4) Let H = (H + H') modulo M. Repeat at Step 3.
11227
11228 Because M > N and H' and M are relatively prime, the search is guaranteed
11229 to stop at an unused slot or find the match. */
11230
11231 /* Create a hash table to map DWO IDs to their CU/TU entry in
11232 .debug_{info,types}.dwo in DWP_FILE.
11233 Returns NULL if there isn't one.
11234 Note: This function processes DWP files only, not DWO files. */
11235
11236 static struct dwp_hash_table *
11237 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11238 struct dwp_file *dwp_file, int is_debug_types)
11239 {
11240 struct objfile *objfile = dwarf2_per_objfile->objfile;
11241 bfd *dbfd = dwp_file->dbfd.get ();
11242 const gdb_byte *index_ptr, *index_end;
11243 struct dwarf2_section_info *index;
11244 uint32_t version, nr_columns, nr_units, nr_slots;
11245 struct dwp_hash_table *htab;
11246
11247 if (is_debug_types)
11248 index = &dwp_file->sections.tu_index;
11249 else
11250 index = &dwp_file->sections.cu_index;
11251
11252 if (index->empty ())
11253 return NULL;
11254 index->read (objfile);
11255
11256 index_ptr = index->buffer;
11257 index_end = index_ptr + index->size;
11258
11259 version = read_4_bytes (dbfd, index_ptr);
11260 index_ptr += 4;
11261 if (version == 2)
11262 nr_columns = read_4_bytes (dbfd, index_ptr);
11263 else
11264 nr_columns = 0;
11265 index_ptr += 4;
11266 nr_units = read_4_bytes (dbfd, index_ptr);
11267 index_ptr += 4;
11268 nr_slots = read_4_bytes (dbfd, index_ptr);
11269 index_ptr += 4;
11270
11271 if (version != 1 && version != 2)
11272 {
11273 error (_("Dwarf Error: unsupported DWP file version (%s)"
11274 " [in module %s]"),
11275 pulongest (version), dwp_file->name);
11276 }
11277 if (nr_slots != (nr_slots & -nr_slots))
11278 {
11279 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11280 " is not power of 2 [in module %s]"),
11281 pulongest (nr_slots), dwp_file->name);
11282 }
11283
11284 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
11285 htab->version = version;
11286 htab->nr_columns = nr_columns;
11287 htab->nr_units = nr_units;
11288 htab->nr_slots = nr_slots;
11289 htab->hash_table = index_ptr;
11290 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11291
11292 /* Exit early if the table is empty. */
11293 if (nr_slots == 0 || nr_units == 0
11294 || (version == 2 && nr_columns == 0))
11295 {
11296 /* All must be zero. */
11297 if (nr_slots != 0 || nr_units != 0
11298 || (version == 2 && nr_columns != 0))
11299 {
11300 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11301 " all zero [in modules %s]"),
11302 dwp_file->name);
11303 }
11304 return htab;
11305 }
11306
11307 if (version == 1)
11308 {
11309 htab->section_pool.v1.indices =
11310 htab->unit_table + sizeof (uint32_t) * nr_slots;
11311 /* It's harder to decide whether the section is too small in v1.
11312 V1 is deprecated anyway so we punt. */
11313 }
11314 else
11315 {
11316 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11317 int *ids = htab->section_pool.v2.section_ids;
11318 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11319 /* Reverse map for error checking. */
11320 int ids_seen[DW_SECT_MAX + 1];
11321 int i;
11322
11323 if (nr_columns < 2)
11324 {
11325 error (_("Dwarf Error: bad DWP hash table, too few columns"
11326 " in section table [in module %s]"),
11327 dwp_file->name);
11328 }
11329 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11330 {
11331 error (_("Dwarf Error: bad DWP hash table, too many columns"
11332 " in section table [in module %s]"),
11333 dwp_file->name);
11334 }
11335 memset (ids, 255, sizeof_ids);
11336 memset (ids_seen, 255, sizeof (ids_seen));
11337 for (i = 0; i < nr_columns; ++i)
11338 {
11339 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11340
11341 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11342 {
11343 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11344 " in section table [in module %s]"),
11345 id, dwp_file->name);
11346 }
11347 if (ids_seen[id] != -1)
11348 {
11349 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11350 " id %d in section table [in module %s]"),
11351 id, dwp_file->name);
11352 }
11353 ids_seen[id] = i;
11354 ids[i] = id;
11355 }
11356 /* Must have exactly one info or types section. */
11357 if (((ids_seen[DW_SECT_INFO] != -1)
11358 + (ids_seen[DW_SECT_TYPES] != -1))
11359 != 1)
11360 {
11361 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11362 " DWO info/types section [in module %s]"),
11363 dwp_file->name);
11364 }
11365 /* Must have an abbrev section. */
11366 if (ids_seen[DW_SECT_ABBREV] == -1)
11367 {
11368 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11369 " section [in module %s]"),
11370 dwp_file->name);
11371 }
11372 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11373 htab->section_pool.v2.sizes =
11374 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11375 * nr_units * nr_columns);
11376 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11377 * nr_units * nr_columns))
11378 > index_end)
11379 {
11380 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11381 " [in module %s]"),
11382 dwp_file->name);
11383 }
11384 }
11385
11386 return htab;
11387 }
11388
11389 /* Update SECTIONS with the data from SECTP.
11390
11391 This function is like the other "locate" section routines that are
11392 passed to bfd_map_over_sections, but in this context the sections to
11393 read comes from the DWP V1 hash table, not the full ELF section table.
11394
11395 The result is non-zero for success, or zero if an error was found. */
11396
11397 static int
11398 locate_v1_virtual_dwo_sections (asection *sectp,
11399 struct virtual_v1_dwo_sections *sections)
11400 {
11401 const struct dwop_section_names *names = &dwop_section_names;
11402
11403 if (section_is_p (sectp->name, &names->abbrev_dwo))
11404 {
11405 /* There can be only one. */
11406 if (sections->abbrev.s.section != NULL)
11407 return 0;
11408 sections->abbrev.s.section = sectp;
11409 sections->abbrev.size = bfd_section_size (sectp);
11410 }
11411 else if (section_is_p (sectp->name, &names->info_dwo)
11412 || section_is_p (sectp->name, &names->types_dwo))
11413 {
11414 /* There can be only one. */
11415 if (sections->info_or_types.s.section != NULL)
11416 return 0;
11417 sections->info_or_types.s.section = sectp;
11418 sections->info_or_types.size = bfd_section_size (sectp);
11419 }
11420 else if (section_is_p (sectp->name, &names->line_dwo))
11421 {
11422 /* There can be only one. */
11423 if (sections->line.s.section != NULL)
11424 return 0;
11425 sections->line.s.section = sectp;
11426 sections->line.size = bfd_section_size (sectp);
11427 }
11428 else if (section_is_p (sectp->name, &names->loc_dwo))
11429 {
11430 /* There can be only one. */
11431 if (sections->loc.s.section != NULL)
11432 return 0;
11433 sections->loc.s.section = sectp;
11434 sections->loc.size = bfd_section_size (sectp);
11435 }
11436 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11437 {
11438 /* There can be only one. */
11439 if (sections->macinfo.s.section != NULL)
11440 return 0;
11441 sections->macinfo.s.section = sectp;
11442 sections->macinfo.size = bfd_section_size (sectp);
11443 }
11444 else if (section_is_p (sectp->name, &names->macro_dwo))
11445 {
11446 /* There can be only one. */
11447 if (sections->macro.s.section != NULL)
11448 return 0;
11449 sections->macro.s.section = sectp;
11450 sections->macro.size = bfd_section_size (sectp);
11451 }
11452 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11453 {
11454 /* There can be only one. */
11455 if (sections->str_offsets.s.section != NULL)
11456 return 0;
11457 sections->str_offsets.s.section = sectp;
11458 sections->str_offsets.size = bfd_section_size (sectp);
11459 }
11460 else
11461 {
11462 /* No other kind of section is valid. */
11463 return 0;
11464 }
11465
11466 return 1;
11467 }
11468
11469 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11470 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11471 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11472 This is for DWP version 1 files. */
11473
11474 static struct dwo_unit *
11475 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11476 struct dwp_file *dwp_file,
11477 uint32_t unit_index,
11478 const char *comp_dir,
11479 ULONGEST signature, int is_debug_types)
11480 {
11481 struct objfile *objfile = dwarf2_per_objfile->objfile;
11482 const struct dwp_hash_table *dwp_htab =
11483 is_debug_types ? dwp_file->tus : dwp_file->cus;
11484 bfd *dbfd = dwp_file->dbfd.get ();
11485 const char *kind = is_debug_types ? "TU" : "CU";
11486 struct dwo_file *dwo_file;
11487 struct dwo_unit *dwo_unit;
11488 struct virtual_v1_dwo_sections sections;
11489 void **dwo_file_slot;
11490 int i;
11491
11492 gdb_assert (dwp_file->version == 1);
11493
11494 if (dwarf_read_debug)
11495 {
11496 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11497 kind,
11498 pulongest (unit_index), hex_string (signature),
11499 dwp_file->name);
11500 }
11501
11502 /* Fetch the sections of this DWO unit.
11503 Put a limit on the number of sections we look for so that bad data
11504 doesn't cause us to loop forever. */
11505
11506 #define MAX_NR_V1_DWO_SECTIONS \
11507 (1 /* .debug_info or .debug_types */ \
11508 + 1 /* .debug_abbrev */ \
11509 + 1 /* .debug_line */ \
11510 + 1 /* .debug_loc */ \
11511 + 1 /* .debug_str_offsets */ \
11512 + 1 /* .debug_macro or .debug_macinfo */ \
11513 + 1 /* trailing zero */)
11514
11515 memset (&sections, 0, sizeof (sections));
11516
11517 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11518 {
11519 asection *sectp;
11520 uint32_t section_nr =
11521 read_4_bytes (dbfd,
11522 dwp_htab->section_pool.v1.indices
11523 + (unit_index + i) * sizeof (uint32_t));
11524
11525 if (section_nr == 0)
11526 break;
11527 if (section_nr >= dwp_file->num_sections)
11528 {
11529 error (_("Dwarf Error: bad DWP hash table, section number too large"
11530 " [in module %s]"),
11531 dwp_file->name);
11532 }
11533
11534 sectp = dwp_file->elf_sections[section_nr];
11535 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11536 {
11537 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11538 " [in module %s]"),
11539 dwp_file->name);
11540 }
11541 }
11542
11543 if (i < 2
11544 || sections.info_or_types.empty ()
11545 || sections.abbrev.empty ())
11546 {
11547 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11548 " [in module %s]"),
11549 dwp_file->name);
11550 }
11551 if (i == MAX_NR_V1_DWO_SECTIONS)
11552 {
11553 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11554 " [in module %s]"),
11555 dwp_file->name);
11556 }
11557
11558 /* It's easier for the rest of the code if we fake a struct dwo_file and
11559 have dwo_unit "live" in that. At least for now.
11560
11561 The DWP file can be made up of a random collection of CUs and TUs.
11562 However, for each CU + set of TUs that came from the same original DWO
11563 file, we can combine them back into a virtual DWO file to save space
11564 (fewer struct dwo_file objects to allocate). Remember that for really
11565 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11566
11567 std::string virtual_dwo_name =
11568 string_printf ("virtual-dwo/%d-%d-%d-%d",
11569 sections.abbrev.get_id (),
11570 sections.line.get_id (),
11571 sections.loc.get_id (),
11572 sections.str_offsets.get_id ());
11573 /* Can we use an existing virtual DWO file? */
11574 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11575 virtual_dwo_name.c_str (),
11576 comp_dir);
11577 /* Create one if necessary. */
11578 if (*dwo_file_slot == NULL)
11579 {
11580 if (dwarf_read_debug)
11581 {
11582 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11583 virtual_dwo_name.c_str ());
11584 }
11585 dwo_file = new struct dwo_file;
11586 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11587 dwo_file->comp_dir = comp_dir;
11588 dwo_file->sections.abbrev = sections.abbrev;
11589 dwo_file->sections.line = sections.line;
11590 dwo_file->sections.loc = sections.loc;
11591 dwo_file->sections.macinfo = sections.macinfo;
11592 dwo_file->sections.macro = sections.macro;
11593 dwo_file->sections.str_offsets = sections.str_offsets;
11594 /* The "str" section is global to the entire DWP file. */
11595 dwo_file->sections.str = dwp_file->sections.str;
11596 /* The info or types section is assigned below to dwo_unit,
11597 there's no need to record it in dwo_file.
11598 Also, we can't simply record type sections in dwo_file because
11599 we record a pointer into the vector in dwo_unit. As we collect more
11600 types we'll grow the vector and eventually have to reallocate space
11601 for it, invalidating all copies of pointers into the previous
11602 contents. */
11603 *dwo_file_slot = dwo_file;
11604 }
11605 else
11606 {
11607 if (dwarf_read_debug)
11608 {
11609 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11610 virtual_dwo_name.c_str ());
11611 }
11612 dwo_file = (struct dwo_file *) *dwo_file_slot;
11613 }
11614
11615 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11616 dwo_unit->dwo_file = dwo_file;
11617 dwo_unit->signature = signature;
11618 dwo_unit->section =
11619 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11620 *dwo_unit->section = sections.info_or_types;
11621 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11622
11623 return dwo_unit;
11624 }
11625
11626 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11627 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11628 piece within that section used by a TU/CU, return a virtual section
11629 of just that piece. */
11630
11631 static struct dwarf2_section_info
11632 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11633 struct dwarf2_section_info *section,
11634 bfd_size_type offset, bfd_size_type size)
11635 {
11636 struct dwarf2_section_info result;
11637 asection *sectp;
11638
11639 gdb_assert (section != NULL);
11640 gdb_assert (!section->is_virtual);
11641
11642 memset (&result, 0, sizeof (result));
11643 result.s.containing_section = section;
11644 result.is_virtual = true;
11645
11646 if (size == 0)
11647 return result;
11648
11649 sectp = section->get_bfd_section ();
11650
11651 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11652 bounds of the real section. This is a pretty-rare event, so just
11653 flag an error (easier) instead of a warning and trying to cope. */
11654 if (sectp == NULL
11655 || offset + size > bfd_section_size (sectp))
11656 {
11657 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11658 " in section %s [in module %s]"),
11659 sectp ? bfd_section_name (sectp) : "<unknown>",
11660 objfile_name (dwarf2_per_objfile->objfile));
11661 }
11662
11663 result.virtual_offset = offset;
11664 result.size = size;
11665 return result;
11666 }
11667
11668 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11669 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11670 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11671 This is for DWP version 2 files. */
11672
11673 static struct dwo_unit *
11674 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11675 struct dwp_file *dwp_file,
11676 uint32_t unit_index,
11677 const char *comp_dir,
11678 ULONGEST signature, int is_debug_types)
11679 {
11680 struct objfile *objfile = dwarf2_per_objfile->objfile;
11681 const struct dwp_hash_table *dwp_htab =
11682 is_debug_types ? dwp_file->tus : dwp_file->cus;
11683 bfd *dbfd = dwp_file->dbfd.get ();
11684 const char *kind = is_debug_types ? "TU" : "CU";
11685 struct dwo_file *dwo_file;
11686 struct dwo_unit *dwo_unit;
11687 struct virtual_v2_dwo_sections sections;
11688 void **dwo_file_slot;
11689 int i;
11690
11691 gdb_assert (dwp_file->version == 2);
11692
11693 if (dwarf_read_debug)
11694 {
11695 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11696 kind,
11697 pulongest (unit_index), hex_string (signature),
11698 dwp_file->name);
11699 }
11700
11701 /* Fetch the section offsets of this DWO unit. */
11702
11703 memset (&sections, 0, sizeof (sections));
11704
11705 for (i = 0; i < dwp_htab->nr_columns; ++i)
11706 {
11707 uint32_t offset = read_4_bytes (dbfd,
11708 dwp_htab->section_pool.v2.offsets
11709 + (((unit_index - 1) * dwp_htab->nr_columns
11710 + i)
11711 * sizeof (uint32_t)));
11712 uint32_t size = read_4_bytes (dbfd,
11713 dwp_htab->section_pool.v2.sizes
11714 + (((unit_index - 1) * dwp_htab->nr_columns
11715 + i)
11716 * sizeof (uint32_t)));
11717
11718 switch (dwp_htab->section_pool.v2.section_ids[i])
11719 {
11720 case DW_SECT_INFO:
11721 case DW_SECT_TYPES:
11722 sections.info_or_types_offset = offset;
11723 sections.info_or_types_size = size;
11724 break;
11725 case DW_SECT_ABBREV:
11726 sections.abbrev_offset = offset;
11727 sections.abbrev_size = size;
11728 break;
11729 case DW_SECT_LINE:
11730 sections.line_offset = offset;
11731 sections.line_size = size;
11732 break;
11733 case DW_SECT_LOC:
11734 sections.loc_offset = offset;
11735 sections.loc_size = size;
11736 break;
11737 case DW_SECT_STR_OFFSETS:
11738 sections.str_offsets_offset = offset;
11739 sections.str_offsets_size = size;
11740 break;
11741 case DW_SECT_MACINFO:
11742 sections.macinfo_offset = offset;
11743 sections.macinfo_size = size;
11744 break;
11745 case DW_SECT_MACRO:
11746 sections.macro_offset = offset;
11747 sections.macro_size = size;
11748 break;
11749 }
11750 }
11751
11752 /* It's easier for the rest of the code if we fake a struct dwo_file and
11753 have dwo_unit "live" in that. At least for now.
11754
11755 The DWP file can be made up of a random collection of CUs and TUs.
11756 However, for each CU + set of TUs that came from the same original DWO
11757 file, we can combine them back into a virtual DWO file to save space
11758 (fewer struct dwo_file objects to allocate). Remember that for really
11759 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11760
11761 std::string virtual_dwo_name =
11762 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11763 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11764 (long) (sections.line_size ? sections.line_offset : 0),
11765 (long) (sections.loc_size ? sections.loc_offset : 0),
11766 (long) (sections.str_offsets_size
11767 ? sections.str_offsets_offset : 0));
11768 /* Can we use an existing virtual DWO file? */
11769 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11770 virtual_dwo_name.c_str (),
11771 comp_dir);
11772 /* Create one if necessary. */
11773 if (*dwo_file_slot == NULL)
11774 {
11775 if (dwarf_read_debug)
11776 {
11777 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11778 virtual_dwo_name.c_str ());
11779 }
11780 dwo_file = new struct dwo_file;
11781 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
11782 dwo_file->comp_dir = comp_dir;
11783 dwo_file->sections.abbrev =
11784 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
11785 sections.abbrev_offset, sections.abbrev_size);
11786 dwo_file->sections.line =
11787 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
11788 sections.line_offset, sections.line_size);
11789 dwo_file->sections.loc =
11790 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
11791 sections.loc_offset, sections.loc_size);
11792 dwo_file->sections.macinfo =
11793 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
11794 sections.macinfo_offset, sections.macinfo_size);
11795 dwo_file->sections.macro =
11796 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
11797 sections.macro_offset, sections.macro_size);
11798 dwo_file->sections.str_offsets =
11799 create_dwp_v2_section (dwarf2_per_objfile,
11800 &dwp_file->sections.str_offsets,
11801 sections.str_offsets_offset,
11802 sections.str_offsets_size);
11803 /* The "str" section is global to the entire DWP file. */
11804 dwo_file->sections.str = dwp_file->sections.str;
11805 /* The info or types section is assigned below to dwo_unit,
11806 there's no need to record it in dwo_file.
11807 Also, we can't simply record type sections in dwo_file because
11808 we record a pointer into the vector in dwo_unit. As we collect more
11809 types we'll grow the vector and eventually have to reallocate space
11810 for it, invalidating all copies of pointers into the previous
11811 contents. */
11812 *dwo_file_slot = dwo_file;
11813 }
11814 else
11815 {
11816 if (dwarf_read_debug)
11817 {
11818 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11819 virtual_dwo_name.c_str ());
11820 }
11821 dwo_file = (struct dwo_file *) *dwo_file_slot;
11822 }
11823
11824 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11825 dwo_unit->dwo_file = dwo_file;
11826 dwo_unit->signature = signature;
11827 dwo_unit->section =
11828 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
11829 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11830 is_debug_types
11831 ? &dwp_file->sections.types
11832 : &dwp_file->sections.info,
11833 sections.info_or_types_offset,
11834 sections.info_or_types_size);
11835 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11836
11837 return dwo_unit;
11838 }
11839
11840 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11841 Returns NULL if the signature isn't found. */
11842
11843 static struct dwo_unit *
11844 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11845 struct dwp_file *dwp_file, const char *comp_dir,
11846 ULONGEST signature, int is_debug_types)
11847 {
11848 const struct dwp_hash_table *dwp_htab =
11849 is_debug_types ? dwp_file->tus : dwp_file->cus;
11850 bfd *dbfd = dwp_file->dbfd.get ();
11851 uint32_t mask = dwp_htab->nr_slots - 1;
11852 uint32_t hash = signature & mask;
11853 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11854 unsigned int i;
11855 void **slot;
11856 struct dwo_unit find_dwo_cu;
11857
11858 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11859 find_dwo_cu.signature = signature;
11860 slot = htab_find_slot (is_debug_types
11861 ? dwp_file->loaded_tus.get ()
11862 : dwp_file->loaded_cus.get (),
11863 &find_dwo_cu, INSERT);
11864
11865 if (*slot != NULL)
11866 return (struct dwo_unit *) *slot;
11867
11868 /* Use a for loop so that we don't loop forever on bad debug info. */
11869 for (i = 0; i < dwp_htab->nr_slots; ++i)
11870 {
11871 ULONGEST signature_in_table;
11872
11873 signature_in_table =
11874 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
11875 if (signature_in_table == signature)
11876 {
11877 uint32_t unit_index =
11878 read_4_bytes (dbfd,
11879 dwp_htab->unit_table + hash * sizeof (uint32_t));
11880
11881 if (dwp_file->version == 1)
11882 {
11883 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11884 dwp_file, unit_index,
11885 comp_dir, signature,
11886 is_debug_types);
11887 }
11888 else
11889 {
11890 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11891 dwp_file, unit_index,
11892 comp_dir, signature,
11893 is_debug_types);
11894 }
11895 return (struct dwo_unit *) *slot;
11896 }
11897 if (signature_in_table == 0)
11898 return NULL;
11899 hash = (hash + hash2) & mask;
11900 }
11901
11902 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11903 " [in module %s]"),
11904 dwp_file->name);
11905 }
11906
11907 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
11908 Open the file specified by FILE_NAME and hand it off to BFD for
11909 preliminary analysis. Return a newly initialized bfd *, which
11910 includes a canonicalized copy of FILE_NAME.
11911 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
11912 SEARCH_CWD is true if the current directory is to be searched.
11913 It will be searched before debug-file-directory.
11914 If successful, the file is added to the bfd include table of the
11915 objfile's bfd (see gdb_bfd_record_inclusion).
11916 If unable to find/open the file, return NULL.
11917 NOTE: This function is derived from symfile_bfd_open. */
11918
11919 static gdb_bfd_ref_ptr
11920 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11921 const char *file_name, int is_dwp, int search_cwd)
11922 {
11923 int desc;
11924 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11925 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11926 to debug_file_directory. */
11927 const char *search_path;
11928 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11929
11930 gdb::unique_xmalloc_ptr<char> search_path_holder;
11931 if (search_cwd)
11932 {
11933 if (*debug_file_directory != '\0')
11934 {
11935 search_path_holder.reset (concat (".", dirname_separator_string,
11936 debug_file_directory,
11937 (char *) NULL));
11938 search_path = search_path_holder.get ();
11939 }
11940 else
11941 search_path = ".";
11942 }
11943 else
11944 search_path = debug_file_directory;
11945
11946 openp_flags flags = OPF_RETURN_REALPATH;
11947 if (is_dwp)
11948 flags |= OPF_SEARCH_IN_PATH;
11949
11950 gdb::unique_xmalloc_ptr<char> absolute_name;
11951 desc = openp (search_path, flags, file_name,
11952 O_RDONLY | O_BINARY, &absolute_name);
11953 if (desc < 0)
11954 return NULL;
11955
11956 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11957 gnutarget, desc));
11958 if (sym_bfd == NULL)
11959 return NULL;
11960 bfd_set_cacheable (sym_bfd.get (), 1);
11961
11962 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11963 return NULL;
11964
11965 /* Success. Record the bfd as having been included by the objfile's bfd.
11966 This is important because things like demangled_names_hash lives in the
11967 objfile's per_bfd space and may have references to things like symbol
11968 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
11969 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
11970
11971 return sym_bfd;
11972 }
11973
11974 /* Try to open DWO file FILE_NAME.
11975 COMP_DIR is the DW_AT_comp_dir attribute.
11976 The result is the bfd handle of the file.
11977 If there is a problem finding or opening the file, return NULL.
11978 Upon success, the canonicalized path of the file is stored in the bfd,
11979 same as symfile_bfd_open. */
11980
11981 static gdb_bfd_ref_ptr
11982 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11983 const char *file_name, const char *comp_dir)
11984 {
11985 if (IS_ABSOLUTE_PATH (file_name))
11986 return try_open_dwop_file (dwarf2_per_objfile, file_name,
11987 0 /*is_dwp*/, 0 /*search_cwd*/);
11988
11989 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
11990
11991 if (comp_dir != NULL)
11992 {
11993 gdb::unique_xmalloc_ptr<char> path_to_try
11994 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
11995
11996 /* NOTE: If comp_dir is a relative path, this will also try the
11997 search path, which seems useful. */
11998 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
11999 path_to_try.get (),
12000 0 /*is_dwp*/,
12001 1 /*search_cwd*/));
12002 if (abfd != NULL)
12003 return abfd;
12004 }
12005
12006 /* That didn't work, try debug-file-directory, which, despite its name,
12007 is a list of paths. */
12008
12009 if (*debug_file_directory == '\0')
12010 return NULL;
12011
12012 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12013 0 /*is_dwp*/, 1 /*search_cwd*/);
12014 }
12015
12016 /* This function is mapped across the sections and remembers the offset and
12017 size of each of the DWO debugging sections we are interested in. */
12018
12019 static void
12020 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12021 {
12022 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12023 const struct dwop_section_names *names = &dwop_section_names;
12024
12025 if (section_is_p (sectp->name, &names->abbrev_dwo))
12026 {
12027 dwo_sections->abbrev.s.section = sectp;
12028 dwo_sections->abbrev.size = bfd_section_size (sectp);
12029 }
12030 else if (section_is_p (sectp->name, &names->info_dwo))
12031 {
12032 dwo_sections->info.s.section = sectp;
12033 dwo_sections->info.size = bfd_section_size (sectp);
12034 }
12035 else if (section_is_p (sectp->name, &names->line_dwo))
12036 {
12037 dwo_sections->line.s.section = sectp;
12038 dwo_sections->line.size = bfd_section_size (sectp);
12039 }
12040 else if (section_is_p (sectp->name, &names->loc_dwo))
12041 {
12042 dwo_sections->loc.s.section = sectp;
12043 dwo_sections->loc.size = bfd_section_size (sectp);
12044 }
12045 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12046 {
12047 dwo_sections->macinfo.s.section = sectp;
12048 dwo_sections->macinfo.size = bfd_section_size (sectp);
12049 }
12050 else if (section_is_p (sectp->name, &names->macro_dwo))
12051 {
12052 dwo_sections->macro.s.section = sectp;
12053 dwo_sections->macro.size = bfd_section_size (sectp);
12054 }
12055 else if (section_is_p (sectp->name, &names->str_dwo))
12056 {
12057 dwo_sections->str.s.section = sectp;
12058 dwo_sections->str.size = bfd_section_size (sectp);
12059 }
12060 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12061 {
12062 dwo_sections->str_offsets.s.section = sectp;
12063 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12064 }
12065 else if (section_is_p (sectp->name, &names->types_dwo))
12066 {
12067 struct dwarf2_section_info type_section;
12068
12069 memset (&type_section, 0, sizeof (type_section));
12070 type_section.s.section = sectp;
12071 type_section.size = bfd_section_size (sectp);
12072 dwo_sections->types.push_back (type_section);
12073 }
12074 }
12075
12076 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12077 by PER_CU. This is for the non-DWP case.
12078 The result is NULL if DWO_NAME can't be found. */
12079
12080 static struct dwo_file *
12081 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12082 const char *dwo_name, const char *comp_dir)
12083 {
12084 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12085
12086 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12087 if (dbfd == NULL)
12088 {
12089 if (dwarf_read_debug)
12090 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12091 return NULL;
12092 }
12093
12094 dwo_file_up dwo_file (new struct dwo_file);
12095 dwo_file->dwo_name = dwo_name;
12096 dwo_file->comp_dir = comp_dir;
12097 dwo_file->dbfd = std::move (dbfd);
12098
12099 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12100 &dwo_file->sections);
12101
12102 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12103 dwo_file->sections.info, dwo_file->cus);
12104
12105 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12106 dwo_file->sections.types, dwo_file->tus);
12107
12108 if (dwarf_read_debug)
12109 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12110
12111 return dwo_file.release ();
12112 }
12113
12114 /* This function is mapped across the sections and remembers the offset and
12115 size of each of the DWP debugging sections common to version 1 and 2 that
12116 we are interested in. */
12117
12118 static void
12119 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12120 void *dwp_file_ptr)
12121 {
12122 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12123 const struct dwop_section_names *names = &dwop_section_names;
12124 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12125
12126 /* Record the ELF section number for later lookup: this is what the
12127 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12128 gdb_assert (elf_section_nr < dwp_file->num_sections);
12129 dwp_file->elf_sections[elf_section_nr] = sectp;
12130
12131 /* Look for specific sections that we need. */
12132 if (section_is_p (sectp->name, &names->str_dwo))
12133 {
12134 dwp_file->sections.str.s.section = sectp;
12135 dwp_file->sections.str.size = bfd_section_size (sectp);
12136 }
12137 else if (section_is_p (sectp->name, &names->cu_index))
12138 {
12139 dwp_file->sections.cu_index.s.section = sectp;
12140 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12141 }
12142 else if (section_is_p (sectp->name, &names->tu_index))
12143 {
12144 dwp_file->sections.tu_index.s.section = sectp;
12145 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12146 }
12147 }
12148
12149 /* This function is mapped across the sections and remembers the offset and
12150 size of each of the DWP version 2 debugging sections that we are interested
12151 in. This is split into a separate function because we don't know if we
12152 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12153
12154 static void
12155 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12156 {
12157 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12158 const struct dwop_section_names *names = &dwop_section_names;
12159 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12160
12161 /* Record the ELF section number for later lookup: this is what the
12162 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12163 gdb_assert (elf_section_nr < dwp_file->num_sections);
12164 dwp_file->elf_sections[elf_section_nr] = sectp;
12165
12166 /* Look for specific sections that we need. */
12167 if (section_is_p (sectp->name, &names->abbrev_dwo))
12168 {
12169 dwp_file->sections.abbrev.s.section = sectp;
12170 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12171 }
12172 else if (section_is_p (sectp->name, &names->info_dwo))
12173 {
12174 dwp_file->sections.info.s.section = sectp;
12175 dwp_file->sections.info.size = bfd_section_size (sectp);
12176 }
12177 else if (section_is_p (sectp->name, &names->line_dwo))
12178 {
12179 dwp_file->sections.line.s.section = sectp;
12180 dwp_file->sections.line.size = bfd_section_size (sectp);
12181 }
12182 else if (section_is_p (sectp->name, &names->loc_dwo))
12183 {
12184 dwp_file->sections.loc.s.section = sectp;
12185 dwp_file->sections.loc.size = bfd_section_size (sectp);
12186 }
12187 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12188 {
12189 dwp_file->sections.macinfo.s.section = sectp;
12190 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12191 }
12192 else if (section_is_p (sectp->name, &names->macro_dwo))
12193 {
12194 dwp_file->sections.macro.s.section = sectp;
12195 dwp_file->sections.macro.size = bfd_section_size (sectp);
12196 }
12197 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12198 {
12199 dwp_file->sections.str_offsets.s.section = sectp;
12200 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12201 }
12202 else if (section_is_p (sectp->name, &names->types_dwo))
12203 {
12204 dwp_file->sections.types.s.section = sectp;
12205 dwp_file->sections.types.size = bfd_section_size (sectp);
12206 }
12207 }
12208
12209 /* Hash function for dwp_file loaded CUs/TUs. */
12210
12211 static hashval_t
12212 hash_dwp_loaded_cutus (const void *item)
12213 {
12214 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12215
12216 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12217 return dwo_unit->signature;
12218 }
12219
12220 /* Equality function for dwp_file loaded CUs/TUs. */
12221
12222 static int
12223 eq_dwp_loaded_cutus (const void *a, const void *b)
12224 {
12225 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12226 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12227
12228 return dua->signature == dub->signature;
12229 }
12230
12231 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12232
12233 static htab_up
12234 allocate_dwp_loaded_cutus_table ()
12235 {
12236 return htab_up (htab_create_alloc (3,
12237 hash_dwp_loaded_cutus,
12238 eq_dwp_loaded_cutus,
12239 NULL, xcalloc, xfree));
12240 }
12241
12242 /* Try to open DWP file FILE_NAME.
12243 The result is the bfd handle of the file.
12244 If there is a problem finding or opening the file, return NULL.
12245 Upon success, the canonicalized path of the file is stored in the bfd,
12246 same as symfile_bfd_open. */
12247
12248 static gdb_bfd_ref_ptr
12249 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12250 const char *file_name)
12251 {
12252 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12253 1 /*is_dwp*/,
12254 1 /*search_cwd*/));
12255 if (abfd != NULL)
12256 return abfd;
12257
12258 /* Work around upstream bug 15652.
12259 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12260 [Whether that's a "bug" is debatable, but it is getting in our way.]
12261 We have no real idea where the dwp file is, because gdb's realpath-ing
12262 of the executable's path may have discarded the needed info.
12263 [IWBN if the dwp file name was recorded in the executable, akin to
12264 .gnu_debuglink, but that doesn't exist yet.]
12265 Strip the directory from FILE_NAME and search again. */
12266 if (*debug_file_directory != '\0')
12267 {
12268 /* Don't implicitly search the current directory here.
12269 If the user wants to search "." to handle this case,
12270 it must be added to debug-file-directory. */
12271 return try_open_dwop_file (dwarf2_per_objfile,
12272 lbasename (file_name), 1 /*is_dwp*/,
12273 0 /*search_cwd*/);
12274 }
12275
12276 return NULL;
12277 }
12278
12279 /* Initialize the use of the DWP file for the current objfile.
12280 By convention the name of the DWP file is ${objfile}.dwp.
12281 The result is NULL if it can't be found. */
12282
12283 static std::unique_ptr<struct dwp_file>
12284 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12285 {
12286 struct objfile *objfile = dwarf2_per_objfile->objfile;
12287
12288 /* Try to find first .dwp for the binary file before any symbolic links
12289 resolving. */
12290
12291 /* If the objfile is a debug file, find the name of the real binary
12292 file and get the name of dwp file from there. */
12293 std::string dwp_name;
12294 if (objfile->separate_debug_objfile_backlink != NULL)
12295 {
12296 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12297 const char *backlink_basename = lbasename (backlink->original_name);
12298
12299 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12300 }
12301 else
12302 dwp_name = objfile->original_name;
12303
12304 dwp_name += ".dwp";
12305
12306 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12307 if (dbfd == NULL
12308 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12309 {
12310 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12311 dwp_name = objfile_name (objfile);
12312 dwp_name += ".dwp";
12313 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12314 }
12315
12316 if (dbfd == NULL)
12317 {
12318 if (dwarf_read_debug)
12319 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12320 return std::unique_ptr<dwp_file> ();
12321 }
12322
12323 const char *name = bfd_get_filename (dbfd.get ());
12324 std::unique_ptr<struct dwp_file> dwp_file
12325 (new struct dwp_file (name, std::move (dbfd)));
12326
12327 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12328 dwp_file->elf_sections =
12329 OBSTACK_CALLOC (&objfile->objfile_obstack,
12330 dwp_file->num_sections, asection *);
12331
12332 bfd_map_over_sections (dwp_file->dbfd.get (),
12333 dwarf2_locate_common_dwp_sections,
12334 dwp_file.get ());
12335
12336 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12337 0);
12338
12339 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12340 1);
12341
12342 /* The DWP file version is stored in the hash table. Oh well. */
12343 if (dwp_file->cus && dwp_file->tus
12344 && dwp_file->cus->version != dwp_file->tus->version)
12345 {
12346 /* Technically speaking, we should try to limp along, but this is
12347 pretty bizarre. We use pulongest here because that's the established
12348 portability solution (e.g, we cannot use %u for uint32_t). */
12349 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12350 " TU version %s [in DWP file %s]"),
12351 pulongest (dwp_file->cus->version),
12352 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12353 }
12354
12355 if (dwp_file->cus)
12356 dwp_file->version = dwp_file->cus->version;
12357 else if (dwp_file->tus)
12358 dwp_file->version = dwp_file->tus->version;
12359 else
12360 dwp_file->version = 2;
12361
12362 if (dwp_file->version == 2)
12363 bfd_map_over_sections (dwp_file->dbfd.get (),
12364 dwarf2_locate_v2_dwp_sections,
12365 dwp_file.get ());
12366
12367 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12368 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12369
12370 if (dwarf_read_debug)
12371 {
12372 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12373 fprintf_unfiltered (gdb_stdlog,
12374 " %s CUs, %s TUs\n",
12375 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12376 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12377 }
12378
12379 return dwp_file;
12380 }
12381
12382 /* Wrapper around open_and_init_dwp_file, only open it once. */
12383
12384 static struct dwp_file *
12385 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12386 {
12387 if (! dwarf2_per_objfile->dwp_checked)
12388 {
12389 dwarf2_per_objfile->dwp_file
12390 = open_and_init_dwp_file (dwarf2_per_objfile);
12391 dwarf2_per_objfile->dwp_checked = 1;
12392 }
12393 return dwarf2_per_objfile->dwp_file.get ();
12394 }
12395
12396 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12397 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12398 or in the DWP file for the objfile, referenced by THIS_UNIT.
12399 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12400 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12401
12402 This is called, for example, when wanting to read a variable with a
12403 complex location. Therefore we don't want to do file i/o for every call.
12404 Therefore we don't want to look for a DWO file on every call.
12405 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12406 then we check if we've already seen DWO_NAME, and only THEN do we check
12407 for a DWO file.
12408
12409 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12410 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12411
12412 static struct dwo_unit *
12413 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12414 const char *dwo_name, const char *comp_dir,
12415 ULONGEST signature, int is_debug_types)
12416 {
12417 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12418 struct objfile *objfile = dwarf2_per_objfile->objfile;
12419 const char *kind = is_debug_types ? "TU" : "CU";
12420 void **dwo_file_slot;
12421 struct dwo_file *dwo_file;
12422 struct dwp_file *dwp_file;
12423
12424 /* First see if there's a DWP file.
12425 If we have a DWP file but didn't find the DWO inside it, don't
12426 look for the original DWO file. It makes gdb behave differently
12427 depending on whether one is debugging in the build tree. */
12428
12429 dwp_file = get_dwp_file (dwarf2_per_objfile);
12430 if (dwp_file != NULL)
12431 {
12432 const struct dwp_hash_table *dwp_htab =
12433 is_debug_types ? dwp_file->tus : dwp_file->cus;
12434
12435 if (dwp_htab != NULL)
12436 {
12437 struct dwo_unit *dwo_cutu =
12438 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12439 signature, is_debug_types);
12440
12441 if (dwo_cutu != NULL)
12442 {
12443 if (dwarf_read_debug)
12444 {
12445 fprintf_unfiltered (gdb_stdlog,
12446 "Virtual DWO %s %s found: @%s\n",
12447 kind, hex_string (signature),
12448 host_address_to_string (dwo_cutu));
12449 }
12450 return dwo_cutu;
12451 }
12452 }
12453 }
12454 else
12455 {
12456 /* No DWP file, look for the DWO file. */
12457
12458 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12459 dwo_name, comp_dir);
12460 if (*dwo_file_slot == NULL)
12461 {
12462 /* Read in the file and build a table of the CUs/TUs it contains. */
12463 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12464 }
12465 /* NOTE: This will be NULL if unable to open the file. */
12466 dwo_file = (struct dwo_file *) *dwo_file_slot;
12467
12468 if (dwo_file != NULL)
12469 {
12470 struct dwo_unit *dwo_cutu = NULL;
12471
12472 if (is_debug_types && dwo_file->tus)
12473 {
12474 struct dwo_unit find_dwo_cutu;
12475
12476 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12477 find_dwo_cutu.signature = signature;
12478 dwo_cutu
12479 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12480 &find_dwo_cutu);
12481 }
12482 else if (!is_debug_types && dwo_file->cus)
12483 {
12484 struct dwo_unit find_dwo_cutu;
12485
12486 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12487 find_dwo_cutu.signature = signature;
12488 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12489 &find_dwo_cutu);
12490 }
12491
12492 if (dwo_cutu != NULL)
12493 {
12494 if (dwarf_read_debug)
12495 {
12496 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12497 kind, dwo_name, hex_string (signature),
12498 host_address_to_string (dwo_cutu));
12499 }
12500 return dwo_cutu;
12501 }
12502 }
12503 }
12504
12505 /* We didn't find it. This could mean a dwo_id mismatch, or
12506 someone deleted the DWO/DWP file, or the search path isn't set up
12507 correctly to find the file. */
12508
12509 if (dwarf_read_debug)
12510 {
12511 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12512 kind, dwo_name, hex_string (signature));
12513 }
12514
12515 /* This is a warning and not a complaint because it can be caused by
12516 pilot error (e.g., user accidentally deleting the DWO). */
12517 {
12518 /* Print the name of the DWP file if we looked there, helps the user
12519 better diagnose the problem. */
12520 std::string dwp_text;
12521
12522 if (dwp_file != NULL)
12523 dwp_text = string_printf (" [in DWP file %s]",
12524 lbasename (dwp_file->name));
12525
12526 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12527 " [in module %s]"),
12528 kind, dwo_name, hex_string (signature),
12529 dwp_text.c_str (),
12530 this_unit->is_debug_types ? "TU" : "CU",
12531 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12532 }
12533 return NULL;
12534 }
12535
12536 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12537 See lookup_dwo_cutu_unit for details. */
12538
12539 static struct dwo_unit *
12540 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12541 const char *dwo_name, const char *comp_dir,
12542 ULONGEST signature)
12543 {
12544 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12545 }
12546
12547 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12548 See lookup_dwo_cutu_unit for details. */
12549
12550 static struct dwo_unit *
12551 lookup_dwo_type_unit (struct signatured_type *this_tu,
12552 const char *dwo_name, const char *comp_dir)
12553 {
12554 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12555 }
12556
12557 /* Traversal function for queue_and_load_all_dwo_tus. */
12558
12559 static int
12560 queue_and_load_dwo_tu (void **slot, void *info)
12561 {
12562 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12563 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12564 ULONGEST signature = dwo_unit->signature;
12565 struct signatured_type *sig_type =
12566 lookup_dwo_signatured_type (per_cu->cu, signature);
12567
12568 if (sig_type != NULL)
12569 {
12570 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12571
12572 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12573 a real dependency of PER_CU on SIG_TYPE. That is detected later
12574 while processing PER_CU. */
12575 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12576 load_full_type_unit (sig_cu);
12577 per_cu->imported_symtabs_push (sig_cu);
12578 }
12579
12580 return 1;
12581 }
12582
12583 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12584 The DWO may have the only definition of the type, though it may not be
12585 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12586 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12587
12588 static void
12589 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12590 {
12591 struct dwo_unit *dwo_unit;
12592 struct dwo_file *dwo_file;
12593
12594 gdb_assert (!per_cu->is_debug_types);
12595 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12596 gdb_assert (per_cu->cu != NULL);
12597
12598 dwo_unit = per_cu->cu->dwo_unit;
12599 gdb_assert (dwo_unit != NULL);
12600
12601 dwo_file = dwo_unit->dwo_file;
12602 if (dwo_file->tus != NULL)
12603 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12604 per_cu);
12605 }
12606
12607 /* Read in various DIEs. */
12608
12609 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12610 Inherit only the children of the DW_AT_abstract_origin DIE not being
12611 already referenced by DW_AT_abstract_origin from the children of the
12612 current DIE. */
12613
12614 static void
12615 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12616 {
12617 struct die_info *child_die;
12618 sect_offset *offsetp;
12619 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12620 struct die_info *origin_die;
12621 /* Iterator of the ORIGIN_DIE children. */
12622 struct die_info *origin_child_die;
12623 struct attribute *attr;
12624 struct dwarf2_cu *origin_cu;
12625 struct pending **origin_previous_list_in_scope;
12626
12627 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12628 if (!attr)
12629 return;
12630
12631 /* Note that following die references may follow to a die in a
12632 different cu. */
12633
12634 origin_cu = cu;
12635 origin_die = follow_die_ref (die, attr, &origin_cu);
12636
12637 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12638 symbols in. */
12639 origin_previous_list_in_scope = origin_cu->list_in_scope;
12640 origin_cu->list_in_scope = cu->list_in_scope;
12641
12642 if (die->tag != origin_die->tag
12643 && !(die->tag == DW_TAG_inlined_subroutine
12644 && origin_die->tag == DW_TAG_subprogram))
12645 complaint (_("DIE %s and its abstract origin %s have different tags"),
12646 sect_offset_str (die->sect_off),
12647 sect_offset_str (origin_die->sect_off));
12648
12649 std::vector<sect_offset> offsets;
12650
12651 for (child_die = die->child;
12652 child_die && child_die->tag;
12653 child_die = child_die->sibling)
12654 {
12655 struct die_info *child_origin_die;
12656 struct dwarf2_cu *child_origin_cu;
12657
12658 /* We are trying to process concrete instance entries:
12659 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12660 it's not relevant to our analysis here. i.e. detecting DIEs that are
12661 present in the abstract instance but not referenced in the concrete
12662 one. */
12663 if (child_die->tag == DW_TAG_call_site
12664 || child_die->tag == DW_TAG_GNU_call_site)
12665 continue;
12666
12667 /* For each CHILD_DIE, find the corresponding child of
12668 ORIGIN_DIE. If there is more than one layer of
12669 DW_AT_abstract_origin, follow them all; there shouldn't be,
12670 but GCC versions at least through 4.4 generate this (GCC PR
12671 40573). */
12672 child_origin_die = child_die;
12673 child_origin_cu = cu;
12674 while (1)
12675 {
12676 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12677 child_origin_cu);
12678 if (attr == NULL)
12679 break;
12680 child_origin_die = follow_die_ref (child_origin_die, attr,
12681 &child_origin_cu);
12682 }
12683
12684 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12685 counterpart may exist. */
12686 if (child_origin_die != child_die)
12687 {
12688 if (child_die->tag != child_origin_die->tag
12689 && !(child_die->tag == DW_TAG_inlined_subroutine
12690 && child_origin_die->tag == DW_TAG_subprogram))
12691 complaint (_("Child DIE %s and its abstract origin %s have "
12692 "different tags"),
12693 sect_offset_str (child_die->sect_off),
12694 sect_offset_str (child_origin_die->sect_off));
12695 if (child_origin_die->parent != origin_die)
12696 complaint (_("Child DIE %s and its abstract origin %s have "
12697 "different parents"),
12698 sect_offset_str (child_die->sect_off),
12699 sect_offset_str (child_origin_die->sect_off));
12700 else
12701 offsets.push_back (child_origin_die->sect_off);
12702 }
12703 }
12704 std::sort (offsets.begin (), offsets.end ());
12705 sect_offset *offsets_end = offsets.data () + offsets.size ();
12706 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12707 if (offsetp[-1] == *offsetp)
12708 complaint (_("Multiple children of DIE %s refer "
12709 "to DIE %s as their abstract origin"),
12710 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12711
12712 offsetp = offsets.data ();
12713 origin_child_die = origin_die->child;
12714 while (origin_child_die && origin_child_die->tag)
12715 {
12716 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
12717 while (offsetp < offsets_end
12718 && *offsetp < origin_child_die->sect_off)
12719 offsetp++;
12720 if (offsetp >= offsets_end
12721 || *offsetp > origin_child_die->sect_off)
12722 {
12723 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12724 Check whether we're already processing ORIGIN_CHILD_DIE.
12725 This can happen with mutually referenced abstract_origins.
12726 PR 16581. */
12727 if (!origin_child_die->in_process)
12728 process_die (origin_child_die, origin_cu);
12729 }
12730 origin_child_die = origin_child_die->sibling;
12731 }
12732 origin_cu->list_in_scope = origin_previous_list_in_scope;
12733
12734 if (cu != origin_cu)
12735 compute_delayed_physnames (origin_cu);
12736 }
12737
12738 static void
12739 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
12740 {
12741 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12742 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12743 struct context_stack *newobj;
12744 CORE_ADDR lowpc;
12745 CORE_ADDR highpc;
12746 struct die_info *child_die;
12747 struct attribute *attr, *call_line, *call_file;
12748 const char *name;
12749 CORE_ADDR baseaddr;
12750 struct block *block;
12751 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
12752 std::vector<struct symbol *> template_args;
12753 struct template_symbol *templ_func = NULL;
12754
12755 if (inlined_func)
12756 {
12757 /* If we do not have call site information, we can't show the
12758 caller of this inlined function. That's too confusing, so
12759 only use the scope for local variables. */
12760 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12761 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12762 if (call_line == NULL || call_file == NULL)
12763 {
12764 read_lexical_block_scope (die, cu);
12765 return;
12766 }
12767 }
12768
12769 baseaddr = objfile->text_section_offset ();
12770
12771 name = dwarf2_name (die, cu);
12772
12773 /* Ignore functions with missing or empty names. These are actually
12774 illegal according to the DWARF standard. */
12775 if (name == NULL)
12776 {
12777 complaint (_("missing name for subprogram DIE at %s"),
12778 sect_offset_str (die->sect_off));
12779 return;
12780 }
12781
12782 /* Ignore functions with missing or invalid low and high pc attributes. */
12783 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
12784 <= PC_BOUNDS_INVALID)
12785 {
12786 attr = dwarf2_attr (die, DW_AT_external, cu);
12787 if (!attr || !DW_UNSND (attr))
12788 complaint (_("cannot get low and high bounds "
12789 "for subprogram DIE at %s"),
12790 sect_offset_str (die->sect_off));
12791 return;
12792 }
12793
12794 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12795 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12796
12797 /* If we have any template arguments, then we must allocate a
12798 different sort of symbol. */
12799 for (child_die = die->child; child_die; child_die = child_die->sibling)
12800 {
12801 if (child_die->tag == DW_TAG_template_type_param
12802 || child_die->tag == DW_TAG_template_value_param)
12803 {
12804 templ_func = allocate_template_symbol (objfile);
12805 templ_func->subclass = SYMBOL_TEMPLATE;
12806 break;
12807 }
12808 }
12809
12810 newobj = cu->get_builder ()->push_context (0, lowpc);
12811 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12812 (struct symbol *) templ_func);
12813
12814 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
12815 set_objfile_main_name (objfile, newobj->name->linkage_name (),
12816 cu->language);
12817
12818 /* If there is a location expression for DW_AT_frame_base, record
12819 it. */
12820 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
12821 if (attr != nullptr)
12822 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
12823
12824 /* If there is a location for the static link, record it. */
12825 newobj->static_link = NULL;
12826 attr = dwarf2_attr (die, DW_AT_static_link, cu);
12827 if (attr != nullptr)
12828 {
12829 newobj->static_link
12830 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
12831 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
12832 cu->per_cu->addr_type ());
12833 }
12834
12835 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
12836
12837 if (die->child != NULL)
12838 {
12839 child_die = die->child;
12840 while (child_die && child_die->tag)
12841 {
12842 if (child_die->tag == DW_TAG_template_type_param
12843 || child_die->tag == DW_TAG_template_value_param)
12844 {
12845 struct symbol *arg = new_symbol (child_die, NULL, cu);
12846
12847 if (arg != NULL)
12848 template_args.push_back (arg);
12849 }
12850 else
12851 process_die (child_die, cu);
12852 child_die = child_die->sibling;
12853 }
12854 }
12855
12856 inherit_abstract_dies (die, cu);
12857
12858 /* If we have a DW_AT_specification, we might need to import using
12859 directives from the context of the specification DIE. See the
12860 comment in determine_prefix. */
12861 if (cu->language == language_cplus
12862 && dwarf2_attr (die, DW_AT_specification, cu))
12863 {
12864 struct dwarf2_cu *spec_cu = cu;
12865 struct die_info *spec_die = die_specification (die, &spec_cu);
12866
12867 while (spec_die)
12868 {
12869 child_die = spec_die->child;
12870 while (child_die && child_die->tag)
12871 {
12872 if (child_die->tag == DW_TAG_imported_module)
12873 process_die (child_die, spec_cu);
12874 child_die = child_die->sibling;
12875 }
12876
12877 /* In some cases, GCC generates specification DIEs that
12878 themselves contain DW_AT_specification attributes. */
12879 spec_die = die_specification (spec_die, &spec_cu);
12880 }
12881 }
12882
12883 struct context_stack cstk = cu->get_builder ()->pop_context ();
12884 /* Make a block for the local symbols within. */
12885 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
12886 cstk.static_link, lowpc, highpc);
12887
12888 /* For C++, set the block's scope. */
12889 if ((cu->language == language_cplus
12890 || cu->language == language_fortran
12891 || cu->language == language_d
12892 || cu->language == language_rust)
12893 && cu->processing_has_namespace_info)
12894 block_set_scope (block, determine_prefix (die, cu),
12895 &objfile->objfile_obstack);
12896
12897 /* If we have address ranges, record them. */
12898 dwarf2_record_block_ranges (die, block, baseaddr, cu);
12899
12900 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
12901
12902 /* Attach template arguments to function. */
12903 if (!template_args.empty ())
12904 {
12905 gdb_assert (templ_func != NULL);
12906
12907 templ_func->n_template_arguments = template_args.size ();
12908 templ_func->template_arguments
12909 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12910 templ_func->n_template_arguments);
12911 memcpy (templ_func->template_arguments,
12912 template_args.data (),
12913 (templ_func->n_template_arguments * sizeof (struct symbol *)));
12914
12915 /* Make sure that the symtab is set on the new symbols. Even
12916 though they don't appear in this symtab directly, other parts
12917 of gdb assume that symbols do, and this is reasonably
12918 true. */
12919 for (symbol *sym : template_args)
12920 symbol_set_symtab (sym, symbol_symtab (templ_func));
12921 }
12922
12923 /* In C++, we can have functions nested inside functions (e.g., when
12924 a function declares a class that has methods). This means that
12925 when we finish processing a function scope, we may need to go
12926 back to building a containing block's symbol lists. */
12927 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12928 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
12929
12930 /* If we've finished processing a top-level function, subsequent
12931 symbols go in the file symbol list. */
12932 if (cu->get_builder ()->outermost_context_p ())
12933 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
12934 }
12935
12936 /* Process all the DIES contained within a lexical block scope. Start
12937 a new scope, process the dies, and then close the scope. */
12938
12939 static void
12940 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
12941 {
12942 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12943 struct gdbarch *gdbarch = get_objfile_arch (objfile);
12944 CORE_ADDR lowpc, highpc;
12945 struct die_info *child_die;
12946 CORE_ADDR baseaddr;
12947
12948 baseaddr = objfile->text_section_offset ();
12949
12950 /* Ignore blocks with missing or invalid low and high pc attributes. */
12951 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12952 as multiple lexical blocks? Handling children in a sane way would
12953 be nasty. Might be easier to properly extend generic blocks to
12954 describe ranges. */
12955 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12956 {
12957 case PC_BOUNDS_NOT_PRESENT:
12958 /* DW_TAG_lexical_block has no attributes, process its children as if
12959 there was no wrapping by that DW_TAG_lexical_block.
12960 GCC does no longer produces such DWARF since GCC r224161. */
12961 for (child_die = die->child;
12962 child_die != NULL && child_die->tag;
12963 child_die = child_die->sibling)
12964 process_die (child_die, cu);
12965 return;
12966 case PC_BOUNDS_INVALID:
12967 return;
12968 }
12969 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12970 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
12971
12972 cu->get_builder ()->push_context (0, lowpc);
12973 if (die->child != NULL)
12974 {
12975 child_die = die->child;
12976 while (child_die && child_die->tag)
12977 {
12978 process_die (child_die, cu);
12979 child_die = child_die->sibling;
12980 }
12981 }
12982 inherit_abstract_dies (die, cu);
12983 struct context_stack cstk = cu->get_builder ()->pop_context ();
12984
12985 if (*cu->get_builder ()->get_local_symbols () != NULL
12986 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
12987 {
12988 struct block *block
12989 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
12990 cstk.start_addr, highpc);
12991
12992 /* Note that recording ranges after traversing children, as we
12993 do here, means that recording a parent's ranges entails
12994 walking across all its children's ranges as they appear in
12995 the address map, which is quadratic behavior.
12996
12997 It would be nicer to record the parent's ranges before
12998 traversing its children, simply overriding whatever you find
12999 there. But since we don't even decide whether to create a
13000 block until after we've traversed its children, that's hard
13001 to do. */
13002 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13003 }
13004 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13005 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13006 }
13007
13008 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13009
13010 static void
13011 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13012 {
13013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13014 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13015 CORE_ADDR pc, baseaddr;
13016 struct attribute *attr;
13017 struct call_site *call_site, call_site_local;
13018 void **slot;
13019 int nparams;
13020 struct die_info *child_die;
13021
13022 baseaddr = objfile->text_section_offset ();
13023
13024 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13025 if (attr == NULL)
13026 {
13027 /* This was a pre-DWARF-5 GNU extension alias
13028 for DW_AT_call_return_pc. */
13029 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13030 }
13031 if (!attr)
13032 {
13033 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13034 "DIE %s [in module %s]"),
13035 sect_offset_str (die->sect_off), objfile_name (objfile));
13036 return;
13037 }
13038 pc = attr->value_as_address () + baseaddr;
13039 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13040
13041 if (cu->call_site_htab == NULL)
13042 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13043 NULL, &objfile->objfile_obstack,
13044 hashtab_obstack_allocate, NULL);
13045 call_site_local.pc = pc;
13046 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13047 if (*slot != NULL)
13048 {
13049 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13050 "DIE %s [in module %s]"),
13051 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13052 objfile_name (objfile));
13053 return;
13054 }
13055
13056 /* Count parameters at the caller. */
13057
13058 nparams = 0;
13059 for (child_die = die->child; child_die && child_die->tag;
13060 child_die = child_die->sibling)
13061 {
13062 if (child_die->tag != DW_TAG_call_site_parameter
13063 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13064 {
13065 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13066 "DW_TAG_call_site child DIE %s [in module %s]"),
13067 child_die->tag, sect_offset_str (child_die->sect_off),
13068 objfile_name (objfile));
13069 continue;
13070 }
13071
13072 nparams++;
13073 }
13074
13075 call_site
13076 = ((struct call_site *)
13077 obstack_alloc (&objfile->objfile_obstack,
13078 sizeof (*call_site)
13079 + (sizeof (*call_site->parameter) * (nparams - 1))));
13080 *slot = call_site;
13081 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13082 call_site->pc = pc;
13083
13084 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13085 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13086 {
13087 struct die_info *func_die;
13088
13089 /* Skip also over DW_TAG_inlined_subroutine. */
13090 for (func_die = die->parent;
13091 func_die && func_die->tag != DW_TAG_subprogram
13092 && func_die->tag != DW_TAG_subroutine_type;
13093 func_die = func_die->parent);
13094
13095 /* DW_AT_call_all_calls is a superset
13096 of DW_AT_call_all_tail_calls. */
13097 if (func_die
13098 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13099 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13100 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13101 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13102 {
13103 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13104 not complete. But keep CALL_SITE for look ups via call_site_htab,
13105 both the initial caller containing the real return address PC and
13106 the final callee containing the current PC of a chain of tail
13107 calls do not need to have the tail call list complete. But any
13108 function candidate for a virtual tail call frame searched via
13109 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13110 determined unambiguously. */
13111 }
13112 else
13113 {
13114 struct type *func_type = NULL;
13115
13116 if (func_die)
13117 func_type = get_die_type (func_die, cu);
13118 if (func_type != NULL)
13119 {
13120 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13121
13122 /* Enlist this call site to the function. */
13123 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13124 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13125 }
13126 else
13127 complaint (_("Cannot find function owning DW_TAG_call_site "
13128 "DIE %s [in module %s]"),
13129 sect_offset_str (die->sect_off), objfile_name (objfile));
13130 }
13131 }
13132
13133 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13134 if (attr == NULL)
13135 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13136 if (attr == NULL)
13137 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13138 if (attr == NULL)
13139 {
13140 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13141 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13142 }
13143 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13144 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13145 /* Keep NULL DWARF_BLOCK. */;
13146 else if (attr->form_is_block ())
13147 {
13148 struct dwarf2_locexpr_baton *dlbaton;
13149
13150 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13151 dlbaton->data = DW_BLOCK (attr)->data;
13152 dlbaton->size = DW_BLOCK (attr)->size;
13153 dlbaton->per_cu = cu->per_cu;
13154
13155 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13156 }
13157 else if (attr->form_is_ref ())
13158 {
13159 struct dwarf2_cu *target_cu = cu;
13160 struct die_info *target_die;
13161
13162 target_die = follow_die_ref (die, attr, &target_cu);
13163 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
13164 if (die_is_declaration (target_die, target_cu))
13165 {
13166 const char *target_physname;
13167
13168 /* Prefer the mangled name; otherwise compute the demangled one. */
13169 target_physname = dw2_linkage_name (target_die, target_cu);
13170 if (target_physname == NULL)
13171 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13172 if (target_physname == NULL)
13173 complaint (_("DW_AT_call_target target DIE has invalid "
13174 "physname, for referencing DIE %s [in module %s]"),
13175 sect_offset_str (die->sect_off), objfile_name (objfile));
13176 else
13177 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13178 }
13179 else
13180 {
13181 CORE_ADDR lowpc;
13182
13183 /* DW_AT_entry_pc should be preferred. */
13184 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13185 <= PC_BOUNDS_INVALID)
13186 complaint (_("DW_AT_call_target target DIE has invalid "
13187 "low pc, for referencing DIE %s [in module %s]"),
13188 sect_offset_str (die->sect_off), objfile_name (objfile));
13189 else
13190 {
13191 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13192 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13193 }
13194 }
13195 }
13196 else
13197 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13198 "block nor reference, for DIE %s [in module %s]"),
13199 sect_offset_str (die->sect_off), objfile_name (objfile));
13200
13201 call_site->per_cu = cu->per_cu;
13202
13203 for (child_die = die->child;
13204 child_die && child_die->tag;
13205 child_die = child_die->sibling)
13206 {
13207 struct call_site_parameter *parameter;
13208 struct attribute *loc, *origin;
13209
13210 if (child_die->tag != DW_TAG_call_site_parameter
13211 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13212 {
13213 /* Already printed the complaint above. */
13214 continue;
13215 }
13216
13217 gdb_assert (call_site->parameter_count < nparams);
13218 parameter = &call_site->parameter[call_site->parameter_count];
13219
13220 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13221 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13222 register is contained in DW_AT_call_value. */
13223
13224 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13225 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13226 if (origin == NULL)
13227 {
13228 /* This was a pre-DWARF-5 GNU extension alias
13229 for DW_AT_call_parameter. */
13230 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13231 }
13232 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13233 {
13234 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13235
13236 sect_offset sect_off
13237 = (sect_offset) dwarf2_get_ref_die_offset (origin);
13238 if (!cu->header.offset_in_cu_p (sect_off))
13239 {
13240 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13241 binding can be done only inside one CU. Such referenced DIE
13242 therefore cannot be even moved to DW_TAG_partial_unit. */
13243 complaint (_("DW_AT_call_parameter offset is not in CU for "
13244 "DW_TAG_call_site child DIE %s [in module %s]"),
13245 sect_offset_str (child_die->sect_off),
13246 objfile_name (objfile));
13247 continue;
13248 }
13249 parameter->u.param_cu_off
13250 = (cu_offset) (sect_off - cu->header.sect_off);
13251 }
13252 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13253 {
13254 complaint (_("No DW_FORM_block* DW_AT_location for "
13255 "DW_TAG_call_site child DIE %s [in module %s]"),
13256 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13257 continue;
13258 }
13259 else
13260 {
13261 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13262 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13263 if (parameter->u.dwarf_reg != -1)
13264 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13265 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13266 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13267 &parameter->u.fb_offset))
13268 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13269 else
13270 {
13271 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13272 "for DW_FORM_block* DW_AT_location is supported for "
13273 "DW_TAG_call_site child DIE %s "
13274 "[in module %s]"),
13275 sect_offset_str (child_die->sect_off),
13276 objfile_name (objfile));
13277 continue;
13278 }
13279 }
13280
13281 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13282 if (attr == NULL)
13283 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13284 if (attr == NULL || !attr->form_is_block ())
13285 {
13286 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13287 "DW_TAG_call_site child DIE %s [in module %s]"),
13288 sect_offset_str (child_die->sect_off),
13289 objfile_name (objfile));
13290 continue;
13291 }
13292 parameter->value = DW_BLOCK (attr)->data;
13293 parameter->value_size = DW_BLOCK (attr)->size;
13294
13295 /* Parameters are not pre-cleared by memset above. */
13296 parameter->data_value = NULL;
13297 parameter->data_value_size = 0;
13298 call_site->parameter_count++;
13299
13300 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13301 if (attr == NULL)
13302 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13303 if (attr != nullptr)
13304 {
13305 if (!attr->form_is_block ())
13306 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13307 "DW_TAG_call_site child DIE %s [in module %s]"),
13308 sect_offset_str (child_die->sect_off),
13309 objfile_name (objfile));
13310 else
13311 {
13312 parameter->data_value = DW_BLOCK (attr)->data;
13313 parameter->data_value_size = DW_BLOCK (attr)->size;
13314 }
13315 }
13316 }
13317 }
13318
13319 /* Helper function for read_variable. If DIE represents a virtual
13320 table, then return the type of the concrete object that is
13321 associated with the virtual table. Otherwise, return NULL. */
13322
13323 static struct type *
13324 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13325 {
13326 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13327 if (attr == NULL)
13328 return NULL;
13329
13330 /* Find the type DIE. */
13331 struct die_info *type_die = NULL;
13332 struct dwarf2_cu *type_cu = cu;
13333
13334 if (attr->form_is_ref ())
13335 type_die = follow_die_ref (die, attr, &type_cu);
13336 if (type_die == NULL)
13337 return NULL;
13338
13339 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13340 return NULL;
13341 return die_containing_type (type_die, type_cu);
13342 }
13343
13344 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13345
13346 static void
13347 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13348 {
13349 struct rust_vtable_symbol *storage = NULL;
13350
13351 if (cu->language == language_rust)
13352 {
13353 struct type *containing_type = rust_containing_type (die, cu);
13354
13355 if (containing_type != NULL)
13356 {
13357 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13358
13359 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
13360 initialize_objfile_symbol (storage);
13361 storage->concrete_type = containing_type;
13362 storage->subclass = SYMBOL_RUST_VTABLE;
13363 }
13364 }
13365
13366 struct symbol *res = new_symbol (die, NULL, cu, storage);
13367 struct attribute *abstract_origin
13368 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13369 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13370 if (res == NULL && loc && abstract_origin)
13371 {
13372 /* We have a variable without a name, but with a location and an abstract
13373 origin. This may be a concrete instance of an abstract variable
13374 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13375 later. */
13376 struct dwarf2_cu *origin_cu = cu;
13377 struct die_info *origin_die
13378 = follow_die_ref (die, abstract_origin, &origin_cu);
13379 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
13380 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
13381 }
13382 }
13383
13384 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13385 reading .debug_rnglists.
13386 Callback's type should be:
13387 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13388 Return true if the attributes are present and valid, otherwise,
13389 return false. */
13390
13391 template <typename Callback>
13392 static bool
13393 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13394 Callback &&callback)
13395 {
13396 struct dwarf2_per_objfile *dwarf2_per_objfile
13397 = cu->per_cu->dwarf2_per_objfile;
13398 struct objfile *objfile = dwarf2_per_objfile->objfile;
13399 bfd *obfd = objfile->obfd;
13400 /* Base address selection entry. */
13401 gdb::optional<CORE_ADDR> base;
13402 const gdb_byte *buffer;
13403 CORE_ADDR baseaddr;
13404 bool overflow = false;
13405
13406 base = cu->base_address;
13407
13408 dwarf2_per_objfile->rnglists.read (objfile);
13409 if (offset >= dwarf2_per_objfile->rnglists.size)
13410 {
13411 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13412 offset);
13413 return false;
13414 }
13415 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13416
13417 baseaddr = objfile->text_section_offset ();
13418
13419 while (1)
13420 {
13421 /* Initialize it due to a false compiler warning. */
13422 CORE_ADDR range_beginning = 0, range_end = 0;
13423 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13424 + dwarf2_per_objfile->rnglists.size);
13425 unsigned int bytes_read;
13426
13427 if (buffer == buf_end)
13428 {
13429 overflow = true;
13430 break;
13431 }
13432 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13433 switch (rlet)
13434 {
13435 case DW_RLE_end_of_list:
13436 break;
13437 case DW_RLE_base_address:
13438 if (buffer + cu->header.addr_size > buf_end)
13439 {
13440 overflow = true;
13441 break;
13442 }
13443 base = cu->header.read_address (obfd, buffer, &bytes_read);
13444 buffer += bytes_read;
13445 break;
13446 case DW_RLE_start_length:
13447 if (buffer + cu->header.addr_size > buf_end)
13448 {
13449 overflow = true;
13450 break;
13451 }
13452 range_beginning = cu->header.read_address (obfd, buffer,
13453 &bytes_read);
13454 buffer += bytes_read;
13455 range_end = (range_beginning
13456 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13457 buffer += bytes_read;
13458 if (buffer > buf_end)
13459 {
13460 overflow = true;
13461 break;
13462 }
13463 break;
13464 case DW_RLE_offset_pair:
13465 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13466 buffer += bytes_read;
13467 if (buffer > buf_end)
13468 {
13469 overflow = true;
13470 break;
13471 }
13472 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13473 buffer += bytes_read;
13474 if (buffer > buf_end)
13475 {
13476 overflow = true;
13477 break;
13478 }
13479 break;
13480 case DW_RLE_start_end:
13481 if (buffer + 2 * cu->header.addr_size > buf_end)
13482 {
13483 overflow = true;
13484 break;
13485 }
13486 range_beginning = cu->header.read_address (obfd, buffer,
13487 &bytes_read);
13488 buffer += bytes_read;
13489 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13490 buffer += bytes_read;
13491 break;
13492 default:
13493 complaint (_("Invalid .debug_rnglists data (no base address)"));
13494 return false;
13495 }
13496 if (rlet == DW_RLE_end_of_list || overflow)
13497 break;
13498 if (rlet == DW_RLE_base_address)
13499 continue;
13500
13501 if (!base.has_value ())
13502 {
13503 /* We have no valid base address for the ranges
13504 data. */
13505 complaint (_("Invalid .debug_rnglists data (no base address)"));
13506 return false;
13507 }
13508
13509 if (range_beginning > range_end)
13510 {
13511 /* Inverted range entries are invalid. */
13512 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13513 return false;
13514 }
13515
13516 /* Empty range entries have no effect. */
13517 if (range_beginning == range_end)
13518 continue;
13519
13520 range_beginning += *base;
13521 range_end += *base;
13522
13523 /* A not-uncommon case of bad debug info.
13524 Don't pollute the addrmap with bad data. */
13525 if (range_beginning + baseaddr == 0
13526 && !dwarf2_per_objfile->has_section_at_zero)
13527 {
13528 complaint (_(".debug_rnglists entry has start address of zero"
13529 " [in module %s]"), objfile_name (objfile));
13530 continue;
13531 }
13532
13533 callback (range_beginning, range_end);
13534 }
13535
13536 if (overflow)
13537 {
13538 complaint (_("Offset %d is not terminated "
13539 "for DW_AT_ranges attribute"),
13540 offset);
13541 return false;
13542 }
13543
13544 return true;
13545 }
13546
13547 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13548 Callback's type should be:
13549 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13550 Return 1 if the attributes are present and valid, otherwise, return 0. */
13551
13552 template <typename Callback>
13553 static int
13554 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13555 Callback &&callback)
13556 {
13557 struct dwarf2_per_objfile *dwarf2_per_objfile
13558 = cu->per_cu->dwarf2_per_objfile;
13559 struct objfile *objfile = dwarf2_per_objfile->objfile;
13560 struct comp_unit_head *cu_header = &cu->header;
13561 bfd *obfd = objfile->obfd;
13562 unsigned int addr_size = cu_header->addr_size;
13563 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13564 /* Base address selection entry. */
13565 gdb::optional<CORE_ADDR> base;
13566 unsigned int dummy;
13567 const gdb_byte *buffer;
13568 CORE_ADDR baseaddr;
13569
13570 if (cu_header->version >= 5)
13571 return dwarf2_rnglists_process (offset, cu, callback);
13572
13573 base = cu->base_address;
13574
13575 dwarf2_per_objfile->ranges.read (objfile);
13576 if (offset >= dwarf2_per_objfile->ranges.size)
13577 {
13578 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13579 offset);
13580 return 0;
13581 }
13582 buffer = dwarf2_per_objfile->ranges.buffer + offset;
13583
13584 baseaddr = objfile->text_section_offset ();
13585
13586 while (1)
13587 {
13588 CORE_ADDR range_beginning, range_end;
13589
13590 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13591 buffer += addr_size;
13592 range_end = cu->header.read_address (obfd, buffer, &dummy);
13593 buffer += addr_size;
13594 offset += 2 * addr_size;
13595
13596 /* An end of list marker is a pair of zero addresses. */
13597 if (range_beginning == 0 && range_end == 0)
13598 /* Found the end of list entry. */
13599 break;
13600
13601 /* Each base address selection entry is a pair of 2 values.
13602 The first is the largest possible address, the second is
13603 the base address. Check for a base address here. */
13604 if ((range_beginning & mask) == mask)
13605 {
13606 /* If we found the largest possible address, then we already
13607 have the base address in range_end. */
13608 base = range_end;
13609 continue;
13610 }
13611
13612 if (!base.has_value ())
13613 {
13614 /* We have no valid base address for the ranges
13615 data. */
13616 complaint (_("Invalid .debug_ranges data (no base address)"));
13617 return 0;
13618 }
13619
13620 if (range_beginning > range_end)
13621 {
13622 /* Inverted range entries are invalid. */
13623 complaint (_("Invalid .debug_ranges data (inverted range)"));
13624 return 0;
13625 }
13626
13627 /* Empty range entries have no effect. */
13628 if (range_beginning == range_end)
13629 continue;
13630
13631 range_beginning += *base;
13632 range_end += *base;
13633
13634 /* A not-uncommon case of bad debug info.
13635 Don't pollute the addrmap with bad data. */
13636 if (range_beginning + baseaddr == 0
13637 && !dwarf2_per_objfile->has_section_at_zero)
13638 {
13639 complaint (_(".debug_ranges entry has start address of zero"
13640 " [in module %s]"), objfile_name (objfile));
13641 continue;
13642 }
13643
13644 callback (range_beginning, range_end);
13645 }
13646
13647 return 1;
13648 }
13649
13650 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13651 Return 1 if the attributes are present and valid, otherwise, return 0.
13652 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13653
13654 static int
13655 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13656 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13657 dwarf2_psymtab *ranges_pst)
13658 {
13659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13661 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13662 int low_set = 0;
13663 CORE_ADDR low = 0;
13664 CORE_ADDR high = 0;
13665 int retval;
13666
13667 retval = dwarf2_ranges_process (offset, cu,
13668 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13669 {
13670 if (ranges_pst != NULL)
13671 {
13672 CORE_ADDR lowpc;
13673 CORE_ADDR highpc;
13674
13675 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13676 range_beginning + baseaddr)
13677 - baseaddr);
13678 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13679 range_end + baseaddr)
13680 - baseaddr);
13681 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13682 lowpc, highpc - 1, ranges_pst);
13683 }
13684
13685 /* FIXME: This is recording everything as a low-high
13686 segment of consecutive addresses. We should have a
13687 data structure for discontiguous block ranges
13688 instead. */
13689 if (! low_set)
13690 {
13691 low = range_beginning;
13692 high = range_end;
13693 low_set = 1;
13694 }
13695 else
13696 {
13697 if (range_beginning < low)
13698 low = range_beginning;
13699 if (range_end > high)
13700 high = range_end;
13701 }
13702 });
13703 if (!retval)
13704 return 0;
13705
13706 if (! low_set)
13707 /* If the first entry is an end-of-list marker, the range
13708 describes an empty scope, i.e. no instructions. */
13709 return 0;
13710
13711 if (low_return)
13712 *low_return = low;
13713 if (high_return)
13714 *high_return = high;
13715 return 1;
13716 }
13717
13718 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
13719 definition for the return value. *LOWPC and *HIGHPC are set iff
13720 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
13721
13722 static enum pc_bounds_kind
13723 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
13724 CORE_ADDR *highpc, struct dwarf2_cu *cu,
13725 dwarf2_psymtab *pst)
13726 {
13727 struct dwarf2_per_objfile *dwarf2_per_objfile
13728 = cu->per_cu->dwarf2_per_objfile;
13729 struct attribute *attr;
13730 struct attribute *attr_high;
13731 CORE_ADDR low = 0;
13732 CORE_ADDR high = 0;
13733 enum pc_bounds_kind ret;
13734
13735 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13736 if (attr_high)
13737 {
13738 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13739 if (attr != nullptr)
13740 {
13741 low = attr->value_as_address ();
13742 high = attr_high->value_as_address ();
13743 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13744 high += low;
13745 }
13746 else
13747 /* Found high w/o low attribute. */
13748 return PC_BOUNDS_INVALID;
13749
13750 /* Found consecutive range of addresses. */
13751 ret = PC_BOUNDS_HIGH_LOW;
13752 }
13753 else
13754 {
13755 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13756 if (attr != NULL)
13757 {
13758 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13759 We take advantage of the fact that DW_AT_ranges does not appear
13760 in DW_TAG_compile_unit of DWO files. */
13761 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13762 unsigned int ranges_offset = (DW_UNSND (attr)
13763 + (need_ranges_base
13764 ? cu->ranges_base
13765 : 0));
13766
13767 /* Value of the DW_AT_ranges attribute is the offset in the
13768 .debug_ranges section. */
13769 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
13770 return PC_BOUNDS_INVALID;
13771 /* Found discontinuous range of addresses. */
13772 ret = PC_BOUNDS_RANGES;
13773 }
13774 else
13775 return PC_BOUNDS_NOT_PRESENT;
13776 }
13777
13778 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
13779 if (high <= low)
13780 return PC_BOUNDS_INVALID;
13781
13782 /* When using the GNU linker, .gnu.linkonce. sections are used to
13783 eliminate duplicate copies of functions and vtables and such.
13784 The linker will arbitrarily choose one and discard the others.
13785 The AT_*_pc values for such functions refer to local labels in
13786 these sections. If the section from that file was discarded, the
13787 labels are not in the output, so the relocs get a value of 0.
13788 If this is a discarded function, mark the pc bounds as invalid,
13789 so that GDB will ignore it. */
13790 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
13791 return PC_BOUNDS_INVALID;
13792
13793 *lowpc = low;
13794 if (highpc)
13795 *highpc = high;
13796 return ret;
13797 }
13798
13799 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
13800 its low and high PC addresses. Do nothing if these addresses could not
13801 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13802 and HIGHPC to the high address if greater than HIGHPC. */
13803
13804 static void
13805 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13806 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13807 struct dwarf2_cu *cu)
13808 {
13809 CORE_ADDR low, high;
13810 struct die_info *child = die->child;
13811
13812 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
13813 {
13814 *lowpc = std::min (*lowpc, low);
13815 *highpc = std::max (*highpc, high);
13816 }
13817
13818 /* If the language does not allow nested subprograms (either inside
13819 subprograms or lexical blocks), we're done. */
13820 if (cu->language != language_ada)
13821 return;
13822
13823 /* Check all the children of the given DIE. If it contains nested
13824 subprograms, then check their pc bounds. Likewise, we need to
13825 check lexical blocks as well, as they may also contain subprogram
13826 definitions. */
13827 while (child && child->tag)
13828 {
13829 if (child->tag == DW_TAG_subprogram
13830 || child->tag == DW_TAG_lexical_block)
13831 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
13832 child = child->sibling;
13833 }
13834 }
13835
13836 /* Get the low and high pc's represented by the scope DIE, and store
13837 them in *LOWPC and *HIGHPC. If the correct values can't be
13838 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13839
13840 static void
13841 get_scope_pc_bounds (struct die_info *die,
13842 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13843 struct dwarf2_cu *cu)
13844 {
13845 CORE_ADDR best_low = (CORE_ADDR) -1;
13846 CORE_ADDR best_high = (CORE_ADDR) 0;
13847 CORE_ADDR current_low, current_high;
13848
13849 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
13850 >= PC_BOUNDS_RANGES)
13851 {
13852 best_low = current_low;
13853 best_high = current_high;
13854 }
13855 else
13856 {
13857 struct die_info *child = die->child;
13858
13859 while (child && child->tag)
13860 {
13861 switch (child->tag) {
13862 case DW_TAG_subprogram:
13863 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
13864 break;
13865 case DW_TAG_namespace:
13866 case DW_TAG_module:
13867 /* FIXME: carlton/2004-01-16: Should we do this for
13868 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13869 that current GCC's always emit the DIEs corresponding
13870 to definitions of methods of classes as children of a
13871 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13872 the DIEs giving the declarations, which could be
13873 anywhere). But I don't see any reason why the
13874 standards says that they have to be there. */
13875 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13876
13877 if (current_low != ((CORE_ADDR) -1))
13878 {
13879 best_low = std::min (best_low, current_low);
13880 best_high = std::max (best_high, current_high);
13881 }
13882 break;
13883 default:
13884 /* Ignore. */
13885 break;
13886 }
13887
13888 child = child->sibling;
13889 }
13890 }
13891
13892 *lowpc = best_low;
13893 *highpc = best_high;
13894 }
13895
13896 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
13897 in DIE. */
13898
13899 static void
13900 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13901 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13902 {
13903 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
13904 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13905 struct attribute *attr;
13906 struct attribute *attr_high;
13907
13908 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13909 if (attr_high)
13910 {
13911 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13912 if (attr != nullptr)
13913 {
13914 CORE_ADDR low = attr->value_as_address ();
13915 CORE_ADDR high = attr_high->value_as_address ();
13916
13917 if (cu->header.version >= 4 && attr_high->form_is_constant ())
13918 high += low;
13919
13920 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13921 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
13922 cu->get_builder ()->record_block_range (block, low, high - 1);
13923 }
13924 }
13925
13926 attr = dwarf2_attr (die, DW_AT_ranges, cu);
13927 if (attr != nullptr)
13928 {
13929 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
13930 We take advantage of the fact that DW_AT_ranges does not appear
13931 in DW_TAG_compile_unit of DWO files. */
13932 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13933
13934 /* The value of the DW_AT_ranges attribute is the offset of the
13935 address range list in the .debug_ranges section. */
13936 unsigned long offset = (DW_UNSND (attr)
13937 + (need_ranges_base ? cu->ranges_base : 0));
13938
13939 std::vector<blockrange> blockvec;
13940 dwarf2_ranges_process (offset, cu,
13941 [&] (CORE_ADDR start, CORE_ADDR end)
13942 {
13943 start += baseaddr;
13944 end += baseaddr;
13945 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13946 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
13947 cu->get_builder ()->record_block_range (block, start, end - 1);
13948 blockvec.emplace_back (start, end);
13949 });
13950
13951 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
13952 }
13953 }
13954
13955 /* Check whether the producer field indicates either of GCC < 4.6, or the
13956 Intel C/C++ compiler, and cache the result in CU. */
13957
13958 static void
13959 check_producer (struct dwarf2_cu *cu)
13960 {
13961 int major, minor;
13962
13963 if (cu->producer == NULL)
13964 {
13965 /* For unknown compilers expect their behavior is DWARF version
13966 compliant.
13967
13968 GCC started to support .debug_types sections by -gdwarf-4 since
13969 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13970 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13971 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13972 interpreted incorrectly by GDB now - GCC PR debug/48229. */
13973 }
13974 else if (producer_is_gcc (cu->producer, &major, &minor))
13975 {
13976 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13977 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
13978 }
13979 else if (producer_is_icc (cu->producer, &major, &minor))
13980 {
13981 cu->producer_is_icc = true;
13982 cu->producer_is_icc_lt_14 = major < 14;
13983 }
13984 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13985 cu->producer_is_codewarrior = true;
13986 else
13987 {
13988 /* For other non-GCC compilers, expect their behavior is DWARF version
13989 compliant. */
13990 }
13991
13992 cu->checked_producer = true;
13993 }
13994
13995 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
13996 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
13997 during 4.6.0 experimental. */
13998
13999 static bool
14000 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14001 {
14002 if (!cu->checked_producer)
14003 check_producer (cu);
14004
14005 return cu->producer_is_gxx_lt_4_6;
14006 }
14007
14008
14009 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14010 with incorrect is_stmt attributes. */
14011
14012 static bool
14013 producer_is_codewarrior (struct dwarf2_cu *cu)
14014 {
14015 if (!cu->checked_producer)
14016 check_producer (cu);
14017
14018 return cu->producer_is_codewarrior;
14019 }
14020
14021 /* Return the default accessibility type if it is not overridden by
14022 DW_AT_accessibility. */
14023
14024 static enum dwarf_access_attribute
14025 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14026 {
14027 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14028 {
14029 /* The default DWARF 2 accessibility for members is public, the default
14030 accessibility for inheritance is private. */
14031
14032 if (die->tag != DW_TAG_inheritance)
14033 return DW_ACCESS_public;
14034 else
14035 return DW_ACCESS_private;
14036 }
14037 else
14038 {
14039 /* DWARF 3+ defines the default accessibility a different way. The same
14040 rules apply now for DW_TAG_inheritance as for the members and it only
14041 depends on the container kind. */
14042
14043 if (die->parent->tag == DW_TAG_class_type)
14044 return DW_ACCESS_private;
14045 else
14046 return DW_ACCESS_public;
14047 }
14048 }
14049
14050 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14051 offset. If the attribute was not found return 0, otherwise return
14052 1. If it was found but could not properly be handled, set *OFFSET
14053 to 0. */
14054
14055 static int
14056 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14057 LONGEST *offset)
14058 {
14059 struct attribute *attr;
14060
14061 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14062 if (attr != NULL)
14063 {
14064 *offset = 0;
14065
14066 /* Note that we do not check for a section offset first here.
14067 This is because DW_AT_data_member_location is new in DWARF 4,
14068 so if we see it, we can assume that a constant form is really
14069 a constant and not a section offset. */
14070 if (attr->form_is_constant ())
14071 *offset = dwarf2_get_attr_constant_value (attr, 0);
14072 else if (attr->form_is_section_offset ())
14073 dwarf2_complex_location_expr_complaint ();
14074 else if (attr->form_is_block ())
14075 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14076 else
14077 dwarf2_complex_location_expr_complaint ();
14078
14079 return 1;
14080 }
14081
14082 return 0;
14083 }
14084
14085 /* Add an aggregate field to the field list. */
14086
14087 static void
14088 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14089 struct dwarf2_cu *cu)
14090 {
14091 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14092 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14093 struct nextfield *new_field;
14094 struct attribute *attr;
14095 struct field *fp;
14096 const char *fieldname = "";
14097
14098 if (die->tag == DW_TAG_inheritance)
14099 {
14100 fip->baseclasses.emplace_back ();
14101 new_field = &fip->baseclasses.back ();
14102 }
14103 else
14104 {
14105 fip->fields.emplace_back ();
14106 new_field = &fip->fields.back ();
14107 }
14108
14109 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14110 if (attr != nullptr)
14111 new_field->accessibility = DW_UNSND (attr);
14112 else
14113 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14114 if (new_field->accessibility != DW_ACCESS_public)
14115 fip->non_public_fields = 1;
14116
14117 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14118 if (attr != nullptr)
14119 new_field->virtuality = DW_UNSND (attr);
14120 else
14121 new_field->virtuality = DW_VIRTUALITY_none;
14122
14123 fp = &new_field->field;
14124
14125 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14126 {
14127 LONGEST offset;
14128
14129 /* Data member other than a C++ static data member. */
14130
14131 /* Get type of field. */
14132 fp->type = die_type (die, cu);
14133
14134 SET_FIELD_BITPOS (*fp, 0);
14135
14136 /* Get bit size of field (zero if none). */
14137 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14138 if (attr != nullptr)
14139 {
14140 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14141 }
14142 else
14143 {
14144 FIELD_BITSIZE (*fp) = 0;
14145 }
14146
14147 /* Get bit offset of field. */
14148 if (handle_data_member_location (die, cu, &offset))
14149 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14150 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14151 if (attr != nullptr)
14152 {
14153 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14154 {
14155 /* For big endian bits, the DW_AT_bit_offset gives the
14156 additional bit offset from the MSB of the containing
14157 anonymous object to the MSB of the field. We don't
14158 have to do anything special since we don't need to
14159 know the size of the anonymous object. */
14160 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14161 }
14162 else
14163 {
14164 /* For little endian bits, compute the bit offset to the
14165 MSB of the anonymous object, subtract off the number of
14166 bits from the MSB of the field to the MSB of the
14167 object, and then subtract off the number of bits of
14168 the field itself. The result is the bit offset of
14169 the LSB of the field. */
14170 int anonymous_size;
14171 int bit_offset = DW_UNSND (attr);
14172
14173 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14174 if (attr != nullptr)
14175 {
14176 /* The size of the anonymous object containing
14177 the bit field is explicit, so use the
14178 indicated size (in bytes). */
14179 anonymous_size = DW_UNSND (attr);
14180 }
14181 else
14182 {
14183 /* The size of the anonymous object containing
14184 the bit field must be inferred from the type
14185 attribute of the data member containing the
14186 bit field. */
14187 anonymous_size = TYPE_LENGTH (fp->type);
14188 }
14189 SET_FIELD_BITPOS (*fp,
14190 (FIELD_BITPOS (*fp)
14191 + anonymous_size * bits_per_byte
14192 - bit_offset - FIELD_BITSIZE (*fp)));
14193 }
14194 }
14195 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14196 if (attr != NULL)
14197 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14198 + dwarf2_get_attr_constant_value (attr, 0)));
14199
14200 /* Get name of field. */
14201 fieldname = dwarf2_name (die, cu);
14202 if (fieldname == NULL)
14203 fieldname = "";
14204
14205 /* The name is already allocated along with this objfile, so we don't
14206 need to duplicate it for the type. */
14207 fp->name = fieldname;
14208
14209 /* Change accessibility for artificial fields (e.g. virtual table
14210 pointer or virtual base class pointer) to private. */
14211 if (dwarf2_attr (die, DW_AT_artificial, cu))
14212 {
14213 FIELD_ARTIFICIAL (*fp) = 1;
14214 new_field->accessibility = DW_ACCESS_private;
14215 fip->non_public_fields = 1;
14216 }
14217 }
14218 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14219 {
14220 /* C++ static member. */
14221
14222 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14223 is a declaration, but all versions of G++ as of this writing
14224 (so through at least 3.2.1) incorrectly generate
14225 DW_TAG_variable tags. */
14226
14227 const char *physname;
14228
14229 /* Get name of field. */
14230 fieldname = dwarf2_name (die, cu);
14231 if (fieldname == NULL)
14232 return;
14233
14234 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14235 if (attr
14236 /* Only create a symbol if this is an external value.
14237 new_symbol checks this and puts the value in the global symbol
14238 table, which we want. If it is not external, new_symbol
14239 will try to put the value in cu->list_in_scope which is wrong. */
14240 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14241 {
14242 /* A static const member, not much different than an enum as far as
14243 we're concerned, except that we can support more types. */
14244 new_symbol (die, NULL, cu);
14245 }
14246
14247 /* Get physical name. */
14248 physname = dwarf2_physname (fieldname, die, cu);
14249
14250 /* The name is already allocated along with this objfile, so we don't
14251 need to duplicate it for the type. */
14252 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14253 FIELD_TYPE (*fp) = die_type (die, cu);
14254 FIELD_NAME (*fp) = fieldname;
14255 }
14256 else if (die->tag == DW_TAG_inheritance)
14257 {
14258 LONGEST offset;
14259
14260 /* C++ base class field. */
14261 if (handle_data_member_location (die, cu, &offset))
14262 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
14263 FIELD_BITSIZE (*fp) = 0;
14264 FIELD_TYPE (*fp) = die_type (die, cu);
14265 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
14266 }
14267 else if (die->tag == DW_TAG_variant_part)
14268 {
14269 /* process_structure_scope will treat this DIE as a union. */
14270 process_structure_scope (die, cu);
14271
14272 /* The variant part is relative to the start of the enclosing
14273 structure. */
14274 SET_FIELD_BITPOS (*fp, 0);
14275 fp->type = get_die_type (die, cu);
14276 fp->artificial = 1;
14277 fp->name = "<<variant>>";
14278
14279 /* Normally a DW_TAG_variant_part won't have a size, but our
14280 representation requires one, so set it to the maximum of the
14281 child sizes, being sure to account for the offset at which
14282 each child is seen. */
14283 if (TYPE_LENGTH (fp->type) == 0)
14284 {
14285 unsigned max = 0;
14286 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
14287 {
14288 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14289 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14290 if (len > max)
14291 max = len;
14292 }
14293 TYPE_LENGTH (fp->type) = max;
14294 }
14295 }
14296 else
14297 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14298 }
14299
14300 /* Can the type given by DIE define another type? */
14301
14302 static bool
14303 type_can_define_types (const struct die_info *die)
14304 {
14305 switch (die->tag)
14306 {
14307 case DW_TAG_typedef:
14308 case DW_TAG_class_type:
14309 case DW_TAG_structure_type:
14310 case DW_TAG_union_type:
14311 case DW_TAG_enumeration_type:
14312 return true;
14313
14314 default:
14315 return false;
14316 }
14317 }
14318
14319 /* Add a type definition defined in the scope of the FIP's class. */
14320
14321 static void
14322 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14323 struct dwarf2_cu *cu)
14324 {
14325 struct decl_field fp;
14326 memset (&fp, 0, sizeof (fp));
14327
14328 gdb_assert (type_can_define_types (die));
14329
14330 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14331 fp.name = dwarf2_name (die, cu);
14332 fp.type = read_type_die (die, cu);
14333
14334 /* Save accessibility. */
14335 enum dwarf_access_attribute accessibility;
14336 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14337 if (attr != NULL)
14338 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14339 else
14340 accessibility = dwarf2_default_access_attribute (die, cu);
14341 switch (accessibility)
14342 {
14343 case DW_ACCESS_public:
14344 /* The assumed value if neither private nor protected. */
14345 break;
14346 case DW_ACCESS_private:
14347 fp.is_private = 1;
14348 break;
14349 case DW_ACCESS_protected:
14350 fp.is_protected = 1;
14351 break;
14352 default:
14353 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14354 }
14355
14356 if (die->tag == DW_TAG_typedef)
14357 fip->typedef_field_list.push_back (fp);
14358 else
14359 fip->nested_types_list.push_back (fp);
14360 }
14361
14362 /* Create the vector of fields, and attach it to the type. */
14363
14364 static void
14365 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14366 struct dwarf2_cu *cu)
14367 {
14368 int nfields = fip->nfields ();
14369
14370 /* Record the field count, allocate space for the array of fields,
14371 and create blank accessibility bitfields if necessary. */
14372 TYPE_NFIELDS (type) = nfields;
14373 TYPE_FIELDS (type) = (struct field *)
14374 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
14375
14376 if (fip->non_public_fields && cu->language != language_ada)
14377 {
14378 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14379
14380 TYPE_FIELD_PRIVATE_BITS (type) =
14381 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14382 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14383
14384 TYPE_FIELD_PROTECTED_BITS (type) =
14385 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14386 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14387
14388 TYPE_FIELD_IGNORE_BITS (type) =
14389 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14390 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14391 }
14392
14393 /* If the type has baseclasses, allocate and clear a bit vector for
14394 TYPE_FIELD_VIRTUAL_BITS. */
14395 if (!fip->baseclasses.empty () && cu->language != language_ada)
14396 {
14397 int num_bytes = B_BYTES (fip->baseclasses.size ());
14398 unsigned char *pointer;
14399
14400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14401 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14402 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14403 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14404 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14405 }
14406
14407 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14408 {
14409 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14410
14411 for (int index = 0; index < nfields; ++index)
14412 {
14413 struct nextfield &field = fip->fields[index];
14414
14415 if (field.variant.is_discriminant)
14416 di->discriminant_index = index;
14417 else if (field.variant.default_branch)
14418 di->default_index = index;
14419 else
14420 di->discriminants[index] = field.variant.discriminant_value;
14421 }
14422 }
14423
14424 /* Copy the saved-up fields into the field vector. */
14425 for (int i = 0; i < nfields; ++i)
14426 {
14427 struct nextfield &field
14428 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14429 : fip->fields[i - fip->baseclasses.size ()]);
14430
14431 TYPE_FIELD (type, i) = field.field;
14432 switch (field.accessibility)
14433 {
14434 case DW_ACCESS_private:
14435 if (cu->language != language_ada)
14436 SET_TYPE_FIELD_PRIVATE (type, i);
14437 break;
14438
14439 case DW_ACCESS_protected:
14440 if (cu->language != language_ada)
14441 SET_TYPE_FIELD_PROTECTED (type, i);
14442 break;
14443
14444 case DW_ACCESS_public:
14445 break;
14446
14447 default:
14448 /* Unknown accessibility. Complain and treat it as public. */
14449 {
14450 complaint (_("unsupported accessibility %d"),
14451 field.accessibility);
14452 }
14453 break;
14454 }
14455 if (i < fip->baseclasses.size ())
14456 {
14457 switch (field.virtuality)
14458 {
14459 case DW_VIRTUALITY_virtual:
14460 case DW_VIRTUALITY_pure_virtual:
14461 if (cu->language == language_ada)
14462 error (_("unexpected virtuality in component of Ada type"));
14463 SET_TYPE_FIELD_VIRTUAL (type, i);
14464 break;
14465 }
14466 }
14467 }
14468 }
14469
14470 /* Return true if this member function is a constructor, false
14471 otherwise. */
14472
14473 static int
14474 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14475 {
14476 const char *fieldname;
14477 const char *type_name;
14478 int len;
14479
14480 if (die->parent == NULL)
14481 return 0;
14482
14483 if (die->parent->tag != DW_TAG_structure_type
14484 && die->parent->tag != DW_TAG_union_type
14485 && die->parent->tag != DW_TAG_class_type)
14486 return 0;
14487
14488 fieldname = dwarf2_name (die, cu);
14489 type_name = dwarf2_name (die->parent, cu);
14490 if (fieldname == NULL || type_name == NULL)
14491 return 0;
14492
14493 len = strlen (fieldname);
14494 return (strncmp (fieldname, type_name, len) == 0
14495 && (type_name[len] == '\0' || type_name[len] == '<'));
14496 }
14497
14498 /* Check if the given VALUE is a recognized enum
14499 dwarf_defaulted_attribute constant according to DWARF5 spec,
14500 Table 7.24. */
14501
14502 static bool
14503 is_valid_DW_AT_defaulted (ULONGEST value)
14504 {
14505 switch (value)
14506 {
14507 case DW_DEFAULTED_no:
14508 case DW_DEFAULTED_in_class:
14509 case DW_DEFAULTED_out_of_class:
14510 return true;
14511 }
14512
14513 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
14514 return false;
14515 }
14516
14517 /* Add a member function to the proper fieldlist. */
14518
14519 static void
14520 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
14521 struct type *type, struct dwarf2_cu *cu)
14522 {
14523 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14524 struct attribute *attr;
14525 int i;
14526 struct fnfieldlist *flp = nullptr;
14527 struct fn_field *fnp;
14528 const char *fieldname;
14529 struct type *this_type;
14530 enum dwarf_access_attribute accessibility;
14531
14532 if (cu->language == language_ada)
14533 error (_("unexpected member function in Ada type"));
14534
14535 /* Get name of member function. */
14536 fieldname = dwarf2_name (die, cu);
14537 if (fieldname == NULL)
14538 return;
14539
14540 /* Look up member function name in fieldlist. */
14541 for (i = 0; i < fip->fnfieldlists.size (); i++)
14542 {
14543 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
14544 {
14545 flp = &fip->fnfieldlists[i];
14546 break;
14547 }
14548 }
14549
14550 /* Create a new fnfieldlist if necessary. */
14551 if (flp == nullptr)
14552 {
14553 fip->fnfieldlists.emplace_back ();
14554 flp = &fip->fnfieldlists.back ();
14555 flp->name = fieldname;
14556 i = fip->fnfieldlists.size () - 1;
14557 }
14558
14559 /* Create a new member function field and add it to the vector of
14560 fnfieldlists. */
14561 flp->fnfields.emplace_back ();
14562 fnp = &flp->fnfields.back ();
14563
14564 /* Delay processing of the physname until later. */
14565 if (cu->language == language_cplus)
14566 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14567 die, cu);
14568 else
14569 {
14570 const char *physname = dwarf2_physname (fieldname, die, cu);
14571 fnp->physname = physname ? physname : "";
14572 }
14573
14574 fnp->type = alloc_type (objfile);
14575 this_type = read_type_die (die, cu);
14576 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
14577 {
14578 int nparams = TYPE_NFIELDS (this_type);
14579
14580 /* TYPE is the domain of this method, and THIS_TYPE is the type
14581 of the method itself (TYPE_CODE_METHOD). */
14582 smash_to_method_type (fnp->type, type,
14583 TYPE_TARGET_TYPE (this_type),
14584 TYPE_FIELDS (this_type),
14585 TYPE_NFIELDS (this_type),
14586 TYPE_VARARGS (this_type));
14587
14588 /* Handle static member functions.
14589 Dwarf2 has no clean way to discern C++ static and non-static
14590 member functions. G++ helps GDB by marking the first
14591 parameter for non-static member functions (which is the this
14592 pointer) as artificial. We obtain this information from
14593 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
14594 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
14595 fnp->voffset = VOFFSET_STATIC;
14596 }
14597 else
14598 complaint (_("member function type missing for '%s'"),
14599 dwarf2_full_name (fieldname, die, cu));
14600
14601 /* Get fcontext from DW_AT_containing_type if present. */
14602 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
14603 fnp->fcontext = die_containing_type (die, cu);
14604
14605 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14606 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
14607
14608 /* Get accessibility. */
14609 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14610 if (attr != nullptr)
14611 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14612 else
14613 accessibility = dwarf2_default_access_attribute (die, cu);
14614 switch (accessibility)
14615 {
14616 case DW_ACCESS_private:
14617 fnp->is_private = 1;
14618 break;
14619 case DW_ACCESS_protected:
14620 fnp->is_protected = 1;
14621 break;
14622 }
14623
14624 /* Check for artificial methods. */
14625 attr = dwarf2_attr (die, DW_AT_artificial, cu);
14626 if (attr && DW_UNSND (attr) != 0)
14627 fnp->is_artificial = 1;
14628
14629 /* Check for defaulted methods. */
14630 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14631 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14632 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14633
14634 /* Check for deleted methods. */
14635 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14636 if (attr != nullptr && DW_UNSND (attr) != 0)
14637 fnp->is_deleted = 1;
14638
14639 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14640
14641 /* Get index in virtual function table if it is a virtual member
14642 function. For older versions of GCC, this is an offset in the
14643 appropriate virtual table, as specified by DW_AT_containing_type.
14644 For everyone else, it is an expression to be evaluated relative
14645 to the object address. */
14646
14647 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
14648 if (attr != nullptr)
14649 {
14650 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
14651 {
14652 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14653 {
14654 /* Old-style GCC. */
14655 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14656 }
14657 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14658 || (DW_BLOCK (attr)->size > 1
14659 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14660 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14661 {
14662 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14663 if ((fnp->voffset % cu->header.addr_size) != 0)
14664 dwarf2_complex_location_expr_complaint ();
14665 else
14666 fnp->voffset /= cu->header.addr_size;
14667 fnp->voffset += 2;
14668 }
14669 else
14670 dwarf2_complex_location_expr_complaint ();
14671
14672 if (!fnp->fcontext)
14673 {
14674 /* If there is no `this' field and no DW_AT_containing_type,
14675 we cannot actually find a base class context for the
14676 vtable! */
14677 if (TYPE_NFIELDS (this_type) == 0
14678 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14679 {
14680 complaint (_("cannot determine context for virtual member "
14681 "function \"%s\" (offset %s)"),
14682 fieldname, sect_offset_str (die->sect_off));
14683 }
14684 else
14685 {
14686 fnp->fcontext
14687 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14688 }
14689 }
14690 }
14691 else if (attr->form_is_section_offset ())
14692 {
14693 dwarf2_complex_location_expr_complaint ();
14694 }
14695 else
14696 {
14697 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14698 fieldname);
14699 }
14700 }
14701 else
14702 {
14703 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14704 if (attr && DW_UNSND (attr))
14705 {
14706 /* GCC does this, as of 2008-08-25; PR debug/37237. */
14707 complaint (_("Member function \"%s\" (offset %s) is virtual "
14708 "but the vtable offset is not specified"),
14709 fieldname, sect_offset_str (die->sect_off));
14710 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14711 TYPE_CPLUS_DYNAMIC (type) = 1;
14712 }
14713 }
14714 }
14715
14716 /* Create the vector of member function fields, and attach it to the type. */
14717
14718 static void
14719 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
14720 struct dwarf2_cu *cu)
14721 {
14722 if (cu->language == language_ada)
14723 error (_("unexpected member functions in Ada type"));
14724
14725 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14726 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
14727 TYPE_ALLOC (type,
14728 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
14729
14730 for (int i = 0; i < fip->fnfieldlists.size (); i++)
14731 {
14732 struct fnfieldlist &nf = fip->fnfieldlists[i];
14733 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
14734
14735 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14736 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
14737 fn_flp->fn_fields = (struct fn_field *)
14738 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14739
14740 for (int k = 0; k < nf.fnfields.size (); ++k)
14741 fn_flp->fn_fields[k] = nf.fnfields[k];
14742 }
14743
14744 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
14745 }
14746
14747 /* Returns non-zero if NAME is the name of a vtable member in CU's
14748 language, zero otherwise. */
14749 static int
14750 is_vtable_name (const char *name, struct dwarf2_cu *cu)
14751 {
14752 static const char vptr[] = "_vptr";
14753
14754 /* Look for the C++ form of the vtable. */
14755 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
14756 return 1;
14757
14758 return 0;
14759 }
14760
14761 /* GCC outputs unnamed structures that are really pointers to member
14762 functions, with the ABI-specified layout. If TYPE describes
14763 such a structure, smash it into a member function type.
14764
14765 GCC shouldn't do this; it should just output pointer to member DIEs.
14766 This is GCC PR debug/28767. */
14767
14768 static void
14769 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
14770 {
14771 struct type *pfn_type, *self_type, *new_type;
14772
14773 /* Check for a structure with no name and two children. */
14774 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14775 return;
14776
14777 /* Check for __pfn and __delta members. */
14778 if (TYPE_FIELD_NAME (type, 0) == NULL
14779 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14780 || TYPE_FIELD_NAME (type, 1) == NULL
14781 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14782 return;
14783
14784 /* Find the type of the method. */
14785 pfn_type = TYPE_FIELD_TYPE (type, 0);
14786 if (pfn_type == NULL
14787 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14788 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
14789 return;
14790
14791 /* Look for the "this" argument. */
14792 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14793 if (TYPE_NFIELDS (pfn_type) == 0
14794 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
14795 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
14796 return;
14797
14798 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
14799 new_type = alloc_type (objfile);
14800 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
14801 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14802 TYPE_VARARGS (pfn_type));
14803 smash_to_methodptr_type (type, new_type);
14804 }
14805
14806 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
14807 appropriate error checking and issuing complaints if there is a
14808 problem. */
14809
14810 static ULONGEST
14811 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14812 {
14813 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14814
14815 if (attr == nullptr)
14816 return 0;
14817
14818 if (!attr->form_is_constant ())
14819 {
14820 complaint (_("DW_AT_alignment must have constant form"
14821 " - DIE at %s [in module %s]"),
14822 sect_offset_str (die->sect_off),
14823 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14824 return 0;
14825 }
14826
14827 ULONGEST align;
14828 if (attr->form == DW_FORM_sdata)
14829 {
14830 LONGEST val = DW_SND (attr);
14831 if (val < 0)
14832 {
14833 complaint (_("DW_AT_alignment value must not be negative"
14834 " - DIE at %s [in module %s]"),
14835 sect_offset_str (die->sect_off),
14836 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14837 return 0;
14838 }
14839 align = val;
14840 }
14841 else
14842 align = DW_UNSND (attr);
14843
14844 if (align == 0)
14845 {
14846 complaint (_("DW_AT_alignment value must not be zero"
14847 " - DIE at %s [in module %s]"),
14848 sect_offset_str (die->sect_off),
14849 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14850 return 0;
14851 }
14852 if ((align & (align - 1)) != 0)
14853 {
14854 complaint (_("DW_AT_alignment value must be a power of 2"
14855 " - DIE at %s [in module %s]"),
14856 sect_offset_str (die->sect_off),
14857 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14858 return 0;
14859 }
14860
14861 return align;
14862 }
14863
14864 /* If the DIE has a DW_AT_alignment attribute, use its value to set
14865 the alignment for TYPE. */
14866
14867 static void
14868 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14869 struct type *type)
14870 {
14871 if (!set_type_align (type, get_alignment (cu, die)))
14872 complaint (_("DW_AT_alignment value too large"
14873 " - DIE at %s [in module %s]"),
14874 sect_offset_str (die->sect_off),
14875 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14876 }
14877
14878 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14879 constant for a type, according to DWARF5 spec, Table 5.5. */
14880
14881 static bool
14882 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14883 {
14884 switch (value)
14885 {
14886 case DW_CC_normal:
14887 case DW_CC_pass_by_reference:
14888 case DW_CC_pass_by_value:
14889 return true;
14890
14891 default:
14892 complaint (_("unrecognized DW_AT_calling_convention value "
14893 "(%s) for a type"), pulongest (value));
14894 return false;
14895 }
14896 }
14897
14898 /* Check if the given VALUE is a valid enum dwarf_calling_convention
14899 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14900 also according to GNU-specific values (see include/dwarf2.h). */
14901
14902 static bool
14903 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14904 {
14905 switch (value)
14906 {
14907 case DW_CC_normal:
14908 case DW_CC_program:
14909 case DW_CC_nocall:
14910 return true;
14911
14912 case DW_CC_GNU_renesas_sh:
14913 case DW_CC_GNU_borland_fastcall_i386:
14914 case DW_CC_GDB_IBM_OpenCL:
14915 return true;
14916
14917 default:
14918 complaint (_("unrecognized DW_AT_calling_convention value "
14919 "(%s) for a subroutine"), pulongest (value));
14920 return false;
14921 }
14922 }
14923
14924 /* Called when we find the DIE that starts a structure or union scope
14925 (definition) to create a type for the structure or union. Fill in
14926 the type's name and general properties; the members will not be
14927 processed until process_structure_scope. A symbol table entry for
14928 the type will also not be done until process_structure_scope (assuming
14929 the type has a name).
14930
14931 NOTE: we need to call these functions regardless of whether or not the
14932 DIE has a DW_AT_name attribute, since it might be an anonymous
14933 structure or union. This gets the type entered into our set of
14934 user defined types. */
14935
14936 static struct type *
14937 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
14938 {
14939 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14940 struct type *type;
14941 struct attribute *attr;
14942 const char *name;
14943
14944 /* If the definition of this type lives in .debug_types, read that type.
14945 Don't follow DW_AT_specification though, that will take us back up
14946 the chain and we want to go down. */
14947 attr = die->attr (DW_AT_signature);
14948 if (attr != nullptr)
14949 {
14950 type = get_DW_AT_signature_type (die, attr, cu);
14951
14952 /* The type's CU may not be the same as CU.
14953 Ensure TYPE is recorded with CU in die_type_hash. */
14954 return set_die_type (die, type, cu);
14955 }
14956
14957 type = alloc_type (objfile);
14958 INIT_CPLUS_SPECIFIC (type);
14959
14960 name = dwarf2_name (die, cu);
14961 if (name != NULL)
14962 {
14963 if (cu->language == language_cplus
14964 || cu->language == language_d
14965 || cu->language == language_rust)
14966 {
14967 const char *full_name = dwarf2_full_name (name, die, cu);
14968
14969 /* dwarf2_full_name might have already finished building the DIE's
14970 type. If so, there is no need to continue. */
14971 if (get_die_type (die, cu) != NULL)
14972 return get_die_type (die, cu);
14973
14974 TYPE_NAME (type) = full_name;
14975 }
14976 else
14977 {
14978 /* The name is already allocated along with this objfile, so
14979 we don't need to duplicate it for the type. */
14980 TYPE_NAME (type) = name;
14981 }
14982 }
14983
14984 if (die->tag == DW_TAG_structure_type)
14985 {
14986 TYPE_CODE (type) = TYPE_CODE_STRUCT;
14987 }
14988 else if (die->tag == DW_TAG_union_type)
14989 {
14990 TYPE_CODE (type) = TYPE_CODE_UNION;
14991 }
14992 else if (die->tag == DW_TAG_variant_part)
14993 {
14994 TYPE_CODE (type) = TYPE_CODE_UNION;
14995 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
14996 }
14997 else
14998 {
14999 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15000 }
15001
15002 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15003 TYPE_DECLARED_CLASS (type) = 1;
15004
15005 /* Store the calling convention in the type if it's available in
15006 the die. Otherwise the calling convention remains set to
15007 the default value DW_CC_normal. */
15008 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15009 if (attr != nullptr
15010 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15011 {
15012 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15013 TYPE_CPLUS_CALLING_CONVENTION (type)
15014 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15015 }
15016
15017 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15018 if (attr != nullptr)
15019 {
15020 if (attr->form_is_constant ())
15021 TYPE_LENGTH (type) = DW_UNSND (attr);
15022 else
15023 {
15024 /* For the moment, dynamic type sizes are not supported
15025 by GDB's struct type. The actual size is determined
15026 on-demand when resolving the type of a given object,
15027 so set the type's length to zero for now. Otherwise,
15028 we record an expression as the length, and that expression
15029 could lead to a very large value, which could eventually
15030 lead to us trying to allocate that much memory when creating
15031 a value of that type. */
15032 TYPE_LENGTH (type) = 0;
15033 }
15034 }
15035 else
15036 {
15037 TYPE_LENGTH (type) = 0;
15038 }
15039
15040 maybe_set_alignment (cu, die, type);
15041
15042 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15043 {
15044 /* ICC<14 does not output the required DW_AT_declaration on
15045 incomplete types, but gives them a size of zero. */
15046 TYPE_STUB (type) = 1;
15047 }
15048 else
15049 TYPE_STUB_SUPPORTED (type) = 1;
15050
15051 if (die_is_declaration (die, cu))
15052 TYPE_STUB (type) = 1;
15053 else if (attr == NULL && die->child == NULL
15054 && producer_is_realview (cu->producer))
15055 /* RealView does not output the required DW_AT_declaration
15056 on incomplete types. */
15057 TYPE_STUB (type) = 1;
15058
15059 /* We need to add the type field to the die immediately so we don't
15060 infinitely recurse when dealing with pointers to the structure
15061 type within the structure itself. */
15062 set_die_type (die, type, cu);
15063
15064 /* set_die_type should be already done. */
15065 set_descriptive_type (type, die, cu);
15066
15067 return type;
15068 }
15069
15070 /* A helper for process_structure_scope that handles a single member
15071 DIE. */
15072
15073 static void
15074 handle_struct_member_die (struct die_info *child_die, struct type *type,
15075 struct field_info *fi,
15076 std::vector<struct symbol *> *template_args,
15077 struct dwarf2_cu *cu)
15078 {
15079 if (child_die->tag == DW_TAG_member
15080 || child_die->tag == DW_TAG_variable
15081 || child_die->tag == DW_TAG_variant_part)
15082 {
15083 /* NOTE: carlton/2002-11-05: A C++ static data member
15084 should be a DW_TAG_member that is a declaration, but
15085 all versions of G++ as of this writing (so through at
15086 least 3.2.1) incorrectly generate DW_TAG_variable
15087 tags for them instead. */
15088 dwarf2_add_field (fi, child_die, cu);
15089 }
15090 else if (child_die->tag == DW_TAG_subprogram)
15091 {
15092 /* Rust doesn't have member functions in the C++ sense.
15093 However, it does emit ordinary functions as children
15094 of a struct DIE. */
15095 if (cu->language == language_rust)
15096 read_func_scope (child_die, cu);
15097 else
15098 {
15099 /* C++ member function. */
15100 dwarf2_add_member_fn (fi, child_die, type, cu);
15101 }
15102 }
15103 else if (child_die->tag == DW_TAG_inheritance)
15104 {
15105 /* C++ base class field. */
15106 dwarf2_add_field (fi, child_die, cu);
15107 }
15108 else if (type_can_define_types (child_die))
15109 dwarf2_add_type_defn (fi, child_die, cu);
15110 else if (child_die->tag == DW_TAG_template_type_param
15111 || child_die->tag == DW_TAG_template_value_param)
15112 {
15113 struct symbol *arg = new_symbol (child_die, NULL, cu);
15114
15115 if (arg != NULL)
15116 template_args->push_back (arg);
15117 }
15118 else if (child_die->tag == DW_TAG_variant)
15119 {
15120 /* In a variant we want to get the discriminant and also add a
15121 field for our sole member child. */
15122 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15123
15124 for (die_info *variant_child = child_die->child;
15125 variant_child != NULL;
15126 variant_child = variant_child->sibling)
15127 {
15128 if (variant_child->tag == DW_TAG_member)
15129 {
15130 handle_struct_member_die (variant_child, type, fi,
15131 template_args, cu);
15132 /* Only handle the one. */
15133 break;
15134 }
15135 }
15136
15137 /* We don't handle this but we might as well report it if we see
15138 it. */
15139 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
15140 complaint (_("DW_AT_discr_list is not supported yet"
15141 " - DIE at %s [in module %s]"),
15142 sect_offset_str (child_die->sect_off),
15143 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15144
15145 /* The first field was just added, so we can stash the
15146 discriminant there. */
15147 gdb_assert (!fi->fields.empty ());
15148 if (discr == NULL)
15149 fi->fields.back ().variant.default_branch = true;
15150 else
15151 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
15152 }
15153 }
15154
15155 /* Finish creating a structure or union type, including filling in
15156 its members and creating a symbol for it. */
15157
15158 static void
15159 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15160 {
15161 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15162 struct die_info *child_die;
15163 struct type *type;
15164
15165 type = get_die_type (die, cu);
15166 if (type == NULL)
15167 type = read_structure_type (die, cu);
15168
15169 /* When reading a DW_TAG_variant_part, we need to notice when we
15170 read the discriminant member, so we can record it later in the
15171 discriminant_info. */
15172 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15173 sect_offset discr_offset {};
15174 bool has_template_parameters = false;
15175
15176 if (is_variant_part)
15177 {
15178 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15179 if (discr == NULL)
15180 {
15181 /* Maybe it's a univariant form, an extension we support.
15182 In this case arrange not to check the offset. */
15183 is_variant_part = false;
15184 }
15185 else if (discr->form_is_ref ())
15186 {
15187 struct dwarf2_cu *target_cu = cu;
15188 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15189
15190 discr_offset = target_die->sect_off;
15191 }
15192 else
15193 {
15194 complaint (_("DW_AT_discr does not have DIE reference form"
15195 " - DIE at %s [in module %s]"),
15196 sect_offset_str (die->sect_off),
15197 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15198 is_variant_part = false;
15199 }
15200 }
15201
15202 if (die->child != NULL && ! die_is_declaration (die, cu))
15203 {
15204 struct field_info fi;
15205 std::vector<struct symbol *> template_args;
15206
15207 child_die = die->child;
15208
15209 while (child_die && child_die->tag)
15210 {
15211 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15212
15213 if (is_variant_part && discr_offset == child_die->sect_off)
15214 fi.fields.back ().variant.is_discriminant = true;
15215
15216 child_die = child_die->sibling;
15217 }
15218
15219 /* Attach template arguments to type. */
15220 if (!template_args.empty ())
15221 {
15222 has_template_parameters = true;
15223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15224 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15225 TYPE_TEMPLATE_ARGUMENTS (type)
15226 = XOBNEWVEC (&objfile->objfile_obstack,
15227 struct symbol *,
15228 TYPE_N_TEMPLATE_ARGUMENTS (type));
15229 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15230 template_args.data (),
15231 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15232 * sizeof (struct symbol *)));
15233 }
15234
15235 /* Attach fields and member functions to the type. */
15236 if (fi.nfields () > 0)
15237 dwarf2_attach_fields_to_type (&fi, type, cu);
15238 if (!fi.fnfieldlists.empty ())
15239 {
15240 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15241
15242 /* Get the type which refers to the base class (possibly this
15243 class itself) which contains the vtable pointer for the current
15244 class from the DW_AT_containing_type attribute. This use of
15245 DW_AT_containing_type is a GNU extension. */
15246
15247 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15248 {
15249 struct type *t = die_containing_type (die, cu);
15250
15251 set_type_vptr_basetype (type, t);
15252 if (type == t)
15253 {
15254 int i;
15255
15256 /* Our own class provides vtbl ptr. */
15257 for (i = TYPE_NFIELDS (t) - 1;
15258 i >= TYPE_N_BASECLASSES (t);
15259 --i)
15260 {
15261 const char *fieldname = TYPE_FIELD_NAME (t, i);
15262
15263 if (is_vtable_name (fieldname, cu))
15264 {
15265 set_type_vptr_fieldno (type, i);
15266 break;
15267 }
15268 }
15269
15270 /* Complain if virtual function table field not found. */
15271 if (i < TYPE_N_BASECLASSES (t))
15272 complaint (_("virtual function table pointer "
15273 "not found when defining class '%s'"),
15274 TYPE_NAME (type) ? TYPE_NAME (type) : "");
15275 }
15276 else
15277 {
15278 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15279 }
15280 }
15281 else if (cu->producer
15282 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15283 {
15284 /* The IBM XLC compiler does not provide direct indication
15285 of the containing type, but the vtable pointer is
15286 always named __vfp. */
15287
15288 int i;
15289
15290 for (i = TYPE_NFIELDS (type) - 1;
15291 i >= TYPE_N_BASECLASSES (type);
15292 --i)
15293 {
15294 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15295 {
15296 set_type_vptr_fieldno (type, i);
15297 set_type_vptr_basetype (type, type);
15298 break;
15299 }
15300 }
15301 }
15302 }
15303
15304 /* Copy fi.typedef_field_list linked list elements content into the
15305 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15306 if (!fi.typedef_field_list.empty ())
15307 {
15308 int count = fi.typedef_field_list.size ();
15309
15310 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15311 TYPE_TYPEDEF_FIELD_ARRAY (type)
15312 = ((struct decl_field *)
15313 TYPE_ALLOC (type,
15314 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15315 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15316
15317 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15318 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15319 }
15320
15321 /* Copy fi.nested_types_list linked list elements content into the
15322 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15323 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15324 {
15325 int count = fi.nested_types_list.size ();
15326
15327 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15328 TYPE_NESTED_TYPES_ARRAY (type)
15329 = ((struct decl_field *)
15330 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15331 TYPE_NESTED_TYPES_COUNT (type) = count;
15332
15333 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15334 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15335 }
15336 }
15337
15338 quirk_gcc_member_function_pointer (type, objfile);
15339 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15340 cu->rust_unions.push_back (type);
15341
15342 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15343 snapshots) has been known to create a die giving a declaration
15344 for a class that has, as a child, a die giving a definition for a
15345 nested class. So we have to process our children even if the
15346 current die is a declaration. Normally, of course, a declaration
15347 won't have any children at all. */
15348
15349 child_die = die->child;
15350
15351 while (child_die != NULL && child_die->tag)
15352 {
15353 if (child_die->tag == DW_TAG_member
15354 || child_die->tag == DW_TAG_variable
15355 || child_die->tag == DW_TAG_inheritance
15356 || child_die->tag == DW_TAG_template_value_param
15357 || child_die->tag == DW_TAG_template_type_param)
15358 {
15359 /* Do nothing. */
15360 }
15361 else
15362 process_die (child_die, cu);
15363
15364 child_die = child_die->sibling;
15365 }
15366
15367 /* Do not consider external references. According to the DWARF standard,
15368 these DIEs are identified by the fact that they have no byte_size
15369 attribute, and a declaration attribute. */
15370 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15371 || !die_is_declaration (die, cu))
15372 {
15373 struct symbol *sym = new_symbol (die, type, cu);
15374
15375 if (has_template_parameters)
15376 {
15377 struct symtab *symtab;
15378 if (sym != nullptr)
15379 symtab = symbol_symtab (sym);
15380 else if (cu->line_header != nullptr)
15381 {
15382 /* Any related symtab will do. */
15383 symtab
15384 = cu->line_header->file_names ()[0].symtab;
15385 }
15386 else
15387 {
15388 symtab = nullptr;
15389 complaint (_("could not find suitable "
15390 "symtab for template parameter"
15391 " - DIE at %s [in module %s]"),
15392 sect_offset_str (die->sect_off),
15393 objfile_name (objfile));
15394 }
15395
15396 if (symtab != nullptr)
15397 {
15398 /* Make sure that the symtab is set on the new symbols.
15399 Even though they don't appear in this symtab directly,
15400 other parts of gdb assume that symbols do, and this is
15401 reasonably true. */
15402 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15403 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15404 }
15405 }
15406 }
15407 }
15408
15409 /* Assuming DIE is an enumeration type, and TYPE is its associated type,
15410 update TYPE using some information only available in DIE's children. */
15411
15412 static void
15413 update_enumeration_type_from_children (struct die_info *die,
15414 struct type *type,
15415 struct dwarf2_cu *cu)
15416 {
15417 struct die_info *child_die;
15418 int unsigned_enum = 1;
15419 int flag_enum = 1;
15420
15421 auto_obstack obstack;
15422
15423 for (child_die = die->child;
15424 child_die != NULL && child_die->tag;
15425 child_die = child_die->sibling)
15426 {
15427 struct attribute *attr;
15428 LONGEST value;
15429 const gdb_byte *bytes;
15430 struct dwarf2_locexpr_baton *baton;
15431 const char *name;
15432
15433 if (child_die->tag != DW_TAG_enumerator)
15434 continue;
15435
15436 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15437 if (attr == NULL)
15438 continue;
15439
15440 name = dwarf2_name (child_die, cu);
15441 if (name == NULL)
15442 name = "<anonymous enumerator>";
15443
15444 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15445 &value, &bytes, &baton);
15446 if (value < 0)
15447 {
15448 unsigned_enum = 0;
15449 flag_enum = 0;
15450 }
15451 else
15452 {
15453 if (count_one_bits_ll (value) >= 2)
15454 flag_enum = 0;
15455 }
15456
15457 /* If we already know that the enum type is neither unsigned, nor
15458 a flag type, no need to look at the rest of the enumerates. */
15459 if (!unsigned_enum && !flag_enum)
15460 break;
15461 }
15462
15463 if (unsigned_enum)
15464 TYPE_UNSIGNED (type) = 1;
15465 if (flag_enum)
15466 TYPE_FLAG_ENUM (type) = 1;
15467 }
15468
15469 /* Given a DW_AT_enumeration_type die, set its type. We do not
15470 complete the type's fields yet, or create any symbols. */
15471
15472 static struct type *
15473 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
15474 {
15475 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15476 struct type *type;
15477 struct attribute *attr;
15478 const char *name;
15479
15480 /* If the definition of this type lives in .debug_types, read that type.
15481 Don't follow DW_AT_specification though, that will take us back up
15482 the chain and we want to go down. */
15483 attr = die->attr (DW_AT_signature);
15484 if (attr != nullptr)
15485 {
15486 type = get_DW_AT_signature_type (die, attr, cu);
15487
15488 /* The type's CU may not be the same as CU.
15489 Ensure TYPE is recorded with CU in die_type_hash. */
15490 return set_die_type (die, type, cu);
15491 }
15492
15493 type = alloc_type (objfile);
15494
15495 TYPE_CODE (type) = TYPE_CODE_ENUM;
15496 name = dwarf2_full_name (NULL, die, cu);
15497 if (name != NULL)
15498 TYPE_NAME (type) = name;
15499
15500 attr = dwarf2_attr (die, DW_AT_type, cu);
15501 if (attr != NULL)
15502 {
15503 struct type *underlying_type = die_type (die, cu);
15504
15505 TYPE_TARGET_TYPE (type) = underlying_type;
15506 }
15507
15508 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15509 if (attr != nullptr)
15510 {
15511 TYPE_LENGTH (type) = DW_UNSND (attr);
15512 }
15513 else
15514 {
15515 TYPE_LENGTH (type) = 0;
15516 }
15517
15518 maybe_set_alignment (cu, die, type);
15519
15520 /* The enumeration DIE can be incomplete. In Ada, any type can be
15521 declared as private in the package spec, and then defined only
15522 inside the package body. Such types are known as Taft Amendment
15523 Types. When another package uses such a type, an incomplete DIE
15524 may be generated by the compiler. */
15525 if (die_is_declaration (die, cu))
15526 TYPE_STUB (type) = 1;
15527
15528 /* Finish the creation of this type by using the enum's children.
15529 We must call this even when the underlying type has been provided
15530 so that we can determine if we're looking at a "flag" enum. */
15531 update_enumeration_type_from_children (die, type, cu);
15532
15533 /* If this type has an underlying type that is not a stub, then we
15534 may use its attributes. We always use the "unsigned" attribute
15535 in this situation, because ordinarily we guess whether the type
15536 is unsigned -- but the guess can be wrong and the underlying type
15537 can tell us the reality. However, we defer to a local size
15538 attribute if one exists, because this lets the compiler override
15539 the underlying type if needed. */
15540 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15541 {
15542 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
15543 if (TYPE_LENGTH (type) == 0)
15544 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
15545 if (TYPE_RAW_ALIGN (type) == 0
15546 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
15547 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
15548 }
15549
15550 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15551
15552 return set_die_type (die, type, cu);
15553 }
15554
15555 /* Given a pointer to a die which begins an enumeration, process all
15556 the dies that define the members of the enumeration, and create the
15557 symbol for the enumeration type.
15558
15559 NOTE: We reverse the order of the element list. */
15560
15561 static void
15562 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15563 {
15564 struct type *this_type;
15565
15566 this_type = get_die_type (die, cu);
15567 if (this_type == NULL)
15568 this_type = read_enumeration_type (die, cu);
15569
15570 if (die->child != NULL)
15571 {
15572 struct die_info *child_die;
15573 struct symbol *sym;
15574 std::vector<struct field> fields;
15575 const char *name;
15576
15577 child_die = die->child;
15578 while (child_die && child_die->tag)
15579 {
15580 if (child_die->tag != DW_TAG_enumerator)
15581 {
15582 process_die (child_die, cu);
15583 }
15584 else
15585 {
15586 name = dwarf2_name (child_die, cu);
15587 if (name)
15588 {
15589 sym = new_symbol (child_die, this_type, cu);
15590
15591 fields.emplace_back ();
15592 struct field &field = fields.back ();
15593
15594 FIELD_NAME (field) = sym->linkage_name ();
15595 FIELD_TYPE (field) = NULL;
15596 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15597 FIELD_BITSIZE (field) = 0;
15598 }
15599 }
15600
15601 child_die = child_die->sibling;
15602 }
15603
15604 if (!fields.empty ())
15605 {
15606 TYPE_NFIELDS (this_type) = fields.size ();
15607 TYPE_FIELDS (this_type) = (struct field *)
15608 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15609 memcpy (TYPE_FIELDS (this_type), fields.data (),
15610 sizeof (struct field) * fields.size ());
15611 }
15612 }
15613
15614 /* If we are reading an enum from a .debug_types unit, and the enum
15615 is a declaration, and the enum is not the signatured type in the
15616 unit, then we do not want to add a symbol for it. Adding a
15617 symbol would in some cases obscure the true definition of the
15618 enum, giving users an incomplete type when the definition is
15619 actually available. Note that we do not want to do this for all
15620 enums which are just declarations, because C++0x allows forward
15621 enum declarations. */
15622 if (cu->per_cu->is_debug_types
15623 && die_is_declaration (die, cu))
15624 {
15625 struct signatured_type *sig_type;
15626
15627 sig_type = (struct signatured_type *) cu->per_cu;
15628 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15629 if (sig_type->type_offset_in_section != die->sect_off)
15630 return;
15631 }
15632
15633 new_symbol (die, this_type, cu);
15634 }
15635
15636 /* Extract all information from a DW_TAG_array_type DIE and put it in
15637 the DIE's type field. For now, this only handles one dimensional
15638 arrays. */
15639
15640 static struct type *
15641 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
15642 {
15643 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15644 struct die_info *child_die;
15645 struct type *type;
15646 struct type *element_type, *range_type, *index_type;
15647 struct attribute *attr;
15648 const char *name;
15649 struct dynamic_prop *byte_stride_prop = NULL;
15650 unsigned int bit_stride = 0;
15651
15652 element_type = die_type (die, cu);
15653
15654 /* The die_type call above may have already set the type for this DIE. */
15655 type = get_die_type (die, cu);
15656 if (type)
15657 return type;
15658
15659 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15660 if (attr != NULL)
15661 {
15662 int stride_ok;
15663 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
15664
15665 byte_stride_prop
15666 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
15667 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15668 prop_type);
15669 if (!stride_ok)
15670 {
15671 complaint (_("unable to read array DW_AT_byte_stride "
15672 " - DIE at %s [in module %s]"),
15673 sect_offset_str (die->sect_off),
15674 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15675 /* Ignore this attribute. We will likely not be able to print
15676 arrays of this type correctly, but there is little we can do
15677 to help if we cannot read the attribute's value. */
15678 byte_stride_prop = NULL;
15679 }
15680 }
15681
15682 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15683 if (attr != NULL)
15684 bit_stride = DW_UNSND (attr);
15685
15686 /* Irix 6.2 native cc creates array types without children for
15687 arrays with unspecified length. */
15688 if (die->child == NULL)
15689 {
15690 index_type = objfile_type (objfile)->builtin_int;
15691 range_type = create_static_range_type (NULL, index_type, 0, -1);
15692 type = create_array_type_with_stride (NULL, element_type, range_type,
15693 byte_stride_prop, bit_stride);
15694 return set_die_type (die, type, cu);
15695 }
15696
15697 std::vector<struct type *> range_types;
15698 child_die = die->child;
15699 while (child_die && child_die->tag)
15700 {
15701 if (child_die->tag == DW_TAG_subrange_type)
15702 {
15703 struct type *child_type = read_type_die (child_die, cu);
15704
15705 if (child_type != NULL)
15706 {
15707 /* The range type was succesfully read. Save it for the
15708 array type creation. */
15709 range_types.push_back (child_type);
15710 }
15711 }
15712 child_die = child_die->sibling;
15713 }
15714
15715 /* Dwarf2 dimensions are output from left to right, create the
15716 necessary array types in backwards order. */
15717
15718 type = element_type;
15719
15720 if (read_array_order (die, cu) == DW_ORD_col_major)
15721 {
15722 int i = 0;
15723
15724 while (i < range_types.size ())
15725 type = create_array_type_with_stride (NULL, type, range_types[i++],
15726 byte_stride_prop, bit_stride);
15727 }
15728 else
15729 {
15730 size_t ndim = range_types.size ();
15731 while (ndim-- > 0)
15732 type = create_array_type_with_stride (NULL, type, range_types[ndim],
15733 byte_stride_prop, bit_stride);
15734 }
15735
15736 /* Understand Dwarf2 support for vector types (like they occur on
15737 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15738 array type. This is not part of the Dwarf2/3 standard yet, but a
15739 custom vendor extension. The main difference between a regular
15740 array and the vector variant is that vectors are passed by value
15741 to functions. */
15742 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
15743 if (attr != nullptr)
15744 make_vector_type (type);
15745
15746 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15747 implementation may choose to implement triple vectors using this
15748 attribute. */
15749 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15750 if (attr != nullptr)
15751 {
15752 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15753 TYPE_LENGTH (type) = DW_UNSND (attr);
15754 else
15755 complaint (_("DW_AT_byte_size for array type smaller "
15756 "than the total size of elements"));
15757 }
15758
15759 name = dwarf2_name (die, cu);
15760 if (name)
15761 TYPE_NAME (type) = name;
15762
15763 maybe_set_alignment (cu, die, type);
15764
15765 /* Install the type in the die. */
15766 set_die_type (die, type, cu);
15767
15768 /* set_die_type should be already done. */
15769 set_descriptive_type (type, die, cu);
15770
15771 return type;
15772 }
15773
15774 static enum dwarf_array_dim_ordering
15775 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
15776 {
15777 struct attribute *attr;
15778
15779 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15780
15781 if (attr != nullptr)
15782 return (enum dwarf_array_dim_ordering) DW_SND (attr);
15783
15784 /* GNU F77 is a special case, as at 08/2004 array type info is the
15785 opposite order to the dwarf2 specification, but data is still
15786 laid out as per normal fortran.
15787
15788 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15789 version checking. */
15790
15791 if (cu->language == language_fortran
15792 && cu->producer && strstr (cu->producer, "GNU F77"))
15793 {
15794 return DW_ORD_row_major;
15795 }
15796
15797 switch (cu->language_defn->la_array_ordering)
15798 {
15799 case array_column_major:
15800 return DW_ORD_col_major;
15801 case array_row_major:
15802 default:
15803 return DW_ORD_row_major;
15804 };
15805 }
15806
15807 /* Extract all information from a DW_TAG_set_type DIE and put it in
15808 the DIE's type field. */
15809
15810 static struct type *
15811 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15812 {
15813 struct type *domain_type, *set_type;
15814 struct attribute *attr;
15815
15816 domain_type = die_type (die, cu);
15817
15818 /* The die_type call above may have already set the type for this DIE. */
15819 set_type = get_die_type (die, cu);
15820 if (set_type)
15821 return set_type;
15822
15823 set_type = create_set_type (NULL, domain_type);
15824
15825 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15826 if (attr != nullptr)
15827 TYPE_LENGTH (set_type) = DW_UNSND (attr);
15828
15829 maybe_set_alignment (cu, die, set_type);
15830
15831 return set_die_type (die, set_type, cu);
15832 }
15833
15834 /* A helper for read_common_block that creates a locexpr baton.
15835 SYM is the symbol which we are marking as computed.
15836 COMMON_DIE is the DIE for the common block.
15837 COMMON_LOC is the location expression attribute for the common
15838 block itself.
15839 MEMBER_LOC is the location expression attribute for the particular
15840 member of the common block that we are processing.
15841 CU is the CU from which the above come. */
15842
15843 static void
15844 mark_common_block_symbol_computed (struct symbol *sym,
15845 struct die_info *common_die,
15846 struct attribute *common_loc,
15847 struct attribute *member_loc,
15848 struct dwarf2_cu *cu)
15849 {
15850 struct dwarf2_per_objfile *dwarf2_per_objfile
15851 = cu->per_cu->dwarf2_per_objfile;
15852 struct objfile *objfile = dwarf2_per_objfile->objfile;
15853 struct dwarf2_locexpr_baton *baton;
15854 gdb_byte *ptr;
15855 unsigned int cu_off;
15856 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15857 LONGEST offset = 0;
15858
15859 gdb_assert (common_loc && member_loc);
15860 gdb_assert (common_loc->form_is_block ());
15861 gdb_assert (member_loc->form_is_block ()
15862 || member_loc->form_is_constant ());
15863
15864 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
15865 baton->per_cu = cu->per_cu;
15866 gdb_assert (baton->per_cu);
15867
15868 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15869
15870 if (member_loc->form_is_constant ())
15871 {
15872 offset = dwarf2_get_attr_constant_value (member_loc, 0);
15873 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15874 }
15875 else
15876 baton->size += DW_BLOCK (member_loc)->size;
15877
15878 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
15879 baton->data = ptr;
15880
15881 *ptr++ = DW_OP_call4;
15882 cu_off = common_die->sect_off - cu->per_cu->sect_off;
15883 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15884 ptr += 4;
15885
15886 if (member_loc->form_is_constant ())
15887 {
15888 *ptr++ = DW_OP_addr;
15889 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15890 ptr += cu->header.addr_size;
15891 }
15892 else
15893 {
15894 /* We have to copy the data here, because DW_OP_call4 will only
15895 use a DW_AT_location attribute. */
15896 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15897 ptr += DW_BLOCK (member_loc)->size;
15898 }
15899
15900 *ptr++ = DW_OP_plus;
15901 gdb_assert (ptr - baton->data == baton->size);
15902
15903 SYMBOL_LOCATION_BATON (sym) = baton;
15904 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
15905 }
15906
15907 /* Create appropriate locally-scoped variables for all the
15908 DW_TAG_common_block entries. Also create a struct common_block
15909 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
15910 is used to separate the common blocks name namespace from regular
15911 variable names. */
15912
15913 static void
15914 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
15915 {
15916 struct attribute *attr;
15917
15918 attr = dwarf2_attr (die, DW_AT_location, cu);
15919 if (attr != nullptr)
15920 {
15921 /* Support the .debug_loc offsets. */
15922 if (attr->form_is_block ())
15923 {
15924 /* Ok. */
15925 }
15926 else if (attr->form_is_section_offset ())
15927 {
15928 dwarf2_complex_location_expr_complaint ();
15929 attr = NULL;
15930 }
15931 else
15932 {
15933 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15934 "common block member");
15935 attr = NULL;
15936 }
15937 }
15938
15939 if (die->child != NULL)
15940 {
15941 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15942 struct die_info *child_die;
15943 size_t n_entries = 0, size;
15944 struct common_block *common_block;
15945 struct symbol *sym;
15946
15947 for (child_die = die->child;
15948 child_die && child_die->tag;
15949 child_die = child_die->sibling)
15950 ++n_entries;
15951
15952 size = (sizeof (struct common_block)
15953 + (n_entries - 1) * sizeof (struct symbol *));
15954 common_block
15955 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15956 size);
15957 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15958 common_block->n_entries = 0;
15959
15960 for (child_die = die->child;
15961 child_die && child_die->tag;
15962 child_die = child_die->sibling)
15963 {
15964 /* Create the symbol in the DW_TAG_common_block block in the current
15965 symbol scope. */
15966 sym = new_symbol (child_die, NULL, cu);
15967 if (sym != NULL)
15968 {
15969 struct attribute *member_loc;
15970
15971 common_block->contents[common_block->n_entries++] = sym;
15972
15973 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15974 cu);
15975 if (member_loc)
15976 {
15977 /* GDB has handled this for a long time, but it is
15978 not specified by DWARF. It seems to have been
15979 emitted by gfortran at least as recently as:
15980 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
15981 complaint (_("Variable in common block has "
15982 "DW_AT_data_member_location "
15983 "- DIE at %s [in module %s]"),
15984 sect_offset_str (child_die->sect_off),
15985 objfile_name (objfile));
15986
15987 if (member_loc->form_is_section_offset ())
15988 dwarf2_complex_location_expr_complaint ();
15989 else if (member_loc->form_is_constant ()
15990 || member_loc->form_is_block ())
15991 {
15992 if (attr != nullptr)
15993 mark_common_block_symbol_computed (sym, die, attr,
15994 member_loc, cu);
15995 }
15996 else
15997 dwarf2_complex_location_expr_complaint ();
15998 }
15999 }
16000 }
16001
16002 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16003 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16004 }
16005 }
16006
16007 /* Create a type for a C++ namespace. */
16008
16009 static struct type *
16010 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16011 {
16012 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16013 const char *previous_prefix, *name;
16014 int is_anonymous;
16015 struct type *type;
16016
16017 /* For extensions, reuse the type of the original namespace. */
16018 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16019 {
16020 struct die_info *ext_die;
16021 struct dwarf2_cu *ext_cu = cu;
16022
16023 ext_die = dwarf2_extension (die, &ext_cu);
16024 type = read_type_die (ext_die, ext_cu);
16025
16026 /* EXT_CU may not be the same as CU.
16027 Ensure TYPE is recorded with CU in die_type_hash. */
16028 return set_die_type (die, type, cu);
16029 }
16030
16031 name = namespace_name (die, &is_anonymous, cu);
16032
16033 /* Now build the name of the current namespace. */
16034
16035 previous_prefix = determine_prefix (die, cu);
16036 if (previous_prefix[0] != '\0')
16037 name = typename_concat (&objfile->objfile_obstack,
16038 previous_prefix, name, 0, cu);
16039
16040 /* Create the type. */
16041 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16042
16043 return set_die_type (die, type, cu);
16044 }
16045
16046 /* Read a namespace scope. */
16047
16048 static void
16049 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16050 {
16051 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16052 int is_anonymous;
16053
16054 /* Add a symbol associated to this if we haven't seen the namespace
16055 before. Also, add a using directive if it's an anonymous
16056 namespace. */
16057
16058 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16059 {
16060 struct type *type;
16061
16062 type = read_type_die (die, cu);
16063 new_symbol (die, type, cu);
16064
16065 namespace_name (die, &is_anonymous, cu);
16066 if (is_anonymous)
16067 {
16068 const char *previous_prefix = determine_prefix (die, cu);
16069
16070 std::vector<const char *> excludes;
16071 add_using_directive (using_directives (cu),
16072 previous_prefix, TYPE_NAME (type), NULL,
16073 NULL, excludes, 0, &objfile->objfile_obstack);
16074 }
16075 }
16076
16077 if (die->child != NULL)
16078 {
16079 struct die_info *child_die = die->child;
16080
16081 while (child_die && child_die->tag)
16082 {
16083 process_die (child_die, cu);
16084 child_die = child_die->sibling;
16085 }
16086 }
16087 }
16088
16089 /* Read a Fortran module as type. This DIE can be only a declaration used for
16090 imported module. Still we need that type as local Fortran "use ... only"
16091 declaration imports depend on the created type in determine_prefix. */
16092
16093 static struct type *
16094 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16095 {
16096 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16097 const char *module_name;
16098 struct type *type;
16099
16100 module_name = dwarf2_name (die, cu);
16101 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16102
16103 return set_die_type (die, type, cu);
16104 }
16105
16106 /* Read a Fortran module. */
16107
16108 static void
16109 read_module (struct die_info *die, struct dwarf2_cu *cu)
16110 {
16111 struct die_info *child_die = die->child;
16112 struct type *type;
16113
16114 type = read_type_die (die, cu);
16115 new_symbol (die, type, cu);
16116
16117 while (child_die && child_die->tag)
16118 {
16119 process_die (child_die, cu);
16120 child_die = child_die->sibling;
16121 }
16122 }
16123
16124 /* Return the name of the namespace represented by DIE. Set
16125 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16126 namespace. */
16127
16128 static const char *
16129 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16130 {
16131 struct die_info *current_die;
16132 const char *name = NULL;
16133
16134 /* Loop through the extensions until we find a name. */
16135
16136 for (current_die = die;
16137 current_die != NULL;
16138 current_die = dwarf2_extension (die, &cu))
16139 {
16140 /* We don't use dwarf2_name here so that we can detect the absence
16141 of a name -> anonymous namespace. */
16142 name = dwarf2_string_attr (die, DW_AT_name, cu);
16143
16144 if (name != NULL)
16145 break;
16146 }
16147
16148 /* Is it an anonymous namespace? */
16149
16150 *is_anonymous = (name == NULL);
16151 if (*is_anonymous)
16152 name = CP_ANONYMOUS_NAMESPACE_STR;
16153
16154 return name;
16155 }
16156
16157 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16158 the user defined type vector. */
16159
16160 static struct type *
16161 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16162 {
16163 struct gdbarch *gdbarch
16164 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
16165 struct comp_unit_head *cu_header = &cu->header;
16166 struct type *type;
16167 struct attribute *attr_byte_size;
16168 struct attribute *attr_address_class;
16169 int byte_size, addr_class;
16170 struct type *target_type;
16171
16172 target_type = die_type (die, cu);
16173
16174 /* The die_type call above may have already set the type for this DIE. */
16175 type = get_die_type (die, cu);
16176 if (type)
16177 return type;
16178
16179 type = lookup_pointer_type (target_type);
16180
16181 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16182 if (attr_byte_size)
16183 byte_size = DW_UNSND (attr_byte_size);
16184 else
16185 byte_size = cu_header->addr_size;
16186
16187 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16188 if (attr_address_class)
16189 addr_class = DW_UNSND (attr_address_class);
16190 else
16191 addr_class = DW_ADDR_none;
16192
16193 ULONGEST alignment = get_alignment (cu, die);
16194
16195 /* If the pointer size, alignment, or address class is different
16196 than the default, create a type variant marked as such and set
16197 the length accordingly. */
16198 if (TYPE_LENGTH (type) != byte_size
16199 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16200 && alignment != TYPE_RAW_ALIGN (type))
16201 || addr_class != DW_ADDR_none)
16202 {
16203 if (gdbarch_address_class_type_flags_p (gdbarch))
16204 {
16205 int type_flags;
16206
16207 type_flags = gdbarch_address_class_type_flags
16208 (gdbarch, byte_size, addr_class);
16209 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16210 == 0);
16211 type = make_type_with_address_space (type, type_flags);
16212 }
16213 else if (TYPE_LENGTH (type) != byte_size)
16214 {
16215 complaint (_("invalid pointer size %d"), byte_size);
16216 }
16217 else if (TYPE_RAW_ALIGN (type) != alignment)
16218 {
16219 complaint (_("Invalid DW_AT_alignment"
16220 " - DIE at %s [in module %s]"),
16221 sect_offset_str (die->sect_off),
16222 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16223 }
16224 else
16225 {
16226 /* Should we also complain about unhandled address classes? */
16227 }
16228 }
16229
16230 TYPE_LENGTH (type) = byte_size;
16231 set_type_align (type, alignment);
16232 return set_die_type (die, type, cu);
16233 }
16234
16235 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16236 the user defined type vector. */
16237
16238 static struct type *
16239 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16240 {
16241 struct type *type;
16242 struct type *to_type;
16243 struct type *domain;
16244
16245 to_type = die_type (die, cu);
16246 domain = die_containing_type (die, cu);
16247
16248 /* The calls above may have already set the type for this DIE. */
16249 type = get_die_type (die, cu);
16250 if (type)
16251 return type;
16252
16253 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16254 type = lookup_methodptr_type (to_type);
16255 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16256 {
16257 struct type *new_type
16258 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
16259
16260 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16261 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16262 TYPE_VARARGS (to_type));
16263 type = lookup_methodptr_type (new_type);
16264 }
16265 else
16266 type = lookup_memberptr_type (to_type, domain);
16267
16268 return set_die_type (die, type, cu);
16269 }
16270
16271 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16272 the user defined type vector. */
16273
16274 static struct type *
16275 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16276 enum type_code refcode)
16277 {
16278 struct comp_unit_head *cu_header = &cu->header;
16279 struct type *type, *target_type;
16280 struct attribute *attr;
16281
16282 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16283
16284 target_type = die_type (die, cu);
16285
16286 /* The die_type call above may have already set the type for this DIE. */
16287 type = get_die_type (die, cu);
16288 if (type)
16289 return type;
16290
16291 type = lookup_reference_type (target_type, refcode);
16292 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16293 if (attr != nullptr)
16294 {
16295 TYPE_LENGTH (type) = DW_UNSND (attr);
16296 }
16297 else
16298 {
16299 TYPE_LENGTH (type) = cu_header->addr_size;
16300 }
16301 maybe_set_alignment (cu, die, type);
16302 return set_die_type (die, type, cu);
16303 }
16304
16305 /* Add the given cv-qualifiers to the element type of the array. GCC
16306 outputs DWARF type qualifiers that apply to an array, not the
16307 element type. But GDB relies on the array element type to carry
16308 the cv-qualifiers. This mimics section 6.7.3 of the C99
16309 specification. */
16310
16311 static struct type *
16312 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16313 struct type *base_type, int cnst, int voltl)
16314 {
16315 struct type *el_type, *inner_array;
16316
16317 base_type = copy_type (base_type);
16318 inner_array = base_type;
16319
16320 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16321 {
16322 TYPE_TARGET_TYPE (inner_array) =
16323 copy_type (TYPE_TARGET_TYPE (inner_array));
16324 inner_array = TYPE_TARGET_TYPE (inner_array);
16325 }
16326
16327 el_type = TYPE_TARGET_TYPE (inner_array);
16328 cnst |= TYPE_CONST (el_type);
16329 voltl |= TYPE_VOLATILE (el_type);
16330 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16331
16332 return set_die_type (die, base_type, cu);
16333 }
16334
16335 static struct type *
16336 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16337 {
16338 struct type *base_type, *cv_type;
16339
16340 base_type = die_type (die, cu);
16341
16342 /* The die_type call above may have already set the type for this DIE. */
16343 cv_type = get_die_type (die, cu);
16344 if (cv_type)
16345 return cv_type;
16346
16347 /* In case the const qualifier is applied to an array type, the element type
16348 is so qualified, not the array type (section 6.7.3 of C99). */
16349 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16350 return add_array_cv_type (die, cu, base_type, 1, 0);
16351
16352 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16353 return set_die_type (die, cv_type, cu);
16354 }
16355
16356 static struct type *
16357 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16358 {
16359 struct type *base_type, *cv_type;
16360
16361 base_type = die_type (die, cu);
16362
16363 /* The die_type call above may have already set the type for this DIE. */
16364 cv_type = get_die_type (die, cu);
16365 if (cv_type)
16366 return cv_type;
16367
16368 /* In case the volatile qualifier is applied to an array type, the
16369 element type is so qualified, not the array type (section 6.7.3
16370 of C99). */
16371 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16372 return add_array_cv_type (die, cu, base_type, 0, 1);
16373
16374 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16375 return set_die_type (die, cv_type, cu);
16376 }
16377
16378 /* Handle DW_TAG_restrict_type. */
16379
16380 static struct type *
16381 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16382 {
16383 struct type *base_type, *cv_type;
16384
16385 base_type = die_type (die, cu);
16386
16387 /* The die_type call above may have already set the type for this DIE. */
16388 cv_type = get_die_type (die, cu);
16389 if (cv_type)
16390 return cv_type;
16391
16392 cv_type = make_restrict_type (base_type);
16393 return set_die_type (die, cv_type, cu);
16394 }
16395
16396 /* Handle DW_TAG_atomic_type. */
16397
16398 static struct type *
16399 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16400 {
16401 struct type *base_type, *cv_type;
16402
16403 base_type = die_type (die, cu);
16404
16405 /* The die_type call above may have already set the type for this DIE. */
16406 cv_type = get_die_type (die, cu);
16407 if (cv_type)
16408 return cv_type;
16409
16410 cv_type = make_atomic_type (base_type);
16411 return set_die_type (die, cv_type, cu);
16412 }
16413
16414 /* Extract all information from a DW_TAG_string_type DIE and add to
16415 the user defined type vector. It isn't really a user defined type,
16416 but it behaves like one, with other DIE's using an AT_user_def_type
16417 attribute to reference it. */
16418
16419 static struct type *
16420 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16421 {
16422 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16423 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16424 struct type *type, *range_type, *index_type, *char_type;
16425 struct attribute *attr;
16426 struct dynamic_prop prop;
16427 bool length_is_constant = true;
16428 LONGEST length;
16429
16430 /* There are a couple of places where bit sizes might be made use of
16431 when parsing a DW_TAG_string_type, however, no producer that we know
16432 of make use of these. Handling bit sizes that are a multiple of the
16433 byte size is easy enough, but what about other bit sizes? Lets deal
16434 with that problem when we have to. Warn about these attributes being
16435 unsupported, then parse the type and ignore them like we always
16436 have. */
16437 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16438 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16439 {
16440 static bool warning_printed = false;
16441 if (!warning_printed)
16442 {
16443 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16444 "currently supported on DW_TAG_string_type."));
16445 warning_printed = true;
16446 }
16447 }
16448
16449 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16450 if (attr != nullptr && !attr->form_is_constant ())
16451 {
16452 /* The string length describes the location at which the length of
16453 the string can be found. The size of the length field can be
16454 specified with one of the attributes below. */
16455 struct type *prop_type;
16456 struct attribute *len
16457 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16458 if (len == nullptr)
16459 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16460 if (len != nullptr && len->form_is_constant ())
16461 {
16462 /* Pass 0 as the default as we know this attribute is constant
16463 and the default value will not be returned. */
16464 LONGEST sz = dwarf2_get_attr_constant_value (len, 0);
16465 prop_type = cu->per_cu->int_type (sz, true);
16466 }
16467 else
16468 {
16469 /* If the size is not specified then we assume it is the size of
16470 an address on this target. */
16471 prop_type = cu->per_cu->addr_sized_int_type (true);
16472 }
16473
16474 /* Convert the attribute into a dynamic property. */
16475 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16476 length = 1;
16477 else
16478 length_is_constant = false;
16479 }
16480 else if (attr != nullptr)
16481 {
16482 /* This DW_AT_string_length just contains the length with no
16483 indirection. There's no need to create a dynamic property in this
16484 case. Pass 0 for the default value as we know it will not be
16485 returned in this case. */
16486 length = dwarf2_get_attr_constant_value (attr, 0);
16487 }
16488 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
16489 {
16490 /* We don't currently support non-constant byte sizes for strings. */
16491 length = dwarf2_get_attr_constant_value (attr, 1);
16492 }
16493 else
16494 {
16495 /* Use 1 as a fallback length if we have nothing else. */
16496 length = 1;
16497 }
16498
16499 index_type = objfile_type (objfile)->builtin_int;
16500 if (length_is_constant)
16501 range_type = create_static_range_type (NULL, index_type, 1, length);
16502 else
16503 {
16504 struct dynamic_prop low_bound;
16505
16506 low_bound.kind = PROP_CONST;
16507 low_bound.data.const_val = 1;
16508 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16509 }
16510 char_type = language_string_char_type (cu->language_defn, gdbarch);
16511 type = create_string_type (NULL, char_type, range_type);
16512
16513 return set_die_type (die, type, cu);
16514 }
16515
16516 /* Assuming that DIE corresponds to a function, returns nonzero
16517 if the function is prototyped. */
16518
16519 static int
16520 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16521 {
16522 struct attribute *attr;
16523
16524 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16525 if (attr && (DW_UNSND (attr) != 0))
16526 return 1;
16527
16528 /* The DWARF standard implies that the DW_AT_prototyped attribute
16529 is only meaningful for C, but the concept also extends to other
16530 languages that allow unprototyped functions (Eg: Objective C).
16531 For all other languages, assume that functions are always
16532 prototyped. */
16533 if (cu->language != language_c
16534 && cu->language != language_objc
16535 && cu->language != language_opencl)
16536 return 1;
16537
16538 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16539 prototyped and unprototyped functions; default to prototyped,
16540 since that is more common in modern code (and RealView warns
16541 about unprototyped functions). */
16542 if (producer_is_realview (cu->producer))
16543 return 1;
16544
16545 return 0;
16546 }
16547
16548 /* Handle DIES due to C code like:
16549
16550 struct foo
16551 {
16552 int (*funcp)(int a, long l);
16553 int b;
16554 };
16555
16556 ('funcp' generates a DW_TAG_subroutine_type DIE). */
16557
16558 static struct type *
16559 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
16560 {
16561 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16562 struct type *type; /* Type that this function returns. */
16563 struct type *ftype; /* Function that returns above type. */
16564 struct attribute *attr;
16565
16566 type = die_type (die, cu);
16567
16568 /* The die_type call above may have already set the type for this DIE. */
16569 ftype = get_die_type (die, cu);
16570 if (ftype)
16571 return ftype;
16572
16573 ftype = lookup_function_type (type);
16574
16575 if (prototyped_function_p (die, cu))
16576 TYPE_PROTOTYPED (ftype) = 1;
16577
16578 /* Store the calling convention in the type if it's available in
16579 the subroutine die. Otherwise set the calling convention to
16580 the default value DW_CC_normal. */
16581 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16582 if (attr != nullptr
16583 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16584 TYPE_CALLING_CONVENTION (ftype)
16585 = (enum dwarf_calling_convention) (DW_UNSND (attr));
16586 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16587 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16588 else
16589 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
16590
16591 /* Record whether the function returns normally to its caller or not
16592 if the DWARF producer set that information. */
16593 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16594 if (attr && (DW_UNSND (attr) != 0))
16595 TYPE_NO_RETURN (ftype) = 1;
16596
16597 /* We need to add the subroutine type to the die immediately so
16598 we don't infinitely recurse when dealing with parameters
16599 declared as the same subroutine type. */
16600 set_die_type (die, ftype, cu);
16601
16602 if (die->child != NULL)
16603 {
16604 struct type *void_type = objfile_type (objfile)->builtin_void;
16605 struct die_info *child_die;
16606 int nparams, iparams;
16607
16608 /* Count the number of parameters.
16609 FIXME: GDB currently ignores vararg functions, but knows about
16610 vararg member functions. */
16611 nparams = 0;
16612 child_die = die->child;
16613 while (child_die && child_die->tag)
16614 {
16615 if (child_die->tag == DW_TAG_formal_parameter)
16616 nparams++;
16617 else if (child_die->tag == DW_TAG_unspecified_parameters)
16618 TYPE_VARARGS (ftype) = 1;
16619 child_die = child_die->sibling;
16620 }
16621
16622 /* Allocate storage for parameters and fill them in. */
16623 TYPE_NFIELDS (ftype) = nparams;
16624 TYPE_FIELDS (ftype) = (struct field *)
16625 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
16626
16627 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16628 even if we error out during the parameters reading below. */
16629 for (iparams = 0; iparams < nparams; iparams++)
16630 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16631
16632 iparams = 0;
16633 child_die = die->child;
16634 while (child_die && child_die->tag)
16635 {
16636 if (child_die->tag == DW_TAG_formal_parameter)
16637 {
16638 struct type *arg_type;
16639
16640 /* DWARF version 2 has no clean way to discern C++
16641 static and non-static member functions. G++ helps
16642 GDB by marking the first parameter for non-static
16643 member functions (which is the this pointer) as
16644 artificial. We pass this information to
16645 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16646
16647 DWARF version 3 added DW_AT_object_pointer, which GCC
16648 4.5 does not yet generate. */
16649 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
16650 if (attr != nullptr)
16651 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16652 else
16653 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
16654 arg_type = die_type (child_die, cu);
16655
16656 /* RealView does not mark THIS as const, which the testsuite
16657 expects. GCC marks THIS as const in method definitions,
16658 but not in the class specifications (GCC PR 43053). */
16659 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16660 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16661 {
16662 int is_this = 0;
16663 struct dwarf2_cu *arg_cu = cu;
16664 const char *name = dwarf2_name (child_die, cu);
16665
16666 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
16667 if (attr != nullptr)
16668 {
16669 /* If the compiler emits this, use it. */
16670 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16671 is_this = 1;
16672 }
16673 else if (name && strcmp (name, "this") == 0)
16674 /* Function definitions will have the argument names. */
16675 is_this = 1;
16676 else if (name == NULL && iparams == 0)
16677 /* Declarations may not have the names, so like
16678 elsewhere in GDB, assume an artificial first
16679 argument is "this". */
16680 is_this = 1;
16681
16682 if (is_this)
16683 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16684 arg_type, 0);
16685 }
16686
16687 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
16688 iparams++;
16689 }
16690 child_die = child_die->sibling;
16691 }
16692 }
16693
16694 return ftype;
16695 }
16696
16697 static struct type *
16698 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
16699 {
16700 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16701 const char *name = NULL;
16702 struct type *this_type, *target_type;
16703
16704 name = dwarf2_full_name (NULL, die, cu);
16705 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16706 TYPE_TARGET_STUB (this_type) = 1;
16707 set_die_type (die, this_type, cu);
16708 target_type = die_type (die, cu);
16709 if (target_type != this_type)
16710 TYPE_TARGET_TYPE (this_type) = target_type;
16711 else
16712 {
16713 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16714 spec and cause infinite loops in GDB. */
16715 complaint (_("Self-referential DW_TAG_typedef "
16716 "- DIE at %s [in module %s]"),
16717 sect_offset_str (die->sect_off), objfile_name (objfile));
16718 TYPE_TARGET_TYPE (this_type) = NULL;
16719 }
16720 if (name == NULL)
16721 {
16722 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16723 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16724 Handle these by just returning the target type, rather than
16725 constructing an anonymous typedef type and trying to handle this
16726 elsewhere. */
16727 set_die_type (die, target_type, cu);
16728 return target_type;
16729 }
16730 return this_type;
16731 }
16732
16733 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16734 (which may be different from NAME) to the architecture back-end to allow
16735 it to guess the correct format if necessary. */
16736
16737 static struct type *
16738 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
16739 const char *name_hint, enum bfd_endian byte_order)
16740 {
16741 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16742 const struct floatformat **format;
16743 struct type *type;
16744
16745 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16746 if (format)
16747 type = init_float_type (objfile, bits, name, format, byte_order);
16748 else
16749 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16750
16751 return type;
16752 }
16753
16754 /* Allocate an integer type of size BITS and name NAME. */
16755
16756 static struct type *
16757 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16758 int bits, int unsigned_p, const char *name)
16759 {
16760 struct type *type;
16761
16762 /* Versions of Intel's C Compiler generate an integer type called "void"
16763 instead of using DW_TAG_unspecified_type. This has been seen on
16764 at least versions 14, 17, and 18. */
16765 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16766 && strcmp (name, "void") == 0)
16767 type = objfile_type (objfile)->builtin_void;
16768 else
16769 type = init_integer_type (objfile, bits, unsigned_p, name);
16770
16771 return type;
16772 }
16773
16774 /* Initialise and return a floating point type of size BITS suitable for
16775 use as a component of a complex number. The NAME_HINT is passed through
16776 when initialising the floating point type and is the name of the complex
16777 type.
16778
16779 As DWARF doesn't currently provide an explicit name for the components
16780 of a complex number, but it can be helpful to have these components
16781 named, we try to select a suitable name based on the size of the
16782 component. */
16783 static struct type *
16784 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16785 struct objfile *objfile,
16786 int bits, const char *name_hint,
16787 enum bfd_endian byte_order)
16788 {
16789 gdbarch *gdbarch = get_objfile_arch (objfile);
16790 struct type *tt = nullptr;
16791
16792 /* Try to find a suitable floating point builtin type of size BITS.
16793 We're going to use the name of this type as the name for the complex
16794 target type that we are about to create. */
16795 switch (cu->language)
16796 {
16797 case language_fortran:
16798 switch (bits)
16799 {
16800 case 32:
16801 tt = builtin_f_type (gdbarch)->builtin_real;
16802 break;
16803 case 64:
16804 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16805 break;
16806 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16807 case 128:
16808 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16809 break;
16810 }
16811 break;
16812 default:
16813 switch (bits)
16814 {
16815 case 32:
16816 tt = builtin_type (gdbarch)->builtin_float;
16817 break;
16818 case 64:
16819 tt = builtin_type (gdbarch)->builtin_double;
16820 break;
16821 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16822 case 128:
16823 tt = builtin_type (gdbarch)->builtin_long_double;
16824 break;
16825 }
16826 break;
16827 }
16828
16829 /* If the type we found doesn't match the size we were looking for, then
16830 pretend we didn't find a type at all, the complex target type we
16831 create will then be nameless. */
16832 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
16833 tt = nullptr;
16834
16835 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
16836 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
16837 }
16838
16839 /* Find a representation of a given base type and install
16840 it in the TYPE field of the die. */
16841
16842 static struct type *
16843 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
16844 {
16845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
16846 struct type *type;
16847 struct attribute *attr;
16848 int encoding = 0, bits = 0;
16849 const char *name;
16850 gdbarch *arch;
16851
16852 attr = dwarf2_attr (die, DW_AT_encoding, cu);
16853 if (attr != nullptr)
16854 encoding = DW_UNSND (attr);
16855 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16856 if (attr != nullptr)
16857 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
16858 name = dwarf2_name (die, cu);
16859 if (!name)
16860 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
16861
16862 arch = get_objfile_arch (objfile);
16863 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16864
16865 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16866 if (attr)
16867 {
16868 int endianity = DW_UNSND (attr);
16869
16870 switch (endianity)
16871 {
16872 case DW_END_big:
16873 byte_order = BFD_ENDIAN_BIG;
16874 break;
16875 case DW_END_little:
16876 byte_order = BFD_ENDIAN_LITTLE;
16877 break;
16878 default:
16879 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16880 break;
16881 }
16882 }
16883
16884 switch (encoding)
16885 {
16886 case DW_ATE_address:
16887 /* Turn DW_ATE_address into a void * pointer. */
16888 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
16889 type = init_pointer_type (objfile, bits, name, type);
16890 break;
16891 case DW_ATE_boolean:
16892 type = init_boolean_type (objfile, bits, 1, name);
16893 break;
16894 case DW_ATE_complex_float:
16895 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16896 byte_order);
16897 type = init_complex_type (objfile, name, type);
16898 break;
16899 case DW_ATE_decimal_float:
16900 type = init_decfloat_type (objfile, bits, name);
16901 break;
16902 case DW_ATE_float:
16903 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
16904 break;
16905 case DW_ATE_signed:
16906 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16907 break;
16908 case DW_ATE_unsigned:
16909 if (cu->language == language_fortran
16910 && name
16911 && startswith (name, "character("))
16912 type = init_character_type (objfile, bits, 1, name);
16913 else
16914 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16915 break;
16916 case DW_ATE_signed_char:
16917 if (cu->language == language_ada || cu->language == language_m2
16918 || cu->language == language_pascal
16919 || cu->language == language_fortran)
16920 type = init_character_type (objfile, bits, 0, name);
16921 else
16922 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
16923 break;
16924 case DW_ATE_unsigned_char:
16925 if (cu->language == language_ada || cu->language == language_m2
16926 || cu->language == language_pascal
16927 || cu->language == language_fortran
16928 || cu->language == language_rust)
16929 type = init_character_type (objfile, bits, 1, name);
16930 else
16931 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16932 break;
16933 case DW_ATE_UTF:
16934 {
16935 if (bits == 16)
16936 type = builtin_type (arch)->builtin_char16;
16937 else if (bits == 32)
16938 type = builtin_type (arch)->builtin_char32;
16939 else
16940 {
16941 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
16942 bits);
16943 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
16944 }
16945 return set_die_type (die, type, cu);
16946 }
16947 break;
16948
16949 default:
16950 complaint (_("unsupported DW_AT_encoding: '%s'"),
16951 dwarf_type_encoding_name (encoding));
16952 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16953 break;
16954 }
16955
16956 if (name && strcmp (name, "char") == 0)
16957 TYPE_NOSIGN (type) = 1;
16958
16959 maybe_set_alignment (cu, die, type);
16960
16961 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
16962
16963 return set_die_type (die, type, cu);
16964 }
16965
16966 /* Parse dwarf attribute if it's a block, reference or constant and put the
16967 resulting value of the attribute into struct bound_prop.
16968 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16969
16970 static int
16971 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
16972 struct dwarf2_cu *cu, struct dynamic_prop *prop,
16973 struct type *default_type)
16974 {
16975 struct dwarf2_property_baton *baton;
16976 struct obstack *obstack
16977 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16978
16979 gdb_assert (default_type != NULL);
16980
16981 if (attr == NULL || prop == NULL)
16982 return 0;
16983
16984 if (attr->form_is_block ())
16985 {
16986 baton = XOBNEW (obstack, struct dwarf2_property_baton);
16987 baton->property_type = default_type;
16988 baton->locexpr.per_cu = cu->per_cu;
16989 baton->locexpr.size = DW_BLOCK (attr)->size;
16990 baton->locexpr.data = DW_BLOCK (attr)->data;
16991 switch (attr->name)
16992 {
16993 case DW_AT_string_length:
16994 baton->locexpr.is_reference = true;
16995 break;
16996 default:
16997 baton->locexpr.is_reference = false;
16998 break;
16999 }
17000 prop->data.baton = baton;
17001 prop->kind = PROP_LOCEXPR;
17002 gdb_assert (prop->data.baton != NULL);
17003 }
17004 else if (attr->form_is_ref ())
17005 {
17006 struct dwarf2_cu *target_cu = cu;
17007 struct die_info *target_die;
17008 struct attribute *target_attr;
17009
17010 target_die = follow_die_ref (die, attr, &target_cu);
17011 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17012 if (target_attr == NULL)
17013 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17014 target_cu);
17015 if (target_attr == NULL)
17016 return 0;
17017
17018 switch (target_attr->name)
17019 {
17020 case DW_AT_location:
17021 if (target_attr->form_is_section_offset ())
17022 {
17023 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17024 baton->property_type = die_type (target_die, target_cu);
17025 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17026 prop->data.baton = baton;
17027 prop->kind = PROP_LOCLIST;
17028 gdb_assert (prop->data.baton != NULL);
17029 }
17030 else if (target_attr->form_is_block ())
17031 {
17032 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17033 baton->property_type = die_type (target_die, target_cu);
17034 baton->locexpr.per_cu = cu->per_cu;
17035 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17036 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17037 baton->locexpr.is_reference = true;
17038 prop->data.baton = baton;
17039 prop->kind = PROP_LOCEXPR;
17040 gdb_assert (prop->data.baton != NULL);
17041 }
17042 else
17043 {
17044 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17045 "dynamic property");
17046 return 0;
17047 }
17048 break;
17049 case DW_AT_data_member_location:
17050 {
17051 LONGEST offset;
17052
17053 if (!handle_data_member_location (target_die, target_cu,
17054 &offset))
17055 return 0;
17056
17057 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17058 baton->property_type = read_type_die (target_die->parent,
17059 target_cu);
17060 baton->offset_info.offset = offset;
17061 baton->offset_info.type = die_type (target_die, target_cu);
17062 prop->data.baton = baton;
17063 prop->kind = PROP_ADDR_OFFSET;
17064 break;
17065 }
17066 }
17067 }
17068 else if (attr->form_is_constant ())
17069 {
17070 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17071 prop->kind = PROP_CONST;
17072 }
17073 else
17074 {
17075 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17076 dwarf2_name (die, cu));
17077 return 0;
17078 }
17079
17080 return 1;
17081 }
17082
17083 /* See read.h. */
17084
17085 struct type *
17086 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17087 {
17088 struct objfile *objfile = dwarf2_per_objfile->objfile;
17089 struct type *int_type;
17090
17091 /* Helper macro to examine the various builtin types. */
17092 #define TRY_TYPE(F) \
17093 int_type = (unsigned_p \
17094 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17095 : objfile_type (objfile)->builtin_ ## F); \
17096 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17097 return int_type
17098
17099 TRY_TYPE (char);
17100 TRY_TYPE (short);
17101 TRY_TYPE (int);
17102 TRY_TYPE (long);
17103 TRY_TYPE (long_long);
17104
17105 #undef TRY_TYPE
17106
17107 gdb_assert_not_reached ("unable to find suitable integer type");
17108 }
17109
17110 /* See read.h. */
17111
17112 struct type *
17113 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17114 {
17115 int addr_size = this->addr_size ();
17116 return int_type (addr_size, unsigned_p);
17117 }
17118
17119 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17120 present (which is valid) then compute the default type based on the
17121 compilation units address size. */
17122
17123 static struct type *
17124 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17125 {
17126 struct type *index_type = die_type (die, cu);
17127
17128 /* Dwarf-2 specifications explicitly allows to create subrange types
17129 without specifying a base type.
17130 In that case, the base type must be set to the type of
17131 the lower bound, upper bound or count, in that order, if any of these
17132 three attributes references an object that has a type.
17133 If no base type is found, the Dwarf-2 specifications say that
17134 a signed integer type of size equal to the size of an address should
17135 be used.
17136 For the following C code: `extern char gdb_int [];'
17137 GCC produces an empty range DIE.
17138 FIXME: muller/2010-05-28: Possible references to object for low bound,
17139 high bound or count are not yet handled by this code. */
17140 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
17141 index_type = cu->per_cu->addr_sized_int_type (false);
17142
17143 return index_type;
17144 }
17145
17146 /* Read the given DW_AT_subrange DIE. */
17147
17148 static struct type *
17149 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17150 {
17151 struct type *base_type, *orig_base_type;
17152 struct type *range_type;
17153 struct attribute *attr;
17154 struct dynamic_prop low, high;
17155 int low_default_is_valid;
17156 int high_bound_is_count = 0;
17157 const char *name;
17158 ULONGEST negative_mask;
17159
17160 orig_base_type = read_subrange_index_type (die, cu);
17161
17162 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17163 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17164 creating the range type, but we use the result of check_typedef
17165 when examining properties of the type. */
17166 base_type = check_typedef (orig_base_type);
17167
17168 /* The die_type call above may have already set the type for this DIE. */
17169 range_type = get_die_type (die, cu);
17170 if (range_type)
17171 return range_type;
17172
17173 low.kind = PROP_CONST;
17174 high.kind = PROP_CONST;
17175 high.data.const_val = 0;
17176
17177 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17178 omitting DW_AT_lower_bound. */
17179 switch (cu->language)
17180 {
17181 case language_c:
17182 case language_cplus:
17183 low.data.const_val = 0;
17184 low_default_is_valid = 1;
17185 break;
17186 case language_fortran:
17187 low.data.const_val = 1;
17188 low_default_is_valid = 1;
17189 break;
17190 case language_d:
17191 case language_objc:
17192 case language_rust:
17193 low.data.const_val = 0;
17194 low_default_is_valid = (cu->header.version >= 4);
17195 break;
17196 case language_ada:
17197 case language_m2:
17198 case language_pascal:
17199 low.data.const_val = 1;
17200 low_default_is_valid = (cu->header.version >= 4);
17201 break;
17202 default:
17203 low.data.const_val = 0;
17204 low_default_is_valid = 0;
17205 break;
17206 }
17207
17208 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17209 if (attr != nullptr)
17210 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17211 else if (!low_default_is_valid)
17212 complaint (_("Missing DW_AT_lower_bound "
17213 "- DIE at %s [in module %s]"),
17214 sect_offset_str (die->sect_off),
17215 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17216
17217 struct attribute *attr_ub, *attr_count;
17218 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17219 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17220 {
17221 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17222 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17223 {
17224 /* If bounds are constant do the final calculation here. */
17225 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17226 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17227 else
17228 high_bound_is_count = 1;
17229 }
17230 else
17231 {
17232 if (attr_ub != NULL)
17233 complaint (_("Unresolved DW_AT_upper_bound "
17234 "- DIE at %s [in module %s]"),
17235 sect_offset_str (die->sect_off),
17236 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17237 if (attr_count != NULL)
17238 complaint (_("Unresolved DW_AT_count "
17239 "- DIE at %s [in module %s]"),
17240 sect_offset_str (die->sect_off),
17241 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17242 }
17243 }
17244
17245 LONGEST bias = 0;
17246 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17247 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17248 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17249
17250 /* Normally, the DWARF producers are expected to use a signed
17251 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17252 But this is unfortunately not always the case, as witnessed
17253 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17254 is used instead. To work around that ambiguity, we treat
17255 the bounds as signed, and thus sign-extend their values, when
17256 the base type is signed. */
17257 negative_mask =
17258 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17259 if (low.kind == PROP_CONST
17260 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17261 low.data.const_val |= negative_mask;
17262 if (high.kind == PROP_CONST
17263 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17264 high.data.const_val |= negative_mask;
17265
17266 /* Check for bit and byte strides. */
17267 struct dynamic_prop byte_stride_prop;
17268 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17269 if (attr_byte_stride != nullptr)
17270 {
17271 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17272 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17273 prop_type);
17274 }
17275
17276 struct dynamic_prop bit_stride_prop;
17277 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17278 if (attr_bit_stride != nullptr)
17279 {
17280 /* It only makes sense to have either a bit or byte stride. */
17281 if (attr_byte_stride != nullptr)
17282 {
17283 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17284 "- DIE at %s [in module %s]"),
17285 sect_offset_str (die->sect_off),
17286 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17287 attr_bit_stride = nullptr;
17288 }
17289 else
17290 {
17291 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17292 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17293 prop_type);
17294 }
17295 }
17296
17297 if (attr_byte_stride != nullptr
17298 || attr_bit_stride != nullptr)
17299 {
17300 bool byte_stride_p = (attr_byte_stride != nullptr);
17301 struct dynamic_prop *stride
17302 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17303
17304 range_type
17305 = create_range_type_with_stride (NULL, orig_base_type, &low,
17306 &high, bias, stride, byte_stride_p);
17307 }
17308 else
17309 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17310
17311 if (high_bound_is_count)
17312 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17313
17314 /* Ada expects an empty array on no boundary attributes. */
17315 if (attr == NULL && cu->language != language_ada)
17316 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17317
17318 name = dwarf2_name (die, cu);
17319 if (name)
17320 TYPE_NAME (range_type) = name;
17321
17322 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17323 if (attr != nullptr)
17324 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17325
17326 maybe_set_alignment (cu, die, range_type);
17327
17328 set_die_type (die, range_type, cu);
17329
17330 /* set_die_type should be already done. */
17331 set_descriptive_type (range_type, die, cu);
17332
17333 return range_type;
17334 }
17335
17336 static struct type *
17337 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17338 {
17339 struct type *type;
17340
17341 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17342 NULL);
17343 TYPE_NAME (type) = dwarf2_name (die, cu);
17344
17345 /* In Ada, an unspecified type is typically used when the description
17346 of the type is deferred to a different unit. When encountering
17347 such a type, we treat it as a stub, and try to resolve it later on,
17348 when needed. */
17349 if (cu->language == language_ada)
17350 TYPE_STUB (type) = 1;
17351
17352 return set_die_type (die, type, cu);
17353 }
17354
17355 /* Read a single die and all its descendents. Set the die's sibling
17356 field to NULL; set other fields in the die correctly, and set all
17357 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17358 location of the info_ptr after reading all of those dies. PARENT
17359 is the parent of the die in question. */
17360
17361 static struct die_info *
17362 read_die_and_children (const struct die_reader_specs *reader,
17363 const gdb_byte *info_ptr,
17364 const gdb_byte **new_info_ptr,
17365 struct die_info *parent)
17366 {
17367 struct die_info *die;
17368 const gdb_byte *cur_ptr;
17369
17370 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17371 if (die == NULL)
17372 {
17373 *new_info_ptr = cur_ptr;
17374 return NULL;
17375 }
17376 store_in_ref_table (die, reader->cu);
17377
17378 if (die->has_children)
17379 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17380 else
17381 {
17382 die->child = NULL;
17383 *new_info_ptr = cur_ptr;
17384 }
17385
17386 die->sibling = NULL;
17387 die->parent = parent;
17388 return die;
17389 }
17390
17391 /* Read a die, all of its descendents, and all of its siblings; set
17392 all of the fields of all of the dies correctly. Arguments are as
17393 in read_die_and_children. */
17394
17395 static struct die_info *
17396 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17397 const gdb_byte *info_ptr,
17398 const gdb_byte **new_info_ptr,
17399 struct die_info *parent)
17400 {
17401 struct die_info *first_die, *last_sibling;
17402 const gdb_byte *cur_ptr;
17403
17404 cur_ptr = info_ptr;
17405 first_die = last_sibling = NULL;
17406
17407 while (1)
17408 {
17409 struct die_info *die
17410 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17411
17412 if (die == NULL)
17413 {
17414 *new_info_ptr = cur_ptr;
17415 return first_die;
17416 }
17417
17418 if (!first_die)
17419 first_die = die;
17420 else
17421 last_sibling->sibling = die;
17422
17423 last_sibling = die;
17424 }
17425 }
17426
17427 /* Read a die, all of its descendents, and all of its siblings; set
17428 all of the fields of all of the dies correctly. Arguments are as
17429 in read_die_and_children.
17430 This the main entry point for reading a DIE and all its children. */
17431
17432 static struct die_info *
17433 read_die_and_siblings (const struct die_reader_specs *reader,
17434 const gdb_byte *info_ptr,
17435 const gdb_byte **new_info_ptr,
17436 struct die_info *parent)
17437 {
17438 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17439 new_info_ptr, parent);
17440
17441 if (dwarf_die_debug)
17442 {
17443 fprintf_unfiltered (gdb_stdlog,
17444 "Read die from %s@0x%x of %s:\n",
17445 reader->die_section->get_name (),
17446 (unsigned) (info_ptr - reader->die_section->buffer),
17447 bfd_get_filename (reader->abfd));
17448 dump_die (die, dwarf_die_debug);
17449 }
17450
17451 return die;
17452 }
17453
17454 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17455 attributes.
17456 The caller is responsible for filling in the extra attributes
17457 and updating (*DIEP)->num_attrs.
17458 Set DIEP to point to a newly allocated die with its information,
17459 except for its child, sibling, and parent fields. */
17460
17461 static const gdb_byte *
17462 read_full_die_1 (const struct die_reader_specs *reader,
17463 struct die_info **diep, const gdb_byte *info_ptr,
17464 int num_extra_attrs)
17465 {
17466 unsigned int abbrev_number, bytes_read, i;
17467 struct abbrev_info *abbrev;
17468 struct die_info *die;
17469 struct dwarf2_cu *cu = reader->cu;
17470 bfd *abfd = reader->abfd;
17471
17472 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
17473 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17474 info_ptr += bytes_read;
17475 if (!abbrev_number)
17476 {
17477 *diep = NULL;
17478 return info_ptr;
17479 }
17480
17481 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
17482 if (!abbrev)
17483 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17484 abbrev_number,
17485 bfd_get_filename (abfd));
17486
17487 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
17488 die->sect_off = sect_off;
17489 die->tag = abbrev->tag;
17490 die->abbrev = abbrev_number;
17491 die->has_children = abbrev->has_children;
17492
17493 /* Make the result usable.
17494 The caller needs to update num_attrs after adding the extra
17495 attributes. */
17496 die->num_attrs = abbrev->num_attrs;
17497
17498 std::vector<int> indexes_that_need_reprocess;
17499 for (i = 0; i < abbrev->num_attrs; ++i)
17500 {
17501 bool need_reprocess;
17502 info_ptr =
17503 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17504 info_ptr, &need_reprocess);
17505 if (need_reprocess)
17506 indexes_that_need_reprocess.push_back (i);
17507 }
17508
17509 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
17510 if (attr != nullptr)
17511 cu->str_offsets_base = DW_UNSND (attr);
17512
17513 auto maybe_addr_base = die->addr_base ();
17514 if (maybe_addr_base.has_value ())
17515 cu->addr_base = *maybe_addr_base;
17516 for (int index : indexes_that_need_reprocess)
17517 read_attribute_reprocess (reader, &die->attrs[index]);
17518 *diep = die;
17519 return info_ptr;
17520 }
17521
17522 /* Read a die and all its attributes.
17523 Set DIEP to point to a newly allocated die with its information,
17524 except for its child, sibling, and parent fields. */
17525
17526 static const gdb_byte *
17527 read_full_die (const struct die_reader_specs *reader,
17528 struct die_info **diep, const gdb_byte *info_ptr)
17529 {
17530 const gdb_byte *result;
17531
17532 result = read_full_die_1 (reader, diep, info_ptr, 0);
17533
17534 if (dwarf_die_debug)
17535 {
17536 fprintf_unfiltered (gdb_stdlog,
17537 "Read die from %s@0x%x of %s:\n",
17538 reader->die_section->get_name (),
17539 (unsigned) (info_ptr - reader->die_section->buffer),
17540 bfd_get_filename (reader->abfd));
17541 dump_die (*diep, dwarf_die_debug);
17542 }
17543
17544 return result;
17545 }
17546 \f
17547
17548 /* Returns nonzero if TAG represents a type that we might generate a partial
17549 symbol for. */
17550
17551 static int
17552 is_type_tag_for_partial (int tag)
17553 {
17554 switch (tag)
17555 {
17556 #if 0
17557 /* Some types that would be reasonable to generate partial symbols for,
17558 that we don't at present. */
17559 case DW_TAG_array_type:
17560 case DW_TAG_file_type:
17561 case DW_TAG_ptr_to_member_type:
17562 case DW_TAG_set_type:
17563 case DW_TAG_string_type:
17564 case DW_TAG_subroutine_type:
17565 #endif
17566 case DW_TAG_base_type:
17567 case DW_TAG_class_type:
17568 case DW_TAG_interface_type:
17569 case DW_TAG_enumeration_type:
17570 case DW_TAG_structure_type:
17571 case DW_TAG_subrange_type:
17572 case DW_TAG_typedef:
17573 case DW_TAG_union_type:
17574 return 1;
17575 default:
17576 return 0;
17577 }
17578 }
17579
17580 /* Load all DIEs that are interesting for partial symbols into memory. */
17581
17582 static struct partial_die_info *
17583 load_partial_dies (const struct die_reader_specs *reader,
17584 const gdb_byte *info_ptr, int building_psymtab)
17585 {
17586 struct dwarf2_cu *cu = reader->cu;
17587 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
17588 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
17589 unsigned int bytes_read;
17590 unsigned int load_all = 0;
17591 int nesting_level = 1;
17592
17593 parent_die = NULL;
17594 last_die = NULL;
17595
17596 gdb_assert (cu->per_cu != NULL);
17597 if (cu->per_cu->load_all_dies)
17598 load_all = 1;
17599
17600 cu->partial_dies
17601 = htab_create_alloc_ex (cu->header.length / 12,
17602 partial_die_hash,
17603 partial_die_eq,
17604 NULL,
17605 &cu->comp_unit_obstack,
17606 hashtab_obstack_allocate,
17607 dummy_obstack_deallocate);
17608
17609 while (1)
17610 {
17611 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
17612
17613 /* A NULL abbrev means the end of a series of children. */
17614 if (abbrev == NULL)
17615 {
17616 if (--nesting_level == 0)
17617 return first_die;
17618
17619 info_ptr += bytes_read;
17620 last_die = parent_die;
17621 parent_die = parent_die->die_parent;
17622 continue;
17623 }
17624
17625 /* Check for template arguments. We never save these; if
17626 they're seen, we just mark the parent, and go on our way. */
17627 if (parent_die != NULL
17628 && cu->language == language_cplus
17629 && (abbrev->tag == DW_TAG_template_type_param
17630 || abbrev->tag == DW_TAG_template_value_param))
17631 {
17632 parent_die->has_template_arguments = 1;
17633
17634 if (!load_all)
17635 {
17636 /* We don't need a partial DIE for the template argument. */
17637 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17638 continue;
17639 }
17640 }
17641
17642 /* We only recurse into c++ subprograms looking for template arguments.
17643 Skip their other children. */
17644 if (!load_all
17645 && cu->language == language_cplus
17646 && parent_die != NULL
17647 && parent_die->tag == DW_TAG_subprogram)
17648 {
17649 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17650 continue;
17651 }
17652
17653 /* Check whether this DIE is interesting enough to save. Normally
17654 we would not be interested in members here, but there may be
17655 later variables referencing them via DW_AT_specification (for
17656 static members). */
17657 if (!load_all
17658 && !is_type_tag_for_partial (abbrev->tag)
17659 && abbrev->tag != DW_TAG_constant
17660 && abbrev->tag != DW_TAG_enumerator
17661 && abbrev->tag != DW_TAG_subprogram
17662 && abbrev->tag != DW_TAG_inlined_subroutine
17663 && abbrev->tag != DW_TAG_lexical_block
17664 && abbrev->tag != DW_TAG_variable
17665 && abbrev->tag != DW_TAG_namespace
17666 && abbrev->tag != DW_TAG_module
17667 && abbrev->tag != DW_TAG_member
17668 && abbrev->tag != DW_TAG_imported_unit
17669 && abbrev->tag != DW_TAG_imported_declaration)
17670 {
17671 /* Otherwise we skip to the next sibling, if any. */
17672 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
17673 continue;
17674 }
17675
17676 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17677 abbrev);
17678
17679 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
17680
17681 /* This two-pass algorithm for processing partial symbols has a
17682 high cost in cache pressure. Thus, handle some simple cases
17683 here which cover the majority of C partial symbols. DIEs
17684 which neither have specification tags in them, nor could have
17685 specification tags elsewhere pointing at them, can simply be
17686 processed and discarded.
17687
17688 This segment is also optional; scan_partial_symbols and
17689 add_partial_symbol will handle these DIEs if we chain
17690 them in normally. When compilers which do not emit large
17691 quantities of duplicate debug information are more common,
17692 this code can probably be removed. */
17693
17694 /* Any complete simple types at the top level (pretty much all
17695 of them, for a language without namespaces), can be processed
17696 directly. */
17697 if (parent_die == NULL
17698 && pdi.has_specification == 0
17699 && pdi.is_declaration == 0
17700 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17701 || pdi.tag == DW_TAG_base_type
17702 || pdi.tag == DW_TAG_subrange_type))
17703 {
17704 if (building_psymtab && pdi.name != NULL)
17705 add_psymbol_to_list (pdi.name, false,
17706 VAR_DOMAIN, LOC_TYPEDEF, -1,
17707 psymbol_placement::STATIC,
17708 0, cu->language, objfile);
17709 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17710 continue;
17711 }
17712
17713 /* The exception for DW_TAG_typedef with has_children above is
17714 a workaround of GCC PR debug/47510. In the case of this complaint
17715 type_name_or_error will error on such types later.
17716
17717 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17718 it could not find the child DIEs referenced later, this is checked
17719 above. In correct DWARF DW_TAG_typedef should have no children. */
17720
17721 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
17722 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
17723 "- DIE at %s [in module %s]"),
17724 sect_offset_str (pdi.sect_off), objfile_name (objfile));
17725
17726 /* If we're at the second level, and we're an enumerator, and
17727 our parent has no specification (meaning possibly lives in a
17728 namespace elsewhere), then we can add the partial symbol now
17729 instead of queueing it. */
17730 if (pdi.tag == DW_TAG_enumerator
17731 && parent_die != NULL
17732 && parent_die->die_parent == NULL
17733 && parent_die->tag == DW_TAG_enumeration_type
17734 && parent_die->has_specification == 0)
17735 {
17736 if (pdi.name == NULL)
17737 complaint (_("malformed enumerator DIE ignored"));
17738 else if (building_psymtab)
17739 add_psymbol_to_list (pdi.name, false,
17740 VAR_DOMAIN, LOC_CONST, -1,
17741 cu->language == language_cplus
17742 ? psymbol_placement::GLOBAL
17743 : psymbol_placement::STATIC,
17744 0, cu->language, objfile);
17745
17746 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
17747 continue;
17748 }
17749
17750 struct partial_die_info *part_die
17751 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
17752
17753 /* We'll save this DIE so link it in. */
17754 part_die->die_parent = parent_die;
17755 part_die->die_sibling = NULL;
17756 part_die->die_child = NULL;
17757
17758 if (last_die && last_die == parent_die)
17759 last_die->die_child = part_die;
17760 else if (last_die)
17761 last_die->die_sibling = part_die;
17762
17763 last_die = part_die;
17764
17765 if (first_die == NULL)
17766 first_die = part_die;
17767
17768 /* Maybe add the DIE to the hash table. Not all DIEs that we
17769 find interesting need to be in the hash table, because we
17770 also have the parent/sibling/child chains; only those that we
17771 might refer to by offset later during partial symbol reading.
17772
17773 For now this means things that might have be the target of a
17774 DW_AT_specification, DW_AT_abstract_origin, or
17775 DW_AT_extension. DW_AT_extension will refer only to
17776 namespaces; DW_AT_abstract_origin refers to functions (and
17777 many things under the function DIE, but we do not recurse
17778 into function DIEs during partial symbol reading) and
17779 possibly variables as well; DW_AT_specification refers to
17780 declarations. Declarations ought to have the DW_AT_declaration
17781 flag. It happens that GCC forgets to put it in sometimes, but
17782 only for functions, not for types.
17783
17784 Adding more things than necessary to the hash table is harmless
17785 except for the performance cost. Adding too few will result in
17786 wasted time in find_partial_die, when we reread the compilation
17787 unit with load_all_dies set. */
17788
17789 if (load_all
17790 || abbrev->tag == DW_TAG_constant
17791 || abbrev->tag == DW_TAG_subprogram
17792 || abbrev->tag == DW_TAG_variable
17793 || abbrev->tag == DW_TAG_namespace
17794 || part_die->is_declaration)
17795 {
17796 void **slot;
17797
17798 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
17799 to_underlying (part_die->sect_off),
17800 INSERT);
17801 *slot = part_die;
17802 }
17803
17804 /* For some DIEs we want to follow their children (if any). For C
17805 we have no reason to follow the children of structures; for other
17806 languages we have to, so that we can get at method physnames
17807 to infer fully qualified class names, for DW_AT_specification,
17808 and for C++ template arguments. For C++, we also look one level
17809 inside functions to find template arguments (if the name of the
17810 function does not already contain the template arguments).
17811
17812 For Ada and Fortran, we need to scan the children of subprograms
17813 and lexical blocks as well because these languages allow the
17814 definition of nested entities that could be interesting for the
17815 debugger, such as nested subprograms for instance. */
17816 if (last_die->has_children
17817 && (load_all
17818 || last_die->tag == DW_TAG_namespace
17819 || last_die->tag == DW_TAG_module
17820 || last_die->tag == DW_TAG_enumeration_type
17821 || (cu->language == language_cplus
17822 && last_die->tag == DW_TAG_subprogram
17823 && (last_die->name == NULL
17824 || strchr (last_die->name, '<') == NULL))
17825 || (cu->language != language_c
17826 && (last_die->tag == DW_TAG_class_type
17827 || last_die->tag == DW_TAG_interface_type
17828 || last_die->tag == DW_TAG_structure_type
17829 || last_die->tag == DW_TAG_union_type))
17830 || ((cu->language == language_ada
17831 || cu->language == language_fortran)
17832 && (last_die->tag == DW_TAG_subprogram
17833 || last_die->tag == DW_TAG_lexical_block))))
17834 {
17835 nesting_level++;
17836 parent_die = last_die;
17837 continue;
17838 }
17839
17840 /* Otherwise we skip to the next sibling, if any. */
17841 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
17842
17843 /* Back to the top, do it again. */
17844 }
17845 }
17846
17847 partial_die_info::partial_die_info (sect_offset sect_off_,
17848 struct abbrev_info *abbrev)
17849 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17850 {
17851 }
17852
17853 /* Read a minimal amount of information into the minimal die structure.
17854 INFO_PTR should point just after the initial uleb128 of a DIE. */
17855
17856 const gdb_byte *
17857 partial_die_info::read (const struct die_reader_specs *reader,
17858 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
17859 {
17860 struct dwarf2_cu *cu = reader->cu;
17861 struct dwarf2_per_objfile *dwarf2_per_objfile
17862 = cu->per_cu->dwarf2_per_objfile;
17863 unsigned int i;
17864 int has_low_pc_attr = 0;
17865 int has_high_pc_attr = 0;
17866 int high_pc_relative = 0;
17867
17868 std::vector<struct attribute> attr_vec (abbrev.num_attrs);
17869 for (i = 0; i < abbrev.num_attrs; ++i)
17870 {
17871 bool need_reprocess;
17872 info_ptr = read_attribute (reader, &attr_vec[i], &abbrev.attrs[i],
17873 info_ptr, &need_reprocess);
17874 /* String and address offsets that need to do the reprocessing have
17875 already been read at this point, so there is no need to wait until
17876 the loop terminates to do the reprocessing. */
17877 if (need_reprocess)
17878 read_attribute_reprocess (reader, &attr_vec[i]);
17879 attribute &attr = attr_vec[i];
17880 /* Store the data if it is of an attribute we want to keep in a
17881 partial symbol table. */
17882 switch (attr.name)
17883 {
17884 case DW_AT_name:
17885 switch (tag)
17886 {
17887 case DW_TAG_compile_unit:
17888 case DW_TAG_partial_unit:
17889 case DW_TAG_type_unit:
17890 /* Compilation units have a DW_AT_name that is a filename, not
17891 a source language identifier. */
17892 case DW_TAG_enumeration_type:
17893 case DW_TAG_enumerator:
17894 /* These tags always have simple identifiers already; no need
17895 to canonicalize them. */
17896 name = DW_STRING (&attr);
17897 break;
17898 default:
17899 {
17900 struct objfile *objfile = dwarf2_per_objfile->objfile;
17901
17902 name
17903 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
17904 }
17905 break;
17906 }
17907 break;
17908 case DW_AT_linkage_name:
17909 case DW_AT_MIPS_linkage_name:
17910 /* Note that both forms of linkage name might appear. We
17911 assume they will be the same, and we only store the last
17912 one we see. */
17913 linkage_name = DW_STRING (&attr);
17914 break;
17915 case DW_AT_low_pc:
17916 has_low_pc_attr = 1;
17917 lowpc = attr.value_as_address ();
17918 break;
17919 case DW_AT_high_pc:
17920 has_high_pc_attr = 1;
17921 highpc = attr.value_as_address ();
17922 if (cu->header.version >= 4 && attr.form_is_constant ())
17923 high_pc_relative = 1;
17924 break;
17925 case DW_AT_location:
17926 /* Support the .debug_loc offsets. */
17927 if (attr.form_is_block ())
17928 {
17929 d.locdesc = DW_BLOCK (&attr);
17930 }
17931 else if (attr.form_is_section_offset ())
17932 {
17933 dwarf2_complex_location_expr_complaint ();
17934 }
17935 else
17936 {
17937 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17938 "partial symbol information");
17939 }
17940 break;
17941 case DW_AT_external:
17942 is_external = DW_UNSND (&attr);
17943 break;
17944 case DW_AT_declaration:
17945 is_declaration = DW_UNSND (&attr);
17946 break;
17947 case DW_AT_type:
17948 has_type = 1;
17949 break;
17950 case DW_AT_abstract_origin:
17951 case DW_AT_specification:
17952 case DW_AT_extension:
17953 has_specification = 1;
17954 spec_offset = dwarf2_get_ref_die_offset (&attr);
17955 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
17956 || cu->per_cu->is_dwz);
17957 break;
17958 case DW_AT_sibling:
17959 /* Ignore absolute siblings, they might point outside of
17960 the current compile unit. */
17961 if (attr.form == DW_FORM_ref_addr)
17962 complaint (_("ignoring absolute DW_AT_sibling"));
17963 else
17964 {
17965 const gdb_byte *buffer = reader->buffer;
17966 sect_offset off = dwarf2_get_ref_die_offset (&attr);
17967 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
17968
17969 if (sibling_ptr < info_ptr)
17970 complaint (_("DW_AT_sibling points backwards"));
17971 else if (sibling_ptr > reader->buffer_end)
17972 reader->die_section->overflow_complaint ();
17973 else
17974 sibling = sibling_ptr;
17975 }
17976 break;
17977 case DW_AT_byte_size:
17978 has_byte_size = 1;
17979 break;
17980 case DW_AT_const_value:
17981 has_const_value = 1;
17982 break;
17983 case DW_AT_calling_convention:
17984 /* DWARF doesn't provide a way to identify a program's source-level
17985 entry point. DW_AT_calling_convention attributes are only meant
17986 to describe functions' calling conventions.
17987
17988 However, because it's a necessary piece of information in
17989 Fortran, and before DWARF 4 DW_CC_program was the only
17990 piece of debugging information whose definition refers to
17991 a 'main program' at all, several compilers marked Fortran
17992 main programs with DW_CC_program --- even when those
17993 functions use the standard calling conventions.
17994
17995 Although DWARF now specifies a way to provide this
17996 information, we support this practice for backward
17997 compatibility. */
17998 if (DW_UNSND (&attr) == DW_CC_program
17999 && cu->language == language_fortran)
18000 main_subprogram = 1;
18001 break;
18002 case DW_AT_inline:
18003 if (DW_UNSND (&attr) == DW_INL_inlined
18004 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18005 may_be_inlined = 1;
18006 break;
18007
18008 case DW_AT_import:
18009 if (tag == DW_TAG_imported_unit)
18010 {
18011 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18012 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18013 || cu->per_cu->is_dwz);
18014 }
18015 break;
18016
18017 case DW_AT_main_subprogram:
18018 main_subprogram = DW_UNSND (&attr);
18019 break;
18020
18021 case DW_AT_ranges:
18022 {
18023 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18024 but that requires a full DIE, so instead we just
18025 reimplement it. */
18026 int need_ranges_base = tag != DW_TAG_compile_unit;
18027 unsigned int ranges_offset = (DW_UNSND (&attr)
18028 + (need_ranges_base
18029 ? cu->ranges_base
18030 : 0));
18031
18032 /* Value of the DW_AT_ranges attribute is the offset in the
18033 .debug_ranges section. */
18034 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18035 nullptr))
18036 has_pc_info = 1;
18037 }
18038 break;
18039
18040 default:
18041 break;
18042 }
18043 }
18044
18045 /* For Ada, if both the name and the linkage name appear, we prefer
18046 the latter. This lets "catch exception" work better, regardless
18047 of the order in which the name and linkage name were emitted.
18048 Really, though, this is just a workaround for the fact that gdb
18049 doesn't store both the name and the linkage name. */
18050 if (cu->language == language_ada && linkage_name != nullptr)
18051 name = linkage_name;
18052
18053 if (high_pc_relative)
18054 highpc += lowpc;
18055
18056 if (has_low_pc_attr && has_high_pc_attr)
18057 {
18058 /* When using the GNU linker, .gnu.linkonce. sections are used to
18059 eliminate duplicate copies of functions and vtables and such.
18060 The linker will arbitrarily choose one and discard the others.
18061 The AT_*_pc values for such functions refer to local labels in
18062 these sections. If the section from that file was discarded, the
18063 labels are not in the output, so the relocs get a value of 0.
18064 If this is a discarded function, mark the pc bounds as invalid,
18065 so that GDB will ignore it. */
18066 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
18067 {
18068 struct objfile *objfile = dwarf2_per_objfile->objfile;
18069 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18070
18071 complaint (_("DW_AT_low_pc %s is zero "
18072 "for DIE at %s [in module %s]"),
18073 paddress (gdbarch, lowpc),
18074 sect_offset_str (sect_off),
18075 objfile_name (objfile));
18076 }
18077 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18078 else if (lowpc >= highpc)
18079 {
18080 struct objfile *objfile = dwarf2_per_objfile->objfile;
18081 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18082
18083 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18084 "for DIE at %s [in module %s]"),
18085 paddress (gdbarch, lowpc),
18086 paddress (gdbarch, highpc),
18087 sect_offset_str (sect_off),
18088 objfile_name (objfile));
18089 }
18090 else
18091 has_pc_info = 1;
18092 }
18093
18094 return info_ptr;
18095 }
18096
18097 /* Find a cached partial DIE at OFFSET in CU. */
18098
18099 struct partial_die_info *
18100 dwarf2_cu::find_partial_die (sect_offset sect_off)
18101 {
18102 struct partial_die_info *lookup_die = NULL;
18103 struct partial_die_info part_die (sect_off);
18104
18105 lookup_die = ((struct partial_die_info *)
18106 htab_find_with_hash (partial_dies, &part_die,
18107 to_underlying (sect_off)));
18108
18109 return lookup_die;
18110 }
18111
18112 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18113 except in the case of .debug_types DIEs which do not reference
18114 outside their CU (they do however referencing other types via
18115 DW_FORM_ref_sig8). */
18116
18117 static const struct cu_partial_die_info
18118 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18119 {
18120 struct dwarf2_per_objfile *dwarf2_per_objfile
18121 = cu->per_cu->dwarf2_per_objfile;
18122 struct objfile *objfile = dwarf2_per_objfile->objfile;
18123 struct dwarf2_per_cu_data *per_cu = NULL;
18124 struct partial_die_info *pd = NULL;
18125
18126 if (offset_in_dwz == cu->per_cu->is_dwz
18127 && cu->header.offset_in_cu_p (sect_off))
18128 {
18129 pd = cu->find_partial_die (sect_off);
18130 if (pd != NULL)
18131 return { cu, pd };
18132 /* We missed recording what we needed.
18133 Load all dies and try again. */
18134 per_cu = cu->per_cu;
18135 }
18136 else
18137 {
18138 /* TUs don't reference other CUs/TUs (except via type signatures). */
18139 if (cu->per_cu->is_debug_types)
18140 {
18141 error (_("Dwarf Error: Type Unit at offset %s contains"
18142 " external reference to offset %s [in module %s].\n"),
18143 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18144 bfd_get_filename (objfile->obfd));
18145 }
18146 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18147 dwarf2_per_objfile);
18148
18149 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18150 load_partial_comp_unit (per_cu);
18151
18152 per_cu->cu->last_used = 0;
18153 pd = per_cu->cu->find_partial_die (sect_off);
18154 }
18155
18156 /* If we didn't find it, and not all dies have been loaded,
18157 load them all and try again. */
18158
18159 if (pd == NULL && per_cu->load_all_dies == 0)
18160 {
18161 per_cu->load_all_dies = 1;
18162
18163 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18164 THIS_CU->cu may already be in use. So we can't just free it and
18165 replace its DIEs with the ones we read in. Instead, we leave those
18166 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18167 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18168 set. */
18169 load_partial_comp_unit (per_cu);
18170
18171 pd = per_cu->cu->find_partial_die (sect_off);
18172 }
18173
18174 if (pd == NULL)
18175 internal_error (__FILE__, __LINE__,
18176 _("could not find partial DIE %s "
18177 "in cache [from module %s]\n"),
18178 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18179 return { per_cu->cu, pd };
18180 }
18181
18182 /* See if we can figure out if the class lives in a namespace. We do
18183 this by looking for a member function; its demangled name will
18184 contain namespace info, if there is any. */
18185
18186 static void
18187 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18188 struct dwarf2_cu *cu)
18189 {
18190 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18191 what template types look like, because the demangler
18192 frequently doesn't give the same name as the debug info. We
18193 could fix this by only using the demangled name to get the
18194 prefix (but see comment in read_structure_type). */
18195
18196 struct partial_die_info *real_pdi;
18197 struct partial_die_info *child_pdi;
18198
18199 /* If this DIE (this DIE's specification, if any) has a parent, then
18200 we should not do this. We'll prepend the parent's fully qualified
18201 name when we create the partial symbol. */
18202
18203 real_pdi = struct_pdi;
18204 while (real_pdi->has_specification)
18205 {
18206 auto res = find_partial_die (real_pdi->spec_offset,
18207 real_pdi->spec_is_dwz, cu);
18208 real_pdi = res.pdi;
18209 cu = res.cu;
18210 }
18211
18212 if (real_pdi->die_parent != NULL)
18213 return;
18214
18215 for (child_pdi = struct_pdi->die_child;
18216 child_pdi != NULL;
18217 child_pdi = child_pdi->die_sibling)
18218 {
18219 if (child_pdi->tag == DW_TAG_subprogram
18220 && child_pdi->linkage_name != NULL)
18221 {
18222 gdb::unique_xmalloc_ptr<char> actual_class_name
18223 (language_class_name_from_physname (cu->language_defn,
18224 child_pdi->linkage_name));
18225 if (actual_class_name != NULL)
18226 {
18227 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18228 struct_pdi->name = objfile->intern (actual_class_name.get ());
18229 }
18230 break;
18231 }
18232 }
18233 }
18234
18235 void
18236 partial_die_info::fixup (struct dwarf2_cu *cu)
18237 {
18238 /* Once we've fixed up a die, there's no point in doing so again.
18239 This also avoids a memory leak if we were to call
18240 guess_partial_die_structure_name multiple times. */
18241 if (fixup_called)
18242 return;
18243
18244 /* If we found a reference attribute and the DIE has no name, try
18245 to find a name in the referred to DIE. */
18246
18247 if (name == NULL && has_specification)
18248 {
18249 struct partial_die_info *spec_die;
18250
18251 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18252 spec_die = res.pdi;
18253 cu = res.cu;
18254
18255 spec_die->fixup (cu);
18256
18257 if (spec_die->name)
18258 {
18259 name = spec_die->name;
18260
18261 /* Copy DW_AT_external attribute if it is set. */
18262 if (spec_die->is_external)
18263 is_external = spec_die->is_external;
18264 }
18265 }
18266
18267 /* Set default names for some unnamed DIEs. */
18268
18269 if (name == NULL && tag == DW_TAG_namespace)
18270 name = CP_ANONYMOUS_NAMESPACE_STR;
18271
18272 /* If there is no parent die to provide a namespace, and there are
18273 children, see if we can determine the namespace from their linkage
18274 name. */
18275 if (cu->language == language_cplus
18276 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
18277 && die_parent == NULL
18278 && has_children
18279 && (tag == DW_TAG_class_type
18280 || tag == DW_TAG_structure_type
18281 || tag == DW_TAG_union_type))
18282 guess_partial_die_structure_name (this, cu);
18283
18284 /* GCC might emit a nameless struct or union that has a linkage
18285 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18286 if (name == NULL
18287 && (tag == DW_TAG_class_type
18288 || tag == DW_TAG_interface_type
18289 || tag == DW_TAG_structure_type
18290 || tag == DW_TAG_union_type)
18291 && linkage_name != NULL)
18292 {
18293 gdb::unique_xmalloc_ptr<char> demangled
18294 (gdb_demangle (linkage_name, DMGL_TYPES));
18295 if (demangled != nullptr)
18296 {
18297 const char *base;
18298
18299 /* Strip any leading namespaces/classes, keep only the base name.
18300 DW_AT_name for named DIEs does not contain the prefixes. */
18301 base = strrchr (demangled.get (), ':');
18302 if (base && base > demangled.get () && base[-1] == ':')
18303 base++;
18304 else
18305 base = demangled.get ();
18306
18307 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18308 name = objfile->intern (base);
18309 }
18310 }
18311
18312 fixup_called = 1;
18313 }
18314
18315 /* Process the attributes that had to be skipped in the first round. These
18316 attributes are the ones that need str_offsets_base or addr_base attributes.
18317 They could not have been processed in the first round, because at the time
18318 the values of str_offsets_base or addr_base may not have been known. */
18319 void read_attribute_reprocess (const struct die_reader_specs *reader,
18320 struct attribute *attr)
18321 {
18322 struct dwarf2_cu *cu = reader->cu;
18323 switch (attr->form)
18324 {
18325 case DW_FORM_addrx:
18326 case DW_FORM_GNU_addr_index:
18327 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18328 break;
18329 case DW_FORM_strx:
18330 case DW_FORM_strx1:
18331 case DW_FORM_strx2:
18332 case DW_FORM_strx3:
18333 case DW_FORM_strx4:
18334 case DW_FORM_GNU_str_index:
18335 {
18336 unsigned int str_index = DW_UNSND (attr);
18337 if (reader->dwo_file != NULL)
18338 {
18339 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18340 DW_STRING_IS_CANONICAL (attr) = 0;
18341 }
18342 else
18343 {
18344 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18345 DW_STRING_IS_CANONICAL (attr) = 0;
18346 }
18347 break;
18348 }
18349 default:
18350 gdb_assert_not_reached (_("Unexpected DWARF form."));
18351 }
18352 }
18353
18354 /* Read an attribute value described by an attribute form. */
18355
18356 static const gdb_byte *
18357 read_attribute_value (const struct die_reader_specs *reader,
18358 struct attribute *attr, unsigned form,
18359 LONGEST implicit_const, const gdb_byte *info_ptr,
18360 bool *need_reprocess)
18361 {
18362 struct dwarf2_cu *cu = reader->cu;
18363 struct dwarf2_per_objfile *dwarf2_per_objfile
18364 = cu->per_cu->dwarf2_per_objfile;
18365 struct objfile *objfile = dwarf2_per_objfile->objfile;
18366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
18367 bfd *abfd = reader->abfd;
18368 struct comp_unit_head *cu_header = &cu->header;
18369 unsigned int bytes_read;
18370 struct dwarf_block *blk;
18371 *need_reprocess = false;
18372
18373 attr->form = (enum dwarf_form) form;
18374 switch (form)
18375 {
18376 case DW_FORM_ref_addr:
18377 if (cu->header.version == 2)
18378 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18379 &bytes_read);
18380 else
18381 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18382 &bytes_read);
18383 info_ptr += bytes_read;
18384 break;
18385 case DW_FORM_GNU_ref_alt:
18386 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18387 info_ptr += bytes_read;
18388 break;
18389 case DW_FORM_addr:
18390 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18391 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18392 info_ptr += bytes_read;
18393 break;
18394 case DW_FORM_block2:
18395 blk = dwarf_alloc_block (cu);
18396 blk->size = read_2_bytes (abfd, info_ptr);
18397 info_ptr += 2;
18398 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18399 info_ptr += blk->size;
18400 DW_BLOCK (attr) = blk;
18401 break;
18402 case DW_FORM_block4:
18403 blk = dwarf_alloc_block (cu);
18404 blk->size = read_4_bytes (abfd, info_ptr);
18405 info_ptr += 4;
18406 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18407 info_ptr += blk->size;
18408 DW_BLOCK (attr) = blk;
18409 break;
18410 case DW_FORM_data2:
18411 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18412 info_ptr += 2;
18413 break;
18414 case DW_FORM_data4:
18415 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18416 info_ptr += 4;
18417 break;
18418 case DW_FORM_data8:
18419 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18420 info_ptr += 8;
18421 break;
18422 case DW_FORM_data16:
18423 blk = dwarf_alloc_block (cu);
18424 blk->size = 16;
18425 blk->data = read_n_bytes (abfd, info_ptr, 16);
18426 info_ptr += 16;
18427 DW_BLOCK (attr) = blk;
18428 break;
18429 case DW_FORM_sec_offset:
18430 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
18431 info_ptr += bytes_read;
18432 break;
18433 case DW_FORM_string:
18434 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
18435 DW_STRING_IS_CANONICAL (attr) = 0;
18436 info_ptr += bytes_read;
18437 break;
18438 case DW_FORM_strp:
18439 if (!cu->per_cu->is_dwz)
18440 {
18441 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18442 abfd, info_ptr, cu_header,
18443 &bytes_read);
18444 DW_STRING_IS_CANONICAL (attr) = 0;
18445 info_ptr += bytes_read;
18446 break;
18447 }
18448 /* FALLTHROUGH */
18449 case DW_FORM_line_strp:
18450 if (!cu->per_cu->is_dwz)
18451 {
18452 DW_STRING (attr)
18453 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18454 &bytes_read);
18455 DW_STRING_IS_CANONICAL (attr) = 0;
18456 info_ptr += bytes_read;
18457 break;
18458 }
18459 /* FALLTHROUGH */
18460 case DW_FORM_GNU_strp_alt:
18461 {
18462 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
18463 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18464 &bytes_read);
18465
18466 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
18467 DW_STRING_IS_CANONICAL (attr) = 0;
18468 info_ptr += bytes_read;
18469 }
18470 break;
18471 case DW_FORM_exprloc:
18472 case DW_FORM_block:
18473 blk = dwarf_alloc_block (cu);
18474 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18475 info_ptr += bytes_read;
18476 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18477 info_ptr += blk->size;
18478 DW_BLOCK (attr) = blk;
18479 break;
18480 case DW_FORM_block1:
18481 blk = dwarf_alloc_block (cu);
18482 blk->size = read_1_byte (abfd, info_ptr);
18483 info_ptr += 1;
18484 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18485 info_ptr += blk->size;
18486 DW_BLOCK (attr) = blk;
18487 break;
18488 case DW_FORM_data1:
18489 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18490 info_ptr += 1;
18491 break;
18492 case DW_FORM_flag:
18493 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18494 info_ptr += 1;
18495 break;
18496 case DW_FORM_flag_present:
18497 DW_UNSND (attr) = 1;
18498 break;
18499 case DW_FORM_sdata:
18500 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18501 info_ptr += bytes_read;
18502 break;
18503 case DW_FORM_udata:
18504 case DW_FORM_rnglistx:
18505 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18506 info_ptr += bytes_read;
18507 break;
18508 case DW_FORM_ref1:
18509 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18510 + read_1_byte (abfd, info_ptr));
18511 info_ptr += 1;
18512 break;
18513 case DW_FORM_ref2:
18514 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18515 + read_2_bytes (abfd, info_ptr));
18516 info_ptr += 2;
18517 break;
18518 case DW_FORM_ref4:
18519 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18520 + read_4_bytes (abfd, info_ptr));
18521 info_ptr += 4;
18522 break;
18523 case DW_FORM_ref8:
18524 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18525 + read_8_bytes (abfd, info_ptr));
18526 info_ptr += 8;
18527 break;
18528 case DW_FORM_ref_sig8:
18529 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
18530 info_ptr += 8;
18531 break;
18532 case DW_FORM_ref_udata:
18533 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
18534 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
18535 info_ptr += bytes_read;
18536 break;
18537 case DW_FORM_indirect:
18538 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18539 info_ptr += bytes_read;
18540 if (form == DW_FORM_implicit_const)
18541 {
18542 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18543 info_ptr += bytes_read;
18544 }
18545 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18546 info_ptr, need_reprocess);
18547 break;
18548 case DW_FORM_implicit_const:
18549 DW_SND (attr) = implicit_const;
18550 break;
18551 case DW_FORM_addrx:
18552 case DW_FORM_GNU_addr_index:
18553 *need_reprocess = true;
18554 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18555 info_ptr += bytes_read;
18556 break;
18557 case DW_FORM_strx:
18558 case DW_FORM_strx1:
18559 case DW_FORM_strx2:
18560 case DW_FORM_strx3:
18561 case DW_FORM_strx4:
18562 case DW_FORM_GNU_str_index:
18563 {
18564 ULONGEST str_index;
18565 if (form == DW_FORM_strx1)
18566 {
18567 str_index = read_1_byte (abfd, info_ptr);
18568 info_ptr += 1;
18569 }
18570 else if (form == DW_FORM_strx2)
18571 {
18572 str_index = read_2_bytes (abfd, info_ptr);
18573 info_ptr += 2;
18574 }
18575 else if (form == DW_FORM_strx3)
18576 {
18577 str_index = read_3_bytes (abfd, info_ptr);
18578 info_ptr += 3;
18579 }
18580 else if (form == DW_FORM_strx4)
18581 {
18582 str_index = read_4_bytes (abfd, info_ptr);
18583 info_ptr += 4;
18584 }
18585 else
18586 {
18587 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18588 info_ptr += bytes_read;
18589 }
18590 *need_reprocess = true;
18591 DW_UNSND (attr) = str_index;
18592 }
18593 break;
18594 default:
18595 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
18596 dwarf_form_name (form),
18597 bfd_get_filename (abfd));
18598 }
18599
18600 /* Super hack. */
18601 if (cu->per_cu->is_dwz && attr->form_is_ref ())
18602 attr->form = DW_FORM_GNU_ref_alt;
18603
18604 /* We have seen instances where the compiler tried to emit a byte
18605 size attribute of -1 which ended up being encoded as an unsigned
18606 0xffffffff. Although 0xffffffff is technically a valid size value,
18607 an object of this size seems pretty unlikely so we can relatively
18608 safely treat these cases as if the size attribute was invalid and
18609 treat them as zero by default. */
18610 if (attr->name == DW_AT_byte_size
18611 && form == DW_FORM_data4
18612 && DW_UNSND (attr) >= 0xffffffff)
18613 {
18614 complaint
18615 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
18616 hex_string (DW_UNSND (attr)));
18617 DW_UNSND (attr) = 0;
18618 }
18619
18620 return info_ptr;
18621 }
18622
18623 /* Read an attribute described by an abbreviated attribute. */
18624
18625 static const gdb_byte *
18626 read_attribute (const struct die_reader_specs *reader,
18627 struct attribute *attr, struct attr_abbrev *abbrev,
18628 const gdb_byte *info_ptr, bool *need_reprocess)
18629 {
18630 attr->name = abbrev->name;
18631 return read_attribute_value (reader, attr, abbrev->form,
18632 abbrev->implicit_const, info_ptr,
18633 need_reprocess);
18634 }
18635
18636 /* Return pointer to string at .debug_str offset STR_OFFSET. */
18637
18638 static const char *
18639 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
18640 LONGEST str_offset)
18641 {
18642 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18643 str_offset, "DW_FORM_strp");
18644 }
18645
18646 /* Return pointer to string at .debug_str offset as read from BUF.
18647 BUF is assumed to be in a compilation unit described by CU_HEADER.
18648 Return *BYTES_READ_PTR count of bytes read from BUF. */
18649
18650 static const char *
18651 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18652 const gdb_byte *buf,
18653 const struct comp_unit_head *cu_header,
18654 unsigned int *bytes_read_ptr)
18655 {
18656 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18657
18658 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
18659 }
18660
18661 /* See read.h. */
18662
18663 const char *
18664 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
18665 const struct comp_unit_head *cu_header,
18666 unsigned int *bytes_read_ptr)
18667 {
18668 bfd *abfd = objfile->obfd;
18669 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
18670
18671 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
18672 }
18673
18674 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
18675 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
18676 ADDR_SIZE is the size of addresses from the CU header. */
18677
18678 static CORE_ADDR
18679 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18680 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18681 int addr_size)
18682 {
18683 struct objfile *objfile = dwarf2_per_objfile->objfile;
18684 bfd *abfd = objfile->obfd;
18685 const gdb_byte *info_ptr;
18686 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
18687
18688 dwarf2_per_objfile->addr.read (objfile);
18689 if (dwarf2_per_objfile->addr.buffer == NULL)
18690 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
18691 objfile_name (objfile));
18692 if (addr_base_or_zero + addr_index * addr_size
18693 >= dwarf2_per_objfile->addr.size)
18694 error (_("DW_FORM_addr_index pointing outside of "
18695 ".debug_addr section [in module %s]"),
18696 objfile_name (objfile));
18697 info_ptr = (dwarf2_per_objfile->addr.buffer
18698 + addr_base_or_zero + addr_index * addr_size);
18699 if (addr_size == 4)
18700 return bfd_get_32 (abfd, info_ptr);
18701 else
18702 return bfd_get_64 (abfd, info_ptr);
18703 }
18704
18705 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18706
18707 static CORE_ADDR
18708 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18709 {
18710 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18711 cu->addr_base, cu->header.addr_size);
18712 }
18713
18714 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18715
18716 static CORE_ADDR
18717 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
18718 unsigned int *bytes_read)
18719 {
18720 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
18721 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18722
18723 return read_addr_index (cu, addr_index);
18724 }
18725
18726 /* See read.h. */
18727
18728 CORE_ADDR
18729 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
18730 {
18731 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
18732 struct dwarf2_cu *cu = per_cu->cu;
18733 gdb::optional<ULONGEST> addr_base;
18734 int addr_size;
18735
18736 /* We need addr_base and addr_size.
18737 If we don't have PER_CU->cu, we have to get it.
18738 Nasty, but the alternative is storing the needed info in PER_CU,
18739 which at this point doesn't seem justified: it's not clear how frequently
18740 it would get used and it would increase the size of every PER_CU.
18741 Entry points like dwarf2_per_cu_addr_size do a similar thing
18742 so we're not in uncharted territory here.
18743 Alas we need to be a bit more complicated as addr_base is contained
18744 in the DIE.
18745
18746 We don't need to read the entire CU(/TU).
18747 We just need the header and top level die.
18748
18749 IWBN to use the aging mechanism to let us lazily later discard the CU.
18750 For now we skip this optimization. */
18751
18752 if (cu != NULL)
18753 {
18754 addr_base = cu->addr_base;
18755 addr_size = cu->header.addr_size;
18756 }
18757 else
18758 {
18759 cutu_reader reader (per_cu, NULL, 0, false);
18760 addr_base = reader.cu->addr_base;
18761 addr_size = reader.cu->header.addr_size;
18762 }
18763
18764 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18765 addr_size);
18766 }
18767
18768 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18769 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18770 DWO file. */
18771
18772 static const char *
18773 read_str_index (struct dwarf2_cu *cu,
18774 struct dwarf2_section_info *str_section,
18775 struct dwarf2_section_info *str_offsets_section,
18776 ULONGEST str_offsets_base, ULONGEST str_index)
18777 {
18778 struct dwarf2_per_objfile *dwarf2_per_objfile
18779 = cu->per_cu->dwarf2_per_objfile;
18780 struct objfile *objfile = dwarf2_per_objfile->objfile;
18781 const char *objf_name = objfile_name (objfile);
18782 bfd *abfd = objfile->obfd;
18783 const gdb_byte *info_ptr;
18784 ULONGEST str_offset;
18785 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
18786
18787 str_section->read (objfile);
18788 str_offsets_section->read (objfile);
18789 if (str_section->buffer == NULL)
18790 error (_("%s used without %s section"
18791 " in CU at offset %s [in module %s]"),
18792 form_name, str_section->get_name (),
18793 sect_offset_str (cu->header.sect_off), objf_name);
18794 if (str_offsets_section->buffer == NULL)
18795 error (_("%s used without %s section"
18796 " in CU at offset %s [in module %s]"),
18797 form_name, str_section->get_name (),
18798 sect_offset_str (cu->header.sect_off), objf_name);
18799 info_ptr = (str_offsets_section->buffer
18800 + str_offsets_base
18801 + str_index * cu->header.offset_size);
18802 if (cu->header.offset_size == 4)
18803 str_offset = bfd_get_32 (abfd, info_ptr);
18804 else
18805 str_offset = bfd_get_64 (abfd, info_ptr);
18806 if (str_offset >= str_section->size)
18807 error (_("Offset from %s pointing outside of"
18808 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18809 form_name, sect_offset_str (cu->header.sect_off), objf_name);
18810 return (const char *) (str_section->buffer + str_offset);
18811 }
18812
18813 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18814
18815 static const char *
18816 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18817 {
18818 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18819 ? reader->cu->header.addr_size : 0;
18820 return read_str_index (reader->cu,
18821 &reader->dwo_file->sections.str,
18822 &reader->dwo_file->sections.str_offsets,
18823 str_offsets_base, str_index);
18824 }
18825
18826 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18827
18828 static const char *
18829 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18830 {
18831 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18832 const char *objf_name = objfile_name (objfile);
18833 static const char form_name[] = "DW_FORM_GNU_str_index";
18834 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18835
18836 if (!cu->str_offsets_base.has_value ())
18837 error (_("%s used in Fission stub without %s"
18838 " in CU at offset 0x%lx [in module %s]"),
18839 form_name, str_offsets_attr_name,
18840 (long) cu->header.offset_size, objf_name);
18841
18842 return read_str_index (cu,
18843 &cu->per_cu->dwarf2_per_objfile->str,
18844 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18845 *cu->str_offsets_base, str_index);
18846 }
18847
18848 /* Return the length of an LEB128 number in BUF. */
18849
18850 static int
18851 leb128_size (const gdb_byte *buf)
18852 {
18853 const gdb_byte *begin = buf;
18854 gdb_byte byte;
18855
18856 while (1)
18857 {
18858 byte = *buf++;
18859 if ((byte & 128) == 0)
18860 return buf - begin;
18861 }
18862 }
18863
18864 static void
18865 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
18866 {
18867 switch (lang)
18868 {
18869 case DW_LANG_C89:
18870 case DW_LANG_C99:
18871 case DW_LANG_C11:
18872 case DW_LANG_C:
18873 case DW_LANG_UPC:
18874 cu->language = language_c;
18875 break;
18876 case DW_LANG_Java:
18877 case DW_LANG_C_plus_plus:
18878 case DW_LANG_C_plus_plus_11:
18879 case DW_LANG_C_plus_plus_14:
18880 cu->language = language_cplus;
18881 break;
18882 case DW_LANG_D:
18883 cu->language = language_d;
18884 break;
18885 case DW_LANG_Fortran77:
18886 case DW_LANG_Fortran90:
18887 case DW_LANG_Fortran95:
18888 case DW_LANG_Fortran03:
18889 case DW_LANG_Fortran08:
18890 cu->language = language_fortran;
18891 break;
18892 case DW_LANG_Go:
18893 cu->language = language_go;
18894 break;
18895 case DW_LANG_Mips_Assembler:
18896 cu->language = language_asm;
18897 break;
18898 case DW_LANG_Ada83:
18899 case DW_LANG_Ada95:
18900 cu->language = language_ada;
18901 break;
18902 case DW_LANG_Modula2:
18903 cu->language = language_m2;
18904 break;
18905 case DW_LANG_Pascal83:
18906 cu->language = language_pascal;
18907 break;
18908 case DW_LANG_ObjC:
18909 cu->language = language_objc;
18910 break;
18911 case DW_LANG_Rust:
18912 case DW_LANG_Rust_old:
18913 cu->language = language_rust;
18914 break;
18915 case DW_LANG_Cobol74:
18916 case DW_LANG_Cobol85:
18917 default:
18918 cu->language = language_minimal;
18919 break;
18920 }
18921 cu->language_defn = language_def (cu->language);
18922 }
18923
18924 /* Return the named attribute or NULL if not there. */
18925
18926 static struct attribute *
18927 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18928 {
18929 for (;;)
18930 {
18931 unsigned int i;
18932 struct attribute *spec = NULL;
18933
18934 for (i = 0; i < die->num_attrs; ++i)
18935 {
18936 if (die->attrs[i].name == name)
18937 return &die->attrs[i];
18938 if (die->attrs[i].name == DW_AT_specification
18939 || die->attrs[i].name == DW_AT_abstract_origin)
18940 spec = &die->attrs[i];
18941 }
18942
18943 if (!spec)
18944 break;
18945
18946 die = follow_die_ref (die, spec, &cu);
18947 }
18948
18949 return NULL;
18950 }
18951
18952 /* Return the string associated with a string-typed attribute, or NULL if it
18953 is either not found or is of an incorrect type. */
18954
18955 static const char *
18956 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
18957 {
18958 struct attribute *attr;
18959 const char *str = NULL;
18960
18961 attr = dwarf2_attr (die, name, cu);
18962
18963 if (attr != NULL)
18964 {
18965 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
18966 || attr->form == DW_FORM_string
18967 || attr->form == DW_FORM_strx
18968 || attr->form == DW_FORM_strx1
18969 || attr->form == DW_FORM_strx2
18970 || attr->form == DW_FORM_strx3
18971 || attr->form == DW_FORM_strx4
18972 || attr->form == DW_FORM_GNU_str_index
18973 || attr->form == DW_FORM_GNU_strp_alt)
18974 str = DW_STRING (attr);
18975 else
18976 complaint (_("string type expected for attribute %s for "
18977 "DIE at %s in module %s"),
18978 dwarf_attr_name (name), sect_offset_str (die->sect_off),
18979 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18980 }
18981
18982 return str;
18983 }
18984
18985 /* Return the dwo name or NULL if not present. If present, it is in either
18986 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
18987 static const char *
18988 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
18989 {
18990 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
18991 if (dwo_name == nullptr)
18992 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
18993 return dwo_name;
18994 }
18995
18996 /* Return non-zero iff the attribute NAME is defined for the given DIE,
18997 and holds a non-zero value. This function should only be used for
18998 DW_FORM_flag or DW_FORM_flag_present attributes. */
18999
19000 static int
19001 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19002 {
19003 struct attribute *attr = dwarf2_attr (die, name, cu);
19004
19005 return (attr && DW_UNSND (attr));
19006 }
19007
19008 static int
19009 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19010 {
19011 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19012 which value is non-zero. However, we have to be careful with
19013 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19014 (via dwarf2_flag_true_p) follows this attribute. So we may
19015 end up accidently finding a declaration attribute that belongs
19016 to a different DIE referenced by the specification attribute,
19017 even though the given DIE does not have a declaration attribute. */
19018 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19019 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19020 }
19021
19022 /* Return the die giving the specification for DIE, if there is
19023 one. *SPEC_CU is the CU containing DIE on input, and the CU
19024 containing the return value on output. If there is no
19025 specification, but there is an abstract origin, that is
19026 returned. */
19027
19028 static struct die_info *
19029 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19030 {
19031 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19032 *spec_cu);
19033
19034 if (spec_attr == NULL)
19035 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19036
19037 if (spec_attr == NULL)
19038 return NULL;
19039 else
19040 return follow_die_ref (die, spec_attr, spec_cu);
19041 }
19042
19043 /* Stub for free_line_header to match void * callback types. */
19044
19045 static void
19046 free_line_header_voidp (void *arg)
19047 {
19048 struct line_header *lh = (struct line_header *) arg;
19049
19050 delete lh;
19051 }
19052
19053 /* A convenience function to find the proper .debug_line section for a CU. */
19054
19055 static struct dwarf2_section_info *
19056 get_debug_line_section (struct dwarf2_cu *cu)
19057 {
19058 struct dwarf2_section_info *section;
19059 struct dwarf2_per_objfile *dwarf2_per_objfile
19060 = cu->per_cu->dwarf2_per_objfile;
19061
19062 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19063 DWO file. */
19064 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19065 section = &cu->dwo_unit->dwo_file->sections.line;
19066 else if (cu->per_cu->is_dwz)
19067 {
19068 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
19069
19070 section = &dwz->line;
19071 }
19072 else
19073 section = &dwarf2_per_objfile->line;
19074
19075 return section;
19076 }
19077
19078 /* Read the statement program header starting at OFFSET in
19079 .debug_line, or .debug_line.dwo. Return a pointer
19080 to a struct line_header, allocated using xmalloc.
19081 Returns NULL if there is a problem reading the header, e.g., if it
19082 has a version we don't understand.
19083
19084 NOTE: the strings in the include directory and file name tables of
19085 the returned object point into the dwarf line section buffer,
19086 and must not be freed. */
19087
19088 static line_header_up
19089 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19090 {
19091 struct dwarf2_section_info *section;
19092 struct dwarf2_per_objfile *dwarf2_per_objfile
19093 = cu->per_cu->dwarf2_per_objfile;
19094
19095 section = get_debug_line_section (cu);
19096 section->read (dwarf2_per_objfile->objfile);
19097 if (section->buffer == NULL)
19098 {
19099 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19100 complaint (_("missing .debug_line.dwo section"));
19101 else
19102 complaint (_("missing .debug_line section"));
19103 return 0;
19104 }
19105
19106 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19107 dwarf2_per_objfile, section,
19108 &cu->header);
19109 }
19110
19111 /* Subroutine of dwarf_decode_lines to simplify it.
19112 Return the file name of the psymtab for the given file_entry.
19113 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19114 If space for the result is malloc'd, *NAME_HOLDER will be set.
19115 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19116
19117 static const char *
19118 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19119 const dwarf2_psymtab *pst,
19120 const char *comp_dir,
19121 gdb::unique_xmalloc_ptr<char> *name_holder)
19122 {
19123 const char *include_name = fe.name;
19124 const char *include_name_to_compare = include_name;
19125 const char *pst_filename;
19126 int file_is_pst;
19127
19128 const char *dir_name = fe.include_dir (lh);
19129
19130 gdb::unique_xmalloc_ptr<char> hold_compare;
19131 if (!IS_ABSOLUTE_PATH (include_name)
19132 && (dir_name != NULL || comp_dir != NULL))
19133 {
19134 /* Avoid creating a duplicate psymtab for PST.
19135 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19136 Before we do the comparison, however, we need to account
19137 for DIR_NAME and COMP_DIR.
19138 First prepend dir_name (if non-NULL). If we still don't
19139 have an absolute path prepend comp_dir (if non-NULL).
19140 However, the directory we record in the include-file's
19141 psymtab does not contain COMP_DIR (to match the
19142 corresponding symtab(s)).
19143
19144 Example:
19145
19146 bash$ cd /tmp
19147 bash$ gcc -g ./hello.c
19148 include_name = "hello.c"
19149 dir_name = "."
19150 DW_AT_comp_dir = comp_dir = "/tmp"
19151 DW_AT_name = "./hello.c"
19152
19153 */
19154
19155 if (dir_name != NULL)
19156 {
19157 name_holder->reset (concat (dir_name, SLASH_STRING,
19158 include_name, (char *) NULL));
19159 include_name = name_holder->get ();
19160 include_name_to_compare = include_name;
19161 }
19162 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19163 {
19164 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19165 include_name, (char *) NULL));
19166 include_name_to_compare = hold_compare.get ();
19167 }
19168 }
19169
19170 pst_filename = pst->filename;
19171 gdb::unique_xmalloc_ptr<char> copied_name;
19172 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19173 {
19174 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19175 pst_filename, (char *) NULL));
19176 pst_filename = copied_name.get ();
19177 }
19178
19179 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19180
19181 if (file_is_pst)
19182 return NULL;
19183 return include_name;
19184 }
19185
19186 /* State machine to track the state of the line number program. */
19187
19188 class lnp_state_machine
19189 {
19190 public:
19191 /* Initialize a machine state for the start of a line number
19192 program. */
19193 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19194 bool record_lines_p);
19195
19196 file_entry *current_file ()
19197 {
19198 /* lh->file_names is 0-based, but the file name numbers in the
19199 statement program are 1-based. */
19200 return m_line_header->file_name_at (m_file);
19201 }
19202
19203 /* Record the line in the state machine. END_SEQUENCE is true if
19204 we're processing the end of a sequence. */
19205 void record_line (bool end_sequence);
19206
19207 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19208 nop-out rest of the lines in this sequence. */
19209 void check_line_address (struct dwarf2_cu *cu,
19210 const gdb_byte *line_ptr,
19211 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19212
19213 void handle_set_discriminator (unsigned int discriminator)
19214 {
19215 m_discriminator = discriminator;
19216 m_line_has_non_zero_discriminator |= discriminator != 0;
19217 }
19218
19219 /* Handle DW_LNE_set_address. */
19220 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19221 {
19222 m_op_index = 0;
19223 address += baseaddr;
19224 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19225 }
19226
19227 /* Handle DW_LNS_advance_pc. */
19228 void handle_advance_pc (CORE_ADDR adjust);
19229
19230 /* Handle a special opcode. */
19231 void handle_special_opcode (unsigned char op_code);
19232
19233 /* Handle DW_LNS_advance_line. */
19234 void handle_advance_line (int line_delta)
19235 {
19236 advance_line (line_delta);
19237 }
19238
19239 /* Handle DW_LNS_set_file. */
19240 void handle_set_file (file_name_index file);
19241
19242 /* Handle DW_LNS_negate_stmt. */
19243 void handle_negate_stmt ()
19244 {
19245 m_is_stmt = !m_is_stmt;
19246 }
19247
19248 /* Handle DW_LNS_const_add_pc. */
19249 void handle_const_add_pc ();
19250
19251 /* Handle DW_LNS_fixed_advance_pc. */
19252 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19253 {
19254 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19255 m_op_index = 0;
19256 }
19257
19258 /* Handle DW_LNS_copy. */
19259 void handle_copy ()
19260 {
19261 record_line (false);
19262 m_discriminator = 0;
19263 }
19264
19265 /* Handle DW_LNE_end_sequence. */
19266 void handle_end_sequence ()
19267 {
19268 m_currently_recording_lines = true;
19269 }
19270
19271 private:
19272 /* Advance the line by LINE_DELTA. */
19273 void advance_line (int line_delta)
19274 {
19275 m_line += line_delta;
19276
19277 if (line_delta != 0)
19278 m_line_has_non_zero_discriminator = m_discriminator != 0;
19279 }
19280
19281 struct dwarf2_cu *m_cu;
19282
19283 gdbarch *m_gdbarch;
19284
19285 /* True if we're recording lines.
19286 Otherwise we're building partial symtabs and are just interested in
19287 finding include files mentioned by the line number program. */
19288 bool m_record_lines_p;
19289
19290 /* The line number header. */
19291 line_header *m_line_header;
19292
19293 /* These are part of the standard DWARF line number state machine,
19294 and initialized according to the DWARF spec. */
19295
19296 unsigned char m_op_index = 0;
19297 /* The line table index of the current file. */
19298 file_name_index m_file = 1;
19299 unsigned int m_line = 1;
19300
19301 /* These are initialized in the constructor. */
19302
19303 CORE_ADDR m_address;
19304 bool m_is_stmt;
19305 unsigned int m_discriminator;
19306
19307 /* Additional bits of state we need to track. */
19308
19309 /* The last file that we called dwarf2_start_subfile for.
19310 This is only used for TLLs. */
19311 unsigned int m_last_file = 0;
19312 /* The last file a line number was recorded for. */
19313 struct subfile *m_last_subfile = NULL;
19314
19315 /* When true, record the lines we decode. */
19316 bool m_currently_recording_lines = false;
19317
19318 /* The last line number that was recorded, used to coalesce
19319 consecutive entries for the same line. This can happen, for
19320 example, when discriminators are present. PR 17276. */
19321 unsigned int m_last_line = 0;
19322 bool m_line_has_non_zero_discriminator = false;
19323 };
19324
19325 void
19326 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19327 {
19328 CORE_ADDR addr_adj = (((m_op_index + adjust)
19329 / m_line_header->maximum_ops_per_instruction)
19330 * m_line_header->minimum_instruction_length);
19331 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19332 m_op_index = ((m_op_index + adjust)
19333 % m_line_header->maximum_ops_per_instruction);
19334 }
19335
19336 void
19337 lnp_state_machine::handle_special_opcode (unsigned char op_code)
19338 {
19339 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
19340 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19341 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19342 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
19343 / m_line_header->maximum_ops_per_instruction)
19344 * m_line_header->minimum_instruction_length);
19345 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19346 m_op_index = ((m_op_index + adj_opcode_d)
19347 % m_line_header->maximum_ops_per_instruction);
19348
19349 int line_delta = m_line_header->line_base + adj_opcode_r;
19350 advance_line (line_delta);
19351 record_line (false);
19352 m_discriminator = 0;
19353 }
19354
19355 void
19356 lnp_state_machine::handle_set_file (file_name_index file)
19357 {
19358 m_file = file;
19359
19360 const file_entry *fe = current_file ();
19361 if (fe == NULL)
19362 dwarf2_debug_line_missing_file_complaint ();
19363 else if (m_record_lines_p)
19364 {
19365 const char *dir = fe->include_dir (m_line_header);
19366
19367 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19368 m_line_has_non_zero_discriminator = m_discriminator != 0;
19369 dwarf2_start_subfile (m_cu, fe->name, dir);
19370 }
19371 }
19372
19373 void
19374 lnp_state_machine::handle_const_add_pc ()
19375 {
19376 CORE_ADDR adjust
19377 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19378
19379 CORE_ADDR addr_adj
19380 = (((m_op_index + adjust)
19381 / m_line_header->maximum_ops_per_instruction)
19382 * m_line_header->minimum_instruction_length);
19383
19384 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19385 m_op_index = ((m_op_index + adjust)
19386 % m_line_header->maximum_ops_per_instruction);
19387 }
19388
19389 /* Return non-zero if we should add LINE to the line number table.
19390 LINE is the line to add, LAST_LINE is the last line that was added,
19391 LAST_SUBFILE is the subfile for LAST_LINE.
19392 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19393 had a non-zero discriminator.
19394
19395 We have to be careful in the presence of discriminators.
19396 E.g., for this line:
19397
19398 for (i = 0; i < 100000; i++);
19399
19400 clang can emit four line number entries for that one line,
19401 each with a different discriminator.
19402 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19403
19404 However, we want gdb to coalesce all four entries into one.
19405 Otherwise the user could stepi into the middle of the line and
19406 gdb would get confused about whether the pc really was in the
19407 middle of the line.
19408
19409 Things are further complicated by the fact that two consecutive
19410 line number entries for the same line is a heuristic used by gcc
19411 to denote the end of the prologue. So we can't just discard duplicate
19412 entries, we have to be selective about it. The heuristic we use is
19413 that we only collapse consecutive entries for the same line if at least
19414 one of those entries has a non-zero discriminator. PR 17276.
19415
19416 Note: Addresses in the line number state machine can never go backwards
19417 within one sequence, thus this coalescing is ok. */
19418
19419 static int
19420 dwarf_record_line_p (struct dwarf2_cu *cu,
19421 unsigned int line, unsigned int last_line,
19422 int line_has_non_zero_discriminator,
19423 struct subfile *last_subfile)
19424 {
19425 if (cu->get_builder ()->get_current_subfile () != last_subfile)
19426 return 1;
19427 if (line != last_line)
19428 return 1;
19429 /* Same line for the same file that we've seen already.
19430 As a last check, for pr 17276, only record the line if the line
19431 has never had a non-zero discriminator. */
19432 if (!line_has_non_zero_discriminator)
19433 return 1;
19434 return 0;
19435 }
19436
19437 /* Use the CU's builder to record line number LINE beginning at
19438 address ADDRESS in the line table of subfile SUBFILE. */
19439
19440 static void
19441 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
19442 unsigned int line, CORE_ADDR address, bool is_stmt,
19443 struct dwarf2_cu *cu)
19444 {
19445 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19446
19447 if (dwarf_line_debug)
19448 {
19449 fprintf_unfiltered (gdb_stdlog,
19450 "Recording line %u, file %s, address %s\n",
19451 line, lbasename (subfile->name),
19452 paddress (gdbarch, address));
19453 }
19454
19455 if (cu != nullptr)
19456 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
19457 }
19458
19459 /* Subroutine of dwarf_decode_lines_1 to simplify it.
19460 Mark the end of a set of line number records.
19461 The arguments are the same as for dwarf_record_line_1.
19462 If SUBFILE is NULL the request is ignored. */
19463
19464 static void
19465 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
19466 CORE_ADDR address, struct dwarf2_cu *cu)
19467 {
19468 if (subfile == NULL)
19469 return;
19470
19471 if (dwarf_line_debug)
19472 {
19473 fprintf_unfiltered (gdb_stdlog,
19474 "Finishing current line, file %s, address %s\n",
19475 lbasename (subfile->name),
19476 paddress (gdbarch, address));
19477 }
19478
19479 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
19480 }
19481
19482 void
19483 lnp_state_machine::record_line (bool end_sequence)
19484 {
19485 if (dwarf_line_debug)
19486 {
19487 fprintf_unfiltered (gdb_stdlog,
19488 "Processing actual line %u: file %u,"
19489 " address %s, is_stmt %u, discrim %u%s\n",
19490 m_line, m_file,
19491 paddress (m_gdbarch, m_address),
19492 m_is_stmt, m_discriminator,
19493 (end_sequence ? "\t(end sequence)" : ""));
19494 }
19495
19496 file_entry *fe = current_file ();
19497
19498 if (fe == NULL)
19499 dwarf2_debug_line_missing_file_complaint ();
19500 /* For now we ignore lines not starting on an instruction boundary.
19501 But not when processing end_sequence for compatibility with the
19502 previous version of the code. */
19503 else if (m_op_index == 0 || end_sequence)
19504 {
19505 fe->included_p = 1;
19506 if (m_record_lines_p)
19507 {
19508 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
19509 || end_sequence)
19510 {
19511 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19512 m_currently_recording_lines ? m_cu : nullptr);
19513 }
19514
19515 if (!end_sequence)
19516 {
19517 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19518
19519 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
19520 m_line_has_non_zero_discriminator,
19521 m_last_subfile))
19522 {
19523 buildsym_compunit *builder = m_cu->get_builder ();
19524 dwarf_record_line_1 (m_gdbarch,
19525 builder->get_current_subfile (),
19526 m_line, m_address, is_stmt,
19527 m_currently_recording_lines ? m_cu : nullptr);
19528 }
19529 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
19530 m_last_line = m_line;
19531 }
19532 }
19533 }
19534 }
19535
19536 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19537 line_header *lh, bool record_lines_p)
19538 {
19539 m_cu = cu;
19540 m_gdbarch = arch;
19541 m_record_lines_p = record_lines_p;
19542 m_line_header = lh;
19543
19544 m_currently_recording_lines = true;
19545
19546 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19547 was a line entry for it so that the backend has a chance to adjust it
19548 and also record it in case it needs it. This is currently used by MIPS
19549 code, cf. `mips_adjust_dwarf2_line'. */
19550 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19551 m_is_stmt = lh->default_is_stmt;
19552 m_discriminator = 0;
19553 }
19554
19555 void
19556 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19557 const gdb_byte *line_ptr,
19558 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
19559 {
19560 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19561 the pc range of the CU. However, we restrict the test to only ADDRESS
19562 values of zero to preserve GDB's previous behaviour which is to handle
19563 the specific case of a function being GC'd by the linker. */
19564
19565 if (address == 0 && address < unrelocated_lowpc)
19566 {
19567 /* This line table is for a function which has been
19568 GCd by the linker. Ignore it. PR gdb/12528 */
19569
19570 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19571 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19572
19573 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
19574 line_offset, objfile_name (objfile));
19575 m_currently_recording_lines = false;
19576 /* Note: m_currently_recording_lines is left as false until we see
19577 DW_LNE_end_sequence. */
19578 }
19579 }
19580
19581 /* Subroutine of dwarf_decode_lines to simplify it.
19582 Process the line number information in LH.
19583 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19584 program in order to set included_p for every referenced header. */
19585
19586 static void
19587 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19588 const int decode_for_pst_p, CORE_ADDR lowpc)
19589 {
19590 const gdb_byte *line_ptr, *extended_end;
19591 const gdb_byte *line_end;
19592 unsigned int bytes_read, extended_len;
19593 unsigned char op_code, extended_op;
19594 CORE_ADDR baseaddr;
19595 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19596 bfd *abfd = objfile->obfd;
19597 struct gdbarch *gdbarch = get_objfile_arch (objfile);
19598 /* True if we're recording line info (as opposed to building partial
19599 symtabs and just interested in finding include files mentioned by
19600 the line number program). */
19601 bool record_lines_p = !decode_for_pst_p;
19602
19603 baseaddr = objfile->text_section_offset ();
19604
19605 line_ptr = lh->statement_program_start;
19606 line_end = lh->statement_program_end;
19607
19608 /* Read the statement sequences until there's nothing left. */
19609 while (line_ptr < line_end)
19610 {
19611 /* The DWARF line number program state machine. Reset the state
19612 machine at the start of each sequence. */
19613 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
19614 bool end_sequence = false;
19615
19616 if (record_lines_p)
19617 {
19618 /* Start a subfile for the current file of the state
19619 machine. */
19620 const file_entry *fe = state_machine.current_file ();
19621
19622 if (fe != NULL)
19623 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
19624 }
19625
19626 /* Decode the table. */
19627 while (line_ptr < line_end && !end_sequence)
19628 {
19629 op_code = read_1_byte (abfd, line_ptr);
19630 line_ptr += 1;
19631
19632 if (op_code >= lh->opcode_base)
19633 {
19634 /* Special opcode. */
19635 state_machine.handle_special_opcode (op_code);
19636 }
19637 else switch (op_code)
19638 {
19639 case DW_LNS_extended_op:
19640 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19641 &bytes_read);
19642 line_ptr += bytes_read;
19643 extended_end = line_ptr + extended_len;
19644 extended_op = read_1_byte (abfd, line_ptr);
19645 line_ptr += 1;
19646 switch (extended_op)
19647 {
19648 case DW_LNE_end_sequence:
19649 state_machine.handle_end_sequence ();
19650 end_sequence = true;
19651 break;
19652 case DW_LNE_set_address:
19653 {
19654 CORE_ADDR address
19655 = cu->header.read_address (abfd, line_ptr, &bytes_read);
19656 line_ptr += bytes_read;
19657
19658 state_machine.check_line_address (cu, line_ptr,
19659 lowpc - baseaddr, address);
19660 state_machine.handle_set_address (baseaddr, address);
19661 }
19662 break;
19663 case DW_LNE_define_file:
19664 {
19665 const char *cur_file;
19666 unsigned int mod_time, length;
19667 dir_index dindex;
19668
19669 cur_file = read_direct_string (abfd, line_ptr,
19670 &bytes_read);
19671 line_ptr += bytes_read;
19672 dindex = (dir_index)
19673 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19674 line_ptr += bytes_read;
19675 mod_time =
19676 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19677 line_ptr += bytes_read;
19678 length =
19679 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19680 line_ptr += bytes_read;
19681 lh->add_file_name (cur_file, dindex, mod_time, length);
19682 }
19683 break;
19684 case DW_LNE_set_discriminator:
19685 {
19686 /* The discriminator is not interesting to the
19687 debugger; just ignore it. We still need to
19688 check its value though:
19689 if there are consecutive entries for the same
19690 (non-prologue) line we want to coalesce them.
19691 PR 17276. */
19692 unsigned int discr
19693 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19694 line_ptr += bytes_read;
19695
19696 state_machine.handle_set_discriminator (discr);
19697 }
19698 break;
19699 default:
19700 complaint (_("mangled .debug_line section"));
19701 return;
19702 }
19703 /* Make sure that we parsed the extended op correctly. If e.g.
19704 we expected a different address size than the producer used,
19705 we may have read the wrong number of bytes. */
19706 if (line_ptr != extended_end)
19707 {
19708 complaint (_("mangled .debug_line section"));
19709 return;
19710 }
19711 break;
19712 case DW_LNS_copy:
19713 state_machine.handle_copy ();
19714 break;
19715 case DW_LNS_advance_pc:
19716 {
19717 CORE_ADDR adjust
19718 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19719 line_ptr += bytes_read;
19720
19721 state_machine.handle_advance_pc (adjust);
19722 }
19723 break;
19724 case DW_LNS_advance_line:
19725 {
19726 int line_delta
19727 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
19728 line_ptr += bytes_read;
19729
19730 state_machine.handle_advance_line (line_delta);
19731 }
19732 break;
19733 case DW_LNS_set_file:
19734 {
19735 file_name_index file
19736 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19737 &bytes_read);
19738 line_ptr += bytes_read;
19739
19740 state_machine.handle_set_file (file);
19741 }
19742 break;
19743 case DW_LNS_set_column:
19744 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19745 line_ptr += bytes_read;
19746 break;
19747 case DW_LNS_negate_stmt:
19748 state_machine.handle_negate_stmt ();
19749 break;
19750 case DW_LNS_set_basic_block:
19751 break;
19752 /* Add to the address register of the state machine the
19753 address increment value corresponding to special opcode
19754 255. I.e., this value is scaled by the minimum
19755 instruction length since special opcode 255 would have
19756 scaled the increment. */
19757 case DW_LNS_const_add_pc:
19758 state_machine.handle_const_add_pc ();
19759 break;
19760 case DW_LNS_fixed_advance_pc:
19761 {
19762 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
19763 line_ptr += 2;
19764
19765 state_machine.handle_fixed_advance_pc (addr_adj);
19766 }
19767 break;
19768 default:
19769 {
19770 /* Unknown standard opcode, ignore it. */
19771 int i;
19772
19773 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
19774 {
19775 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19776 line_ptr += bytes_read;
19777 }
19778 }
19779 }
19780 }
19781
19782 if (!end_sequence)
19783 dwarf2_debug_line_missing_end_sequence_complaint ();
19784
19785 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19786 in which case we still finish recording the last line). */
19787 state_machine.record_line (true);
19788 }
19789 }
19790
19791 /* Decode the Line Number Program (LNP) for the given line_header
19792 structure and CU. The actual information extracted and the type
19793 of structures created from the LNP depends on the value of PST.
19794
19795 1. If PST is NULL, then this procedure uses the data from the program
19796 to create all necessary symbol tables, and their linetables.
19797
19798 2. If PST is not NULL, this procedure reads the program to determine
19799 the list of files included by the unit represented by PST, and
19800 builds all the associated partial symbol tables.
19801
19802 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19803 It is used for relative paths in the line table.
19804 NOTE: When processing partial symtabs (pst != NULL),
19805 comp_dir == pst->dirname.
19806
19807 NOTE: It is important that psymtabs have the same file name (via strcmp)
19808 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19809 symtab we don't use it in the name of the psymtabs we create.
19810 E.g. expand_line_sal requires this when finding psymtabs to expand.
19811 A good testcase for this is mb-inline.exp.
19812
19813 LOWPC is the lowest address in CU (or 0 if not known).
19814
19815 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19816 for its PC<->lines mapping information. Otherwise only the filename
19817 table is read in. */
19818
19819 static void
19820 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
19821 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
19822 CORE_ADDR lowpc, int decode_mapping)
19823 {
19824 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19825 const int decode_for_pst_p = (pst != NULL);
19826
19827 if (decode_mapping)
19828 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
19829
19830 if (decode_for_pst_p)
19831 {
19832 /* Now that we're done scanning the Line Header Program, we can
19833 create the psymtab of each included file. */
19834 for (auto &file_entry : lh->file_names ())
19835 if (file_entry.included_p == 1)
19836 {
19837 gdb::unique_xmalloc_ptr<char> name_holder;
19838 const char *include_name =
19839 psymtab_include_file_name (lh, file_entry, pst,
19840 comp_dir, &name_holder);
19841 if (include_name != NULL)
19842 dwarf2_create_include_psymtab (include_name, pst, objfile);
19843 }
19844 }
19845 else
19846 {
19847 /* Make sure a symtab is created for every file, even files
19848 which contain only variables (i.e. no code with associated
19849 line numbers). */
19850 buildsym_compunit *builder = cu->get_builder ();
19851 struct compunit_symtab *cust = builder->get_compunit_symtab ();
19852
19853 for (auto &fe : lh->file_names ())
19854 {
19855 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
19856 if (builder->get_current_subfile ()->symtab == NULL)
19857 {
19858 builder->get_current_subfile ()->symtab
19859 = allocate_symtab (cust,
19860 builder->get_current_subfile ()->name);
19861 }
19862 fe.symtab = builder->get_current_subfile ()->symtab;
19863 }
19864 }
19865 }
19866
19867 /* Start a subfile for DWARF. FILENAME is the name of the file and
19868 DIRNAME the name of the source directory which contains FILENAME
19869 or NULL if not known.
19870 This routine tries to keep line numbers from identical absolute and
19871 relative file names in a common subfile.
19872
19873 Using the `list' example from the GDB testsuite, which resides in
19874 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19875 of /srcdir/list0.c yields the following debugging information for list0.c:
19876
19877 DW_AT_name: /srcdir/list0.c
19878 DW_AT_comp_dir: /compdir
19879 files.files[0].name: list0.h
19880 files.files[0].dir: /srcdir
19881 files.files[1].name: list0.c
19882 files.files[1].dir: /srcdir
19883
19884 The line number information for list0.c has to end up in a single
19885 subfile, so that `break /srcdir/list0.c:1' works as expected.
19886 start_subfile will ensure that this happens provided that we pass the
19887 concatenation of files.files[1].dir and files.files[1].name as the
19888 subfile's name. */
19889
19890 static void
19891 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19892 const char *dirname)
19893 {
19894 gdb::unique_xmalloc_ptr<char> copy;
19895
19896 /* In order not to lose the line information directory,
19897 we concatenate it to the filename when it makes sense.
19898 Note that the Dwarf3 standard says (speaking of filenames in line
19899 information): ``The directory index is ignored for file names
19900 that represent full path names''. Thus ignoring dirname in the
19901 `else' branch below isn't an issue. */
19902
19903 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
19904 {
19905 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19906 filename = copy.get ();
19907 }
19908
19909 cu->get_builder ()->start_subfile (filename);
19910 }
19911
19912 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19913 buildsym_compunit constructor. */
19914
19915 struct compunit_symtab *
19916 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19917 CORE_ADDR low_pc)
19918 {
19919 gdb_assert (m_builder == nullptr);
19920
19921 m_builder.reset (new struct buildsym_compunit
19922 (per_cu->dwarf2_per_objfile->objfile,
19923 name, comp_dir, language, low_pc));
19924
19925 list_in_scope = get_builder ()->get_file_symbols ();
19926
19927 get_builder ()->record_debugformat ("DWARF 2");
19928 get_builder ()->record_producer (producer);
19929
19930 processing_has_namespace_info = false;
19931
19932 return get_builder ()->get_compunit_symtab ();
19933 }
19934
19935 static void
19936 var_decode_location (struct attribute *attr, struct symbol *sym,
19937 struct dwarf2_cu *cu)
19938 {
19939 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19940 struct comp_unit_head *cu_header = &cu->header;
19941
19942 /* NOTE drow/2003-01-30: There used to be a comment and some special
19943 code here to turn a symbol with DW_AT_external and a
19944 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
19945 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
19946 with some versions of binutils) where shared libraries could have
19947 relocations against symbols in their debug information - the
19948 minimal symbol would have the right address, but the debug info
19949 would not. It's no longer necessary, because we will explicitly
19950 apply relocations when we read in the debug information now. */
19951
19952 /* A DW_AT_location attribute with no contents indicates that a
19953 variable has been optimized away. */
19954 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
19955 {
19956 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
19957 return;
19958 }
19959
19960 /* Handle one degenerate form of location expression specially, to
19961 preserve GDB's previous behavior when section offsets are
19962 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
19963 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
19964
19965 if (attr->form_is_block ()
19966 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
19967 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
19968 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
19969 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
19970 && (DW_BLOCK (attr)->size
19971 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
19972 {
19973 unsigned int dummy;
19974
19975 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
19976 SET_SYMBOL_VALUE_ADDRESS
19977 (sym, cu->header.read_address (objfile->obfd,
19978 DW_BLOCK (attr)->data + 1,
19979 &dummy));
19980 else
19981 SET_SYMBOL_VALUE_ADDRESS
19982 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
19983 &dummy));
19984 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
19985 fixup_symbol_section (sym, objfile);
19986 SET_SYMBOL_VALUE_ADDRESS
19987 (sym,
19988 SYMBOL_VALUE_ADDRESS (sym)
19989 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
19990 return;
19991 }
19992
19993 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
19994 expression evaluator, and use LOC_COMPUTED only when necessary
19995 (i.e. when the value of a register or memory location is
19996 referenced, or a thread-local block, etc.). Then again, it might
19997 not be worthwhile. I'm assuming that it isn't unless performance
19998 or memory numbers show me otherwise. */
19999
20000 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20001
20002 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20003 cu->has_loclist = true;
20004 }
20005
20006 /* Given a pointer to a DWARF information entry, figure out if we need
20007 to make a symbol table entry for it, and if so, create a new entry
20008 and return a pointer to it.
20009 If TYPE is NULL, determine symbol type from the die, otherwise
20010 used the passed type.
20011 If SPACE is not NULL, use it to hold the new symbol. If it is
20012 NULL, allocate a new symbol on the objfile's obstack. */
20013
20014 static struct symbol *
20015 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20016 struct symbol *space)
20017 {
20018 struct dwarf2_per_objfile *dwarf2_per_objfile
20019 = cu->per_cu->dwarf2_per_objfile;
20020 struct objfile *objfile = dwarf2_per_objfile->objfile;
20021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
20022 struct symbol *sym = NULL;
20023 const char *name;
20024 struct attribute *attr = NULL;
20025 struct attribute *attr2 = NULL;
20026 CORE_ADDR baseaddr;
20027 struct pending **list_to_add = NULL;
20028
20029 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20030
20031 baseaddr = objfile->text_section_offset ();
20032
20033 name = dwarf2_name (die, cu);
20034 if (name)
20035 {
20036 const char *linkagename;
20037 int suppress_add = 0;
20038
20039 if (space)
20040 sym = space;
20041 else
20042 sym = allocate_symbol (objfile);
20043 OBJSTAT (objfile, n_syms++);
20044
20045 /* Cache this symbol's name and the name's demangled form (if any). */
20046 sym->set_language (cu->language, &objfile->objfile_obstack);
20047 linkagename = dwarf2_physname (name, die, cu);
20048 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
20049
20050 /* Fortran does not have mangling standard and the mangling does differ
20051 between gfortran, iFort etc. */
20052 if (cu->language == language_fortran
20053 && symbol_get_demangled_name (sym) == NULL)
20054 symbol_set_demangled_name (sym,
20055 dwarf2_full_name (name, die, cu),
20056 NULL);
20057
20058 /* Default assumptions.
20059 Use the passed type or decode it from the die. */
20060 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20061 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20062 if (type != NULL)
20063 SYMBOL_TYPE (sym) = type;
20064 else
20065 SYMBOL_TYPE (sym) = die_type (die, cu);
20066 attr = dwarf2_attr (die,
20067 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20068 cu);
20069 if (attr != nullptr)
20070 {
20071 SYMBOL_LINE (sym) = DW_UNSND (attr);
20072 }
20073
20074 attr = dwarf2_attr (die,
20075 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20076 cu);
20077 if (attr != nullptr)
20078 {
20079 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20080 struct file_entry *fe;
20081
20082 if (cu->line_header != NULL)
20083 fe = cu->line_header->file_name_at (file_index);
20084 else
20085 fe = NULL;
20086
20087 if (fe == NULL)
20088 complaint (_("file index out of range"));
20089 else
20090 symbol_set_symtab (sym, fe->symtab);
20091 }
20092
20093 switch (die->tag)
20094 {
20095 case DW_TAG_label:
20096 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20097 if (attr != nullptr)
20098 {
20099 CORE_ADDR addr;
20100
20101 addr = attr->value_as_address ();
20102 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20103 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20104 }
20105 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20106 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20107 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20108 add_symbol_to_list (sym, cu->list_in_scope);
20109 break;
20110 case DW_TAG_subprogram:
20111 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20112 finish_block. */
20113 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20114 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20115 if ((attr2 && (DW_UNSND (attr2) != 0))
20116 || cu->language == language_ada
20117 || cu->language == language_fortran)
20118 {
20119 /* Subprograms marked external are stored as a global symbol.
20120 Ada and Fortran subprograms, whether marked external or
20121 not, are always stored as a global symbol, because we want
20122 to be able to access them globally. For instance, we want
20123 to be able to break on a nested subprogram without having
20124 to specify the context. */
20125 list_to_add = cu->get_builder ()->get_global_symbols ();
20126 }
20127 else
20128 {
20129 list_to_add = cu->list_in_scope;
20130 }
20131 break;
20132 case DW_TAG_inlined_subroutine:
20133 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20134 finish_block. */
20135 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20136 SYMBOL_INLINED (sym) = 1;
20137 list_to_add = cu->list_in_scope;
20138 break;
20139 case DW_TAG_template_value_param:
20140 suppress_add = 1;
20141 /* Fall through. */
20142 case DW_TAG_constant:
20143 case DW_TAG_variable:
20144 case DW_TAG_member:
20145 /* Compilation with minimal debug info may result in
20146 variables with missing type entries. Change the
20147 misleading `void' type to something sensible. */
20148 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
20149 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20150
20151 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20152 /* In the case of DW_TAG_member, we should only be called for
20153 static const members. */
20154 if (die->tag == DW_TAG_member)
20155 {
20156 /* dwarf2_add_field uses die_is_declaration,
20157 so we do the same. */
20158 gdb_assert (die_is_declaration (die, cu));
20159 gdb_assert (attr);
20160 }
20161 if (attr != nullptr)
20162 {
20163 dwarf2_const_value (attr, sym, cu);
20164 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20165 if (!suppress_add)
20166 {
20167 if (attr2 && (DW_UNSND (attr2) != 0))
20168 list_to_add = cu->get_builder ()->get_global_symbols ();
20169 else
20170 list_to_add = cu->list_in_scope;
20171 }
20172 break;
20173 }
20174 attr = dwarf2_attr (die, DW_AT_location, cu);
20175 if (attr != nullptr)
20176 {
20177 var_decode_location (attr, sym, cu);
20178 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20179
20180 /* Fortran explicitly imports any global symbols to the local
20181 scope by DW_TAG_common_block. */
20182 if (cu->language == language_fortran && die->parent
20183 && die->parent->tag == DW_TAG_common_block)
20184 attr2 = NULL;
20185
20186 if (SYMBOL_CLASS (sym) == LOC_STATIC
20187 && SYMBOL_VALUE_ADDRESS (sym) == 0
20188 && !dwarf2_per_objfile->has_section_at_zero)
20189 {
20190 /* When a static variable is eliminated by the linker,
20191 the corresponding debug information is not stripped
20192 out, but the variable address is set to null;
20193 do not add such variables into symbol table. */
20194 }
20195 else if (attr2 && (DW_UNSND (attr2) != 0))
20196 {
20197 if (SYMBOL_CLASS (sym) == LOC_STATIC
20198 && (objfile->flags & OBJF_MAINLINE) == 0
20199 && dwarf2_per_objfile->can_copy)
20200 {
20201 /* A global static variable might be subject to
20202 copy relocation. We first check for a local
20203 minsym, though, because maybe the symbol was
20204 marked hidden, in which case this would not
20205 apply. */
20206 bound_minimal_symbol found
20207 = (lookup_minimal_symbol_linkage
20208 (sym->linkage_name (), objfile));
20209 if (found.minsym != nullptr)
20210 sym->maybe_copied = 1;
20211 }
20212
20213 /* A variable with DW_AT_external is never static,
20214 but it may be block-scoped. */
20215 list_to_add
20216 = ((cu->list_in_scope
20217 == cu->get_builder ()->get_file_symbols ())
20218 ? cu->get_builder ()->get_global_symbols ()
20219 : cu->list_in_scope);
20220 }
20221 else
20222 list_to_add = cu->list_in_scope;
20223 }
20224 else
20225 {
20226 /* We do not know the address of this symbol.
20227 If it is an external symbol and we have type information
20228 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20229 The address of the variable will then be determined from
20230 the minimal symbol table whenever the variable is
20231 referenced. */
20232 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20233
20234 /* Fortran explicitly imports any global symbols to the local
20235 scope by DW_TAG_common_block. */
20236 if (cu->language == language_fortran && die->parent
20237 && die->parent->tag == DW_TAG_common_block)
20238 {
20239 /* SYMBOL_CLASS doesn't matter here because
20240 read_common_block is going to reset it. */
20241 if (!suppress_add)
20242 list_to_add = cu->list_in_scope;
20243 }
20244 else if (attr2 && (DW_UNSND (attr2) != 0)
20245 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20246 {
20247 /* A variable with DW_AT_external is never static, but it
20248 may be block-scoped. */
20249 list_to_add
20250 = ((cu->list_in_scope
20251 == cu->get_builder ()->get_file_symbols ())
20252 ? cu->get_builder ()->get_global_symbols ()
20253 : cu->list_in_scope);
20254
20255 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20256 }
20257 else if (!die_is_declaration (die, cu))
20258 {
20259 /* Use the default LOC_OPTIMIZED_OUT class. */
20260 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20261 if (!suppress_add)
20262 list_to_add = cu->list_in_scope;
20263 }
20264 }
20265 break;
20266 case DW_TAG_formal_parameter:
20267 {
20268 /* If we are inside a function, mark this as an argument. If
20269 not, we might be looking at an argument to an inlined function
20270 when we do not have enough information to show inlined frames;
20271 pretend it's a local variable in that case so that the user can
20272 still see it. */
20273 struct context_stack *curr
20274 = cu->get_builder ()->get_current_context_stack ();
20275 if (curr != nullptr && curr->name != nullptr)
20276 SYMBOL_IS_ARGUMENT (sym) = 1;
20277 attr = dwarf2_attr (die, DW_AT_location, cu);
20278 if (attr != nullptr)
20279 {
20280 var_decode_location (attr, sym, cu);
20281 }
20282 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20283 if (attr != nullptr)
20284 {
20285 dwarf2_const_value (attr, sym, cu);
20286 }
20287
20288 list_to_add = cu->list_in_scope;
20289 }
20290 break;
20291 case DW_TAG_unspecified_parameters:
20292 /* From varargs functions; gdb doesn't seem to have any
20293 interest in this information, so just ignore it for now.
20294 (FIXME?) */
20295 break;
20296 case DW_TAG_template_type_param:
20297 suppress_add = 1;
20298 /* Fall through. */
20299 case DW_TAG_class_type:
20300 case DW_TAG_interface_type:
20301 case DW_TAG_structure_type:
20302 case DW_TAG_union_type:
20303 case DW_TAG_set_type:
20304 case DW_TAG_enumeration_type:
20305 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20306 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20307
20308 {
20309 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20310 really ever be static objects: otherwise, if you try
20311 to, say, break of a class's method and you're in a file
20312 which doesn't mention that class, it won't work unless
20313 the check for all static symbols in lookup_symbol_aux
20314 saves you. See the OtherFileClass tests in
20315 gdb.c++/namespace.exp. */
20316
20317 if (!suppress_add)
20318 {
20319 buildsym_compunit *builder = cu->get_builder ();
20320 list_to_add
20321 = (cu->list_in_scope == builder->get_file_symbols ()
20322 && cu->language == language_cplus
20323 ? builder->get_global_symbols ()
20324 : cu->list_in_scope);
20325
20326 /* The semantics of C++ state that "struct foo {
20327 ... }" also defines a typedef for "foo". */
20328 if (cu->language == language_cplus
20329 || cu->language == language_ada
20330 || cu->language == language_d
20331 || cu->language == language_rust)
20332 {
20333 /* The symbol's name is already allocated along
20334 with this objfile, so we don't need to
20335 duplicate it for the type. */
20336 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
20337 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
20338 }
20339 }
20340 }
20341 break;
20342 case DW_TAG_typedef:
20343 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20344 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20345 list_to_add = cu->list_in_scope;
20346 break;
20347 case DW_TAG_base_type:
20348 case DW_TAG_subrange_type:
20349 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20350 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20351 list_to_add = cu->list_in_scope;
20352 break;
20353 case DW_TAG_enumerator:
20354 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20355 if (attr != nullptr)
20356 {
20357 dwarf2_const_value (attr, sym, cu);
20358 }
20359 {
20360 /* NOTE: carlton/2003-11-10: See comment above in the
20361 DW_TAG_class_type, etc. block. */
20362
20363 list_to_add
20364 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
20365 && cu->language == language_cplus
20366 ? cu->get_builder ()->get_global_symbols ()
20367 : cu->list_in_scope);
20368 }
20369 break;
20370 case DW_TAG_imported_declaration:
20371 case DW_TAG_namespace:
20372 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20373 list_to_add = cu->get_builder ()->get_global_symbols ();
20374 break;
20375 case DW_TAG_module:
20376 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20377 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
20378 list_to_add = cu->get_builder ()->get_global_symbols ();
20379 break;
20380 case DW_TAG_common_block:
20381 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
20382 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
20383 add_symbol_to_list (sym, cu->list_in_scope);
20384 break;
20385 default:
20386 /* Not a tag we recognize. Hopefully we aren't processing
20387 trash data, but since we must specifically ignore things
20388 we don't recognize, there is nothing else we should do at
20389 this point. */
20390 complaint (_("unsupported tag: '%s'"),
20391 dwarf_tag_name (die->tag));
20392 break;
20393 }
20394
20395 if (suppress_add)
20396 {
20397 sym->hash_next = objfile->template_symbols;
20398 objfile->template_symbols = sym;
20399 list_to_add = NULL;
20400 }
20401
20402 if (list_to_add != NULL)
20403 add_symbol_to_list (sym, list_to_add);
20404
20405 /* For the benefit of old versions of GCC, check for anonymous
20406 namespaces based on the demangled name. */
20407 if (!cu->processing_has_namespace_info
20408 && cu->language == language_cplus)
20409 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
20410 }
20411 return (sym);
20412 }
20413
20414 /* Given an attr with a DW_FORM_dataN value in host byte order,
20415 zero-extend it as appropriate for the symbol's type. The DWARF
20416 standard (v4) is not entirely clear about the meaning of using
20417 DW_FORM_dataN for a constant with a signed type, where the type is
20418 wider than the data. The conclusion of a discussion on the DWARF
20419 list was that this is unspecified. We choose to always zero-extend
20420 because that is the interpretation long in use by GCC. */
20421
20422 static gdb_byte *
20423 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
20424 struct dwarf2_cu *cu, LONGEST *value, int bits)
20425 {
20426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20427 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20428 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
20429 LONGEST l = DW_UNSND (attr);
20430
20431 if (bits < sizeof (*value) * 8)
20432 {
20433 l &= ((LONGEST) 1 << bits) - 1;
20434 *value = l;
20435 }
20436 else if (bits == sizeof (*value) * 8)
20437 *value = l;
20438 else
20439 {
20440 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
20441 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20442 return bytes;
20443 }
20444
20445 return NULL;
20446 }
20447
20448 /* Read a constant value from an attribute. Either set *VALUE, or if
20449 the value does not fit in *VALUE, set *BYTES - either already
20450 allocated on the objfile obstack, or newly allocated on OBSTACK,
20451 or, set *BATON, if we translated the constant to a location
20452 expression. */
20453
20454 static void
20455 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
20456 const char *name, struct obstack *obstack,
20457 struct dwarf2_cu *cu,
20458 LONGEST *value, const gdb_byte **bytes,
20459 struct dwarf2_locexpr_baton **baton)
20460 {
20461 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20462 struct comp_unit_head *cu_header = &cu->header;
20463 struct dwarf_block *blk;
20464 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20465 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20466
20467 *value = 0;
20468 *bytes = NULL;
20469 *baton = NULL;
20470
20471 switch (attr->form)
20472 {
20473 case DW_FORM_addr:
20474 case DW_FORM_addrx:
20475 case DW_FORM_GNU_addr_index:
20476 {
20477 gdb_byte *data;
20478
20479 if (TYPE_LENGTH (type) != cu_header->addr_size)
20480 dwarf2_const_value_length_mismatch_complaint (name,
20481 cu_header->addr_size,
20482 TYPE_LENGTH (type));
20483 /* Symbols of this form are reasonably rare, so we just
20484 piggyback on the existing location code rather than writing
20485 a new implementation of symbol_computed_ops. */
20486 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
20487 (*baton)->per_cu = cu->per_cu;
20488 gdb_assert ((*baton)->per_cu);
20489
20490 (*baton)->size = 2 + cu_header->addr_size;
20491 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
20492 (*baton)->data = data;
20493
20494 data[0] = DW_OP_addr;
20495 store_unsigned_integer (&data[1], cu_header->addr_size,
20496 byte_order, DW_ADDR (attr));
20497 data[cu_header->addr_size + 1] = DW_OP_stack_value;
20498 }
20499 break;
20500 case DW_FORM_string:
20501 case DW_FORM_strp:
20502 case DW_FORM_strx:
20503 case DW_FORM_GNU_str_index:
20504 case DW_FORM_GNU_strp_alt:
20505 /* DW_STRING is already allocated on the objfile obstack, point
20506 directly to it. */
20507 *bytes = (const gdb_byte *) DW_STRING (attr);
20508 break;
20509 case DW_FORM_block1:
20510 case DW_FORM_block2:
20511 case DW_FORM_block4:
20512 case DW_FORM_block:
20513 case DW_FORM_exprloc:
20514 case DW_FORM_data16:
20515 blk = DW_BLOCK (attr);
20516 if (TYPE_LENGTH (type) != blk->size)
20517 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20518 TYPE_LENGTH (type));
20519 *bytes = blk->data;
20520 break;
20521
20522 /* The DW_AT_const_value attributes are supposed to carry the
20523 symbol's value "represented as it would be on the target
20524 architecture." By the time we get here, it's already been
20525 converted to host endianness, so we just need to sign- or
20526 zero-extend it as appropriate. */
20527 case DW_FORM_data1:
20528 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
20529 break;
20530 case DW_FORM_data2:
20531 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
20532 break;
20533 case DW_FORM_data4:
20534 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
20535 break;
20536 case DW_FORM_data8:
20537 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
20538 break;
20539
20540 case DW_FORM_sdata:
20541 case DW_FORM_implicit_const:
20542 *value = DW_SND (attr);
20543 break;
20544
20545 case DW_FORM_udata:
20546 *value = DW_UNSND (attr);
20547 break;
20548
20549 default:
20550 complaint (_("unsupported const value attribute form: '%s'"),
20551 dwarf_form_name (attr->form));
20552 *value = 0;
20553 break;
20554 }
20555 }
20556
20557
20558 /* Copy constant value from an attribute to a symbol. */
20559
20560 static void
20561 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
20562 struct dwarf2_cu *cu)
20563 {
20564 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20565 LONGEST value;
20566 const gdb_byte *bytes;
20567 struct dwarf2_locexpr_baton *baton;
20568
20569 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
20570 sym->print_name (),
20571 &objfile->objfile_obstack, cu,
20572 &value, &bytes, &baton);
20573
20574 if (baton != NULL)
20575 {
20576 SYMBOL_LOCATION_BATON (sym) = baton;
20577 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
20578 }
20579 else if (bytes != NULL)
20580 {
20581 SYMBOL_VALUE_BYTES (sym) = bytes;
20582 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
20583 }
20584 else
20585 {
20586 SYMBOL_VALUE (sym) = value;
20587 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
20588 }
20589 }
20590
20591 /* Return the type of the die in question using its DW_AT_type attribute. */
20592
20593 static struct type *
20594 die_type (struct die_info *die, struct dwarf2_cu *cu)
20595 {
20596 struct attribute *type_attr;
20597
20598 type_attr = dwarf2_attr (die, DW_AT_type, cu);
20599 if (!type_attr)
20600 {
20601 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20602 /* A missing DW_AT_type represents a void type. */
20603 return objfile_type (objfile)->builtin_void;
20604 }
20605
20606 return lookup_die_type (die, type_attr, cu);
20607 }
20608
20609 /* True iff CU's producer generates GNAT Ada auxiliary information
20610 that allows to find parallel types through that information instead
20611 of having to do expensive parallel lookups by type name. */
20612
20613 static int
20614 need_gnat_info (struct dwarf2_cu *cu)
20615 {
20616 /* Assume that the Ada compiler was GNAT, which always produces
20617 the auxiliary information. */
20618 return (cu->language == language_ada);
20619 }
20620
20621 /* Return the auxiliary type of the die in question using its
20622 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20623 attribute is not present. */
20624
20625 static struct type *
20626 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20627 {
20628 struct attribute *type_attr;
20629
20630 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20631 if (!type_attr)
20632 return NULL;
20633
20634 return lookup_die_type (die, type_attr, cu);
20635 }
20636
20637 /* If DIE has a descriptive_type attribute, then set the TYPE's
20638 descriptive type accordingly. */
20639
20640 static void
20641 set_descriptive_type (struct type *type, struct die_info *die,
20642 struct dwarf2_cu *cu)
20643 {
20644 struct type *descriptive_type = die_descriptive_type (die, cu);
20645
20646 if (descriptive_type)
20647 {
20648 ALLOCATE_GNAT_AUX_TYPE (type);
20649 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20650 }
20651 }
20652
20653 /* Return the containing type of the die in question using its
20654 DW_AT_containing_type attribute. */
20655
20656 static struct type *
20657 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
20658 {
20659 struct attribute *type_attr;
20660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20661
20662 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
20663 if (!type_attr)
20664 error (_("Dwarf Error: Problem turning containing type into gdb type "
20665 "[in module %s]"), objfile_name (objfile));
20666
20667 return lookup_die_type (die, type_attr, cu);
20668 }
20669
20670 /* Return an error marker type to use for the ill formed type in DIE/CU. */
20671
20672 static struct type *
20673 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20674 {
20675 struct dwarf2_per_objfile *dwarf2_per_objfile
20676 = cu->per_cu->dwarf2_per_objfile;
20677 struct objfile *objfile = dwarf2_per_objfile->objfile;
20678 char *saved;
20679
20680 std::string message
20681 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20682 objfile_name (objfile),
20683 sect_offset_str (cu->header.sect_off),
20684 sect_offset_str (die->sect_off));
20685 saved = obstack_strdup (&objfile->objfile_obstack, message);
20686
20687 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
20688 }
20689
20690 /* Look up the type of DIE in CU using its type attribute ATTR.
20691 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20692 DW_AT_containing_type.
20693 If there is no type substitute an error marker. */
20694
20695 static struct type *
20696 lookup_die_type (struct die_info *die, const struct attribute *attr,
20697 struct dwarf2_cu *cu)
20698 {
20699 struct dwarf2_per_objfile *dwarf2_per_objfile
20700 = cu->per_cu->dwarf2_per_objfile;
20701 struct objfile *objfile = dwarf2_per_objfile->objfile;
20702 struct type *this_type;
20703
20704 gdb_assert (attr->name == DW_AT_type
20705 || attr->name == DW_AT_GNAT_descriptive_type
20706 || attr->name == DW_AT_containing_type);
20707
20708 /* First see if we have it cached. */
20709
20710 if (attr->form == DW_FORM_GNU_ref_alt)
20711 {
20712 struct dwarf2_per_cu_data *per_cu;
20713 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20714
20715 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20716 dwarf2_per_objfile);
20717 this_type = get_die_type_at_offset (sect_off, per_cu);
20718 }
20719 else if (attr->form_is_ref ())
20720 {
20721 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
20722
20723 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
20724 }
20725 else if (attr->form == DW_FORM_ref_sig8)
20726 {
20727 ULONGEST signature = DW_SIGNATURE (attr);
20728
20729 return get_signatured_type (die, signature, cu);
20730 }
20731 else
20732 {
20733 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
20734 " at %s [in module %s]"),
20735 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
20736 objfile_name (objfile));
20737 return build_error_marker_type (cu, die);
20738 }
20739
20740 /* If not cached we need to read it in. */
20741
20742 if (this_type == NULL)
20743 {
20744 struct die_info *type_die = NULL;
20745 struct dwarf2_cu *type_cu = cu;
20746
20747 if (attr->form_is_ref ())
20748 type_die = follow_die_ref (die, attr, &type_cu);
20749 if (type_die == NULL)
20750 return build_error_marker_type (cu, die);
20751 /* If we find the type now, it's probably because the type came
20752 from an inter-CU reference and the type's CU got expanded before
20753 ours. */
20754 this_type = read_type_die (type_die, type_cu);
20755 }
20756
20757 /* If we still don't have a type use an error marker. */
20758
20759 if (this_type == NULL)
20760 return build_error_marker_type (cu, die);
20761
20762 return this_type;
20763 }
20764
20765 /* Return the type in DIE, CU.
20766 Returns NULL for invalid types.
20767
20768 This first does a lookup in die_type_hash,
20769 and only reads the die in if necessary.
20770
20771 NOTE: This can be called when reading in partial or full symbols. */
20772
20773 static struct type *
20774 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
20775 {
20776 struct type *this_type;
20777
20778 this_type = get_die_type (die, cu);
20779 if (this_type)
20780 return this_type;
20781
20782 return read_type_die_1 (die, cu);
20783 }
20784
20785 /* Read the type in DIE, CU.
20786 Returns NULL for invalid types. */
20787
20788 static struct type *
20789 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20790 {
20791 struct type *this_type = NULL;
20792
20793 switch (die->tag)
20794 {
20795 case DW_TAG_class_type:
20796 case DW_TAG_interface_type:
20797 case DW_TAG_structure_type:
20798 case DW_TAG_union_type:
20799 this_type = read_structure_type (die, cu);
20800 break;
20801 case DW_TAG_enumeration_type:
20802 this_type = read_enumeration_type (die, cu);
20803 break;
20804 case DW_TAG_subprogram:
20805 case DW_TAG_subroutine_type:
20806 case DW_TAG_inlined_subroutine:
20807 this_type = read_subroutine_type (die, cu);
20808 break;
20809 case DW_TAG_array_type:
20810 this_type = read_array_type (die, cu);
20811 break;
20812 case DW_TAG_set_type:
20813 this_type = read_set_type (die, cu);
20814 break;
20815 case DW_TAG_pointer_type:
20816 this_type = read_tag_pointer_type (die, cu);
20817 break;
20818 case DW_TAG_ptr_to_member_type:
20819 this_type = read_tag_ptr_to_member_type (die, cu);
20820 break;
20821 case DW_TAG_reference_type:
20822 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20823 break;
20824 case DW_TAG_rvalue_reference_type:
20825 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
20826 break;
20827 case DW_TAG_const_type:
20828 this_type = read_tag_const_type (die, cu);
20829 break;
20830 case DW_TAG_volatile_type:
20831 this_type = read_tag_volatile_type (die, cu);
20832 break;
20833 case DW_TAG_restrict_type:
20834 this_type = read_tag_restrict_type (die, cu);
20835 break;
20836 case DW_TAG_string_type:
20837 this_type = read_tag_string_type (die, cu);
20838 break;
20839 case DW_TAG_typedef:
20840 this_type = read_typedef (die, cu);
20841 break;
20842 case DW_TAG_subrange_type:
20843 this_type = read_subrange_type (die, cu);
20844 break;
20845 case DW_TAG_base_type:
20846 this_type = read_base_type (die, cu);
20847 break;
20848 case DW_TAG_unspecified_type:
20849 this_type = read_unspecified_type (die, cu);
20850 break;
20851 case DW_TAG_namespace:
20852 this_type = read_namespace_type (die, cu);
20853 break;
20854 case DW_TAG_module:
20855 this_type = read_module_type (die, cu);
20856 break;
20857 case DW_TAG_atomic_type:
20858 this_type = read_tag_atomic_type (die, cu);
20859 break;
20860 default:
20861 complaint (_("unexpected tag in read_type_die: '%s'"),
20862 dwarf_tag_name (die->tag));
20863 break;
20864 }
20865
20866 return this_type;
20867 }
20868
20869 /* See if we can figure out if the class lives in a namespace. We do
20870 this by looking for a member function; its demangled name will
20871 contain namespace info, if there is any.
20872 Return the computed name or NULL.
20873 Space for the result is allocated on the objfile's obstack.
20874 This is the full-die version of guess_partial_die_structure_name.
20875 In this case we know DIE has no useful parent. */
20876
20877 static const char *
20878 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20879 {
20880 struct die_info *spec_die;
20881 struct dwarf2_cu *spec_cu;
20882 struct die_info *child;
20883 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20884
20885 spec_cu = cu;
20886 spec_die = die_specification (die, &spec_cu);
20887 if (spec_die != NULL)
20888 {
20889 die = spec_die;
20890 cu = spec_cu;
20891 }
20892
20893 for (child = die->child;
20894 child != NULL;
20895 child = child->sibling)
20896 {
20897 if (child->tag == DW_TAG_subprogram)
20898 {
20899 const char *linkage_name = dw2_linkage_name (child, cu);
20900
20901 if (linkage_name != NULL)
20902 {
20903 gdb::unique_xmalloc_ptr<char> actual_name
20904 (language_class_name_from_physname (cu->language_defn,
20905 linkage_name));
20906 const char *name = NULL;
20907
20908 if (actual_name != NULL)
20909 {
20910 const char *die_name = dwarf2_name (die, cu);
20911
20912 if (die_name != NULL
20913 && strcmp (die_name, actual_name.get ()) != 0)
20914 {
20915 /* Strip off the class name from the full name.
20916 We want the prefix. */
20917 int die_name_len = strlen (die_name);
20918 int actual_name_len = strlen (actual_name.get ());
20919 const char *ptr = actual_name.get ();
20920
20921 /* Test for '::' as a sanity check. */
20922 if (actual_name_len > die_name_len + 2
20923 && ptr[actual_name_len - die_name_len - 1] == ':')
20924 name = obstack_strndup (
20925 &objfile->per_bfd->storage_obstack,
20926 ptr, actual_name_len - die_name_len - 2);
20927 }
20928 }
20929 return name;
20930 }
20931 }
20932 }
20933
20934 return NULL;
20935 }
20936
20937 /* GCC might emit a nameless typedef that has a linkage name. Determine the
20938 prefix part in such case. See
20939 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20940
20941 static const char *
20942 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
20943 {
20944 struct attribute *attr;
20945 const char *base;
20946
20947 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
20948 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
20949 return NULL;
20950
20951 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
20952 return NULL;
20953
20954 attr = dw2_linkage_name_attr (die, cu);
20955 if (attr == NULL || DW_STRING (attr) == NULL)
20956 return NULL;
20957
20958 /* dwarf2_name had to be already called. */
20959 gdb_assert (DW_STRING_IS_CANONICAL (attr));
20960
20961 /* Strip the base name, keep any leading namespaces/classes. */
20962 base = strrchr (DW_STRING (attr), ':');
20963 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
20964 return "";
20965
20966 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
20967 return obstack_strndup (&objfile->per_bfd->storage_obstack,
20968 DW_STRING (attr),
20969 &base[-1] - DW_STRING (attr));
20970 }
20971
20972 /* Return the name of the namespace/class that DIE is defined within,
20973 or "" if we can't tell. The caller should not xfree the result.
20974
20975 For example, if we're within the method foo() in the following
20976 code:
20977
20978 namespace N {
20979 class C {
20980 void foo () {
20981 }
20982 };
20983 }
20984
20985 then determine_prefix on foo's die will return "N::C". */
20986
20987 static const char *
20988 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
20989 {
20990 struct dwarf2_per_objfile *dwarf2_per_objfile
20991 = cu->per_cu->dwarf2_per_objfile;
20992 struct die_info *parent, *spec_die;
20993 struct dwarf2_cu *spec_cu;
20994 struct type *parent_type;
20995 const char *retval;
20996
20997 if (cu->language != language_cplus
20998 && cu->language != language_fortran && cu->language != language_d
20999 && cu->language != language_rust)
21000 return "";
21001
21002 retval = anonymous_struct_prefix (die, cu);
21003 if (retval)
21004 return retval;
21005
21006 /* We have to be careful in the presence of DW_AT_specification.
21007 For example, with GCC 3.4, given the code
21008
21009 namespace N {
21010 void foo() {
21011 // Definition of N::foo.
21012 }
21013 }
21014
21015 then we'll have a tree of DIEs like this:
21016
21017 1: DW_TAG_compile_unit
21018 2: DW_TAG_namespace // N
21019 3: DW_TAG_subprogram // declaration of N::foo
21020 4: DW_TAG_subprogram // definition of N::foo
21021 DW_AT_specification // refers to die #3
21022
21023 Thus, when processing die #4, we have to pretend that we're in
21024 the context of its DW_AT_specification, namely the contex of die
21025 #3. */
21026 spec_cu = cu;
21027 spec_die = die_specification (die, &spec_cu);
21028 if (spec_die == NULL)
21029 parent = die->parent;
21030 else
21031 {
21032 parent = spec_die->parent;
21033 cu = spec_cu;
21034 }
21035
21036 if (parent == NULL)
21037 return "";
21038 else if (parent->building_fullname)
21039 {
21040 const char *name;
21041 const char *parent_name;
21042
21043 /* It has been seen on RealView 2.2 built binaries,
21044 DW_TAG_template_type_param types actually _defined_ as
21045 children of the parent class:
21046
21047 enum E {};
21048 template class <class Enum> Class{};
21049 Class<enum E> class_e;
21050
21051 1: DW_TAG_class_type (Class)
21052 2: DW_TAG_enumeration_type (E)
21053 3: DW_TAG_enumerator (enum1:0)
21054 3: DW_TAG_enumerator (enum2:1)
21055 ...
21056 2: DW_TAG_template_type_param
21057 DW_AT_type DW_FORM_ref_udata (E)
21058
21059 Besides being broken debug info, it can put GDB into an
21060 infinite loop. Consider:
21061
21062 When we're building the full name for Class<E>, we'll start
21063 at Class, and go look over its template type parameters,
21064 finding E. We'll then try to build the full name of E, and
21065 reach here. We're now trying to build the full name of E,
21066 and look over the parent DIE for containing scope. In the
21067 broken case, if we followed the parent DIE of E, we'd again
21068 find Class, and once again go look at its template type
21069 arguments, etc., etc. Simply don't consider such parent die
21070 as source-level parent of this die (it can't be, the language
21071 doesn't allow it), and break the loop here. */
21072 name = dwarf2_name (die, cu);
21073 parent_name = dwarf2_name (parent, cu);
21074 complaint (_("template param type '%s' defined within parent '%s'"),
21075 name ? name : "<unknown>",
21076 parent_name ? parent_name : "<unknown>");
21077 return "";
21078 }
21079 else
21080 switch (parent->tag)
21081 {
21082 case DW_TAG_namespace:
21083 parent_type = read_type_die (parent, cu);
21084 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21085 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21086 Work around this problem here. */
21087 if (cu->language == language_cplus
21088 && strcmp (TYPE_NAME (parent_type), "::") == 0)
21089 return "";
21090 /* We give a name to even anonymous namespaces. */
21091 return TYPE_NAME (parent_type);
21092 case DW_TAG_class_type:
21093 case DW_TAG_interface_type:
21094 case DW_TAG_structure_type:
21095 case DW_TAG_union_type:
21096 case DW_TAG_module:
21097 parent_type = read_type_die (parent, cu);
21098 if (TYPE_NAME (parent_type) != NULL)
21099 return TYPE_NAME (parent_type);
21100 else
21101 /* An anonymous structure is only allowed non-static data
21102 members; no typedefs, no member functions, et cetera.
21103 So it does not need a prefix. */
21104 return "";
21105 case DW_TAG_compile_unit:
21106 case DW_TAG_partial_unit:
21107 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21108 if (cu->language == language_cplus
21109 && !dwarf2_per_objfile->types.empty ()
21110 && die->child != NULL
21111 && (die->tag == DW_TAG_class_type
21112 || die->tag == DW_TAG_structure_type
21113 || die->tag == DW_TAG_union_type))
21114 {
21115 const char *name = guess_full_die_structure_name (die, cu);
21116 if (name != NULL)
21117 return name;
21118 }
21119 return "";
21120 case DW_TAG_subprogram:
21121 /* Nested subroutines in Fortran get a prefix with the name
21122 of the parent's subroutine. */
21123 if (cu->language == language_fortran)
21124 {
21125 if ((die->tag == DW_TAG_subprogram)
21126 && (dwarf2_name (parent, cu) != NULL))
21127 return dwarf2_name (parent, cu);
21128 }
21129 return determine_prefix (parent, cu);
21130 case DW_TAG_enumeration_type:
21131 parent_type = read_type_die (parent, cu);
21132 if (TYPE_DECLARED_CLASS (parent_type))
21133 {
21134 if (TYPE_NAME (parent_type) != NULL)
21135 return TYPE_NAME (parent_type);
21136 return "";
21137 }
21138 /* Fall through. */
21139 default:
21140 return determine_prefix (parent, cu);
21141 }
21142 }
21143
21144 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21145 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21146 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21147 an obconcat, otherwise allocate storage for the result. The CU argument is
21148 used to determine the language and hence, the appropriate separator. */
21149
21150 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21151
21152 static char *
21153 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21154 int physname, struct dwarf2_cu *cu)
21155 {
21156 const char *lead = "";
21157 const char *sep;
21158
21159 if (suffix == NULL || suffix[0] == '\0'
21160 || prefix == NULL || prefix[0] == '\0')
21161 sep = "";
21162 else if (cu->language == language_d)
21163 {
21164 /* For D, the 'main' function could be defined in any module, but it
21165 should never be prefixed. */
21166 if (strcmp (suffix, "D main") == 0)
21167 {
21168 prefix = "";
21169 sep = "";
21170 }
21171 else
21172 sep = ".";
21173 }
21174 else if (cu->language == language_fortran && physname)
21175 {
21176 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21177 DW_AT_MIPS_linkage_name is preferred and used instead. */
21178
21179 lead = "__";
21180 sep = "_MOD_";
21181 }
21182 else
21183 sep = "::";
21184
21185 if (prefix == NULL)
21186 prefix = "";
21187 if (suffix == NULL)
21188 suffix = "";
21189
21190 if (obs == NULL)
21191 {
21192 char *retval
21193 = ((char *)
21194 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21195
21196 strcpy (retval, lead);
21197 strcat (retval, prefix);
21198 strcat (retval, sep);
21199 strcat (retval, suffix);
21200 return retval;
21201 }
21202 else
21203 {
21204 /* We have an obstack. */
21205 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21206 }
21207 }
21208
21209 /* Get name of a die, return NULL if not found. */
21210
21211 static const char *
21212 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21213 struct objfile *objfile)
21214 {
21215 if (name && cu->language == language_cplus)
21216 {
21217 std::string canon_name = cp_canonicalize_string (name);
21218
21219 if (!canon_name.empty ())
21220 {
21221 if (canon_name != name)
21222 name = objfile->intern (canon_name);
21223 }
21224 }
21225
21226 return name;
21227 }
21228
21229 /* Get name of a die, return NULL if not found.
21230 Anonymous namespaces are converted to their magic string. */
21231
21232 static const char *
21233 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21234 {
21235 struct attribute *attr;
21236 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
21237
21238 attr = dwarf2_attr (die, DW_AT_name, cu);
21239 if ((!attr || !DW_STRING (attr))
21240 && die->tag != DW_TAG_namespace
21241 && die->tag != DW_TAG_class_type
21242 && die->tag != DW_TAG_interface_type
21243 && die->tag != DW_TAG_structure_type
21244 && die->tag != DW_TAG_union_type)
21245 return NULL;
21246
21247 switch (die->tag)
21248 {
21249 case DW_TAG_compile_unit:
21250 case DW_TAG_partial_unit:
21251 /* Compilation units have a DW_AT_name that is a filename, not
21252 a source language identifier. */
21253 case DW_TAG_enumeration_type:
21254 case DW_TAG_enumerator:
21255 /* These tags always have simple identifiers already; no need
21256 to canonicalize them. */
21257 return DW_STRING (attr);
21258
21259 case DW_TAG_namespace:
21260 if (attr != NULL && DW_STRING (attr) != NULL)
21261 return DW_STRING (attr);
21262 return CP_ANONYMOUS_NAMESPACE_STR;
21263
21264 case DW_TAG_class_type:
21265 case DW_TAG_interface_type:
21266 case DW_TAG_structure_type:
21267 case DW_TAG_union_type:
21268 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21269 structures or unions. These were of the form "._%d" in GCC 4.1,
21270 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21271 and GCC 4.4. We work around this problem by ignoring these. */
21272 if (attr && DW_STRING (attr)
21273 && (startswith (DW_STRING (attr), "._")
21274 || startswith (DW_STRING (attr), "<anonymous")))
21275 return NULL;
21276
21277 /* GCC might emit a nameless typedef that has a linkage name. See
21278 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21279 if (!attr || DW_STRING (attr) == NULL)
21280 {
21281 attr = dw2_linkage_name_attr (die, cu);
21282 if (attr == NULL || DW_STRING (attr) == NULL)
21283 return NULL;
21284
21285 /* Avoid demangling DW_STRING (attr) the second time on a second
21286 call for the same DIE. */
21287 if (!DW_STRING_IS_CANONICAL (attr))
21288 {
21289 gdb::unique_xmalloc_ptr<char> demangled
21290 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21291 if (demangled == nullptr)
21292 return nullptr;
21293
21294 DW_STRING (attr) = objfile->intern (demangled.get ());
21295 DW_STRING_IS_CANONICAL (attr) = 1;
21296 }
21297
21298 /* Strip any leading namespaces/classes, keep only the base name.
21299 DW_AT_name for named DIEs does not contain the prefixes. */
21300 const char *base = strrchr (DW_STRING (attr), ':');
21301 if (base && base > DW_STRING (attr) && base[-1] == ':')
21302 return &base[1];
21303 else
21304 return DW_STRING (attr);
21305 }
21306 break;
21307
21308 default:
21309 break;
21310 }
21311
21312 if (!DW_STRING_IS_CANONICAL (attr))
21313 {
21314 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21315 objfile);
21316 DW_STRING_IS_CANONICAL (attr) = 1;
21317 }
21318 return DW_STRING (attr);
21319 }
21320
21321 /* Return the die that this die in an extension of, or NULL if there
21322 is none. *EXT_CU is the CU containing DIE on input, and the CU
21323 containing the return value on output. */
21324
21325 static struct die_info *
21326 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21327 {
21328 struct attribute *attr;
21329
21330 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21331 if (attr == NULL)
21332 return NULL;
21333
21334 return follow_die_ref (die, attr, ext_cu);
21335 }
21336
21337 /* A convenience function that returns an "unknown" DWARF name,
21338 including the value of V. STR is the name of the entity being
21339 printed, e.g., "TAG". */
21340
21341 static const char *
21342 dwarf_unknown (const char *str, unsigned v)
21343 {
21344 char *cell = get_print_cell ();
21345 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
21346 return cell;
21347 }
21348
21349 /* Convert a DIE tag into its string name. */
21350
21351 static const char *
21352 dwarf_tag_name (unsigned tag)
21353 {
21354 const char *name = get_DW_TAG_name (tag);
21355
21356 if (name == NULL)
21357 return dwarf_unknown ("TAG", tag);
21358
21359 return name;
21360 }
21361
21362 /* Convert a DWARF attribute code into its string name. */
21363
21364 static const char *
21365 dwarf_attr_name (unsigned attr)
21366 {
21367 const char *name;
21368
21369 #ifdef MIPS /* collides with DW_AT_HP_block_index */
21370 if (attr == DW_AT_MIPS_fde)
21371 return "DW_AT_MIPS_fde";
21372 #else
21373 if (attr == DW_AT_HP_block_index)
21374 return "DW_AT_HP_block_index";
21375 #endif
21376
21377 name = get_DW_AT_name (attr);
21378
21379 if (name == NULL)
21380 return dwarf_unknown ("AT", attr);
21381
21382 return name;
21383 }
21384
21385 /* Convert a DWARF value form code into its string name. */
21386
21387 static const char *
21388 dwarf_form_name (unsigned form)
21389 {
21390 const char *name = get_DW_FORM_name (form);
21391
21392 if (name == NULL)
21393 return dwarf_unknown ("FORM", form);
21394
21395 return name;
21396 }
21397
21398 static const char *
21399 dwarf_bool_name (unsigned mybool)
21400 {
21401 if (mybool)
21402 return "TRUE";
21403 else
21404 return "FALSE";
21405 }
21406
21407 /* Convert a DWARF type code into its string name. */
21408
21409 static const char *
21410 dwarf_type_encoding_name (unsigned enc)
21411 {
21412 const char *name = get_DW_ATE_name (enc);
21413
21414 if (name == NULL)
21415 return dwarf_unknown ("ATE", enc);
21416
21417 return name;
21418 }
21419
21420 static void
21421 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
21422 {
21423 unsigned int i;
21424
21425 print_spaces (indent, f);
21426 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
21427 dwarf_tag_name (die->tag), die->abbrev,
21428 sect_offset_str (die->sect_off));
21429
21430 if (die->parent != NULL)
21431 {
21432 print_spaces (indent, f);
21433 fprintf_unfiltered (f, " parent at offset: %s\n",
21434 sect_offset_str (die->parent->sect_off));
21435 }
21436
21437 print_spaces (indent, f);
21438 fprintf_unfiltered (f, " has children: %s\n",
21439 dwarf_bool_name (die->child != NULL));
21440
21441 print_spaces (indent, f);
21442 fprintf_unfiltered (f, " attributes:\n");
21443
21444 for (i = 0; i < die->num_attrs; ++i)
21445 {
21446 print_spaces (indent, f);
21447 fprintf_unfiltered (f, " %s (%s) ",
21448 dwarf_attr_name (die->attrs[i].name),
21449 dwarf_form_name (die->attrs[i].form));
21450
21451 switch (die->attrs[i].form)
21452 {
21453 case DW_FORM_addr:
21454 case DW_FORM_addrx:
21455 case DW_FORM_GNU_addr_index:
21456 fprintf_unfiltered (f, "address: ");
21457 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
21458 break;
21459 case DW_FORM_block2:
21460 case DW_FORM_block4:
21461 case DW_FORM_block:
21462 case DW_FORM_block1:
21463 fprintf_unfiltered (f, "block: size %s",
21464 pulongest (DW_BLOCK (&die->attrs[i])->size));
21465 break;
21466 case DW_FORM_exprloc:
21467 fprintf_unfiltered (f, "expression: size %s",
21468 pulongest (DW_BLOCK (&die->attrs[i])->size));
21469 break;
21470 case DW_FORM_data16:
21471 fprintf_unfiltered (f, "constant of 16 bytes");
21472 break;
21473 case DW_FORM_ref_addr:
21474 fprintf_unfiltered (f, "ref address: ");
21475 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21476 break;
21477 case DW_FORM_GNU_ref_alt:
21478 fprintf_unfiltered (f, "alt ref address: ");
21479 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21480 break;
21481 case DW_FORM_ref1:
21482 case DW_FORM_ref2:
21483 case DW_FORM_ref4:
21484 case DW_FORM_ref8:
21485 case DW_FORM_ref_udata:
21486 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
21487 (long) (DW_UNSND (&die->attrs[i])));
21488 break;
21489 case DW_FORM_data1:
21490 case DW_FORM_data2:
21491 case DW_FORM_data4:
21492 case DW_FORM_data8:
21493 case DW_FORM_udata:
21494 case DW_FORM_sdata:
21495 fprintf_unfiltered (f, "constant: %s",
21496 pulongest (DW_UNSND (&die->attrs[i])));
21497 break;
21498 case DW_FORM_sec_offset:
21499 fprintf_unfiltered (f, "section offset: %s",
21500 pulongest (DW_UNSND (&die->attrs[i])));
21501 break;
21502 case DW_FORM_ref_sig8:
21503 fprintf_unfiltered (f, "signature: %s",
21504 hex_string (DW_SIGNATURE (&die->attrs[i])));
21505 break;
21506 case DW_FORM_string:
21507 case DW_FORM_strp:
21508 case DW_FORM_line_strp:
21509 case DW_FORM_strx:
21510 case DW_FORM_GNU_str_index:
21511 case DW_FORM_GNU_strp_alt:
21512 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
21513 DW_STRING (&die->attrs[i])
21514 ? DW_STRING (&die->attrs[i]) : "",
21515 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
21516 break;
21517 case DW_FORM_flag:
21518 if (DW_UNSND (&die->attrs[i]))
21519 fprintf_unfiltered (f, "flag: TRUE");
21520 else
21521 fprintf_unfiltered (f, "flag: FALSE");
21522 break;
21523 case DW_FORM_flag_present:
21524 fprintf_unfiltered (f, "flag: TRUE");
21525 break;
21526 case DW_FORM_indirect:
21527 /* The reader will have reduced the indirect form to
21528 the "base form" so this form should not occur. */
21529 fprintf_unfiltered (f,
21530 "unexpected attribute form: DW_FORM_indirect");
21531 break;
21532 case DW_FORM_implicit_const:
21533 fprintf_unfiltered (f, "constant: %s",
21534 plongest (DW_SND (&die->attrs[i])));
21535 break;
21536 default:
21537 fprintf_unfiltered (f, "unsupported attribute form: %d.",
21538 die->attrs[i].form);
21539 break;
21540 }
21541 fprintf_unfiltered (f, "\n");
21542 }
21543 }
21544
21545 static void
21546 dump_die_for_error (struct die_info *die)
21547 {
21548 dump_die_shallow (gdb_stderr, 0, die);
21549 }
21550
21551 static void
21552 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21553 {
21554 int indent = level * 4;
21555
21556 gdb_assert (die != NULL);
21557
21558 if (level >= max_level)
21559 return;
21560
21561 dump_die_shallow (f, indent, die);
21562
21563 if (die->child != NULL)
21564 {
21565 print_spaces (indent, f);
21566 fprintf_unfiltered (f, " Children:");
21567 if (level + 1 < max_level)
21568 {
21569 fprintf_unfiltered (f, "\n");
21570 dump_die_1 (f, level + 1, max_level, die->child);
21571 }
21572 else
21573 {
21574 fprintf_unfiltered (f,
21575 " [not printed, max nesting level reached]\n");
21576 }
21577 }
21578
21579 if (die->sibling != NULL && level > 0)
21580 {
21581 dump_die_1 (f, level, max_level, die->sibling);
21582 }
21583 }
21584
21585 /* This is called from the pdie macro in gdbinit.in.
21586 It's not static so gcc will keep a copy callable from gdb. */
21587
21588 void
21589 dump_die (struct die_info *die, int max_level)
21590 {
21591 dump_die_1 (gdb_stdlog, 0, max_level, die);
21592 }
21593
21594 static void
21595 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
21596 {
21597 void **slot;
21598
21599 slot = htab_find_slot_with_hash (cu->die_hash, die,
21600 to_underlying (die->sect_off),
21601 INSERT);
21602
21603 *slot = die;
21604 }
21605
21606 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
21607 required kind. */
21608
21609 static sect_offset
21610 dwarf2_get_ref_die_offset (const struct attribute *attr)
21611 {
21612 if (attr->form_is_ref ())
21613 return (sect_offset) DW_UNSND (attr);
21614
21615 complaint (_("unsupported die ref attribute form: '%s'"),
21616 dwarf_form_name (attr->form));
21617 return {};
21618 }
21619
21620 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
21621 * the value held by the attribute is not constant. */
21622
21623 static LONGEST
21624 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
21625 {
21626 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
21627 return DW_SND (attr);
21628 else if (attr->form == DW_FORM_udata
21629 || attr->form == DW_FORM_data1
21630 || attr->form == DW_FORM_data2
21631 || attr->form == DW_FORM_data4
21632 || attr->form == DW_FORM_data8)
21633 return DW_UNSND (attr);
21634 else
21635 {
21636 /* For DW_FORM_data16 see attribute::form_is_constant. */
21637 complaint (_("Attribute value is not a constant (%s)"),
21638 dwarf_form_name (attr->form));
21639 return default_value;
21640 }
21641 }
21642
21643 /* Follow reference or signature attribute ATTR of SRC_DIE.
21644 On entry *REF_CU is the CU of SRC_DIE.
21645 On exit *REF_CU is the CU of the result. */
21646
21647 static struct die_info *
21648 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
21649 struct dwarf2_cu **ref_cu)
21650 {
21651 struct die_info *die;
21652
21653 if (attr->form_is_ref ())
21654 die = follow_die_ref (src_die, attr, ref_cu);
21655 else if (attr->form == DW_FORM_ref_sig8)
21656 die = follow_die_sig (src_die, attr, ref_cu);
21657 else
21658 {
21659 dump_die_for_error (src_die);
21660 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
21661 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
21662 }
21663
21664 return die;
21665 }
21666
21667 /* Follow reference OFFSET.
21668 On entry *REF_CU is the CU of the source die referencing OFFSET.
21669 On exit *REF_CU is the CU of the result.
21670 Returns NULL if OFFSET is invalid. */
21671
21672 static struct die_info *
21673 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
21674 struct dwarf2_cu **ref_cu)
21675 {
21676 struct die_info temp_die;
21677 struct dwarf2_cu *target_cu, *cu = *ref_cu;
21678 struct dwarf2_per_objfile *dwarf2_per_objfile
21679 = cu->per_cu->dwarf2_per_objfile;
21680
21681 gdb_assert (cu->per_cu != NULL);
21682
21683 target_cu = cu;
21684
21685 if (cu->per_cu->is_debug_types)
21686 {
21687 /* .debug_types CUs cannot reference anything outside their CU.
21688 If they need to, they have to reference a signatured type via
21689 DW_FORM_ref_sig8. */
21690 if (!cu->header.offset_in_cu_p (sect_off))
21691 return NULL;
21692 }
21693 else if (offset_in_dwz != cu->per_cu->is_dwz
21694 || !cu->header.offset_in_cu_p (sect_off))
21695 {
21696 struct dwarf2_per_cu_data *per_cu;
21697
21698 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
21699 dwarf2_per_objfile);
21700
21701 /* If necessary, add it to the queue and load its DIEs. */
21702 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
21703 load_full_comp_unit (per_cu, false, cu->language);
21704
21705 target_cu = per_cu->cu;
21706 }
21707 else if (cu->dies == NULL)
21708 {
21709 /* We're loading full DIEs during partial symbol reading. */
21710 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
21711 load_full_comp_unit (cu->per_cu, false, language_minimal);
21712 }
21713
21714 *ref_cu = target_cu;
21715 temp_die.sect_off = sect_off;
21716
21717 if (target_cu != cu)
21718 target_cu->ancestor = cu;
21719
21720 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
21721 &temp_die,
21722 to_underlying (sect_off));
21723 }
21724
21725 /* Follow reference attribute ATTR of SRC_DIE.
21726 On entry *REF_CU is the CU of SRC_DIE.
21727 On exit *REF_CU is the CU of the result. */
21728
21729 static struct die_info *
21730 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
21731 struct dwarf2_cu **ref_cu)
21732 {
21733 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
21734 struct dwarf2_cu *cu = *ref_cu;
21735 struct die_info *die;
21736
21737 die = follow_die_offset (sect_off,
21738 (attr->form == DW_FORM_GNU_ref_alt
21739 || cu->per_cu->is_dwz),
21740 ref_cu);
21741 if (!die)
21742 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21743 "at %s [in module %s]"),
21744 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
21745 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
21746
21747 return die;
21748 }
21749
21750 /* See read.h. */
21751
21752 struct dwarf2_locexpr_baton
21753 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
21754 dwarf2_per_cu_data *per_cu,
21755 CORE_ADDR (*get_frame_pc) (void *baton),
21756 void *baton, bool resolve_abstract_p)
21757 {
21758 struct dwarf2_cu *cu;
21759 struct die_info *die;
21760 struct attribute *attr;
21761 struct dwarf2_locexpr_baton retval;
21762 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21763 struct objfile *objfile = dwarf2_per_objfile->objfile;
21764
21765 if (per_cu->cu == NULL)
21766 load_cu (per_cu, false);
21767 cu = per_cu->cu;
21768 if (cu == NULL)
21769 {
21770 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21771 Instead just throw an error, not much else we can do. */
21772 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21773 sect_offset_str (sect_off), objfile_name (objfile));
21774 }
21775
21776 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21777 if (!die)
21778 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21779 sect_offset_str (sect_off), objfile_name (objfile));
21780
21781 attr = dwarf2_attr (die, DW_AT_location, cu);
21782 if (!attr && resolve_abstract_p
21783 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
21784 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21785 {
21786 CORE_ADDR pc = (*get_frame_pc) (baton);
21787 CORE_ADDR baseaddr = objfile->text_section_offset ();
21788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
21789
21790 for (const auto &cand_off
21791 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
21792 {
21793 struct dwarf2_cu *cand_cu = cu;
21794 struct die_info *cand
21795 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21796 if (!cand
21797 || !cand->parent
21798 || cand->parent->tag != DW_TAG_subprogram)
21799 continue;
21800
21801 CORE_ADDR pc_low, pc_high;
21802 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
21803 if (pc_low == ((CORE_ADDR) -1))
21804 continue;
21805 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21806 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21807 if (!(pc_low <= pc && pc < pc_high))
21808 continue;
21809
21810 die = cand;
21811 attr = dwarf2_attr (die, DW_AT_location, cu);
21812 break;
21813 }
21814 }
21815
21816 if (!attr)
21817 {
21818 /* DWARF: "If there is no such attribute, then there is no effect.".
21819 DATA is ignored if SIZE is 0. */
21820
21821 retval.data = NULL;
21822 retval.size = 0;
21823 }
21824 else if (attr->form_is_section_offset ())
21825 {
21826 struct dwarf2_loclist_baton loclist_baton;
21827 CORE_ADDR pc = (*get_frame_pc) (baton);
21828 size_t size;
21829
21830 fill_in_loclist_baton (cu, &loclist_baton, attr);
21831
21832 retval.data = dwarf2_find_location_expression (&loclist_baton,
21833 &size, pc);
21834 retval.size = size;
21835 }
21836 else
21837 {
21838 if (!attr->form_is_block ())
21839 error (_("Dwarf Error: DIE at %s referenced in module %s "
21840 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
21841 sect_offset_str (sect_off), objfile_name (objfile));
21842
21843 retval.data = DW_BLOCK (attr)->data;
21844 retval.size = DW_BLOCK (attr)->size;
21845 }
21846 retval.per_cu = cu->per_cu;
21847
21848 age_cached_comp_units (dwarf2_per_objfile);
21849
21850 return retval;
21851 }
21852
21853 /* See read.h. */
21854
21855 struct dwarf2_locexpr_baton
21856 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
21857 dwarf2_per_cu_data *per_cu,
21858 CORE_ADDR (*get_frame_pc) (void *baton),
21859 void *baton)
21860 {
21861 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
21862
21863 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
21864 }
21865
21866 /* Write a constant of a given type as target-ordered bytes into
21867 OBSTACK. */
21868
21869 static const gdb_byte *
21870 write_constant_as_bytes (struct obstack *obstack,
21871 enum bfd_endian byte_order,
21872 struct type *type,
21873 ULONGEST value,
21874 LONGEST *len)
21875 {
21876 gdb_byte *result;
21877
21878 *len = TYPE_LENGTH (type);
21879 result = (gdb_byte *) obstack_alloc (obstack, *len);
21880 store_unsigned_integer (result, *len, byte_order, value);
21881
21882 return result;
21883 }
21884
21885 /* See read.h. */
21886
21887 const gdb_byte *
21888 dwarf2_fetch_constant_bytes (sect_offset sect_off,
21889 dwarf2_per_cu_data *per_cu,
21890 obstack *obstack,
21891 LONGEST *len)
21892 {
21893 struct dwarf2_cu *cu;
21894 struct die_info *die;
21895 struct attribute *attr;
21896 const gdb_byte *result = NULL;
21897 struct type *type;
21898 LONGEST value;
21899 enum bfd_endian byte_order;
21900 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
21901
21902 if (per_cu->cu == NULL)
21903 load_cu (per_cu, false);
21904 cu = per_cu->cu;
21905 if (cu == NULL)
21906 {
21907 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21908 Instead just throw an error, not much else we can do. */
21909 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21910 sect_offset_str (sect_off), objfile_name (objfile));
21911 }
21912
21913 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
21914 if (!die)
21915 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21916 sect_offset_str (sect_off), objfile_name (objfile));
21917
21918 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21919 if (attr == NULL)
21920 return NULL;
21921
21922 byte_order = (bfd_big_endian (objfile->obfd)
21923 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21924
21925 switch (attr->form)
21926 {
21927 case DW_FORM_addr:
21928 case DW_FORM_addrx:
21929 case DW_FORM_GNU_addr_index:
21930 {
21931 gdb_byte *tem;
21932
21933 *len = cu->header.addr_size;
21934 tem = (gdb_byte *) obstack_alloc (obstack, *len);
21935 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21936 result = tem;
21937 }
21938 break;
21939 case DW_FORM_string:
21940 case DW_FORM_strp:
21941 case DW_FORM_strx:
21942 case DW_FORM_GNU_str_index:
21943 case DW_FORM_GNU_strp_alt:
21944 /* DW_STRING is already allocated on the objfile obstack, point
21945 directly to it. */
21946 result = (const gdb_byte *) DW_STRING (attr);
21947 *len = strlen (DW_STRING (attr));
21948 break;
21949 case DW_FORM_block1:
21950 case DW_FORM_block2:
21951 case DW_FORM_block4:
21952 case DW_FORM_block:
21953 case DW_FORM_exprloc:
21954 case DW_FORM_data16:
21955 result = DW_BLOCK (attr)->data;
21956 *len = DW_BLOCK (attr)->size;
21957 break;
21958
21959 /* The DW_AT_const_value attributes are supposed to carry the
21960 symbol's value "represented as it would be on the target
21961 architecture." By the time we get here, it's already been
21962 converted to host endianness, so we just need to sign- or
21963 zero-extend it as appropriate. */
21964 case DW_FORM_data1:
21965 type = die_type (die, cu);
21966 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21967 if (result == NULL)
21968 result = write_constant_as_bytes (obstack, byte_order,
21969 type, value, len);
21970 break;
21971 case DW_FORM_data2:
21972 type = die_type (die, cu);
21973 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21974 if (result == NULL)
21975 result = write_constant_as_bytes (obstack, byte_order,
21976 type, value, len);
21977 break;
21978 case DW_FORM_data4:
21979 type = die_type (die, cu);
21980 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21981 if (result == NULL)
21982 result = write_constant_as_bytes (obstack, byte_order,
21983 type, value, len);
21984 break;
21985 case DW_FORM_data8:
21986 type = die_type (die, cu);
21987 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21988 if (result == NULL)
21989 result = write_constant_as_bytes (obstack, byte_order,
21990 type, value, len);
21991 break;
21992
21993 case DW_FORM_sdata:
21994 case DW_FORM_implicit_const:
21995 type = die_type (die, cu);
21996 result = write_constant_as_bytes (obstack, byte_order,
21997 type, DW_SND (attr), len);
21998 break;
21999
22000 case DW_FORM_udata:
22001 type = die_type (die, cu);
22002 result = write_constant_as_bytes (obstack, byte_order,
22003 type, DW_UNSND (attr), len);
22004 break;
22005
22006 default:
22007 complaint (_("unsupported const value attribute form: '%s'"),
22008 dwarf_form_name (attr->form));
22009 break;
22010 }
22011
22012 return result;
22013 }
22014
22015 /* See read.h. */
22016
22017 struct type *
22018 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22019 dwarf2_per_cu_data *per_cu)
22020 {
22021 struct dwarf2_cu *cu;
22022 struct die_info *die;
22023
22024 if (per_cu->cu == NULL)
22025 load_cu (per_cu, false);
22026 cu = per_cu->cu;
22027 if (!cu)
22028 return NULL;
22029
22030 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22031 if (!die)
22032 return NULL;
22033
22034 return die_type (die, cu);
22035 }
22036
22037 /* See read.h. */
22038
22039 struct type *
22040 dwarf2_get_die_type (cu_offset die_offset,
22041 struct dwarf2_per_cu_data *per_cu)
22042 {
22043 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22044 return get_die_type_at_offset (die_offset_sect, per_cu);
22045 }
22046
22047 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22048 On entry *REF_CU is the CU of SRC_DIE.
22049 On exit *REF_CU is the CU of the result.
22050 Returns NULL if the referenced DIE isn't found. */
22051
22052 static struct die_info *
22053 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22054 struct dwarf2_cu **ref_cu)
22055 {
22056 struct die_info temp_die;
22057 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22058 struct die_info *die;
22059
22060 /* While it might be nice to assert sig_type->type == NULL here,
22061 we can get here for DW_AT_imported_declaration where we need
22062 the DIE not the type. */
22063
22064 /* If necessary, add it to the queue and load its DIEs. */
22065
22066 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22067 read_signatured_type (sig_type);
22068
22069 sig_cu = sig_type->per_cu.cu;
22070 gdb_assert (sig_cu != NULL);
22071 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22072 temp_die.sect_off = sig_type->type_offset_in_section;
22073 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22074 to_underlying (temp_die.sect_off));
22075 if (die)
22076 {
22077 struct dwarf2_per_objfile *dwarf2_per_objfile
22078 = (*ref_cu)->per_cu->dwarf2_per_objfile;
22079
22080 /* For .gdb_index version 7 keep track of included TUs.
22081 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22082 if (dwarf2_per_objfile->index_table != NULL
22083 && dwarf2_per_objfile->index_table->version <= 7)
22084 {
22085 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22086 }
22087
22088 *ref_cu = sig_cu;
22089 if (sig_cu != cu)
22090 sig_cu->ancestor = cu;
22091
22092 return die;
22093 }
22094
22095 return NULL;
22096 }
22097
22098 /* Follow signatured type referenced by ATTR in SRC_DIE.
22099 On entry *REF_CU is the CU of SRC_DIE.
22100 On exit *REF_CU is the CU of the result.
22101 The result is the DIE of the type.
22102 If the referenced type cannot be found an error is thrown. */
22103
22104 static struct die_info *
22105 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22106 struct dwarf2_cu **ref_cu)
22107 {
22108 ULONGEST signature = DW_SIGNATURE (attr);
22109 struct signatured_type *sig_type;
22110 struct die_info *die;
22111
22112 gdb_assert (attr->form == DW_FORM_ref_sig8);
22113
22114 sig_type = lookup_signatured_type (*ref_cu, signature);
22115 /* sig_type will be NULL if the signatured type is missing from
22116 the debug info. */
22117 if (sig_type == NULL)
22118 {
22119 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22120 " from DIE at %s [in module %s]"),
22121 hex_string (signature), sect_offset_str (src_die->sect_off),
22122 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22123 }
22124
22125 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22126 if (die == NULL)
22127 {
22128 dump_die_for_error (src_die);
22129 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22130 " from DIE at %s [in module %s]"),
22131 hex_string (signature), sect_offset_str (src_die->sect_off),
22132 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
22133 }
22134
22135 return die;
22136 }
22137
22138 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22139 reading in and processing the type unit if necessary. */
22140
22141 static struct type *
22142 get_signatured_type (struct die_info *die, ULONGEST signature,
22143 struct dwarf2_cu *cu)
22144 {
22145 struct dwarf2_per_objfile *dwarf2_per_objfile
22146 = cu->per_cu->dwarf2_per_objfile;
22147 struct signatured_type *sig_type;
22148 struct dwarf2_cu *type_cu;
22149 struct die_info *type_die;
22150 struct type *type;
22151
22152 sig_type = lookup_signatured_type (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 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22158 " from DIE at %s [in module %s]"),
22159 hex_string (signature), sect_offset_str (die->sect_off),
22160 objfile_name (dwarf2_per_objfile->objfile));
22161 return build_error_marker_type (cu, die);
22162 }
22163
22164 /* If we already know the type we're done. */
22165 if (sig_type->type != NULL)
22166 return sig_type->type;
22167
22168 type_cu = cu;
22169 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22170 if (type_die != NULL)
22171 {
22172 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22173 is created. This is important, for example, because for c++ classes
22174 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22175 type = read_type_die (type_die, type_cu);
22176 if (type == NULL)
22177 {
22178 complaint (_("Dwarf Error: Cannot build signatured type %s"
22179 " referenced from DIE at %s [in module %s]"),
22180 hex_string (signature), sect_offset_str (die->sect_off),
22181 objfile_name (dwarf2_per_objfile->objfile));
22182 type = build_error_marker_type (cu, die);
22183 }
22184 }
22185 else
22186 {
22187 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22188 " from DIE at %s [in module %s]"),
22189 hex_string (signature), sect_offset_str (die->sect_off),
22190 objfile_name (dwarf2_per_objfile->objfile));
22191 type = build_error_marker_type (cu, die);
22192 }
22193 sig_type->type = type;
22194
22195 return type;
22196 }
22197
22198 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22199 reading in and processing the type unit if necessary. */
22200
22201 static struct type *
22202 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22203 struct dwarf2_cu *cu) /* ARI: editCase function */
22204 {
22205 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22206 if (attr->form_is_ref ())
22207 {
22208 struct dwarf2_cu *type_cu = cu;
22209 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22210
22211 return read_type_die (type_die, type_cu);
22212 }
22213 else if (attr->form == DW_FORM_ref_sig8)
22214 {
22215 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22216 }
22217 else
22218 {
22219 struct dwarf2_per_objfile *dwarf2_per_objfile
22220 = cu->per_cu->dwarf2_per_objfile;
22221
22222 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22223 " at %s [in module %s]"),
22224 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22225 objfile_name (dwarf2_per_objfile->objfile));
22226 return build_error_marker_type (cu, die);
22227 }
22228 }
22229
22230 /* Load the DIEs associated with type unit PER_CU into memory. */
22231
22232 static void
22233 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
22234 {
22235 struct signatured_type *sig_type;
22236
22237 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22238 gdb_assert (! per_cu->type_unit_group_p ());
22239
22240 /* We have the per_cu, but we need the signatured_type.
22241 Fortunately this is an easy translation. */
22242 gdb_assert (per_cu->is_debug_types);
22243 sig_type = (struct signatured_type *) per_cu;
22244
22245 gdb_assert (per_cu->cu == NULL);
22246
22247 read_signatured_type (sig_type);
22248
22249 gdb_assert (per_cu->cu != NULL);
22250 }
22251
22252 /* Read in a signatured type and build its CU and DIEs.
22253 If the type is a stub for the real type in a DWO file,
22254 read in the real type from the DWO file as well. */
22255
22256 static void
22257 read_signatured_type (struct signatured_type *sig_type)
22258 {
22259 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22260
22261 gdb_assert (per_cu->is_debug_types);
22262 gdb_assert (per_cu->cu == NULL);
22263
22264 cutu_reader reader (per_cu, NULL, 0, false);
22265
22266 if (!reader.dummy_p)
22267 {
22268 struct dwarf2_cu *cu = reader.cu;
22269 const gdb_byte *info_ptr = reader.info_ptr;
22270
22271 gdb_assert (cu->die_hash == NULL);
22272 cu->die_hash =
22273 htab_create_alloc_ex (cu->header.length / 12,
22274 die_hash,
22275 die_eq,
22276 NULL,
22277 &cu->comp_unit_obstack,
22278 hashtab_obstack_allocate,
22279 dummy_obstack_deallocate);
22280
22281 if (reader.comp_unit_die->has_children)
22282 reader.comp_unit_die->child
22283 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22284 reader.comp_unit_die);
22285 cu->dies = reader.comp_unit_die;
22286 /* comp_unit_die is not stored in die_hash, no need. */
22287
22288 /* We try not to read any attributes in this function, because
22289 not all CUs needed for references have been loaded yet, and
22290 symbol table processing isn't initialized. But we have to
22291 set the CU language, or we won't be able to build types
22292 correctly. Similarly, if we do not read the producer, we can
22293 not apply producer-specific interpretation. */
22294 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22295
22296 reader.keep ();
22297 }
22298
22299 sig_type->per_cu.tu_read = 1;
22300 }
22301
22302 /* Decode simple location descriptions.
22303 Given a pointer to a dwarf block that defines a location, compute
22304 the location and return the value.
22305
22306 NOTE drow/2003-11-18: This function is called in two situations
22307 now: for the address of static or global variables (partial symbols
22308 only) and for offsets into structures which are expected to be
22309 (more or less) constant. The partial symbol case should go away,
22310 and only the constant case should remain. That will let this
22311 function complain more accurately. A few special modes are allowed
22312 without complaint for global variables (for instance, global
22313 register values and thread-local values).
22314
22315 A location description containing no operations indicates that the
22316 object is optimized out. The return value is 0 for that case.
22317 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22318 callers will only want a very basic result and this can become a
22319 complaint.
22320
22321 Note that stack[0] is unused except as a default error return. */
22322
22323 static CORE_ADDR
22324 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
22325 {
22326 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
22327 size_t i;
22328 size_t size = blk->size;
22329 const gdb_byte *data = blk->data;
22330 CORE_ADDR stack[64];
22331 int stacki;
22332 unsigned int bytes_read, unsnd;
22333 gdb_byte op;
22334
22335 i = 0;
22336 stacki = 0;
22337 stack[stacki] = 0;
22338 stack[++stacki] = 0;
22339
22340 while (i < size)
22341 {
22342 op = data[i++];
22343 switch (op)
22344 {
22345 case DW_OP_lit0:
22346 case DW_OP_lit1:
22347 case DW_OP_lit2:
22348 case DW_OP_lit3:
22349 case DW_OP_lit4:
22350 case DW_OP_lit5:
22351 case DW_OP_lit6:
22352 case DW_OP_lit7:
22353 case DW_OP_lit8:
22354 case DW_OP_lit9:
22355 case DW_OP_lit10:
22356 case DW_OP_lit11:
22357 case DW_OP_lit12:
22358 case DW_OP_lit13:
22359 case DW_OP_lit14:
22360 case DW_OP_lit15:
22361 case DW_OP_lit16:
22362 case DW_OP_lit17:
22363 case DW_OP_lit18:
22364 case DW_OP_lit19:
22365 case DW_OP_lit20:
22366 case DW_OP_lit21:
22367 case DW_OP_lit22:
22368 case DW_OP_lit23:
22369 case DW_OP_lit24:
22370 case DW_OP_lit25:
22371 case DW_OP_lit26:
22372 case DW_OP_lit27:
22373 case DW_OP_lit28:
22374 case DW_OP_lit29:
22375 case DW_OP_lit30:
22376 case DW_OP_lit31:
22377 stack[++stacki] = op - DW_OP_lit0;
22378 break;
22379
22380 case DW_OP_reg0:
22381 case DW_OP_reg1:
22382 case DW_OP_reg2:
22383 case DW_OP_reg3:
22384 case DW_OP_reg4:
22385 case DW_OP_reg5:
22386 case DW_OP_reg6:
22387 case DW_OP_reg7:
22388 case DW_OP_reg8:
22389 case DW_OP_reg9:
22390 case DW_OP_reg10:
22391 case DW_OP_reg11:
22392 case DW_OP_reg12:
22393 case DW_OP_reg13:
22394 case DW_OP_reg14:
22395 case DW_OP_reg15:
22396 case DW_OP_reg16:
22397 case DW_OP_reg17:
22398 case DW_OP_reg18:
22399 case DW_OP_reg19:
22400 case DW_OP_reg20:
22401 case DW_OP_reg21:
22402 case DW_OP_reg22:
22403 case DW_OP_reg23:
22404 case DW_OP_reg24:
22405 case DW_OP_reg25:
22406 case DW_OP_reg26:
22407 case DW_OP_reg27:
22408 case DW_OP_reg28:
22409 case DW_OP_reg29:
22410 case DW_OP_reg30:
22411 case DW_OP_reg31:
22412 stack[++stacki] = op - DW_OP_reg0;
22413 if (i < size)
22414 dwarf2_complex_location_expr_complaint ();
22415 break;
22416
22417 case DW_OP_regx:
22418 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22419 i += bytes_read;
22420 stack[++stacki] = unsnd;
22421 if (i < size)
22422 dwarf2_complex_location_expr_complaint ();
22423 break;
22424
22425 case DW_OP_addr:
22426 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22427 &bytes_read);
22428 i += bytes_read;
22429 break;
22430
22431 case DW_OP_const1u:
22432 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22433 i += 1;
22434 break;
22435
22436 case DW_OP_const1s:
22437 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22438 i += 1;
22439 break;
22440
22441 case DW_OP_const2u:
22442 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22443 i += 2;
22444 break;
22445
22446 case DW_OP_const2s:
22447 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22448 i += 2;
22449 break;
22450
22451 case DW_OP_const4u:
22452 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22453 i += 4;
22454 break;
22455
22456 case DW_OP_const4s:
22457 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22458 i += 4;
22459 break;
22460
22461 case DW_OP_const8u:
22462 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22463 i += 8;
22464 break;
22465
22466 case DW_OP_constu:
22467 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22468 &bytes_read);
22469 i += bytes_read;
22470 break;
22471
22472 case DW_OP_consts:
22473 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22474 i += bytes_read;
22475 break;
22476
22477 case DW_OP_dup:
22478 stack[stacki + 1] = stack[stacki];
22479 stacki++;
22480 break;
22481
22482 case DW_OP_plus:
22483 stack[stacki - 1] += stack[stacki];
22484 stacki--;
22485 break;
22486
22487 case DW_OP_plus_uconst:
22488 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22489 &bytes_read);
22490 i += bytes_read;
22491 break;
22492
22493 case DW_OP_minus:
22494 stack[stacki - 1] -= stack[stacki];
22495 stacki--;
22496 break;
22497
22498 case DW_OP_deref:
22499 /* If we're not the last op, then we definitely can't encode
22500 this using GDB's address_class enum. This is valid for partial
22501 global symbols, although the variable's address will be bogus
22502 in the psymtab. */
22503 if (i < size)
22504 dwarf2_complex_location_expr_complaint ();
22505 break;
22506
22507 case DW_OP_GNU_push_tls_address:
22508 case DW_OP_form_tls_address:
22509 /* The top of the stack has the offset from the beginning
22510 of the thread control block at which the variable is located. */
22511 /* Nothing should follow this operator, so the top of stack would
22512 be returned. */
22513 /* This is valid for partial global symbols, but the variable's
22514 address will be bogus in the psymtab. Make it always at least
22515 non-zero to not look as a variable garbage collected by linker
22516 which have DW_OP_addr 0. */
22517 if (i < size)
22518 dwarf2_complex_location_expr_complaint ();
22519 stack[stacki]++;
22520 break;
22521
22522 case DW_OP_GNU_uninit:
22523 break;
22524
22525 case DW_OP_addrx:
22526 case DW_OP_GNU_addr_index:
22527 case DW_OP_GNU_const_index:
22528 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22529 &bytes_read);
22530 i += bytes_read;
22531 break;
22532
22533 default:
22534 {
22535 const char *name = get_DW_OP_name (op);
22536
22537 if (name)
22538 complaint (_("unsupported stack op: '%s'"),
22539 name);
22540 else
22541 complaint (_("unsupported stack op: '%02x'"),
22542 op);
22543 }
22544
22545 return (stack[stacki]);
22546 }
22547
22548 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22549 outside of the allocated space. Also enforce minimum>0. */
22550 if (stacki >= ARRAY_SIZE (stack) - 1)
22551 {
22552 complaint (_("location description stack overflow"));
22553 return 0;
22554 }
22555
22556 if (stacki <= 0)
22557 {
22558 complaint (_("location description stack underflow"));
22559 return 0;
22560 }
22561 }
22562 return (stack[stacki]);
22563 }
22564
22565 /* memory allocation interface */
22566
22567 static struct dwarf_block *
22568 dwarf_alloc_block (struct dwarf2_cu *cu)
22569 {
22570 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
22571 }
22572
22573 static struct die_info *
22574 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
22575 {
22576 struct die_info *die;
22577 size_t size = sizeof (struct die_info);
22578
22579 if (num_attrs > 1)
22580 size += (num_attrs - 1) * sizeof (struct attribute);
22581
22582 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
22583 memset (die, 0, sizeof (struct die_info));
22584 return (die);
22585 }
22586
22587 \f
22588
22589 /* Macro support. */
22590
22591 /* An overload of dwarf_decode_macros that finds the correct section
22592 and ensures it is read in before calling the other overload. */
22593
22594 static void
22595 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22596 int section_is_gnu)
22597 {
22598 struct dwarf2_per_objfile *dwarf2_per_objfile
22599 = cu->per_cu->dwarf2_per_objfile;
22600 struct objfile *objfile = dwarf2_per_objfile->objfile;
22601 const struct line_header *lh = cu->line_header;
22602 unsigned int offset_size = cu->header.offset_size;
22603 struct dwarf2_section_info *section;
22604 const char *section_name;
22605
22606 if (cu->dwo_unit != nullptr)
22607 {
22608 if (section_is_gnu)
22609 {
22610 section = &cu->dwo_unit->dwo_file->sections.macro;
22611 section_name = ".debug_macro.dwo";
22612 }
22613 else
22614 {
22615 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22616 section_name = ".debug_macinfo.dwo";
22617 }
22618 }
22619 else
22620 {
22621 if (section_is_gnu)
22622 {
22623 section = &dwarf2_per_objfile->macro;
22624 section_name = ".debug_macro";
22625 }
22626 else
22627 {
22628 section = &dwarf2_per_objfile->macinfo;
22629 section_name = ".debug_macinfo";
22630 }
22631 }
22632
22633 section->read (objfile);
22634 if (section->buffer == nullptr)
22635 {
22636 complaint (_("missing %s section"), section_name);
22637 return;
22638 }
22639
22640 buildsym_compunit *builder = cu->get_builder ();
22641
22642 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22643 offset_size, offset, section_is_gnu);
22644 }
22645
22646 /* Return the .debug_loc section to use for CU.
22647 For DWO files use .debug_loc.dwo. */
22648
22649 static struct dwarf2_section_info *
22650 cu_debug_loc_section (struct dwarf2_cu *cu)
22651 {
22652 struct dwarf2_per_objfile *dwarf2_per_objfile
22653 = cu->per_cu->dwarf2_per_objfile;
22654
22655 if (cu->dwo_unit)
22656 {
22657 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
22658
22659 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22660 }
22661 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22662 : &dwarf2_per_objfile->loc);
22663 }
22664
22665 /* A helper function that fills in a dwarf2_loclist_baton. */
22666
22667 static void
22668 fill_in_loclist_baton (struct dwarf2_cu *cu,
22669 struct dwarf2_loclist_baton *baton,
22670 const struct attribute *attr)
22671 {
22672 struct dwarf2_per_objfile *dwarf2_per_objfile
22673 = cu->per_cu->dwarf2_per_objfile;
22674 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22675
22676 section->read (dwarf2_per_objfile->objfile);
22677
22678 baton->per_cu = cu->per_cu;
22679 gdb_assert (baton->per_cu);
22680 /* We don't know how long the location list is, but make sure we
22681 don't run off the edge of the section. */
22682 baton->size = section->size - DW_UNSND (attr);
22683 baton->data = section->buffer + DW_UNSND (attr);
22684 if (cu->base_address.has_value ())
22685 baton->base_address = *cu->base_address;
22686 else
22687 baton->base_address = 0;
22688 baton->from_dwo = cu->dwo_unit != NULL;
22689 }
22690
22691 static void
22692 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
22693 struct dwarf2_cu *cu, int is_block)
22694 {
22695 struct dwarf2_per_objfile *dwarf2_per_objfile
22696 = cu->per_cu->dwarf2_per_objfile;
22697 struct objfile *objfile = dwarf2_per_objfile->objfile;
22698 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22699
22700 if (attr->form_is_section_offset ()
22701 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
22702 the section. If so, fall through to the complaint in the
22703 other branch. */
22704 && DW_UNSND (attr) < section->get_size (objfile))
22705 {
22706 struct dwarf2_loclist_baton *baton;
22707
22708 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
22709
22710 fill_in_loclist_baton (cu, baton, attr);
22711
22712 if (!cu->base_address.has_value ())
22713 complaint (_("Location list used without "
22714 "specifying the CU base address."));
22715
22716 SYMBOL_ACLASS_INDEX (sym) = (is_block
22717 ? dwarf2_loclist_block_index
22718 : dwarf2_loclist_index);
22719 SYMBOL_LOCATION_BATON (sym) = baton;
22720 }
22721 else
22722 {
22723 struct dwarf2_locexpr_baton *baton;
22724
22725 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
22726 baton->per_cu = cu->per_cu;
22727 gdb_assert (baton->per_cu);
22728
22729 if (attr->form_is_block ())
22730 {
22731 /* Note that we're just copying the block's data pointer
22732 here, not the actual data. We're still pointing into the
22733 info_buffer for SYM's objfile; right now we never release
22734 that buffer, but when we do clean up properly this may
22735 need to change. */
22736 baton->size = DW_BLOCK (attr)->size;
22737 baton->data = DW_BLOCK (attr)->data;
22738 }
22739 else
22740 {
22741 dwarf2_invalid_attrib_class_complaint ("location description",
22742 sym->natural_name ());
22743 baton->size = 0;
22744 }
22745
22746 SYMBOL_ACLASS_INDEX (sym) = (is_block
22747 ? dwarf2_locexpr_block_index
22748 : dwarf2_locexpr_index);
22749 SYMBOL_LOCATION_BATON (sym) = baton;
22750 }
22751 }
22752
22753 /* See read.h. */
22754
22755 struct objfile *
22756 dwarf2_per_cu_data::objfile () const
22757 {
22758 struct objfile *objfile = dwarf2_per_objfile->objfile;
22759
22760 /* Return the master objfile, so that we can report and look up the
22761 correct file containing this variable. */
22762 if (objfile->separate_debug_objfile_backlink)
22763 objfile = objfile->separate_debug_objfile_backlink;
22764
22765 return objfile;
22766 }
22767
22768 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22769 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22770 CU_HEADERP first. */
22771
22772 static const struct comp_unit_head *
22773 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
22774 const struct dwarf2_per_cu_data *per_cu)
22775 {
22776 const gdb_byte *info_ptr;
22777
22778 if (per_cu->cu)
22779 return &per_cu->cu->header;
22780
22781 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
22782
22783 memset (cu_headerp, 0, sizeof (*cu_headerp));
22784 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22785 rcuh_kind::COMPILE);
22786
22787 return cu_headerp;
22788 }
22789
22790 /* See read.h. */
22791
22792 int
22793 dwarf2_per_cu_data::addr_size () const
22794 {
22795 struct comp_unit_head cu_header_local;
22796 const struct comp_unit_head *cu_headerp;
22797
22798 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22799
22800 return cu_headerp->addr_size;
22801 }
22802
22803 /* See read.h. */
22804
22805 int
22806 dwarf2_per_cu_data::offset_size () const
22807 {
22808 struct comp_unit_head cu_header_local;
22809 const struct comp_unit_head *cu_headerp;
22810
22811 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22812
22813 return cu_headerp->offset_size;
22814 }
22815
22816 /* See read.h. */
22817
22818 int
22819 dwarf2_per_cu_data::ref_addr_size () const
22820 {
22821 struct comp_unit_head cu_header_local;
22822 const struct comp_unit_head *cu_headerp;
22823
22824 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
22825
22826 if (cu_headerp->version == 2)
22827 return cu_headerp->addr_size;
22828 else
22829 return cu_headerp->offset_size;
22830 }
22831
22832 /* See read.h. */
22833
22834 CORE_ADDR
22835 dwarf2_per_cu_data::text_offset () const
22836 {
22837 struct objfile *objfile = dwarf2_per_objfile->objfile;
22838
22839 return objfile->text_section_offset ();
22840 }
22841
22842 /* See read.h. */
22843
22844 struct type *
22845 dwarf2_per_cu_data::addr_type () const
22846 {
22847 struct objfile *objfile = dwarf2_per_objfile->objfile;
22848 struct type *void_type = objfile_type (objfile)->builtin_void;
22849 struct type *addr_type = lookup_pointer_type (void_type);
22850 int addr_size = this->addr_size ();
22851
22852 if (TYPE_LENGTH (addr_type) == addr_size)
22853 return addr_type;
22854
22855 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
22856 return addr_type;
22857 }
22858
22859 /* A helper function for dwarf2_find_containing_comp_unit that returns
22860 the index of the result, and that searches a vector. It will
22861 return a result even if the offset in question does not actually
22862 occur in any CU. This is separate so that it can be unit
22863 tested. */
22864
22865 static int
22866 dwarf2_find_containing_comp_unit
22867 (sect_offset sect_off,
22868 unsigned int offset_in_dwz,
22869 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
22870 {
22871 int low, high;
22872
22873 low = 0;
22874 high = all_comp_units.size () - 1;
22875 while (high > low)
22876 {
22877 struct dwarf2_per_cu_data *mid_cu;
22878 int mid = low + (high - low) / 2;
22879
22880 mid_cu = all_comp_units[mid];
22881 if (mid_cu->is_dwz > offset_in_dwz
22882 || (mid_cu->is_dwz == offset_in_dwz
22883 && mid_cu->sect_off + mid_cu->length > sect_off))
22884 high = mid;
22885 else
22886 low = mid + 1;
22887 }
22888 gdb_assert (low == high);
22889 return low;
22890 }
22891
22892 /* Locate the .debug_info compilation unit from CU's objfile which contains
22893 the DIE at OFFSET. Raises an error on failure. */
22894
22895 static struct dwarf2_per_cu_data *
22896 dwarf2_find_containing_comp_unit (sect_offset sect_off,
22897 unsigned int offset_in_dwz,
22898 struct dwarf2_per_objfile *dwarf2_per_objfile)
22899 {
22900 int low
22901 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22902 dwarf2_per_objfile->all_comp_units);
22903 struct dwarf2_per_cu_data *this_cu
22904 = dwarf2_per_objfile->all_comp_units[low];
22905
22906 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
22907 {
22908 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
22909 error (_("Dwarf Error: could not find partial DIE containing "
22910 "offset %s [in module %s]"),
22911 sect_offset_str (sect_off),
22912 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
22913
22914 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22915 <= sect_off);
22916 return dwarf2_per_objfile->all_comp_units[low-1];
22917 }
22918 else
22919 {
22920 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
22921 && sect_off >= this_cu->sect_off + this_cu->length)
22922 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
22923 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
22924 return this_cu;
22925 }
22926 }
22927
22928 #if GDB_SELF_TEST
22929
22930 namespace selftests {
22931 namespace find_containing_comp_unit {
22932
22933 static void
22934 run_test ()
22935 {
22936 struct dwarf2_per_cu_data one {};
22937 struct dwarf2_per_cu_data two {};
22938 struct dwarf2_per_cu_data three {};
22939 struct dwarf2_per_cu_data four {};
22940
22941 one.length = 5;
22942 two.sect_off = sect_offset (one.length);
22943 two.length = 7;
22944
22945 three.length = 5;
22946 three.is_dwz = 1;
22947 four.sect_off = sect_offset (three.length);
22948 four.length = 7;
22949 four.is_dwz = 1;
22950
22951 std::vector<dwarf2_per_cu_data *> units;
22952 units.push_back (&one);
22953 units.push_back (&two);
22954 units.push_back (&three);
22955 units.push_back (&four);
22956
22957 int result;
22958
22959 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22960 SELF_CHECK (units[result] == &one);
22961 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22962 SELF_CHECK (units[result] == &one);
22963 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22964 SELF_CHECK (units[result] == &two);
22965
22966 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22967 SELF_CHECK (units[result] == &three);
22968 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22969 SELF_CHECK (units[result] == &three);
22970 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22971 SELF_CHECK (units[result] == &four);
22972 }
22973
22974 }
22975 }
22976
22977 #endif /* GDB_SELF_TEST */
22978
22979 /* Initialize dwarf2_cu CU, owned by PER_CU. */
22980
22981 dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22982 : per_cu (per_cu_),
22983 mark (false),
22984 has_loclist (false),
22985 checked_producer (false),
22986 producer_is_gxx_lt_4_6 (false),
22987 producer_is_gcc_lt_4_3 (false),
22988 producer_is_icc (false),
22989 producer_is_icc_lt_14 (false),
22990 producer_is_codewarrior (false),
22991 processing_has_namespace_info (false)
22992 {
22993 per_cu->cu = this;
22994 }
22995
22996 /* Destroy a dwarf2_cu. */
22997
22998 dwarf2_cu::~dwarf2_cu ()
22999 {
23000 per_cu->cu = NULL;
23001 }
23002
23003 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23004
23005 static void
23006 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23007 enum language pretend_language)
23008 {
23009 struct attribute *attr;
23010
23011 /* Set the language we're debugging. */
23012 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23013 if (attr != nullptr)
23014 set_cu_language (DW_UNSND (attr), cu);
23015 else
23016 {
23017 cu->language = pretend_language;
23018 cu->language_defn = language_def (cu->language);
23019 }
23020
23021 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23022 }
23023
23024 /* Increase the age counter on each cached compilation unit, and free
23025 any that are too old. */
23026
23027 static void
23028 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23029 {
23030 struct dwarf2_per_cu_data *per_cu, **last_chain;
23031
23032 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23033 per_cu = dwarf2_per_objfile->read_in_chain;
23034 while (per_cu != NULL)
23035 {
23036 per_cu->cu->last_used ++;
23037 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23038 dwarf2_mark (per_cu->cu);
23039 per_cu = per_cu->cu->read_in_chain;
23040 }
23041
23042 per_cu = dwarf2_per_objfile->read_in_chain;
23043 last_chain = &dwarf2_per_objfile->read_in_chain;
23044 while (per_cu != NULL)
23045 {
23046 struct dwarf2_per_cu_data *next_cu;
23047
23048 next_cu = per_cu->cu->read_in_chain;
23049
23050 if (!per_cu->cu->mark)
23051 {
23052 delete per_cu->cu;
23053 *last_chain = next_cu;
23054 }
23055 else
23056 last_chain = &per_cu->cu->read_in_chain;
23057
23058 per_cu = next_cu;
23059 }
23060 }
23061
23062 /* Remove a single compilation unit from the cache. */
23063
23064 static void
23065 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23066 {
23067 struct dwarf2_per_cu_data *per_cu, **last_chain;
23068 struct dwarf2_per_objfile *dwarf2_per_objfile
23069 = target_per_cu->dwarf2_per_objfile;
23070
23071 per_cu = dwarf2_per_objfile->read_in_chain;
23072 last_chain = &dwarf2_per_objfile->read_in_chain;
23073 while (per_cu != NULL)
23074 {
23075 struct dwarf2_per_cu_data *next_cu;
23076
23077 next_cu = per_cu->cu->read_in_chain;
23078
23079 if (per_cu == target_per_cu)
23080 {
23081 delete per_cu->cu;
23082 per_cu->cu = NULL;
23083 *last_chain = next_cu;
23084 break;
23085 }
23086 else
23087 last_chain = &per_cu->cu->read_in_chain;
23088
23089 per_cu = next_cu;
23090 }
23091 }
23092
23093 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23094 We store these in a hash table separate from the DIEs, and preserve them
23095 when the DIEs are flushed out of cache.
23096
23097 The CU "per_cu" pointer is needed because offset alone is not enough to
23098 uniquely identify the type. A file may have multiple .debug_types sections,
23099 or the type may come from a DWO file. Furthermore, while it's more logical
23100 to use per_cu->section+offset, with Fission the section with the data is in
23101 the DWO file but we don't know that section at the point we need it.
23102 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23103 because we can enter the lookup routine, get_die_type_at_offset, from
23104 outside this file, and thus won't necessarily have PER_CU->cu.
23105 Fortunately, PER_CU is stable for the life of the objfile. */
23106
23107 struct dwarf2_per_cu_offset_and_type
23108 {
23109 const struct dwarf2_per_cu_data *per_cu;
23110 sect_offset sect_off;
23111 struct type *type;
23112 };
23113
23114 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23115
23116 static hashval_t
23117 per_cu_offset_and_type_hash (const void *item)
23118 {
23119 const struct dwarf2_per_cu_offset_and_type *ofs
23120 = (const struct dwarf2_per_cu_offset_and_type *) item;
23121
23122 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23123 }
23124
23125 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23126
23127 static int
23128 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23129 {
23130 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23131 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23132 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23133 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23134
23135 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23136 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23137 }
23138
23139 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23140 table if necessary. For convenience, return TYPE.
23141
23142 The DIEs reading must have careful ordering to:
23143 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23144 reading current DIE.
23145 * Not trying to dereference contents of still incompletely read in types
23146 while reading in other DIEs.
23147 * Enable referencing still incompletely read in types just by a pointer to
23148 the type without accessing its fields.
23149
23150 Therefore caller should follow these rules:
23151 * Try to fetch any prerequisite types we may need to build this DIE type
23152 before building the type and calling set_die_type.
23153 * After building type call set_die_type for current DIE as soon as
23154 possible before fetching more types to complete the current type.
23155 * Make the type as complete as possible before fetching more types. */
23156
23157 static struct type *
23158 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23159 {
23160 struct dwarf2_per_objfile *dwarf2_per_objfile
23161 = cu->per_cu->dwarf2_per_objfile;
23162 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23163 struct objfile *objfile = dwarf2_per_objfile->objfile;
23164 struct attribute *attr;
23165 struct dynamic_prop prop;
23166
23167 /* For Ada types, make sure that the gnat-specific data is always
23168 initialized (if not already set). There are a few types where
23169 we should not be doing so, because the type-specific area is
23170 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23171 where the type-specific area is used to store the floatformat).
23172 But this is not a problem, because the gnat-specific information
23173 is actually not needed for these types. */
23174 if (need_gnat_info (cu)
23175 && TYPE_CODE (type) != TYPE_CODE_FUNC
23176 && TYPE_CODE (type) != TYPE_CODE_FLT
23177 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23178 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23179 && TYPE_CODE (type) != TYPE_CODE_METHOD
23180 && !HAVE_GNAT_AUX_INFO (type))
23181 INIT_GNAT_SPECIFIC (type);
23182
23183 /* Read DW_AT_allocated and set in type. */
23184 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23185 if (attr != NULL && attr->form_is_block ())
23186 {
23187 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23188 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23189 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
23190 }
23191 else if (attr != NULL)
23192 {
23193 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23194 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23195 sect_offset_str (die->sect_off));
23196 }
23197
23198 /* Read DW_AT_associated and set in type. */
23199 attr = dwarf2_attr (die, DW_AT_associated, cu);
23200 if (attr != NULL && attr->form_is_block ())
23201 {
23202 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23203 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23204 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
23205 }
23206 else if (attr != NULL)
23207 {
23208 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23209 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23210 sect_offset_str (die->sect_off));
23211 }
23212
23213 /* Read DW_AT_data_location and set in type. */
23214 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23215 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23216 cu->per_cu->addr_type ()))
23217 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
23218
23219 if (dwarf2_per_objfile->die_type_hash == NULL)
23220 dwarf2_per_objfile->die_type_hash
23221 = htab_up (htab_create_alloc (127,
23222 per_cu_offset_and_type_hash,
23223 per_cu_offset_and_type_eq,
23224 NULL, xcalloc, xfree));
23225
23226 ofs.per_cu = cu->per_cu;
23227 ofs.sect_off = die->sect_off;
23228 ofs.type = type;
23229 slot = (struct dwarf2_per_cu_offset_and_type **)
23230 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23231 if (*slot)
23232 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23233 sect_offset_str (die->sect_off));
23234 *slot = XOBNEW (&objfile->objfile_obstack,
23235 struct dwarf2_per_cu_offset_and_type);
23236 **slot = ofs;
23237 return type;
23238 }
23239
23240 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23241 or return NULL if the die does not have a saved type. */
23242
23243 static struct type *
23244 get_die_type_at_offset (sect_offset sect_off,
23245 struct dwarf2_per_cu_data *per_cu)
23246 {
23247 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23248 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23249
23250 if (dwarf2_per_objfile->die_type_hash == NULL)
23251 return NULL;
23252
23253 ofs.per_cu = per_cu;
23254 ofs.sect_off = sect_off;
23255 slot = ((struct dwarf2_per_cu_offset_and_type *)
23256 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23257 if (slot)
23258 return slot->type;
23259 else
23260 return NULL;
23261 }
23262
23263 /* Look up the type for DIE in CU in die_type_hash,
23264 or return NULL if DIE does not have a saved type. */
23265
23266 static struct type *
23267 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23268 {
23269 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23270 }
23271
23272 /* Add a dependence relationship from CU to REF_PER_CU. */
23273
23274 static void
23275 dwarf2_add_dependence (struct dwarf2_cu *cu,
23276 struct dwarf2_per_cu_data *ref_per_cu)
23277 {
23278 void **slot;
23279
23280 if (cu->dependencies == NULL)
23281 cu->dependencies
23282 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23283 NULL, &cu->comp_unit_obstack,
23284 hashtab_obstack_allocate,
23285 dummy_obstack_deallocate);
23286
23287 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23288 if (*slot == NULL)
23289 *slot = ref_per_cu;
23290 }
23291
23292 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23293 Set the mark field in every compilation unit in the
23294 cache that we must keep because we are keeping CU. */
23295
23296 static int
23297 dwarf2_mark_helper (void **slot, void *data)
23298 {
23299 struct dwarf2_per_cu_data *per_cu;
23300
23301 per_cu = (struct dwarf2_per_cu_data *) *slot;
23302
23303 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23304 reading of the chain. As such dependencies remain valid it is not much
23305 useful to track and undo them during QUIT cleanups. */
23306 if (per_cu->cu == NULL)
23307 return 1;
23308
23309 if (per_cu->cu->mark)
23310 return 1;
23311 per_cu->cu->mark = true;
23312
23313 if (per_cu->cu->dependencies != NULL)
23314 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23315
23316 return 1;
23317 }
23318
23319 /* Set the mark field in CU and in every other compilation unit in the
23320 cache that we must keep because we are keeping CU. */
23321
23322 static void
23323 dwarf2_mark (struct dwarf2_cu *cu)
23324 {
23325 if (cu->mark)
23326 return;
23327 cu->mark = true;
23328 if (cu->dependencies != NULL)
23329 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23330 }
23331
23332 static void
23333 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23334 {
23335 while (per_cu)
23336 {
23337 per_cu->cu->mark = false;
23338 per_cu = per_cu->cu->read_in_chain;
23339 }
23340 }
23341
23342 /* Trivial hash function for partial_die_info: the hash value of a DIE
23343 is its offset in .debug_info for this objfile. */
23344
23345 static hashval_t
23346 partial_die_hash (const void *item)
23347 {
23348 const struct partial_die_info *part_die
23349 = (const struct partial_die_info *) item;
23350
23351 return to_underlying (part_die->sect_off);
23352 }
23353
23354 /* Trivial comparison function for partial_die_info structures: two DIEs
23355 are equal if they have the same offset. */
23356
23357 static int
23358 partial_die_eq (const void *item_lhs, const void *item_rhs)
23359 {
23360 const struct partial_die_info *part_die_lhs
23361 = (const struct partial_die_info *) item_lhs;
23362 const struct partial_die_info *part_die_rhs
23363 = (const struct partial_die_info *) item_rhs;
23364
23365 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23366 }
23367
23368 struct cmd_list_element *set_dwarf_cmdlist;
23369 struct cmd_list_element *show_dwarf_cmdlist;
23370
23371 static void
23372 set_dwarf_cmd (const char *args, int from_tty)
23373 {
23374 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
23375 gdb_stdout);
23376 }
23377
23378 static void
23379 show_dwarf_cmd (const char *args, int from_tty)
23380 {
23381 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
23382 }
23383
23384 static void
23385 show_check_physname (struct ui_file *file, int from_tty,
23386 struct cmd_list_element *c, const char *value)
23387 {
23388 fprintf_filtered (file,
23389 _("Whether to check \"physname\" is %s.\n"),
23390 value);
23391 }
23392
23393 void _initialize_dwarf2_read ();
23394 void
23395 _initialize_dwarf2_read ()
23396 {
23397 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23398 Set DWARF specific variables.\n\
23399 Configure DWARF variables such as the cache size."),
23400 &set_dwarf_cmdlist, "maintenance set dwarf ",
23401 0/*allow-unknown*/, &maintenance_set_cmdlist);
23402
23403 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
23404 Show DWARF specific variables.\n\
23405 Show DWARF variables such as the cache size."),
23406 &show_dwarf_cmdlist, "maintenance show dwarf ",
23407 0/*allow-unknown*/, &maintenance_show_cmdlist);
23408
23409 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23410 &dwarf_max_cache_age, _("\
23411 Set the upper bound on the age of cached DWARF compilation units."), _("\
23412 Show the upper bound on the age of cached DWARF compilation units."), _("\
23413 A higher limit means that cached compilation units will be stored\n\
23414 in memory longer, and more total memory will be used. Zero disables\n\
23415 caching, which can slow down startup."),
23416 NULL,
23417 show_dwarf_max_cache_age,
23418 &set_dwarf_cmdlist,
23419 &show_dwarf_cmdlist);
23420
23421 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23422 Set debugging of the DWARF reader."), _("\
23423 Show debugging of the DWARF reader."), _("\
23424 When enabled (non-zero), debugging messages are printed during DWARF\n\
23425 reading and symtab expansion. A value of 1 (one) provides basic\n\
23426 information. A value greater than 1 provides more verbose information."),
23427 NULL,
23428 NULL,
23429 &setdebuglist, &showdebuglist);
23430
23431 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23432 Set debugging of the DWARF DIE reader."), _("\
23433 Show debugging of the DWARF DIE reader."), _("\
23434 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23435 The value is the maximum depth to print."),
23436 NULL,
23437 NULL,
23438 &setdebuglist, &showdebuglist);
23439
23440 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23441 Set debugging of the dwarf line reader."), _("\
23442 Show debugging of the dwarf line reader."), _("\
23443 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23444 A value of 1 (one) provides basic information.\n\
23445 A value greater than 1 provides more verbose information."),
23446 NULL,
23447 NULL,
23448 &setdebuglist, &showdebuglist);
23449
23450 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23451 Set cross-checking of \"physname\" code against demangler."), _("\
23452 Show cross-checking of \"physname\" code against demangler."), _("\
23453 When enabled, GDB's internal \"physname\" code is checked against\n\
23454 the demangler."),
23455 NULL, show_check_physname,
23456 &setdebuglist, &showdebuglist);
23457
23458 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23459 no_class, &use_deprecated_index_sections, _("\
23460 Set whether to use deprecated gdb_index sections."), _("\
23461 Show whether to use deprecated gdb_index sections."), _("\
23462 When enabled, deprecated .gdb_index sections are used anyway.\n\
23463 Normally they are ignored either because of a missing feature or\n\
23464 performance issue.\n\
23465 Warning: This option must be enabled before gdb reads the file."),
23466 NULL,
23467 NULL,
23468 &setlist, &showlist);
23469
23470 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23471 &dwarf2_locexpr_funcs);
23472 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23473 &dwarf2_loclist_funcs);
23474
23475 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23476 &dwarf2_block_frame_base_locexpr_funcs);
23477 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23478 &dwarf2_block_frame_base_loclist_funcs);
23479
23480 #if GDB_SELF_TEST
23481 selftests::register_test ("dw2_expand_symtabs_matching",
23482 selftests::dw2_expand_symtabs_matching::run_test);
23483 selftests::register_test ("dwarf2_find_containing_comp_unit",
23484 selftests::find_containing_comp_unit::run_test);
23485 #endif
23486 }
This page took 0.786509 seconds and 4 git commands to generate.