]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/dwarf2/read.c
Add dwarf2_per_objfile parameter to process_full_{comp,type}_unit
[thirdparty/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 "dwarf2/stringify.h"
44 #include "bfd.h"
45 #include "elf-bfd.h"
46 #include "symtab.h"
47 #include "gdbtypes.h"
48 #include "objfiles.h"
49 #include "dwarf2.h"
50 #include "buildsym.h"
51 #include "demangle.h"
52 #include "gdb-demangle.h"
53 #include "filenames.h" /* for DOSish file names */
54 #include "language.h"
55 #include "complaints.h"
56 #include "dwarf2/expr.h"
57 #include "dwarf2/loc.h"
58 #include "cp-support.h"
59 #include "hashtab.h"
60 #include "command.h"
61 #include "gdbcmd.h"
62 #include "block.h"
63 #include "addrmap.h"
64 #include "typeprint.h"
65 #include "psympriv.h"
66 #include "c-lang.h"
67 #include "go-lang.h"
68 #include "valprint.h"
69 #include "gdbcore.h" /* for gnutarget */
70 #include "gdb/gdb-index.h"
71 #include "gdb_bfd.h"
72 #include "f-lang.h"
73 #include "source.h"
74 #include "build-id.h"
75 #include "namespace.h"
76 #include "gdbsupport/function-view.h"
77 #include "gdbsupport/gdb_optional.h"
78 #include "gdbsupport/underlying.h"
79 #include "gdbsupport/hash_enum.h"
80 #include "filename-seen-cache.h"
81 #include "producer.h"
82 #include <fcntl.h>
83 #include <algorithm>
84 #include <unordered_map>
85 #include "gdbsupport/selftest.h"
86 #include "rust-lang.h"
87 #include "gdbsupport/pathstuff.h"
88 #include "count-one-bits.h"
89 #include "debuginfod-support.h"
90
91 /* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94 static unsigned int dwarf_read_debug = 0;
95
96 /* When non-zero, dump DIEs after they are read in. */
97 static unsigned int dwarf_die_debug = 0;
98
99 /* When non-zero, dump line number entries as they are read in. */
100 unsigned int dwarf_line_debug = 0;
101
102 /* When true, cross-check physname against demangler. */
103 static bool check_physname = false;
104
105 /* When true, do not reject deprecated .gdb_index sections. */
106 static bool use_deprecated_index_sections = false;
107
108 static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
109
110 /* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112 static int dwarf2_locexpr_index;
113 static int dwarf2_loclist_index;
114 static int dwarf2_locexpr_block_index;
115 static int dwarf2_loclist_block_index;
116
117 /* Size of .debug_loclists section header for 32-bit DWARF format. */
118 #define LOCLIST_HEADER_SIZE32 12
119
120 /* Size of .debug_loclists section header for 64-bit DWARF format. */
121 #define LOCLIST_HEADER_SIZE64 20
122
123 /* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136 struct name_component
137 {
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146 };
147
148 /* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151 struct mapped_index_base
152 {
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
187
188 /* Prevent deleting/destroying via a base class pointer. */
189 protected:
190 ~mapped_index_base() = default;
191 };
192
193 /* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
195 struct mapped_index final : public mapped_index_base
196 {
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
204 /* Index data format version. */
205 int version = 0;
206
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
209
210 /* The symbol table, implemented as a hash table. */
211 gdb::array_view<symbol_table_slot> symbol_table;
212
213 /* A pointer to the constant pool. */
214 const char *constant_pool = nullptr;
215
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
219 return bucket.name == 0 && bucket.vec == 0;
220 }
221
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
224 const char *symbol_name_at (offset_type idx) const override
225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
226
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
229 };
230
231 /* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
233 struct mapped_debug_names final : public mapped_index_base
234 {
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
281 };
282
283 /* See dwarf2read.h. */
284
285 dwarf2_per_objfile *
286 get_dwarf2_per_objfile (struct objfile *objfile)
287 {
288 return dwarf2_objfile_data_key.get (objfile);
289 }
290
291 /* Default names of the debugging sections. */
292
293 /* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
296 static const struct dwarf2_debug_sections dwarf2_elf_names =
297 {
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
302 { ".debug_loclists", ".zdebug_loclists" },
303 { ".debug_macinfo", ".zdebug_macinfo" },
304 { ".debug_macro", ".zdebug_macro" },
305 { ".debug_str", ".zdebug_str" },
306 { ".debug_str_offsets", ".zdebug_str_offsets" },
307 { ".debug_line_str", ".zdebug_line_str" },
308 { ".debug_ranges", ".zdebug_ranges" },
309 { ".debug_rnglists", ".zdebug_rnglists" },
310 { ".debug_types", ".zdebug_types" },
311 { ".debug_addr", ".zdebug_addr" },
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
314 { ".gdb_index", ".zgdb_index" },
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
317 23
318 };
319
320 /* List of DWO/DWP sections. */
321
322 static const struct dwop_section_names
323 {
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
328 struct dwarf2_section_names loclists_dwo;
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
336 }
337 dwop_section_names =
338 {
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
351 };
352
353 /* local data types */
354
355 /* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357 struct loclist_header
358 {
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377 };
378
379 /* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381 struct delayed_method_info
382 {
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397 };
398
399 /* Internal state when decoding a particular compilation unit. */
400 struct dwarf2_cu
401 {
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
404 ~dwarf2_cu ();
405
406 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
407
408 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
409 Create the set of symtabs used by this TU, or if this TU is sharing
410 symtabs with another TU and the symtabs have already been created
411 then restore those symtabs in the line header.
412 We don't need the pc/line-number mapping for type units. */
413 void setup_type_unit_groups (struct die_info *die);
414
415 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
416 buildsym_compunit constructor. */
417 struct compunit_symtab *start_symtab (const char *name,
418 const char *comp_dir,
419 CORE_ADDR low_pc);
420
421 /* Reset the builder. */
422 void reset_builder () { m_builder.reset (); }
423
424 /* The header of the compilation unit. */
425 struct comp_unit_head header {};
426
427 /* Base address of this compilation unit. */
428 gdb::optional<CORE_ADDR> base_address;
429
430 /* The language we are debugging. */
431 enum language language = language_unknown;
432 const struct language_defn *language_defn = nullptr;
433
434 const char *producer = nullptr;
435
436 private:
437 /* The symtab builder for this CU. This is only non-NULL when full
438 symbols are being read. */
439 std::unique_ptr<buildsym_compunit> m_builder;
440
441 public:
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
451 struct pending **list_in_scope = nullptr;
452
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
455 htab_t partial_dies = nullptr;
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
459 auto_obstack comp_unit_obstack;
460
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
466
467 /* Backlink to our per_cu entry. */
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* The dwarf2_per_objfile that owns this. */
471 struct dwarf2_per_objfile *per_objfile;
472
473 /* How many compilation units ago was this CU last referenced? */
474 int last_used = 0;
475
476 /* A hash table of DIE cu_offset for following references with
477 die_info->offset.sect_off as hash. */
478 htab_t die_hash = nullptr;
479
480 /* Full DIEs if read in. */
481 struct die_info *dies = nullptr;
482
483 /* A set of pointers to dwarf2_per_cu_data objects for compilation
484 units referenced by this one. Only set during full symbol processing;
485 partial symbol tables do not have dependencies. */
486 htab_t dependencies = nullptr;
487
488 /* Header data from the line table, during full symbol processing. */
489 struct line_header *line_header = nullptr;
490 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
491 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
492 this is the DW_TAG_compile_unit die for this CU. We'll hold on
493 to the line header as long as this DIE is being processed. See
494 process_die_scope. */
495 die_info *line_header_die_owner = nullptr;
496
497 /* A list of methods which need to have physnames computed
498 after all type information has been read. */
499 std::vector<delayed_method_info> method_list;
500
501 /* To be copied to symtab->call_site_htab. */
502 htab_t call_site_htab = nullptr;
503
504 /* Non-NULL if this CU came from a DWO file.
505 There is an invariant here that is important to remember:
506 Except for attributes copied from the top level DIE in the "main"
507 (or "stub") file in preparation for reading the DWO file
508 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
509 Either there isn't a DWO file (in which case this is NULL and the point
510 is moot), or there is and either we're not going to read it (in which
511 case this is NULL) or there is and we are reading it (in which case this
512 is non-NULL). */
513 struct dwo_unit *dwo_unit = nullptr;
514
515 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
516 Note this value comes from the Fission stub CU/TU's DIE. */
517 gdb::optional<ULONGEST> addr_base;
518
519 /* The DW_AT_rnglists_base attribute if present.
520 Note this value comes from the Fission stub CU/TU's DIE.
521 Also note that the value is zero in the non-DWO case so this value can
522 be used without needing to know whether DWO files are in use or not.
523 N.B. This does not apply to DW_AT_ranges appearing in
524 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
525 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
526 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
527 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
528 ULONGEST ranges_base = 0;
529
530 /* The DW_AT_loclists_base attribute if present. */
531 ULONGEST loclist_base = 0;
532
533 /* When reading debug info generated by older versions of rustc, we
534 have to rewrite some union types to be struct types with a
535 variant part. This rewriting must be done after the CU is fully
536 read in, because otherwise at the point of rewriting some struct
537 type might not have been fully processed. So, we keep a list of
538 all such types here and process them after expansion. */
539 std::vector<struct type *> rust_unions;
540
541 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
542 files, the value is implicitly zero. For DWARF 5 version DWO files, the
543 value is often implicit and is the size of the header of
544 .debug_str_offsets section (8 or 4, depending on the address size). */
545 gdb::optional<ULONGEST> str_offsets_base;
546
547 /* Mark used when releasing cached dies. */
548 bool mark : 1;
549
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
554 bool has_loclist : 1;
555
556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
563 bool producer_is_icc : 1;
564 bool producer_is_icc_lt_14 : 1;
565 bool producer_is_codewarrior : 1;
566
567 /* When true, the file that we're processing is known to have
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
571 bool processing_has_namespace_info : 1;
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
592 };
593
594 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597 struct stmt_list_hash
598 {
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
603 sect_offset line_sect_off;
604 };
605
606 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
607 an object of this type. */
608
609 struct type_unit_group
610 {
611 /* dwarf2read.c's main "handle" on a TU symtab.
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab. */
615 struct dwarf2_per_cu_data per_cu;
616
617 /* The TUs that share this DW_AT_stmt_list entry.
618 This is added to while parsing type units to build partial symtabs,
619 and is deleted afterwards and not used again. */
620 std::vector<signatured_type *> *tus;
621
622 /* The compunit symtab.
623 Type units in a group needn't all be defined in the same source file,
624 so we create an essentially anonymous symtab as the compunit symtab. */
625 struct compunit_symtab *compunit_symtab;
626
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
629
630 /* The symbol tables for this TU (obtained from the files listed in
631 DW_AT_stmt_list).
632 WARNING: The order of entries here must match the order of entries
633 in the line header. After the first TU using this type_unit_group, the
634 line header for the subsequent TUs is recreated from this. This is done
635 because we need to use the same symtabs for each TU using the same
636 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
637 there's no guarantee the line header doesn't have duplicate entries. */
638 struct symtab **symtabs;
639 };
640
641 /* These sections are what may appear in a (real or virtual) DWO file. */
642
643 struct dwo_sections
644 {
645 struct dwarf2_section_info abbrev;
646 struct dwarf2_section_info line;
647 struct dwarf2_section_info loc;
648 struct dwarf2_section_info loclists;
649 struct dwarf2_section_info macinfo;
650 struct dwarf2_section_info macro;
651 struct dwarf2_section_info str;
652 struct dwarf2_section_info str_offsets;
653 /* In the case of a virtual DWO file, these two are unused. */
654 struct dwarf2_section_info info;
655 std::vector<dwarf2_section_info> types;
656 };
657
658 /* CUs/TUs in DWP/DWO files. */
659
660 struct dwo_unit
661 {
662 /* Backlink to the containing struct dwo_file. */
663 struct dwo_file *dwo_file;
664
665 /* The "id" that distinguishes this CU/TU.
666 .debug_info calls this "dwo_id", .debug_types calls this "signature".
667 Since signatures came first, we stick with it for consistency. */
668 ULONGEST signature;
669
670 /* The section this CU/TU lives in, in the DWO file. */
671 struct dwarf2_section_info *section;
672
673 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
674 sect_offset sect_off;
675 unsigned int length;
676
677 /* For types, offset in the type's DIE of the type defined by this TU. */
678 cu_offset type_offset_in_tu;
679 };
680
681 /* include/dwarf2.h defines the DWP section codes.
682 It defines a max value but it doesn't define a min value, which we
683 use for error checking, so provide one. */
684
685 enum dwp_v2_section_ids
686 {
687 DW_SECT_MIN = 1
688 };
689
690 /* Data for one DWO file.
691
692 This includes virtual DWO files (a virtual DWO file is a DWO file as it
693 appears in a DWP file). DWP files don't really have DWO files per se -
694 comdat folding of types "loses" the DWO file they came from, and from
695 a high level view DWP files appear to contain a mass of random types.
696 However, to maintain consistency with the non-DWP case we pretend DWP
697 files contain virtual DWO files, and we assign each TU with one virtual
698 DWO file (generally based on the line and abbrev section offsets -
699 a heuristic that seems to work in practice). */
700
701 struct dwo_file
702 {
703 dwo_file () = default;
704 DISABLE_COPY_AND_ASSIGN (dwo_file);
705
706 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
707 For virtual DWO files the name is constructed from the section offsets
708 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
709 from related CU+TUs. */
710 const char *dwo_name = nullptr;
711
712 /* The DW_AT_comp_dir attribute. */
713 const char *comp_dir = nullptr;
714
715 /* The bfd, when the file is open. Otherwise this is NULL.
716 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
717 gdb_bfd_ref_ptr dbfd;
718
719 /* The sections that make up this DWO file.
720 Remember that for virtual DWO files in DWP V2, these are virtual
721 sections (for lack of a better name). */
722 struct dwo_sections sections {};
723
724 /* The CUs in the file.
725 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
726 an extension to handle LLVM's Link Time Optimization output (where
727 multiple source files may be compiled into a single object/dwo pair). */
728 htab_up cus;
729
730 /* Table of TUs in the file.
731 Each element is a struct dwo_unit. */
732 htab_up tus;
733 };
734
735 /* These sections are what may appear in a DWP file. */
736
737 struct dwp_sections
738 {
739 /* These are used by both DWP version 1 and 2. */
740 struct dwarf2_section_info str;
741 struct dwarf2_section_info cu_index;
742 struct dwarf2_section_info tu_index;
743
744 /* These are only used by DWP version 2 files.
745 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
746 sections are referenced by section number, and are not recorded here.
747 In DWP version 2 there is at most one copy of all these sections, each
748 section being (effectively) comprised of the concatenation of all of the
749 individual sections that exist in the version 1 format.
750 To keep the code simple we treat each of these concatenated pieces as a
751 section itself (a virtual section?). */
752 struct dwarf2_section_info abbrev;
753 struct dwarf2_section_info info;
754 struct dwarf2_section_info line;
755 struct dwarf2_section_info loc;
756 struct dwarf2_section_info macinfo;
757 struct dwarf2_section_info macro;
758 struct dwarf2_section_info str_offsets;
759 struct dwarf2_section_info types;
760 };
761
762 /* These sections are what may appear in a virtual DWO file in DWP version 1.
763 A virtual DWO file is a DWO file as it appears in a DWP file. */
764
765 struct virtual_v1_dwo_sections
766 {
767 struct dwarf2_section_info abbrev;
768 struct dwarf2_section_info line;
769 struct dwarf2_section_info loc;
770 struct dwarf2_section_info macinfo;
771 struct dwarf2_section_info macro;
772 struct dwarf2_section_info str_offsets;
773 /* Each DWP hash table entry records one CU or one TU.
774 That is recorded here, and copied to dwo_unit.section. */
775 struct dwarf2_section_info info_or_types;
776 };
777
778 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
779 In version 2, the sections of the DWO files are concatenated together
780 and stored in one section of that name. Thus each ELF section contains
781 several "virtual" sections. */
782
783 struct virtual_v2_dwo_sections
784 {
785 bfd_size_type abbrev_offset;
786 bfd_size_type abbrev_size;
787
788 bfd_size_type line_offset;
789 bfd_size_type line_size;
790
791 bfd_size_type loc_offset;
792 bfd_size_type loc_size;
793
794 bfd_size_type macinfo_offset;
795 bfd_size_type macinfo_size;
796
797 bfd_size_type macro_offset;
798 bfd_size_type macro_size;
799
800 bfd_size_type str_offsets_offset;
801 bfd_size_type str_offsets_size;
802
803 /* Each DWP hash table entry records one CU or one TU.
804 That is recorded here, and copied to dwo_unit.section. */
805 bfd_size_type info_or_types_offset;
806 bfd_size_type info_or_types_size;
807 };
808
809 /* Contents of DWP hash tables. */
810
811 struct dwp_hash_table
812 {
813 uint32_t version, nr_columns;
814 uint32_t nr_units, nr_slots;
815 const gdb_byte *hash_table, *unit_table;
816 union
817 {
818 struct
819 {
820 const gdb_byte *indices;
821 } v1;
822 struct
823 {
824 /* This is indexed by column number and gives the id of the section
825 in that column. */
826 #define MAX_NR_V2_DWO_SECTIONS \
827 (1 /* .debug_info or .debug_types */ \
828 + 1 /* .debug_abbrev */ \
829 + 1 /* .debug_line */ \
830 + 1 /* .debug_loc */ \
831 + 1 /* .debug_str_offsets */ \
832 + 1 /* .debug_macro or .debug_macinfo */)
833 int section_ids[MAX_NR_V2_DWO_SECTIONS];
834 const gdb_byte *offsets;
835 const gdb_byte *sizes;
836 } v2;
837 } section_pool;
838 };
839
840 /* Data for one DWP file. */
841
842 struct dwp_file
843 {
844 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
845 : name (name_),
846 dbfd (std::move (abfd))
847 {
848 }
849
850 /* Name of the file. */
851 const char *name;
852
853 /* File format version. */
854 int version = 0;
855
856 /* The bfd. */
857 gdb_bfd_ref_ptr dbfd;
858
859 /* Section info for this file. */
860 struct dwp_sections sections {};
861
862 /* Table of CUs in the file. */
863 const struct dwp_hash_table *cus = nullptr;
864
865 /* Table of TUs in the file. */
866 const struct dwp_hash_table *tus = nullptr;
867
868 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
869 htab_up loaded_cus;
870 htab_up loaded_tus;
871
872 /* Table to map ELF section numbers to their sections.
873 This is only needed for the DWP V1 file format. */
874 unsigned int num_sections = 0;
875 asection **elf_sections = nullptr;
876 };
877
878 /* Struct used to pass misc. parameters to read_die_and_children, et
879 al. which are used for both .debug_info and .debug_types dies.
880 All parameters here are unchanging for the life of the call. This
881 struct exists to abstract away the constant parameters of die reading. */
882
883 struct die_reader_specs
884 {
885 /* The bfd of die_section. */
886 bfd* abfd;
887
888 /* The CU of the DIE we are parsing. */
889 struct dwarf2_cu *cu;
890
891 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
892 struct dwo_file *dwo_file;
893
894 /* The section the die comes from.
895 This is either .debug_info or .debug_types, or the .dwo variants. */
896 struct dwarf2_section_info *die_section;
897
898 /* die_section->buffer. */
899 const gdb_byte *buffer;
900
901 /* The end of the buffer. */
902 const gdb_byte *buffer_end;
903
904 /* The abbreviation table to use when reading the DIEs. */
905 struct abbrev_table *abbrev_table;
906 };
907
908 /* A subclass of die_reader_specs that holds storage and has complex
909 constructor and destructor behavior. */
910
911 class cutu_reader : public die_reader_specs
912 {
913 public:
914
915 cutu_reader (dwarf2_per_cu_data *this_cu,
916 dwarf2_per_objfile *per_objfile,
917 struct abbrev_table *abbrev_table,
918 int use_existing_cu,
919 bool skip_partial);
920
921 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
922 dwarf2_per_objfile *per_objfile,
923 struct dwarf2_cu *parent_cu = nullptr,
924 struct dwo_file *dwo_file = nullptr);
925
926 DISABLE_COPY_AND_ASSIGN (cutu_reader);
927
928 const gdb_byte *info_ptr = nullptr;
929 struct die_info *comp_unit_die = nullptr;
930 bool dummy_p = false;
931
932 /* Release the new CU, putting it on the chain. This cannot be done
933 for dummy CUs. */
934 void keep ();
935
936 private:
937 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
938 dwarf2_per_objfile *per_objfile,
939 int use_existing_cu);
940
941 struct dwarf2_per_cu_data *m_this_cu;
942 std::unique_ptr<dwarf2_cu> m_new_cu;
943
944 /* The ordinary abbreviation table. */
945 abbrev_table_up m_abbrev_table_holder;
946
947 /* The DWO abbreviation table. */
948 abbrev_table_up m_dwo_abbrev_table;
949 };
950
951 /* When we construct a partial symbol table entry we only
952 need this much information. */
953 struct partial_die_info : public allocate_on_obstack
954 {
955 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
956
957 /* Disable assign but still keep copy ctor, which is needed
958 load_partial_dies. */
959 partial_die_info& operator=(const partial_die_info& rhs) = delete;
960
961 /* Adjust the partial die before generating a symbol for it. This
962 function may set the is_external flag or change the DIE's
963 name. */
964 void fixup (struct dwarf2_cu *cu);
965
966 /* Read a minimal amount of information into the minimal die
967 structure. */
968 const gdb_byte *read (const struct die_reader_specs *reader,
969 const struct abbrev_info &abbrev,
970 const gdb_byte *info_ptr);
971
972 /* Offset of this DIE. */
973 const sect_offset sect_off;
974
975 /* DWARF-2 tag for this DIE. */
976 const ENUM_BITFIELD(dwarf_tag) tag : 16;
977
978 /* Assorted flags describing the data found in this DIE. */
979 const unsigned int has_children : 1;
980
981 unsigned int is_external : 1;
982 unsigned int is_declaration : 1;
983 unsigned int has_type : 1;
984 unsigned int has_specification : 1;
985 unsigned int has_pc_info : 1;
986 unsigned int may_be_inlined : 1;
987
988 /* This DIE has been marked DW_AT_main_subprogram. */
989 unsigned int main_subprogram : 1;
990
991 /* Flag set if the SCOPE field of this structure has been
992 computed. */
993 unsigned int scope_set : 1;
994
995 /* Flag set if the DIE has a byte_size attribute. */
996 unsigned int has_byte_size : 1;
997
998 /* Flag set if the DIE has a DW_AT_const_value attribute. */
999 unsigned int has_const_value : 1;
1000
1001 /* Flag set if any of the DIE's children are template arguments. */
1002 unsigned int has_template_arguments : 1;
1003
1004 /* Flag set if fixup has been called on this die. */
1005 unsigned int fixup_called : 1;
1006
1007 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1008 unsigned int is_dwz : 1;
1009
1010 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1011 unsigned int spec_is_dwz : 1;
1012
1013 /* The name of this DIE. Normally the value of DW_AT_name, but
1014 sometimes a default name for unnamed DIEs. */
1015 const char *name = nullptr;
1016
1017 /* The linkage name, if present. */
1018 const char *linkage_name = nullptr;
1019
1020 /* The scope to prepend to our children. This is generally
1021 allocated on the comp_unit_obstack, so will disappear
1022 when this compilation unit leaves the cache. */
1023 const char *scope = nullptr;
1024
1025 /* Some data associated with the partial DIE. The tag determines
1026 which field is live. */
1027 union
1028 {
1029 /* The location description associated with this DIE, if any. */
1030 struct dwarf_block *locdesc;
1031 /* The offset of an import, for DW_TAG_imported_unit. */
1032 sect_offset sect_off;
1033 } d {};
1034
1035 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1036 CORE_ADDR lowpc = 0;
1037 CORE_ADDR highpc = 0;
1038
1039 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1040 DW_AT_sibling, if any. */
1041 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1042 could return DW_AT_sibling values to its caller load_partial_dies. */
1043 const gdb_byte *sibling = nullptr;
1044
1045 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1046 DW_AT_specification (or DW_AT_abstract_origin or
1047 DW_AT_extension). */
1048 sect_offset spec_offset {};
1049
1050 /* Pointers to this DIE's parent, first child, and next sibling,
1051 if any. */
1052 struct partial_die_info *die_parent = nullptr;
1053 struct partial_die_info *die_child = nullptr;
1054 struct partial_die_info *die_sibling = nullptr;
1055
1056 friend struct partial_die_info *
1057 dwarf2_cu::find_partial_die (sect_offset sect_off);
1058
1059 private:
1060 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1061 partial_die_info (sect_offset sect_off)
1062 : partial_die_info (sect_off, DW_TAG_padding, 0)
1063 {
1064 }
1065
1066 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1067 int has_children_)
1068 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1069 {
1070 is_external = 0;
1071 is_declaration = 0;
1072 has_type = 0;
1073 has_specification = 0;
1074 has_pc_info = 0;
1075 may_be_inlined = 0;
1076 main_subprogram = 0;
1077 scope_set = 0;
1078 has_byte_size = 0;
1079 has_const_value = 0;
1080 has_template_arguments = 0;
1081 fixup_called = 0;
1082 is_dwz = 0;
1083 spec_is_dwz = 0;
1084 }
1085 };
1086
1087 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1088 but this would require a corresponding change in unpack_field_as_long
1089 and friends. */
1090 static int bits_per_byte = 8;
1091
1092 struct variant_part_builder;
1093
1094 /* When reading a variant, we track a bit more information about the
1095 field, and store it in an object of this type. */
1096
1097 struct variant_field
1098 {
1099 int first_field = -1;
1100 int last_field = -1;
1101
1102 /* A variant can contain other variant parts. */
1103 std::vector<variant_part_builder> variant_parts;
1104
1105 /* If we see a DW_TAG_variant, then this will be set if this is the
1106 default branch. */
1107 bool default_branch = false;
1108 /* If we see a DW_AT_discr_value, then this will be the discriminant
1109 value. */
1110 ULONGEST discriminant_value = 0;
1111 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1112 data. */
1113 struct dwarf_block *discr_list_data = nullptr;
1114 };
1115
1116 /* This represents a DW_TAG_variant_part. */
1117
1118 struct variant_part_builder
1119 {
1120 /* The offset of the discriminant field. */
1121 sect_offset discriminant_offset {};
1122
1123 /* Variants that are direct children of this variant part. */
1124 std::vector<variant_field> variants;
1125
1126 /* True if we're currently reading a variant. */
1127 bool processing_variant = false;
1128 };
1129
1130 struct nextfield
1131 {
1132 int accessibility = 0;
1133 int virtuality = 0;
1134 /* Variant parts need to find the discriminant, which is a DIE
1135 reference. We track the section offset of each field to make
1136 this link. */
1137 sect_offset offset;
1138 struct field field {};
1139 };
1140
1141 struct fnfieldlist
1142 {
1143 const char *name = nullptr;
1144 std::vector<struct fn_field> fnfields;
1145 };
1146
1147 /* The routines that read and process dies for a C struct or C++ class
1148 pass lists of data member fields and lists of member function fields
1149 in an instance of a field_info structure, as defined below. */
1150 struct field_info
1151 {
1152 /* List of data member and baseclasses fields. */
1153 std::vector<struct nextfield> fields;
1154 std::vector<struct nextfield> baseclasses;
1155
1156 /* Set if the accessibility of one of the fields is not public. */
1157 int non_public_fields = 0;
1158
1159 /* Member function fieldlist array, contains name of possibly overloaded
1160 member function, number of overloaded member functions and a pointer
1161 to the head of the member function field chain. */
1162 std::vector<struct fnfieldlist> fnfieldlists;
1163
1164 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1165 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1166 std::vector<struct decl_field> typedef_field_list;
1167
1168 /* Nested types defined by this class and the number of elements in this
1169 list. */
1170 std::vector<struct decl_field> nested_types_list;
1171
1172 /* If non-null, this is the variant part we are currently
1173 reading. */
1174 variant_part_builder *current_variant_part = nullptr;
1175 /* This holds all the top-level variant parts attached to the type
1176 we're reading. */
1177 std::vector<variant_part_builder> variant_parts;
1178
1179 /* Return the total number of fields (including baseclasses). */
1180 int nfields () const
1181 {
1182 return fields.size () + baseclasses.size ();
1183 }
1184 };
1185
1186 /* Loaded secondary compilation units are kept in memory until they
1187 have not been referenced for the processing of this many
1188 compilation units. Set this to zero to disable caching. Cache
1189 sizes of up to at least twenty will improve startup time for
1190 typical inter-CU-reference binaries, at an obvious memory cost. */
1191 static int dwarf_max_cache_age = 5;
1192 static void
1193 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1194 struct cmd_list_element *c, const char *value)
1195 {
1196 fprintf_filtered (file, _("The upper bound on the age of cached "
1197 "DWARF compilation units is %s.\n"),
1198 value);
1199 }
1200 \f
1201 /* local function prototypes */
1202
1203 static void dwarf2_find_base_address (struct die_info *die,
1204 struct dwarf2_cu *cu);
1205
1206 static dwarf2_psymtab *create_partial_symtab
1207 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1208 const char *name);
1209
1210 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1211 const gdb_byte *info_ptr,
1212 struct die_info *type_unit_die);
1213
1214 static void dwarf2_build_psymtabs_hard
1215 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1216
1217 static void scan_partial_symbols (struct partial_die_info *,
1218 CORE_ADDR *, CORE_ADDR *,
1219 int, struct dwarf2_cu *);
1220
1221 static void add_partial_symbol (struct partial_die_info *,
1222 struct dwarf2_cu *);
1223
1224 static void add_partial_namespace (struct partial_die_info *pdi,
1225 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1226 int set_addrmap, struct dwarf2_cu *cu);
1227
1228 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1229 CORE_ADDR *highpc, int set_addrmap,
1230 struct dwarf2_cu *cu);
1231
1232 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1233 struct dwarf2_cu *cu);
1234
1235 static void add_partial_subprogram (struct partial_die_info *pdi,
1236 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1237 int need_pc, struct dwarf2_cu *cu);
1238
1239 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1240
1241 static struct partial_die_info *load_partial_dies
1242 (const struct die_reader_specs *, const gdb_byte *, int);
1243
1244 /* A pair of partial_die_info and compilation unit. */
1245 struct cu_partial_die_info
1246 {
1247 /* The compilation unit of the partial_die_info. */
1248 struct dwarf2_cu *cu;
1249 /* A partial_die_info. */
1250 struct partial_die_info *pdi;
1251
1252 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1253 : cu (cu),
1254 pdi (pdi)
1255 { /* Nothing. */ }
1256
1257 private:
1258 cu_partial_die_info () = delete;
1259 };
1260
1261 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1262 struct dwarf2_cu *);
1263
1264 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1265 struct attribute *, struct attr_abbrev *,
1266 const gdb_byte *, bool *need_reprocess);
1267
1268 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1269 struct attribute *attr);
1270
1271 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1272
1273 static sect_offset read_abbrev_offset
1274 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1275 struct dwarf2_section_info *, sect_offset);
1276
1277 static const char *read_indirect_string
1278 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1279 const struct comp_unit_head *, unsigned int *);
1280
1281 static const char *read_indirect_string_at_offset
1282 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1283
1284 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1285 const gdb_byte *,
1286 unsigned int *);
1287
1288 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1289 ULONGEST str_index);
1290
1291 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1292 ULONGEST str_index);
1293
1294 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1295
1296 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1297 struct dwarf2_cu *);
1298
1299 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1300 struct dwarf2_cu *cu);
1301
1302 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1303
1304 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1305 struct dwarf2_cu *cu);
1306
1307 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1308
1309 static struct die_info *die_specification (struct die_info *die,
1310 struct dwarf2_cu **);
1311
1312 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1313 struct dwarf2_cu *cu);
1314
1315 static void dwarf_decode_lines (struct line_header *, const char *,
1316 struct dwarf2_cu *, dwarf2_psymtab *,
1317 CORE_ADDR, int decode_mapping);
1318
1319 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1320 const char *);
1321
1322 static struct symbol *new_symbol (struct die_info *, struct type *,
1323 struct dwarf2_cu *, struct symbol * = NULL);
1324
1325 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1326 struct dwarf2_cu *);
1327
1328 static void dwarf2_const_value_attr (const struct attribute *attr,
1329 struct type *type,
1330 const char *name,
1331 struct obstack *obstack,
1332 struct dwarf2_cu *cu, LONGEST *value,
1333 const gdb_byte **bytes,
1334 struct dwarf2_locexpr_baton **baton);
1335
1336 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1337
1338 static int need_gnat_info (struct dwarf2_cu *);
1339
1340 static struct type *die_descriptive_type (struct die_info *,
1341 struct dwarf2_cu *);
1342
1343 static void set_descriptive_type (struct type *, struct die_info *,
1344 struct dwarf2_cu *);
1345
1346 static struct type *die_containing_type (struct die_info *,
1347 struct dwarf2_cu *);
1348
1349 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1350 struct dwarf2_cu *);
1351
1352 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1353
1354 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1355
1356 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1357
1358 static char *typename_concat (struct obstack *obs, const char *prefix,
1359 const char *suffix, int physname,
1360 struct dwarf2_cu *cu);
1361
1362 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1363
1364 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1365
1366 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1367
1368 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1369
1370 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1371
1372 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1373
1374 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1375 struct dwarf2_cu *, dwarf2_psymtab *);
1376
1377 /* Return the .debug_loclists section to use for cu. */
1378 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1379
1380 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1381 values. Keep the items ordered with increasing constraints compliance. */
1382 enum pc_bounds_kind
1383 {
1384 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1385 PC_BOUNDS_NOT_PRESENT,
1386
1387 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1388 were present but they do not form a valid range of PC addresses. */
1389 PC_BOUNDS_INVALID,
1390
1391 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1392 PC_BOUNDS_RANGES,
1393
1394 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1395 PC_BOUNDS_HIGH_LOW,
1396 };
1397
1398 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1399 CORE_ADDR *, CORE_ADDR *,
1400 struct dwarf2_cu *,
1401 dwarf2_psymtab *);
1402
1403 static void get_scope_pc_bounds (struct die_info *,
1404 CORE_ADDR *, CORE_ADDR *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1408 CORE_ADDR, struct dwarf2_cu *);
1409
1410 static void dwarf2_add_field (struct field_info *, struct die_info *,
1411 struct dwarf2_cu *);
1412
1413 static void dwarf2_attach_fields_to_type (struct field_info *,
1414 struct type *, struct dwarf2_cu *);
1415
1416 static void dwarf2_add_member_fn (struct field_info *,
1417 struct die_info *, struct type *,
1418 struct dwarf2_cu *);
1419
1420 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1421 struct type *,
1422 struct dwarf2_cu *);
1423
1424 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1425
1426 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1427
1428 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1429
1430 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1431
1432 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1433
1434 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1435
1436 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1437
1438 static struct type *read_module_type (struct die_info *die,
1439 struct dwarf2_cu *cu);
1440
1441 static const char *namespace_name (struct die_info *die,
1442 int *is_anonymous, struct dwarf2_cu *);
1443
1444 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1447 bool * = nullptr);
1448
1449 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1450 struct dwarf2_cu *);
1451
1452 static struct die_info *read_die_and_siblings_1
1453 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1454 struct die_info *);
1455
1456 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1457 const gdb_byte *info_ptr,
1458 const gdb_byte **new_info_ptr,
1459 struct die_info *parent);
1460
1461 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1462 struct die_info **, const gdb_byte *,
1463 int);
1464
1465 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1466 struct die_info **, const gdb_byte *);
1467
1468 static void process_die (struct die_info *, struct dwarf2_cu *);
1469
1470 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1471 struct objfile *);
1472
1473 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1474
1475 static const char *dwarf2_full_name (const char *name,
1476 struct die_info *die,
1477 struct dwarf2_cu *cu);
1478
1479 static const char *dwarf2_physname (const char *name, struct die_info *die,
1480 struct dwarf2_cu *cu);
1481
1482 static struct die_info *dwarf2_extension (struct die_info *die,
1483 struct dwarf2_cu **);
1484
1485 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1486
1487 static void dump_die_for_error (struct die_info *);
1488
1489 static void dump_die_1 (struct ui_file *, int level, int max_level,
1490 struct die_info *);
1491
1492 /*static*/ void dump_die (struct die_info *, int max_level);
1493
1494 static void store_in_ref_table (struct die_info *,
1495 struct dwarf2_cu *);
1496
1497 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1498 const struct attribute *,
1499 struct dwarf2_cu **);
1500
1501 static struct die_info *follow_die_ref (struct die_info *,
1502 const struct attribute *,
1503 struct dwarf2_cu **);
1504
1505 static struct die_info *follow_die_sig (struct die_info *,
1506 const struct attribute *,
1507 struct dwarf2_cu **);
1508
1509 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1510 struct dwarf2_cu *);
1511
1512 static struct type *get_DW_AT_signature_type (struct die_info *,
1513 const struct attribute *,
1514 struct dwarf2_cu *);
1515
1516 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1517 dwarf2_per_objfile *per_objfile);
1518
1519 static void read_signatured_type (signatured_type *sig_type,
1520 dwarf2_per_objfile *per_objfile);
1521
1522 static int attr_to_dynamic_prop (const struct attribute *attr,
1523 struct die_info *die, struct dwarf2_cu *cu,
1524 struct dynamic_prop *prop, struct type *type);
1525
1526 /* memory allocation interface */
1527
1528 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1529
1530 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1531
1532 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1533
1534 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1535 struct dwarf2_loclist_baton *baton,
1536 const struct attribute *attr);
1537
1538 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1539 struct symbol *sym,
1540 struct dwarf2_cu *cu,
1541 int is_block);
1542
1543 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1544 const gdb_byte *info_ptr,
1545 struct abbrev_info *abbrev);
1546
1547 static hashval_t partial_die_hash (const void *item);
1548
1549 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1550
1551 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1552 (sect_offset sect_off, unsigned int offset_in_dwz,
1553 struct dwarf2_per_objfile *dwarf2_per_objfile);
1554
1555 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1556 struct die_info *comp_unit_die,
1557 enum language pretend_language);
1558
1559 static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1560
1561 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
1562
1563 static struct type *set_die_type (struct die_info *, struct type *,
1564 struct dwarf2_cu *);
1565
1566 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1567
1568 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1569
1570 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1571 dwarf2_per_objfile *per_objfile,
1572 bool skip_partial,
1573 enum language pretend_language);
1574
1575 static void process_full_comp_unit (dwarf2_per_cu_data *per_cu,
1576 dwarf2_per_objfile *per_objfile,
1577 enum language pretend_language);
1578
1579 static void process_full_type_unit (dwarf2_per_cu_data *per_cu,
1580 dwarf2_per_objfile *per_objfile,
1581 enum language pretend_language);
1582
1583 static void dwarf2_add_dependence (struct dwarf2_cu *,
1584 struct dwarf2_per_cu_data *);
1585
1586 static void dwarf2_mark (struct dwarf2_cu *);
1587
1588 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1589
1590 static struct type *get_die_type_at_offset (sect_offset,
1591 struct dwarf2_per_cu_data *);
1592
1593 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1594
1595 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1596 enum language pretend_language);
1597
1598 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1599
1600 /* Class, the destructor of which frees all allocated queue entries. This
1601 will only have work to do if an error was thrown while processing the
1602 dwarf. If no error was thrown then the queue entries should have all
1603 been processed, and freed, as we went along. */
1604
1605 class dwarf2_queue_guard
1606 {
1607 public:
1608 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1609 : m_per_objfile (per_objfile)
1610 {
1611 }
1612
1613 /* Free any entries remaining on the queue. There should only be
1614 entries left if we hit an error while processing the dwarf. */
1615 ~dwarf2_queue_guard ()
1616 {
1617 /* Ensure that no memory is allocated by the queue. */
1618 std::queue<dwarf2_queue_item> empty;
1619 std::swap (m_per_objfile->per_bfd->queue, empty);
1620 }
1621
1622 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1623
1624 private:
1625 dwarf2_per_objfile *m_per_objfile;
1626 };
1627
1628 dwarf2_queue_item::~dwarf2_queue_item ()
1629 {
1630 /* Anything still marked queued is likely to be in an
1631 inconsistent state, so discard it. */
1632 if (per_cu->queued)
1633 {
1634 if (per_cu->cu != NULL)
1635 free_one_cached_comp_unit (per_cu);
1636 per_cu->queued = 0;
1637 }
1638 }
1639
1640 /* The return type of find_file_and_directory. Note, the enclosed
1641 string pointers are only valid while this object is valid. */
1642
1643 struct file_and_directory
1644 {
1645 /* The filename. This is never NULL. */
1646 const char *name;
1647
1648 /* The compilation directory. NULL if not known. If we needed to
1649 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1650 points directly to the DW_AT_comp_dir string attribute owned by
1651 the obstack that owns the DIE. */
1652 const char *comp_dir;
1653
1654 /* If we needed to build a new string for comp_dir, this is what
1655 owns the storage. */
1656 std::string comp_dir_storage;
1657 };
1658
1659 static file_and_directory find_file_and_directory (struct die_info *die,
1660 struct dwarf2_cu *cu);
1661
1662 static htab_up allocate_signatured_type_table ();
1663
1664 static htab_up allocate_dwo_unit_table ();
1665
1666 static struct dwo_unit *lookup_dwo_unit_in_dwp
1667 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1668 struct dwp_file *dwp_file, const char *comp_dir,
1669 ULONGEST signature, int is_debug_types);
1670
1671 static struct dwp_file *get_dwp_file
1672 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1673
1674 static struct dwo_unit *lookup_dwo_comp_unit
1675 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
1676
1677 static struct dwo_unit *lookup_dwo_type_unit
1678 (struct signatured_type *, const char *, const char *);
1679
1680 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1681
1682 /* A unique pointer to a dwo_file. */
1683
1684 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1685
1686 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1687
1688 static void check_producer (struct dwarf2_cu *cu);
1689
1690 static void free_line_header_voidp (void *arg);
1691 \f
1692 /* Various complaints about symbol reading that don't abort the process. */
1693
1694 static void
1695 dwarf2_debug_line_missing_file_complaint (void)
1696 {
1697 complaint (_(".debug_line section has line data without a file"));
1698 }
1699
1700 static void
1701 dwarf2_debug_line_missing_end_sequence_complaint (void)
1702 {
1703 complaint (_(".debug_line section has line "
1704 "program sequence without an end"));
1705 }
1706
1707 static void
1708 dwarf2_complex_location_expr_complaint (void)
1709 {
1710 complaint (_("location expression too complex"));
1711 }
1712
1713 static void
1714 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1715 int arg3)
1716 {
1717 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1718 arg1, arg2, arg3);
1719 }
1720
1721 static void
1722 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1723 {
1724 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1725 arg1, arg2);
1726 }
1727
1728 /* Hash function for line_header_hash. */
1729
1730 static hashval_t
1731 line_header_hash (const struct line_header *ofs)
1732 {
1733 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1734 }
1735
1736 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1737
1738 static hashval_t
1739 line_header_hash_voidp (const void *item)
1740 {
1741 const struct line_header *ofs = (const struct line_header *) item;
1742
1743 return line_header_hash (ofs);
1744 }
1745
1746 /* Equality function for line_header_hash. */
1747
1748 static int
1749 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1750 {
1751 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1752 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1753
1754 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1755 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1756 }
1757
1758 \f
1759
1760 /* See declaration. */
1761
1762 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1763 bool can_copy_)
1764 : obfd (obfd),
1765 can_copy (can_copy_)
1766 {
1767 if (names == NULL)
1768 names = &dwarf2_elf_names;
1769
1770 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1771 locate_sections (obfd, sec, *names);
1772 }
1773
1774 dwarf2_per_bfd::~dwarf2_per_bfd ()
1775 {
1776 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1777 free_cached_comp_units ();
1778
1779 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1780 per_cu->imported_symtabs_free ();
1781
1782 for (signatured_type *sig_type : all_type_units)
1783 sig_type->per_cu.imported_symtabs_free ();
1784
1785 /* Everything else should be on this->obstack. */
1786 }
1787
1788 /* See declaration. */
1789
1790 void
1791 dwarf2_per_bfd::free_cached_comp_units ()
1792 {
1793 dwarf2_per_cu_data *per_cu = read_in_chain;
1794 dwarf2_per_cu_data **last_chain = &read_in_chain;
1795 while (per_cu != NULL)
1796 {
1797 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1798
1799 delete per_cu->cu;
1800 *last_chain = next_cu;
1801 per_cu = next_cu;
1802 }
1803 }
1804
1805 /* A helper class that calls free_cached_comp_units on
1806 destruction. */
1807
1808 class free_cached_comp_units
1809 {
1810 public:
1811
1812 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1813 : m_per_objfile (per_objfile)
1814 {
1815 }
1816
1817 ~free_cached_comp_units ()
1818 {
1819 m_per_objfile->per_bfd->free_cached_comp_units ();
1820 }
1821
1822 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1823
1824 private:
1825
1826 dwarf2_per_objfile *m_per_objfile;
1827 };
1828
1829 /* See read.h. */
1830
1831 bool
1832 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1833 {
1834 gdb_assert (per_cu->index < this->m_symtabs.size ());
1835
1836 return this->m_symtabs[per_cu->index] != nullptr;
1837 }
1838
1839 /* See read.h. */
1840
1841 compunit_symtab *
1842 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1843 {
1844 gdb_assert (per_cu->index < this->m_symtabs.size ());
1845
1846 return this->m_symtabs[per_cu->index];
1847 }
1848
1849 /* See read.h. */
1850
1851 void
1852 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1853 compunit_symtab *symtab)
1854 {
1855 gdb_assert (per_cu->index < this->m_symtabs.size ());
1856 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1857
1858 this->m_symtabs[per_cu->index] = symtab;
1859 }
1860
1861 /* Try to locate the sections we need for DWARF 2 debugging
1862 information and return true if we have enough to do something.
1863 NAMES points to the dwarf2 section names, or is NULL if the standard
1864 ELF names are used. CAN_COPY is true for formats where symbol
1865 interposition is possible and so symbol values must follow copy
1866 relocation rules. */
1867
1868 int
1869 dwarf2_has_info (struct objfile *objfile,
1870 const struct dwarf2_debug_sections *names,
1871 bool can_copy)
1872 {
1873 if (objfile->flags & OBJF_READNEVER)
1874 return 0;
1875
1876 struct dwarf2_per_objfile *dwarf2_per_objfile
1877 = get_dwarf2_per_objfile (objfile);
1878
1879 if (dwarf2_per_objfile == NULL)
1880 {
1881 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1882 sharing yet). */
1883 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1884
1885 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1886 }
1887
1888 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1889 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1890 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1891 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1892 }
1893
1894 /* When loading sections, we look either for uncompressed section or for
1895 compressed section names. */
1896
1897 static int
1898 section_is_p (const char *section_name,
1899 const struct dwarf2_section_names *names)
1900 {
1901 if (names->normal != NULL
1902 && strcmp (section_name, names->normal) == 0)
1903 return 1;
1904 if (names->compressed != NULL
1905 && strcmp (section_name, names->compressed) == 0)
1906 return 1;
1907 return 0;
1908 }
1909
1910 /* See declaration. */
1911
1912 void
1913 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1914 const dwarf2_debug_sections &names)
1915 {
1916 flagword aflag = bfd_section_flags (sectp);
1917
1918 if ((aflag & SEC_HAS_CONTENTS) == 0)
1919 {
1920 }
1921 else if (elf_section_data (sectp)->this_hdr.sh_size
1922 > bfd_get_file_size (abfd))
1923 {
1924 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1925 warning (_("Discarding section %s which has a section size (%s"
1926 ") larger than the file size [in module %s]"),
1927 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1928 bfd_get_filename (abfd));
1929 }
1930 else if (section_is_p (sectp->name, &names.info))
1931 {
1932 this->info.s.section = sectp;
1933 this->info.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.abbrev))
1936 {
1937 this->abbrev.s.section = sectp;
1938 this->abbrev.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.line))
1941 {
1942 this->line.s.section = sectp;
1943 this->line.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.loc))
1946 {
1947 this->loc.s.section = sectp;
1948 this->loc.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.loclists))
1951 {
1952 this->loclists.s.section = sectp;
1953 this->loclists.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.macinfo))
1956 {
1957 this->macinfo.s.section = sectp;
1958 this->macinfo.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.macro))
1961 {
1962 this->macro.s.section = sectp;
1963 this->macro.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.str))
1966 {
1967 this->str.s.section = sectp;
1968 this->str.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.str_offsets))
1971 {
1972 this->str_offsets.s.section = sectp;
1973 this->str_offsets.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.line_str))
1976 {
1977 this->line_str.s.section = sectp;
1978 this->line_str.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.addr))
1981 {
1982 this->addr.s.section = sectp;
1983 this->addr.size = bfd_section_size (sectp);
1984 }
1985 else if (section_is_p (sectp->name, &names.frame))
1986 {
1987 this->frame.s.section = sectp;
1988 this->frame.size = bfd_section_size (sectp);
1989 }
1990 else if (section_is_p (sectp->name, &names.eh_frame))
1991 {
1992 this->eh_frame.s.section = sectp;
1993 this->eh_frame.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.ranges))
1996 {
1997 this->ranges.s.section = sectp;
1998 this->ranges.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.rnglists))
2001 {
2002 this->rnglists.s.section = sectp;
2003 this->rnglists.size = bfd_section_size (sectp);
2004 }
2005 else if (section_is_p (sectp->name, &names.types))
2006 {
2007 struct dwarf2_section_info type_section;
2008
2009 memset (&type_section, 0, sizeof (type_section));
2010 type_section.s.section = sectp;
2011 type_section.size = bfd_section_size (sectp);
2012
2013 this->types.push_back (type_section);
2014 }
2015 else if (section_is_p (sectp->name, &names.gdb_index))
2016 {
2017 this->gdb_index.s.section = sectp;
2018 this->gdb_index.size = bfd_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &names.debug_names))
2021 {
2022 this->debug_names.s.section = sectp;
2023 this->debug_names.size = bfd_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &names.debug_aranges))
2026 {
2027 this->debug_aranges.s.section = sectp;
2028 this->debug_aranges.size = bfd_section_size (sectp);
2029 }
2030
2031 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2032 && bfd_section_vma (sectp) == 0)
2033 this->has_section_at_zero = true;
2034 }
2035
2036 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2037 SECTION_NAME. */
2038
2039 void
2040 dwarf2_get_section_info (struct objfile *objfile,
2041 enum dwarf2_section_enum sect,
2042 asection **sectp, const gdb_byte **bufp,
2043 bfd_size_type *sizep)
2044 {
2045 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2046 struct dwarf2_section_info *info;
2047
2048 /* We may see an objfile without any DWARF, in which case we just
2049 return nothing. */
2050 if (data == NULL)
2051 {
2052 *sectp = NULL;
2053 *bufp = NULL;
2054 *sizep = 0;
2055 return;
2056 }
2057 switch (sect)
2058 {
2059 case DWARF2_DEBUG_FRAME:
2060 info = &data->per_bfd->frame;
2061 break;
2062 case DWARF2_EH_FRAME:
2063 info = &data->per_bfd->eh_frame;
2064 break;
2065 default:
2066 gdb_assert_not_reached ("unexpected section");
2067 }
2068
2069 info->read (objfile);
2070
2071 *sectp = info->get_bfd_section ();
2072 *bufp = info->buffer;
2073 *sizep = info->size;
2074 }
2075
2076 /* A helper function to find the sections for a .dwz file. */
2077
2078 static void
2079 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2080 {
2081 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2082
2083 /* Note that we only support the standard ELF names, because .dwz
2084 is ELF-only (at the time of writing). */
2085 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2086 {
2087 dwz_file->abbrev.s.section = sectp;
2088 dwz_file->abbrev.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2091 {
2092 dwz_file->info.s.section = sectp;
2093 dwz_file->info.size = bfd_section_size (sectp);
2094 }
2095 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2096 {
2097 dwz_file->str.s.section = sectp;
2098 dwz_file->str.size = bfd_section_size (sectp);
2099 }
2100 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2101 {
2102 dwz_file->line.s.section = sectp;
2103 dwz_file->line.size = bfd_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2106 {
2107 dwz_file->macro.s.section = sectp;
2108 dwz_file->macro.size = bfd_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2111 {
2112 dwz_file->gdb_index.s.section = sectp;
2113 dwz_file->gdb_index.size = bfd_section_size (sectp);
2114 }
2115 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2116 {
2117 dwz_file->debug_names.s.section = sectp;
2118 dwz_file->debug_names.size = bfd_section_size (sectp);
2119 }
2120 }
2121
2122 /* See dwarf2read.h. */
2123
2124 struct dwz_file *
2125 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2126 {
2127 const char *filename;
2128 bfd_size_type buildid_len_arg;
2129 size_t buildid_len;
2130 bfd_byte *buildid;
2131
2132 if (per_bfd->dwz_file != NULL)
2133 return per_bfd->dwz_file.get ();
2134
2135 bfd_set_error (bfd_error_no_error);
2136 gdb::unique_xmalloc_ptr<char> data
2137 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2138 &buildid_len_arg, &buildid));
2139 if (data == NULL)
2140 {
2141 if (bfd_get_error () == bfd_error_no_error)
2142 return NULL;
2143 error (_("could not read '.gnu_debugaltlink' section: %s"),
2144 bfd_errmsg (bfd_get_error ()));
2145 }
2146
2147 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2148
2149 buildid_len = (size_t) buildid_len_arg;
2150
2151 filename = data.get ();
2152
2153 std::string abs_storage;
2154 if (!IS_ABSOLUTE_PATH (filename))
2155 {
2156 gdb::unique_xmalloc_ptr<char> abs
2157 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2158
2159 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2160 filename = abs_storage.c_str ();
2161 }
2162
2163 /* First try the file name given in the section. If that doesn't
2164 work, try to use the build-id instead. */
2165 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2166 if (dwz_bfd != NULL)
2167 {
2168 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2169 dwz_bfd.reset (nullptr);
2170 }
2171
2172 if (dwz_bfd == NULL)
2173 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2174
2175 if (dwz_bfd == nullptr)
2176 {
2177 gdb::unique_xmalloc_ptr<char> alt_filename;
2178 const char *origname = bfd_get_filename (per_bfd->obfd);
2179
2180 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2181 buildid_len,
2182 origname,
2183 &alt_filename));
2184
2185 if (fd.get () >= 0)
2186 {
2187 /* File successfully retrieved from server. */
2188 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2189
2190 if (dwz_bfd == nullptr)
2191 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2192 alt_filename.get ());
2193 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2194 dwz_bfd.reset (nullptr);
2195 }
2196 }
2197
2198 if (dwz_bfd == NULL)
2199 error (_("could not find '.gnu_debugaltlink' file for %s"),
2200 bfd_get_filename (per_bfd->obfd));
2201
2202 std::unique_ptr<struct dwz_file> result
2203 (new struct dwz_file (std::move (dwz_bfd)));
2204
2205 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2206 result.get ());
2207
2208 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2209 per_bfd->dwz_file = std::move (result);
2210 return per_bfd->dwz_file.get ();
2211 }
2212 \f
2213 /* DWARF quick_symbols_functions support. */
2214
2215 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2216 unique line tables, so we maintain a separate table of all .debug_line
2217 derived entries to support the sharing.
2218 All the quick functions need is the list of file names. We discard the
2219 line_header when we're done and don't need to record it here. */
2220 struct quick_file_names
2221 {
2222 /* The data used to construct the hash key. */
2223 struct stmt_list_hash hash;
2224
2225 /* The number of entries in file_names, real_names. */
2226 unsigned int num_file_names;
2227
2228 /* The file names from the line table, after being run through
2229 file_full_name. */
2230 const char **file_names;
2231
2232 /* The file names from the line table after being run through
2233 gdb_realpath. These are computed lazily. */
2234 const char **real_names;
2235 };
2236
2237 /* When using the index (and thus not using psymtabs), each CU has an
2238 object of this type. This is used to hold information needed by
2239 the various "quick" methods. */
2240 struct dwarf2_per_cu_quick_data
2241 {
2242 /* The file table. This can be NULL if there was no file table
2243 or it's currently not read in.
2244 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2245 struct quick_file_names *file_names;
2246
2247 /* A temporary mark bit used when iterating over all CUs in
2248 expand_symtabs_matching. */
2249 unsigned int mark : 1;
2250
2251 /* True if we've tried to read the file table and found there isn't one.
2252 There will be no point in trying to read it again next time. */
2253 unsigned int no_file_data : 1;
2254 };
2255
2256 /* Utility hash function for a stmt_list_hash. */
2257
2258 static hashval_t
2259 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2260 {
2261 hashval_t v = 0;
2262
2263 if (stmt_list_hash->dwo_unit != NULL)
2264 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2265 v += to_underlying (stmt_list_hash->line_sect_off);
2266 return v;
2267 }
2268
2269 /* Utility equality function for a stmt_list_hash. */
2270
2271 static int
2272 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2273 const struct stmt_list_hash *rhs)
2274 {
2275 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2276 return 0;
2277 if (lhs->dwo_unit != NULL
2278 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2279 return 0;
2280
2281 return lhs->line_sect_off == rhs->line_sect_off;
2282 }
2283
2284 /* Hash function for a quick_file_names. */
2285
2286 static hashval_t
2287 hash_file_name_entry (const void *e)
2288 {
2289 const struct quick_file_names *file_data
2290 = (const struct quick_file_names *) e;
2291
2292 return hash_stmt_list_entry (&file_data->hash);
2293 }
2294
2295 /* Equality function for a quick_file_names. */
2296
2297 static int
2298 eq_file_name_entry (const void *a, const void *b)
2299 {
2300 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2301 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2302
2303 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2304 }
2305
2306 /* Delete function for a quick_file_names. */
2307
2308 static void
2309 delete_file_name_entry (void *e)
2310 {
2311 struct quick_file_names *file_data = (struct quick_file_names *) e;
2312 int i;
2313
2314 for (i = 0; i < file_data->num_file_names; ++i)
2315 {
2316 xfree ((void*) file_data->file_names[i]);
2317 if (file_data->real_names)
2318 xfree ((void*) file_data->real_names[i]);
2319 }
2320
2321 /* The space for the struct itself lives on the obstack, so we don't
2322 free it here. */
2323 }
2324
2325 /* Create a quick_file_names hash table. */
2326
2327 static htab_up
2328 create_quick_file_names_table (unsigned int nr_initial_entries)
2329 {
2330 return htab_up (htab_create_alloc (nr_initial_entries,
2331 hash_file_name_entry, eq_file_name_entry,
2332 delete_file_name_entry, xcalloc, xfree));
2333 }
2334
2335 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2336 function is unrelated to symtabs, symtab would have to be created afterwards.
2337 You should call age_cached_comp_units after processing the CU. */
2338
2339 static void
2340 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2341 bool skip_partial)
2342 {
2343 if (per_cu->is_debug_types)
2344 load_full_type_unit (per_cu, per_objfile);
2345 else
2346 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2347
2348 if (per_cu->cu == NULL)
2349 return; /* Dummy CU. */
2350
2351 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
2352 }
2353
2354 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2355
2356 static void
2357 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2358 dwarf2_per_objfile *dwarf2_per_objfile,
2359 bool skip_partial)
2360 {
2361 /* Skip type_unit_groups, reading the type units they contain
2362 is handled elsewhere. */
2363 if (per_cu->type_unit_group_p ())
2364 return;
2365
2366 /* The destructor of dwarf2_queue_guard frees any entries left on
2367 the queue. After this point we're guaranteed to leave this function
2368 with the dwarf queue empty. */
2369 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2370
2371 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2372 {
2373 queue_comp_unit (per_cu, language_minimal);
2374 load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2375
2376 /* If we just loaded a CU from a DWO, and we're working with an index
2377 that may badly handle TUs, load all the TUs in that DWO as well.
2378 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2379 if (!per_cu->is_debug_types
2380 && per_cu->cu != NULL
2381 && per_cu->cu->dwo_unit != NULL
2382 && dwarf2_per_objfile->per_bfd->index_table != NULL
2383 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2384 /* DWP files aren't supported yet. */
2385 && get_dwp_file (dwarf2_per_objfile) == NULL)
2386 queue_and_load_all_dwo_tus (per_cu);
2387 }
2388
2389 process_queue (dwarf2_per_objfile);
2390
2391 /* Age the cache, releasing compilation units that have not
2392 been used recently. */
2393 age_cached_comp_units (dwarf2_per_objfile);
2394 }
2395
2396 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2397 the per-objfile for which this symtab is instantiated.
2398
2399 Returns the resulting symbol table. */
2400
2401 static struct compunit_symtab *
2402 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2403 dwarf2_per_objfile *dwarf2_per_objfile,
2404 bool skip_partial)
2405 {
2406 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2407
2408 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2409 {
2410 free_cached_comp_units freer (dwarf2_per_objfile);
2411 scoped_restore decrementer = increment_reading_symtab ();
2412 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2413 process_cu_includes (dwarf2_per_objfile);
2414 }
2415
2416 return dwarf2_per_objfile->get_symtab (per_cu);
2417 }
2418
2419 /* See declaration. */
2420
2421 dwarf2_per_cu_data *
2422 dwarf2_per_bfd::get_cutu (int index)
2423 {
2424 if (index >= this->all_comp_units.size ())
2425 {
2426 index -= this->all_comp_units.size ();
2427 gdb_assert (index < this->all_type_units.size ());
2428 return &this->all_type_units[index]->per_cu;
2429 }
2430
2431 return this->all_comp_units[index];
2432 }
2433
2434 /* See declaration. */
2435
2436 dwarf2_per_cu_data *
2437 dwarf2_per_bfd::get_cu (int index)
2438 {
2439 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2440
2441 return this->all_comp_units[index];
2442 }
2443
2444 /* See declaration. */
2445
2446 signatured_type *
2447 dwarf2_per_bfd::get_tu (int index)
2448 {
2449 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2450
2451 return this->all_type_units[index];
2452 }
2453
2454 /* See read.h. */
2455
2456 dwarf2_per_cu_data *
2457 dwarf2_per_bfd::allocate_per_cu ()
2458 {
2459 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2460 result->per_bfd = this;
2461 result->index = m_num_psymtabs++;
2462 return result;
2463 }
2464
2465 /* See read.h. */
2466
2467 signatured_type *
2468 dwarf2_per_bfd::allocate_signatured_type ()
2469 {
2470 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2471 result->per_cu.per_bfd = this;
2472 result->per_cu.index = m_num_psymtabs++;
2473 return result;
2474 }
2475
2476 /* Return a new dwarf2_per_cu_data allocated on the dwarf2_per_objfile
2477 obstack, and constructed with the specified field values. */
2478
2479 static dwarf2_per_cu_data *
2480 create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2481 struct dwarf2_section_info *section,
2482 int is_dwz,
2483 sect_offset sect_off, ULONGEST length)
2484 {
2485 dwarf2_per_cu_data *the_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
2486 the_cu->sect_off = sect_off;
2487 the_cu->length = length;
2488 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
2489 the_cu->section = section;
2490 the_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2491 struct dwarf2_per_cu_quick_data);
2492 the_cu->is_dwz = is_dwz;
2493 return the_cu;
2494 }
2495
2496 /* A helper for create_cus_from_index that handles a given list of
2497 CUs. */
2498
2499 static void
2500 create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2501 const gdb_byte *cu_list, offset_type n_elements,
2502 struct dwarf2_section_info *section,
2503 int is_dwz)
2504 {
2505 for (offset_type i = 0; i < n_elements; i += 2)
2506 {
2507 gdb_static_assert (sizeof (ULONGEST) >= 8);
2508
2509 sect_offset sect_off
2510 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2511 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2512 cu_list += 2 * 8;
2513
2514 dwarf2_per_cu_data *per_cu
2515 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2516 sect_off, length);
2517 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
2518 }
2519 }
2520
2521 /* Read the CU list from the mapped index, and use it to create all
2522 the CU objects for this objfile. */
2523
2524 static void
2525 create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2526 const gdb_byte *cu_list, offset_type cu_list_elements,
2527 const gdb_byte *dwz_list, offset_type dwz_elements)
2528 {
2529 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
2530 dwarf2_per_objfile->per_bfd->all_comp_units.reserve
2531 ((cu_list_elements + dwz_elements) / 2);
2532
2533 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
2534 &dwarf2_per_objfile->per_bfd->info, 0);
2535
2536 if (dwz_elements == 0)
2537 return;
2538
2539 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
2540 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
2541 &dwz->info, 1);
2542 }
2543
2544 /* Create the signatured type hash table from the index. */
2545
2546 static void
2547 create_signatured_type_table_from_index
2548 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2549 struct dwarf2_section_info *section,
2550 const gdb_byte *bytes,
2551 offset_type elements)
2552 {
2553 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2554 dwarf2_per_objfile->per_bfd->all_type_units.reserve (elements / 3);
2555
2556 htab_up sig_types_hash = allocate_signatured_type_table ();
2557
2558 for (offset_type i = 0; i < elements; i += 3)
2559 {
2560 struct signatured_type *sig_type;
2561 ULONGEST signature;
2562 void **slot;
2563 cu_offset type_offset_in_tu;
2564
2565 gdb_static_assert (sizeof (ULONGEST) >= 8);
2566 sect_offset sect_off
2567 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2568 type_offset_in_tu
2569 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2570 BFD_ENDIAN_LITTLE);
2571 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2572 bytes += 3 * 8;
2573
2574 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2575 sig_type->signature = signature;
2576 sig_type->type_offset_in_tu = type_offset_in_tu;
2577 sig_type->per_cu.is_debug_types = 1;
2578 sig_type->per_cu.section = section;
2579 sig_type->per_cu.sect_off = sect_off;
2580 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2581 sig_type->per_cu.v.quick
2582 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2583 struct dwarf2_per_cu_quick_data);
2584
2585 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2586 *slot = sig_type;
2587
2588 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2589 }
2590
2591 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2592 }
2593
2594 /* Create the signatured type hash table from .debug_names. */
2595
2596 static void
2597 create_signatured_type_table_from_debug_names
2598 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2599 const mapped_debug_names &map,
2600 struct dwarf2_section_info *section,
2601 struct dwarf2_section_info *abbrev_section)
2602 {
2603 struct objfile *objfile = dwarf2_per_objfile->objfile;
2604
2605 section->read (objfile);
2606 abbrev_section->read (objfile);
2607
2608 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2609 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2610
2611 htab_up sig_types_hash = allocate_signatured_type_table ();
2612
2613 for (uint32_t i = 0; i < map.tu_count; ++i)
2614 {
2615 struct signatured_type *sig_type;
2616 void **slot;
2617
2618 sect_offset sect_off
2619 = (sect_offset) (extract_unsigned_integer
2620 (map.tu_table_reordered + i * map.offset_size,
2621 map.offset_size,
2622 map.dwarf5_byte_order));
2623
2624 comp_unit_head cu_header;
2625 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2626 abbrev_section,
2627 section->buffer + to_underlying (sect_off),
2628 rcuh_kind::TYPE);
2629
2630 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2631 sig_type->signature = cu_header.signature;
2632 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2633 sig_type->per_cu.is_debug_types = 1;
2634 sig_type->per_cu.section = section;
2635 sig_type->per_cu.sect_off = sect_off;
2636 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
2637 sig_type->per_cu.v.quick
2638 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2639 struct dwarf2_per_cu_quick_data);
2640
2641 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2642 *slot = sig_type;
2643
2644 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2645 }
2646
2647 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2648 }
2649
2650 /* Read the address map data from the mapped index, and use it to
2651 populate the objfile's psymtabs_addrmap. */
2652
2653 static void
2654 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2655 struct mapped_index *index)
2656 {
2657 struct objfile *objfile = dwarf2_per_objfile->objfile;
2658 struct gdbarch *gdbarch = objfile->arch ();
2659 const gdb_byte *iter, *end;
2660 struct addrmap *mutable_map;
2661 CORE_ADDR baseaddr;
2662
2663 auto_obstack temp_obstack;
2664
2665 mutable_map = addrmap_create_mutable (&temp_obstack);
2666
2667 iter = index->address_table.data ();
2668 end = iter + index->address_table.size ();
2669
2670 baseaddr = objfile->text_section_offset ();
2671
2672 while (iter < end)
2673 {
2674 ULONGEST hi, lo, cu_index;
2675 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2676 iter += 8;
2677 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2678 iter += 8;
2679 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2680 iter += 4;
2681
2682 if (lo > hi)
2683 {
2684 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2685 hex_string (lo), hex_string (hi));
2686 continue;
2687 }
2688
2689 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2690 {
2691 complaint (_(".gdb_index address table has invalid CU number %u"),
2692 (unsigned) cu_index);
2693 continue;
2694 }
2695
2696 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2697 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2698 addrmap_set_empty (mutable_map, lo, hi - 1,
2699 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2700 }
2701
2702 objfile->partial_symtabs->psymtabs_addrmap
2703 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2704 }
2705
2706 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2707 populate the objfile's psymtabs_addrmap. */
2708
2709 static void
2710 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2711 struct dwarf2_section_info *section)
2712 {
2713 struct objfile *objfile = dwarf2_per_objfile->objfile;
2714 bfd *abfd = objfile->obfd;
2715 struct gdbarch *gdbarch = objfile->arch ();
2716 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2717
2718 auto_obstack temp_obstack;
2719 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2720
2721 std::unordered_map<sect_offset,
2722 dwarf2_per_cu_data *,
2723 gdb::hash_enum<sect_offset>>
2724 debug_info_offset_to_per_cu;
2725 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2726 {
2727 const auto insertpair
2728 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2729 if (!insertpair.second)
2730 {
2731 warning (_("Section .debug_aranges in %s has duplicate "
2732 "debug_info_offset %s, ignoring .debug_aranges."),
2733 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2734 return;
2735 }
2736 }
2737
2738 section->read (objfile);
2739
2740 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2741
2742 const gdb_byte *addr = section->buffer;
2743
2744 while (addr < section->buffer + section->size)
2745 {
2746 const gdb_byte *const entry_addr = addr;
2747 unsigned int bytes_read;
2748
2749 const LONGEST entry_length = read_initial_length (abfd, addr,
2750 &bytes_read);
2751 addr += bytes_read;
2752
2753 const gdb_byte *const entry_end = addr + entry_length;
2754 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2755 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2756 if (addr + entry_length > section->buffer + section->size)
2757 {
2758 warning (_("Section .debug_aranges in %s entry at offset %s "
2759 "length %s exceeds section length %s, "
2760 "ignoring .debug_aranges."),
2761 objfile_name (objfile),
2762 plongest (entry_addr - section->buffer),
2763 plongest (bytes_read + entry_length),
2764 pulongest (section->size));
2765 return;
2766 }
2767
2768 /* The version number. */
2769 const uint16_t version = read_2_bytes (abfd, addr);
2770 addr += 2;
2771 if (version != 2)
2772 {
2773 warning (_("Section .debug_aranges in %s entry at offset %s "
2774 "has unsupported version %d, ignoring .debug_aranges."),
2775 objfile_name (objfile),
2776 plongest (entry_addr - section->buffer), version);
2777 return;
2778 }
2779
2780 const uint64_t debug_info_offset
2781 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2782 addr += offset_size;
2783 const auto per_cu_it
2784 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2785 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2786 {
2787 warning (_("Section .debug_aranges in %s entry at offset %s "
2788 "debug_info_offset %s does not exists, "
2789 "ignoring .debug_aranges."),
2790 objfile_name (objfile),
2791 plongest (entry_addr - section->buffer),
2792 pulongest (debug_info_offset));
2793 return;
2794 }
2795 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2796
2797 const uint8_t address_size = *addr++;
2798 if (address_size < 1 || address_size > 8)
2799 {
2800 warning (_("Section .debug_aranges in %s entry at offset %s "
2801 "address_size %u is invalid, ignoring .debug_aranges."),
2802 objfile_name (objfile),
2803 plongest (entry_addr - section->buffer), address_size);
2804 return;
2805 }
2806
2807 const uint8_t segment_selector_size = *addr++;
2808 if (segment_selector_size != 0)
2809 {
2810 warning (_("Section .debug_aranges in %s entry at offset %s "
2811 "segment_selector_size %u is not supported, "
2812 "ignoring .debug_aranges."),
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer),
2815 segment_selector_size);
2816 return;
2817 }
2818
2819 /* Must pad to an alignment boundary that is twice the address
2820 size. It is undocumented by the DWARF standard but GCC does
2821 use it. */
2822 for (size_t padding = ((-(addr - section->buffer))
2823 & (2 * address_size - 1));
2824 padding > 0; padding--)
2825 if (*addr++ != 0)
2826 {
2827 warning (_("Section .debug_aranges in %s entry at offset %s "
2828 "padding is not zero, ignoring .debug_aranges."),
2829 objfile_name (objfile),
2830 plongest (entry_addr - section->buffer));
2831 return;
2832 }
2833
2834 for (;;)
2835 {
2836 if (addr + 2 * address_size > entry_end)
2837 {
2838 warning (_("Section .debug_aranges in %s entry at offset %s "
2839 "address list is not properly terminated, "
2840 "ignoring .debug_aranges."),
2841 objfile_name (objfile),
2842 plongest (entry_addr - section->buffer));
2843 return;
2844 }
2845 ULONGEST start = extract_unsigned_integer (addr, address_size,
2846 dwarf5_byte_order);
2847 addr += address_size;
2848 ULONGEST length = extract_unsigned_integer (addr, address_size,
2849 dwarf5_byte_order);
2850 addr += address_size;
2851 if (start == 0 && length == 0)
2852 break;
2853 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2854 {
2855 /* Symbol was eliminated due to a COMDAT group. */
2856 continue;
2857 }
2858 ULONGEST end = start + length;
2859 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2860 - baseaddr);
2861 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2862 - baseaddr);
2863 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2864 }
2865 }
2866
2867 objfile->partial_symtabs->psymtabs_addrmap
2868 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2869 }
2870
2871 /* Find a slot in the mapped index INDEX for the object named NAME.
2872 If NAME is found, set *VEC_OUT to point to the CU vector in the
2873 constant pool and return true. If NAME cannot be found, return
2874 false. */
2875
2876 static bool
2877 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2878 offset_type **vec_out)
2879 {
2880 offset_type hash;
2881 offset_type slot, step;
2882 int (*cmp) (const char *, const char *);
2883
2884 gdb::unique_xmalloc_ptr<char> without_params;
2885 if (current_language->la_language == language_cplus
2886 || current_language->la_language == language_fortran
2887 || current_language->la_language == language_d)
2888 {
2889 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2890 not contain any. */
2891
2892 if (strchr (name, '(') != NULL)
2893 {
2894 without_params = cp_remove_params (name);
2895
2896 if (without_params != NULL)
2897 name = without_params.get ();
2898 }
2899 }
2900
2901 /* Index version 4 did not support case insensitive searches. But the
2902 indices for case insensitive languages are built in lowercase, therefore
2903 simulate our NAME being searched is also lowercased. */
2904 hash = mapped_index_string_hash ((index->version == 4
2905 && case_sensitivity == case_sensitive_off
2906 ? 5 : index->version),
2907 name);
2908
2909 slot = hash & (index->symbol_table.size () - 1);
2910 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2911 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2912
2913 for (;;)
2914 {
2915 const char *str;
2916
2917 const auto &bucket = index->symbol_table[slot];
2918 if (bucket.name == 0 && bucket.vec == 0)
2919 return false;
2920
2921 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2922 if (!cmp (name, str))
2923 {
2924 *vec_out = (offset_type *) (index->constant_pool
2925 + MAYBE_SWAP (bucket.vec));
2926 return true;
2927 }
2928
2929 slot = (slot + step) & (index->symbol_table.size () - 1);
2930 }
2931 }
2932
2933 /* A helper function that reads the .gdb_index from BUFFER and fills
2934 in MAP. FILENAME is the name of the file containing the data;
2935 it is used for error reporting. DEPRECATED_OK is true if it is
2936 ok to use deprecated sections.
2937
2938 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2939 out parameters that are filled in with information about the CU and
2940 TU lists in the section.
2941
2942 Returns true if all went well, false otherwise. */
2943
2944 static bool
2945 read_gdb_index_from_buffer (const char *filename,
2946 bool deprecated_ok,
2947 gdb::array_view<const gdb_byte> buffer,
2948 struct mapped_index *map,
2949 const gdb_byte **cu_list,
2950 offset_type *cu_list_elements,
2951 const gdb_byte **types_list,
2952 offset_type *types_list_elements)
2953 {
2954 const gdb_byte *addr = &buffer[0];
2955
2956 /* Version check. */
2957 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2958 /* Versions earlier than 3 emitted every copy of a psymbol. This
2959 causes the index to behave very poorly for certain requests. Version 3
2960 contained incomplete addrmap. So, it seems better to just ignore such
2961 indices. */
2962 if (version < 4)
2963 {
2964 static int warning_printed = 0;
2965 if (!warning_printed)
2966 {
2967 warning (_("Skipping obsolete .gdb_index section in %s."),
2968 filename);
2969 warning_printed = 1;
2970 }
2971 return 0;
2972 }
2973 /* Index version 4 uses a different hash function than index version
2974 5 and later.
2975
2976 Versions earlier than 6 did not emit psymbols for inlined
2977 functions. Using these files will cause GDB not to be able to
2978 set breakpoints on inlined functions by name, so we ignore these
2979 indices unless the user has done
2980 "set use-deprecated-index-sections on". */
2981 if (version < 6 && !deprecated_ok)
2982 {
2983 static int warning_printed = 0;
2984 if (!warning_printed)
2985 {
2986 warning (_("\
2987 Skipping deprecated .gdb_index section in %s.\n\
2988 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2989 to use the section anyway."),
2990 filename);
2991 warning_printed = 1;
2992 }
2993 return 0;
2994 }
2995 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2996 of the TU (for symbols coming from TUs),
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2998 Plus gold-generated indices can have duplicate entries for global symbols,
2999 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3000 These are just performance bugs, and we can't distinguish gdb-generated
3001 indices from gold-generated ones, so issue no warning here. */
3002
3003 /* Indexes with higher version than the one supported by GDB may be no
3004 longer backward compatible. */
3005 if (version > 8)
3006 return 0;
3007
3008 map->version = version;
3009
3010 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3011
3012 int i = 0;
3013 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3014 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3015 / 8);
3016 ++i;
3017
3018 *types_list = addr + MAYBE_SWAP (metadata[i]);
3019 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3020 - MAYBE_SWAP (metadata[i]))
3021 / 8);
3022 ++i;
3023
3024 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3025 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3026 map->address_table
3027 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3028 ++i;
3029
3030 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->symbol_table
3033 = gdb::array_view<mapped_index::symbol_table_slot>
3034 ((mapped_index::symbol_table_slot *) symbol_table,
3035 (mapped_index::symbol_table_slot *) symbol_table_end);
3036
3037 ++i;
3038 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3039
3040 return 1;
3041 }
3042
3043 /* Callback types for dwarf2_read_gdb_index. */
3044
3045 typedef gdb::function_view
3046 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3047 get_gdb_index_contents_ftype;
3048 typedef gdb::function_view
3049 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3050 get_gdb_index_contents_dwz_ftype;
3051
3052 /* Read .gdb_index. If everything went ok, initialize the "quick"
3053 elements of all the CUs and return 1. Otherwise, return 0. */
3054
3055 static int
3056 dwarf2_read_gdb_index
3057 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3058 get_gdb_index_contents_ftype get_gdb_index_contents,
3059 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3060 {
3061 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3062 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3063 struct dwz_file *dwz;
3064 struct objfile *objfile = dwarf2_per_objfile->objfile;
3065
3066 gdb::array_view<const gdb_byte> main_index_contents
3067 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3068
3069 if (main_index_contents.empty ())
3070 return 0;
3071
3072 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3073 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3074 use_deprecated_index_sections,
3075 main_index_contents, map.get (), &cu_list,
3076 &cu_list_elements, &types_list,
3077 &types_list_elements))
3078 return 0;
3079
3080 /* Don't use the index if it's empty. */
3081 if (map->symbol_table.empty ())
3082 return 0;
3083
3084 /* If there is a .dwz file, read it so we can get its CU list as
3085 well. */
3086 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3087 if (dwz != NULL)
3088 {
3089 struct mapped_index dwz_map;
3090 const gdb_byte *dwz_types_ignore;
3091 offset_type dwz_types_elements_ignore;
3092
3093 gdb::array_view<const gdb_byte> dwz_index_content
3094 = get_gdb_index_contents_dwz (objfile, dwz);
3095
3096 if (dwz_index_content.empty ())
3097 return 0;
3098
3099 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3100 1, dwz_index_content, &dwz_map,
3101 &dwz_list, &dwz_list_elements,
3102 &dwz_types_ignore,
3103 &dwz_types_elements_ignore))
3104 {
3105 warning (_("could not read '.gdb_index' section from %s; skipping"),
3106 bfd_get_filename (dwz->dwz_bfd.get ()));
3107 return 0;
3108 }
3109 }
3110
3111 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3112 dwz_list, dwz_list_elements);
3113
3114 if (types_list_elements)
3115 {
3116 /* We can only handle a single .debug_types when we have an
3117 index. */
3118 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3119 return 0;
3120
3121 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3122
3123 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3124 types_list, types_list_elements);
3125 }
3126
3127 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3128
3129 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3130 dwarf2_per_objfile->per_bfd->using_index = 1;
3131 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3132 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3133
3134 return 1;
3135 }
3136
3137 /* die_reader_func for dw2_get_file_names. */
3138
3139 static void
3140 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3141 const gdb_byte *info_ptr,
3142 struct die_info *comp_unit_die)
3143 {
3144 struct dwarf2_cu *cu = reader->cu;
3145 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3146 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3147 struct dwarf2_per_cu_data *lh_cu;
3148 struct attribute *attr;
3149 void **slot;
3150 struct quick_file_names *qfn;
3151
3152 gdb_assert (! this_cu->is_debug_types);
3153
3154 /* Our callers never want to match partial units -- instead they
3155 will match the enclosing full CU. */
3156 if (comp_unit_die->tag == DW_TAG_partial_unit)
3157 {
3158 this_cu->v.quick->no_file_data = 1;
3159 return;
3160 }
3161
3162 lh_cu = this_cu;
3163 slot = NULL;
3164
3165 line_header_up lh;
3166 sect_offset line_offset {};
3167
3168 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3169 if (attr != nullptr)
3170 {
3171 struct quick_file_names find_entry;
3172
3173 line_offset = (sect_offset) DW_UNSND (attr);
3174
3175 /* We may have already read in this line header (TU line header sharing).
3176 If we have we're done. */
3177 find_entry.hash.dwo_unit = cu->dwo_unit;
3178 find_entry.hash.line_sect_off = line_offset;
3179 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3180 &find_entry, INSERT);
3181 if (*slot != NULL)
3182 {
3183 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3184 return;
3185 }
3186
3187 lh = dwarf_decode_line_header (line_offset, cu);
3188 }
3189 if (lh == NULL)
3190 {
3191 lh_cu->v.quick->no_file_data = 1;
3192 return;
3193 }
3194
3195 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3196 qfn->hash.dwo_unit = cu->dwo_unit;
3197 qfn->hash.line_sect_off = line_offset;
3198 gdb_assert (slot != NULL);
3199 *slot = qfn;
3200
3201 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3202
3203 int offset = 0;
3204 if (strcmp (fnd.name, "<unknown>") != 0)
3205 ++offset;
3206
3207 qfn->num_file_names = offset + lh->file_names_size ();
3208 qfn->file_names =
3209 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3210 qfn->num_file_names);
3211 if (offset != 0)
3212 qfn->file_names[0] = xstrdup (fnd.name);
3213 for (int i = 0; i < lh->file_names_size (); ++i)
3214 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3215 fnd.comp_dir).release ();
3216 qfn->real_names = NULL;
3217
3218 lh_cu->v.quick->file_names = qfn;
3219 }
3220
3221 /* A helper for the "quick" functions which attempts to read the line
3222 table for THIS_CU. */
3223
3224 static struct quick_file_names *
3225 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3226 dwarf2_per_objfile *per_objfile)
3227 {
3228 /* This should never be called for TUs. */
3229 gdb_assert (! this_cu->is_debug_types);
3230 /* Nor type unit groups. */
3231 gdb_assert (! this_cu->type_unit_group_p ());
3232
3233 if (this_cu->v.quick->file_names != NULL)
3234 return this_cu->v.quick->file_names;
3235 /* If we know there is no line data, no point in looking again. */
3236 if (this_cu->v.quick->no_file_data)
3237 return NULL;
3238
3239 cutu_reader reader (this_cu, per_objfile);
3240 if (!reader.dummy_p)
3241 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3242
3243 if (this_cu->v.quick->no_file_data)
3244 return NULL;
3245 return this_cu->v.quick->file_names;
3246 }
3247
3248 /* A helper for the "quick" functions which computes and caches the
3249 real path for a given file name from the line table. */
3250
3251 static const char *
3252 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3253 struct quick_file_names *qfn, int index)
3254 {
3255 if (qfn->real_names == NULL)
3256 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3257 qfn->num_file_names, const char *);
3258
3259 if (qfn->real_names[index] == NULL)
3260 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3261
3262 return qfn->real_names[index];
3263 }
3264
3265 static struct symtab *
3266 dw2_find_last_source_symtab (struct objfile *objfile)
3267 {
3268 struct dwarf2_per_objfile *dwarf2_per_objfile
3269 = get_dwarf2_per_objfile (objfile);
3270 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3271 compunit_symtab *cust
3272 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3273
3274 if (cust == NULL)
3275 return NULL;
3276
3277 return compunit_primary_filetab (cust);
3278 }
3279
3280 /* Traversal function for dw2_forget_cached_source_info. */
3281
3282 static int
3283 dw2_free_cached_file_names (void **slot, void *info)
3284 {
3285 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3286
3287 if (file_data->real_names)
3288 {
3289 int i;
3290
3291 for (i = 0; i < file_data->num_file_names; ++i)
3292 {
3293 xfree ((void*) file_data->real_names[i]);
3294 file_data->real_names[i] = NULL;
3295 }
3296 }
3297
3298 return 1;
3299 }
3300
3301 static void
3302 dw2_forget_cached_source_info (struct objfile *objfile)
3303 {
3304 struct dwarf2_per_objfile *dwarf2_per_objfile
3305 = get_dwarf2_per_objfile (objfile);
3306
3307 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3308 dw2_free_cached_file_names, NULL);
3309 }
3310
3311 /* Helper function for dw2_map_symtabs_matching_filename that expands
3312 the symtabs and calls the iterator. */
3313
3314 static int
3315 dw2_map_expand_apply (struct objfile *objfile,
3316 struct dwarf2_per_cu_data *per_cu,
3317 const char *name, const char *real_path,
3318 gdb::function_view<bool (symtab *)> callback)
3319 {
3320 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3321
3322 /* Don't visit already-expanded CUs. */
3323 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3324 if (per_objfile->symtab_set_p (per_cu))
3325 return 0;
3326
3327 /* This may expand more than one symtab, and we want to iterate over
3328 all of them. */
3329 dw2_instantiate_symtab (per_cu, per_objfile, false);
3330
3331 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3332 last_made, callback);
3333 }
3334
3335 /* Implementation of the map_symtabs_matching_filename method. */
3336
3337 static bool
3338 dw2_map_symtabs_matching_filename
3339 (struct objfile *objfile, const char *name, const char *real_path,
3340 gdb::function_view<bool (symtab *)> callback)
3341 {
3342 const char *name_basename = lbasename (name);
3343 struct dwarf2_per_objfile *dwarf2_per_objfile
3344 = get_dwarf2_per_objfile (objfile);
3345
3346 /* The rule is CUs specify all the files, including those used by
3347 any TU, so there's no need to scan TUs here. */
3348
3349 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3350 {
3351 /* We only need to look at symtabs not already expanded. */
3352 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3353 continue;
3354
3355 quick_file_names *file_data
3356 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3357 if (file_data == NULL)
3358 continue;
3359
3360 for (int j = 0; j < file_data->num_file_names; ++j)
3361 {
3362 const char *this_name = file_data->file_names[j];
3363 const char *this_real_name;
3364
3365 if (compare_filenames_for_search (this_name, name))
3366 {
3367 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3368 callback))
3369 return true;
3370 continue;
3371 }
3372
3373 /* Before we invoke realpath, which can get expensive when many
3374 files are involved, do a quick comparison of the basenames. */
3375 if (! basenames_may_differ
3376 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3377 continue;
3378
3379 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3380 file_data, j);
3381 if (compare_filenames_for_search (this_real_name, name))
3382 {
3383 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3384 callback))
3385 return true;
3386 continue;
3387 }
3388
3389 if (real_path != NULL)
3390 {
3391 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3392 gdb_assert (IS_ABSOLUTE_PATH (name));
3393 if (this_real_name != NULL
3394 && FILENAME_CMP (real_path, this_real_name) == 0)
3395 {
3396 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3397 callback))
3398 return true;
3399 continue;
3400 }
3401 }
3402 }
3403 }
3404
3405 return false;
3406 }
3407
3408 /* Struct used to manage iterating over all CUs looking for a symbol. */
3409
3410 struct dw2_symtab_iterator
3411 {
3412 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3413 struct dwarf2_per_objfile *dwarf2_per_objfile;
3414 /* If set, only look for symbols that match that block. Valid values are
3415 GLOBAL_BLOCK and STATIC_BLOCK. */
3416 gdb::optional<block_enum> block_index;
3417 /* The kind of symbol we're looking for. */
3418 domain_enum domain;
3419 /* The list of CUs from the index entry of the symbol,
3420 or NULL if not found. */
3421 offset_type *vec;
3422 /* The next element in VEC to look at. */
3423 int next;
3424 /* The number of elements in VEC, or zero if there is no match. */
3425 int length;
3426 /* Have we seen a global version of the symbol?
3427 If so we can ignore all further global instances.
3428 This is to work around gold/15646, inefficient gold-generated
3429 indices. */
3430 int global_seen;
3431 };
3432
3433 /* Initialize the index symtab iterator ITER. */
3434
3435 static void
3436 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3437 struct dwarf2_per_objfile *dwarf2_per_objfile,
3438 gdb::optional<block_enum> block_index,
3439 domain_enum domain,
3440 const char *name)
3441 {
3442 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3443 iter->block_index = block_index;
3444 iter->domain = domain;
3445 iter->next = 0;
3446 iter->global_seen = 0;
3447
3448 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3449
3450 /* index is NULL if OBJF_READNOW. */
3451 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3452 iter->length = MAYBE_SWAP (*iter->vec);
3453 else
3454 {
3455 iter->vec = NULL;
3456 iter->length = 0;
3457 }
3458 }
3459
3460 /* Return the next matching CU or NULL if there are no more. */
3461
3462 static struct dwarf2_per_cu_data *
3463 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3464 {
3465 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3466
3467 for ( ; iter->next < iter->length; ++iter->next)
3468 {
3469 offset_type cu_index_and_attrs =
3470 MAYBE_SWAP (iter->vec[iter->next + 1]);
3471 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3472 gdb_index_symbol_kind symbol_kind =
3473 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3474 /* Only check the symbol attributes if they're present.
3475 Indices prior to version 7 don't record them,
3476 and indices >= 7 may elide them for certain symbols
3477 (gold does this). */
3478 int attrs_valid =
3479 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3480 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3481
3482 /* Don't crash on bad data. */
3483 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3484 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3485 {
3486 complaint (_(".gdb_index entry has bad CU index"
3487 " [in module %s]"),
3488 objfile_name (dwarf2_per_objfile->objfile));
3489 continue;
3490 }
3491
3492 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3493
3494 /* Skip if already read in. */
3495 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3496 continue;
3497
3498 /* Check static vs global. */
3499 if (attrs_valid)
3500 {
3501 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3502
3503 if (iter->block_index.has_value ())
3504 {
3505 bool want_static = *iter->block_index == STATIC_BLOCK;
3506
3507 if (is_static != want_static)
3508 continue;
3509 }
3510
3511 /* Work around gold/15646. */
3512 if (!is_static && iter->global_seen)
3513 continue;
3514 if (!is_static)
3515 iter->global_seen = 1;
3516 }
3517
3518 /* Only check the symbol's kind if it has one. */
3519 if (attrs_valid)
3520 {
3521 switch (iter->domain)
3522 {
3523 case VAR_DOMAIN:
3524 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3525 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3526 /* Some types are also in VAR_DOMAIN. */
3527 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3528 continue;
3529 break;
3530 case STRUCT_DOMAIN:
3531 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3532 continue;
3533 break;
3534 case LABEL_DOMAIN:
3535 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3536 continue;
3537 break;
3538 case MODULE_DOMAIN:
3539 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3540 continue;
3541 break;
3542 default:
3543 break;
3544 }
3545 }
3546
3547 ++iter->next;
3548 return per_cu;
3549 }
3550
3551 return NULL;
3552 }
3553
3554 static struct compunit_symtab *
3555 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3556 const char *name, domain_enum domain)
3557 {
3558 struct compunit_symtab *stab_best = NULL;
3559 struct dwarf2_per_objfile *dwarf2_per_objfile
3560 = get_dwarf2_per_objfile (objfile);
3561
3562 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3563
3564 struct dw2_symtab_iterator iter;
3565 struct dwarf2_per_cu_data *per_cu;
3566
3567 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3568
3569 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3570 {
3571 struct symbol *sym, *with_opaque = NULL;
3572 struct compunit_symtab *stab
3573 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3574 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3575 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3576
3577 sym = block_find_symbol (block, name, domain,
3578 block_find_non_opaque_type_preferred,
3579 &with_opaque);
3580
3581 /* Some caution must be observed with overloaded functions
3582 and methods, since the index will not contain any overload
3583 information (but NAME might contain it). */
3584
3585 if (sym != NULL
3586 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3587 return stab;
3588 if (with_opaque != NULL
3589 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3590 stab_best = stab;
3591
3592 /* Keep looking through other CUs. */
3593 }
3594
3595 return stab_best;
3596 }
3597
3598 static void
3599 dw2_print_stats (struct objfile *objfile)
3600 {
3601 struct dwarf2_per_objfile *dwarf2_per_objfile
3602 = get_dwarf2_per_objfile (objfile);
3603 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3604 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3605 int count = 0;
3606
3607 for (int i = 0; i < total; ++i)
3608 {
3609 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3610
3611 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3612 ++count;
3613 }
3614 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3615 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3616 }
3617
3618 /* This dumps minimal information about the index.
3619 It is called via "mt print objfiles".
3620 One use is to verify .gdb_index has been loaded by the
3621 gdb.dwarf2/gdb-index.exp testcase. */
3622
3623 static void
3624 dw2_dump (struct objfile *objfile)
3625 {
3626 struct dwarf2_per_objfile *dwarf2_per_objfile
3627 = get_dwarf2_per_objfile (objfile);
3628
3629 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3630 printf_filtered (".gdb_index:");
3631 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3632 {
3633 printf_filtered (" version %d\n",
3634 dwarf2_per_objfile->per_bfd->index_table->version);
3635 }
3636 else
3637 printf_filtered (" faked for \"readnow\"\n");
3638 printf_filtered ("\n");
3639 }
3640
3641 static void
3642 dw2_expand_symtabs_for_function (struct objfile *objfile,
3643 const char *func_name)
3644 {
3645 struct dwarf2_per_objfile *dwarf2_per_objfile
3646 = get_dwarf2_per_objfile (objfile);
3647
3648 struct dw2_symtab_iterator iter;
3649 struct dwarf2_per_cu_data *per_cu;
3650
3651 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3652
3653 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3654 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3655
3656 }
3657
3658 static void
3659 dw2_expand_all_symtabs (struct objfile *objfile)
3660 {
3661 struct dwarf2_per_objfile *dwarf2_per_objfile
3662 = get_dwarf2_per_objfile (objfile);
3663 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3664 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3665
3666 for (int i = 0; i < total_units; ++i)
3667 {
3668 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3669
3670 /* We don't want to directly expand a partial CU, because if we
3671 read it with the wrong language, then assertion failures can
3672 be triggered later on. See PR symtab/23010. So, tell
3673 dw2_instantiate_symtab to skip partial CUs -- any important
3674 partial CU will be read via DW_TAG_imported_unit anyway. */
3675 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3676 }
3677 }
3678
3679 static void
3680 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3681 const char *fullname)
3682 {
3683 struct dwarf2_per_objfile *dwarf2_per_objfile
3684 = get_dwarf2_per_objfile (objfile);
3685
3686 /* We don't need to consider type units here.
3687 This is only called for examining code, e.g. expand_line_sal.
3688 There can be an order of magnitude (or more) more type units
3689 than comp units, and we avoid them if we can. */
3690
3691 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3692 {
3693 /* We only need to look at symtabs not already expanded. */
3694 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3695 continue;
3696
3697 quick_file_names *file_data
3698 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3699 if (file_data == NULL)
3700 continue;
3701
3702 for (int j = 0; j < file_data->num_file_names; ++j)
3703 {
3704 const char *this_fullname = file_data->file_names[j];
3705
3706 if (filename_cmp (this_fullname, fullname) == 0)
3707 {
3708 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3709 break;
3710 }
3711 }
3712 }
3713 }
3714
3715 static void
3716 dw2_expand_symtabs_matching_symbol
3717 (mapped_index_base &index,
3718 const lookup_name_info &lookup_name_in,
3719 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3720 enum search_domain kind,
3721 gdb::function_view<bool (offset_type)> match_callback);
3722
3723 static void
3724 dw2_expand_symtabs_matching_one
3725 (dwarf2_per_cu_data *per_cu,
3726 dwarf2_per_objfile *per_objfile,
3727 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3728 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3729
3730 static void
3731 dw2_map_matching_symbols
3732 (struct objfile *objfile,
3733 const lookup_name_info &name, domain_enum domain,
3734 int global,
3735 gdb::function_view<symbol_found_callback_ftype> callback,
3736 symbol_compare_ftype *ordered_compare)
3737 {
3738 /* Used for Ada. */
3739 struct dwarf2_per_objfile *dwarf2_per_objfile
3740 = get_dwarf2_per_objfile (objfile);
3741
3742 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3743
3744 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3745 {
3746 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3747 here though if the current language is Ada for a non-Ada objfile
3748 using GNU index. */
3749 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3750
3751 const char *match_name = name.ada ().lookup_name ().c_str ();
3752 auto matcher = [&] (const char *symname)
3753 {
3754 if (ordered_compare == nullptr)
3755 return true;
3756 return ordered_compare (symname, match_name) == 0;
3757 };
3758
3759 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3760 [&] (offset_type namei)
3761 {
3762 struct dw2_symtab_iterator iter;
3763 struct dwarf2_per_cu_data *per_cu;
3764
3765 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3766 match_name);
3767 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3768 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3769 nullptr);
3770 return true;
3771 });
3772 }
3773 else
3774 {
3775 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3776 proceed assuming all symtabs have been read in. */
3777 }
3778
3779 for (compunit_symtab *cust : objfile->compunits ())
3780 {
3781 const struct block *block;
3782
3783 if (cust == NULL)
3784 continue;
3785 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3786 if (!iterate_over_symbols_terminated (block, name,
3787 domain, callback))
3788 return;
3789 }
3790 }
3791
3792 /* Starting from a search name, return the string that finds the upper
3793 bound of all strings that start with SEARCH_NAME in a sorted name
3794 list. Returns the empty string to indicate that the upper bound is
3795 the end of the list. */
3796
3797 static std::string
3798 make_sort_after_prefix_name (const char *search_name)
3799 {
3800 /* When looking to complete "func", we find the upper bound of all
3801 symbols that start with "func" by looking for where we'd insert
3802 the closest string that would follow "func" in lexicographical
3803 order. Usually, that's "func"-with-last-character-incremented,
3804 i.e. "fund". Mind non-ASCII characters, though. Usually those
3805 will be UTF-8 multi-byte sequences, but we can't be certain.
3806 Especially mind the 0xff character, which is a valid character in
3807 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3808 rule out compilers allowing it in identifiers. Note that
3809 conveniently, strcmp/strcasecmp are specified to compare
3810 characters interpreted as unsigned char. So what we do is treat
3811 the whole string as a base 256 number composed of a sequence of
3812 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3813 to 0, and carries 1 to the following more-significant position.
3814 If the very first character in SEARCH_NAME ends up incremented
3815 and carries/overflows, then the upper bound is the end of the
3816 list. The string after the empty string is also the empty
3817 string.
3818
3819 Some examples of this operation:
3820
3821 SEARCH_NAME => "+1" RESULT
3822
3823 "abc" => "abd"
3824 "ab\xff" => "ac"
3825 "\xff" "a" "\xff" => "\xff" "b"
3826 "\xff" => ""
3827 "\xff\xff" => ""
3828 "" => ""
3829
3830 Then, with these symbols for example:
3831
3832 func
3833 func1
3834 fund
3835
3836 completing "func" looks for symbols between "func" and
3837 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3838 which finds "func" and "func1", but not "fund".
3839
3840 And with:
3841
3842 funcÿ (Latin1 'ÿ' [0xff])
3843 funcÿ1
3844 fund
3845
3846 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3847 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3848
3849 And with:
3850
3851 ÿÿ (Latin1 'ÿ' [0xff])
3852 ÿÿ1
3853
3854 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3855 the end of the list.
3856 */
3857 std::string after = search_name;
3858 while (!after.empty () && (unsigned char) after.back () == 0xff)
3859 after.pop_back ();
3860 if (!after.empty ())
3861 after.back () = (unsigned char) after.back () + 1;
3862 return after;
3863 }
3864
3865 /* See declaration. */
3866
3867 std::pair<std::vector<name_component>::const_iterator,
3868 std::vector<name_component>::const_iterator>
3869 mapped_index_base::find_name_components_bounds
3870 (const lookup_name_info &lookup_name_without_params, language lang) const
3871 {
3872 auto *name_cmp
3873 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3874
3875 const char *lang_name
3876 = lookup_name_without_params.language_lookup_name (lang);
3877
3878 /* Comparison function object for lower_bound that matches against a
3879 given symbol name. */
3880 auto lookup_compare_lower = [&] (const name_component &elem,
3881 const char *name)
3882 {
3883 const char *elem_qualified = this->symbol_name_at (elem.idx);
3884 const char *elem_name = elem_qualified + elem.name_offset;
3885 return name_cmp (elem_name, name) < 0;
3886 };
3887
3888 /* Comparison function object for upper_bound that matches against a
3889 given symbol name. */
3890 auto lookup_compare_upper = [&] (const char *name,
3891 const name_component &elem)
3892 {
3893 const char *elem_qualified = this->symbol_name_at (elem.idx);
3894 const char *elem_name = elem_qualified + elem.name_offset;
3895 return name_cmp (name, elem_name) < 0;
3896 };
3897
3898 auto begin = this->name_components.begin ();
3899 auto end = this->name_components.end ();
3900
3901 /* Find the lower bound. */
3902 auto lower = [&] ()
3903 {
3904 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3905 return begin;
3906 else
3907 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3908 } ();
3909
3910 /* Find the upper bound. */
3911 auto upper = [&] ()
3912 {
3913 if (lookup_name_without_params.completion_mode ())
3914 {
3915 /* In completion mode, we want UPPER to point past all
3916 symbols names that have the same prefix. I.e., with
3917 these symbols, and completing "func":
3918
3919 function << lower bound
3920 function1
3921 other_function << upper bound
3922
3923 We find the upper bound by looking for the insertion
3924 point of "func"-with-last-character-incremented,
3925 i.e. "fund". */
3926 std::string after = make_sort_after_prefix_name (lang_name);
3927 if (after.empty ())
3928 return end;
3929 return std::lower_bound (lower, end, after.c_str (),
3930 lookup_compare_lower);
3931 }
3932 else
3933 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3934 } ();
3935
3936 return {lower, upper};
3937 }
3938
3939 /* See declaration. */
3940
3941 void
3942 mapped_index_base::build_name_components ()
3943 {
3944 if (!this->name_components.empty ())
3945 return;
3946
3947 this->name_components_casing = case_sensitivity;
3948 auto *name_cmp
3949 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3950
3951 /* The code below only knows how to break apart components of C++
3952 symbol names (and other languages that use '::' as
3953 namespace/module separator) and Ada symbol names. */
3954 auto count = this->symbol_name_count ();
3955 for (offset_type idx = 0; idx < count; idx++)
3956 {
3957 if (this->symbol_name_slot_invalid (idx))
3958 continue;
3959
3960 const char *name = this->symbol_name_at (idx);
3961
3962 /* Add each name component to the name component table. */
3963 unsigned int previous_len = 0;
3964
3965 if (strstr (name, "::") != nullptr)
3966 {
3967 for (unsigned int current_len = cp_find_first_component (name);
3968 name[current_len] != '\0';
3969 current_len += cp_find_first_component (name + current_len))
3970 {
3971 gdb_assert (name[current_len] == ':');
3972 this->name_components.push_back ({previous_len, idx});
3973 /* Skip the '::'. */
3974 current_len += 2;
3975 previous_len = current_len;
3976 }
3977 }
3978 else
3979 {
3980 /* Handle the Ada encoded (aka mangled) form here. */
3981 for (const char *iter = strstr (name, "__");
3982 iter != nullptr;
3983 iter = strstr (iter, "__"))
3984 {
3985 this->name_components.push_back ({previous_len, idx});
3986 iter += 2;
3987 previous_len = iter - name;
3988 }
3989 }
3990
3991 this->name_components.push_back ({previous_len, idx});
3992 }
3993
3994 /* Sort name_components elements by name. */
3995 auto name_comp_compare = [&] (const name_component &left,
3996 const name_component &right)
3997 {
3998 const char *left_qualified = this->symbol_name_at (left.idx);
3999 const char *right_qualified = this->symbol_name_at (right.idx);
4000
4001 const char *left_name = left_qualified + left.name_offset;
4002 const char *right_name = right_qualified + right.name_offset;
4003
4004 return name_cmp (left_name, right_name) < 0;
4005 };
4006
4007 std::sort (this->name_components.begin (),
4008 this->name_components.end (),
4009 name_comp_compare);
4010 }
4011
4012 /* Helper for dw2_expand_symtabs_matching that works with a
4013 mapped_index_base instead of the containing objfile. This is split
4014 to a separate function in order to be able to unit test the
4015 name_components matching using a mock mapped_index_base. For each
4016 symbol name that matches, calls MATCH_CALLBACK, passing it the
4017 symbol's index in the mapped_index_base symbol table. */
4018
4019 static void
4020 dw2_expand_symtabs_matching_symbol
4021 (mapped_index_base &index,
4022 const lookup_name_info &lookup_name_in,
4023 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4024 enum search_domain kind,
4025 gdb::function_view<bool (offset_type)> match_callback)
4026 {
4027 lookup_name_info lookup_name_without_params
4028 = lookup_name_in.make_ignore_params ();
4029
4030 /* Build the symbol name component sorted vector, if we haven't
4031 yet. */
4032 index.build_name_components ();
4033
4034 /* The same symbol may appear more than once in the range though.
4035 E.g., if we're looking for symbols that complete "w", and we have
4036 a symbol named "w1::w2", we'll find the two name components for
4037 that same symbol in the range. To be sure we only call the
4038 callback once per symbol, we first collect the symbol name
4039 indexes that matched in a temporary vector and ignore
4040 duplicates. */
4041 std::vector<offset_type> matches;
4042
4043 struct name_and_matcher
4044 {
4045 symbol_name_matcher_ftype *matcher;
4046 const char *name;
4047
4048 bool operator== (const name_and_matcher &other) const
4049 {
4050 return matcher == other.matcher && strcmp (name, other.name) == 0;
4051 }
4052 };
4053
4054 /* A vector holding all the different symbol name matchers, for all
4055 languages. */
4056 std::vector<name_and_matcher> matchers;
4057
4058 for (int i = 0; i < nr_languages; i++)
4059 {
4060 enum language lang_e = (enum language) i;
4061
4062 const language_defn *lang = language_def (lang_e);
4063 symbol_name_matcher_ftype *name_matcher
4064 = get_symbol_name_matcher (lang, lookup_name_without_params);
4065
4066 name_and_matcher key {
4067 name_matcher,
4068 lookup_name_without_params.language_lookup_name (lang_e)
4069 };
4070
4071 /* Don't insert the same comparison routine more than once.
4072 Note that we do this linear walk. This is not a problem in
4073 practice because the number of supported languages is
4074 low. */
4075 if (std::find (matchers.begin (), matchers.end (), key)
4076 != matchers.end ())
4077 continue;
4078 matchers.push_back (std::move (key));
4079
4080 auto bounds
4081 = index.find_name_components_bounds (lookup_name_without_params,
4082 lang_e);
4083
4084 /* Now for each symbol name in range, check to see if we have a name
4085 match, and if so, call the MATCH_CALLBACK callback. */
4086
4087 for (; bounds.first != bounds.second; ++bounds.first)
4088 {
4089 const char *qualified = index.symbol_name_at (bounds.first->idx);
4090
4091 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4092 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4093 continue;
4094
4095 matches.push_back (bounds.first->idx);
4096 }
4097 }
4098
4099 std::sort (matches.begin (), matches.end ());
4100
4101 /* Finally call the callback, once per match. */
4102 ULONGEST prev = -1;
4103 for (offset_type idx : matches)
4104 {
4105 if (prev != idx)
4106 {
4107 if (!match_callback (idx))
4108 break;
4109 prev = idx;
4110 }
4111 }
4112
4113 /* Above we use a type wider than idx's for 'prev', since 0 and
4114 (offset_type)-1 are both possible values. */
4115 static_assert (sizeof (prev) > sizeof (offset_type), "");
4116 }
4117
4118 #if GDB_SELF_TEST
4119
4120 namespace selftests { namespace dw2_expand_symtabs_matching {
4121
4122 /* A mock .gdb_index/.debug_names-like name index table, enough to
4123 exercise dw2_expand_symtabs_matching_symbol, which works with the
4124 mapped_index_base interface. Builds an index from the symbol list
4125 passed as parameter to the constructor. */
4126 class mock_mapped_index : public mapped_index_base
4127 {
4128 public:
4129 mock_mapped_index (gdb::array_view<const char *> symbols)
4130 : m_symbol_table (symbols)
4131 {}
4132
4133 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4134
4135 /* Return the number of names in the symbol table. */
4136 size_t symbol_name_count () const override
4137 {
4138 return m_symbol_table.size ();
4139 }
4140
4141 /* Get the name of the symbol at IDX in the symbol table. */
4142 const char *symbol_name_at (offset_type idx) const override
4143 {
4144 return m_symbol_table[idx];
4145 }
4146
4147 private:
4148 gdb::array_view<const char *> m_symbol_table;
4149 };
4150
4151 /* Convenience function that converts a NULL pointer to a "<null>"
4152 string, to pass to print routines. */
4153
4154 static const char *
4155 string_or_null (const char *str)
4156 {
4157 return str != NULL ? str : "<null>";
4158 }
4159
4160 /* Check if a lookup_name_info built from
4161 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4162 index. EXPECTED_LIST is the list of expected matches, in expected
4163 matching order. If no match expected, then an empty list is
4164 specified. Returns true on success. On failure prints a warning
4165 indicating the file:line that failed, and returns false. */
4166
4167 static bool
4168 check_match (const char *file, int line,
4169 mock_mapped_index &mock_index,
4170 const char *name, symbol_name_match_type match_type,
4171 bool completion_mode,
4172 std::initializer_list<const char *> expected_list)
4173 {
4174 lookup_name_info lookup_name (name, match_type, completion_mode);
4175
4176 bool matched = true;
4177
4178 auto mismatch = [&] (const char *expected_str,
4179 const char *got)
4180 {
4181 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4182 "expected=\"%s\", got=\"%s\"\n"),
4183 file, line,
4184 (match_type == symbol_name_match_type::FULL
4185 ? "FULL" : "WILD"),
4186 name, string_or_null (expected_str), string_or_null (got));
4187 matched = false;
4188 };
4189
4190 auto expected_it = expected_list.begin ();
4191 auto expected_end = expected_list.end ();
4192
4193 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4194 NULL, ALL_DOMAIN,
4195 [&] (offset_type idx)
4196 {
4197 const char *matched_name = mock_index.symbol_name_at (idx);
4198 const char *expected_str
4199 = expected_it == expected_end ? NULL : *expected_it++;
4200
4201 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4202 mismatch (expected_str, matched_name);
4203 return true;
4204 });
4205
4206 const char *expected_str
4207 = expected_it == expected_end ? NULL : *expected_it++;
4208 if (expected_str != NULL)
4209 mismatch (expected_str, NULL);
4210
4211 return matched;
4212 }
4213
4214 /* The symbols added to the mock mapped_index for testing (in
4215 canonical form). */
4216 static const char *test_symbols[] = {
4217 "function",
4218 "std::bar",
4219 "std::zfunction",
4220 "std::zfunction2",
4221 "w1::w2",
4222 "ns::foo<char*>",
4223 "ns::foo<int>",
4224 "ns::foo<long>",
4225 "ns2::tmpl<int>::foo2",
4226 "(anonymous namespace)::A::B::C",
4227
4228 /* These are used to check that the increment-last-char in the
4229 matching algorithm for completion doesn't match "t1_fund" when
4230 completing "t1_func". */
4231 "t1_func",
4232 "t1_func1",
4233 "t1_fund",
4234 "t1_fund1",
4235
4236 /* A UTF-8 name with multi-byte sequences to make sure that
4237 cp-name-parser understands this as a single identifier ("função"
4238 is "function" in PT). */
4239 u8"u8função",
4240
4241 /* \377 (0xff) is Latin1 'ÿ'. */
4242 "yfunc\377",
4243
4244 /* \377 (0xff) is Latin1 'ÿ'. */
4245 "\377",
4246 "\377\377123",
4247
4248 /* A name with all sorts of complications. Starts with "z" to make
4249 it easier for the completion tests below. */
4250 #define Z_SYM_NAME \
4251 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4252 "::tuple<(anonymous namespace)::ui*, " \
4253 "std::default_delete<(anonymous namespace)::ui>, void>"
4254
4255 Z_SYM_NAME
4256 };
4257
4258 /* Returns true if the mapped_index_base::find_name_component_bounds
4259 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4260 in completion mode. */
4261
4262 static bool
4263 check_find_bounds_finds (mapped_index_base &index,
4264 const char *search_name,
4265 gdb::array_view<const char *> expected_syms)
4266 {
4267 lookup_name_info lookup_name (search_name,
4268 symbol_name_match_type::FULL, true);
4269
4270 auto bounds = index.find_name_components_bounds (lookup_name,
4271 language_cplus);
4272
4273 size_t distance = std::distance (bounds.first, bounds.second);
4274 if (distance != expected_syms.size ())
4275 return false;
4276
4277 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4278 {
4279 auto nc_elem = bounds.first + exp_elem;
4280 const char *qualified = index.symbol_name_at (nc_elem->idx);
4281 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4282 return false;
4283 }
4284
4285 return true;
4286 }
4287
4288 /* Test the lower-level mapped_index::find_name_component_bounds
4289 method. */
4290
4291 static void
4292 test_mapped_index_find_name_component_bounds ()
4293 {
4294 mock_mapped_index mock_index (test_symbols);
4295
4296 mock_index.build_name_components ();
4297
4298 /* Test the lower-level mapped_index::find_name_component_bounds
4299 method in completion mode. */
4300 {
4301 static const char *expected_syms[] = {
4302 "t1_func",
4303 "t1_func1",
4304 };
4305
4306 SELF_CHECK (check_find_bounds_finds (mock_index,
4307 "t1_func", expected_syms));
4308 }
4309
4310 /* Check that the increment-last-char in the name matching algorithm
4311 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4312 {
4313 static const char *expected_syms1[] = {
4314 "\377",
4315 "\377\377123",
4316 };
4317 SELF_CHECK (check_find_bounds_finds (mock_index,
4318 "\377", expected_syms1));
4319
4320 static const char *expected_syms2[] = {
4321 "\377\377123",
4322 };
4323 SELF_CHECK (check_find_bounds_finds (mock_index,
4324 "\377\377", expected_syms2));
4325 }
4326 }
4327
4328 /* Test dw2_expand_symtabs_matching_symbol. */
4329
4330 static void
4331 test_dw2_expand_symtabs_matching_symbol ()
4332 {
4333 mock_mapped_index mock_index (test_symbols);
4334
4335 /* We let all tests run until the end even if some fails, for debug
4336 convenience. */
4337 bool any_mismatch = false;
4338
4339 /* Create the expected symbols list (an initializer_list). Needed
4340 because lists have commas, and we need to pass them to CHECK,
4341 which is a macro. */
4342 #define EXPECT(...) { __VA_ARGS__ }
4343
4344 /* Wrapper for check_match that passes down the current
4345 __FILE__/__LINE__. */
4346 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4347 any_mismatch |= !check_match (__FILE__, __LINE__, \
4348 mock_index, \
4349 NAME, MATCH_TYPE, COMPLETION_MODE, \
4350 EXPECTED_LIST)
4351
4352 /* Identity checks. */
4353 for (const char *sym : test_symbols)
4354 {
4355 /* Should be able to match all existing symbols. */
4356 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4357 EXPECT (sym));
4358
4359 /* Should be able to match all existing symbols with
4360 parameters. */
4361 std::string with_params = std::string (sym) + "(int)";
4362 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4363 EXPECT (sym));
4364
4365 /* Should be able to match all existing symbols with
4366 parameters and qualifiers. */
4367 with_params = std::string (sym) + " ( int ) const";
4368 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4369 EXPECT (sym));
4370
4371 /* This should really find sym, but cp-name-parser.y doesn't
4372 know about lvalue/rvalue qualifiers yet. */
4373 with_params = std::string (sym) + " ( int ) &&";
4374 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4375 {});
4376 }
4377
4378 /* Check that the name matching algorithm for completion doesn't get
4379 confused with Latin1 'ÿ' / 0xff. */
4380 {
4381 static const char str[] = "\377";
4382 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4383 EXPECT ("\377", "\377\377123"));
4384 }
4385
4386 /* Check that the increment-last-char in the matching algorithm for
4387 completion doesn't match "t1_fund" when completing "t1_func". */
4388 {
4389 static const char str[] = "t1_func";
4390 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4391 EXPECT ("t1_func", "t1_func1"));
4392 }
4393
4394 /* Check that completion mode works at each prefix of the expected
4395 symbol name. */
4396 {
4397 static const char str[] = "function(int)";
4398 size_t len = strlen (str);
4399 std::string lookup;
4400
4401 for (size_t i = 1; i < len; i++)
4402 {
4403 lookup.assign (str, i);
4404 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4405 EXPECT ("function"));
4406 }
4407 }
4408
4409 /* While "w" is a prefix of both components, the match function
4410 should still only be called once. */
4411 {
4412 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4413 EXPECT ("w1::w2"));
4414 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4415 EXPECT ("w1::w2"));
4416 }
4417
4418 /* Same, with a "complicated" symbol. */
4419 {
4420 static const char str[] = Z_SYM_NAME;
4421 size_t len = strlen (str);
4422 std::string lookup;
4423
4424 for (size_t i = 1; i < len; i++)
4425 {
4426 lookup.assign (str, i);
4427 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4428 EXPECT (Z_SYM_NAME));
4429 }
4430 }
4431
4432 /* In FULL mode, an incomplete symbol doesn't match. */
4433 {
4434 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4435 {});
4436 }
4437
4438 /* A complete symbol with parameters matches any overload, since the
4439 index has no overload info. */
4440 {
4441 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4442 EXPECT ("std::zfunction", "std::zfunction2"));
4443 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4444 EXPECT ("std::zfunction", "std::zfunction2"));
4445 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4446 EXPECT ("std::zfunction", "std::zfunction2"));
4447 }
4448
4449 /* Check that whitespace is ignored appropriately. A symbol with a
4450 template argument list. */
4451 {
4452 static const char expected[] = "ns::foo<int>";
4453 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4454 EXPECT (expected));
4455 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4456 EXPECT (expected));
4457 }
4458
4459 /* Check that whitespace is ignored appropriately. A symbol with a
4460 template argument list that includes a pointer. */
4461 {
4462 static const char expected[] = "ns::foo<char*>";
4463 /* Try both completion and non-completion modes. */
4464 static const bool completion_mode[2] = {false, true};
4465 for (size_t i = 0; i < 2; i++)
4466 {
4467 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4468 completion_mode[i], EXPECT (expected));
4469 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4470 completion_mode[i], EXPECT (expected));
4471
4472 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4473 completion_mode[i], EXPECT (expected));
4474 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4475 completion_mode[i], EXPECT (expected));
4476 }
4477 }
4478
4479 {
4480 /* Check method qualifiers are ignored. */
4481 static const char expected[] = "ns::foo<char*>";
4482 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4483 symbol_name_match_type::FULL, true, EXPECT (expected));
4484 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4485 symbol_name_match_type::FULL, true, EXPECT (expected));
4486 CHECK_MATCH ("foo < char * > ( int ) const",
4487 symbol_name_match_type::WILD, true, EXPECT (expected));
4488 CHECK_MATCH ("foo < char * > ( int ) &&",
4489 symbol_name_match_type::WILD, true, EXPECT (expected));
4490 }
4491
4492 /* Test lookup names that don't match anything. */
4493 {
4494 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4495 {});
4496
4497 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4498 {});
4499 }
4500
4501 /* Some wild matching tests, exercising "(anonymous namespace)",
4502 which should not be confused with a parameter list. */
4503 {
4504 static const char *syms[] = {
4505 "A::B::C",
4506 "B::C",
4507 "C",
4508 "A :: B :: C ( int )",
4509 "B :: C ( int )",
4510 "C ( int )",
4511 };
4512
4513 for (const char *s : syms)
4514 {
4515 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4516 EXPECT ("(anonymous namespace)::A::B::C"));
4517 }
4518 }
4519
4520 {
4521 static const char expected[] = "ns2::tmpl<int>::foo2";
4522 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4523 EXPECT (expected));
4524 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4525 EXPECT (expected));
4526 }
4527
4528 SELF_CHECK (!any_mismatch);
4529
4530 #undef EXPECT
4531 #undef CHECK_MATCH
4532 }
4533
4534 static void
4535 run_test ()
4536 {
4537 test_mapped_index_find_name_component_bounds ();
4538 test_dw2_expand_symtabs_matching_symbol ();
4539 }
4540
4541 }} // namespace selftests::dw2_expand_symtabs_matching
4542
4543 #endif /* GDB_SELF_TEST */
4544
4545 /* If FILE_MATCHER is NULL or if PER_CU has
4546 dwarf2_per_cu_quick_data::MARK set (see
4547 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4548 EXPANSION_NOTIFY on it. */
4549
4550 static void
4551 dw2_expand_symtabs_matching_one
4552 (dwarf2_per_cu_data *per_cu,
4553 dwarf2_per_objfile *per_objfile,
4554 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4555 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4556 {
4557 if (file_matcher == NULL || per_cu->v.quick->mark)
4558 {
4559 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4560
4561 compunit_symtab *symtab
4562 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4563 gdb_assert (symtab != nullptr);
4564
4565 if (expansion_notify != NULL && symtab_was_null)
4566 expansion_notify (symtab);
4567 }
4568 }
4569
4570 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4571 matched, to expand corresponding CUs that were marked. IDX is the
4572 index of the symbol name that matched. */
4573
4574 static void
4575 dw2_expand_marked_cus
4576 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4577 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4578 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4579 search_domain kind)
4580 {
4581 offset_type *vec, vec_len, vec_idx;
4582 bool global_seen = false;
4583 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4584
4585 vec = (offset_type *) (index.constant_pool
4586 + MAYBE_SWAP (index.symbol_table[idx].vec));
4587 vec_len = MAYBE_SWAP (vec[0]);
4588 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4589 {
4590 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4591 /* This value is only valid for index versions >= 7. */
4592 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4593 gdb_index_symbol_kind symbol_kind =
4594 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4595 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4596 /* Only check the symbol attributes if they're present.
4597 Indices prior to version 7 don't record them,
4598 and indices >= 7 may elide them for certain symbols
4599 (gold does this). */
4600 int attrs_valid =
4601 (index.version >= 7
4602 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4603
4604 /* Work around gold/15646. */
4605 if (attrs_valid)
4606 {
4607 if (!is_static && global_seen)
4608 continue;
4609 if (!is_static)
4610 global_seen = true;
4611 }
4612
4613 /* Only check the symbol's kind if it has one. */
4614 if (attrs_valid)
4615 {
4616 switch (kind)
4617 {
4618 case VARIABLES_DOMAIN:
4619 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4620 continue;
4621 break;
4622 case FUNCTIONS_DOMAIN:
4623 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4624 continue;
4625 break;
4626 case TYPES_DOMAIN:
4627 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4628 continue;
4629 break;
4630 case MODULES_DOMAIN:
4631 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4632 continue;
4633 break;
4634 default:
4635 break;
4636 }
4637 }
4638
4639 /* Don't crash on bad data. */
4640 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4641 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4642 {
4643 complaint (_(".gdb_index entry has bad CU index"
4644 " [in module %s]"),
4645 objfile_name (dwarf2_per_objfile->objfile));
4646 continue;
4647 }
4648
4649 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4650 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4651 expansion_notify);
4652 }
4653 }
4654
4655 /* If FILE_MATCHER is non-NULL, set all the
4656 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4657 that match FILE_MATCHER. */
4658
4659 static void
4660 dw_expand_symtabs_matching_file_matcher
4661 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4662 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4663 {
4664 if (file_matcher == NULL)
4665 return;
4666
4667 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4668 htab_eq_pointer,
4669 NULL, xcalloc, xfree));
4670 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4671 htab_eq_pointer,
4672 NULL, xcalloc, xfree));
4673
4674 /* The rule is CUs specify all the files, including those used by
4675 any TU, so there's no need to scan TUs here. */
4676
4677 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4678 {
4679 QUIT;
4680
4681 per_cu->v.quick->mark = 0;
4682
4683 /* We only need to look at symtabs not already expanded. */
4684 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4685 continue;
4686
4687 quick_file_names *file_data
4688 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4689 if (file_data == NULL)
4690 continue;
4691
4692 if (htab_find (visited_not_found.get (), file_data) != NULL)
4693 continue;
4694 else if (htab_find (visited_found.get (), file_data) != NULL)
4695 {
4696 per_cu->v.quick->mark = 1;
4697 continue;
4698 }
4699
4700 for (int j = 0; j < file_data->num_file_names; ++j)
4701 {
4702 const char *this_real_name;
4703
4704 if (file_matcher (file_data->file_names[j], false))
4705 {
4706 per_cu->v.quick->mark = 1;
4707 break;
4708 }
4709
4710 /* Before we invoke realpath, which can get expensive when many
4711 files are involved, do a quick comparison of the basenames. */
4712 if (!basenames_may_differ
4713 && !file_matcher (lbasename (file_data->file_names[j]),
4714 true))
4715 continue;
4716
4717 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4718 file_data, j);
4719 if (file_matcher (this_real_name, false))
4720 {
4721 per_cu->v.quick->mark = 1;
4722 break;
4723 }
4724 }
4725
4726 void **slot = htab_find_slot (per_cu->v.quick->mark
4727 ? visited_found.get ()
4728 : visited_not_found.get (),
4729 file_data, INSERT);
4730 *slot = file_data;
4731 }
4732 }
4733
4734 static void
4735 dw2_expand_symtabs_matching
4736 (struct objfile *objfile,
4737 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4738 const lookup_name_info *lookup_name,
4739 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4740 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4741 enum search_domain kind)
4742 {
4743 struct dwarf2_per_objfile *dwarf2_per_objfile
4744 = get_dwarf2_per_objfile (objfile);
4745
4746 /* index_table is NULL if OBJF_READNOW. */
4747 if (!dwarf2_per_objfile->per_bfd->index_table)
4748 return;
4749
4750 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4751
4752 if (symbol_matcher == NULL && lookup_name == NULL)
4753 {
4754 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4755 {
4756 QUIT;
4757
4758 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4759 file_matcher, expansion_notify);
4760 }
4761 return;
4762 }
4763
4764 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4765
4766 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4767 symbol_matcher,
4768 kind, [&] (offset_type idx)
4769 {
4770 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4771 expansion_notify, kind);
4772 return true;
4773 });
4774 }
4775
4776 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4777 symtab. */
4778
4779 static struct compunit_symtab *
4780 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4781 CORE_ADDR pc)
4782 {
4783 int i;
4784
4785 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4786 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4787 return cust;
4788
4789 if (cust->includes == NULL)
4790 return NULL;
4791
4792 for (i = 0; cust->includes[i]; ++i)
4793 {
4794 struct compunit_symtab *s = cust->includes[i];
4795
4796 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4797 if (s != NULL)
4798 return s;
4799 }
4800
4801 return NULL;
4802 }
4803
4804 static struct compunit_symtab *
4805 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4806 struct bound_minimal_symbol msymbol,
4807 CORE_ADDR pc,
4808 struct obj_section *section,
4809 int warn_if_readin)
4810 {
4811 struct dwarf2_per_cu_data *data;
4812 struct compunit_symtab *result;
4813
4814 if (!objfile->partial_symtabs->psymtabs_addrmap)
4815 return NULL;
4816
4817 CORE_ADDR baseaddr = objfile->text_section_offset ();
4818 data = (struct dwarf2_per_cu_data *) addrmap_find
4819 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4820 if (!data)
4821 return NULL;
4822
4823 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4824 if (warn_if_readin && per_objfile->symtab_set_p (data))
4825 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4826 paddress (objfile->arch (), pc));
4827
4828 result = recursively_find_pc_sect_compunit_symtab
4829 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4830
4831 gdb_assert (result != NULL);
4832 return result;
4833 }
4834
4835 static void
4836 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4837 void *data, int need_fullname)
4838 {
4839 struct dwarf2_per_objfile *dwarf2_per_objfile
4840 = get_dwarf2_per_objfile (objfile);
4841
4842 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4843 {
4844 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4845
4846 htab_up visited (htab_create_alloc (10,
4847 htab_hash_pointer, htab_eq_pointer,
4848 NULL, xcalloc, xfree));
4849
4850 /* The rule is CUs specify all the files, including those used
4851 by any TU, so there's no need to scan TUs here. We can
4852 ignore file names coming from already-expanded CUs. */
4853
4854 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4855 {
4856 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4857 {
4858 void **slot = htab_find_slot (visited.get (),
4859 per_cu->v.quick->file_names,
4860 INSERT);
4861
4862 *slot = per_cu->v.quick->file_names;
4863 }
4864 }
4865
4866 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4867 {
4868 /* We only need to look at symtabs not already expanded. */
4869 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4870 continue;
4871
4872 quick_file_names *file_data
4873 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4874 if (file_data == NULL)
4875 continue;
4876
4877 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4878 if (*slot)
4879 {
4880 /* Already visited. */
4881 continue;
4882 }
4883 *slot = file_data;
4884
4885 for (int j = 0; j < file_data->num_file_names; ++j)
4886 {
4887 const char *filename = file_data->file_names[j];
4888 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4889 }
4890 }
4891 }
4892
4893 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4894 {
4895 gdb::unique_xmalloc_ptr<char> this_real_name;
4896
4897 if (need_fullname)
4898 this_real_name = gdb_realpath (filename);
4899 (*fun) (filename, this_real_name.get (), data);
4900 });
4901 }
4902
4903 static int
4904 dw2_has_symbols (struct objfile *objfile)
4905 {
4906 return 1;
4907 }
4908
4909 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4910 {
4911 dw2_has_symbols,
4912 dw2_find_last_source_symtab,
4913 dw2_forget_cached_source_info,
4914 dw2_map_symtabs_matching_filename,
4915 dw2_lookup_symbol,
4916 NULL,
4917 dw2_print_stats,
4918 dw2_dump,
4919 dw2_expand_symtabs_for_function,
4920 dw2_expand_all_symtabs,
4921 dw2_expand_symtabs_with_fullname,
4922 dw2_map_matching_symbols,
4923 dw2_expand_symtabs_matching,
4924 dw2_find_pc_sect_compunit_symtab,
4925 NULL,
4926 dw2_map_symbol_filenames
4927 };
4928
4929 /* DWARF-5 debug_names reader. */
4930
4931 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4932 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4933
4934 /* A helper function that reads the .debug_names section in SECTION
4935 and fills in MAP. FILENAME is the name of the file containing the
4936 section; it is used for error reporting.
4937
4938 Returns true if all went well, false otherwise. */
4939
4940 static bool
4941 read_debug_names_from_section (struct objfile *objfile,
4942 const char *filename,
4943 struct dwarf2_section_info *section,
4944 mapped_debug_names &map)
4945 {
4946 if (section->empty ())
4947 return false;
4948
4949 /* Older elfutils strip versions could keep the section in the main
4950 executable while splitting it for the separate debug info file. */
4951 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4952 return false;
4953
4954 section->read (objfile);
4955
4956 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4957
4958 const gdb_byte *addr = section->buffer;
4959
4960 bfd *const abfd = section->get_bfd_owner ();
4961
4962 unsigned int bytes_read;
4963 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4964 addr += bytes_read;
4965
4966 map.dwarf5_is_dwarf64 = bytes_read != 4;
4967 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4968 if (bytes_read + length != section->size)
4969 {
4970 /* There may be multiple per-CU indices. */
4971 warning (_("Section .debug_names in %s length %s does not match "
4972 "section length %s, ignoring .debug_names."),
4973 filename, plongest (bytes_read + length),
4974 pulongest (section->size));
4975 return false;
4976 }
4977
4978 /* The version number. */
4979 uint16_t version = read_2_bytes (abfd, addr);
4980 addr += 2;
4981 if (version != 5)
4982 {
4983 warning (_("Section .debug_names in %s has unsupported version %d, "
4984 "ignoring .debug_names."),
4985 filename, version);
4986 return false;
4987 }
4988
4989 /* Padding. */
4990 uint16_t padding = read_2_bytes (abfd, addr);
4991 addr += 2;
4992 if (padding != 0)
4993 {
4994 warning (_("Section .debug_names in %s has unsupported padding %d, "
4995 "ignoring .debug_names."),
4996 filename, padding);
4997 return false;
4998 }
4999
5000 /* comp_unit_count - The number of CUs in the CU list. */
5001 map.cu_count = read_4_bytes (abfd, addr);
5002 addr += 4;
5003
5004 /* local_type_unit_count - The number of TUs in the local TU
5005 list. */
5006 map.tu_count = read_4_bytes (abfd, addr);
5007 addr += 4;
5008
5009 /* foreign_type_unit_count - The number of TUs in the foreign TU
5010 list. */
5011 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5012 addr += 4;
5013 if (foreign_tu_count != 0)
5014 {
5015 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5016 "ignoring .debug_names."),
5017 filename, static_cast<unsigned long> (foreign_tu_count));
5018 return false;
5019 }
5020
5021 /* bucket_count - The number of hash buckets in the hash lookup
5022 table. */
5023 map.bucket_count = read_4_bytes (abfd, addr);
5024 addr += 4;
5025
5026 /* name_count - The number of unique names in the index. */
5027 map.name_count = read_4_bytes (abfd, addr);
5028 addr += 4;
5029
5030 /* abbrev_table_size - The size in bytes of the abbreviations
5031 table. */
5032 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5033 addr += 4;
5034
5035 /* augmentation_string_size - The size in bytes of the augmentation
5036 string. This value is rounded up to a multiple of 4. */
5037 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5038 addr += 4;
5039 map.augmentation_is_gdb = ((augmentation_string_size
5040 == sizeof (dwarf5_augmentation))
5041 && memcmp (addr, dwarf5_augmentation,
5042 sizeof (dwarf5_augmentation)) == 0);
5043 augmentation_string_size += (-augmentation_string_size) & 3;
5044 addr += augmentation_string_size;
5045
5046 /* List of CUs */
5047 map.cu_table_reordered = addr;
5048 addr += map.cu_count * map.offset_size;
5049
5050 /* List of Local TUs */
5051 map.tu_table_reordered = addr;
5052 addr += map.tu_count * map.offset_size;
5053
5054 /* Hash Lookup Table */
5055 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5056 addr += map.bucket_count * 4;
5057 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5058 addr += map.name_count * 4;
5059
5060 /* Name Table */
5061 map.name_table_string_offs_reordered = addr;
5062 addr += map.name_count * map.offset_size;
5063 map.name_table_entry_offs_reordered = addr;
5064 addr += map.name_count * map.offset_size;
5065
5066 const gdb_byte *abbrev_table_start = addr;
5067 for (;;)
5068 {
5069 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5070 addr += bytes_read;
5071 if (index_num == 0)
5072 break;
5073
5074 const auto insertpair
5075 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5076 if (!insertpair.second)
5077 {
5078 warning (_("Section .debug_names in %s has duplicate index %s, "
5079 "ignoring .debug_names."),
5080 filename, pulongest (index_num));
5081 return false;
5082 }
5083 mapped_debug_names::index_val &indexval = insertpair.first->second;
5084 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5085 addr += bytes_read;
5086
5087 for (;;)
5088 {
5089 mapped_debug_names::index_val::attr attr;
5090 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5091 addr += bytes_read;
5092 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5093 addr += bytes_read;
5094 if (attr.form == DW_FORM_implicit_const)
5095 {
5096 attr.implicit_const = read_signed_leb128 (abfd, addr,
5097 &bytes_read);
5098 addr += bytes_read;
5099 }
5100 if (attr.dw_idx == 0 && attr.form == 0)
5101 break;
5102 indexval.attr_vec.push_back (std::move (attr));
5103 }
5104 }
5105 if (addr != abbrev_table_start + abbrev_table_size)
5106 {
5107 warning (_("Section .debug_names in %s has abbreviation_table "
5108 "of size %s vs. written as %u, ignoring .debug_names."),
5109 filename, plongest (addr - abbrev_table_start),
5110 abbrev_table_size);
5111 return false;
5112 }
5113 map.entry_pool = addr;
5114
5115 return true;
5116 }
5117
5118 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5119 list. */
5120
5121 static void
5122 create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
5123 const mapped_debug_names &map,
5124 dwarf2_section_info &section,
5125 bool is_dwz)
5126 {
5127 if (!map.augmentation_is_gdb)
5128 {
5129 for (uint32_t i = 0; i < map.cu_count; ++i)
5130 {
5131 sect_offset sect_off
5132 = (sect_offset) (extract_unsigned_integer
5133 (map.cu_table_reordered + i * map.offset_size,
5134 map.offset_size,
5135 map.dwarf5_byte_order));
5136 /* We don't know the length of the CU, because the CU list in a
5137 .debug_names index can be incomplete, so we can't use the start of
5138 the next CU as end of this CU. We create the CUs here with length 0,
5139 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5140 dwarf2_per_cu_data *per_cu
5141 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5142 sect_off, 0);
5143 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5144 }
5145 }
5146
5147 sect_offset sect_off_prev;
5148 for (uint32_t i = 0; i <= map.cu_count; ++i)
5149 {
5150 sect_offset sect_off_next;
5151 if (i < map.cu_count)
5152 {
5153 sect_off_next
5154 = (sect_offset) (extract_unsigned_integer
5155 (map.cu_table_reordered + i * map.offset_size,
5156 map.offset_size,
5157 map.dwarf5_byte_order));
5158 }
5159 else
5160 sect_off_next = (sect_offset) section.size;
5161 if (i >= 1)
5162 {
5163 const ULONGEST length = sect_off_next - sect_off_prev;
5164 dwarf2_per_cu_data *per_cu
5165 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
5166 sect_off_prev, length);
5167 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (per_cu);
5168 }
5169 sect_off_prev = sect_off_next;
5170 }
5171 }
5172
5173 /* Read the CU list from the mapped index, and use it to create all
5174 the CU objects for this dwarf2_per_objfile. */
5175
5176 static void
5177 create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
5178 const mapped_debug_names &map,
5179 const mapped_debug_names &dwz_map)
5180 {
5181 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
5182 dwarf2_per_objfile->per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5183
5184 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5185 dwarf2_per_objfile->per_bfd->info,
5186 false /* is_dwz */);
5187
5188 if (dwz_map.cu_count == 0)
5189 return;
5190
5191 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5192 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
5193 true /* is_dwz */);
5194 }
5195
5196 /* Read .debug_names. If everything went ok, initialize the "quick"
5197 elements of all the CUs and return true. Otherwise, return false. */
5198
5199 static bool
5200 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5201 {
5202 std::unique_ptr<mapped_debug_names> map
5203 (new mapped_debug_names (dwarf2_per_objfile));
5204 mapped_debug_names dwz_map (dwarf2_per_objfile);
5205 struct objfile *objfile = dwarf2_per_objfile->objfile;
5206
5207 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5208 &dwarf2_per_objfile->per_bfd->debug_names,
5209 *map))
5210 return false;
5211
5212 /* Don't use the index if it's empty. */
5213 if (map->name_count == 0)
5214 return false;
5215
5216 /* If there is a .dwz file, read it so we can get its CU list as
5217 well. */
5218 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5219 if (dwz != NULL)
5220 {
5221 if (!read_debug_names_from_section (objfile,
5222 bfd_get_filename (dwz->dwz_bfd.get ()),
5223 &dwz->debug_names, dwz_map))
5224 {
5225 warning (_("could not read '.debug_names' section from %s; skipping"),
5226 bfd_get_filename (dwz->dwz_bfd.get ()));
5227 return false;
5228 }
5229 }
5230
5231 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
5232
5233 if (map->tu_count != 0)
5234 {
5235 /* We can only handle a single .debug_types when we have an
5236 index. */
5237 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5238 return false;
5239
5240 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5241
5242 create_signatured_type_table_from_debug_names
5243 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5244 }
5245
5246 create_addrmap_from_aranges (dwarf2_per_objfile,
5247 &dwarf2_per_objfile->per_bfd->debug_aranges);
5248
5249 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5250 dwarf2_per_objfile->per_bfd->using_index = 1;
5251 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5252 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5253
5254 return true;
5255 }
5256
5257 /* Type used to manage iterating over all CUs looking for a symbol for
5258 .debug_names. */
5259
5260 class dw2_debug_names_iterator
5261 {
5262 public:
5263 dw2_debug_names_iterator (const mapped_debug_names &map,
5264 gdb::optional<block_enum> block_index,
5265 domain_enum domain,
5266 const char *name)
5267 : m_map (map), m_block_index (block_index), m_domain (domain),
5268 m_addr (find_vec_in_debug_names (map, name))
5269 {}
5270
5271 dw2_debug_names_iterator (const mapped_debug_names &map,
5272 search_domain search, uint32_t namei)
5273 : m_map (map),
5274 m_search (search),
5275 m_addr (find_vec_in_debug_names (map, namei))
5276 {}
5277
5278 dw2_debug_names_iterator (const mapped_debug_names &map,
5279 block_enum block_index, domain_enum domain,
5280 uint32_t namei)
5281 : m_map (map), m_block_index (block_index), m_domain (domain),
5282 m_addr (find_vec_in_debug_names (map, namei))
5283 {}
5284
5285 /* Return the next matching CU or NULL if there are no more. */
5286 dwarf2_per_cu_data *next ();
5287
5288 private:
5289 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5290 const char *name);
5291 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5292 uint32_t namei);
5293
5294 /* The internalized form of .debug_names. */
5295 const mapped_debug_names &m_map;
5296
5297 /* If set, only look for symbols that match that block. Valid values are
5298 GLOBAL_BLOCK and STATIC_BLOCK. */
5299 const gdb::optional<block_enum> m_block_index;
5300
5301 /* The kind of symbol we're looking for. */
5302 const domain_enum m_domain = UNDEF_DOMAIN;
5303 const search_domain m_search = ALL_DOMAIN;
5304
5305 /* The list of CUs from the index entry of the symbol, or NULL if
5306 not found. */
5307 const gdb_byte *m_addr;
5308 };
5309
5310 const char *
5311 mapped_debug_names::namei_to_name (uint32_t namei) const
5312 {
5313 const ULONGEST namei_string_offs
5314 = extract_unsigned_integer ((name_table_string_offs_reordered
5315 + namei * offset_size),
5316 offset_size,
5317 dwarf5_byte_order);
5318 return read_indirect_string_at_offset (dwarf2_per_objfile,
5319 namei_string_offs);
5320 }
5321
5322 /* Find a slot in .debug_names for the object named NAME. If NAME is
5323 found, return pointer to its pool data. If NAME cannot be found,
5324 return NULL. */
5325
5326 const gdb_byte *
5327 dw2_debug_names_iterator::find_vec_in_debug_names
5328 (const mapped_debug_names &map, const char *name)
5329 {
5330 int (*cmp) (const char *, const char *);
5331
5332 gdb::unique_xmalloc_ptr<char> without_params;
5333 if (current_language->la_language == language_cplus
5334 || current_language->la_language == language_fortran
5335 || current_language->la_language == language_d)
5336 {
5337 /* NAME is already canonical. Drop any qualifiers as
5338 .debug_names does not contain any. */
5339
5340 if (strchr (name, '(') != NULL)
5341 {
5342 without_params = cp_remove_params (name);
5343 if (without_params != NULL)
5344 name = without_params.get ();
5345 }
5346 }
5347
5348 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5349
5350 const uint32_t full_hash = dwarf5_djb_hash (name);
5351 uint32_t namei
5352 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5353 (map.bucket_table_reordered
5354 + (full_hash % map.bucket_count)), 4,
5355 map.dwarf5_byte_order);
5356 if (namei == 0)
5357 return NULL;
5358 --namei;
5359 if (namei >= map.name_count)
5360 {
5361 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5362 "[in module %s]"),
5363 namei, map.name_count,
5364 objfile_name (map.dwarf2_per_objfile->objfile));
5365 return NULL;
5366 }
5367
5368 for (;;)
5369 {
5370 const uint32_t namei_full_hash
5371 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5372 (map.hash_table_reordered + namei), 4,
5373 map.dwarf5_byte_order);
5374 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5375 return NULL;
5376
5377 if (full_hash == namei_full_hash)
5378 {
5379 const char *const namei_string = map.namei_to_name (namei);
5380
5381 #if 0 /* An expensive sanity check. */
5382 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5383 {
5384 complaint (_("Wrong .debug_names hash for string at index %u "
5385 "[in module %s]"),
5386 namei, objfile_name (dwarf2_per_objfile->objfile));
5387 return NULL;
5388 }
5389 #endif
5390
5391 if (cmp (namei_string, name) == 0)
5392 {
5393 const ULONGEST namei_entry_offs
5394 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5395 + namei * map.offset_size),
5396 map.offset_size, map.dwarf5_byte_order);
5397 return map.entry_pool + namei_entry_offs;
5398 }
5399 }
5400
5401 ++namei;
5402 if (namei >= map.name_count)
5403 return NULL;
5404 }
5405 }
5406
5407 const gdb_byte *
5408 dw2_debug_names_iterator::find_vec_in_debug_names
5409 (const mapped_debug_names &map, uint32_t namei)
5410 {
5411 if (namei >= map.name_count)
5412 {
5413 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5414 "[in module %s]"),
5415 namei, map.name_count,
5416 objfile_name (map.dwarf2_per_objfile->objfile));
5417 return NULL;
5418 }
5419
5420 const ULONGEST namei_entry_offs
5421 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5422 + namei * map.offset_size),
5423 map.offset_size, map.dwarf5_byte_order);
5424 return map.entry_pool + namei_entry_offs;
5425 }
5426
5427 /* See dw2_debug_names_iterator. */
5428
5429 dwarf2_per_cu_data *
5430 dw2_debug_names_iterator::next ()
5431 {
5432 if (m_addr == NULL)
5433 return NULL;
5434
5435 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5436 struct objfile *objfile = dwarf2_per_objfile->objfile;
5437 bfd *const abfd = objfile->obfd;
5438
5439 again:
5440
5441 unsigned int bytes_read;
5442 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5443 m_addr += bytes_read;
5444 if (abbrev == 0)
5445 return NULL;
5446
5447 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5448 if (indexval_it == m_map.abbrev_map.cend ())
5449 {
5450 complaint (_("Wrong .debug_names undefined abbrev code %s "
5451 "[in module %s]"),
5452 pulongest (abbrev), objfile_name (objfile));
5453 return NULL;
5454 }
5455 const mapped_debug_names::index_val &indexval = indexval_it->second;
5456 enum class symbol_linkage {
5457 unknown,
5458 static_,
5459 extern_,
5460 } symbol_linkage_ = symbol_linkage::unknown;
5461 dwarf2_per_cu_data *per_cu = NULL;
5462 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5463 {
5464 ULONGEST ull;
5465 switch (attr.form)
5466 {
5467 case DW_FORM_implicit_const:
5468 ull = attr.implicit_const;
5469 break;
5470 case DW_FORM_flag_present:
5471 ull = 1;
5472 break;
5473 case DW_FORM_udata:
5474 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5475 m_addr += bytes_read;
5476 break;
5477 case DW_FORM_ref4:
5478 ull = read_4_bytes (abfd, m_addr);
5479 m_addr += 4;
5480 break;
5481 case DW_FORM_ref8:
5482 ull = read_8_bytes (abfd, m_addr);
5483 m_addr += 8;
5484 break;
5485 case DW_FORM_ref_sig8:
5486 ull = read_8_bytes (abfd, m_addr);
5487 m_addr += 8;
5488 break;
5489 default:
5490 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5491 dwarf_form_name (attr.form),
5492 objfile_name (objfile));
5493 return NULL;
5494 }
5495 switch (attr.dw_idx)
5496 {
5497 case DW_IDX_compile_unit:
5498 /* Don't crash on bad data. */
5499 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
5500 {
5501 complaint (_(".debug_names entry has bad CU index %s"
5502 " [in module %s]"),
5503 pulongest (ull),
5504 objfile_name (dwarf2_per_objfile->objfile));
5505 continue;
5506 }
5507 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
5508 break;
5509 case DW_IDX_type_unit:
5510 /* Don't crash on bad data. */
5511 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
5512 {
5513 complaint (_(".debug_names entry has bad TU index %s"
5514 " [in module %s]"),
5515 pulongest (ull),
5516 objfile_name (dwarf2_per_objfile->objfile));
5517 continue;
5518 }
5519 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
5520 break;
5521 case DW_IDX_die_offset:
5522 /* In a per-CU index (as opposed to a per-module index), index
5523 entries without CU attribute implicitly refer to the single CU. */
5524 if (per_cu == NULL)
5525 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
5526 break;
5527 case DW_IDX_GNU_internal:
5528 if (!m_map.augmentation_is_gdb)
5529 break;
5530 symbol_linkage_ = symbol_linkage::static_;
5531 break;
5532 case DW_IDX_GNU_external:
5533 if (!m_map.augmentation_is_gdb)
5534 break;
5535 symbol_linkage_ = symbol_linkage::extern_;
5536 break;
5537 }
5538 }
5539
5540 /* Skip if already read in. */
5541 if (dwarf2_per_objfile->symtab_set_p (per_cu))
5542 goto again;
5543
5544 /* Check static vs global. */
5545 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5546 {
5547 const bool want_static = *m_block_index == STATIC_BLOCK;
5548 const bool symbol_is_static =
5549 symbol_linkage_ == symbol_linkage::static_;
5550 if (want_static != symbol_is_static)
5551 goto again;
5552 }
5553
5554 /* Match dw2_symtab_iter_next, symbol_kind
5555 and debug_names::psymbol_tag. */
5556 switch (m_domain)
5557 {
5558 case VAR_DOMAIN:
5559 switch (indexval.dwarf_tag)
5560 {
5561 case DW_TAG_variable:
5562 case DW_TAG_subprogram:
5563 /* Some types are also in VAR_DOMAIN. */
5564 case DW_TAG_typedef:
5565 case DW_TAG_structure_type:
5566 break;
5567 default:
5568 goto again;
5569 }
5570 break;
5571 case STRUCT_DOMAIN:
5572 switch (indexval.dwarf_tag)
5573 {
5574 case DW_TAG_typedef:
5575 case DW_TAG_structure_type:
5576 break;
5577 default:
5578 goto again;
5579 }
5580 break;
5581 case LABEL_DOMAIN:
5582 switch (indexval.dwarf_tag)
5583 {
5584 case 0:
5585 case DW_TAG_variable:
5586 break;
5587 default:
5588 goto again;
5589 }
5590 break;
5591 case MODULE_DOMAIN:
5592 switch (indexval.dwarf_tag)
5593 {
5594 case DW_TAG_module:
5595 break;
5596 default:
5597 goto again;
5598 }
5599 break;
5600 default:
5601 break;
5602 }
5603
5604 /* Match dw2_expand_symtabs_matching, symbol_kind and
5605 debug_names::psymbol_tag. */
5606 switch (m_search)
5607 {
5608 case VARIABLES_DOMAIN:
5609 switch (indexval.dwarf_tag)
5610 {
5611 case DW_TAG_variable:
5612 break;
5613 default:
5614 goto again;
5615 }
5616 break;
5617 case FUNCTIONS_DOMAIN:
5618 switch (indexval.dwarf_tag)
5619 {
5620 case DW_TAG_subprogram:
5621 break;
5622 default:
5623 goto again;
5624 }
5625 break;
5626 case TYPES_DOMAIN:
5627 switch (indexval.dwarf_tag)
5628 {
5629 case DW_TAG_typedef:
5630 case DW_TAG_structure_type:
5631 break;
5632 default:
5633 goto again;
5634 }
5635 break;
5636 case MODULES_DOMAIN:
5637 switch (indexval.dwarf_tag)
5638 {
5639 case DW_TAG_module:
5640 break;
5641 default:
5642 goto again;
5643 }
5644 default:
5645 break;
5646 }
5647
5648 return per_cu;
5649 }
5650
5651 static struct compunit_symtab *
5652 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5653 const char *name, domain_enum domain)
5654 {
5655 struct dwarf2_per_objfile *dwarf2_per_objfile
5656 = get_dwarf2_per_objfile (objfile);
5657
5658 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5659 if (!mapp)
5660 {
5661 /* index is NULL if OBJF_READNOW. */
5662 return NULL;
5663 }
5664 const auto &map = *mapp;
5665
5666 dw2_debug_names_iterator iter (map, block_index, domain, name);
5667
5668 struct compunit_symtab *stab_best = NULL;
5669 struct dwarf2_per_cu_data *per_cu;
5670 while ((per_cu = iter.next ()) != NULL)
5671 {
5672 struct symbol *sym, *with_opaque = NULL;
5673 compunit_symtab *stab
5674 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5675 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5676 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5677
5678 sym = block_find_symbol (block, name, domain,
5679 block_find_non_opaque_type_preferred,
5680 &with_opaque);
5681
5682 /* Some caution must be observed with overloaded functions and
5683 methods, since the index will not contain any overload
5684 information (but NAME might contain it). */
5685
5686 if (sym != NULL
5687 && strcmp_iw (sym->search_name (), name) == 0)
5688 return stab;
5689 if (with_opaque != NULL
5690 && strcmp_iw (with_opaque->search_name (), name) == 0)
5691 stab_best = stab;
5692
5693 /* Keep looking through other CUs. */
5694 }
5695
5696 return stab_best;
5697 }
5698
5699 /* This dumps minimal information about .debug_names. It is called
5700 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5701 uses this to verify that .debug_names has been loaded. */
5702
5703 static void
5704 dw2_debug_names_dump (struct objfile *objfile)
5705 {
5706 struct dwarf2_per_objfile *dwarf2_per_objfile
5707 = get_dwarf2_per_objfile (objfile);
5708
5709 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5710 printf_filtered (".debug_names:");
5711 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5712 printf_filtered (" exists\n");
5713 else
5714 printf_filtered (" faked for \"readnow\"\n");
5715 printf_filtered ("\n");
5716 }
5717
5718 static void
5719 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5720 const char *func_name)
5721 {
5722 struct dwarf2_per_objfile *dwarf2_per_objfile
5723 = get_dwarf2_per_objfile (objfile);
5724
5725 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5726 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5727 {
5728 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5729
5730 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
5731
5732 struct dwarf2_per_cu_data *per_cu;
5733 while ((per_cu = iter.next ()) != NULL)
5734 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5735 }
5736 }
5737
5738 static void
5739 dw2_debug_names_map_matching_symbols
5740 (struct objfile *objfile,
5741 const lookup_name_info &name, domain_enum domain,
5742 int global,
5743 gdb::function_view<symbol_found_callback_ftype> callback,
5744 symbol_compare_ftype *ordered_compare)
5745 {
5746 struct dwarf2_per_objfile *dwarf2_per_objfile
5747 = get_dwarf2_per_objfile (objfile);
5748
5749 /* debug_names_table is NULL if OBJF_READNOW. */
5750 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5751 return;
5752
5753 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5754 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5755
5756 const char *match_name = name.ada ().lookup_name ().c_str ();
5757 auto matcher = [&] (const char *symname)
5758 {
5759 if (ordered_compare == nullptr)
5760 return true;
5761 return ordered_compare (symname, match_name) == 0;
5762 };
5763
5764 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5765 [&] (offset_type namei)
5766 {
5767 /* The name was matched, now expand corresponding CUs that were
5768 marked. */
5769 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5770
5771 struct dwarf2_per_cu_data *per_cu;
5772 while ((per_cu = iter.next ()) != NULL)
5773 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5774 nullptr);
5775 return true;
5776 });
5777
5778 /* It's a shame we couldn't do this inside the
5779 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5780 that have already been expanded. Instead, this loop matches what
5781 the psymtab code does. */
5782 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5783 {
5784 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5785 if (symtab != nullptr)
5786 {
5787 const struct block *block
5788 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5789 if (!iterate_over_symbols_terminated (block, name,
5790 domain, callback))
5791 break;
5792 }
5793 }
5794 }
5795
5796 static void
5797 dw2_debug_names_expand_symtabs_matching
5798 (struct objfile *objfile,
5799 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5800 const lookup_name_info *lookup_name,
5801 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5802 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5803 enum search_domain kind)
5804 {
5805 struct dwarf2_per_objfile *dwarf2_per_objfile
5806 = get_dwarf2_per_objfile (objfile);
5807
5808 /* debug_names_table is NULL if OBJF_READNOW. */
5809 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5810 return;
5811
5812 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5813
5814 if (symbol_matcher == NULL && lookup_name == NULL)
5815 {
5816 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5817 {
5818 QUIT;
5819
5820 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5821 file_matcher, expansion_notify);
5822 }
5823 return;
5824 }
5825
5826 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5827
5828 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5829 symbol_matcher,
5830 kind, [&] (offset_type namei)
5831 {
5832 /* The name was matched, now expand corresponding CUs that were
5833 marked. */
5834 dw2_debug_names_iterator iter (map, kind, namei);
5835
5836 struct dwarf2_per_cu_data *per_cu;
5837 while ((per_cu = iter.next ()) != NULL)
5838 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5839 file_matcher, expansion_notify);
5840 return true;
5841 });
5842 }
5843
5844 const struct quick_symbol_functions dwarf2_debug_names_functions =
5845 {
5846 dw2_has_symbols,
5847 dw2_find_last_source_symtab,
5848 dw2_forget_cached_source_info,
5849 dw2_map_symtabs_matching_filename,
5850 dw2_debug_names_lookup_symbol,
5851 NULL,
5852 dw2_print_stats,
5853 dw2_debug_names_dump,
5854 dw2_debug_names_expand_symtabs_for_function,
5855 dw2_expand_all_symtabs,
5856 dw2_expand_symtabs_with_fullname,
5857 dw2_debug_names_map_matching_symbols,
5858 dw2_debug_names_expand_symtabs_matching,
5859 dw2_find_pc_sect_compunit_symtab,
5860 NULL,
5861 dw2_map_symbol_filenames
5862 };
5863
5864 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5865 to either a dwarf2_per_bfd or dwz_file object. */
5866
5867 template <typename T>
5868 static gdb::array_view<const gdb_byte>
5869 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5870 {
5871 dwarf2_section_info *section = &section_owner->gdb_index;
5872
5873 if (section->empty ())
5874 return {};
5875
5876 /* Older elfutils strip versions could keep the section in the main
5877 executable while splitting it for the separate debug info file. */
5878 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5879 return {};
5880
5881 section->read (obj);
5882
5883 /* dwarf2_section_info::size is a bfd_size_type, while
5884 gdb::array_view works with size_t. On 32-bit hosts, with
5885 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5886 is 32-bit. So we need an explicit narrowing conversion here.
5887 This is fine, because it's impossible to allocate or mmap an
5888 array/buffer larger than what size_t can represent. */
5889 return gdb::make_array_view (section->buffer, section->size);
5890 }
5891
5892 /* Lookup the index cache for the contents of the index associated to
5893 DWARF2_OBJ. */
5894
5895 static gdb::array_view<const gdb_byte>
5896 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5897 {
5898 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5899 if (build_id == nullptr)
5900 return {};
5901
5902 return global_index_cache.lookup_gdb_index (build_id,
5903 &dwarf2_per_bfd->index_cache_res);
5904 }
5905
5906 /* Same as the above, but for DWZ. */
5907
5908 static gdb::array_view<const gdb_byte>
5909 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5910 {
5911 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5912 if (build_id == nullptr)
5913 return {};
5914
5915 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5916 }
5917
5918 /* See symfile.h. */
5919
5920 bool
5921 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5922 {
5923 struct dwarf2_per_objfile *dwarf2_per_objfile
5924 = get_dwarf2_per_objfile (objfile);
5925
5926 /* If we're about to read full symbols, don't bother with the
5927 indices. In this case we also don't care if some other debug
5928 format is making psymtabs, because they are all about to be
5929 expanded anyway. */
5930 if ((objfile->flags & OBJF_READNOW))
5931 {
5932 dwarf2_per_objfile->per_bfd->using_index = 1;
5933 create_all_comp_units (dwarf2_per_objfile);
5934 create_all_type_units (dwarf2_per_objfile);
5935 dwarf2_per_objfile->per_bfd->quick_file_names_table
5936 = create_quick_file_names_table
5937 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5938 dwarf2_per_objfile->resize_symtabs ();
5939
5940 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5941 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5942 {
5943 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5944
5945 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5946 struct dwarf2_per_cu_quick_data);
5947 }
5948
5949 /* Return 1 so that gdb sees the "quick" functions. However,
5950 these functions will be no-ops because we will have expanded
5951 all symtabs. */
5952 *index_kind = dw_index_kind::GDB_INDEX;
5953 return true;
5954 }
5955
5956 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5957 {
5958 *index_kind = dw_index_kind::DEBUG_NAMES;
5959 dwarf2_per_objfile->resize_symtabs ();
5960 return true;
5961 }
5962
5963 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5964 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5965 get_gdb_index_contents_from_section<dwz_file>))
5966 {
5967 *index_kind = dw_index_kind::GDB_INDEX;
5968 dwarf2_per_objfile->resize_symtabs ();
5969 return true;
5970 }
5971
5972 /* ... otherwise, try to find the index in the index cache. */
5973 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5974 get_gdb_index_contents_from_cache,
5975 get_gdb_index_contents_from_cache_dwz))
5976 {
5977 global_index_cache.hit ();
5978 *index_kind = dw_index_kind::GDB_INDEX;
5979 dwarf2_per_objfile->resize_symtabs ();
5980 return true;
5981 }
5982
5983 global_index_cache.miss ();
5984 return false;
5985 }
5986
5987 \f
5988
5989 /* Build a partial symbol table. */
5990
5991 void
5992 dwarf2_build_psymtabs (struct objfile *objfile)
5993 {
5994 struct dwarf2_per_objfile *dwarf2_per_objfile
5995 = get_dwarf2_per_objfile (objfile);
5996
5997 init_psymbol_list (objfile, 1024);
5998
5999 try
6000 {
6001 /* This isn't really ideal: all the data we allocate on the
6002 objfile's obstack is still uselessly kept around. However,
6003 freeing it seems unsafe. */
6004 psymtab_discarder psymtabs (objfile);
6005 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
6006 psymtabs.keep ();
6007
6008 dwarf2_per_objfile->resize_symtabs ();
6009
6010 /* (maybe) store an index in the cache. */
6011 global_index_cache.store (dwarf2_per_objfile);
6012 }
6013 catch (const gdb_exception_error &except)
6014 {
6015 exception_print (gdb_stderr, except);
6016 }
6017 }
6018
6019 /* Find the base address of the compilation unit for range lists and
6020 location lists. It will normally be specified by DW_AT_low_pc.
6021 In DWARF-3 draft 4, the base address could be overridden by
6022 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6023 compilation units with discontinuous ranges. */
6024
6025 static void
6026 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6027 {
6028 struct attribute *attr;
6029
6030 cu->base_address.reset ();
6031
6032 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6033 if (attr != nullptr)
6034 cu->base_address = attr->value_as_address ();
6035 else
6036 {
6037 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6038 if (attr != nullptr)
6039 cu->base_address = attr->value_as_address ();
6040 }
6041 }
6042
6043 /* Helper function that returns the proper abbrev section for
6044 THIS_CU. */
6045
6046 static struct dwarf2_section_info *
6047 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6048 {
6049 struct dwarf2_section_info *abbrev;
6050 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6051
6052 if (this_cu->is_dwz)
6053 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6054 else
6055 abbrev = &per_bfd->abbrev;
6056
6057 return abbrev;
6058 }
6059
6060 /* Fetch the abbreviation table offset from a comp or type unit header. */
6061
6062 static sect_offset
6063 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6064 struct dwarf2_section_info *section,
6065 sect_offset sect_off)
6066 {
6067 bfd *abfd = section->get_bfd_owner ();
6068 const gdb_byte *info_ptr;
6069 unsigned int initial_length_size, offset_size;
6070 uint16_t version;
6071
6072 section->read (dwarf2_per_objfile->objfile);
6073 info_ptr = section->buffer + to_underlying (sect_off);
6074 read_initial_length (abfd, info_ptr, &initial_length_size);
6075 offset_size = initial_length_size == 4 ? 4 : 8;
6076 info_ptr += initial_length_size;
6077
6078 version = read_2_bytes (abfd, info_ptr);
6079 info_ptr += 2;
6080 if (version >= 5)
6081 {
6082 /* Skip unit type and address size. */
6083 info_ptr += 2;
6084 }
6085
6086 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6087 }
6088
6089 /* A partial symtab that is used only for include files. */
6090 struct dwarf2_include_psymtab : public partial_symtab
6091 {
6092 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6093 : partial_symtab (filename, objfile)
6094 {
6095 }
6096
6097 void read_symtab (struct objfile *objfile) override
6098 {
6099 /* It's an include file, no symbols to read for it.
6100 Everything is in the includer symtab. */
6101
6102 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6103 expansion of the includer psymtab. We use the dependencies[0] field to
6104 model the includer. But if we go the regular route of calling
6105 expand_psymtab here, and having expand_psymtab call expand_dependencies
6106 to expand the includer, we'll only use expand_psymtab on the includer
6107 (making it a non-toplevel psymtab), while if we expand the includer via
6108 another path, we'll use read_symtab (making it a toplevel psymtab).
6109 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6110 psymtab, and trigger read_symtab on the includer here directly. */
6111 includer ()->read_symtab (objfile);
6112 }
6113
6114 void expand_psymtab (struct objfile *objfile) override
6115 {
6116 /* This is not called by read_symtab, and should not be called by any
6117 expand_dependencies. */
6118 gdb_assert (false);
6119 }
6120
6121 bool readin_p (struct objfile *objfile) const override
6122 {
6123 return includer ()->readin_p (objfile);
6124 }
6125
6126 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6127 {
6128 return nullptr;
6129 }
6130
6131 private:
6132 partial_symtab *includer () const
6133 {
6134 /* An include psymtab has exactly one dependency: the psymtab that
6135 includes it. */
6136 gdb_assert (this->number_of_dependencies == 1);
6137 return this->dependencies[0];
6138 }
6139 };
6140
6141 /* Allocate a new partial symtab for file named NAME and mark this new
6142 partial symtab as being an include of PST. */
6143
6144 static void
6145 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6146 struct objfile *objfile)
6147 {
6148 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6149
6150 if (!IS_ABSOLUTE_PATH (subpst->filename))
6151 subpst->dirname = pst->dirname;
6152
6153 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6154 subpst->dependencies[0] = pst;
6155 subpst->number_of_dependencies = 1;
6156 }
6157
6158 /* Read the Line Number Program data and extract the list of files
6159 included by the source file represented by PST. Build an include
6160 partial symtab for each of these included files. */
6161
6162 static void
6163 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6164 struct die_info *die,
6165 dwarf2_psymtab *pst)
6166 {
6167 line_header_up lh;
6168 struct attribute *attr;
6169
6170 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6171 if (attr != nullptr)
6172 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6173 if (lh == NULL)
6174 return; /* No linetable, so no includes. */
6175
6176 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6177 that we pass in the raw text_low here; that is ok because we're
6178 only decoding the line table to make include partial symtabs, and
6179 so the addresses aren't really used. */
6180 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6181 pst->raw_text_low (), 1);
6182 }
6183
6184 static hashval_t
6185 hash_signatured_type (const void *item)
6186 {
6187 const struct signatured_type *sig_type
6188 = (const struct signatured_type *) item;
6189
6190 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6191 return sig_type->signature;
6192 }
6193
6194 static int
6195 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6196 {
6197 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6198 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6199
6200 return lhs->signature == rhs->signature;
6201 }
6202
6203 /* Allocate a hash table for signatured types. */
6204
6205 static htab_up
6206 allocate_signatured_type_table ()
6207 {
6208 return htab_up (htab_create_alloc (41,
6209 hash_signatured_type,
6210 eq_signatured_type,
6211 NULL, xcalloc, xfree));
6212 }
6213
6214 /* A helper function to add a signatured type CU to a table. */
6215
6216 static int
6217 add_signatured_type_cu_to_table (void **slot, void *datum)
6218 {
6219 struct signatured_type *sigt = (struct signatured_type *) *slot;
6220 std::vector<signatured_type *> *all_type_units
6221 = (std::vector<signatured_type *> *) datum;
6222
6223 all_type_units->push_back (sigt);
6224
6225 return 1;
6226 }
6227
6228 /* A helper for create_debug_types_hash_table. Read types from SECTION
6229 and fill them into TYPES_HTAB. It will process only type units,
6230 therefore DW_UT_type. */
6231
6232 static void
6233 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6234 struct dwo_file *dwo_file,
6235 dwarf2_section_info *section, htab_up &types_htab,
6236 rcuh_kind section_kind)
6237 {
6238 struct objfile *objfile = dwarf2_per_objfile->objfile;
6239 struct dwarf2_section_info *abbrev_section;
6240 bfd *abfd;
6241 const gdb_byte *info_ptr, *end_ptr;
6242
6243 abbrev_section = (dwo_file != NULL
6244 ? &dwo_file->sections.abbrev
6245 : &dwarf2_per_objfile->per_bfd->abbrev);
6246
6247 if (dwarf_read_debug)
6248 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6249 section->get_name (),
6250 abbrev_section->get_file_name ());
6251
6252 section->read (objfile);
6253 info_ptr = section->buffer;
6254
6255 if (info_ptr == NULL)
6256 return;
6257
6258 /* We can't set abfd until now because the section may be empty or
6259 not present, in which case the bfd is unknown. */
6260 abfd = section->get_bfd_owner ();
6261
6262 /* We don't use cutu_reader here because we don't need to read
6263 any dies: the signature is in the header. */
6264
6265 end_ptr = info_ptr + section->size;
6266 while (info_ptr < end_ptr)
6267 {
6268 struct signatured_type *sig_type;
6269 struct dwo_unit *dwo_tu;
6270 void **slot;
6271 const gdb_byte *ptr = info_ptr;
6272 struct comp_unit_head header;
6273 unsigned int length;
6274
6275 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6276
6277 /* Initialize it due to a false compiler warning. */
6278 header.signature = -1;
6279 header.type_cu_offset_in_tu = (cu_offset) -1;
6280
6281 /* We need to read the type's signature in order to build the hash
6282 table, but we don't need anything else just yet. */
6283
6284 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6285 abbrev_section, ptr, section_kind);
6286
6287 length = header.get_length ();
6288
6289 /* Skip dummy type units. */
6290 if (ptr >= info_ptr + length
6291 || peek_abbrev_code (abfd, ptr) == 0
6292 || header.unit_type != DW_UT_type)
6293 {
6294 info_ptr += length;
6295 continue;
6296 }
6297
6298 if (types_htab == NULL)
6299 {
6300 if (dwo_file)
6301 types_htab = allocate_dwo_unit_table ();
6302 else
6303 types_htab = allocate_signatured_type_table ();
6304 }
6305
6306 if (dwo_file)
6307 {
6308 sig_type = NULL;
6309 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6310 struct dwo_unit);
6311 dwo_tu->dwo_file = dwo_file;
6312 dwo_tu->signature = header.signature;
6313 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6314 dwo_tu->section = section;
6315 dwo_tu->sect_off = sect_off;
6316 dwo_tu->length = length;
6317 }
6318 else
6319 {
6320 /* N.B.: type_offset is not usable if this type uses a DWO file.
6321 The real type_offset is in the DWO file. */
6322 dwo_tu = NULL;
6323 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6324 sig_type->signature = header.signature;
6325 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6326 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6327 sig_type->per_cu.is_debug_types = 1;
6328 sig_type->per_cu.section = section;
6329 sig_type->per_cu.sect_off = sect_off;
6330 sig_type->per_cu.length = length;
6331 }
6332
6333 slot = htab_find_slot (types_htab.get (),
6334 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6335 INSERT);
6336 gdb_assert (slot != NULL);
6337 if (*slot != NULL)
6338 {
6339 sect_offset dup_sect_off;
6340
6341 if (dwo_file)
6342 {
6343 const struct dwo_unit *dup_tu
6344 = (const struct dwo_unit *) *slot;
6345
6346 dup_sect_off = dup_tu->sect_off;
6347 }
6348 else
6349 {
6350 const struct signatured_type *dup_tu
6351 = (const struct signatured_type *) *slot;
6352
6353 dup_sect_off = dup_tu->per_cu.sect_off;
6354 }
6355
6356 complaint (_("debug type entry at offset %s is duplicate to"
6357 " the entry at offset %s, signature %s"),
6358 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6359 hex_string (header.signature));
6360 }
6361 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6362
6363 if (dwarf_read_debug > 1)
6364 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6365 sect_offset_str (sect_off),
6366 hex_string (header.signature));
6367
6368 info_ptr += length;
6369 }
6370 }
6371
6372 /* Create the hash table of all entries in the .debug_types
6373 (or .debug_types.dwo) section(s).
6374 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6375 otherwise it is NULL.
6376
6377 The result is a pointer to the hash table or NULL if there are no types.
6378
6379 Note: This function processes DWO files only, not DWP files. */
6380
6381 static void
6382 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6383 struct dwo_file *dwo_file,
6384 gdb::array_view<dwarf2_section_info> type_sections,
6385 htab_up &types_htab)
6386 {
6387 for (dwarf2_section_info &section : type_sections)
6388 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6389 types_htab, rcuh_kind::TYPE);
6390 }
6391
6392 /* Create the hash table of all entries in the .debug_types section,
6393 and initialize all_type_units.
6394 The result is zero if there is an error (e.g. missing .debug_types section),
6395 otherwise non-zero. */
6396
6397 static int
6398 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6399 {
6400 htab_up types_htab;
6401
6402 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6403 &dwarf2_per_objfile->per_bfd->info, types_htab,
6404 rcuh_kind::COMPILE);
6405 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6406 dwarf2_per_objfile->per_bfd->types, types_htab);
6407 if (types_htab == NULL)
6408 {
6409 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6410 return 0;
6411 }
6412
6413 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6414
6415 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6416 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6417 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6418
6419 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6420 add_signatured_type_cu_to_table,
6421 &dwarf2_per_objfile->per_bfd->all_type_units);
6422
6423 return 1;
6424 }
6425
6426 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6427 If SLOT is non-NULL, it is the entry to use in the hash table.
6428 Otherwise we find one. */
6429
6430 static struct signatured_type *
6431 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6432 void **slot)
6433 {
6434 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6435 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6436 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6437
6438 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6439
6440 dwarf2_per_objfile->resize_symtabs ();
6441
6442 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6443 sig_type->signature = sig;
6444 sig_type->per_cu.is_debug_types = 1;
6445 if (dwarf2_per_objfile->per_bfd->using_index)
6446 {
6447 sig_type->per_cu.v.quick =
6448 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6449 struct dwarf2_per_cu_quick_data);
6450 }
6451
6452 if (slot == NULL)
6453 {
6454 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6455 sig_type, INSERT);
6456 }
6457 gdb_assert (*slot == NULL);
6458 *slot = sig_type;
6459 /* The rest of sig_type must be filled in by the caller. */
6460 return sig_type;
6461 }
6462
6463 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6464 Fill in SIG_ENTRY with DWO_ENTRY. */
6465
6466 static void
6467 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6468 struct signatured_type *sig_entry,
6469 struct dwo_unit *dwo_entry)
6470 {
6471 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6472
6473 /* Make sure we're not clobbering something we don't expect to. */
6474 gdb_assert (! sig_entry->per_cu.queued);
6475 gdb_assert (sig_entry->per_cu.cu == NULL);
6476 if (per_bfd->using_index)
6477 {
6478 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6479 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6480 }
6481 else
6482 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6483 gdb_assert (sig_entry->signature == dwo_entry->signature);
6484 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6485 gdb_assert (sig_entry->type_unit_group == NULL);
6486 gdb_assert (sig_entry->dwo_unit == NULL);
6487
6488 sig_entry->per_cu.section = dwo_entry->section;
6489 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6490 sig_entry->per_cu.length = dwo_entry->length;
6491 sig_entry->per_cu.reading_dwo_directly = 1;
6492 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
6493 sig_entry->per_cu.per_bfd = per_bfd;
6494 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6495 sig_entry->dwo_unit = dwo_entry;
6496 }
6497
6498 /* Subroutine of lookup_signatured_type.
6499 If we haven't read the TU yet, create the signatured_type data structure
6500 for a TU to be read in directly from a DWO file, bypassing the stub.
6501 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6502 using .gdb_index, then when reading a CU we want to stay in the DWO file
6503 containing that CU. Otherwise we could end up reading several other DWO
6504 files (due to comdat folding) to process the transitive closure of all the
6505 mentioned TUs, and that can be slow. The current DWO file will have every
6506 type signature that it needs.
6507 We only do this for .gdb_index because in the psymtab case we already have
6508 to read all the DWOs to build the type unit groups. */
6509
6510 static struct signatured_type *
6511 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6512 {
6513 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6514 struct dwo_file *dwo_file;
6515 struct dwo_unit find_dwo_entry, *dwo_entry;
6516 struct signatured_type find_sig_entry, *sig_entry;
6517 void **slot;
6518
6519 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6520
6521 /* If TU skeletons have been removed then we may not have read in any
6522 TUs yet. */
6523 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6524 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6525
6526 /* We only ever need to read in one copy of a signatured type.
6527 Use the global signatured_types array to do our own comdat-folding
6528 of types. If this is the first time we're reading this TU, and
6529 the TU has an entry in .gdb_index, replace the recorded data from
6530 .gdb_index with this TU. */
6531
6532 find_sig_entry.signature = sig;
6533 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6534 &find_sig_entry, INSERT);
6535 sig_entry = (struct signatured_type *) *slot;
6536
6537 /* We can get here with the TU already read, *or* in the process of being
6538 read. Don't reassign the global entry to point to this DWO if that's
6539 the case. Also note that if the TU is already being read, it may not
6540 have come from a DWO, the program may be a mix of Fission-compiled
6541 code and non-Fission-compiled code. */
6542
6543 /* Have we already tried to read this TU?
6544 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6545 needn't exist in the global table yet). */
6546 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6547 return sig_entry;
6548
6549 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6550 dwo_unit of the TU itself. */
6551 dwo_file = cu->dwo_unit->dwo_file;
6552
6553 /* Ok, this is the first time we're reading this TU. */
6554 if (dwo_file->tus == NULL)
6555 return NULL;
6556 find_dwo_entry.signature = sig;
6557 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6558 &find_dwo_entry);
6559 if (dwo_entry == NULL)
6560 return NULL;
6561
6562 /* If the global table doesn't have an entry for this TU, add one. */
6563 if (sig_entry == NULL)
6564 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6565
6566 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6567 sig_entry->per_cu.tu_read = 1;
6568 return sig_entry;
6569 }
6570
6571 /* Subroutine of lookup_signatured_type.
6572 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6573 then try the DWP file. If the TU stub (skeleton) has been removed then
6574 it won't be in .gdb_index. */
6575
6576 static struct signatured_type *
6577 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6578 {
6579 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6580 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6581 struct dwo_unit *dwo_entry;
6582 struct signatured_type find_sig_entry, *sig_entry;
6583 void **slot;
6584
6585 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6586 gdb_assert (dwp_file != NULL);
6587
6588 /* If TU skeletons have been removed then we may not have read in any
6589 TUs yet. */
6590 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6591 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6592
6593 find_sig_entry.signature = sig;
6594 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6595 &find_sig_entry, INSERT);
6596 sig_entry = (struct signatured_type *) *slot;
6597
6598 /* Have we already tried to read this TU?
6599 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6600 needn't exist in the global table yet). */
6601 if (sig_entry != NULL)
6602 return sig_entry;
6603
6604 if (dwp_file->tus == NULL)
6605 return NULL;
6606 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6607 sig, 1 /* is_debug_types */);
6608 if (dwo_entry == NULL)
6609 return NULL;
6610
6611 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6612 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6613
6614 return sig_entry;
6615 }
6616
6617 /* Lookup a signature based type for DW_FORM_ref_sig8.
6618 Returns NULL if signature SIG is not present in the table.
6619 It is up to the caller to complain about this. */
6620
6621 static struct signatured_type *
6622 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6623 {
6624 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6625
6626 if (cu->dwo_unit
6627 && dwarf2_per_objfile->per_bfd->using_index)
6628 {
6629 /* We're in a DWO/DWP file, and we're using .gdb_index.
6630 These cases require special processing. */
6631 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6632 return lookup_dwo_signatured_type (cu, sig);
6633 else
6634 return lookup_dwp_signatured_type (cu, sig);
6635 }
6636 else
6637 {
6638 struct signatured_type find_entry, *entry;
6639
6640 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6641 return NULL;
6642 find_entry.signature = sig;
6643 entry = ((struct signatured_type *)
6644 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6645 &find_entry));
6646 return entry;
6647 }
6648 }
6649
6650 /* Low level DIE reading support. */
6651
6652 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6653
6654 static void
6655 init_cu_die_reader (struct die_reader_specs *reader,
6656 struct dwarf2_cu *cu,
6657 struct dwarf2_section_info *section,
6658 struct dwo_file *dwo_file,
6659 struct abbrev_table *abbrev_table)
6660 {
6661 gdb_assert (section->readin && section->buffer != NULL);
6662 reader->abfd = section->get_bfd_owner ();
6663 reader->cu = cu;
6664 reader->dwo_file = dwo_file;
6665 reader->die_section = section;
6666 reader->buffer = section->buffer;
6667 reader->buffer_end = section->buffer + section->size;
6668 reader->abbrev_table = abbrev_table;
6669 }
6670
6671 /* Subroutine of cutu_reader to simplify it.
6672 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6673 There's just a lot of work to do, and cutu_reader is big enough
6674 already.
6675
6676 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6677 from it to the DIE in the DWO. If NULL we are skipping the stub.
6678 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6679 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6680 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6681 STUB_COMP_DIR may be non-NULL.
6682 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6683 are filled in with the info of the DIE from the DWO file.
6684 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6685 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6686 kept around for at least as long as *RESULT_READER.
6687
6688 The result is non-zero if a valid (non-dummy) DIE was found. */
6689
6690 static int
6691 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6692 struct dwo_unit *dwo_unit,
6693 struct die_info *stub_comp_unit_die,
6694 const char *stub_comp_dir,
6695 struct die_reader_specs *result_reader,
6696 const gdb_byte **result_info_ptr,
6697 struct die_info **result_comp_unit_die,
6698 abbrev_table_up *result_dwo_abbrev_table)
6699 {
6700 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
6701 struct objfile *objfile = dwarf2_per_objfile->objfile;
6702 struct dwarf2_cu *cu = this_cu->cu;
6703 bfd *abfd;
6704 const gdb_byte *begin_info_ptr, *info_ptr;
6705 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6706 int i,num_extra_attrs;
6707 struct dwarf2_section_info *dwo_abbrev_section;
6708 struct die_info *comp_unit_die;
6709
6710 /* At most one of these may be provided. */
6711 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6712
6713 /* These attributes aren't processed until later:
6714 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6715 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6716 referenced later. However, these attributes are found in the stub
6717 which we won't have later. In order to not impose this complication
6718 on the rest of the code, we read them here and copy them to the
6719 DWO CU/TU die. */
6720
6721 stmt_list = NULL;
6722 low_pc = NULL;
6723 high_pc = NULL;
6724 ranges = NULL;
6725 comp_dir = NULL;
6726
6727 if (stub_comp_unit_die != NULL)
6728 {
6729 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6730 DWO file. */
6731 if (! this_cu->is_debug_types)
6732 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6733 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6734 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6735 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6736 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6737
6738 cu->addr_base = stub_comp_unit_die->addr_base ();
6739
6740 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6741 here (if needed). We need the value before we can process
6742 DW_AT_ranges. */
6743 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6744 }
6745 else if (stub_comp_dir != NULL)
6746 {
6747 /* Reconstruct the comp_dir attribute to simplify the code below. */
6748 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6749 comp_dir->name = DW_AT_comp_dir;
6750 comp_dir->form = DW_FORM_string;
6751 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6752 DW_STRING (comp_dir) = stub_comp_dir;
6753 }
6754
6755 /* Set up for reading the DWO CU/TU. */
6756 cu->dwo_unit = dwo_unit;
6757 dwarf2_section_info *section = dwo_unit->section;
6758 section->read (objfile);
6759 abfd = section->get_bfd_owner ();
6760 begin_info_ptr = info_ptr = (section->buffer
6761 + to_underlying (dwo_unit->sect_off));
6762 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6763
6764 if (this_cu->is_debug_types)
6765 {
6766 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6767
6768 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6769 &cu->header, section,
6770 dwo_abbrev_section,
6771 info_ptr, rcuh_kind::TYPE);
6772 /* This is not an assert because it can be caused by bad debug info. */
6773 if (sig_type->signature != cu->header.signature)
6774 {
6775 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6776 " TU at offset %s [in module %s]"),
6777 hex_string (sig_type->signature),
6778 hex_string (cu->header.signature),
6779 sect_offset_str (dwo_unit->sect_off),
6780 bfd_get_filename (abfd));
6781 }
6782 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6783 /* For DWOs coming from DWP files, we don't know the CU length
6784 nor the type's offset in the TU until now. */
6785 dwo_unit->length = cu->header.get_length ();
6786 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6787
6788 /* Establish the type offset that can be used to lookup the type.
6789 For DWO files, we don't know it until now. */
6790 sig_type->type_offset_in_section
6791 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6792 }
6793 else
6794 {
6795 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6796 &cu->header, section,
6797 dwo_abbrev_section,
6798 info_ptr, rcuh_kind::COMPILE);
6799 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6800 /* For DWOs coming from DWP files, we don't know the CU length
6801 until now. */
6802 dwo_unit->length = cu->header.get_length ();
6803 }
6804
6805 *result_dwo_abbrev_table
6806 = abbrev_table::read (objfile, dwo_abbrev_section,
6807 cu->header.abbrev_sect_off);
6808 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6809 result_dwo_abbrev_table->get ());
6810
6811 /* Read in the die, but leave space to copy over the attributes
6812 from the stub. This has the benefit of simplifying the rest of
6813 the code - all the work to maintain the illusion of a single
6814 DW_TAG_{compile,type}_unit DIE is done here. */
6815 num_extra_attrs = ((stmt_list != NULL)
6816 + (low_pc != NULL)
6817 + (high_pc != NULL)
6818 + (ranges != NULL)
6819 + (comp_dir != NULL));
6820 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6821 num_extra_attrs);
6822
6823 /* Copy over the attributes from the stub to the DIE we just read in. */
6824 comp_unit_die = *result_comp_unit_die;
6825 i = comp_unit_die->num_attrs;
6826 if (stmt_list != NULL)
6827 comp_unit_die->attrs[i++] = *stmt_list;
6828 if (low_pc != NULL)
6829 comp_unit_die->attrs[i++] = *low_pc;
6830 if (high_pc != NULL)
6831 comp_unit_die->attrs[i++] = *high_pc;
6832 if (ranges != NULL)
6833 comp_unit_die->attrs[i++] = *ranges;
6834 if (comp_dir != NULL)
6835 comp_unit_die->attrs[i++] = *comp_dir;
6836 comp_unit_die->num_attrs += num_extra_attrs;
6837
6838 if (dwarf_die_debug)
6839 {
6840 fprintf_unfiltered (gdb_stdlog,
6841 "Read die from %s@0x%x of %s:\n",
6842 section->get_name (),
6843 (unsigned) (begin_info_ptr - section->buffer),
6844 bfd_get_filename (abfd));
6845 dump_die (comp_unit_die, dwarf_die_debug);
6846 }
6847
6848 /* Skip dummy compilation units. */
6849 if (info_ptr >= begin_info_ptr + dwo_unit->length
6850 || peek_abbrev_code (abfd, info_ptr) == 0)
6851 return 0;
6852
6853 *result_info_ptr = info_ptr;
6854 return 1;
6855 }
6856
6857 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6858 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6859 signature is part of the header. */
6860 static gdb::optional<ULONGEST>
6861 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6862 {
6863 if (cu->header.version >= 5)
6864 return cu->header.signature;
6865 struct attribute *attr;
6866 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6867 if (attr == nullptr)
6868 return gdb::optional<ULONGEST> ();
6869 return DW_UNSND (attr);
6870 }
6871
6872 /* Subroutine of cutu_reader to simplify it.
6873 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6874 Returns NULL if the specified DWO unit cannot be found. */
6875
6876 static struct dwo_unit *
6877 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
6878 struct die_info *comp_unit_die,
6879 const char *dwo_name)
6880 {
6881 struct dwarf2_cu *cu = this_cu->cu;
6882 struct dwo_unit *dwo_unit;
6883 const char *comp_dir;
6884
6885 gdb_assert (cu != NULL);
6886
6887 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6888 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6889 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6890
6891 if (this_cu->is_debug_types)
6892 {
6893 struct signatured_type *sig_type;
6894
6895 /* Since this_cu is the first member of struct signatured_type,
6896 we can go from a pointer to one to a pointer to the other. */
6897 sig_type = (struct signatured_type *) this_cu;
6898 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6899 }
6900 else
6901 {
6902 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6903 if (!signature.has_value ())
6904 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6905 " [in module %s]"),
6906 dwo_name, bfd_get_filename (this_cu->per_bfd->obfd));
6907 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
6908 *signature);
6909 }
6910
6911 return dwo_unit;
6912 }
6913
6914 /* Subroutine of cutu_reader to simplify it.
6915 See it for a description of the parameters.
6916 Read a TU directly from a DWO file, bypassing the stub. */
6917
6918 void
6919 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6920 dwarf2_per_objfile *per_objfile,
6921 int use_existing_cu)
6922 {
6923 struct signatured_type *sig_type;
6924
6925 /* Verify we can do the following downcast, and that we have the
6926 data we need. */
6927 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6928 sig_type = (struct signatured_type *) this_cu;
6929 gdb_assert (sig_type->dwo_unit != NULL);
6930
6931 if (use_existing_cu && this_cu->cu != NULL)
6932 {
6933 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6934 /* There's no need to do the rereading_dwo_cu handling that
6935 cutu_reader does since we don't read the stub. */
6936 }
6937 else
6938 {
6939 /* If !use_existing_cu, this_cu->cu must be NULL. */
6940 gdb_assert (this_cu->cu == NULL);
6941 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6942 }
6943
6944 /* A future optimization, if needed, would be to use an existing
6945 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6946 could share abbrev tables. */
6947
6948 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
6949 NULL /* stub_comp_unit_die */,
6950 sig_type->dwo_unit->dwo_file->comp_dir,
6951 this, &info_ptr,
6952 &comp_unit_die,
6953 &m_dwo_abbrev_table) == 0)
6954 {
6955 /* Dummy die. */
6956 dummy_p = true;
6957 }
6958 }
6959
6960 /* Initialize a CU (or TU) and read its DIEs.
6961 If the CU defers to a DWO file, read the DWO file as well.
6962
6963 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6964 Otherwise the table specified in the comp unit header is read in and used.
6965 This is an optimization for when we already have the abbrev table.
6966
6967 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6968 Otherwise, a new CU is allocated with xmalloc. */
6969
6970 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6971 dwarf2_per_objfile *dwarf2_per_objfile,
6972 struct abbrev_table *abbrev_table,
6973 int use_existing_cu,
6974 bool skip_partial)
6975 : die_reader_specs {},
6976 m_this_cu (this_cu)
6977 {
6978 struct objfile *objfile = dwarf2_per_objfile->objfile;
6979 struct dwarf2_section_info *section = this_cu->section;
6980 bfd *abfd = section->get_bfd_owner ();
6981 struct dwarf2_cu *cu;
6982 const gdb_byte *begin_info_ptr;
6983 struct signatured_type *sig_type = NULL;
6984 struct dwarf2_section_info *abbrev_section;
6985 /* Non-zero if CU currently points to a DWO file and we need to
6986 reread it. When this happens we need to reread the skeleton die
6987 before we can reread the DWO file (this only applies to CUs, not TUs). */
6988 int rereading_dwo_cu = 0;
6989
6990 if (dwarf_die_debug)
6991 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6992 this_cu->is_debug_types ? "type" : "comp",
6993 sect_offset_str (this_cu->sect_off));
6994
6995 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6996 file (instead of going through the stub), short-circuit all of this. */
6997 if (this_cu->reading_dwo_directly)
6998 {
6999 /* Narrow down the scope of possibilities to have to understand. */
7000 gdb_assert (this_cu->is_debug_types);
7001 gdb_assert (abbrev_table == NULL);
7002 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, use_existing_cu);
7003 return;
7004 }
7005
7006 /* This is cheap if the section is already read in. */
7007 section->read (objfile);
7008
7009 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7010
7011 abbrev_section = get_abbrev_section_for_cu (this_cu);
7012
7013 if (use_existing_cu && this_cu->cu != NULL)
7014 {
7015 cu = this_cu->cu;
7016 /* If this CU is from a DWO file we need to start over, we need to
7017 refetch the attributes from the skeleton CU.
7018 This could be optimized by retrieving those attributes from when we
7019 were here the first time: the previous comp_unit_die was stored in
7020 comp_unit_obstack. But there's no data yet that we need this
7021 optimization. */
7022 if (cu->dwo_unit != NULL)
7023 rereading_dwo_cu = 1;
7024 }
7025 else
7026 {
7027 /* If !use_existing_cu, this_cu->cu must be NULL. */
7028 gdb_assert (this_cu->cu == NULL);
7029 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7030 cu = m_new_cu.get ();
7031 }
7032
7033 /* Get the header. */
7034 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7035 {
7036 /* We already have the header, there's no need to read it in again. */
7037 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7038 }
7039 else
7040 {
7041 if (this_cu->is_debug_types)
7042 {
7043 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7044 &cu->header, section,
7045 abbrev_section, info_ptr,
7046 rcuh_kind::TYPE);
7047
7048 /* Since per_cu is the first member of struct signatured_type,
7049 we can go from a pointer to one to a pointer to the other. */
7050 sig_type = (struct signatured_type *) this_cu;
7051 gdb_assert (sig_type->signature == cu->header.signature);
7052 gdb_assert (sig_type->type_offset_in_tu
7053 == cu->header.type_cu_offset_in_tu);
7054 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7055
7056 /* LENGTH has not been set yet for type units if we're
7057 using .gdb_index. */
7058 this_cu->length = cu->header.get_length ();
7059
7060 /* Establish the type offset that can be used to lookup the type. */
7061 sig_type->type_offset_in_section =
7062 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7063
7064 this_cu->dwarf_version = cu->header.version;
7065 }
7066 else
7067 {
7068 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7069 &cu->header, section,
7070 abbrev_section,
7071 info_ptr,
7072 rcuh_kind::COMPILE);
7073
7074 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7075 if (this_cu->length == 0)
7076 this_cu->length = cu->header.get_length ();
7077 else
7078 gdb_assert (this_cu->length == cu->header.get_length ());
7079 this_cu->dwarf_version = cu->header.version;
7080 }
7081 }
7082
7083 /* Skip dummy compilation units. */
7084 if (info_ptr >= begin_info_ptr + this_cu->length
7085 || peek_abbrev_code (abfd, info_ptr) == 0)
7086 {
7087 dummy_p = true;
7088 return;
7089 }
7090
7091 /* If we don't have them yet, read the abbrevs for this compilation unit.
7092 And if we need to read them now, make sure they're freed when we're
7093 done. */
7094 if (abbrev_table != NULL)
7095 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7096 else
7097 {
7098 m_abbrev_table_holder
7099 = abbrev_table::read (objfile, abbrev_section,
7100 cu->header.abbrev_sect_off);
7101 abbrev_table = m_abbrev_table_holder.get ();
7102 }
7103
7104 /* Read the top level CU/TU die. */
7105 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7106 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7107
7108 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7109 {
7110 dummy_p = true;
7111 return;
7112 }
7113
7114 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7115 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7116 table from the DWO file and pass the ownership over to us. It will be
7117 referenced from READER, so we must make sure to free it after we're done
7118 with READER.
7119
7120 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7121 DWO CU, that this test will fail (the attribute will not be present). */
7122 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7123 if (dwo_name != nullptr)
7124 {
7125 struct dwo_unit *dwo_unit;
7126 struct die_info *dwo_comp_unit_die;
7127
7128 if (comp_unit_die->has_children)
7129 {
7130 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7131 " has children (offset %s) [in module %s]"),
7132 sect_offset_str (this_cu->sect_off),
7133 bfd_get_filename (abfd));
7134 }
7135 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
7136 if (dwo_unit != NULL)
7137 {
7138 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
7139 comp_unit_die, NULL,
7140 this, &info_ptr,
7141 &dwo_comp_unit_die,
7142 &m_dwo_abbrev_table) == 0)
7143 {
7144 /* Dummy die. */
7145 dummy_p = true;
7146 return;
7147 }
7148 comp_unit_die = dwo_comp_unit_die;
7149 }
7150 else
7151 {
7152 /* Yikes, we couldn't find the rest of the DIE, we only have
7153 the stub. A complaint has already been logged. There's
7154 not much more we can do except pass on the stub DIE to
7155 die_reader_func. We don't want to throw an error on bad
7156 debug info. */
7157 }
7158 }
7159 }
7160
7161 void
7162 cutu_reader::keep ()
7163 {
7164 /* Done, clean up. */
7165 gdb_assert (!dummy_p);
7166 if (m_new_cu != NULL)
7167 {
7168 /* We know that m_this_cu->cu is set, since we are in the process of
7169 parsing the CU. */
7170 gdb_assert (m_this_cu->cu != nullptr);
7171 dwarf2_per_objfile *dwarf2_per_objfile = m_this_cu->cu->per_objfile;
7172
7173 /* Link this CU into read_in_chain. */
7174 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->per_bfd->read_in_chain;
7175 dwarf2_per_objfile->per_bfd->read_in_chain = m_this_cu;
7176 /* The chain owns it now. */
7177 m_new_cu.release ();
7178 }
7179 }
7180
7181 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7182 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7183 assumed to have already done the lookup to find the DWO file).
7184
7185 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7186 THIS_CU->is_debug_types, but nothing else.
7187
7188 We fill in THIS_CU->length.
7189
7190 THIS_CU->cu is always freed when done.
7191 This is done in order to not leave THIS_CU->cu in a state where we have
7192 to care whether it refers to the "main" CU or the DWO CU.
7193
7194 When parent_cu is passed, it is used to provide a default value for
7195 str_offsets_base and addr_base from the parent. */
7196
7197 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7198 dwarf2_per_objfile *dwarf2_per_objfile,
7199 struct dwarf2_cu *parent_cu,
7200 struct dwo_file *dwo_file)
7201 : die_reader_specs {},
7202 m_this_cu (this_cu)
7203 {
7204 struct objfile *objfile = dwarf2_per_objfile->objfile;
7205 struct dwarf2_section_info *section = this_cu->section;
7206 bfd *abfd = section->get_bfd_owner ();
7207 struct dwarf2_section_info *abbrev_section;
7208 const gdb_byte *begin_info_ptr, *info_ptr;
7209
7210 if (dwarf_die_debug)
7211 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7212 this_cu->is_debug_types ? "type" : "comp",
7213 sect_offset_str (this_cu->sect_off));
7214
7215 gdb_assert (this_cu->cu == NULL);
7216
7217 abbrev_section = (dwo_file != NULL
7218 ? &dwo_file->sections.abbrev
7219 : get_abbrev_section_for_cu (this_cu));
7220
7221 /* This is cheap if the section is already read in. */
7222 section->read (objfile);
7223
7224 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7225
7226 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7227 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7228 &m_new_cu->header, section,
7229 abbrev_section, info_ptr,
7230 (this_cu->is_debug_types
7231 ? rcuh_kind::TYPE
7232 : rcuh_kind::COMPILE));
7233
7234 if (parent_cu != nullptr)
7235 {
7236 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7237 m_new_cu->addr_base = parent_cu->addr_base;
7238 }
7239 this_cu->length = m_new_cu->header.get_length ();
7240
7241 /* Skip dummy compilation units. */
7242 if (info_ptr >= begin_info_ptr + this_cu->length
7243 || peek_abbrev_code (abfd, info_ptr) == 0)
7244 {
7245 dummy_p = true;
7246 return;
7247 }
7248
7249 m_abbrev_table_holder
7250 = abbrev_table::read (objfile, abbrev_section,
7251 m_new_cu->header.abbrev_sect_off);
7252
7253 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7254 m_abbrev_table_holder.get ());
7255 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7256 }
7257
7258 \f
7259 /* Type Unit Groups.
7260
7261 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7262 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7263 so that all types coming from the same compilation (.o file) are grouped
7264 together. A future step could be to put the types in the same symtab as
7265 the CU the types ultimately came from. */
7266
7267 static hashval_t
7268 hash_type_unit_group (const void *item)
7269 {
7270 const struct type_unit_group *tu_group
7271 = (const struct type_unit_group *) item;
7272
7273 return hash_stmt_list_entry (&tu_group->hash);
7274 }
7275
7276 static int
7277 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7278 {
7279 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7280 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7281
7282 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7283 }
7284
7285 /* Allocate a hash table for type unit groups. */
7286
7287 static htab_up
7288 allocate_type_unit_groups_table ()
7289 {
7290 return htab_up (htab_create_alloc (3,
7291 hash_type_unit_group,
7292 eq_type_unit_group,
7293 NULL, xcalloc, xfree));
7294 }
7295
7296 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7297 partial symtabs. We combine several TUs per psymtab to not let the size
7298 of any one psymtab grow too big. */
7299 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7300 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7301
7302 /* Helper routine for get_type_unit_group.
7303 Create the type_unit_group object used to hold one or more TUs. */
7304
7305 static struct type_unit_group *
7306 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7307 {
7308 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7309 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7310 struct dwarf2_per_cu_data *per_cu;
7311 struct type_unit_group *tu_group;
7312
7313 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7314 struct type_unit_group);
7315 per_cu = &tu_group->per_cu;
7316 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
7317 per_cu->per_bfd = per_bfd;
7318
7319 if (per_bfd->using_index)
7320 {
7321 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7322 struct dwarf2_per_cu_quick_data);
7323 }
7324 else
7325 {
7326 unsigned int line_offset = to_underlying (line_offset_struct);
7327 dwarf2_psymtab *pst;
7328 std::string name;
7329
7330 /* Give the symtab a useful name for debug purposes. */
7331 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7332 name = string_printf ("<type_units_%d>",
7333 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7334 else
7335 name = string_printf ("<type_units_at_0x%x>", line_offset);
7336
7337 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7338 pst->anonymous = true;
7339 }
7340
7341 tu_group->hash.dwo_unit = cu->dwo_unit;
7342 tu_group->hash.line_sect_off = line_offset_struct;
7343
7344 return tu_group;
7345 }
7346
7347 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7348 STMT_LIST is a DW_AT_stmt_list attribute. */
7349
7350 static struct type_unit_group *
7351 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7352 {
7353 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7354 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7355 struct type_unit_group *tu_group;
7356 void **slot;
7357 unsigned int line_offset;
7358 struct type_unit_group type_unit_group_for_lookup;
7359
7360 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7361 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7362
7363 /* Do we need to create a new group, or can we use an existing one? */
7364
7365 if (stmt_list)
7366 {
7367 line_offset = DW_UNSND (stmt_list);
7368 ++tu_stats->nr_symtab_sharers;
7369 }
7370 else
7371 {
7372 /* Ugh, no stmt_list. Rare, but we have to handle it.
7373 We can do various things here like create one group per TU or
7374 spread them over multiple groups to split up the expansion work.
7375 To avoid worst case scenarios (too many groups or too large groups)
7376 we, umm, group them in bunches. */
7377 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7378 | (tu_stats->nr_stmt_less_type_units
7379 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7380 ++tu_stats->nr_stmt_less_type_units;
7381 }
7382
7383 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7384 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7385 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7386 &type_unit_group_for_lookup, INSERT);
7387 if (*slot != NULL)
7388 {
7389 tu_group = (struct type_unit_group *) *slot;
7390 gdb_assert (tu_group != NULL);
7391 }
7392 else
7393 {
7394 sect_offset line_offset_struct = (sect_offset) line_offset;
7395 tu_group = create_type_unit_group (cu, line_offset_struct);
7396 *slot = tu_group;
7397 ++tu_stats->nr_symtabs;
7398 }
7399
7400 return tu_group;
7401 }
7402 \f
7403 /* Partial symbol tables. */
7404
7405 /* Create a psymtab named NAME and assign it to PER_CU.
7406
7407 The caller must fill in the following details:
7408 dirname, textlow, texthigh. */
7409
7410 static dwarf2_psymtab *
7411 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7412 dwarf2_per_objfile *per_objfile,
7413 const char *name)
7414 {
7415 struct objfile *objfile = per_objfile->objfile;
7416 dwarf2_psymtab *pst;
7417
7418 pst = new dwarf2_psymtab (name, objfile, per_cu);
7419
7420 pst->psymtabs_addrmap_supported = true;
7421
7422 /* This is the glue that links PST into GDB's symbol API. */
7423 per_cu->v.psymtab = pst;
7424
7425 return pst;
7426 }
7427
7428 /* DIE reader function for process_psymtab_comp_unit. */
7429
7430 static void
7431 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7432 const gdb_byte *info_ptr,
7433 struct die_info *comp_unit_die,
7434 enum language pretend_language)
7435 {
7436 struct dwarf2_cu *cu = reader->cu;
7437 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7438 struct objfile *objfile = per_objfile->objfile;
7439 struct gdbarch *gdbarch = objfile->arch ();
7440 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7441 CORE_ADDR baseaddr;
7442 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7443 dwarf2_psymtab *pst;
7444 enum pc_bounds_kind cu_bounds_kind;
7445 const char *filename;
7446
7447 gdb_assert (! per_cu->is_debug_types);
7448
7449 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7450
7451 /* Allocate a new partial symbol table structure. */
7452 gdb::unique_xmalloc_ptr<char> debug_filename;
7453 static const char artificial[] = "<artificial>";
7454 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7455 if (filename == NULL)
7456 filename = "";
7457 else if (strcmp (filename, artificial) == 0)
7458 {
7459 debug_filename.reset (concat (artificial, "@",
7460 sect_offset_str (per_cu->sect_off),
7461 (char *) NULL));
7462 filename = debug_filename.get ();
7463 }
7464
7465 pst = create_partial_symtab (per_cu, per_objfile, filename);
7466
7467 /* This must be done before calling dwarf2_build_include_psymtabs. */
7468 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7469
7470 baseaddr = objfile->text_section_offset ();
7471
7472 dwarf2_find_base_address (comp_unit_die, cu);
7473
7474 /* Possibly set the default values of LOWPC and HIGHPC from
7475 `DW_AT_ranges'. */
7476 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7477 &best_highpc, cu, pst);
7478 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7479 {
7480 CORE_ADDR low
7481 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7482 - baseaddr);
7483 CORE_ADDR high
7484 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7485 - baseaddr - 1);
7486 /* Store the contiguous range if it is not empty; it can be
7487 empty for CUs with no code. */
7488 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7489 low, high, pst);
7490 }
7491
7492 /* Check if comp unit has_children.
7493 If so, read the rest of the partial symbols from this comp unit.
7494 If not, there's no more debug_info for this comp unit. */
7495 if (comp_unit_die->has_children)
7496 {
7497 struct partial_die_info *first_die;
7498 CORE_ADDR lowpc, highpc;
7499
7500 lowpc = ((CORE_ADDR) -1);
7501 highpc = ((CORE_ADDR) 0);
7502
7503 first_die = load_partial_dies (reader, info_ptr, 1);
7504
7505 scan_partial_symbols (first_die, &lowpc, &highpc,
7506 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7507
7508 /* If we didn't find a lowpc, set it to highpc to avoid
7509 complaints from `maint check'. */
7510 if (lowpc == ((CORE_ADDR) -1))
7511 lowpc = highpc;
7512
7513 /* If the compilation unit didn't have an explicit address range,
7514 then use the information extracted from its child dies. */
7515 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7516 {
7517 best_lowpc = lowpc;
7518 best_highpc = highpc;
7519 }
7520 }
7521 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7522 best_lowpc + baseaddr)
7523 - baseaddr);
7524 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7525 best_highpc + baseaddr)
7526 - baseaddr);
7527
7528 end_psymtab_common (objfile, pst);
7529
7530 if (!cu->per_cu->imported_symtabs_empty ())
7531 {
7532 int i;
7533 int len = cu->per_cu->imported_symtabs_size ();
7534
7535 /* Fill in 'dependencies' here; we fill in 'users' in a
7536 post-pass. */
7537 pst->number_of_dependencies = len;
7538 pst->dependencies
7539 = objfile->partial_symtabs->allocate_dependencies (len);
7540 for (i = 0; i < len; ++i)
7541 {
7542 pst->dependencies[i]
7543 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7544 }
7545
7546 cu->per_cu->imported_symtabs_free ();
7547 }
7548
7549 /* Get the list of files included in the current compilation unit,
7550 and build a psymtab for each of them. */
7551 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7552
7553 if (dwarf_read_debug)
7554 fprintf_unfiltered (gdb_stdlog,
7555 "Psymtab for %s unit @%s: %s - %s"
7556 ", %d global, %d static syms\n",
7557 per_cu->is_debug_types ? "type" : "comp",
7558 sect_offset_str (per_cu->sect_off),
7559 paddress (gdbarch, pst->text_low (objfile)),
7560 paddress (gdbarch, pst->text_high (objfile)),
7561 pst->n_global_syms, pst->n_static_syms);
7562 }
7563
7564 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7565 Process compilation unit THIS_CU for a psymtab. */
7566
7567 static void
7568 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7569 dwarf2_per_objfile *per_objfile,
7570 bool want_partial_unit,
7571 enum language pretend_language)
7572 {
7573 /* If this compilation unit was already read in, free the
7574 cached copy in order to read it in again. This is
7575 necessary because we skipped some symbols when we first
7576 read in the compilation unit (see load_partial_dies).
7577 This problem could be avoided, but the benefit is unclear. */
7578 if (this_cu->cu != NULL)
7579 free_one_cached_comp_unit (this_cu);
7580
7581 cutu_reader reader (this_cu, per_objfile, NULL, 0, false);
7582
7583 switch (reader.comp_unit_die->tag)
7584 {
7585 case DW_TAG_compile_unit:
7586 this_cu->unit_type = DW_UT_compile;
7587 break;
7588 case DW_TAG_partial_unit:
7589 this_cu->unit_type = DW_UT_partial;
7590 break;
7591 default:
7592 abort ();
7593 }
7594
7595 if (reader.dummy_p)
7596 {
7597 /* Nothing. */
7598 }
7599 else if (this_cu->is_debug_types)
7600 build_type_psymtabs_reader (&reader, reader.info_ptr,
7601 reader.comp_unit_die);
7602 else if (want_partial_unit
7603 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7604 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7605 reader.comp_unit_die,
7606 pretend_language);
7607
7608 this_cu->lang = this_cu->cu->language;
7609
7610 /* Age out any secondary CUs. */
7611 age_cached_comp_units (this_cu->dwarf2_per_objfile);
7612 }
7613
7614 /* Reader function for build_type_psymtabs. */
7615
7616 static void
7617 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7618 const gdb_byte *info_ptr,
7619 struct die_info *type_unit_die)
7620 {
7621 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7622 struct objfile *objfile = dwarf2_per_objfile->objfile;
7623 struct dwarf2_cu *cu = reader->cu;
7624 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7625 struct signatured_type *sig_type;
7626 struct type_unit_group *tu_group;
7627 struct attribute *attr;
7628 struct partial_die_info *first_die;
7629 CORE_ADDR lowpc, highpc;
7630 dwarf2_psymtab *pst;
7631
7632 gdb_assert (per_cu->is_debug_types);
7633 sig_type = (struct signatured_type *) per_cu;
7634
7635 if (! type_unit_die->has_children)
7636 return;
7637
7638 attr = type_unit_die->attr (DW_AT_stmt_list);
7639 tu_group = get_type_unit_group (cu, attr);
7640
7641 if (tu_group->tus == nullptr)
7642 tu_group->tus = new std::vector<signatured_type *>;
7643 tu_group->tus->push_back (sig_type);
7644
7645 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7646 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7647 pst->anonymous = true;
7648
7649 first_die = load_partial_dies (reader, info_ptr, 1);
7650
7651 lowpc = (CORE_ADDR) -1;
7652 highpc = (CORE_ADDR) 0;
7653 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7654
7655 end_psymtab_common (objfile, pst);
7656 }
7657
7658 /* Struct used to sort TUs by their abbreviation table offset. */
7659
7660 struct tu_abbrev_offset
7661 {
7662 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7663 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7664 {}
7665
7666 signatured_type *sig_type;
7667 sect_offset abbrev_offset;
7668 };
7669
7670 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7671
7672 static bool
7673 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7674 const struct tu_abbrev_offset &b)
7675 {
7676 return a.abbrev_offset < b.abbrev_offset;
7677 }
7678
7679 /* Efficiently read all the type units.
7680 This does the bulk of the work for build_type_psymtabs.
7681
7682 The efficiency is because we sort TUs by the abbrev table they use and
7683 only read each abbrev table once. In one program there are 200K TUs
7684 sharing 8K abbrev tables.
7685
7686 The main purpose of this function is to support building the
7687 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7688 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7689 can collapse the search space by grouping them by stmt_list.
7690 The savings can be significant, in the same program from above the 200K TUs
7691 share 8K stmt_list tables.
7692
7693 FUNC is expected to call get_type_unit_group, which will create the
7694 struct type_unit_group if necessary and add it to
7695 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7696
7697 static void
7698 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7699 {
7700 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7701 abbrev_table_up abbrev_table;
7702 sect_offset abbrev_offset;
7703
7704 /* It's up to the caller to not call us multiple times. */
7705 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7706
7707 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7708 return;
7709
7710 /* TUs typically share abbrev tables, and there can be way more TUs than
7711 abbrev tables. Sort by abbrev table to reduce the number of times we
7712 read each abbrev table in.
7713 Alternatives are to punt or to maintain a cache of abbrev tables.
7714 This is simpler and efficient enough for now.
7715
7716 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7717 symtab to use). Typically TUs with the same abbrev offset have the same
7718 stmt_list value too so in practice this should work well.
7719
7720 The basic algorithm here is:
7721
7722 sort TUs by abbrev table
7723 for each TU with same abbrev table:
7724 read abbrev table if first user
7725 read TU top level DIE
7726 [IWBN if DWO skeletons had DW_AT_stmt_list]
7727 call FUNC */
7728
7729 if (dwarf_read_debug)
7730 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7731
7732 /* Sort in a separate table to maintain the order of all_type_units
7733 for .gdb_index: TU indices directly index all_type_units. */
7734 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7735 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7736
7737 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7738 sorted_by_abbrev.emplace_back
7739 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7740 sig_type->per_cu.section,
7741 sig_type->per_cu.sect_off));
7742
7743 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7744 sort_tu_by_abbrev_offset);
7745
7746 abbrev_offset = (sect_offset) ~(unsigned) 0;
7747
7748 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7749 {
7750 /* Switch to the next abbrev table if necessary. */
7751 if (abbrev_table == NULL
7752 || tu.abbrev_offset != abbrev_offset)
7753 {
7754 abbrev_offset = tu.abbrev_offset;
7755 abbrev_table =
7756 abbrev_table::read (dwarf2_per_objfile->objfile,
7757 &dwarf2_per_objfile->per_bfd->abbrev,
7758 abbrev_offset);
7759 ++tu_stats->nr_uniq_abbrev_tables;
7760 }
7761
7762 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7763 abbrev_table.get (), 0, false);
7764 if (!reader.dummy_p)
7765 build_type_psymtabs_reader (&reader, reader.info_ptr,
7766 reader.comp_unit_die);
7767 }
7768 }
7769
7770 /* Print collected type unit statistics. */
7771
7772 static void
7773 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7774 {
7775 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7776
7777 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7778 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7779 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7780 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7781 tu_stats->nr_uniq_abbrev_tables);
7782 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7783 tu_stats->nr_symtabs);
7784 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7785 tu_stats->nr_symtab_sharers);
7786 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7787 tu_stats->nr_stmt_less_type_units);
7788 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7789 tu_stats->nr_all_type_units_reallocs);
7790 }
7791
7792 /* Traversal function for build_type_psymtabs. */
7793
7794 static int
7795 build_type_psymtab_dependencies (void **slot, void *info)
7796 {
7797 struct dwarf2_per_objfile *dwarf2_per_objfile
7798 = (struct dwarf2_per_objfile *) info;
7799 struct objfile *objfile = dwarf2_per_objfile->objfile;
7800 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7801 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7802 dwarf2_psymtab *pst = per_cu->v.psymtab;
7803 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7804 int i;
7805
7806 gdb_assert (len > 0);
7807 gdb_assert (per_cu->type_unit_group_p ());
7808
7809 pst->number_of_dependencies = len;
7810 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7811 for (i = 0; i < len; ++i)
7812 {
7813 struct signatured_type *iter = tu_group->tus->at (i);
7814 gdb_assert (iter->per_cu.is_debug_types);
7815 pst->dependencies[i] = iter->per_cu.v.psymtab;
7816 iter->type_unit_group = tu_group;
7817 }
7818
7819 delete tu_group->tus;
7820 tu_group->tus = nullptr;
7821
7822 return 1;
7823 }
7824
7825 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7826 Build partial symbol tables for the .debug_types comp-units. */
7827
7828 static void
7829 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7830 {
7831 if (! create_all_type_units (dwarf2_per_objfile))
7832 return;
7833
7834 build_type_psymtabs_1 (dwarf2_per_objfile);
7835 }
7836
7837 /* Traversal function for process_skeletonless_type_unit.
7838 Read a TU in a DWO file and build partial symbols for it. */
7839
7840 static int
7841 process_skeletonless_type_unit (void **slot, void *info)
7842 {
7843 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7844 struct dwarf2_per_objfile *dwarf2_per_objfile
7845 = (struct dwarf2_per_objfile *) info;
7846 struct signatured_type find_entry, *entry;
7847
7848 /* If this TU doesn't exist in the global table, add it and read it in. */
7849
7850 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7851 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7852
7853 find_entry.signature = dwo_unit->signature;
7854 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7855 &find_entry, INSERT);
7856 /* If we've already seen this type there's nothing to do. What's happening
7857 is we're doing our own version of comdat-folding here. */
7858 if (*slot != NULL)
7859 return 1;
7860
7861 /* This does the job that create_all_type_units would have done for
7862 this TU. */
7863 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7864 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7865 *slot = entry;
7866
7867 /* This does the job that build_type_psymtabs_1 would have done. */
7868 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, NULL, 0, false);
7869 if (!reader.dummy_p)
7870 build_type_psymtabs_reader (&reader, reader.info_ptr,
7871 reader.comp_unit_die);
7872
7873 return 1;
7874 }
7875
7876 /* Traversal function for process_skeletonless_type_units. */
7877
7878 static int
7879 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7880 {
7881 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7882
7883 if (dwo_file->tus != NULL)
7884 htab_traverse_noresize (dwo_file->tus.get (),
7885 process_skeletonless_type_unit, info);
7886
7887 return 1;
7888 }
7889
7890 /* Scan all TUs of DWO files, verifying we've processed them.
7891 This is needed in case a TU was emitted without its skeleton.
7892 Note: This can't be done until we know what all the DWO files are. */
7893
7894 static void
7895 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7896 {
7897 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7898 if (get_dwp_file (dwarf2_per_objfile) == NULL
7899 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7900 {
7901 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7902 process_dwo_file_for_skeletonless_type_units,
7903 dwarf2_per_objfile);
7904 }
7905 }
7906
7907 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7908
7909 static void
7910 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7911 {
7912 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7913 {
7914 dwarf2_psymtab *pst = per_cu->v.psymtab;
7915
7916 if (pst == NULL)
7917 continue;
7918
7919 for (int j = 0; j < pst->number_of_dependencies; ++j)
7920 {
7921 /* Set the 'user' field only if it is not already set. */
7922 if (pst->dependencies[j]->user == NULL)
7923 pst->dependencies[j]->user = pst;
7924 }
7925 }
7926 }
7927
7928 /* Build the partial symbol table by doing a quick pass through the
7929 .debug_info and .debug_abbrev sections. */
7930
7931 static void
7932 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7933 {
7934 struct objfile *objfile = dwarf2_per_objfile->objfile;
7935
7936 if (dwarf_read_debug)
7937 {
7938 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7939 objfile_name (objfile));
7940 }
7941
7942 scoped_restore restore_reading_psyms
7943 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7944 true);
7945
7946 dwarf2_per_objfile->per_bfd->info.read (objfile);
7947
7948 /* Any cached compilation units will be linked by the per-objfile
7949 read_in_chain. Make sure to free them when we're done. */
7950 free_cached_comp_units freer (dwarf2_per_objfile);
7951
7952 build_type_psymtabs (dwarf2_per_objfile);
7953
7954 create_all_comp_units (dwarf2_per_objfile);
7955
7956 /* Create a temporary address map on a temporary obstack. We later
7957 copy this to the final obstack. */
7958 auto_obstack temp_obstack;
7959
7960 scoped_restore save_psymtabs_addrmap
7961 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7962 addrmap_create_mutable (&temp_obstack));
7963
7964 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7965 {
7966 if (per_cu->v.psymtab != NULL)
7967 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7968 continue;
7969 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7970 language_minimal);
7971 }
7972
7973 /* This has to wait until we read the CUs, we need the list of DWOs. */
7974 process_skeletonless_type_units (dwarf2_per_objfile);
7975
7976 /* Now that all TUs have been processed we can fill in the dependencies. */
7977 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7978 {
7979 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7980 build_type_psymtab_dependencies, dwarf2_per_objfile);
7981 }
7982
7983 if (dwarf_read_debug)
7984 print_tu_stats (dwarf2_per_objfile);
7985
7986 set_partial_user (dwarf2_per_objfile);
7987
7988 objfile->partial_symtabs->psymtabs_addrmap
7989 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7990 objfile->partial_symtabs->obstack ());
7991 /* At this point we want to keep the address map. */
7992 save_psymtabs_addrmap.release ();
7993
7994 if (dwarf_read_debug)
7995 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7996 objfile_name (objfile));
7997 }
7998
7999 /* Load the partial DIEs for a secondary CU into memory.
8000 This is also used when rereading a primary CU with load_all_dies. */
8001
8002 static void
8003 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8004 dwarf2_per_objfile *per_objfile)
8005 {
8006 cutu_reader reader (this_cu, per_objfile, NULL, 1, false);
8007
8008 if (!reader.dummy_p)
8009 {
8010 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8011 language_minimal);
8012
8013 /* Check if comp unit has_children.
8014 If so, read the rest of the partial symbols from this comp unit.
8015 If not, there's no more debug_info for this comp unit. */
8016 if (reader.comp_unit_die->has_children)
8017 load_partial_dies (&reader, reader.info_ptr, 0);
8018
8019 reader.keep ();
8020 }
8021 }
8022
8023 static void
8024 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8025 struct dwarf2_section_info *section,
8026 struct dwarf2_section_info *abbrev_section,
8027 unsigned int is_dwz)
8028 {
8029 const gdb_byte *info_ptr;
8030 struct objfile *objfile = dwarf2_per_objfile->objfile;
8031
8032 if (dwarf_read_debug)
8033 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8034 section->get_name (),
8035 section->get_file_name ());
8036
8037 section->read (objfile);
8038
8039 info_ptr = section->buffer;
8040
8041 while (info_ptr < section->buffer + section->size)
8042 {
8043 struct dwarf2_per_cu_data *this_cu;
8044
8045 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8046
8047 comp_unit_head cu_header;
8048 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8049 abbrev_section, info_ptr,
8050 rcuh_kind::COMPILE);
8051
8052 /* Save the compilation unit for later lookup. */
8053 if (cu_header.unit_type != DW_UT_type)
8054 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8055 else
8056 {
8057 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8058 sig_type->signature = cu_header.signature;
8059 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8060 this_cu = &sig_type->per_cu;
8061 }
8062 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8063 this_cu->sect_off = sect_off;
8064 this_cu->length = cu_header.length + cu_header.initial_length_size;
8065 this_cu->is_dwz = is_dwz;
8066 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8067 this_cu->section = section;
8068
8069 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8070
8071 info_ptr = info_ptr + this_cu->length;
8072 }
8073 }
8074
8075 /* Create a list of all compilation units in OBJFILE.
8076 This is only done for -readnow and building partial symtabs. */
8077
8078 static void
8079 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8080 {
8081 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8082 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8083 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8084
8085 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8086 if (dwz != NULL)
8087 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8088 1);
8089 }
8090
8091 /* Process all loaded DIEs for compilation unit CU, starting at
8092 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8093 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8094 DW_AT_ranges). See the comments of add_partial_subprogram on how
8095 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8096
8097 static void
8098 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8099 CORE_ADDR *highpc, int set_addrmap,
8100 struct dwarf2_cu *cu)
8101 {
8102 struct partial_die_info *pdi;
8103
8104 /* Now, march along the PDI's, descending into ones which have
8105 interesting children but skipping the children of the other ones,
8106 until we reach the end of the compilation unit. */
8107
8108 pdi = first_die;
8109
8110 while (pdi != NULL)
8111 {
8112 pdi->fixup (cu);
8113
8114 /* Anonymous namespaces or modules have no name but have interesting
8115 children, so we need to look at them. Ditto for anonymous
8116 enums. */
8117
8118 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8119 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8120 || pdi->tag == DW_TAG_imported_unit
8121 || pdi->tag == DW_TAG_inlined_subroutine)
8122 {
8123 switch (pdi->tag)
8124 {
8125 case DW_TAG_subprogram:
8126 case DW_TAG_inlined_subroutine:
8127 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8128 break;
8129 case DW_TAG_constant:
8130 case DW_TAG_variable:
8131 case DW_TAG_typedef:
8132 case DW_TAG_union_type:
8133 if (!pdi->is_declaration
8134 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8135 {
8136 add_partial_symbol (pdi, cu);
8137 }
8138 break;
8139 case DW_TAG_class_type:
8140 case DW_TAG_interface_type:
8141 case DW_TAG_structure_type:
8142 if (!pdi->is_declaration)
8143 {
8144 add_partial_symbol (pdi, cu);
8145 }
8146 if ((cu->language == language_rust
8147 || cu->language == language_cplus) && pdi->has_children)
8148 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8149 set_addrmap, cu);
8150 break;
8151 case DW_TAG_enumeration_type:
8152 if (!pdi->is_declaration)
8153 add_partial_enumeration (pdi, cu);
8154 break;
8155 case DW_TAG_base_type:
8156 case DW_TAG_subrange_type:
8157 /* File scope base type definitions are added to the partial
8158 symbol table. */
8159 add_partial_symbol (pdi, cu);
8160 break;
8161 case DW_TAG_namespace:
8162 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8163 break;
8164 case DW_TAG_module:
8165 if (!pdi->is_declaration)
8166 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8167 break;
8168 case DW_TAG_imported_unit:
8169 {
8170 struct dwarf2_per_cu_data *per_cu;
8171
8172 /* For now we don't handle imported units in type units. */
8173 if (cu->per_cu->is_debug_types)
8174 {
8175 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8176 " supported in type units [in module %s]"),
8177 objfile_name (cu->per_objfile->objfile));
8178 }
8179
8180 per_cu = dwarf2_find_containing_comp_unit
8181 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8182
8183 /* Go read the partial unit, if needed. */
8184 if (per_cu->v.psymtab == NULL)
8185 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8186 cu->language);
8187
8188 cu->per_cu->imported_symtabs_push (per_cu);
8189 }
8190 break;
8191 case DW_TAG_imported_declaration:
8192 add_partial_symbol (pdi, cu);
8193 break;
8194 default:
8195 break;
8196 }
8197 }
8198
8199 /* If the die has a sibling, skip to the sibling. */
8200
8201 pdi = pdi->die_sibling;
8202 }
8203 }
8204
8205 /* Functions used to compute the fully scoped name of a partial DIE.
8206
8207 Normally, this is simple. For C++, the parent DIE's fully scoped
8208 name is concatenated with "::" and the partial DIE's name.
8209 Enumerators are an exception; they use the scope of their parent
8210 enumeration type, i.e. the name of the enumeration type is not
8211 prepended to the enumerator.
8212
8213 There are two complexities. One is DW_AT_specification; in this
8214 case "parent" means the parent of the target of the specification,
8215 instead of the direct parent of the DIE. The other is compilers
8216 which do not emit DW_TAG_namespace; in this case we try to guess
8217 the fully qualified name of structure types from their members'
8218 linkage names. This must be done using the DIE's children rather
8219 than the children of any DW_AT_specification target. We only need
8220 to do this for structures at the top level, i.e. if the target of
8221 any DW_AT_specification (if any; otherwise the DIE itself) does not
8222 have a parent. */
8223
8224 /* Compute the scope prefix associated with PDI's parent, in
8225 compilation unit CU. The result will be allocated on CU's
8226 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8227 field. NULL is returned if no prefix is necessary. */
8228 static const char *
8229 partial_die_parent_scope (struct partial_die_info *pdi,
8230 struct dwarf2_cu *cu)
8231 {
8232 const char *grandparent_scope;
8233 struct partial_die_info *parent, *real_pdi;
8234
8235 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8236 then this means the parent of the specification DIE. */
8237
8238 real_pdi = pdi;
8239 while (real_pdi->has_specification)
8240 {
8241 auto res = find_partial_die (real_pdi->spec_offset,
8242 real_pdi->spec_is_dwz, cu);
8243 real_pdi = res.pdi;
8244 cu = res.cu;
8245 }
8246
8247 parent = real_pdi->die_parent;
8248 if (parent == NULL)
8249 return NULL;
8250
8251 if (parent->scope_set)
8252 return parent->scope;
8253
8254 parent->fixup (cu);
8255
8256 grandparent_scope = partial_die_parent_scope (parent, cu);
8257
8258 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8259 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8260 Work around this problem here. */
8261 if (cu->language == language_cplus
8262 && parent->tag == DW_TAG_namespace
8263 && strcmp (parent->name, "::") == 0
8264 && grandparent_scope == NULL)
8265 {
8266 parent->scope = NULL;
8267 parent->scope_set = 1;
8268 return NULL;
8269 }
8270
8271 /* Nested subroutines in Fortran get a prefix. */
8272 if (pdi->tag == DW_TAG_enumerator)
8273 /* Enumerators should not get the name of the enumeration as a prefix. */
8274 parent->scope = grandparent_scope;
8275 else if (parent->tag == DW_TAG_namespace
8276 || parent->tag == DW_TAG_module
8277 || parent->tag == DW_TAG_structure_type
8278 || parent->tag == DW_TAG_class_type
8279 || parent->tag == DW_TAG_interface_type
8280 || parent->tag == DW_TAG_union_type
8281 || parent->tag == DW_TAG_enumeration_type
8282 || (cu->language == language_fortran
8283 && parent->tag == DW_TAG_subprogram
8284 && pdi->tag == DW_TAG_subprogram))
8285 {
8286 if (grandparent_scope == NULL)
8287 parent->scope = parent->name;
8288 else
8289 parent->scope = typename_concat (&cu->comp_unit_obstack,
8290 grandparent_scope,
8291 parent->name, 0, cu);
8292 }
8293 else
8294 {
8295 /* FIXME drow/2004-04-01: What should we be doing with
8296 function-local names? For partial symbols, we should probably be
8297 ignoring them. */
8298 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8299 dwarf_tag_name (parent->tag),
8300 sect_offset_str (pdi->sect_off));
8301 parent->scope = grandparent_scope;
8302 }
8303
8304 parent->scope_set = 1;
8305 return parent->scope;
8306 }
8307
8308 /* Return the fully scoped name associated with PDI, from compilation unit
8309 CU. The result will be allocated with malloc. */
8310
8311 static gdb::unique_xmalloc_ptr<char>
8312 partial_die_full_name (struct partial_die_info *pdi,
8313 struct dwarf2_cu *cu)
8314 {
8315 const char *parent_scope;
8316
8317 /* If this is a template instantiation, we can not work out the
8318 template arguments from partial DIEs. So, unfortunately, we have
8319 to go through the full DIEs. At least any work we do building
8320 types here will be reused if full symbols are loaded later. */
8321 if (pdi->has_template_arguments)
8322 {
8323 pdi->fixup (cu);
8324
8325 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8326 {
8327 struct die_info *die;
8328 struct attribute attr;
8329 struct dwarf2_cu *ref_cu = cu;
8330
8331 /* DW_FORM_ref_addr is using section offset. */
8332 attr.name = (enum dwarf_attribute) 0;
8333 attr.form = DW_FORM_ref_addr;
8334 attr.u.unsnd = to_underlying (pdi->sect_off);
8335 die = follow_die_ref (NULL, &attr, &ref_cu);
8336
8337 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8338 }
8339 }
8340
8341 parent_scope = partial_die_parent_scope (pdi, cu);
8342 if (parent_scope == NULL)
8343 return NULL;
8344 else
8345 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8346 pdi->name, 0, cu));
8347 }
8348
8349 static void
8350 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8351 {
8352 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8353 struct objfile *objfile = dwarf2_per_objfile->objfile;
8354 struct gdbarch *gdbarch = objfile->arch ();
8355 CORE_ADDR addr = 0;
8356 const char *actual_name = NULL;
8357 CORE_ADDR baseaddr;
8358
8359 baseaddr = objfile->text_section_offset ();
8360
8361 gdb::unique_xmalloc_ptr<char> built_actual_name
8362 = partial_die_full_name (pdi, cu);
8363 if (built_actual_name != NULL)
8364 actual_name = built_actual_name.get ();
8365
8366 if (actual_name == NULL)
8367 actual_name = pdi->name;
8368
8369 partial_symbol psymbol;
8370 memset (&psymbol, 0, sizeof (psymbol));
8371 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8372 psymbol.ginfo.section = -1;
8373
8374 /* The code below indicates that the psymbol should be installed by
8375 setting this. */
8376 gdb::optional<psymbol_placement> where;
8377
8378 switch (pdi->tag)
8379 {
8380 case DW_TAG_inlined_subroutine:
8381 case DW_TAG_subprogram:
8382 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8383 - baseaddr);
8384 if (pdi->is_external
8385 || cu->language == language_ada
8386 || (cu->language == language_fortran
8387 && pdi->die_parent != NULL
8388 && pdi->die_parent->tag == DW_TAG_subprogram))
8389 {
8390 /* Normally, only "external" DIEs are part of the global scope.
8391 But in Ada and Fortran, we want to be able to access nested
8392 procedures globally. So all Ada and Fortran subprograms are
8393 stored in the global scope. */
8394 where = psymbol_placement::GLOBAL;
8395 }
8396 else
8397 where = psymbol_placement::STATIC;
8398
8399 psymbol.domain = VAR_DOMAIN;
8400 psymbol.aclass = LOC_BLOCK;
8401 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8402 psymbol.ginfo.value.address = addr;
8403
8404 if (pdi->main_subprogram && actual_name != NULL)
8405 set_objfile_main_name (objfile, actual_name, cu->language);
8406 break;
8407 case DW_TAG_constant:
8408 psymbol.domain = VAR_DOMAIN;
8409 psymbol.aclass = LOC_STATIC;
8410 where = (pdi->is_external
8411 ? psymbol_placement::GLOBAL
8412 : psymbol_placement::STATIC);
8413 break;
8414 case DW_TAG_variable:
8415 if (pdi->d.locdesc)
8416 addr = decode_locdesc (pdi->d.locdesc, cu);
8417
8418 if (pdi->d.locdesc
8419 && addr == 0
8420 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8421 {
8422 /* A global or static variable may also have been stripped
8423 out by the linker if unused, in which case its address
8424 will be nullified; do not add such variables into partial
8425 symbol table then. */
8426 }
8427 else if (pdi->is_external)
8428 {
8429 /* Global Variable.
8430 Don't enter into the minimal symbol tables as there is
8431 a minimal symbol table entry from the ELF symbols already.
8432 Enter into partial symbol table if it has a location
8433 descriptor or a type.
8434 If the location descriptor is missing, new_symbol will create
8435 a LOC_UNRESOLVED symbol, the address of the variable will then
8436 be determined from the minimal symbol table whenever the variable
8437 is referenced.
8438 The address for the partial symbol table entry is not
8439 used by GDB, but it comes in handy for debugging partial symbol
8440 table building. */
8441
8442 if (pdi->d.locdesc || pdi->has_type)
8443 {
8444 psymbol.domain = VAR_DOMAIN;
8445 psymbol.aclass = LOC_STATIC;
8446 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8447 psymbol.ginfo.value.address = addr;
8448 where = psymbol_placement::GLOBAL;
8449 }
8450 }
8451 else
8452 {
8453 int has_loc = pdi->d.locdesc != NULL;
8454
8455 /* Static Variable. Skip symbols whose value we cannot know (those
8456 without location descriptors or constant values). */
8457 if (!has_loc && !pdi->has_const_value)
8458 return;
8459
8460 psymbol.domain = VAR_DOMAIN;
8461 psymbol.aclass = LOC_STATIC;
8462 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8463 if (has_loc)
8464 psymbol.ginfo.value.address = addr;
8465 where = psymbol_placement::STATIC;
8466 }
8467 break;
8468 case DW_TAG_typedef:
8469 case DW_TAG_base_type:
8470 case DW_TAG_subrange_type:
8471 psymbol.domain = VAR_DOMAIN;
8472 psymbol.aclass = LOC_TYPEDEF;
8473 where = psymbol_placement::STATIC;
8474 break;
8475 case DW_TAG_imported_declaration:
8476 case DW_TAG_namespace:
8477 psymbol.domain = VAR_DOMAIN;
8478 psymbol.aclass = LOC_TYPEDEF;
8479 where = psymbol_placement::GLOBAL;
8480 break;
8481 case DW_TAG_module:
8482 /* With Fortran 77 there might be a "BLOCK DATA" module
8483 available without any name. If so, we skip the module as it
8484 doesn't bring any value. */
8485 if (actual_name != nullptr)
8486 {
8487 psymbol.domain = MODULE_DOMAIN;
8488 psymbol.aclass = LOC_TYPEDEF;
8489 where = psymbol_placement::GLOBAL;
8490 }
8491 break;
8492 case DW_TAG_class_type:
8493 case DW_TAG_interface_type:
8494 case DW_TAG_structure_type:
8495 case DW_TAG_union_type:
8496 case DW_TAG_enumeration_type:
8497 /* Skip external references. The DWARF standard says in the section
8498 about "Structure, Union, and Class Type Entries": "An incomplete
8499 structure, union or class type is represented by a structure,
8500 union or class entry that does not have a byte size attribute
8501 and that has a DW_AT_declaration attribute." */
8502 if (!pdi->has_byte_size && pdi->is_declaration)
8503 return;
8504
8505 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8506 static vs. global. */
8507 psymbol.domain = STRUCT_DOMAIN;
8508 psymbol.aclass = LOC_TYPEDEF;
8509 where = (cu->language == language_cplus
8510 ? psymbol_placement::GLOBAL
8511 : psymbol_placement::STATIC);
8512 break;
8513 case DW_TAG_enumerator:
8514 psymbol.domain = VAR_DOMAIN;
8515 psymbol.aclass = LOC_CONST;
8516 where = (cu->language == language_cplus
8517 ? psymbol_placement::GLOBAL
8518 : psymbol_placement::STATIC);
8519 break;
8520 default:
8521 break;
8522 }
8523
8524 if (where.has_value ())
8525 {
8526 if (built_actual_name != nullptr)
8527 actual_name = objfile->intern (actual_name);
8528 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8529 psymbol.ginfo.set_linkage_name (actual_name);
8530 else
8531 {
8532 psymbol.ginfo.set_demangled_name (actual_name,
8533 &objfile->objfile_obstack);
8534 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8535 }
8536 add_psymbol_to_list (psymbol, *where, objfile);
8537 }
8538 }
8539
8540 /* Read a partial die corresponding to a namespace; also, add a symbol
8541 corresponding to that namespace to the symbol table. NAMESPACE is
8542 the name of the enclosing namespace. */
8543
8544 static void
8545 add_partial_namespace (struct partial_die_info *pdi,
8546 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8547 int set_addrmap, struct dwarf2_cu *cu)
8548 {
8549 /* Add a symbol for the namespace. */
8550
8551 add_partial_symbol (pdi, cu);
8552
8553 /* Now scan partial symbols in that namespace. */
8554
8555 if (pdi->has_children)
8556 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8557 }
8558
8559 /* Read a partial die corresponding to a Fortran module. */
8560
8561 static void
8562 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8563 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8564 {
8565 /* Add a symbol for the namespace. */
8566
8567 add_partial_symbol (pdi, cu);
8568
8569 /* Now scan partial symbols in that module. */
8570
8571 if (pdi->has_children)
8572 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8573 }
8574
8575 /* Read a partial die corresponding to a subprogram or an inlined
8576 subprogram and create a partial symbol for that subprogram.
8577 When the CU language allows it, this routine also defines a partial
8578 symbol for each nested subprogram that this subprogram contains.
8579 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8580 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8581
8582 PDI may also be a lexical block, in which case we simply search
8583 recursively for subprograms defined inside that lexical block.
8584 Again, this is only performed when the CU language allows this
8585 type of definitions. */
8586
8587 static void
8588 add_partial_subprogram (struct partial_die_info *pdi,
8589 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8590 int set_addrmap, struct dwarf2_cu *cu)
8591 {
8592 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8593 {
8594 if (pdi->has_pc_info)
8595 {
8596 if (pdi->lowpc < *lowpc)
8597 *lowpc = pdi->lowpc;
8598 if (pdi->highpc > *highpc)
8599 *highpc = pdi->highpc;
8600 if (set_addrmap)
8601 {
8602 struct objfile *objfile = cu->per_objfile->objfile;
8603 struct gdbarch *gdbarch = objfile->arch ();
8604 CORE_ADDR baseaddr;
8605 CORE_ADDR this_highpc;
8606 CORE_ADDR this_lowpc;
8607
8608 baseaddr = objfile->text_section_offset ();
8609 this_lowpc
8610 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8611 pdi->lowpc + baseaddr)
8612 - baseaddr);
8613 this_highpc
8614 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8615 pdi->highpc + baseaddr)
8616 - baseaddr);
8617 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8618 this_lowpc, this_highpc - 1,
8619 cu->per_cu->v.psymtab);
8620 }
8621 }
8622
8623 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8624 {
8625 if (!pdi->is_declaration)
8626 /* Ignore subprogram DIEs that do not have a name, they are
8627 illegal. Do not emit a complaint at this point, we will
8628 do so when we convert this psymtab into a symtab. */
8629 if (pdi->name)
8630 add_partial_symbol (pdi, cu);
8631 }
8632 }
8633
8634 if (! pdi->has_children)
8635 return;
8636
8637 if (cu->language == language_ada || cu->language == language_fortran)
8638 {
8639 pdi = pdi->die_child;
8640 while (pdi != NULL)
8641 {
8642 pdi->fixup (cu);
8643 if (pdi->tag == DW_TAG_subprogram
8644 || pdi->tag == DW_TAG_inlined_subroutine
8645 || pdi->tag == DW_TAG_lexical_block)
8646 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8647 pdi = pdi->die_sibling;
8648 }
8649 }
8650 }
8651
8652 /* Read a partial die corresponding to an enumeration type. */
8653
8654 static void
8655 add_partial_enumeration (struct partial_die_info *enum_pdi,
8656 struct dwarf2_cu *cu)
8657 {
8658 struct partial_die_info *pdi;
8659
8660 if (enum_pdi->name != NULL)
8661 add_partial_symbol (enum_pdi, cu);
8662
8663 pdi = enum_pdi->die_child;
8664 while (pdi)
8665 {
8666 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8667 complaint (_("malformed enumerator DIE ignored"));
8668 else
8669 add_partial_symbol (pdi, cu);
8670 pdi = pdi->die_sibling;
8671 }
8672 }
8673
8674 /* Return the initial uleb128 in the die at INFO_PTR. */
8675
8676 static unsigned int
8677 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8678 {
8679 unsigned int bytes_read;
8680
8681 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8682 }
8683
8684 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8685 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8686
8687 Return the corresponding abbrev, or NULL if the number is zero (indicating
8688 an empty DIE). In either case *BYTES_READ will be set to the length of
8689 the initial number. */
8690
8691 static struct abbrev_info *
8692 peek_die_abbrev (const die_reader_specs &reader,
8693 const gdb_byte *info_ptr, unsigned int *bytes_read)
8694 {
8695 dwarf2_cu *cu = reader.cu;
8696 bfd *abfd = cu->per_objfile->objfile->obfd;
8697 unsigned int abbrev_number
8698 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8699
8700 if (abbrev_number == 0)
8701 return NULL;
8702
8703 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8704 if (!abbrev)
8705 {
8706 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8707 " at offset %s [in module %s]"),
8708 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8709 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8710 }
8711
8712 return abbrev;
8713 }
8714
8715 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8716 Returns a pointer to the end of a series of DIEs, terminated by an empty
8717 DIE. Any children of the skipped DIEs will also be skipped. */
8718
8719 static const gdb_byte *
8720 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8721 {
8722 while (1)
8723 {
8724 unsigned int bytes_read;
8725 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8726
8727 if (abbrev == NULL)
8728 return info_ptr + bytes_read;
8729 else
8730 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8731 }
8732 }
8733
8734 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8735 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8736 abbrev corresponding to that skipped uleb128 should be passed in
8737 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8738 children. */
8739
8740 static const gdb_byte *
8741 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8742 struct abbrev_info *abbrev)
8743 {
8744 unsigned int bytes_read;
8745 struct attribute attr;
8746 bfd *abfd = reader->abfd;
8747 struct dwarf2_cu *cu = reader->cu;
8748 const gdb_byte *buffer = reader->buffer;
8749 const gdb_byte *buffer_end = reader->buffer_end;
8750 unsigned int form, i;
8751
8752 for (i = 0; i < abbrev->num_attrs; i++)
8753 {
8754 /* The only abbrev we care about is DW_AT_sibling. */
8755 if (abbrev->attrs[i].name == DW_AT_sibling)
8756 {
8757 bool ignored;
8758 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8759 &ignored);
8760 if (attr.form == DW_FORM_ref_addr)
8761 complaint (_("ignoring absolute DW_AT_sibling"));
8762 else
8763 {
8764 sect_offset off = attr.get_ref_die_offset ();
8765 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8766
8767 if (sibling_ptr < info_ptr)
8768 complaint (_("DW_AT_sibling points backwards"));
8769 else if (sibling_ptr > reader->buffer_end)
8770 reader->die_section->overflow_complaint ();
8771 else
8772 return sibling_ptr;
8773 }
8774 }
8775
8776 /* If it isn't DW_AT_sibling, skip this attribute. */
8777 form = abbrev->attrs[i].form;
8778 skip_attribute:
8779 switch (form)
8780 {
8781 case DW_FORM_ref_addr:
8782 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8783 and later it is offset sized. */
8784 if (cu->header.version == 2)
8785 info_ptr += cu->header.addr_size;
8786 else
8787 info_ptr += cu->header.offset_size;
8788 break;
8789 case DW_FORM_GNU_ref_alt:
8790 info_ptr += cu->header.offset_size;
8791 break;
8792 case DW_FORM_addr:
8793 info_ptr += cu->header.addr_size;
8794 break;
8795 case DW_FORM_data1:
8796 case DW_FORM_ref1:
8797 case DW_FORM_flag:
8798 case DW_FORM_strx1:
8799 info_ptr += 1;
8800 break;
8801 case DW_FORM_flag_present:
8802 case DW_FORM_implicit_const:
8803 break;
8804 case DW_FORM_data2:
8805 case DW_FORM_ref2:
8806 case DW_FORM_strx2:
8807 info_ptr += 2;
8808 break;
8809 case DW_FORM_strx3:
8810 info_ptr += 3;
8811 break;
8812 case DW_FORM_data4:
8813 case DW_FORM_ref4:
8814 case DW_FORM_strx4:
8815 info_ptr += 4;
8816 break;
8817 case DW_FORM_data8:
8818 case DW_FORM_ref8:
8819 case DW_FORM_ref_sig8:
8820 info_ptr += 8;
8821 break;
8822 case DW_FORM_data16:
8823 info_ptr += 16;
8824 break;
8825 case DW_FORM_string:
8826 read_direct_string (abfd, info_ptr, &bytes_read);
8827 info_ptr += bytes_read;
8828 break;
8829 case DW_FORM_sec_offset:
8830 case DW_FORM_strp:
8831 case DW_FORM_GNU_strp_alt:
8832 info_ptr += cu->header.offset_size;
8833 break;
8834 case DW_FORM_exprloc:
8835 case DW_FORM_block:
8836 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8837 info_ptr += bytes_read;
8838 break;
8839 case DW_FORM_block1:
8840 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8841 break;
8842 case DW_FORM_block2:
8843 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8844 break;
8845 case DW_FORM_block4:
8846 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8847 break;
8848 case DW_FORM_addrx:
8849 case DW_FORM_strx:
8850 case DW_FORM_sdata:
8851 case DW_FORM_udata:
8852 case DW_FORM_ref_udata:
8853 case DW_FORM_GNU_addr_index:
8854 case DW_FORM_GNU_str_index:
8855 case DW_FORM_rnglistx:
8856 case DW_FORM_loclistx:
8857 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8858 break;
8859 case DW_FORM_indirect:
8860 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8861 info_ptr += bytes_read;
8862 /* We need to continue parsing from here, so just go back to
8863 the top. */
8864 goto skip_attribute;
8865
8866 default:
8867 error (_("Dwarf Error: Cannot handle %s "
8868 "in DWARF reader [in module %s]"),
8869 dwarf_form_name (form),
8870 bfd_get_filename (abfd));
8871 }
8872 }
8873
8874 if (abbrev->has_children)
8875 return skip_children (reader, info_ptr);
8876 else
8877 return info_ptr;
8878 }
8879
8880 /* Locate ORIG_PDI's sibling.
8881 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8882
8883 static const gdb_byte *
8884 locate_pdi_sibling (const struct die_reader_specs *reader,
8885 struct partial_die_info *orig_pdi,
8886 const gdb_byte *info_ptr)
8887 {
8888 /* Do we know the sibling already? */
8889
8890 if (orig_pdi->sibling)
8891 return orig_pdi->sibling;
8892
8893 /* Are there any children to deal with? */
8894
8895 if (!orig_pdi->has_children)
8896 return info_ptr;
8897
8898 /* Skip the children the long way. */
8899
8900 return skip_children (reader, info_ptr);
8901 }
8902
8903 /* Expand this partial symbol table into a full symbol table. SELF is
8904 not NULL. */
8905
8906 void
8907 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8908 {
8909 struct dwarf2_per_objfile *dwarf2_per_objfile
8910 = get_dwarf2_per_objfile (objfile);
8911
8912 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8913
8914 /* If this psymtab is constructed from a debug-only objfile, the
8915 has_section_at_zero flag will not necessarily be correct. We
8916 can get the correct value for this flag by looking at the data
8917 associated with the (presumably stripped) associated objfile. */
8918 if (objfile->separate_debug_objfile_backlink)
8919 {
8920 struct dwarf2_per_objfile *dpo_backlink
8921 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8922
8923 dwarf2_per_objfile->per_bfd->has_section_at_zero
8924 = dpo_backlink->per_bfd->has_section_at_zero;
8925 }
8926
8927 expand_psymtab (objfile);
8928
8929 process_cu_includes (dwarf2_per_objfile);
8930 }
8931 \f
8932 /* Reading in full CUs. */
8933
8934 /* Add PER_CU to the queue. */
8935
8936 static void
8937 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8938 enum language pretend_language)
8939 {
8940 per_cu->queued = 1;
8941 per_cu->per_bfd->queue.emplace (per_cu, pretend_language);
8942 }
8943
8944 /* If PER_CU is not yet queued, add it to the queue.
8945 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8946 dependency.
8947 The result is non-zero if PER_CU was queued, otherwise the result is zero
8948 meaning either PER_CU is already queued or it is already loaded.
8949
8950 N.B. There is an invariant here that if a CU is queued then it is loaded.
8951 The caller is required to load PER_CU if we return non-zero. */
8952
8953 static int
8954 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8955 struct dwarf2_per_cu_data *per_cu,
8956 enum language pretend_language)
8957 {
8958 /* We may arrive here during partial symbol reading, if we need full
8959 DIEs to process an unusual case (e.g. template arguments). Do
8960 not queue PER_CU, just tell our caller to load its DIEs. */
8961 if (per_cu->per_bfd->reading_partial_symbols)
8962 {
8963 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8964 return 1;
8965 return 0;
8966 }
8967
8968 /* Mark the dependence relation so that we don't flush PER_CU
8969 too early. */
8970 if (dependent_cu != NULL)
8971 dwarf2_add_dependence (dependent_cu, per_cu);
8972
8973 /* If it's already on the queue, we have nothing to do. */
8974 if (per_cu->queued)
8975 return 0;
8976
8977 /* If the compilation unit is already loaded, just mark it as
8978 used. */
8979 if (per_cu->cu != NULL)
8980 {
8981 per_cu->cu->last_used = 0;
8982 return 0;
8983 }
8984
8985 /* Add it to the queue. */
8986 queue_comp_unit (per_cu, pretend_language);
8987
8988 return 1;
8989 }
8990
8991 /* Process the queue. */
8992
8993 static void
8994 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8995 {
8996 if (dwarf_read_debug)
8997 {
8998 fprintf_unfiltered (gdb_stdlog,
8999 "Expanding one or more symtabs of objfile %s ...\n",
9000 objfile_name (dwarf2_per_objfile->objfile));
9001 }
9002
9003 /* The queue starts out with one item, but following a DIE reference
9004 may load a new CU, adding it to the end of the queue. */
9005 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
9006 {
9007 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
9008
9009 if (!dwarf2_per_objfile->symtab_set_p (item.per_cu)
9010 /* Skip dummy CUs. */
9011 && item.per_cu->cu != NULL)
9012 {
9013 struct dwarf2_per_cu_data *per_cu = item.per_cu;
9014 unsigned int debug_print_threshold;
9015 char buf[100];
9016
9017 if (per_cu->is_debug_types)
9018 {
9019 struct signatured_type *sig_type =
9020 (struct signatured_type *) per_cu;
9021
9022 sprintf (buf, "TU %s at offset %s",
9023 hex_string (sig_type->signature),
9024 sect_offset_str (per_cu->sect_off));
9025 /* There can be 100s of TUs.
9026 Only print them in verbose mode. */
9027 debug_print_threshold = 2;
9028 }
9029 else
9030 {
9031 sprintf (buf, "CU at offset %s",
9032 sect_offset_str (per_cu->sect_off));
9033 debug_print_threshold = 1;
9034 }
9035
9036 if (dwarf_read_debug >= debug_print_threshold)
9037 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9038
9039 if (per_cu->is_debug_types)
9040 process_full_type_unit (per_cu, dwarf2_per_objfile,
9041 item.pretend_language);
9042 else
9043 process_full_comp_unit (per_cu, dwarf2_per_objfile,
9044 item.pretend_language);
9045
9046 if (dwarf_read_debug >= debug_print_threshold)
9047 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9048 }
9049
9050 item.per_cu->queued = 0;
9051 dwarf2_per_objfile->per_bfd->queue.pop ();
9052 }
9053
9054 if (dwarf_read_debug)
9055 {
9056 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9057 objfile_name (dwarf2_per_objfile->objfile));
9058 }
9059 }
9060
9061 /* Read in full symbols for PST, and anything it depends on. */
9062
9063 void
9064 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9065 {
9066 gdb_assert (!readin_p (objfile));
9067
9068 expand_dependencies (objfile);
9069
9070 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9071 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9072 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9073 }
9074
9075 /* See psympriv.h. */
9076
9077 bool
9078 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9079 {
9080 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9081 return per_objfile->symtab_set_p (per_cu_data);
9082 }
9083
9084 /* See psympriv.h. */
9085
9086 compunit_symtab *
9087 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9088 {
9089 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9090 return per_objfile->get_symtab (per_cu_data);
9091 }
9092
9093 /* Trivial hash function for die_info: the hash value of a DIE
9094 is its offset in .debug_info for this objfile. */
9095
9096 static hashval_t
9097 die_hash (const void *item)
9098 {
9099 const struct die_info *die = (const struct die_info *) item;
9100
9101 return to_underlying (die->sect_off);
9102 }
9103
9104 /* Trivial comparison function for die_info structures: two DIEs
9105 are equal if they have the same offset. */
9106
9107 static int
9108 die_eq (const void *item_lhs, const void *item_rhs)
9109 {
9110 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9111 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9112
9113 return die_lhs->sect_off == die_rhs->sect_off;
9114 }
9115
9116 /* Load the DIEs associated with PER_CU into memory. */
9117
9118 static void
9119 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9120 dwarf2_per_objfile *per_objfile,
9121 bool skip_partial,
9122 enum language pretend_language)
9123 {
9124 gdb_assert (! this_cu->is_debug_types);
9125
9126 cutu_reader reader (this_cu, per_objfile, NULL, 1, skip_partial);
9127 if (reader.dummy_p)
9128 return;
9129
9130 struct dwarf2_cu *cu = reader.cu;
9131 const gdb_byte *info_ptr = reader.info_ptr;
9132
9133 gdb_assert (cu->die_hash == NULL);
9134 cu->die_hash =
9135 htab_create_alloc_ex (cu->header.length / 12,
9136 die_hash,
9137 die_eq,
9138 NULL,
9139 &cu->comp_unit_obstack,
9140 hashtab_obstack_allocate,
9141 dummy_obstack_deallocate);
9142
9143 if (reader.comp_unit_die->has_children)
9144 reader.comp_unit_die->child
9145 = read_die_and_siblings (&reader, reader.info_ptr,
9146 &info_ptr, reader.comp_unit_die);
9147 cu->dies = reader.comp_unit_die;
9148 /* comp_unit_die is not stored in die_hash, no need. */
9149
9150 /* We try not to read any attributes in this function, because not
9151 all CUs needed for references have been loaded yet, and symbol
9152 table processing isn't initialized. But we have to set the CU language,
9153 or we won't be able to build types correctly.
9154 Similarly, if we do not read the producer, we can not apply
9155 producer-specific interpretation. */
9156 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9157
9158 reader.keep ();
9159 }
9160
9161 /* Add a DIE to the delayed physname list. */
9162
9163 static void
9164 add_to_method_list (struct type *type, int fnfield_index, int index,
9165 const char *name, struct die_info *die,
9166 struct dwarf2_cu *cu)
9167 {
9168 struct delayed_method_info mi;
9169 mi.type = type;
9170 mi.fnfield_index = fnfield_index;
9171 mi.index = index;
9172 mi.name = name;
9173 mi.die = die;
9174 cu->method_list.push_back (mi);
9175 }
9176
9177 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9178 "const" / "volatile". If so, decrements LEN by the length of the
9179 modifier and return true. Otherwise return false. */
9180
9181 template<size_t N>
9182 static bool
9183 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9184 {
9185 size_t mod_len = sizeof (mod) - 1;
9186 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9187 {
9188 len -= mod_len;
9189 return true;
9190 }
9191 return false;
9192 }
9193
9194 /* Compute the physnames of any methods on the CU's method list.
9195
9196 The computation of method physnames is delayed in order to avoid the
9197 (bad) condition that one of the method's formal parameters is of an as yet
9198 incomplete type. */
9199
9200 static void
9201 compute_delayed_physnames (struct dwarf2_cu *cu)
9202 {
9203 /* Only C++ delays computing physnames. */
9204 if (cu->method_list.empty ())
9205 return;
9206 gdb_assert (cu->language == language_cplus);
9207
9208 for (const delayed_method_info &mi : cu->method_list)
9209 {
9210 const char *physname;
9211 struct fn_fieldlist *fn_flp
9212 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9213 physname = dwarf2_physname (mi.name, mi.die, cu);
9214 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9215 = physname ? physname : "";
9216
9217 /* Since there's no tag to indicate whether a method is a
9218 const/volatile overload, extract that information out of the
9219 demangled name. */
9220 if (physname != NULL)
9221 {
9222 size_t len = strlen (physname);
9223
9224 while (1)
9225 {
9226 if (physname[len] == ')') /* shortcut */
9227 break;
9228 else if (check_modifier (physname, len, " const"))
9229 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9230 else if (check_modifier (physname, len, " volatile"))
9231 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9232 else
9233 break;
9234 }
9235 }
9236 }
9237
9238 /* The list is no longer needed. */
9239 cu->method_list.clear ();
9240 }
9241
9242 /* Go objects should be embedded in a DW_TAG_module DIE,
9243 and it's not clear if/how imported objects will appear.
9244 To keep Go support simple until that's worked out,
9245 go back through what we've read and create something usable.
9246 We could do this while processing each DIE, and feels kinda cleaner,
9247 but that way is more invasive.
9248 This is to, for example, allow the user to type "p var" or "b main"
9249 without having to specify the package name, and allow lookups
9250 of module.object to work in contexts that use the expression
9251 parser. */
9252
9253 static void
9254 fixup_go_packaging (struct dwarf2_cu *cu)
9255 {
9256 gdb::unique_xmalloc_ptr<char> package_name;
9257 struct pending *list;
9258 int i;
9259
9260 for (list = *cu->get_builder ()->get_global_symbols ();
9261 list != NULL;
9262 list = list->next)
9263 {
9264 for (i = 0; i < list->nsyms; ++i)
9265 {
9266 struct symbol *sym = list->symbol[i];
9267
9268 if (sym->language () == language_go
9269 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9270 {
9271 gdb::unique_xmalloc_ptr<char> this_package_name
9272 (go_symbol_package_name (sym));
9273
9274 if (this_package_name == NULL)
9275 continue;
9276 if (package_name == NULL)
9277 package_name = std::move (this_package_name);
9278 else
9279 {
9280 struct objfile *objfile = cu->per_objfile->objfile;
9281 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9282 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9283 (symbol_symtab (sym) != NULL
9284 ? symtab_to_filename_for_display
9285 (symbol_symtab (sym))
9286 : objfile_name (objfile)),
9287 this_package_name.get (), package_name.get ());
9288 }
9289 }
9290 }
9291 }
9292
9293 if (package_name != NULL)
9294 {
9295 struct objfile *objfile = cu->per_objfile->objfile;
9296 const char *saved_package_name = objfile->intern (package_name.get ());
9297 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9298 saved_package_name);
9299 struct symbol *sym;
9300
9301 sym = new (&objfile->objfile_obstack) symbol;
9302 sym->set_language (language_go, &objfile->objfile_obstack);
9303 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9304 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9305 e.g., "main" finds the "main" module and not C's main(). */
9306 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9307 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9308 SYMBOL_TYPE (sym) = type;
9309
9310 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9311 }
9312 }
9313
9314 /* Allocate a fully-qualified name consisting of the two parts on the
9315 obstack. */
9316
9317 static const char *
9318 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9319 {
9320 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9321 }
9322
9323 /* A helper that allocates a variant part to attach to a Rust enum
9324 type. OBSTACK is where the results should be allocated. TYPE is
9325 the type we're processing. DISCRIMINANT_INDEX is the index of the
9326 discriminant. It must be the index of one of the fields of TYPE.
9327 DEFAULT_INDEX is the index of the default field; or -1 if there is
9328 no default. RANGES is indexed by "effective" field number (the
9329 field index, but omitting the discriminant and default fields) and
9330 must hold the discriminant values used by the variants. Note that
9331 RANGES must have a lifetime at least as long as OBSTACK -- either
9332 already allocated on it, or static. */
9333
9334 static void
9335 alloc_rust_variant (struct obstack *obstack, struct type *type,
9336 int discriminant_index, int default_index,
9337 gdb::array_view<discriminant_range> ranges)
9338 {
9339 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9340 must be handled by the caller. */
9341 gdb_assert (discriminant_index >= 0
9342 && discriminant_index < type->num_fields ());
9343 gdb_assert (default_index == -1
9344 || (default_index >= 0 && default_index < type->num_fields ()));
9345
9346 /* We have one variant for each non-discriminant field. */
9347 int n_variants = type->num_fields () - 1;
9348
9349 variant *variants = new (obstack) variant[n_variants];
9350 int var_idx = 0;
9351 int range_idx = 0;
9352 for (int i = 0; i < type->num_fields (); ++i)
9353 {
9354 if (i == discriminant_index)
9355 continue;
9356
9357 variants[var_idx].first_field = i;
9358 variants[var_idx].last_field = i + 1;
9359
9360 /* The default field does not need a range, but other fields do.
9361 We skipped the discriminant above. */
9362 if (i != default_index)
9363 {
9364 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9365 ++range_idx;
9366 }
9367
9368 ++var_idx;
9369 }
9370
9371 gdb_assert (range_idx == ranges.size ());
9372 gdb_assert (var_idx == n_variants);
9373
9374 variant_part *part = new (obstack) variant_part;
9375 part->discriminant_index = discriminant_index;
9376 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9377 discriminant_index));
9378 part->variants = gdb::array_view<variant> (variants, n_variants);
9379
9380 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9381 gdb::array_view<variant_part> *prop_value
9382 = new (storage) gdb::array_view<variant_part> (part, 1);
9383
9384 struct dynamic_prop prop;
9385 prop.kind = PROP_VARIANT_PARTS;
9386 prop.data.variant_parts = prop_value;
9387
9388 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9389 }
9390
9391 /* Some versions of rustc emitted enums in an unusual way.
9392
9393 Ordinary enums were emitted as unions. The first element of each
9394 structure in the union was named "RUST$ENUM$DISR". This element
9395 held the discriminant.
9396
9397 These versions of Rust also implemented the "non-zero"
9398 optimization. When the enum had two values, and one is empty and
9399 the other holds a pointer that cannot be zero, the pointer is used
9400 as the discriminant, with a zero value meaning the empty variant.
9401 Here, the union's first member is of the form
9402 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9403 where the fieldnos are the indices of the fields that should be
9404 traversed in order to find the field (which may be several fields deep)
9405 and the variantname is the name of the variant of the case when the
9406 field is zero.
9407
9408 This function recognizes whether TYPE is of one of these forms,
9409 and, if so, smashes it to be a variant type. */
9410
9411 static void
9412 quirk_rust_enum (struct type *type, struct objfile *objfile)
9413 {
9414 gdb_assert (type->code () == TYPE_CODE_UNION);
9415
9416 /* We don't need to deal with empty enums. */
9417 if (type->num_fields () == 0)
9418 return;
9419
9420 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9421 if (type->num_fields () == 1
9422 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9423 {
9424 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9425
9426 /* Decode the field name to find the offset of the
9427 discriminant. */
9428 ULONGEST bit_offset = 0;
9429 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9430 while (name[0] >= '0' && name[0] <= '9')
9431 {
9432 char *tail;
9433 unsigned long index = strtoul (name, &tail, 10);
9434 name = tail;
9435 if (*name != '$'
9436 || index >= field_type->num_fields ()
9437 || (TYPE_FIELD_LOC_KIND (field_type, index)
9438 != FIELD_LOC_KIND_BITPOS))
9439 {
9440 complaint (_("Could not parse Rust enum encoding string \"%s\""
9441 "[in module %s]"),
9442 TYPE_FIELD_NAME (type, 0),
9443 objfile_name (objfile));
9444 return;
9445 }
9446 ++name;
9447
9448 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9449 field_type = TYPE_FIELD_TYPE (field_type, index);
9450 }
9451
9452 /* Smash this type to be a structure type. We have to do this
9453 because the type has already been recorded. */
9454 type->set_code (TYPE_CODE_STRUCT);
9455 type->set_num_fields (3);
9456 /* Save the field we care about. */
9457 struct field saved_field = type->field (0);
9458 type->set_fields
9459 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9460
9461 /* Put the discriminant at index 0. */
9462 TYPE_FIELD_TYPE (type, 0) = field_type;
9463 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9464 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9465 SET_FIELD_BITPOS (type->field (0), bit_offset);
9466
9467 /* The order of fields doesn't really matter, so put the real
9468 field at index 1 and the data-less field at index 2. */
9469 type->field (1) = saved_field;
9470 TYPE_FIELD_NAME (type, 1)
9471 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9472 TYPE_FIELD_TYPE (type, 1)->set_name
9473 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9474 TYPE_FIELD_NAME (type, 1)));
9475
9476 const char *dataless_name
9477 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9478 name);
9479 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9480 dataless_name);
9481 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9482 /* NAME points into the original discriminant name, which
9483 already has the correct lifetime. */
9484 TYPE_FIELD_NAME (type, 2) = name;
9485 SET_FIELD_BITPOS (type->field (2), 0);
9486
9487 /* Indicate that this is a variant type. */
9488 static discriminant_range ranges[1] = { { 0, 0 } };
9489 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9490 }
9491 /* A union with a single anonymous field is probably an old-style
9492 univariant enum. */
9493 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9494 {
9495 /* Smash this type to be a structure type. We have to do this
9496 because the type has already been recorded. */
9497 type->set_code (TYPE_CODE_STRUCT);
9498
9499 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9500 const char *variant_name
9501 = rust_last_path_segment (field_type->name ());
9502 TYPE_FIELD_NAME (type, 0) = variant_name;
9503 field_type->set_name
9504 (rust_fully_qualify (&objfile->objfile_obstack,
9505 type->name (), variant_name));
9506 }
9507 else
9508 {
9509 struct type *disr_type = nullptr;
9510 for (int i = 0; i < type->num_fields (); ++i)
9511 {
9512 disr_type = TYPE_FIELD_TYPE (type, i);
9513
9514 if (disr_type->code () != TYPE_CODE_STRUCT)
9515 {
9516 /* All fields of a true enum will be structs. */
9517 return;
9518 }
9519 else if (disr_type->num_fields () == 0)
9520 {
9521 /* Could be data-less variant, so keep going. */
9522 disr_type = nullptr;
9523 }
9524 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9525 "RUST$ENUM$DISR") != 0)
9526 {
9527 /* Not a Rust enum. */
9528 return;
9529 }
9530 else
9531 {
9532 /* Found one. */
9533 break;
9534 }
9535 }
9536
9537 /* If we got here without a discriminant, then it's probably
9538 just a union. */
9539 if (disr_type == nullptr)
9540 return;
9541
9542 /* Smash this type to be a structure type. We have to do this
9543 because the type has already been recorded. */
9544 type->set_code (TYPE_CODE_STRUCT);
9545
9546 /* Make space for the discriminant field. */
9547 struct field *disr_field = &disr_type->field (0);
9548 field *new_fields
9549 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9550 * sizeof (struct field)));
9551 memcpy (new_fields + 1, type->fields (),
9552 type->num_fields () * sizeof (struct field));
9553 type->set_fields (new_fields);
9554 type->set_num_fields (type->num_fields () + 1);
9555
9556 /* Install the discriminant at index 0 in the union. */
9557 type->field (0) = *disr_field;
9558 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9559 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9560
9561 /* We need a way to find the correct discriminant given a
9562 variant name. For convenience we build a map here. */
9563 struct type *enum_type = FIELD_TYPE (*disr_field);
9564 std::unordered_map<std::string, ULONGEST> discriminant_map;
9565 for (int i = 0; i < enum_type->num_fields (); ++i)
9566 {
9567 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9568 {
9569 const char *name
9570 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9571 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9572 }
9573 }
9574
9575 int n_fields = type->num_fields ();
9576 /* We don't need a range entry for the discriminant, but we do
9577 need one for every other field, as there is no default
9578 variant. */
9579 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9580 discriminant_range,
9581 n_fields - 1);
9582 /* Skip the discriminant here. */
9583 for (int i = 1; i < n_fields; ++i)
9584 {
9585 /* Find the final word in the name of this variant's type.
9586 That name can be used to look up the correct
9587 discriminant. */
9588 const char *variant_name
9589 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9590
9591 auto iter = discriminant_map.find (variant_name);
9592 if (iter != discriminant_map.end ())
9593 {
9594 ranges[i].low = iter->second;
9595 ranges[i].high = iter->second;
9596 }
9597
9598 /* Remove the discriminant field, if it exists. */
9599 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9600 if (sub_type->num_fields () > 0)
9601 {
9602 sub_type->set_num_fields (sub_type->num_fields () - 1);
9603 sub_type->set_fields (sub_type->fields () + 1);
9604 }
9605 TYPE_FIELD_NAME (type, i) = variant_name;
9606 sub_type->set_name
9607 (rust_fully_qualify (&objfile->objfile_obstack,
9608 type->name (), variant_name));
9609 }
9610
9611 /* Indicate that this is a variant type. */
9612 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9613 gdb::array_view<discriminant_range> (ranges,
9614 n_fields - 1));
9615 }
9616 }
9617
9618 /* Rewrite some Rust unions to be structures with variants parts. */
9619
9620 static void
9621 rust_union_quirks (struct dwarf2_cu *cu)
9622 {
9623 gdb_assert (cu->language == language_rust);
9624 for (type *type_ : cu->rust_unions)
9625 quirk_rust_enum (type_, cu->per_objfile->objfile);
9626 /* We don't need this any more. */
9627 cu->rust_unions.clear ();
9628 }
9629
9630 /* A helper function for computing the list of all symbol tables
9631 included by PER_CU. */
9632
9633 static void
9634 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9635 htab_t all_children, htab_t all_type_symtabs,
9636 dwarf2_per_cu_data *per_cu,
9637 dwarf2_per_objfile *per_objfile,
9638 struct compunit_symtab *immediate_parent)
9639 {
9640 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9641 if (*slot != NULL)
9642 {
9643 /* This inclusion and its children have been processed. */
9644 return;
9645 }
9646
9647 *slot = per_cu;
9648
9649 /* Only add a CU if it has a symbol table. */
9650 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9651 if (cust != NULL)
9652 {
9653 /* If this is a type unit only add its symbol table if we haven't
9654 seen it yet (type unit per_cu's can share symtabs). */
9655 if (per_cu->is_debug_types)
9656 {
9657 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9658 if (*slot == NULL)
9659 {
9660 *slot = cust;
9661 result->push_back (cust);
9662 if (cust->user == NULL)
9663 cust->user = immediate_parent;
9664 }
9665 }
9666 else
9667 {
9668 result->push_back (cust);
9669 if (cust->user == NULL)
9670 cust->user = immediate_parent;
9671 }
9672 }
9673
9674 if (!per_cu->imported_symtabs_empty ())
9675 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9676 {
9677 recursively_compute_inclusions (result, all_children,
9678 all_type_symtabs, ptr, per_objfile,
9679 cust);
9680 }
9681 }
9682
9683 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9684 PER_CU. */
9685
9686 static void
9687 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9688 dwarf2_per_objfile *per_objfile)
9689 {
9690 gdb_assert (! per_cu->is_debug_types);
9691
9692 if (!per_cu->imported_symtabs_empty ())
9693 {
9694 int len;
9695 std::vector<compunit_symtab *> result_symtabs;
9696 htab_t all_children, all_type_symtabs;
9697 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9698
9699 /* If we don't have a symtab, we can just skip this case. */
9700 if (cust == NULL)
9701 return;
9702
9703 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9704 NULL, xcalloc, xfree);
9705 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9706 NULL, xcalloc, xfree);
9707
9708 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9709 {
9710 recursively_compute_inclusions (&result_symtabs, all_children,
9711 all_type_symtabs, ptr, per_objfile,
9712 cust);
9713 }
9714
9715 /* Now we have a transitive closure of all the included symtabs. */
9716 len = result_symtabs.size ();
9717 cust->includes
9718 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
9719 struct compunit_symtab *, len + 1);
9720 memcpy (cust->includes, result_symtabs.data (),
9721 len * sizeof (compunit_symtab *));
9722 cust->includes[len] = NULL;
9723
9724 htab_delete (all_children);
9725 htab_delete (all_type_symtabs);
9726 }
9727 }
9728
9729 /* Compute the 'includes' field for the symtabs of all the CUs we just
9730 read. */
9731
9732 static void
9733 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9734 {
9735 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9736 {
9737 if (! iter->is_debug_types)
9738 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9739 }
9740
9741 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9742 }
9743
9744 /* Generate full symbol information for PER_CU, whose DIEs have
9745 already been loaded into memory. */
9746
9747 static void
9748 process_full_comp_unit (dwarf2_per_cu_data *per_cu,
9749 dwarf2_per_objfile *dwarf2_per_objfile,
9750 enum language pretend_language)
9751 {
9752 struct dwarf2_cu *cu = per_cu->cu;
9753 struct objfile *objfile = dwarf2_per_objfile->objfile;
9754 struct gdbarch *gdbarch = objfile->arch ();
9755 CORE_ADDR lowpc, highpc;
9756 struct compunit_symtab *cust;
9757 CORE_ADDR baseaddr;
9758 struct block *static_block;
9759 CORE_ADDR addr;
9760
9761 baseaddr = objfile->text_section_offset ();
9762
9763 /* Clear the list here in case something was left over. */
9764 cu->method_list.clear ();
9765
9766 cu->language = pretend_language;
9767 cu->language_defn = language_def (cu->language);
9768
9769 /* Do line number decoding in read_file_scope () */
9770 process_die (cu->dies, cu);
9771
9772 /* For now fudge the Go package. */
9773 if (cu->language == language_go)
9774 fixup_go_packaging (cu);
9775
9776 /* Now that we have processed all the DIEs in the CU, all the types
9777 should be complete, and it should now be safe to compute all of the
9778 physnames. */
9779 compute_delayed_physnames (cu);
9780
9781 if (cu->language == language_rust)
9782 rust_union_quirks (cu);
9783
9784 /* Some compilers don't define a DW_AT_high_pc attribute for the
9785 compilation unit. If the DW_AT_high_pc is missing, synthesize
9786 it, by scanning the DIE's below the compilation unit. */
9787 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9788
9789 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9790 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9791
9792 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9793 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9794 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9795 addrmap to help ensure it has an accurate map of pc values belonging to
9796 this comp unit. */
9797 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9798
9799 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9800 SECT_OFF_TEXT (objfile),
9801 0);
9802
9803 if (cust != NULL)
9804 {
9805 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9806
9807 /* Set symtab language to language from DW_AT_language. If the
9808 compilation is from a C file generated by language preprocessors, do
9809 not set the language if it was already deduced by start_subfile. */
9810 if (!(cu->language == language_c
9811 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9812 COMPUNIT_FILETABS (cust)->language = cu->language;
9813
9814 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9815 produce DW_AT_location with location lists but it can be possibly
9816 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9817 there were bugs in prologue debug info, fixed later in GCC-4.5
9818 by "unwind info for epilogues" patch (which is not directly related).
9819
9820 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9821 needed, it would be wrong due to missing DW_AT_producer there.
9822
9823 Still one can confuse GDB by using non-standard GCC compilation
9824 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9825 */
9826 if (cu->has_loclist && gcc_4_minor >= 5)
9827 cust->locations_valid = 1;
9828
9829 if (gcc_4_minor >= 5)
9830 cust->epilogue_unwind_valid = 1;
9831
9832 cust->call_site_htab = cu->call_site_htab;
9833 }
9834
9835 dwarf2_per_objfile->set_symtab (per_cu, cust);
9836
9837 /* Push it for inclusion processing later. */
9838 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (per_cu);
9839
9840 /* Not needed any more. */
9841 cu->reset_builder ();
9842 }
9843
9844 /* Generate full symbol information for type unit PER_CU, whose DIEs have
9845 already been loaded into memory. */
9846
9847 static void
9848 process_full_type_unit (dwarf2_per_cu_data *per_cu,
9849 dwarf2_per_objfile *dwarf2_per_objfile,
9850 enum language pretend_language)
9851 {
9852 struct dwarf2_cu *cu = per_cu->cu;
9853 struct objfile *objfile = dwarf2_per_objfile->objfile;
9854 struct compunit_symtab *cust;
9855 struct signatured_type *sig_type;
9856
9857 gdb_assert (per_cu->is_debug_types);
9858 sig_type = (struct signatured_type *) per_cu;
9859
9860 /* Clear the list here in case something was left over. */
9861 cu->method_list.clear ();
9862
9863 cu->language = pretend_language;
9864 cu->language_defn = language_def (cu->language);
9865
9866 /* The symbol tables are set up in read_type_unit_scope. */
9867 process_die (cu->dies, cu);
9868
9869 /* For now fudge the Go package. */
9870 if (cu->language == language_go)
9871 fixup_go_packaging (cu);
9872
9873 /* Now that we have processed all the DIEs in the CU, all the types
9874 should be complete, and it should now be safe to compute all of the
9875 physnames. */
9876 compute_delayed_physnames (cu);
9877
9878 if (cu->language == language_rust)
9879 rust_union_quirks (cu);
9880
9881 /* TUs share symbol tables.
9882 If this is the first TU to use this symtab, complete the construction
9883 of it with end_expandable_symtab. Otherwise, complete the addition of
9884 this TU's symbols to the existing symtab. */
9885 if (sig_type->type_unit_group->compunit_symtab == NULL)
9886 {
9887 buildsym_compunit *builder = cu->get_builder ();
9888 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9889 sig_type->type_unit_group->compunit_symtab = cust;
9890
9891 if (cust != NULL)
9892 {
9893 /* Set symtab language to language from DW_AT_language. If the
9894 compilation is from a C file generated by language preprocessors,
9895 do not set the language if it was already deduced by
9896 start_subfile. */
9897 if (!(cu->language == language_c
9898 && COMPUNIT_FILETABS (cust)->language != language_c))
9899 COMPUNIT_FILETABS (cust)->language = cu->language;
9900 }
9901 }
9902 else
9903 {
9904 cu->get_builder ()->augment_type_symtab ();
9905 cust = sig_type->type_unit_group->compunit_symtab;
9906 }
9907
9908 dwarf2_per_objfile->set_symtab (per_cu, cust);
9909
9910 /* Not needed any more. */
9911 cu->reset_builder ();
9912 }
9913
9914 /* Process an imported unit DIE. */
9915
9916 static void
9917 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9918 {
9919 struct attribute *attr;
9920
9921 /* For now we don't handle imported units in type units. */
9922 if (cu->per_cu->is_debug_types)
9923 {
9924 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9925 " supported in type units [in module %s]"),
9926 objfile_name (cu->per_objfile->objfile));
9927 }
9928
9929 attr = dwarf2_attr (die, DW_AT_import, cu);
9930 if (attr != NULL)
9931 {
9932 sect_offset sect_off = attr->get_ref_die_offset ();
9933 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9934 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9935 dwarf2_per_cu_data *per_cu
9936 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9937
9938 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9939 into another compilation unit, at root level. Regard this as a hint,
9940 and ignore it. */
9941 if (die->parent && die->parent->parent == NULL
9942 && per_cu->unit_type == DW_UT_compile
9943 && per_cu->lang == language_cplus)
9944 return;
9945
9946 /* If necessary, add it to the queue and load its DIEs. */
9947 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
9948 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9949
9950 cu->per_cu->imported_symtabs_push (per_cu);
9951 }
9952 }
9953
9954 /* RAII object that represents a process_die scope: i.e.,
9955 starts/finishes processing a DIE. */
9956 class process_die_scope
9957 {
9958 public:
9959 process_die_scope (die_info *die, dwarf2_cu *cu)
9960 : m_die (die), m_cu (cu)
9961 {
9962 /* We should only be processing DIEs not already in process. */
9963 gdb_assert (!m_die->in_process);
9964 m_die->in_process = true;
9965 }
9966
9967 ~process_die_scope ()
9968 {
9969 m_die->in_process = false;
9970
9971 /* If we're done processing the DIE for the CU that owns the line
9972 header, we don't need the line header anymore. */
9973 if (m_cu->line_header_die_owner == m_die)
9974 {
9975 delete m_cu->line_header;
9976 m_cu->line_header = NULL;
9977 m_cu->line_header_die_owner = NULL;
9978 }
9979 }
9980
9981 private:
9982 die_info *m_die;
9983 dwarf2_cu *m_cu;
9984 };
9985
9986 /* Process a die and its children. */
9987
9988 static void
9989 process_die (struct die_info *die, struct dwarf2_cu *cu)
9990 {
9991 process_die_scope scope (die, cu);
9992
9993 switch (die->tag)
9994 {
9995 case DW_TAG_padding:
9996 break;
9997 case DW_TAG_compile_unit:
9998 case DW_TAG_partial_unit:
9999 read_file_scope (die, cu);
10000 break;
10001 case DW_TAG_type_unit:
10002 read_type_unit_scope (die, cu);
10003 break;
10004 case DW_TAG_subprogram:
10005 /* Nested subprograms in Fortran get a prefix. */
10006 if (cu->language == language_fortran
10007 && die->parent != NULL
10008 && die->parent->tag == DW_TAG_subprogram)
10009 cu->processing_has_namespace_info = true;
10010 /* Fall through. */
10011 case DW_TAG_inlined_subroutine:
10012 read_func_scope (die, cu);
10013 break;
10014 case DW_TAG_lexical_block:
10015 case DW_TAG_try_block:
10016 case DW_TAG_catch_block:
10017 read_lexical_block_scope (die, cu);
10018 break;
10019 case DW_TAG_call_site:
10020 case DW_TAG_GNU_call_site:
10021 read_call_site_scope (die, cu);
10022 break;
10023 case DW_TAG_class_type:
10024 case DW_TAG_interface_type:
10025 case DW_TAG_structure_type:
10026 case DW_TAG_union_type:
10027 process_structure_scope (die, cu);
10028 break;
10029 case DW_TAG_enumeration_type:
10030 process_enumeration_scope (die, cu);
10031 break;
10032
10033 /* These dies have a type, but processing them does not create
10034 a symbol or recurse to process the children. Therefore we can
10035 read them on-demand through read_type_die. */
10036 case DW_TAG_subroutine_type:
10037 case DW_TAG_set_type:
10038 case DW_TAG_array_type:
10039 case DW_TAG_pointer_type:
10040 case DW_TAG_ptr_to_member_type:
10041 case DW_TAG_reference_type:
10042 case DW_TAG_rvalue_reference_type:
10043 case DW_TAG_string_type:
10044 break;
10045
10046 case DW_TAG_base_type:
10047 case DW_TAG_subrange_type:
10048 case DW_TAG_typedef:
10049 /* Add a typedef symbol for the type definition, if it has a
10050 DW_AT_name. */
10051 new_symbol (die, read_type_die (die, cu), cu);
10052 break;
10053 case DW_TAG_common_block:
10054 read_common_block (die, cu);
10055 break;
10056 case DW_TAG_common_inclusion:
10057 break;
10058 case DW_TAG_namespace:
10059 cu->processing_has_namespace_info = true;
10060 read_namespace (die, cu);
10061 break;
10062 case DW_TAG_module:
10063 cu->processing_has_namespace_info = true;
10064 read_module (die, cu);
10065 break;
10066 case DW_TAG_imported_declaration:
10067 cu->processing_has_namespace_info = true;
10068 if (read_namespace_alias (die, cu))
10069 break;
10070 /* The declaration is not a global namespace alias. */
10071 /* Fall through. */
10072 case DW_TAG_imported_module:
10073 cu->processing_has_namespace_info = true;
10074 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10075 || cu->language != language_fortran))
10076 complaint (_("Tag '%s' has unexpected children"),
10077 dwarf_tag_name (die->tag));
10078 read_import_statement (die, cu);
10079 break;
10080
10081 case DW_TAG_imported_unit:
10082 process_imported_unit_die (die, cu);
10083 break;
10084
10085 case DW_TAG_variable:
10086 read_variable (die, cu);
10087 break;
10088
10089 default:
10090 new_symbol (die, NULL, cu);
10091 break;
10092 }
10093 }
10094 \f
10095 /* DWARF name computation. */
10096
10097 /* A helper function for dwarf2_compute_name which determines whether DIE
10098 needs to have the name of the scope prepended to the name listed in the
10099 die. */
10100
10101 static int
10102 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10103 {
10104 struct attribute *attr;
10105
10106 switch (die->tag)
10107 {
10108 case DW_TAG_namespace:
10109 case DW_TAG_typedef:
10110 case DW_TAG_class_type:
10111 case DW_TAG_interface_type:
10112 case DW_TAG_structure_type:
10113 case DW_TAG_union_type:
10114 case DW_TAG_enumeration_type:
10115 case DW_TAG_enumerator:
10116 case DW_TAG_subprogram:
10117 case DW_TAG_inlined_subroutine:
10118 case DW_TAG_member:
10119 case DW_TAG_imported_declaration:
10120 return 1;
10121
10122 case DW_TAG_variable:
10123 case DW_TAG_constant:
10124 /* We only need to prefix "globally" visible variables. These include
10125 any variable marked with DW_AT_external or any variable that
10126 lives in a namespace. [Variables in anonymous namespaces
10127 require prefixing, but they are not DW_AT_external.] */
10128
10129 if (dwarf2_attr (die, DW_AT_specification, cu))
10130 {
10131 struct dwarf2_cu *spec_cu = cu;
10132
10133 return die_needs_namespace (die_specification (die, &spec_cu),
10134 spec_cu);
10135 }
10136
10137 attr = dwarf2_attr (die, DW_AT_external, cu);
10138 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10139 && die->parent->tag != DW_TAG_module)
10140 return 0;
10141 /* A variable in a lexical block of some kind does not need a
10142 namespace, even though in C++ such variables may be external
10143 and have a mangled name. */
10144 if (die->parent->tag == DW_TAG_lexical_block
10145 || die->parent->tag == DW_TAG_try_block
10146 || die->parent->tag == DW_TAG_catch_block
10147 || die->parent->tag == DW_TAG_subprogram)
10148 return 0;
10149 return 1;
10150
10151 default:
10152 return 0;
10153 }
10154 }
10155
10156 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10157 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10158 defined for the given DIE. */
10159
10160 static struct attribute *
10161 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10162 {
10163 struct attribute *attr;
10164
10165 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10166 if (attr == NULL)
10167 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10168
10169 return attr;
10170 }
10171
10172 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10173 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10174 defined for the given DIE. */
10175
10176 static const char *
10177 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10178 {
10179 const char *linkage_name;
10180
10181 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10182 if (linkage_name == NULL)
10183 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10184
10185 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10186 See https://github.com/rust-lang/rust/issues/32925. */
10187 if (cu->language == language_rust && linkage_name != NULL
10188 && strchr (linkage_name, '{') != NULL)
10189 linkage_name = NULL;
10190
10191 return linkage_name;
10192 }
10193
10194 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10195 compute the physname for the object, which include a method's:
10196 - formal parameters (C++),
10197 - receiver type (Go),
10198
10199 The term "physname" is a bit confusing.
10200 For C++, for example, it is the demangled name.
10201 For Go, for example, it's the mangled name.
10202
10203 For Ada, return the DIE's linkage name rather than the fully qualified
10204 name. PHYSNAME is ignored..
10205
10206 The result is allocated on the objfile->per_bfd's obstack and
10207 canonicalized. */
10208
10209 static const char *
10210 dwarf2_compute_name (const char *name,
10211 struct die_info *die, struct dwarf2_cu *cu,
10212 int physname)
10213 {
10214 struct objfile *objfile = cu->per_objfile->objfile;
10215
10216 if (name == NULL)
10217 name = dwarf2_name (die, cu);
10218
10219 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10220 but otherwise compute it by typename_concat inside GDB.
10221 FIXME: Actually this is not really true, or at least not always true.
10222 It's all very confusing. compute_and_set_names doesn't try to demangle
10223 Fortran names because there is no mangling standard. So new_symbol
10224 will set the demangled name to the result of dwarf2_full_name, and it is
10225 the demangled name that GDB uses if it exists. */
10226 if (cu->language == language_ada
10227 || (cu->language == language_fortran && physname))
10228 {
10229 /* For Ada unit, we prefer the linkage name over the name, as
10230 the former contains the exported name, which the user expects
10231 to be able to reference. Ideally, we want the user to be able
10232 to reference this entity using either natural or linkage name,
10233 but we haven't started looking at this enhancement yet. */
10234 const char *linkage_name = dw2_linkage_name (die, cu);
10235
10236 if (linkage_name != NULL)
10237 return linkage_name;
10238 }
10239
10240 /* These are the only languages we know how to qualify names in. */
10241 if (name != NULL
10242 && (cu->language == language_cplus
10243 || cu->language == language_fortran || cu->language == language_d
10244 || cu->language == language_rust))
10245 {
10246 if (die_needs_namespace (die, cu))
10247 {
10248 const char *prefix;
10249 const char *canonical_name = NULL;
10250
10251 string_file buf;
10252
10253 prefix = determine_prefix (die, cu);
10254 if (*prefix != '\0')
10255 {
10256 gdb::unique_xmalloc_ptr<char> prefixed_name
10257 (typename_concat (NULL, prefix, name, physname, cu));
10258
10259 buf.puts (prefixed_name.get ());
10260 }
10261 else
10262 buf.puts (name);
10263
10264 /* Template parameters may be specified in the DIE's DW_AT_name, or
10265 as children with DW_TAG_template_type_param or
10266 DW_TAG_value_type_param. If the latter, add them to the name
10267 here. If the name already has template parameters, then
10268 skip this step; some versions of GCC emit both, and
10269 it is more efficient to use the pre-computed name.
10270
10271 Something to keep in mind about this process: it is very
10272 unlikely, or in some cases downright impossible, to produce
10273 something that will match the mangled name of a function.
10274 If the definition of the function has the same debug info,
10275 we should be able to match up with it anyway. But fallbacks
10276 using the minimal symbol, for instance to find a method
10277 implemented in a stripped copy of libstdc++, will not work.
10278 If we do not have debug info for the definition, we will have to
10279 match them up some other way.
10280
10281 When we do name matching there is a related problem with function
10282 templates; two instantiated function templates are allowed to
10283 differ only by their return types, which we do not add here. */
10284
10285 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10286 {
10287 struct attribute *attr;
10288 struct die_info *child;
10289 int first = 1;
10290
10291 die->building_fullname = 1;
10292
10293 for (child = die->child; child != NULL; child = child->sibling)
10294 {
10295 struct type *type;
10296 LONGEST value;
10297 const gdb_byte *bytes;
10298 struct dwarf2_locexpr_baton *baton;
10299 struct value *v;
10300
10301 if (child->tag != DW_TAG_template_type_param
10302 && child->tag != DW_TAG_template_value_param)
10303 continue;
10304
10305 if (first)
10306 {
10307 buf.puts ("<");
10308 first = 0;
10309 }
10310 else
10311 buf.puts (", ");
10312
10313 attr = dwarf2_attr (child, DW_AT_type, cu);
10314 if (attr == NULL)
10315 {
10316 complaint (_("template parameter missing DW_AT_type"));
10317 buf.puts ("UNKNOWN_TYPE");
10318 continue;
10319 }
10320 type = die_type (child, cu);
10321
10322 if (child->tag == DW_TAG_template_type_param)
10323 {
10324 c_print_type (type, "", &buf, -1, 0, cu->language,
10325 &type_print_raw_options);
10326 continue;
10327 }
10328
10329 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10330 if (attr == NULL)
10331 {
10332 complaint (_("template parameter missing "
10333 "DW_AT_const_value"));
10334 buf.puts ("UNKNOWN_VALUE");
10335 continue;
10336 }
10337
10338 dwarf2_const_value_attr (attr, type, name,
10339 &cu->comp_unit_obstack, cu,
10340 &value, &bytes, &baton);
10341
10342 if (TYPE_NOSIGN (type))
10343 /* GDB prints characters as NUMBER 'CHAR'. If that's
10344 changed, this can use value_print instead. */
10345 c_printchar (value, type, &buf);
10346 else
10347 {
10348 struct value_print_options opts;
10349
10350 if (baton != NULL)
10351 v = dwarf2_evaluate_loc_desc (type, NULL,
10352 baton->data,
10353 baton->size,
10354 baton->per_cu);
10355 else if (bytes != NULL)
10356 {
10357 v = allocate_value (type);
10358 memcpy (value_contents_writeable (v), bytes,
10359 TYPE_LENGTH (type));
10360 }
10361 else
10362 v = value_from_longest (type, value);
10363
10364 /* Specify decimal so that we do not depend on
10365 the radix. */
10366 get_formatted_print_options (&opts, 'd');
10367 opts.raw = 1;
10368 value_print (v, &buf, &opts);
10369 release_value (v);
10370 }
10371 }
10372
10373 die->building_fullname = 0;
10374
10375 if (!first)
10376 {
10377 /* Close the argument list, with a space if necessary
10378 (nested templates). */
10379 if (!buf.empty () && buf.string ().back () == '>')
10380 buf.puts (" >");
10381 else
10382 buf.puts (">");
10383 }
10384 }
10385
10386 /* For C++ methods, append formal parameter type
10387 information, if PHYSNAME. */
10388
10389 if (physname && die->tag == DW_TAG_subprogram
10390 && cu->language == language_cplus)
10391 {
10392 struct type *type = read_type_die (die, cu);
10393
10394 c_type_print_args (type, &buf, 1, cu->language,
10395 &type_print_raw_options);
10396
10397 if (cu->language == language_cplus)
10398 {
10399 /* Assume that an artificial first parameter is
10400 "this", but do not crash if it is not. RealView
10401 marks unnamed (and thus unused) parameters as
10402 artificial; there is no way to differentiate
10403 the two cases. */
10404 if (type->num_fields () > 0
10405 && TYPE_FIELD_ARTIFICIAL (type, 0)
10406 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10407 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10408 0))))
10409 buf.puts (" const");
10410 }
10411 }
10412
10413 const std::string &intermediate_name = buf.string ();
10414
10415 if (cu->language == language_cplus)
10416 canonical_name
10417 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10418 objfile);
10419
10420 /* If we only computed INTERMEDIATE_NAME, or if
10421 INTERMEDIATE_NAME is already canonical, then we need to
10422 intern it. */
10423 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10424 name = objfile->intern (intermediate_name);
10425 else
10426 name = canonical_name;
10427 }
10428 }
10429
10430 return name;
10431 }
10432
10433 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10434 If scope qualifiers are appropriate they will be added. The result
10435 will be allocated on the storage_obstack, or NULL if the DIE does
10436 not have a name. NAME may either be from a previous call to
10437 dwarf2_name or NULL.
10438
10439 The output string will be canonicalized (if C++). */
10440
10441 static const char *
10442 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10443 {
10444 return dwarf2_compute_name (name, die, cu, 0);
10445 }
10446
10447 /* Construct a physname for the given DIE in CU. NAME may either be
10448 from a previous call to dwarf2_name or NULL. The result will be
10449 allocated on the objfile_objstack or NULL if the DIE does not have a
10450 name.
10451
10452 The output string will be canonicalized (if C++). */
10453
10454 static const char *
10455 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10456 {
10457 struct objfile *objfile = cu->per_objfile->objfile;
10458 const char *retval, *mangled = NULL, *canon = NULL;
10459 int need_copy = 1;
10460
10461 /* In this case dwarf2_compute_name is just a shortcut not building anything
10462 on its own. */
10463 if (!die_needs_namespace (die, cu))
10464 return dwarf2_compute_name (name, die, cu, 1);
10465
10466 if (cu->language != language_rust)
10467 mangled = dw2_linkage_name (die, cu);
10468
10469 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10470 has computed. */
10471 gdb::unique_xmalloc_ptr<char> demangled;
10472 if (mangled != NULL)
10473 {
10474
10475 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10476 {
10477 /* Do nothing (do not demangle the symbol name). */
10478 }
10479 else if (cu->language == language_go)
10480 {
10481 /* This is a lie, but we already lie to the caller new_symbol.
10482 new_symbol assumes we return the mangled name.
10483 This just undoes that lie until things are cleaned up. */
10484 }
10485 else
10486 {
10487 /* Use DMGL_RET_DROP for C++ template functions to suppress
10488 their return type. It is easier for GDB users to search
10489 for such functions as `name(params)' than `long name(params)'.
10490 In such case the minimal symbol names do not match the full
10491 symbol names but for template functions there is never a need
10492 to look up their definition from their declaration so
10493 the only disadvantage remains the minimal symbol variant
10494 `long name(params)' does not have the proper inferior type. */
10495 demangled.reset (gdb_demangle (mangled,
10496 (DMGL_PARAMS | DMGL_ANSI
10497 | DMGL_RET_DROP)));
10498 }
10499 if (demangled)
10500 canon = demangled.get ();
10501 else
10502 {
10503 canon = mangled;
10504 need_copy = 0;
10505 }
10506 }
10507
10508 if (canon == NULL || check_physname)
10509 {
10510 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10511
10512 if (canon != NULL && strcmp (physname, canon) != 0)
10513 {
10514 /* It may not mean a bug in GDB. The compiler could also
10515 compute DW_AT_linkage_name incorrectly. But in such case
10516 GDB would need to be bug-to-bug compatible. */
10517
10518 complaint (_("Computed physname <%s> does not match demangled <%s> "
10519 "(from linkage <%s>) - DIE at %s [in module %s]"),
10520 physname, canon, mangled, sect_offset_str (die->sect_off),
10521 objfile_name (objfile));
10522
10523 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10524 is available here - over computed PHYSNAME. It is safer
10525 against both buggy GDB and buggy compilers. */
10526
10527 retval = canon;
10528 }
10529 else
10530 {
10531 retval = physname;
10532 need_copy = 0;
10533 }
10534 }
10535 else
10536 retval = canon;
10537
10538 if (need_copy)
10539 retval = objfile->intern (retval);
10540
10541 return retval;
10542 }
10543
10544 /* Inspect DIE in CU for a namespace alias. If one exists, record
10545 a new symbol for it.
10546
10547 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10548
10549 static int
10550 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10551 {
10552 struct attribute *attr;
10553
10554 /* If the die does not have a name, this is not a namespace
10555 alias. */
10556 attr = dwarf2_attr (die, DW_AT_name, cu);
10557 if (attr != NULL)
10558 {
10559 int num;
10560 struct die_info *d = die;
10561 struct dwarf2_cu *imported_cu = cu;
10562
10563 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10564 keep inspecting DIEs until we hit the underlying import. */
10565 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10566 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10567 {
10568 attr = dwarf2_attr (d, DW_AT_import, cu);
10569 if (attr == NULL)
10570 break;
10571
10572 d = follow_die_ref (d, attr, &imported_cu);
10573 if (d->tag != DW_TAG_imported_declaration)
10574 break;
10575 }
10576
10577 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10578 {
10579 complaint (_("DIE at %s has too many recursively imported "
10580 "declarations"), sect_offset_str (d->sect_off));
10581 return 0;
10582 }
10583
10584 if (attr != NULL)
10585 {
10586 struct type *type;
10587 sect_offset sect_off = attr->get_ref_die_offset ();
10588
10589 type = get_die_type_at_offset (sect_off, cu->per_cu);
10590 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10591 {
10592 /* This declaration is a global namespace alias. Add
10593 a symbol for it whose type is the aliased namespace. */
10594 new_symbol (die, type, cu);
10595 return 1;
10596 }
10597 }
10598 }
10599
10600 return 0;
10601 }
10602
10603 /* Return the using directives repository (global or local?) to use in the
10604 current context for CU.
10605
10606 For Ada, imported declarations can materialize renamings, which *may* be
10607 global. However it is impossible (for now?) in DWARF to distinguish
10608 "external" imported declarations and "static" ones. As all imported
10609 declarations seem to be static in all other languages, make them all CU-wide
10610 global only in Ada. */
10611
10612 static struct using_direct **
10613 using_directives (struct dwarf2_cu *cu)
10614 {
10615 if (cu->language == language_ada
10616 && cu->get_builder ()->outermost_context_p ())
10617 return cu->get_builder ()->get_global_using_directives ();
10618 else
10619 return cu->get_builder ()->get_local_using_directives ();
10620 }
10621
10622 /* Read the import statement specified by the given die and record it. */
10623
10624 static void
10625 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10626 {
10627 struct objfile *objfile = cu->per_objfile->objfile;
10628 struct attribute *import_attr;
10629 struct die_info *imported_die, *child_die;
10630 struct dwarf2_cu *imported_cu;
10631 const char *imported_name;
10632 const char *imported_name_prefix;
10633 const char *canonical_name;
10634 const char *import_alias;
10635 const char *imported_declaration = NULL;
10636 const char *import_prefix;
10637 std::vector<const char *> excludes;
10638
10639 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10640 if (import_attr == NULL)
10641 {
10642 complaint (_("Tag '%s' has no DW_AT_import"),
10643 dwarf_tag_name (die->tag));
10644 return;
10645 }
10646
10647 imported_cu = cu;
10648 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10649 imported_name = dwarf2_name (imported_die, imported_cu);
10650 if (imported_name == NULL)
10651 {
10652 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10653
10654 The import in the following code:
10655 namespace A
10656 {
10657 typedef int B;
10658 }
10659
10660 int main ()
10661 {
10662 using A::B;
10663 B b;
10664 return b;
10665 }
10666
10667 ...
10668 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10669 <52> DW_AT_decl_file : 1
10670 <53> DW_AT_decl_line : 6
10671 <54> DW_AT_import : <0x75>
10672 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10673 <59> DW_AT_name : B
10674 <5b> DW_AT_decl_file : 1
10675 <5c> DW_AT_decl_line : 2
10676 <5d> DW_AT_type : <0x6e>
10677 ...
10678 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10679 <76> DW_AT_byte_size : 4
10680 <77> DW_AT_encoding : 5 (signed)
10681
10682 imports the wrong die ( 0x75 instead of 0x58 ).
10683 This case will be ignored until the gcc bug is fixed. */
10684 return;
10685 }
10686
10687 /* Figure out the local name after import. */
10688 import_alias = dwarf2_name (die, cu);
10689
10690 /* Figure out where the statement is being imported to. */
10691 import_prefix = determine_prefix (die, cu);
10692
10693 /* Figure out what the scope of the imported die is and prepend it
10694 to the name of the imported die. */
10695 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10696
10697 if (imported_die->tag != DW_TAG_namespace
10698 && imported_die->tag != DW_TAG_module)
10699 {
10700 imported_declaration = imported_name;
10701 canonical_name = imported_name_prefix;
10702 }
10703 else if (strlen (imported_name_prefix) > 0)
10704 canonical_name = obconcat (&objfile->objfile_obstack,
10705 imported_name_prefix,
10706 (cu->language == language_d ? "." : "::"),
10707 imported_name, (char *) NULL);
10708 else
10709 canonical_name = imported_name;
10710
10711 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10712 for (child_die = die->child; child_die && child_die->tag;
10713 child_die = child_die->sibling)
10714 {
10715 /* DWARF-4: A Fortran use statement with a “rename list” may be
10716 represented by an imported module entry with an import attribute
10717 referring to the module and owned entries corresponding to those
10718 entities that are renamed as part of being imported. */
10719
10720 if (child_die->tag != DW_TAG_imported_declaration)
10721 {
10722 complaint (_("child DW_TAG_imported_declaration expected "
10723 "- DIE at %s [in module %s]"),
10724 sect_offset_str (child_die->sect_off),
10725 objfile_name (objfile));
10726 continue;
10727 }
10728
10729 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10730 if (import_attr == NULL)
10731 {
10732 complaint (_("Tag '%s' has no DW_AT_import"),
10733 dwarf_tag_name (child_die->tag));
10734 continue;
10735 }
10736
10737 imported_cu = cu;
10738 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10739 &imported_cu);
10740 imported_name = dwarf2_name (imported_die, imported_cu);
10741 if (imported_name == NULL)
10742 {
10743 complaint (_("child DW_TAG_imported_declaration has unknown "
10744 "imported name - DIE at %s [in module %s]"),
10745 sect_offset_str (child_die->sect_off),
10746 objfile_name (objfile));
10747 continue;
10748 }
10749
10750 excludes.push_back (imported_name);
10751
10752 process_die (child_die, cu);
10753 }
10754
10755 add_using_directive (using_directives (cu),
10756 import_prefix,
10757 canonical_name,
10758 import_alias,
10759 imported_declaration,
10760 excludes,
10761 0,
10762 &objfile->objfile_obstack);
10763 }
10764
10765 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10766 types, but gives them a size of zero. Starting with version 14,
10767 ICC is compatible with GCC. */
10768
10769 static bool
10770 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10771 {
10772 if (!cu->checked_producer)
10773 check_producer (cu);
10774
10775 return cu->producer_is_icc_lt_14;
10776 }
10777
10778 /* ICC generates a DW_AT_type for C void functions. This was observed on
10779 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10780 which says that void functions should not have a DW_AT_type. */
10781
10782 static bool
10783 producer_is_icc (struct dwarf2_cu *cu)
10784 {
10785 if (!cu->checked_producer)
10786 check_producer (cu);
10787
10788 return cu->producer_is_icc;
10789 }
10790
10791 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10792 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10793 this, it was first present in GCC release 4.3.0. */
10794
10795 static bool
10796 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10797 {
10798 if (!cu->checked_producer)
10799 check_producer (cu);
10800
10801 return cu->producer_is_gcc_lt_4_3;
10802 }
10803
10804 static file_and_directory
10805 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10806 {
10807 file_and_directory res;
10808
10809 /* Find the filename. Do not use dwarf2_name here, since the filename
10810 is not a source language identifier. */
10811 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10812 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10813
10814 if (res.comp_dir == NULL
10815 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10816 && IS_ABSOLUTE_PATH (res.name))
10817 {
10818 res.comp_dir_storage = ldirname (res.name);
10819 if (!res.comp_dir_storage.empty ())
10820 res.comp_dir = res.comp_dir_storage.c_str ();
10821 }
10822 if (res.comp_dir != NULL)
10823 {
10824 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10825 directory, get rid of it. */
10826 const char *cp = strchr (res.comp_dir, ':');
10827
10828 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10829 res.comp_dir = cp + 1;
10830 }
10831
10832 if (res.name == NULL)
10833 res.name = "<unknown>";
10834
10835 return res;
10836 }
10837
10838 /* Handle DW_AT_stmt_list for a compilation unit.
10839 DIE is the DW_TAG_compile_unit die for CU.
10840 COMP_DIR is the compilation directory. LOWPC is passed to
10841 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10842
10843 static void
10844 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10845 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10846 {
10847 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10848 struct attribute *attr;
10849 struct line_header line_header_local;
10850 hashval_t line_header_local_hash;
10851 void **slot;
10852 int decode_mapping;
10853
10854 gdb_assert (! cu->per_cu->is_debug_types);
10855
10856 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10857 if (attr == NULL)
10858 return;
10859
10860 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10861
10862 /* The line header hash table is only created if needed (it exists to
10863 prevent redundant reading of the line table for partial_units).
10864 If we're given a partial_unit, we'll need it. If we're given a
10865 compile_unit, then use the line header hash table if it's already
10866 created, but don't create one just yet. */
10867
10868 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10869 && die->tag == DW_TAG_partial_unit)
10870 {
10871 dwarf2_per_objfile->per_bfd->line_header_hash
10872 .reset (htab_create_alloc (127, line_header_hash_voidp,
10873 line_header_eq_voidp,
10874 free_line_header_voidp,
10875 xcalloc, xfree));
10876 }
10877
10878 line_header_local.sect_off = line_offset;
10879 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10880 line_header_local_hash = line_header_hash (&line_header_local);
10881 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10882 {
10883 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10884 &line_header_local,
10885 line_header_local_hash, NO_INSERT);
10886
10887 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10888 is not present in *SLOT (since if there is something in *SLOT then
10889 it will be for a partial_unit). */
10890 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10891 {
10892 gdb_assert (*slot != NULL);
10893 cu->line_header = (struct line_header *) *slot;
10894 return;
10895 }
10896 }
10897
10898 /* dwarf_decode_line_header does not yet provide sufficient information.
10899 We always have to call also dwarf_decode_lines for it. */
10900 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10901 if (lh == NULL)
10902 return;
10903
10904 cu->line_header = lh.release ();
10905 cu->line_header_die_owner = die;
10906
10907 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10908 slot = NULL;
10909 else
10910 {
10911 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10912 &line_header_local,
10913 line_header_local_hash, INSERT);
10914 gdb_assert (slot != NULL);
10915 }
10916 if (slot != NULL && *slot == NULL)
10917 {
10918 /* This newly decoded line number information unit will be owned
10919 by line_header_hash hash table. */
10920 *slot = cu->line_header;
10921 cu->line_header_die_owner = NULL;
10922 }
10923 else
10924 {
10925 /* We cannot free any current entry in (*slot) as that struct line_header
10926 may be already used by multiple CUs. Create only temporary decoded
10927 line_header for this CU - it may happen at most once for each line
10928 number information unit. And if we're not using line_header_hash
10929 then this is what we want as well. */
10930 gdb_assert (die->tag != DW_TAG_partial_unit);
10931 }
10932 decode_mapping = (die->tag != DW_TAG_partial_unit);
10933 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10934 decode_mapping);
10935
10936 }
10937
10938 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10939
10940 static void
10941 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10942 {
10943 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10944 struct objfile *objfile = dwarf2_per_objfile->objfile;
10945 struct gdbarch *gdbarch = objfile->arch ();
10946 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10947 CORE_ADDR highpc = ((CORE_ADDR) 0);
10948 struct attribute *attr;
10949 struct die_info *child_die;
10950 CORE_ADDR baseaddr;
10951
10952 prepare_one_comp_unit (cu, die, cu->language);
10953 baseaddr = objfile->text_section_offset ();
10954
10955 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10956
10957 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10958 from finish_block. */
10959 if (lowpc == ((CORE_ADDR) -1))
10960 lowpc = highpc;
10961 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10962
10963 file_and_directory fnd = find_file_and_directory (die, cu);
10964
10965 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10966 standardised yet. As a workaround for the language detection we fall
10967 back to the DW_AT_producer string. */
10968 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10969 cu->language = language_opencl;
10970
10971 /* Similar hack for Go. */
10972 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10973 set_cu_language (DW_LANG_Go, cu);
10974
10975 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10976
10977 /* Decode line number information if present. We do this before
10978 processing child DIEs, so that the line header table is available
10979 for DW_AT_decl_file. */
10980 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10981
10982 /* Process all dies in compilation unit. */
10983 if (die->child != NULL)
10984 {
10985 child_die = die->child;
10986 while (child_die && child_die->tag)
10987 {
10988 process_die (child_die, cu);
10989 child_die = child_die->sibling;
10990 }
10991 }
10992
10993 /* Decode macro information, if present. Dwarf 2 macro information
10994 refers to information in the line number info statement program
10995 header, so we can only read it if we've read the header
10996 successfully. */
10997 attr = dwarf2_attr (die, DW_AT_macros, cu);
10998 if (attr == NULL)
10999 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11000 if (attr && cu->line_header)
11001 {
11002 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11003 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11004
11005 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11006 }
11007 else
11008 {
11009 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11010 if (attr && cu->line_header)
11011 {
11012 unsigned int macro_offset = DW_UNSND (attr);
11013
11014 dwarf_decode_macros (cu, macro_offset, 0);
11015 }
11016 }
11017 }
11018
11019 void
11020 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11021 {
11022 struct type_unit_group *tu_group;
11023 int first_time;
11024 struct attribute *attr;
11025 unsigned int i;
11026 struct signatured_type *sig_type;
11027
11028 gdb_assert (per_cu->is_debug_types);
11029 sig_type = (struct signatured_type *) per_cu;
11030
11031 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11032
11033 /* If we're using .gdb_index (includes -readnow) then
11034 per_cu->type_unit_group may not have been set up yet. */
11035 if (sig_type->type_unit_group == NULL)
11036 sig_type->type_unit_group = get_type_unit_group (this, attr);
11037 tu_group = sig_type->type_unit_group;
11038
11039 /* If we've already processed this stmt_list there's no real need to
11040 do it again, we could fake it and just recreate the part we need
11041 (file name,index -> symtab mapping). If data shows this optimization
11042 is useful we can do it then. */
11043 first_time = tu_group->compunit_symtab == NULL;
11044
11045 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11046 debug info. */
11047 line_header_up lh;
11048 if (attr != NULL)
11049 {
11050 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11051 lh = dwarf_decode_line_header (line_offset, this);
11052 }
11053 if (lh == NULL)
11054 {
11055 if (first_time)
11056 start_symtab ("", NULL, 0);
11057 else
11058 {
11059 gdb_assert (tu_group->symtabs == NULL);
11060 gdb_assert (m_builder == nullptr);
11061 struct compunit_symtab *cust = tu_group->compunit_symtab;
11062 m_builder.reset (new struct buildsym_compunit
11063 (COMPUNIT_OBJFILE (cust), "",
11064 COMPUNIT_DIRNAME (cust),
11065 compunit_language (cust),
11066 0, cust));
11067 list_in_scope = get_builder ()->get_file_symbols ();
11068 }
11069 return;
11070 }
11071
11072 line_header = lh.release ();
11073 line_header_die_owner = die;
11074
11075 if (first_time)
11076 {
11077 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11078
11079 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11080 still initializing it, and our caller (a few levels up)
11081 process_full_type_unit still needs to know if this is the first
11082 time. */
11083
11084 tu_group->symtabs
11085 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11086 struct symtab *, line_header->file_names_size ());
11087
11088 auto &file_names = line_header->file_names ();
11089 for (i = 0; i < file_names.size (); ++i)
11090 {
11091 file_entry &fe = file_names[i];
11092 dwarf2_start_subfile (this, fe.name,
11093 fe.include_dir (line_header));
11094 buildsym_compunit *b = get_builder ();
11095 if (b->get_current_subfile ()->symtab == NULL)
11096 {
11097 /* NOTE: start_subfile will recognize when it's been
11098 passed a file it has already seen. So we can't
11099 assume there's a simple mapping from
11100 cu->line_header->file_names to subfiles, plus
11101 cu->line_header->file_names may contain dups. */
11102 b->get_current_subfile ()->symtab
11103 = allocate_symtab (cust, b->get_current_subfile ()->name);
11104 }
11105
11106 fe.symtab = b->get_current_subfile ()->symtab;
11107 tu_group->symtabs[i] = fe.symtab;
11108 }
11109 }
11110 else
11111 {
11112 gdb_assert (m_builder == nullptr);
11113 struct compunit_symtab *cust = tu_group->compunit_symtab;
11114 m_builder.reset (new struct buildsym_compunit
11115 (COMPUNIT_OBJFILE (cust), "",
11116 COMPUNIT_DIRNAME (cust),
11117 compunit_language (cust),
11118 0, cust));
11119 list_in_scope = get_builder ()->get_file_symbols ();
11120
11121 auto &file_names = line_header->file_names ();
11122 for (i = 0; i < file_names.size (); ++i)
11123 {
11124 file_entry &fe = file_names[i];
11125 fe.symtab = tu_group->symtabs[i];
11126 }
11127 }
11128
11129 /* The main symtab is allocated last. Type units don't have DW_AT_name
11130 so they don't have a "real" (so to speak) symtab anyway.
11131 There is later code that will assign the main symtab to all symbols
11132 that don't have one. We need to handle the case of a symbol with a
11133 missing symtab (DW_AT_decl_file) anyway. */
11134 }
11135
11136 /* Process DW_TAG_type_unit.
11137 For TUs we want to skip the first top level sibling if it's not the
11138 actual type being defined by this TU. In this case the first top
11139 level sibling is there to provide context only. */
11140
11141 static void
11142 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11143 {
11144 struct die_info *child_die;
11145
11146 prepare_one_comp_unit (cu, die, language_minimal);
11147
11148 /* Initialize (or reinitialize) the machinery for building symtabs.
11149 We do this before processing child DIEs, so that the line header table
11150 is available for DW_AT_decl_file. */
11151 cu->setup_type_unit_groups (die);
11152
11153 if (die->child != NULL)
11154 {
11155 child_die = die->child;
11156 while (child_die && child_die->tag)
11157 {
11158 process_die (child_die, cu);
11159 child_die = child_die->sibling;
11160 }
11161 }
11162 }
11163 \f
11164 /* DWO/DWP files.
11165
11166 http://gcc.gnu.org/wiki/DebugFission
11167 http://gcc.gnu.org/wiki/DebugFissionDWP
11168
11169 To simplify handling of both DWO files ("object" files with the DWARF info)
11170 and DWP files (a file with the DWOs packaged up into one file), we treat
11171 DWP files as having a collection of virtual DWO files. */
11172
11173 static hashval_t
11174 hash_dwo_file (const void *item)
11175 {
11176 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11177 hashval_t hash;
11178
11179 hash = htab_hash_string (dwo_file->dwo_name);
11180 if (dwo_file->comp_dir != NULL)
11181 hash += htab_hash_string (dwo_file->comp_dir);
11182 return hash;
11183 }
11184
11185 static int
11186 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11187 {
11188 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11189 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11190
11191 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11192 return 0;
11193 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11194 return lhs->comp_dir == rhs->comp_dir;
11195 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11196 }
11197
11198 /* Allocate a hash table for DWO files. */
11199
11200 static htab_up
11201 allocate_dwo_file_hash_table ()
11202 {
11203 auto delete_dwo_file = [] (void *item)
11204 {
11205 struct dwo_file *dwo_file = (struct dwo_file *) item;
11206
11207 delete dwo_file;
11208 };
11209
11210 return htab_up (htab_create_alloc (41,
11211 hash_dwo_file,
11212 eq_dwo_file,
11213 delete_dwo_file,
11214 xcalloc, xfree));
11215 }
11216
11217 /* Lookup DWO file DWO_NAME. */
11218
11219 static void **
11220 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11221 const char *dwo_name,
11222 const char *comp_dir)
11223 {
11224 struct dwo_file find_entry;
11225 void **slot;
11226
11227 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11228 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11229
11230 find_entry.dwo_name = dwo_name;
11231 find_entry.comp_dir = comp_dir;
11232 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11233 INSERT);
11234
11235 return slot;
11236 }
11237
11238 static hashval_t
11239 hash_dwo_unit (const void *item)
11240 {
11241 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11242
11243 /* This drops the top 32 bits of the id, but is ok for a hash. */
11244 return dwo_unit->signature;
11245 }
11246
11247 static int
11248 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11249 {
11250 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11251 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11252
11253 /* The signature is assumed to be unique within the DWO file.
11254 So while object file CU dwo_id's always have the value zero,
11255 that's OK, assuming each object file DWO file has only one CU,
11256 and that's the rule for now. */
11257 return lhs->signature == rhs->signature;
11258 }
11259
11260 /* Allocate a hash table for DWO CUs,TUs.
11261 There is one of these tables for each of CUs,TUs for each DWO file. */
11262
11263 static htab_up
11264 allocate_dwo_unit_table ()
11265 {
11266 /* Start out with a pretty small number.
11267 Generally DWO files contain only one CU and maybe some TUs. */
11268 return htab_up (htab_create_alloc (3,
11269 hash_dwo_unit,
11270 eq_dwo_unit,
11271 NULL, xcalloc, xfree));
11272 }
11273
11274 /* die_reader_func for create_dwo_cu. */
11275
11276 static void
11277 create_dwo_cu_reader (const struct die_reader_specs *reader,
11278 const gdb_byte *info_ptr,
11279 struct die_info *comp_unit_die,
11280 struct dwo_file *dwo_file,
11281 struct dwo_unit *dwo_unit)
11282 {
11283 struct dwarf2_cu *cu = reader->cu;
11284 sect_offset sect_off = cu->per_cu->sect_off;
11285 struct dwarf2_section_info *section = cu->per_cu->section;
11286
11287 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11288 if (!signature.has_value ())
11289 {
11290 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11291 " its dwo_id [in module %s]"),
11292 sect_offset_str (sect_off), dwo_file->dwo_name);
11293 return;
11294 }
11295
11296 dwo_unit->dwo_file = dwo_file;
11297 dwo_unit->signature = *signature;
11298 dwo_unit->section = section;
11299 dwo_unit->sect_off = sect_off;
11300 dwo_unit->length = cu->per_cu->length;
11301
11302 if (dwarf_read_debug)
11303 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11304 sect_offset_str (sect_off),
11305 hex_string (dwo_unit->signature));
11306 }
11307
11308 /* Create the dwo_units for the CUs in a DWO_FILE.
11309 Note: This function processes DWO files only, not DWP files. */
11310
11311 static void
11312 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11313 dwarf2_cu *cu, struct dwo_file &dwo_file,
11314 dwarf2_section_info &section, htab_up &cus_htab)
11315 {
11316 struct objfile *objfile = dwarf2_per_objfile->objfile;
11317 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11318 const gdb_byte *info_ptr, *end_ptr;
11319
11320 section.read (objfile);
11321 info_ptr = section.buffer;
11322
11323 if (info_ptr == NULL)
11324 return;
11325
11326 if (dwarf_read_debug)
11327 {
11328 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11329 section.get_name (),
11330 section.get_file_name ());
11331 }
11332
11333 end_ptr = info_ptr + section.size;
11334 while (info_ptr < end_ptr)
11335 {
11336 struct dwarf2_per_cu_data per_cu;
11337 struct dwo_unit read_unit {};
11338 struct dwo_unit *dwo_unit;
11339 void **slot;
11340 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11341
11342 memset (&per_cu, 0, sizeof (per_cu));
11343 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
11344 per_cu.per_bfd = per_bfd;
11345 per_cu.is_debug_types = 0;
11346 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11347 per_cu.section = &section;
11348
11349 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11350 if (!reader.dummy_p)
11351 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11352 &dwo_file, &read_unit);
11353 info_ptr += per_cu.length;
11354
11355 // If the unit could not be parsed, skip it.
11356 if (read_unit.dwo_file == NULL)
11357 continue;
11358
11359 if (cus_htab == NULL)
11360 cus_htab = allocate_dwo_unit_table ();
11361
11362 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11363 struct dwo_unit);
11364 *dwo_unit = read_unit;
11365 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11366 gdb_assert (slot != NULL);
11367 if (*slot != NULL)
11368 {
11369 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11370 sect_offset dup_sect_off = dup_cu->sect_off;
11371
11372 complaint (_("debug cu entry at offset %s is duplicate to"
11373 " the entry at offset %s, signature %s"),
11374 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11375 hex_string (dwo_unit->signature));
11376 }
11377 *slot = (void *)dwo_unit;
11378 }
11379 }
11380
11381 /* DWP file .debug_{cu,tu}_index section format:
11382 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11383
11384 DWP Version 1:
11385
11386 Both index sections have the same format, and serve to map a 64-bit
11387 signature to a set of section numbers. Each section begins with a header,
11388 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11389 indexes, and a pool of 32-bit section numbers. The index sections will be
11390 aligned at 8-byte boundaries in the file.
11391
11392 The index section header consists of:
11393
11394 V, 32 bit version number
11395 -, 32 bits unused
11396 N, 32 bit number of compilation units or type units in the index
11397 M, 32 bit number of slots in the hash table
11398
11399 Numbers are recorded using the byte order of the application binary.
11400
11401 The hash table begins at offset 16 in the section, and consists of an array
11402 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11403 order of the application binary). Unused slots in the hash table are 0.
11404 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11405
11406 The parallel table begins immediately after the hash table
11407 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11408 array of 32-bit indexes (using the byte order of the application binary),
11409 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11410 table contains a 32-bit index into the pool of section numbers. For unused
11411 hash table slots, the corresponding entry in the parallel table will be 0.
11412
11413 The pool of section numbers begins immediately following the hash table
11414 (at offset 16 + 12 * M from the beginning of the section). The pool of
11415 section numbers consists of an array of 32-bit words (using the byte order
11416 of the application binary). Each item in the array is indexed starting
11417 from 0. The hash table entry provides the index of the first section
11418 number in the set. Additional section numbers in the set follow, and the
11419 set is terminated by a 0 entry (section number 0 is not used in ELF).
11420
11421 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11422 section must be the first entry in the set, and the .debug_abbrev.dwo must
11423 be the second entry. Other members of the set may follow in any order.
11424
11425 ---
11426
11427 DWP Version 2:
11428
11429 DWP Version 2 combines all the .debug_info, etc. sections into one,
11430 and the entries in the index tables are now offsets into these sections.
11431 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11432 section.
11433
11434 Index Section Contents:
11435 Header
11436 Hash Table of Signatures dwp_hash_table.hash_table
11437 Parallel Table of Indices dwp_hash_table.unit_table
11438 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11439 Table of Section Sizes dwp_hash_table.v2.sizes
11440
11441 The index section header consists of:
11442
11443 V, 32 bit version number
11444 L, 32 bit number of columns in the table of section offsets
11445 N, 32 bit number of compilation units or type units in the index
11446 M, 32 bit number of slots in the hash table
11447
11448 Numbers are recorded using the byte order of the application binary.
11449
11450 The hash table has the same format as version 1.
11451 The parallel table of indices has the same format as version 1,
11452 except that the entries are origin-1 indices into the table of sections
11453 offsets and the table of section sizes.
11454
11455 The table of offsets begins immediately following the parallel table
11456 (at offset 16 + 12 * M from the beginning of the section). The table is
11457 a two-dimensional array of 32-bit words (using the byte order of the
11458 application binary), with L columns and N+1 rows, in row-major order.
11459 Each row in the array is indexed starting from 0. The first row provides
11460 a key to the remaining rows: each column in this row provides an identifier
11461 for a debug section, and the offsets in the same column of subsequent rows
11462 refer to that section. The section identifiers are:
11463
11464 DW_SECT_INFO 1 .debug_info.dwo
11465 DW_SECT_TYPES 2 .debug_types.dwo
11466 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11467 DW_SECT_LINE 4 .debug_line.dwo
11468 DW_SECT_LOC 5 .debug_loc.dwo
11469 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11470 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11471 DW_SECT_MACRO 8 .debug_macro.dwo
11472
11473 The offsets provided by the CU and TU index sections are the base offsets
11474 for the contributions made by each CU or TU to the corresponding section
11475 in the package file. Each CU and TU header contains an abbrev_offset
11476 field, used to find the abbreviations table for that CU or TU within the
11477 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11478 be interpreted as relative to the base offset given in the index section.
11479 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11480 should be interpreted as relative to the base offset for .debug_line.dwo,
11481 and offsets into other debug sections obtained from DWARF attributes should
11482 also be interpreted as relative to the corresponding base offset.
11483
11484 The table of sizes begins immediately following the table of offsets.
11485 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11486 with L columns and N rows, in row-major order. Each row in the array is
11487 indexed starting from 1 (row 0 is shared by the two tables).
11488
11489 ---
11490
11491 Hash table lookup is handled the same in version 1 and 2:
11492
11493 We assume that N and M will not exceed 2^32 - 1.
11494 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11495
11496 Given a 64-bit compilation unit signature or a type signature S, an entry
11497 in the hash table is located as follows:
11498
11499 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11500 the low-order k bits all set to 1.
11501
11502 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11503
11504 3) If the hash table entry at index H matches the signature, use that
11505 entry. If the hash table entry at index H is unused (all zeroes),
11506 terminate the search: the signature is not present in the table.
11507
11508 4) Let H = (H + H') modulo M. Repeat at Step 3.
11509
11510 Because M > N and H' and M are relatively prime, the search is guaranteed
11511 to stop at an unused slot or find the match. */
11512
11513 /* Create a hash table to map DWO IDs to their CU/TU entry in
11514 .debug_{info,types}.dwo in DWP_FILE.
11515 Returns NULL if there isn't one.
11516 Note: This function processes DWP files only, not DWO files. */
11517
11518 static struct dwp_hash_table *
11519 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11520 struct dwp_file *dwp_file, int is_debug_types)
11521 {
11522 struct objfile *objfile = dwarf2_per_objfile->objfile;
11523 bfd *dbfd = dwp_file->dbfd.get ();
11524 const gdb_byte *index_ptr, *index_end;
11525 struct dwarf2_section_info *index;
11526 uint32_t version, nr_columns, nr_units, nr_slots;
11527 struct dwp_hash_table *htab;
11528
11529 if (is_debug_types)
11530 index = &dwp_file->sections.tu_index;
11531 else
11532 index = &dwp_file->sections.cu_index;
11533
11534 if (index->empty ())
11535 return NULL;
11536 index->read (objfile);
11537
11538 index_ptr = index->buffer;
11539 index_end = index_ptr + index->size;
11540
11541 version = read_4_bytes (dbfd, index_ptr);
11542 index_ptr += 4;
11543 if (version == 2)
11544 nr_columns = read_4_bytes (dbfd, index_ptr);
11545 else
11546 nr_columns = 0;
11547 index_ptr += 4;
11548 nr_units = read_4_bytes (dbfd, index_ptr);
11549 index_ptr += 4;
11550 nr_slots = read_4_bytes (dbfd, index_ptr);
11551 index_ptr += 4;
11552
11553 if (version != 1 && version != 2)
11554 {
11555 error (_("Dwarf Error: unsupported DWP file version (%s)"
11556 " [in module %s]"),
11557 pulongest (version), dwp_file->name);
11558 }
11559 if (nr_slots != (nr_slots & -nr_slots))
11560 {
11561 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11562 " is not power of 2 [in module %s]"),
11563 pulongest (nr_slots), dwp_file->name);
11564 }
11565
11566 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11567 htab->version = version;
11568 htab->nr_columns = nr_columns;
11569 htab->nr_units = nr_units;
11570 htab->nr_slots = nr_slots;
11571 htab->hash_table = index_ptr;
11572 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11573
11574 /* Exit early if the table is empty. */
11575 if (nr_slots == 0 || nr_units == 0
11576 || (version == 2 && nr_columns == 0))
11577 {
11578 /* All must be zero. */
11579 if (nr_slots != 0 || nr_units != 0
11580 || (version == 2 && nr_columns != 0))
11581 {
11582 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11583 " all zero [in modules %s]"),
11584 dwp_file->name);
11585 }
11586 return htab;
11587 }
11588
11589 if (version == 1)
11590 {
11591 htab->section_pool.v1.indices =
11592 htab->unit_table + sizeof (uint32_t) * nr_slots;
11593 /* It's harder to decide whether the section is too small in v1.
11594 V1 is deprecated anyway so we punt. */
11595 }
11596 else
11597 {
11598 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11599 int *ids = htab->section_pool.v2.section_ids;
11600 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11601 /* Reverse map for error checking. */
11602 int ids_seen[DW_SECT_MAX + 1];
11603 int i;
11604
11605 if (nr_columns < 2)
11606 {
11607 error (_("Dwarf Error: bad DWP hash table, too few columns"
11608 " in section table [in module %s]"),
11609 dwp_file->name);
11610 }
11611 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11612 {
11613 error (_("Dwarf Error: bad DWP hash table, too many columns"
11614 " in section table [in module %s]"),
11615 dwp_file->name);
11616 }
11617 memset (ids, 255, sizeof_ids);
11618 memset (ids_seen, 255, sizeof (ids_seen));
11619 for (i = 0; i < nr_columns; ++i)
11620 {
11621 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11622
11623 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11624 {
11625 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11626 " in section table [in module %s]"),
11627 id, dwp_file->name);
11628 }
11629 if (ids_seen[id] != -1)
11630 {
11631 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11632 " id %d in section table [in module %s]"),
11633 id, dwp_file->name);
11634 }
11635 ids_seen[id] = i;
11636 ids[i] = id;
11637 }
11638 /* Must have exactly one info or types section. */
11639 if (((ids_seen[DW_SECT_INFO] != -1)
11640 + (ids_seen[DW_SECT_TYPES] != -1))
11641 != 1)
11642 {
11643 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11644 " DWO info/types section [in module %s]"),
11645 dwp_file->name);
11646 }
11647 /* Must have an abbrev section. */
11648 if (ids_seen[DW_SECT_ABBREV] == -1)
11649 {
11650 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11651 " section [in module %s]"),
11652 dwp_file->name);
11653 }
11654 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11655 htab->section_pool.v2.sizes =
11656 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11657 * nr_units * nr_columns);
11658 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11659 * nr_units * nr_columns))
11660 > index_end)
11661 {
11662 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11663 " [in module %s]"),
11664 dwp_file->name);
11665 }
11666 }
11667
11668 return htab;
11669 }
11670
11671 /* Update SECTIONS with the data from SECTP.
11672
11673 This function is like the other "locate" section routines that are
11674 passed to bfd_map_over_sections, but in this context the sections to
11675 read comes from the DWP V1 hash table, not the full ELF section table.
11676
11677 The result is non-zero for success, or zero if an error was found. */
11678
11679 static int
11680 locate_v1_virtual_dwo_sections (asection *sectp,
11681 struct virtual_v1_dwo_sections *sections)
11682 {
11683 const struct dwop_section_names *names = &dwop_section_names;
11684
11685 if (section_is_p (sectp->name, &names->abbrev_dwo))
11686 {
11687 /* There can be only one. */
11688 if (sections->abbrev.s.section != NULL)
11689 return 0;
11690 sections->abbrev.s.section = sectp;
11691 sections->abbrev.size = bfd_section_size (sectp);
11692 }
11693 else if (section_is_p (sectp->name, &names->info_dwo)
11694 || section_is_p (sectp->name, &names->types_dwo))
11695 {
11696 /* There can be only one. */
11697 if (sections->info_or_types.s.section != NULL)
11698 return 0;
11699 sections->info_or_types.s.section = sectp;
11700 sections->info_or_types.size = bfd_section_size (sectp);
11701 }
11702 else if (section_is_p (sectp->name, &names->line_dwo))
11703 {
11704 /* There can be only one. */
11705 if (sections->line.s.section != NULL)
11706 return 0;
11707 sections->line.s.section = sectp;
11708 sections->line.size = bfd_section_size (sectp);
11709 }
11710 else if (section_is_p (sectp->name, &names->loc_dwo))
11711 {
11712 /* There can be only one. */
11713 if (sections->loc.s.section != NULL)
11714 return 0;
11715 sections->loc.s.section = sectp;
11716 sections->loc.size = bfd_section_size (sectp);
11717 }
11718 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11719 {
11720 /* There can be only one. */
11721 if (sections->macinfo.s.section != NULL)
11722 return 0;
11723 sections->macinfo.s.section = sectp;
11724 sections->macinfo.size = bfd_section_size (sectp);
11725 }
11726 else if (section_is_p (sectp->name, &names->macro_dwo))
11727 {
11728 /* There can be only one. */
11729 if (sections->macro.s.section != NULL)
11730 return 0;
11731 sections->macro.s.section = sectp;
11732 sections->macro.size = bfd_section_size (sectp);
11733 }
11734 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11735 {
11736 /* There can be only one. */
11737 if (sections->str_offsets.s.section != NULL)
11738 return 0;
11739 sections->str_offsets.s.section = sectp;
11740 sections->str_offsets.size = bfd_section_size (sectp);
11741 }
11742 else
11743 {
11744 /* No other kind of section is valid. */
11745 return 0;
11746 }
11747
11748 return 1;
11749 }
11750
11751 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11752 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11753 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11754 This is for DWP version 1 files. */
11755
11756 static struct dwo_unit *
11757 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11758 struct dwp_file *dwp_file,
11759 uint32_t unit_index,
11760 const char *comp_dir,
11761 ULONGEST signature, int is_debug_types)
11762 {
11763 const struct dwp_hash_table *dwp_htab =
11764 is_debug_types ? dwp_file->tus : dwp_file->cus;
11765 bfd *dbfd = dwp_file->dbfd.get ();
11766 const char *kind = is_debug_types ? "TU" : "CU";
11767 struct dwo_file *dwo_file;
11768 struct dwo_unit *dwo_unit;
11769 struct virtual_v1_dwo_sections sections;
11770 void **dwo_file_slot;
11771 int i;
11772
11773 gdb_assert (dwp_file->version == 1);
11774
11775 if (dwarf_read_debug)
11776 {
11777 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11778 kind,
11779 pulongest (unit_index), hex_string (signature),
11780 dwp_file->name);
11781 }
11782
11783 /* Fetch the sections of this DWO unit.
11784 Put a limit on the number of sections we look for so that bad data
11785 doesn't cause us to loop forever. */
11786
11787 #define MAX_NR_V1_DWO_SECTIONS \
11788 (1 /* .debug_info or .debug_types */ \
11789 + 1 /* .debug_abbrev */ \
11790 + 1 /* .debug_line */ \
11791 + 1 /* .debug_loc */ \
11792 + 1 /* .debug_str_offsets */ \
11793 + 1 /* .debug_macro or .debug_macinfo */ \
11794 + 1 /* trailing zero */)
11795
11796 memset (&sections, 0, sizeof (sections));
11797
11798 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11799 {
11800 asection *sectp;
11801 uint32_t section_nr =
11802 read_4_bytes (dbfd,
11803 dwp_htab->section_pool.v1.indices
11804 + (unit_index + i) * sizeof (uint32_t));
11805
11806 if (section_nr == 0)
11807 break;
11808 if (section_nr >= dwp_file->num_sections)
11809 {
11810 error (_("Dwarf Error: bad DWP hash table, section number too large"
11811 " [in module %s]"),
11812 dwp_file->name);
11813 }
11814
11815 sectp = dwp_file->elf_sections[section_nr];
11816 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11817 {
11818 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11819 " [in module %s]"),
11820 dwp_file->name);
11821 }
11822 }
11823
11824 if (i < 2
11825 || sections.info_or_types.empty ()
11826 || sections.abbrev.empty ())
11827 {
11828 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11829 " [in module %s]"),
11830 dwp_file->name);
11831 }
11832 if (i == MAX_NR_V1_DWO_SECTIONS)
11833 {
11834 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11835 " [in module %s]"),
11836 dwp_file->name);
11837 }
11838
11839 /* It's easier for the rest of the code if we fake a struct dwo_file and
11840 have dwo_unit "live" in that. At least for now.
11841
11842 The DWP file can be made up of a random collection of CUs and TUs.
11843 However, for each CU + set of TUs that came from the same original DWO
11844 file, we can combine them back into a virtual DWO file to save space
11845 (fewer struct dwo_file objects to allocate). Remember that for really
11846 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11847
11848 std::string virtual_dwo_name =
11849 string_printf ("virtual-dwo/%d-%d-%d-%d",
11850 sections.abbrev.get_id (),
11851 sections.line.get_id (),
11852 sections.loc.get_id (),
11853 sections.str_offsets.get_id ());
11854 /* Can we use an existing virtual DWO file? */
11855 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11856 virtual_dwo_name.c_str (),
11857 comp_dir);
11858 /* Create one if necessary. */
11859 if (*dwo_file_slot == NULL)
11860 {
11861 if (dwarf_read_debug)
11862 {
11863 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11864 virtual_dwo_name.c_str ());
11865 }
11866 dwo_file = new struct dwo_file;
11867 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11868 dwo_file->comp_dir = comp_dir;
11869 dwo_file->sections.abbrev = sections.abbrev;
11870 dwo_file->sections.line = sections.line;
11871 dwo_file->sections.loc = sections.loc;
11872 dwo_file->sections.macinfo = sections.macinfo;
11873 dwo_file->sections.macro = sections.macro;
11874 dwo_file->sections.str_offsets = sections.str_offsets;
11875 /* The "str" section is global to the entire DWP file. */
11876 dwo_file->sections.str = dwp_file->sections.str;
11877 /* The info or types section is assigned below to dwo_unit,
11878 there's no need to record it in dwo_file.
11879 Also, we can't simply record type sections in dwo_file because
11880 we record a pointer into the vector in dwo_unit. As we collect more
11881 types we'll grow the vector and eventually have to reallocate space
11882 for it, invalidating all copies of pointers into the previous
11883 contents. */
11884 *dwo_file_slot = dwo_file;
11885 }
11886 else
11887 {
11888 if (dwarf_read_debug)
11889 {
11890 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11891 virtual_dwo_name.c_str ());
11892 }
11893 dwo_file = (struct dwo_file *) *dwo_file_slot;
11894 }
11895
11896 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11897 dwo_unit->dwo_file = dwo_file;
11898 dwo_unit->signature = signature;
11899 dwo_unit->section =
11900 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11901 *dwo_unit->section = sections.info_or_types;
11902 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11903
11904 return dwo_unit;
11905 }
11906
11907 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11908 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11909 piece within that section used by a TU/CU, return a virtual section
11910 of just that piece. */
11911
11912 static struct dwarf2_section_info
11913 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11914 struct dwarf2_section_info *section,
11915 bfd_size_type offset, bfd_size_type size)
11916 {
11917 struct dwarf2_section_info result;
11918 asection *sectp;
11919
11920 gdb_assert (section != NULL);
11921 gdb_assert (!section->is_virtual);
11922
11923 memset (&result, 0, sizeof (result));
11924 result.s.containing_section = section;
11925 result.is_virtual = true;
11926
11927 if (size == 0)
11928 return result;
11929
11930 sectp = section->get_bfd_section ();
11931
11932 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11933 bounds of the real section. This is a pretty-rare event, so just
11934 flag an error (easier) instead of a warning and trying to cope. */
11935 if (sectp == NULL
11936 || offset + size > bfd_section_size (sectp))
11937 {
11938 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11939 " in section %s [in module %s]"),
11940 sectp ? bfd_section_name (sectp) : "<unknown>",
11941 objfile_name (dwarf2_per_objfile->objfile));
11942 }
11943
11944 result.virtual_offset = offset;
11945 result.size = size;
11946 return result;
11947 }
11948
11949 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11950 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11951 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11952 This is for DWP version 2 files. */
11953
11954 static struct dwo_unit *
11955 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11956 struct dwp_file *dwp_file,
11957 uint32_t unit_index,
11958 const char *comp_dir,
11959 ULONGEST signature, int is_debug_types)
11960 {
11961 const struct dwp_hash_table *dwp_htab =
11962 is_debug_types ? dwp_file->tus : dwp_file->cus;
11963 bfd *dbfd = dwp_file->dbfd.get ();
11964 const char *kind = is_debug_types ? "TU" : "CU";
11965 struct dwo_file *dwo_file;
11966 struct dwo_unit *dwo_unit;
11967 struct virtual_v2_dwo_sections sections;
11968 void **dwo_file_slot;
11969 int i;
11970
11971 gdb_assert (dwp_file->version == 2);
11972
11973 if (dwarf_read_debug)
11974 {
11975 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11976 kind,
11977 pulongest (unit_index), hex_string (signature),
11978 dwp_file->name);
11979 }
11980
11981 /* Fetch the section offsets of this DWO unit. */
11982
11983 memset (&sections, 0, sizeof (sections));
11984
11985 for (i = 0; i < dwp_htab->nr_columns; ++i)
11986 {
11987 uint32_t offset = read_4_bytes (dbfd,
11988 dwp_htab->section_pool.v2.offsets
11989 + (((unit_index - 1) * dwp_htab->nr_columns
11990 + i)
11991 * sizeof (uint32_t)));
11992 uint32_t size = read_4_bytes (dbfd,
11993 dwp_htab->section_pool.v2.sizes
11994 + (((unit_index - 1) * dwp_htab->nr_columns
11995 + i)
11996 * sizeof (uint32_t)));
11997
11998 switch (dwp_htab->section_pool.v2.section_ids[i])
11999 {
12000 case DW_SECT_INFO:
12001 case DW_SECT_TYPES:
12002 sections.info_or_types_offset = offset;
12003 sections.info_or_types_size = size;
12004 break;
12005 case DW_SECT_ABBREV:
12006 sections.abbrev_offset = offset;
12007 sections.abbrev_size = size;
12008 break;
12009 case DW_SECT_LINE:
12010 sections.line_offset = offset;
12011 sections.line_size = size;
12012 break;
12013 case DW_SECT_LOC:
12014 sections.loc_offset = offset;
12015 sections.loc_size = size;
12016 break;
12017 case DW_SECT_STR_OFFSETS:
12018 sections.str_offsets_offset = offset;
12019 sections.str_offsets_size = size;
12020 break;
12021 case DW_SECT_MACINFO:
12022 sections.macinfo_offset = offset;
12023 sections.macinfo_size = size;
12024 break;
12025 case DW_SECT_MACRO:
12026 sections.macro_offset = offset;
12027 sections.macro_size = size;
12028 break;
12029 }
12030 }
12031
12032 /* It's easier for the rest of the code if we fake a struct dwo_file and
12033 have dwo_unit "live" in that. At least for now.
12034
12035 The DWP file can be made up of a random collection of CUs and TUs.
12036 However, for each CU + set of TUs that came from the same original DWO
12037 file, we can combine them back into a virtual DWO file to save space
12038 (fewer struct dwo_file objects to allocate). Remember that for really
12039 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12040
12041 std::string virtual_dwo_name =
12042 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12043 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12044 (long) (sections.line_size ? sections.line_offset : 0),
12045 (long) (sections.loc_size ? sections.loc_offset : 0),
12046 (long) (sections.str_offsets_size
12047 ? sections.str_offsets_offset : 0));
12048 /* Can we use an existing virtual DWO file? */
12049 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12050 virtual_dwo_name.c_str (),
12051 comp_dir);
12052 /* Create one if necessary. */
12053 if (*dwo_file_slot == NULL)
12054 {
12055 if (dwarf_read_debug)
12056 {
12057 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12058 virtual_dwo_name.c_str ());
12059 }
12060 dwo_file = new struct dwo_file;
12061 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12062 dwo_file->comp_dir = comp_dir;
12063 dwo_file->sections.abbrev =
12064 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12065 sections.abbrev_offset, sections.abbrev_size);
12066 dwo_file->sections.line =
12067 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12068 sections.line_offset, sections.line_size);
12069 dwo_file->sections.loc =
12070 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12071 sections.loc_offset, sections.loc_size);
12072 dwo_file->sections.macinfo =
12073 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12074 sections.macinfo_offset, sections.macinfo_size);
12075 dwo_file->sections.macro =
12076 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12077 sections.macro_offset, sections.macro_size);
12078 dwo_file->sections.str_offsets =
12079 create_dwp_v2_section (dwarf2_per_objfile,
12080 &dwp_file->sections.str_offsets,
12081 sections.str_offsets_offset,
12082 sections.str_offsets_size);
12083 /* The "str" section is global to the entire DWP file. */
12084 dwo_file->sections.str = dwp_file->sections.str;
12085 /* The info or types section is assigned below to dwo_unit,
12086 there's no need to record it in dwo_file.
12087 Also, we can't simply record type sections in dwo_file because
12088 we record a pointer into the vector in dwo_unit. As we collect more
12089 types we'll grow the vector and eventually have to reallocate space
12090 for it, invalidating all copies of pointers into the previous
12091 contents. */
12092 *dwo_file_slot = dwo_file;
12093 }
12094 else
12095 {
12096 if (dwarf_read_debug)
12097 {
12098 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12099 virtual_dwo_name.c_str ());
12100 }
12101 dwo_file = (struct dwo_file *) *dwo_file_slot;
12102 }
12103
12104 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12105 dwo_unit->dwo_file = dwo_file;
12106 dwo_unit->signature = signature;
12107 dwo_unit->section =
12108 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12109 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12110 is_debug_types
12111 ? &dwp_file->sections.types
12112 : &dwp_file->sections.info,
12113 sections.info_or_types_offset,
12114 sections.info_or_types_size);
12115 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12116
12117 return dwo_unit;
12118 }
12119
12120 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12121 Returns NULL if the signature isn't found. */
12122
12123 static struct dwo_unit *
12124 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12125 struct dwp_file *dwp_file, const char *comp_dir,
12126 ULONGEST signature, int is_debug_types)
12127 {
12128 const struct dwp_hash_table *dwp_htab =
12129 is_debug_types ? dwp_file->tus : dwp_file->cus;
12130 bfd *dbfd = dwp_file->dbfd.get ();
12131 uint32_t mask = dwp_htab->nr_slots - 1;
12132 uint32_t hash = signature & mask;
12133 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12134 unsigned int i;
12135 void **slot;
12136 struct dwo_unit find_dwo_cu;
12137
12138 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12139 find_dwo_cu.signature = signature;
12140 slot = htab_find_slot (is_debug_types
12141 ? dwp_file->loaded_tus.get ()
12142 : dwp_file->loaded_cus.get (),
12143 &find_dwo_cu, INSERT);
12144
12145 if (*slot != NULL)
12146 return (struct dwo_unit *) *slot;
12147
12148 /* Use a for loop so that we don't loop forever on bad debug info. */
12149 for (i = 0; i < dwp_htab->nr_slots; ++i)
12150 {
12151 ULONGEST signature_in_table;
12152
12153 signature_in_table =
12154 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12155 if (signature_in_table == signature)
12156 {
12157 uint32_t unit_index =
12158 read_4_bytes (dbfd,
12159 dwp_htab->unit_table + hash * sizeof (uint32_t));
12160
12161 if (dwp_file->version == 1)
12162 {
12163 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12164 dwp_file, unit_index,
12165 comp_dir, signature,
12166 is_debug_types);
12167 }
12168 else
12169 {
12170 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12171 dwp_file, unit_index,
12172 comp_dir, signature,
12173 is_debug_types);
12174 }
12175 return (struct dwo_unit *) *slot;
12176 }
12177 if (signature_in_table == 0)
12178 return NULL;
12179 hash = (hash + hash2) & mask;
12180 }
12181
12182 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12183 " [in module %s]"),
12184 dwp_file->name);
12185 }
12186
12187 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12188 Open the file specified by FILE_NAME and hand it off to BFD for
12189 preliminary analysis. Return a newly initialized bfd *, which
12190 includes a canonicalized copy of FILE_NAME.
12191 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12192 SEARCH_CWD is true if the current directory is to be searched.
12193 It will be searched before debug-file-directory.
12194 If successful, the file is added to the bfd include table of the
12195 objfile's bfd (see gdb_bfd_record_inclusion).
12196 If unable to find/open the file, return NULL.
12197 NOTE: This function is derived from symfile_bfd_open. */
12198
12199 static gdb_bfd_ref_ptr
12200 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12201 const char *file_name, int is_dwp, int search_cwd)
12202 {
12203 int desc;
12204 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12205 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12206 to debug_file_directory. */
12207 const char *search_path;
12208 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12209
12210 gdb::unique_xmalloc_ptr<char> search_path_holder;
12211 if (search_cwd)
12212 {
12213 if (*debug_file_directory != '\0')
12214 {
12215 search_path_holder.reset (concat (".", dirname_separator_string,
12216 debug_file_directory,
12217 (char *) NULL));
12218 search_path = search_path_holder.get ();
12219 }
12220 else
12221 search_path = ".";
12222 }
12223 else
12224 search_path = debug_file_directory;
12225
12226 openp_flags flags = OPF_RETURN_REALPATH;
12227 if (is_dwp)
12228 flags |= OPF_SEARCH_IN_PATH;
12229
12230 gdb::unique_xmalloc_ptr<char> absolute_name;
12231 desc = openp (search_path, flags, file_name,
12232 O_RDONLY | O_BINARY, &absolute_name);
12233 if (desc < 0)
12234 return NULL;
12235
12236 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12237 gnutarget, desc));
12238 if (sym_bfd == NULL)
12239 return NULL;
12240 bfd_set_cacheable (sym_bfd.get (), 1);
12241
12242 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12243 return NULL;
12244
12245 /* Success. Record the bfd as having been included by the objfile's bfd.
12246 This is important because things like demangled_names_hash lives in the
12247 objfile's per_bfd space and may have references to things like symbol
12248 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12249 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12250
12251 return sym_bfd;
12252 }
12253
12254 /* Try to open DWO file FILE_NAME.
12255 COMP_DIR is the DW_AT_comp_dir attribute.
12256 The result is the bfd handle of the file.
12257 If there is a problem finding or opening the file, return NULL.
12258 Upon success, the canonicalized path of the file is stored in the bfd,
12259 same as symfile_bfd_open. */
12260
12261 static gdb_bfd_ref_ptr
12262 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12263 const char *file_name, const char *comp_dir)
12264 {
12265 if (IS_ABSOLUTE_PATH (file_name))
12266 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12267 0 /*is_dwp*/, 0 /*search_cwd*/);
12268
12269 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12270
12271 if (comp_dir != NULL)
12272 {
12273 gdb::unique_xmalloc_ptr<char> path_to_try
12274 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12275
12276 /* NOTE: If comp_dir is a relative path, this will also try the
12277 search path, which seems useful. */
12278 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12279 path_to_try.get (),
12280 0 /*is_dwp*/,
12281 1 /*search_cwd*/));
12282 if (abfd != NULL)
12283 return abfd;
12284 }
12285
12286 /* That didn't work, try debug-file-directory, which, despite its name,
12287 is a list of paths. */
12288
12289 if (*debug_file_directory == '\0')
12290 return NULL;
12291
12292 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12293 0 /*is_dwp*/, 1 /*search_cwd*/);
12294 }
12295
12296 /* This function is mapped across the sections and remembers the offset and
12297 size of each of the DWO debugging sections we are interested in. */
12298
12299 static void
12300 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12301 {
12302 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12303 const struct dwop_section_names *names = &dwop_section_names;
12304
12305 if (section_is_p (sectp->name, &names->abbrev_dwo))
12306 {
12307 dwo_sections->abbrev.s.section = sectp;
12308 dwo_sections->abbrev.size = bfd_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->info_dwo))
12311 {
12312 dwo_sections->info.s.section = sectp;
12313 dwo_sections->info.size = bfd_section_size (sectp);
12314 }
12315 else if (section_is_p (sectp->name, &names->line_dwo))
12316 {
12317 dwo_sections->line.s.section = sectp;
12318 dwo_sections->line.size = bfd_section_size (sectp);
12319 }
12320 else if (section_is_p (sectp->name, &names->loc_dwo))
12321 {
12322 dwo_sections->loc.s.section = sectp;
12323 dwo_sections->loc.size = bfd_section_size (sectp);
12324 }
12325 else if (section_is_p (sectp->name, &names->loclists_dwo))
12326 {
12327 dwo_sections->loclists.s.section = sectp;
12328 dwo_sections->loclists.size = bfd_section_size (sectp);
12329 }
12330 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12331 {
12332 dwo_sections->macinfo.s.section = sectp;
12333 dwo_sections->macinfo.size = bfd_section_size (sectp);
12334 }
12335 else if (section_is_p (sectp->name, &names->macro_dwo))
12336 {
12337 dwo_sections->macro.s.section = sectp;
12338 dwo_sections->macro.size = bfd_section_size (sectp);
12339 }
12340 else if (section_is_p (sectp->name, &names->str_dwo))
12341 {
12342 dwo_sections->str.s.section = sectp;
12343 dwo_sections->str.size = bfd_section_size (sectp);
12344 }
12345 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12346 {
12347 dwo_sections->str_offsets.s.section = sectp;
12348 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12349 }
12350 else if (section_is_p (sectp->name, &names->types_dwo))
12351 {
12352 struct dwarf2_section_info type_section;
12353
12354 memset (&type_section, 0, sizeof (type_section));
12355 type_section.s.section = sectp;
12356 type_section.size = bfd_section_size (sectp);
12357 dwo_sections->types.push_back (type_section);
12358 }
12359 }
12360
12361 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12362 by PER_CU. This is for the non-DWP case.
12363 The result is NULL if DWO_NAME can't be found. */
12364
12365 static struct dwo_file *
12366 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12367 const char *dwo_name, const char *comp_dir)
12368 {
12369 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
12370
12371 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12372 if (dbfd == NULL)
12373 {
12374 if (dwarf_read_debug)
12375 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12376 return NULL;
12377 }
12378
12379 dwo_file_up dwo_file (new struct dwo_file);
12380 dwo_file->dwo_name = dwo_name;
12381 dwo_file->comp_dir = comp_dir;
12382 dwo_file->dbfd = std::move (dbfd);
12383
12384 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12385 &dwo_file->sections);
12386
12387 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12388 dwo_file->sections.info, dwo_file->cus);
12389
12390 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12391 dwo_file->sections.types, dwo_file->tus);
12392
12393 if (dwarf_read_debug)
12394 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12395
12396 return dwo_file.release ();
12397 }
12398
12399 /* This function is mapped across the sections and remembers the offset and
12400 size of each of the DWP debugging sections common to version 1 and 2 that
12401 we are interested in. */
12402
12403 static void
12404 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12405 void *dwp_file_ptr)
12406 {
12407 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12408 const struct dwop_section_names *names = &dwop_section_names;
12409 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12410
12411 /* Record the ELF section number for later lookup: this is what the
12412 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12413 gdb_assert (elf_section_nr < dwp_file->num_sections);
12414 dwp_file->elf_sections[elf_section_nr] = sectp;
12415
12416 /* Look for specific sections that we need. */
12417 if (section_is_p (sectp->name, &names->str_dwo))
12418 {
12419 dwp_file->sections.str.s.section = sectp;
12420 dwp_file->sections.str.size = bfd_section_size (sectp);
12421 }
12422 else if (section_is_p (sectp->name, &names->cu_index))
12423 {
12424 dwp_file->sections.cu_index.s.section = sectp;
12425 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12426 }
12427 else if (section_is_p (sectp->name, &names->tu_index))
12428 {
12429 dwp_file->sections.tu_index.s.section = sectp;
12430 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12431 }
12432 }
12433
12434 /* This function is mapped across the sections and remembers the offset and
12435 size of each of the DWP version 2 debugging sections that we are interested
12436 in. This is split into a separate function because we don't know if we
12437 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12438
12439 static void
12440 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12441 {
12442 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12443 const struct dwop_section_names *names = &dwop_section_names;
12444 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12445
12446 /* Record the ELF section number for later lookup: this is what the
12447 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12448 gdb_assert (elf_section_nr < dwp_file->num_sections);
12449 dwp_file->elf_sections[elf_section_nr] = sectp;
12450
12451 /* Look for specific sections that we need. */
12452 if (section_is_p (sectp->name, &names->abbrev_dwo))
12453 {
12454 dwp_file->sections.abbrev.s.section = sectp;
12455 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12456 }
12457 else if (section_is_p (sectp->name, &names->info_dwo))
12458 {
12459 dwp_file->sections.info.s.section = sectp;
12460 dwp_file->sections.info.size = bfd_section_size (sectp);
12461 }
12462 else if (section_is_p (sectp->name, &names->line_dwo))
12463 {
12464 dwp_file->sections.line.s.section = sectp;
12465 dwp_file->sections.line.size = bfd_section_size (sectp);
12466 }
12467 else if (section_is_p (sectp->name, &names->loc_dwo))
12468 {
12469 dwp_file->sections.loc.s.section = sectp;
12470 dwp_file->sections.loc.size = bfd_section_size (sectp);
12471 }
12472 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12473 {
12474 dwp_file->sections.macinfo.s.section = sectp;
12475 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12476 }
12477 else if (section_is_p (sectp->name, &names->macro_dwo))
12478 {
12479 dwp_file->sections.macro.s.section = sectp;
12480 dwp_file->sections.macro.size = bfd_section_size (sectp);
12481 }
12482 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12483 {
12484 dwp_file->sections.str_offsets.s.section = sectp;
12485 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12486 }
12487 else if (section_is_p (sectp->name, &names->types_dwo))
12488 {
12489 dwp_file->sections.types.s.section = sectp;
12490 dwp_file->sections.types.size = bfd_section_size (sectp);
12491 }
12492 }
12493
12494 /* Hash function for dwp_file loaded CUs/TUs. */
12495
12496 static hashval_t
12497 hash_dwp_loaded_cutus (const void *item)
12498 {
12499 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12500
12501 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12502 return dwo_unit->signature;
12503 }
12504
12505 /* Equality function for dwp_file loaded CUs/TUs. */
12506
12507 static int
12508 eq_dwp_loaded_cutus (const void *a, const void *b)
12509 {
12510 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12511 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12512
12513 return dua->signature == dub->signature;
12514 }
12515
12516 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12517
12518 static htab_up
12519 allocate_dwp_loaded_cutus_table ()
12520 {
12521 return htab_up (htab_create_alloc (3,
12522 hash_dwp_loaded_cutus,
12523 eq_dwp_loaded_cutus,
12524 NULL, xcalloc, xfree));
12525 }
12526
12527 /* Try to open DWP file FILE_NAME.
12528 The result is the bfd handle of the file.
12529 If there is a problem finding or opening the file, return NULL.
12530 Upon success, the canonicalized path of the file is stored in the bfd,
12531 same as symfile_bfd_open. */
12532
12533 static gdb_bfd_ref_ptr
12534 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12535 const char *file_name)
12536 {
12537 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12538 1 /*is_dwp*/,
12539 1 /*search_cwd*/));
12540 if (abfd != NULL)
12541 return abfd;
12542
12543 /* Work around upstream bug 15652.
12544 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12545 [Whether that's a "bug" is debatable, but it is getting in our way.]
12546 We have no real idea where the dwp file is, because gdb's realpath-ing
12547 of the executable's path may have discarded the needed info.
12548 [IWBN if the dwp file name was recorded in the executable, akin to
12549 .gnu_debuglink, but that doesn't exist yet.]
12550 Strip the directory from FILE_NAME and search again. */
12551 if (*debug_file_directory != '\0')
12552 {
12553 /* Don't implicitly search the current directory here.
12554 If the user wants to search "." to handle this case,
12555 it must be added to debug-file-directory. */
12556 return try_open_dwop_file (dwarf2_per_objfile,
12557 lbasename (file_name), 1 /*is_dwp*/,
12558 0 /*search_cwd*/);
12559 }
12560
12561 return NULL;
12562 }
12563
12564 /* Initialize the use of the DWP file for the current objfile.
12565 By convention the name of the DWP file is ${objfile}.dwp.
12566 The result is NULL if it can't be found. */
12567
12568 static std::unique_ptr<struct dwp_file>
12569 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12570 {
12571 struct objfile *objfile = dwarf2_per_objfile->objfile;
12572
12573 /* Try to find first .dwp for the binary file before any symbolic links
12574 resolving. */
12575
12576 /* If the objfile is a debug file, find the name of the real binary
12577 file and get the name of dwp file from there. */
12578 std::string dwp_name;
12579 if (objfile->separate_debug_objfile_backlink != NULL)
12580 {
12581 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12582 const char *backlink_basename = lbasename (backlink->original_name);
12583
12584 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12585 }
12586 else
12587 dwp_name = objfile->original_name;
12588
12589 dwp_name += ".dwp";
12590
12591 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12592 if (dbfd == NULL
12593 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12594 {
12595 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12596 dwp_name = objfile_name (objfile);
12597 dwp_name += ".dwp";
12598 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12599 }
12600
12601 if (dbfd == NULL)
12602 {
12603 if (dwarf_read_debug)
12604 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12605 return std::unique_ptr<dwp_file> ();
12606 }
12607
12608 const char *name = bfd_get_filename (dbfd.get ());
12609 std::unique_ptr<struct dwp_file> dwp_file
12610 (new struct dwp_file (name, std::move (dbfd)));
12611
12612 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12613 dwp_file->elf_sections =
12614 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12615 dwp_file->num_sections, asection *);
12616
12617 bfd_map_over_sections (dwp_file->dbfd.get (),
12618 dwarf2_locate_common_dwp_sections,
12619 dwp_file.get ());
12620
12621 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12622 0);
12623
12624 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12625 1);
12626
12627 /* The DWP file version is stored in the hash table. Oh well. */
12628 if (dwp_file->cus && dwp_file->tus
12629 && dwp_file->cus->version != dwp_file->tus->version)
12630 {
12631 /* Technically speaking, we should try to limp along, but this is
12632 pretty bizarre. We use pulongest here because that's the established
12633 portability solution (e.g, we cannot use %u for uint32_t). */
12634 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12635 " TU version %s [in DWP file %s]"),
12636 pulongest (dwp_file->cus->version),
12637 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12638 }
12639
12640 if (dwp_file->cus)
12641 dwp_file->version = dwp_file->cus->version;
12642 else if (dwp_file->tus)
12643 dwp_file->version = dwp_file->tus->version;
12644 else
12645 dwp_file->version = 2;
12646
12647 if (dwp_file->version == 2)
12648 bfd_map_over_sections (dwp_file->dbfd.get (),
12649 dwarf2_locate_v2_dwp_sections,
12650 dwp_file.get ());
12651
12652 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12653 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12654
12655 if (dwarf_read_debug)
12656 {
12657 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12658 fprintf_unfiltered (gdb_stdlog,
12659 " %s CUs, %s TUs\n",
12660 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12661 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12662 }
12663
12664 return dwp_file;
12665 }
12666
12667 /* Wrapper around open_and_init_dwp_file, only open it once. */
12668
12669 static struct dwp_file *
12670 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12671 {
12672 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12673 {
12674 dwarf2_per_objfile->per_bfd->dwp_file
12675 = open_and_init_dwp_file (dwarf2_per_objfile);
12676 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12677 }
12678 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12679 }
12680
12681 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12682 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12683 or in the DWP file for the objfile, referenced by THIS_UNIT.
12684 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12685 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12686
12687 This is called, for example, when wanting to read a variable with a
12688 complex location. Therefore we don't want to do file i/o for every call.
12689 Therefore we don't want to look for a DWO file on every call.
12690 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12691 then we check if we've already seen DWO_NAME, and only THEN do we check
12692 for a DWO file.
12693
12694 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12695 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12696
12697 static struct dwo_unit *
12698 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12699 const char *dwo_name, const char *comp_dir,
12700 ULONGEST signature, int is_debug_types)
12701 {
12702 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
12703 struct objfile *objfile = dwarf2_per_objfile->objfile;
12704 const char *kind = is_debug_types ? "TU" : "CU";
12705 void **dwo_file_slot;
12706 struct dwo_file *dwo_file;
12707 struct dwp_file *dwp_file;
12708
12709 /* First see if there's a DWP file.
12710 If we have a DWP file but didn't find the DWO inside it, don't
12711 look for the original DWO file. It makes gdb behave differently
12712 depending on whether one is debugging in the build tree. */
12713
12714 dwp_file = get_dwp_file (dwarf2_per_objfile);
12715 if (dwp_file != NULL)
12716 {
12717 const struct dwp_hash_table *dwp_htab =
12718 is_debug_types ? dwp_file->tus : dwp_file->cus;
12719
12720 if (dwp_htab != NULL)
12721 {
12722 struct dwo_unit *dwo_cutu =
12723 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12724 signature, is_debug_types);
12725
12726 if (dwo_cutu != NULL)
12727 {
12728 if (dwarf_read_debug)
12729 {
12730 fprintf_unfiltered (gdb_stdlog,
12731 "Virtual DWO %s %s found: @%s\n",
12732 kind, hex_string (signature),
12733 host_address_to_string (dwo_cutu));
12734 }
12735 return dwo_cutu;
12736 }
12737 }
12738 }
12739 else
12740 {
12741 /* No DWP file, look for the DWO file. */
12742
12743 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12744 dwo_name, comp_dir);
12745 if (*dwo_file_slot == NULL)
12746 {
12747 /* Read in the file and build a table of the CUs/TUs it contains. */
12748 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
12749 }
12750 /* NOTE: This will be NULL if unable to open the file. */
12751 dwo_file = (struct dwo_file *) *dwo_file_slot;
12752
12753 if (dwo_file != NULL)
12754 {
12755 struct dwo_unit *dwo_cutu = NULL;
12756
12757 if (is_debug_types && dwo_file->tus)
12758 {
12759 struct dwo_unit find_dwo_cutu;
12760
12761 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12762 find_dwo_cutu.signature = signature;
12763 dwo_cutu
12764 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12765 &find_dwo_cutu);
12766 }
12767 else if (!is_debug_types && dwo_file->cus)
12768 {
12769 struct dwo_unit find_dwo_cutu;
12770
12771 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12772 find_dwo_cutu.signature = signature;
12773 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12774 &find_dwo_cutu);
12775 }
12776
12777 if (dwo_cutu != NULL)
12778 {
12779 if (dwarf_read_debug)
12780 {
12781 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12782 kind, dwo_name, hex_string (signature),
12783 host_address_to_string (dwo_cutu));
12784 }
12785 return dwo_cutu;
12786 }
12787 }
12788 }
12789
12790 /* We didn't find it. This could mean a dwo_id mismatch, or
12791 someone deleted the DWO/DWP file, or the search path isn't set up
12792 correctly to find the file. */
12793
12794 if (dwarf_read_debug)
12795 {
12796 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12797 kind, dwo_name, hex_string (signature));
12798 }
12799
12800 /* This is a warning and not a complaint because it can be caused by
12801 pilot error (e.g., user accidentally deleting the DWO). */
12802 {
12803 /* Print the name of the DWP file if we looked there, helps the user
12804 better diagnose the problem. */
12805 std::string dwp_text;
12806
12807 if (dwp_file != NULL)
12808 dwp_text = string_printf (" [in DWP file %s]",
12809 lbasename (dwp_file->name));
12810
12811 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12812 " [in module %s]"),
12813 kind, dwo_name, hex_string (signature),
12814 dwp_text.c_str (),
12815 this_unit->is_debug_types ? "TU" : "CU",
12816 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
12817 }
12818 return NULL;
12819 }
12820
12821 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12822 See lookup_dwo_cutu_unit for details. */
12823
12824 static struct dwo_unit *
12825 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12826 const char *dwo_name, const char *comp_dir,
12827 ULONGEST signature)
12828 {
12829 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12830 }
12831
12832 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12833 See lookup_dwo_cutu_unit for details. */
12834
12835 static struct dwo_unit *
12836 lookup_dwo_type_unit (struct signatured_type *this_tu,
12837 const char *dwo_name, const char *comp_dir)
12838 {
12839 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12840 }
12841
12842 /* Traversal function for queue_and_load_all_dwo_tus. */
12843
12844 static int
12845 queue_and_load_dwo_tu (void **slot, void *info)
12846 {
12847 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12848 dwarf2_cu *cu = (dwarf2_cu *) info;
12849 ULONGEST signature = dwo_unit->signature;
12850 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12851
12852 if (sig_type != NULL)
12853 {
12854 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12855
12856 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12857 a real dependency of PER_CU on SIG_TYPE. That is detected later
12858 while processing PER_CU. */
12859 if (maybe_queue_comp_unit (NULL, sig_cu, cu->language))
12860 load_full_type_unit (sig_cu, cu->per_objfile);
12861 cu->per_cu->imported_symtabs_push (sig_cu);
12862 }
12863
12864 return 1;
12865 }
12866
12867 /* Queue all TUs contained in the DWO of PER_CU to be read in.
12868 The DWO may have the only definition of the type, though it may not be
12869 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12870 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12871
12872 static void
12873 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12874 {
12875 struct dwo_unit *dwo_unit;
12876 struct dwo_file *dwo_file;
12877
12878 gdb_assert (!per_cu->is_debug_types);
12879 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
12880 gdb_assert (per_cu->cu != NULL);
12881
12882 dwo_unit = per_cu->cu->dwo_unit;
12883 gdb_assert (dwo_unit != NULL);
12884
12885 dwo_file = dwo_unit->dwo_file;
12886 if (dwo_file->tus != NULL)
12887 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12888 per_cu->cu);
12889 }
12890
12891 /* Read in various DIEs. */
12892
12893 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12894 Inherit only the children of the DW_AT_abstract_origin DIE not being
12895 already referenced by DW_AT_abstract_origin from the children of the
12896 current DIE. */
12897
12898 static void
12899 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12900 {
12901 struct die_info *child_die;
12902 sect_offset *offsetp;
12903 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12904 struct die_info *origin_die;
12905 /* Iterator of the ORIGIN_DIE children. */
12906 struct die_info *origin_child_die;
12907 struct attribute *attr;
12908 struct dwarf2_cu *origin_cu;
12909 struct pending **origin_previous_list_in_scope;
12910
12911 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12912 if (!attr)
12913 return;
12914
12915 /* Note that following die references may follow to a die in a
12916 different cu. */
12917
12918 origin_cu = cu;
12919 origin_die = follow_die_ref (die, attr, &origin_cu);
12920
12921 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12922 symbols in. */
12923 origin_previous_list_in_scope = origin_cu->list_in_scope;
12924 origin_cu->list_in_scope = cu->list_in_scope;
12925
12926 if (die->tag != origin_die->tag
12927 && !(die->tag == DW_TAG_inlined_subroutine
12928 && origin_die->tag == DW_TAG_subprogram))
12929 complaint (_("DIE %s and its abstract origin %s have different tags"),
12930 sect_offset_str (die->sect_off),
12931 sect_offset_str (origin_die->sect_off));
12932
12933 std::vector<sect_offset> offsets;
12934
12935 for (child_die = die->child;
12936 child_die && child_die->tag;
12937 child_die = child_die->sibling)
12938 {
12939 struct die_info *child_origin_die;
12940 struct dwarf2_cu *child_origin_cu;
12941
12942 /* We are trying to process concrete instance entries:
12943 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12944 it's not relevant to our analysis here. i.e. detecting DIEs that are
12945 present in the abstract instance but not referenced in the concrete
12946 one. */
12947 if (child_die->tag == DW_TAG_call_site
12948 || child_die->tag == DW_TAG_GNU_call_site)
12949 continue;
12950
12951 /* For each CHILD_DIE, find the corresponding child of
12952 ORIGIN_DIE. If there is more than one layer of
12953 DW_AT_abstract_origin, follow them all; there shouldn't be,
12954 but GCC versions at least through 4.4 generate this (GCC PR
12955 40573). */
12956 child_origin_die = child_die;
12957 child_origin_cu = cu;
12958 while (1)
12959 {
12960 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12961 child_origin_cu);
12962 if (attr == NULL)
12963 break;
12964 child_origin_die = follow_die_ref (child_origin_die, attr,
12965 &child_origin_cu);
12966 }
12967
12968 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12969 counterpart may exist. */
12970 if (child_origin_die != child_die)
12971 {
12972 if (child_die->tag != child_origin_die->tag
12973 && !(child_die->tag == DW_TAG_inlined_subroutine
12974 && child_origin_die->tag == DW_TAG_subprogram))
12975 complaint (_("Child DIE %s and its abstract origin %s have "
12976 "different tags"),
12977 sect_offset_str (child_die->sect_off),
12978 sect_offset_str (child_origin_die->sect_off));
12979 if (child_origin_die->parent != origin_die)
12980 complaint (_("Child DIE %s and its abstract origin %s have "
12981 "different parents"),
12982 sect_offset_str (child_die->sect_off),
12983 sect_offset_str (child_origin_die->sect_off));
12984 else
12985 offsets.push_back (child_origin_die->sect_off);
12986 }
12987 }
12988 std::sort (offsets.begin (), offsets.end ());
12989 sect_offset *offsets_end = offsets.data () + offsets.size ();
12990 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
12991 if (offsetp[-1] == *offsetp)
12992 complaint (_("Multiple children of DIE %s refer "
12993 "to DIE %s as their abstract origin"),
12994 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
12995
12996 offsetp = offsets.data ();
12997 origin_child_die = origin_die->child;
12998 while (origin_child_die && origin_child_die->tag)
12999 {
13000 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13001 while (offsetp < offsets_end
13002 && *offsetp < origin_child_die->sect_off)
13003 offsetp++;
13004 if (offsetp >= offsets_end
13005 || *offsetp > origin_child_die->sect_off)
13006 {
13007 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13008 Check whether we're already processing ORIGIN_CHILD_DIE.
13009 This can happen with mutually referenced abstract_origins.
13010 PR 16581. */
13011 if (!origin_child_die->in_process)
13012 process_die (origin_child_die, origin_cu);
13013 }
13014 origin_child_die = origin_child_die->sibling;
13015 }
13016 origin_cu->list_in_scope = origin_previous_list_in_scope;
13017
13018 if (cu != origin_cu)
13019 compute_delayed_physnames (origin_cu);
13020 }
13021
13022 static void
13023 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13024 {
13025 struct objfile *objfile = cu->per_objfile->objfile;
13026 struct gdbarch *gdbarch = objfile->arch ();
13027 struct context_stack *newobj;
13028 CORE_ADDR lowpc;
13029 CORE_ADDR highpc;
13030 struct die_info *child_die;
13031 struct attribute *attr, *call_line, *call_file;
13032 const char *name;
13033 CORE_ADDR baseaddr;
13034 struct block *block;
13035 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13036 std::vector<struct symbol *> template_args;
13037 struct template_symbol *templ_func = NULL;
13038
13039 if (inlined_func)
13040 {
13041 /* If we do not have call site information, we can't show the
13042 caller of this inlined function. That's too confusing, so
13043 only use the scope for local variables. */
13044 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13045 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13046 if (call_line == NULL || call_file == NULL)
13047 {
13048 read_lexical_block_scope (die, cu);
13049 return;
13050 }
13051 }
13052
13053 baseaddr = objfile->text_section_offset ();
13054
13055 name = dwarf2_name (die, cu);
13056
13057 /* Ignore functions with missing or empty names. These are actually
13058 illegal according to the DWARF standard. */
13059 if (name == NULL)
13060 {
13061 complaint (_("missing name for subprogram DIE at %s"),
13062 sect_offset_str (die->sect_off));
13063 return;
13064 }
13065
13066 /* Ignore functions with missing or invalid low and high pc attributes. */
13067 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13068 <= PC_BOUNDS_INVALID)
13069 {
13070 attr = dwarf2_attr (die, DW_AT_external, cu);
13071 if (!attr || !DW_UNSND (attr))
13072 complaint (_("cannot get low and high bounds "
13073 "for subprogram DIE at %s"),
13074 sect_offset_str (die->sect_off));
13075 return;
13076 }
13077
13078 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13079 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13080
13081 /* If we have any template arguments, then we must allocate a
13082 different sort of symbol. */
13083 for (child_die = die->child; child_die; child_die = child_die->sibling)
13084 {
13085 if (child_die->tag == DW_TAG_template_type_param
13086 || child_die->tag == DW_TAG_template_value_param)
13087 {
13088 templ_func = new (&objfile->objfile_obstack) template_symbol;
13089 templ_func->subclass = SYMBOL_TEMPLATE;
13090 break;
13091 }
13092 }
13093
13094 newobj = cu->get_builder ()->push_context (0, lowpc);
13095 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13096 (struct symbol *) templ_func);
13097
13098 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13099 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13100 cu->language);
13101
13102 /* If there is a location expression for DW_AT_frame_base, record
13103 it. */
13104 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13105 if (attr != nullptr)
13106 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13107
13108 /* If there is a location for the static link, record it. */
13109 newobj->static_link = NULL;
13110 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13111 if (attr != nullptr)
13112 {
13113 newobj->static_link
13114 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13115 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13116 cu->per_cu->addr_type ());
13117 }
13118
13119 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13120
13121 if (die->child != NULL)
13122 {
13123 child_die = die->child;
13124 while (child_die && child_die->tag)
13125 {
13126 if (child_die->tag == DW_TAG_template_type_param
13127 || child_die->tag == DW_TAG_template_value_param)
13128 {
13129 struct symbol *arg = new_symbol (child_die, NULL, cu);
13130
13131 if (arg != NULL)
13132 template_args.push_back (arg);
13133 }
13134 else
13135 process_die (child_die, cu);
13136 child_die = child_die->sibling;
13137 }
13138 }
13139
13140 inherit_abstract_dies (die, cu);
13141
13142 /* If we have a DW_AT_specification, we might need to import using
13143 directives from the context of the specification DIE. See the
13144 comment in determine_prefix. */
13145 if (cu->language == language_cplus
13146 && dwarf2_attr (die, DW_AT_specification, cu))
13147 {
13148 struct dwarf2_cu *spec_cu = cu;
13149 struct die_info *spec_die = die_specification (die, &spec_cu);
13150
13151 while (spec_die)
13152 {
13153 child_die = spec_die->child;
13154 while (child_die && child_die->tag)
13155 {
13156 if (child_die->tag == DW_TAG_imported_module)
13157 process_die (child_die, spec_cu);
13158 child_die = child_die->sibling;
13159 }
13160
13161 /* In some cases, GCC generates specification DIEs that
13162 themselves contain DW_AT_specification attributes. */
13163 spec_die = die_specification (spec_die, &spec_cu);
13164 }
13165 }
13166
13167 struct context_stack cstk = cu->get_builder ()->pop_context ();
13168 /* Make a block for the local symbols within. */
13169 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13170 cstk.static_link, lowpc, highpc);
13171
13172 /* For C++, set the block's scope. */
13173 if ((cu->language == language_cplus
13174 || cu->language == language_fortran
13175 || cu->language == language_d
13176 || cu->language == language_rust)
13177 && cu->processing_has_namespace_info)
13178 block_set_scope (block, determine_prefix (die, cu),
13179 &objfile->objfile_obstack);
13180
13181 /* If we have address ranges, record them. */
13182 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13183
13184 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13185
13186 /* Attach template arguments to function. */
13187 if (!template_args.empty ())
13188 {
13189 gdb_assert (templ_func != NULL);
13190
13191 templ_func->n_template_arguments = template_args.size ();
13192 templ_func->template_arguments
13193 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13194 templ_func->n_template_arguments);
13195 memcpy (templ_func->template_arguments,
13196 template_args.data (),
13197 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13198
13199 /* Make sure that the symtab is set on the new symbols. Even
13200 though they don't appear in this symtab directly, other parts
13201 of gdb assume that symbols do, and this is reasonably
13202 true. */
13203 for (symbol *sym : template_args)
13204 symbol_set_symtab (sym, symbol_symtab (templ_func));
13205 }
13206
13207 /* In C++, we can have functions nested inside functions (e.g., when
13208 a function declares a class that has methods). This means that
13209 when we finish processing a function scope, we may need to go
13210 back to building a containing block's symbol lists. */
13211 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13212 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13213
13214 /* If we've finished processing a top-level function, subsequent
13215 symbols go in the file symbol list. */
13216 if (cu->get_builder ()->outermost_context_p ())
13217 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13218 }
13219
13220 /* Process all the DIES contained within a lexical block scope. Start
13221 a new scope, process the dies, and then close the scope. */
13222
13223 static void
13224 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13225 {
13226 struct objfile *objfile = cu->per_objfile->objfile;
13227 struct gdbarch *gdbarch = objfile->arch ();
13228 CORE_ADDR lowpc, highpc;
13229 struct die_info *child_die;
13230 CORE_ADDR baseaddr;
13231
13232 baseaddr = objfile->text_section_offset ();
13233
13234 /* Ignore blocks with missing or invalid low and high pc attributes. */
13235 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13236 as multiple lexical blocks? Handling children in a sane way would
13237 be nasty. Might be easier to properly extend generic blocks to
13238 describe ranges. */
13239 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13240 {
13241 case PC_BOUNDS_NOT_PRESENT:
13242 /* DW_TAG_lexical_block has no attributes, process its children as if
13243 there was no wrapping by that DW_TAG_lexical_block.
13244 GCC does no longer produces such DWARF since GCC r224161. */
13245 for (child_die = die->child;
13246 child_die != NULL && child_die->tag;
13247 child_die = child_die->sibling)
13248 {
13249 /* We might already be processing this DIE. This can happen
13250 in an unusual circumstance -- where a subroutine A
13251 appears lexically in another subroutine B, but A actually
13252 inlines B. The recursion is broken here, rather than in
13253 inherit_abstract_dies, because it seems better to simply
13254 drop concrete children here. */
13255 if (!child_die->in_process)
13256 process_die (child_die, cu);
13257 }
13258 return;
13259 case PC_BOUNDS_INVALID:
13260 return;
13261 }
13262 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13263 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13264
13265 cu->get_builder ()->push_context (0, lowpc);
13266 if (die->child != NULL)
13267 {
13268 child_die = die->child;
13269 while (child_die && child_die->tag)
13270 {
13271 process_die (child_die, cu);
13272 child_die = child_die->sibling;
13273 }
13274 }
13275 inherit_abstract_dies (die, cu);
13276 struct context_stack cstk = cu->get_builder ()->pop_context ();
13277
13278 if (*cu->get_builder ()->get_local_symbols () != NULL
13279 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13280 {
13281 struct block *block
13282 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13283 cstk.start_addr, highpc);
13284
13285 /* Note that recording ranges after traversing children, as we
13286 do here, means that recording a parent's ranges entails
13287 walking across all its children's ranges as they appear in
13288 the address map, which is quadratic behavior.
13289
13290 It would be nicer to record the parent's ranges before
13291 traversing its children, simply overriding whatever you find
13292 there. But since we don't even decide whether to create a
13293 block until after we've traversed its children, that's hard
13294 to do. */
13295 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13296 }
13297 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13298 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13299 }
13300
13301 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13302
13303 static void
13304 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13305 {
13306 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13307 struct objfile *objfile = per_objfile->objfile;
13308 struct gdbarch *gdbarch = objfile->arch ();
13309 CORE_ADDR pc, baseaddr;
13310 struct attribute *attr;
13311 struct call_site *call_site, call_site_local;
13312 void **slot;
13313 int nparams;
13314 struct die_info *child_die;
13315
13316 baseaddr = objfile->text_section_offset ();
13317
13318 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13319 if (attr == NULL)
13320 {
13321 /* This was a pre-DWARF-5 GNU extension alias
13322 for DW_AT_call_return_pc. */
13323 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13324 }
13325 if (!attr)
13326 {
13327 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13328 "DIE %s [in module %s]"),
13329 sect_offset_str (die->sect_off), objfile_name (objfile));
13330 return;
13331 }
13332 pc = attr->value_as_address () + baseaddr;
13333 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13334
13335 if (cu->call_site_htab == NULL)
13336 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13337 NULL, &objfile->objfile_obstack,
13338 hashtab_obstack_allocate, NULL);
13339 call_site_local.pc = pc;
13340 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13341 if (*slot != NULL)
13342 {
13343 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13344 "DIE %s [in module %s]"),
13345 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13346 objfile_name (objfile));
13347 return;
13348 }
13349
13350 /* Count parameters at the caller. */
13351
13352 nparams = 0;
13353 for (child_die = die->child; child_die && child_die->tag;
13354 child_die = child_die->sibling)
13355 {
13356 if (child_die->tag != DW_TAG_call_site_parameter
13357 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13358 {
13359 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13360 "DW_TAG_call_site child DIE %s [in module %s]"),
13361 child_die->tag, sect_offset_str (child_die->sect_off),
13362 objfile_name (objfile));
13363 continue;
13364 }
13365
13366 nparams++;
13367 }
13368
13369 call_site
13370 = ((struct call_site *)
13371 obstack_alloc (&objfile->objfile_obstack,
13372 sizeof (*call_site)
13373 + (sizeof (*call_site->parameter) * (nparams - 1))));
13374 *slot = call_site;
13375 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13376 call_site->pc = pc;
13377
13378 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13379 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13380 {
13381 struct die_info *func_die;
13382
13383 /* Skip also over DW_TAG_inlined_subroutine. */
13384 for (func_die = die->parent;
13385 func_die && func_die->tag != DW_TAG_subprogram
13386 && func_die->tag != DW_TAG_subroutine_type;
13387 func_die = func_die->parent);
13388
13389 /* DW_AT_call_all_calls is a superset
13390 of DW_AT_call_all_tail_calls. */
13391 if (func_die
13392 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13393 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13394 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13395 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13396 {
13397 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13398 not complete. But keep CALL_SITE for look ups via call_site_htab,
13399 both the initial caller containing the real return address PC and
13400 the final callee containing the current PC of a chain of tail
13401 calls do not need to have the tail call list complete. But any
13402 function candidate for a virtual tail call frame searched via
13403 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13404 determined unambiguously. */
13405 }
13406 else
13407 {
13408 struct type *func_type = NULL;
13409
13410 if (func_die)
13411 func_type = get_die_type (func_die, cu);
13412 if (func_type != NULL)
13413 {
13414 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13415
13416 /* Enlist this call site to the function. */
13417 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13418 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13419 }
13420 else
13421 complaint (_("Cannot find function owning DW_TAG_call_site "
13422 "DIE %s [in module %s]"),
13423 sect_offset_str (die->sect_off), objfile_name (objfile));
13424 }
13425 }
13426
13427 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13428 if (attr == NULL)
13429 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13430 if (attr == NULL)
13431 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13432 if (attr == NULL)
13433 {
13434 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13435 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13436 }
13437 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13438 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13439 /* Keep NULL DWARF_BLOCK. */;
13440 else if (attr->form_is_block ())
13441 {
13442 struct dwarf2_locexpr_baton *dlbaton;
13443
13444 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13445 dlbaton->data = DW_BLOCK (attr)->data;
13446 dlbaton->size = DW_BLOCK (attr)->size;
13447 dlbaton->per_objfile = per_objfile;
13448 dlbaton->per_cu = cu->per_cu;
13449
13450 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13451 }
13452 else if (attr->form_is_ref ())
13453 {
13454 struct dwarf2_cu *target_cu = cu;
13455 struct die_info *target_die;
13456
13457 target_die = follow_die_ref (die, attr, &target_cu);
13458 gdb_assert (target_cu->per_objfile->objfile == objfile);
13459 if (die_is_declaration (target_die, target_cu))
13460 {
13461 const char *target_physname;
13462
13463 /* Prefer the mangled name; otherwise compute the demangled one. */
13464 target_physname = dw2_linkage_name (target_die, target_cu);
13465 if (target_physname == NULL)
13466 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13467 if (target_physname == NULL)
13468 complaint (_("DW_AT_call_target target DIE has invalid "
13469 "physname, for referencing DIE %s [in module %s]"),
13470 sect_offset_str (die->sect_off), objfile_name (objfile));
13471 else
13472 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13473 }
13474 else
13475 {
13476 CORE_ADDR lowpc;
13477
13478 /* DW_AT_entry_pc should be preferred. */
13479 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13480 <= PC_BOUNDS_INVALID)
13481 complaint (_("DW_AT_call_target target DIE has invalid "
13482 "low pc, for referencing DIE %s [in module %s]"),
13483 sect_offset_str (die->sect_off), objfile_name (objfile));
13484 else
13485 {
13486 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13487 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13488 }
13489 }
13490 }
13491 else
13492 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13493 "block nor reference, for DIE %s [in module %s]"),
13494 sect_offset_str (die->sect_off), objfile_name (objfile));
13495
13496 call_site->per_cu = cu->per_cu;
13497
13498 for (child_die = die->child;
13499 child_die && child_die->tag;
13500 child_die = child_die->sibling)
13501 {
13502 struct call_site_parameter *parameter;
13503 struct attribute *loc, *origin;
13504
13505 if (child_die->tag != DW_TAG_call_site_parameter
13506 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13507 {
13508 /* Already printed the complaint above. */
13509 continue;
13510 }
13511
13512 gdb_assert (call_site->parameter_count < nparams);
13513 parameter = &call_site->parameter[call_site->parameter_count];
13514
13515 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13516 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13517 register is contained in DW_AT_call_value. */
13518
13519 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13520 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13521 if (origin == NULL)
13522 {
13523 /* This was a pre-DWARF-5 GNU extension alias
13524 for DW_AT_call_parameter. */
13525 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13526 }
13527 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13528 {
13529 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13530
13531 sect_offset sect_off = origin->get_ref_die_offset ();
13532 if (!cu->header.offset_in_cu_p (sect_off))
13533 {
13534 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13535 binding can be done only inside one CU. Such referenced DIE
13536 therefore cannot be even moved to DW_TAG_partial_unit. */
13537 complaint (_("DW_AT_call_parameter offset is not in CU for "
13538 "DW_TAG_call_site child DIE %s [in module %s]"),
13539 sect_offset_str (child_die->sect_off),
13540 objfile_name (objfile));
13541 continue;
13542 }
13543 parameter->u.param_cu_off
13544 = (cu_offset) (sect_off - cu->header.sect_off);
13545 }
13546 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13547 {
13548 complaint (_("No DW_FORM_block* DW_AT_location for "
13549 "DW_TAG_call_site child DIE %s [in module %s]"),
13550 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13551 continue;
13552 }
13553 else
13554 {
13555 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13556 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13557 if (parameter->u.dwarf_reg != -1)
13558 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13559 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13560 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13561 &parameter->u.fb_offset))
13562 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13563 else
13564 {
13565 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13566 "for DW_FORM_block* DW_AT_location is supported for "
13567 "DW_TAG_call_site child DIE %s "
13568 "[in module %s]"),
13569 sect_offset_str (child_die->sect_off),
13570 objfile_name (objfile));
13571 continue;
13572 }
13573 }
13574
13575 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13576 if (attr == NULL)
13577 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13578 if (attr == NULL || !attr->form_is_block ())
13579 {
13580 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13581 "DW_TAG_call_site child DIE %s [in module %s]"),
13582 sect_offset_str (child_die->sect_off),
13583 objfile_name (objfile));
13584 continue;
13585 }
13586 parameter->value = DW_BLOCK (attr)->data;
13587 parameter->value_size = DW_BLOCK (attr)->size;
13588
13589 /* Parameters are not pre-cleared by memset above. */
13590 parameter->data_value = NULL;
13591 parameter->data_value_size = 0;
13592 call_site->parameter_count++;
13593
13594 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13595 if (attr == NULL)
13596 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13597 if (attr != nullptr)
13598 {
13599 if (!attr->form_is_block ())
13600 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13601 "DW_TAG_call_site child DIE %s [in module %s]"),
13602 sect_offset_str (child_die->sect_off),
13603 objfile_name (objfile));
13604 else
13605 {
13606 parameter->data_value = DW_BLOCK (attr)->data;
13607 parameter->data_value_size = DW_BLOCK (attr)->size;
13608 }
13609 }
13610 }
13611 }
13612
13613 /* Helper function for read_variable. If DIE represents a virtual
13614 table, then return the type of the concrete object that is
13615 associated with the virtual table. Otherwise, return NULL. */
13616
13617 static struct type *
13618 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13619 {
13620 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13621 if (attr == NULL)
13622 return NULL;
13623
13624 /* Find the type DIE. */
13625 struct die_info *type_die = NULL;
13626 struct dwarf2_cu *type_cu = cu;
13627
13628 if (attr->form_is_ref ())
13629 type_die = follow_die_ref (die, attr, &type_cu);
13630 if (type_die == NULL)
13631 return NULL;
13632
13633 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13634 return NULL;
13635 return die_containing_type (type_die, type_cu);
13636 }
13637
13638 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13639
13640 static void
13641 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13642 {
13643 struct rust_vtable_symbol *storage = NULL;
13644
13645 if (cu->language == language_rust)
13646 {
13647 struct type *containing_type = rust_containing_type (die, cu);
13648
13649 if (containing_type != NULL)
13650 {
13651 struct objfile *objfile = cu->per_objfile->objfile;
13652
13653 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13654 storage->concrete_type = containing_type;
13655 storage->subclass = SYMBOL_RUST_VTABLE;
13656 }
13657 }
13658
13659 struct symbol *res = new_symbol (die, NULL, cu, storage);
13660 struct attribute *abstract_origin
13661 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13662 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13663 if (res == NULL && loc && abstract_origin)
13664 {
13665 /* We have a variable without a name, but with a location and an abstract
13666 origin. This may be a concrete instance of an abstract variable
13667 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13668 later. */
13669 struct dwarf2_cu *origin_cu = cu;
13670 struct die_info *origin_die
13671 = follow_die_ref (die, abstract_origin, &origin_cu);
13672 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13673 per_objfile->per_bfd->abstract_to_concrete
13674 [origin_die->sect_off].push_back (die->sect_off);
13675 }
13676 }
13677
13678 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13679 reading .debug_rnglists.
13680 Callback's type should be:
13681 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13682 Return true if the attributes are present and valid, otherwise,
13683 return false. */
13684
13685 template <typename Callback>
13686 static bool
13687 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13688 Callback &&callback)
13689 {
13690 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13691 struct objfile *objfile = dwarf2_per_objfile->objfile;
13692 bfd *obfd = objfile->obfd;
13693 /* Base address selection entry. */
13694 gdb::optional<CORE_ADDR> base;
13695 const gdb_byte *buffer;
13696 CORE_ADDR baseaddr;
13697 bool overflow = false;
13698
13699 base = cu->base_address;
13700
13701 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13702 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13703 {
13704 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13705 offset);
13706 return false;
13707 }
13708 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13709
13710 baseaddr = objfile->text_section_offset ();
13711
13712 while (1)
13713 {
13714 /* Initialize it due to a false compiler warning. */
13715 CORE_ADDR range_beginning = 0, range_end = 0;
13716 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13717 + dwarf2_per_objfile->per_bfd->rnglists.size);
13718 unsigned int bytes_read;
13719
13720 if (buffer == buf_end)
13721 {
13722 overflow = true;
13723 break;
13724 }
13725 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13726 switch (rlet)
13727 {
13728 case DW_RLE_end_of_list:
13729 break;
13730 case DW_RLE_base_address:
13731 if (buffer + cu->header.addr_size > buf_end)
13732 {
13733 overflow = true;
13734 break;
13735 }
13736 base = cu->header.read_address (obfd, buffer, &bytes_read);
13737 buffer += bytes_read;
13738 break;
13739 case DW_RLE_start_length:
13740 if (buffer + cu->header.addr_size > buf_end)
13741 {
13742 overflow = true;
13743 break;
13744 }
13745 range_beginning = cu->header.read_address (obfd, buffer,
13746 &bytes_read);
13747 buffer += bytes_read;
13748 range_end = (range_beginning
13749 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13750 buffer += bytes_read;
13751 if (buffer > buf_end)
13752 {
13753 overflow = true;
13754 break;
13755 }
13756 break;
13757 case DW_RLE_offset_pair:
13758 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13759 buffer += bytes_read;
13760 if (buffer > buf_end)
13761 {
13762 overflow = true;
13763 break;
13764 }
13765 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13766 buffer += bytes_read;
13767 if (buffer > buf_end)
13768 {
13769 overflow = true;
13770 break;
13771 }
13772 break;
13773 case DW_RLE_start_end:
13774 if (buffer + 2 * cu->header.addr_size > buf_end)
13775 {
13776 overflow = true;
13777 break;
13778 }
13779 range_beginning = cu->header.read_address (obfd, buffer,
13780 &bytes_read);
13781 buffer += bytes_read;
13782 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13783 buffer += bytes_read;
13784 break;
13785 default:
13786 complaint (_("Invalid .debug_rnglists data (no base address)"));
13787 return false;
13788 }
13789 if (rlet == DW_RLE_end_of_list || overflow)
13790 break;
13791 if (rlet == DW_RLE_base_address)
13792 continue;
13793
13794 if (!base.has_value ())
13795 {
13796 /* We have no valid base address for the ranges
13797 data. */
13798 complaint (_("Invalid .debug_rnglists data (no base address)"));
13799 return false;
13800 }
13801
13802 if (range_beginning > range_end)
13803 {
13804 /* Inverted range entries are invalid. */
13805 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13806 return false;
13807 }
13808
13809 /* Empty range entries have no effect. */
13810 if (range_beginning == range_end)
13811 continue;
13812
13813 range_beginning += *base;
13814 range_end += *base;
13815
13816 /* A not-uncommon case of bad debug info.
13817 Don't pollute the addrmap with bad data. */
13818 if (range_beginning + baseaddr == 0
13819 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13820 {
13821 complaint (_(".debug_rnglists entry has start address of zero"
13822 " [in module %s]"), objfile_name (objfile));
13823 continue;
13824 }
13825
13826 callback (range_beginning, range_end);
13827 }
13828
13829 if (overflow)
13830 {
13831 complaint (_("Offset %d is not terminated "
13832 "for DW_AT_ranges attribute"),
13833 offset);
13834 return false;
13835 }
13836
13837 return true;
13838 }
13839
13840 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13841 Callback's type should be:
13842 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13843 Return 1 if the attributes are present and valid, otherwise, return 0. */
13844
13845 template <typename Callback>
13846 static int
13847 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13848 Callback &&callback)
13849 {
13850 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13851 struct objfile *objfile = per_objfile->objfile;
13852 struct comp_unit_head *cu_header = &cu->header;
13853 bfd *obfd = objfile->obfd;
13854 unsigned int addr_size = cu_header->addr_size;
13855 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13856 /* Base address selection entry. */
13857 gdb::optional<CORE_ADDR> base;
13858 unsigned int dummy;
13859 const gdb_byte *buffer;
13860 CORE_ADDR baseaddr;
13861
13862 if (cu_header->version >= 5)
13863 return dwarf2_rnglists_process (offset, cu, callback);
13864
13865 base = cu->base_address;
13866
13867 per_objfile->per_bfd->ranges.read (objfile);
13868 if (offset >= per_objfile->per_bfd->ranges.size)
13869 {
13870 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13871 offset);
13872 return 0;
13873 }
13874 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13875
13876 baseaddr = objfile->text_section_offset ();
13877
13878 while (1)
13879 {
13880 CORE_ADDR range_beginning, range_end;
13881
13882 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13883 buffer += addr_size;
13884 range_end = cu->header.read_address (obfd, buffer, &dummy);
13885 buffer += addr_size;
13886 offset += 2 * addr_size;
13887
13888 /* An end of list marker is a pair of zero addresses. */
13889 if (range_beginning == 0 && range_end == 0)
13890 /* Found the end of list entry. */
13891 break;
13892
13893 /* Each base address selection entry is a pair of 2 values.
13894 The first is the largest possible address, the second is
13895 the base address. Check for a base address here. */
13896 if ((range_beginning & mask) == mask)
13897 {
13898 /* If we found the largest possible address, then we already
13899 have the base address in range_end. */
13900 base = range_end;
13901 continue;
13902 }
13903
13904 if (!base.has_value ())
13905 {
13906 /* We have no valid base address for the ranges
13907 data. */
13908 complaint (_("Invalid .debug_ranges data (no base address)"));
13909 return 0;
13910 }
13911
13912 if (range_beginning > range_end)
13913 {
13914 /* Inverted range entries are invalid. */
13915 complaint (_("Invalid .debug_ranges data (inverted range)"));
13916 return 0;
13917 }
13918
13919 /* Empty range entries have no effect. */
13920 if (range_beginning == range_end)
13921 continue;
13922
13923 range_beginning += *base;
13924 range_end += *base;
13925
13926 /* A not-uncommon case of bad debug info.
13927 Don't pollute the addrmap with bad data. */
13928 if (range_beginning + baseaddr == 0
13929 && !per_objfile->per_bfd->has_section_at_zero)
13930 {
13931 complaint (_(".debug_ranges entry has start address of zero"
13932 " [in module %s]"), objfile_name (objfile));
13933 continue;
13934 }
13935
13936 callback (range_beginning, range_end);
13937 }
13938
13939 return 1;
13940 }
13941
13942 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13943 Return 1 if the attributes are present and valid, otherwise, return 0.
13944 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13945
13946 static int
13947 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13948 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13949 dwarf2_psymtab *ranges_pst)
13950 {
13951 struct objfile *objfile = cu->per_objfile->objfile;
13952 struct gdbarch *gdbarch = objfile->arch ();
13953 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13954 int low_set = 0;
13955 CORE_ADDR low = 0;
13956 CORE_ADDR high = 0;
13957 int retval;
13958
13959 retval = dwarf2_ranges_process (offset, cu,
13960 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13961 {
13962 if (ranges_pst != NULL)
13963 {
13964 CORE_ADDR lowpc;
13965 CORE_ADDR highpc;
13966
13967 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13968 range_beginning + baseaddr)
13969 - baseaddr);
13970 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13971 range_end + baseaddr)
13972 - baseaddr);
13973 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13974 lowpc, highpc - 1, ranges_pst);
13975 }
13976
13977 /* FIXME: This is recording everything as a low-high
13978 segment of consecutive addresses. We should have a
13979 data structure for discontiguous block ranges
13980 instead. */
13981 if (! low_set)
13982 {
13983 low = range_beginning;
13984 high = range_end;
13985 low_set = 1;
13986 }
13987 else
13988 {
13989 if (range_beginning < low)
13990 low = range_beginning;
13991 if (range_end > high)
13992 high = range_end;
13993 }
13994 });
13995 if (!retval)
13996 return 0;
13997
13998 if (! low_set)
13999 /* If the first entry is an end-of-list marker, the range
14000 describes an empty scope, i.e. no instructions. */
14001 return 0;
14002
14003 if (low_return)
14004 *low_return = low;
14005 if (high_return)
14006 *high_return = high;
14007 return 1;
14008 }
14009
14010 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14011 definition for the return value. *LOWPC and *HIGHPC are set iff
14012 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14013
14014 static enum pc_bounds_kind
14015 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14016 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14017 dwarf2_psymtab *pst)
14018 {
14019 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14020 struct attribute *attr;
14021 struct attribute *attr_high;
14022 CORE_ADDR low = 0;
14023 CORE_ADDR high = 0;
14024 enum pc_bounds_kind ret;
14025
14026 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14027 if (attr_high)
14028 {
14029 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14030 if (attr != nullptr)
14031 {
14032 low = attr->value_as_address ();
14033 high = attr_high->value_as_address ();
14034 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14035 high += low;
14036 }
14037 else
14038 /* Found high w/o low attribute. */
14039 return PC_BOUNDS_INVALID;
14040
14041 /* Found consecutive range of addresses. */
14042 ret = PC_BOUNDS_HIGH_LOW;
14043 }
14044 else
14045 {
14046 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14047 if (attr != NULL)
14048 {
14049 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14050 We take advantage of the fact that DW_AT_ranges does not appear
14051 in DW_TAG_compile_unit of DWO files. */
14052 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14053 unsigned int ranges_offset = (DW_UNSND (attr)
14054 + (need_ranges_base
14055 ? cu->ranges_base
14056 : 0));
14057
14058 /* Value of the DW_AT_ranges attribute is the offset in the
14059 .debug_ranges section. */
14060 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14061 return PC_BOUNDS_INVALID;
14062 /* Found discontinuous range of addresses. */
14063 ret = PC_BOUNDS_RANGES;
14064 }
14065 else
14066 return PC_BOUNDS_NOT_PRESENT;
14067 }
14068
14069 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14070 if (high <= low)
14071 return PC_BOUNDS_INVALID;
14072
14073 /* When using the GNU linker, .gnu.linkonce. sections are used to
14074 eliminate duplicate copies of functions and vtables and such.
14075 The linker will arbitrarily choose one and discard the others.
14076 The AT_*_pc values for such functions refer to local labels in
14077 these sections. If the section from that file was discarded, the
14078 labels are not in the output, so the relocs get a value of 0.
14079 If this is a discarded function, mark the pc bounds as invalid,
14080 so that GDB will ignore it. */
14081 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14082 return PC_BOUNDS_INVALID;
14083
14084 *lowpc = low;
14085 if (highpc)
14086 *highpc = high;
14087 return ret;
14088 }
14089
14090 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14091 its low and high PC addresses. Do nothing if these addresses could not
14092 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14093 and HIGHPC to the high address if greater than HIGHPC. */
14094
14095 static void
14096 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14097 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14098 struct dwarf2_cu *cu)
14099 {
14100 CORE_ADDR low, high;
14101 struct die_info *child = die->child;
14102
14103 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14104 {
14105 *lowpc = std::min (*lowpc, low);
14106 *highpc = std::max (*highpc, high);
14107 }
14108
14109 /* If the language does not allow nested subprograms (either inside
14110 subprograms or lexical blocks), we're done. */
14111 if (cu->language != language_ada)
14112 return;
14113
14114 /* Check all the children of the given DIE. If it contains nested
14115 subprograms, then check their pc bounds. Likewise, we need to
14116 check lexical blocks as well, as they may also contain subprogram
14117 definitions. */
14118 while (child && child->tag)
14119 {
14120 if (child->tag == DW_TAG_subprogram
14121 || child->tag == DW_TAG_lexical_block)
14122 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14123 child = child->sibling;
14124 }
14125 }
14126
14127 /* Get the low and high pc's represented by the scope DIE, and store
14128 them in *LOWPC and *HIGHPC. If the correct values can't be
14129 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14130
14131 static void
14132 get_scope_pc_bounds (struct die_info *die,
14133 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14134 struct dwarf2_cu *cu)
14135 {
14136 CORE_ADDR best_low = (CORE_ADDR) -1;
14137 CORE_ADDR best_high = (CORE_ADDR) 0;
14138 CORE_ADDR current_low, current_high;
14139
14140 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14141 >= PC_BOUNDS_RANGES)
14142 {
14143 best_low = current_low;
14144 best_high = current_high;
14145 }
14146 else
14147 {
14148 struct die_info *child = die->child;
14149
14150 while (child && child->tag)
14151 {
14152 switch (child->tag) {
14153 case DW_TAG_subprogram:
14154 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14155 break;
14156 case DW_TAG_namespace:
14157 case DW_TAG_module:
14158 /* FIXME: carlton/2004-01-16: Should we do this for
14159 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14160 that current GCC's always emit the DIEs corresponding
14161 to definitions of methods of classes as children of a
14162 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14163 the DIEs giving the declarations, which could be
14164 anywhere). But I don't see any reason why the
14165 standards says that they have to be there. */
14166 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14167
14168 if (current_low != ((CORE_ADDR) -1))
14169 {
14170 best_low = std::min (best_low, current_low);
14171 best_high = std::max (best_high, current_high);
14172 }
14173 break;
14174 default:
14175 /* Ignore. */
14176 break;
14177 }
14178
14179 child = child->sibling;
14180 }
14181 }
14182
14183 *lowpc = best_low;
14184 *highpc = best_high;
14185 }
14186
14187 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14188 in DIE. */
14189
14190 static void
14191 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14192 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14193 {
14194 struct objfile *objfile = cu->per_objfile->objfile;
14195 struct gdbarch *gdbarch = objfile->arch ();
14196 struct attribute *attr;
14197 struct attribute *attr_high;
14198
14199 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14200 if (attr_high)
14201 {
14202 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14203 if (attr != nullptr)
14204 {
14205 CORE_ADDR low = attr->value_as_address ();
14206 CORE_ADDR high = attr_high->value_as_address ();
14207
14208 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14209 high += low;
14210
14211 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14212 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14213 cu->get_builder ()->record_block_range (block, low, high - 1);
14214 }
14215 }
14216
14217 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14218 if (attr != nullptr)
14219 {
14220 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14221 We take advantage of the fact that DW_AT_ranges does not appear
14222 in DW_TAG_compile_unit of DWO files. */
14223 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14224
14225 /* The value of the DW_AT_ranges attribute is the offset of the
14226 address range list in the .debug_ranges section. */
14227 unsigned long offset = (DW_UNSND (attr)
14228 + (need_ranges_base ? cu->ranges_base : 0));
14229
14230 std::vector<blockrange> blockvec;
14231 dwarf2_ranges_process (offset, cu,
14232 [&] (CORE_ADDR start, CORE_ADDR end)
14233 {
14234 start += baseaddr;
14235 end += baseaddr;
14236 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14237 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14238 cu->get_builder ()->record_block_range (block, start, end - 1);
14239 blockvec.emplace_back (start, end);
14240 });
14241
14242 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14243 }
14244 }
14245
14246 /* Check whether the producer field indicates either of GCC < 4.6, or the
14247 Intel C/C++ compiler, and cache the result in CU. */
14248
14249 static void
14250 check_producer (struct dwarf2_cu *cu)
14251 {
14252 int major, minor;
14253
14254 if (cu->producer == NULL)
14255 {
14256 /* For unknown compilers expect their behavior is DWARF version
14257 compliant.
14258
14259 GCC started to support .debug_types sections by -gdwarf-4 since
14260 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14261 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14262 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14263 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14264 }
14265 else if (producer_is_gcc (cu->producer, &major, &minor))
14266 {
14267 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14268 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14269 }
14270 else if (producer_is_icc (cu->producer, &major, &minor))
14271 {
14272 cu->producer_is_icc = true;
14273 cu->producer_is_icc_lt_14 = major < 14;
14274 }
14275 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14276 cu->producer_is_codewarrior = true;
14277 else
14278 {
14279 /* For other non-GCC compilers, expect their behavior is DWARF version
14280 compliant. */
14281 }
14282
14283 cu->checked_producer = true;
14284 }
14285
14286 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14287 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14288 during 4.6.0 experimental. */
14289
14290 static bool
14291 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14292 {
14293 if (!cu->checked_producer)
14294 check_producer (cu);
14295
14296 return cu->producer_is_gxx_lt_4_6;
14297 }
14298
14299
14300 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14301 with incorrect is_stmt attributes. */
14302
14303 static bool
14304 producer_is_codewarrior (struct dwarf2_cu *cu)
14305 {
14306 if (!cu->checked_producer)
14307 check_producer (cu);
14308
14309 return cu->producer_is_codewarrior;
14310 }
14311
14312 /* Return the default accessibility type if it is not overridden by
14313 DW_AT_accessibility. */
14314
14315 static enum dwarf_access_attribute
14316 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14317 {
14318 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14319 {
14320 /* The default DWARF 2 accessibility for members is public, the default
14321 accessibility for inheritance is private. */
14322
14323 if (die->tag != DW_TAG_inheritance)
14324 return DW_ACCESS_public;
14325 else
14326 return DW_ACCESS_private;
14327 }
14328 else
14329 {
14330 /* DWARF 3+ defines the default accessibility a different way. The same
14331 rules apply now for DW_TAG_inheritance as for the members and it only
14332 depends on the container kind. */
14333
14334 if (die->parent->tag == DW_TAG_class_type)
14335 return DW_ACCESS_private;
14336 else
14337 return DW_ACCESS_public;
14338 }
14339 }
14340
14341 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14342 offset. If the attribute was not found return 0, otherwise return
14343 1. If it was found but could not properly be handled, set *OFFSET
14344 to 0. */
14345
14346 static int
14347 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14348 LONGEST *offset)
14349 {
14350 struct attribute *attr;
14351
14352 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14353 if (attr != NULL)
14354 {
14355 *offset = 0;
14356
14357 /* Note that we do not check for a section offset first here.
14358 This is because DW_AT_data_member_location is new in DWARF 4,
14359 so if we see it, we can assume that a constant form is really
14360 a constant and not a section offset. */
14361 if (attr->form_is_constant ())
14362 *offset = attr->constant_value (0);
14363 else if (attr->form_is_section_offset ())
14364 dwarf2_complex_location_expr_complaint ();
14365 else if (attr->form_is_block ())
14366 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14367 else
14368 dwarf2_complex_location_expr_complaint ();
14369
14370 return 1;
14371 }
14372
14373 return 0;
14374 }
14375
14376 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14377
14378 static void
14379 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14380 struct field *field)
14381 {
14382 struct attribute *attr;
14383
14384 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14385 if (attr != NULL)
14386 {
14387 if (attr->form_is_constant ())
14388 {
14389 LONGEST offset = attr->constant_value (0);
14390 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14391 }
14392 else if (attr->form_is_section_offset ())
14393 dwarf2_complex_location_expr_complaint ();
14394 else if (attr->form_is_block ())
14395 {
14396 bool handled;
14397 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14398 if (handled)
14399 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14400 else
14401 {
14402 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14403 struct objfile *objfile = per_objfile->objfile;
14404 struct dwarf2_locexpr_baton *dlbaton
14405 = XOBNEW (&objfile->objfile_obstack,
14406 struct dwarf2_locexpr_baton);
14407 dlbaton->data = DW_BLOCK (attr)->data;
14408 dlbaton->size = DW_BLOCK (attr)->size;
14409 /* When using this baton, we want to compute the address
14410 of the field, not the value. This is why
14411 is_reference is set to false here. */
14412 dlbaton->is_reference = false;
14413 dlbaton->per_objfile = per_objfile;
14414 dlbaton->per_cu = cu->per_cu;
14415
14416 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14417 }
14418 }
14419 else
14420 dwarf2_complex_location_expr_complaint ();
14421 }
14422 }
14423
14424 /* Add an aggregate field to the field list. */
14425
14426 static void
14427 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14428 struct dwarf2_cu *cu)
14429 {
14430 struct objfile *objfile = cu->per_objfile->objfile;
14431 struct gdbarch *gdbarch = objfile->arch ();
14432 struct nextfield *new_field;
14433 struct attribute *attr;
14434 struct field *fp;
14435 const char *fieldname = "";
14436
14437 if (die->tag == DW_TAG_inheritance)
14438 {
14439 fip->baseclasses.emplace_back ();
14440 new_field = &fip->baseclasses.back ();
14441 }
14442 else
14443 {
14444 fip->fields.emplace_back ();
14445 new_field = &fip->fields.back ();
14446 }
14447
14448 new_field->offset = die->sect_off;
14449
14450 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14451 if (attr != nullptr)
14452 new_field->accessibility = DW_UNSND (attr);
14453 else
14454 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14455 if (new_field->accessibility != DW_ACCESS_public)
14456 fip->non_public_fields = 1;
14457
14458 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14459 if (attr != nullptr)
14460 new_field->virtuality = DW_UNSND (attr);
14461 else
14462 new_field->virtuality = DW_VIRTUALITY_none;
14463
14464 fp = &new_field->field;
14465
14466 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14467 {
14468 /* Data member other than a C++ static data member. */
14469
14470 /* Get type of field. */
14471 fp->type = die_type (die, cu);
14472
14473 SET_FIELD_BITPOS (*fp, 0);
14474
14475 /* Get bit size of field (zero if none). */
14476 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14477 if (attr != nullptr)
14478 {
14479 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14480 }
14481 else
14482 {
14483 FIELD_BITSIZE (*fp) = 0;
14484 }
14485
14486 /* Get bit offset of field. */
14487 handle_data_member_location (die, cu, fp);
14488 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14489 if (attr != nullptr)
14490 {
14491 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14492 {
14493 /* For big endian bits, the DW_AT_bit_offset gives the
14494 additional bit offset from the MSB of the containing
14495 anonymous object to the MSB of the field. We don't
14496 have to do anything special since we don't need to
14497 know the size of the anonymous object. */
14498 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14499 }
14500 else
14501 {
14502 /* For little endian bits, compute the bit offset to the
14503 MSB of the anonymous object, subtract off the number of
14504 bits from the MSB of the field to the MSB of the
14505 object, and then subtract off the number of bits of
14506 the field itself. The result is the bit offset of
14507 the LSB of the field. */
14508 int anonymous_size;
14509 int bit_offset = DW_UNSND (attr);
14510
14511 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14512 if (attr != nullptr)
14513 {
14514 /* The size of the anonymous object containing
14515 the bit field is explicit, so use the
14516 indicated size (in bytes). */
14517 anonymous_size = DW_UNSND (attr);
14518 }
14519 else
14520 {
14521 /* The size of the anonymous object containing
14522 the bit field must be inferred from the type
14523 attribute of the data member containing the
14524 bit field. */
14525 anonymous_size = TYPE_LENGTH (fp->type);
14526 }
14527 SET_FIELD_BITPOS (*fp,
14528 (FIELD_BITPOS (*fp)
14529 + anonymous_size * bits_per_byte
14530 - bit_offset - FIELD_BITSIZE (*fp)));
14531 }
14532 }
14533 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14534 if (attr != NULL)
14535 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14536 + attr->constant_value (0)));
14537
14538 /* Get name of field. */
14539 fieldname = dwarf2_name (die, cu);
14540 if (fieldname == NULL)
14541 fieldname = "";
14542
14543 /* The name is already allocated along with this objfile, so we don't
14544 need to duplicate it for the type. */
14545 fp->name = fieldname;
14546
14547 /* Change accessibility for artificial fields (e.g. virtual table
14548 pointer or virtual base class pointer) to private. */
14549 if (dwarf2_attr (die, DW_AT_artificial, cu))
14550 {
14551 FIELD_ARTIFICIAL (*fp) = 1;
14552 new_field->accessibility = DW_ACCESS_private;
14553 fip->non_public_fields = 1;
14554 }
14555 }
14556 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14557 {
14558 /* C++ static member. */
14559
14560 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14561 is a declaration, but all versions of G++ as of this writing
14562 (so through at least 3.2.1) incorrectly generate
14563 DW_TAG_variable tags. */
14564
14565 const char *physname;
14566
14567 /* Get name of field. */
14568 fieldname = dwarf2_name (die, cu);
14569 if (fieldname == NULL)
14570 return;
14571
14572 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14573 if (attr
14574 /* Only create a symbol if this is an external value.
14575 new_symbol checks this and puts the value in the global symbol
14576 table, which we want. If it is not external, new_symbol
14577 will try to put the value in cu->list_in_scope which is wrong. */
14578 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14579 {
14580 /* A static const member, not much different than an enum as far as
14581 we're concerned, except that we can support more types. */
14582 new_symbol (die, NULL, cu);
14583 }
14584
14585 /* Get physical name. */
14586 physname = dwarf2_physname (fieldname, die, cu);
14587
14588 /* The name is already allocated along with this objfile, so we don't
14589 need to duplicate it for the type. */
14590 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14591 FIELD_TYPE (*fp) = die_type (die, cu);
14592 FIELD_NAME (*fp) = fieldname;
14593 }
14594 else if (die->tag == DW_TAG_inheritance)
14595 {
14596 /* C++ base class field. */
14597 handle_data_member_location (die, cu, fp);
14598 FIELD_BITSIZE (*fp) = 0;
14599 FIELD_TYPE (*fp) = die_type (die, cu);
14600 FIELD_NAME (*fp) = fp->type->name ();
14601 }
14602 else
14603 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14604 }
14605
14606 /* Can the type given by DIE define another type? */
14607
14608 static bool
14609 type_can_define_types (const struct die_info *die)
14610 {
14611 switch (die->tag)
14612 {
14613 case DW_TAG_typedef:
14614 case DW_TAG_class_type:
14615 case DW_TAG_structure_type:
14616 case DW_TAG_union_type:
14617 case DW_TAG_enumeration_type:
14618 return true;
14619
14620 default:
14621 return false;
14622 }
14623 }
14624
14625 /* Add a type definition defined in the scope of the FIP's class. */
14626
14627 static void
14628 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14629 struct dwarf2_cu *cu)
14630 {
14631 struct decl_field fp;
14632 memset (&fp, 0, sizeof (fp));
14633
14634 gdb_assert (type_can_define_types (die));
14635
14636 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14637 fp.name = dwarf2_name (die, cu);
14638 fp.type = read_type_die (die, cu);
14639
14640 /* Save accessibility. */
14641 enum dwarf_access_attribute accessibility;
14642 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14643 if (attr != NULL)
14644 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14645 else
14646 accessibility = dwarf2_default_access_attribute (die, cu);
14647 switch (accessibility)
14648 {
14649 case DW_ACCESS_public:
14650 /* The assumed value if neither private nor protected. */
14651 break;
14652 case DW_ACCESS_private:
14653 fp.is_private = 1;
14654 break;
14655 case DW_ACCESS_protected:
14656 fp.is_protected = 1;
14657 break;
14658 default:
14659 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14660 }
14661
14662 if (die->tag == DW_TAG_typedef)
14663 fip->typedef_field_list.push_back (fp);
14664 else
14665 fip->nested_types_list.push_back (fp);
14666 }
14667
14668 /* A convenience typedef that's used when finding the discriminant
14669 field for a variant part. */
14670 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14671 offset_map_type;
14672
14673 /* Compute the discriminant range for a given variant. OBSTACK is
14674 where the results will be stored. VARIANT is the variant to
14675 process. IS_UNSIGNED indicates whether the discriminant is signed
14676 or unsigned. */
14677
14678 static const gdb::array_view<discriminant_range>
14679 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14680 bool is_unsigned)
14681 {
14682 std::vector<discriminant_range> ranges;
14683
14684 if (variant.default_branch)
14685 return {};
14686
14687 if (variant.discr_list_data == nullptr)
14688 {
14689 discriminant_range r
14690 = {variant.discriminant_value, variant.discriminant_value};
14691 ranges.push_back (r);
14692 }
14693 else
14694 {
14695 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14696 variant.discr_list_data->size);
14697 while (!data.empty ())
14698 {
14699 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14700 {
14701 complaint (_("invalid discriminant marker: %d"), data[0]);
14702 break;
14703 }
14704 bool is_range = data[0] == DW_DSC_range;
14705 data = data.slice (1);
14706
14707 ULONGEST low, high;
14708 unsigned int bytes_read;
14709
14710 if (data.empty ())
14711 {
14712 complaint (_("DW_AT_discr_list missing low value"));
14713 break;
14714 }
14715 if (is_unsigned)
14716 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14717 else
14718 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14719 &bytes_read);
14720 data = data.slice (bytes_read);
14721
14722 if (is_range)
14723 {
14724 if (data.empty ())
14725 {
14726 complaint (_("DW_AT_discr_list missing high value"));
14727 break;
14728 }
14729 if (is_unsigned)
14730 high = read_unsigned_leb128 (nullptr, data.data (),
14731 &bytes_read);
14732 else
14733 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14734 &bytes_read);
14735 data = data.slice (bytes_read);
14736 }
14737 else
14738 high = low;
14739
14740 ranges.push_back ({ low, high });
14741 }
14742 }
14743
14744 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14745 ranges.size ());
14746 std::copy (ranges.begin (), ranges.end (), result);
14747 return gdb::array_view<discriminant_range> (result, ranges.size ());
14748 }
14749
14750 static const gdb::array_view<variant_part> create_variant_parts
14751 (struct obstack *obstack,
14752 const offset_map_type &offset_map,
14753 struct field_info *fi,
14754 const std::vector<variant_part_builder> &variant_parts);
14755
14756 /* Fill in a "struct variant" for a given variant field. RESULT is
14757 the variant to fill in. OBSTACK is where any needed allocations
14758 will be done. OFFSET_MAP holds the mapping from section offsets to
14759 fields for the type. FI describes the fields of the type we're
14760 processing. FIELD is the variant field we're converting. */
14761
14762 static void
14763 create_one_variant (variant &result, struct obstack *obstack,
14764 const offset_map_type &offset_map,
14765 struct field_info *fi, const variant_field &field)
14766 {
14767 result.discriminants = convert_variant_range (obstack, field, false);
14768 result.first_field = field.first_field + fi->baseclasses.size ();
14769 result.last_field = field.last_field + fi->baseclasses.size ();
14770 result.parts = create_variant_parts (obstack, offset_map, fi,
14771 field.variant_parts);
14772 }
14773
14774 /* Fill in a "struct variant_part" for a given variant part. RESULT
14775 is the variant part to fill in. OBSTACK is where any needed
14776 allocations will be done. OFFSET_MAP holds the mapping from
14777 section offsets to fields for the type. FI describes the fields of
14778 the type we're processing. BUILDER is the variant part to be
14779 converted. */
14780
14781 static void
14782 create_one_variant_part (variant_part &result,
14783 struct obstack *obstack,
14784 const offset_map_type &offset_map,
14785 struct field_info *fi,
14786 const variant_part_builder &builder)
14787 {
14788 auto iter = offset_map.find (builder.discriminant_offset);
14789 if (iter == offset_map.end ())
14790 {
14791 result.discriminant_index = -1;
14792 /* Doesn't matter. */
14793 result.is_unsigned = false;
14794 }
14795 else
14796 {
14797 result.discriminant_index = iter->second;
14798 result.is_unsigned
14799 = TYPE_UNSIGNED (FIELD_TYPE
14800 (fi->fields[result.discriminant_index].field));
14801 }
14802
14803 size_t n = builder.variants.size ();
14804 variant *output = new (obstack) variant[n];
14805 for (size_t i = 0; i < n; ++i)
14806 create_one_variant (output[i], obstack, offset_map, fi,
14807 builder.variants[i]);
14808
14809 result.variants = gdb::array_view<variant> (output, n);
14810 }
14811
14812 /* Create a vector of variant parts that can be attached to a type.
14813 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14814 holds the mapping from section offsets to fields for the type. FI
14815 describes the fields of the type we're processing. VARIANT_PARTS
14816 is the vector to convert. */
14817
14818 static const gdb::array_view<variant_part>
14819 create_variant_parts (struct obstack *obstack,
14820 const offset_map_type &offset_map,
14821 struct field_info *fi,
14822 const std::vector<variant_part_builder> &variant_parts)
14823 {
14824 if (variant_parts.empty ())
14825 return {};
14826
14827 size_t n = variant_parts.size ();
14828 variant_part *result = new (obstack) variant_part[n];
14829 for (size_t i = 0; i < n; ++i)
14830 create_one_variant_part (result[i], obstack, offset_map, fi,
14831 variant_parts[i]);
14832
14833 return gdb::array_view<variant_part> (result, n);
14834 }
14835
14836 /* Compute the variant part vector for FIP, attaching it to TYPE when
14837 done. */
14838
14839 static void
14840 add_variant_property (struct field_info *fip, struct type *type,
14841 struct dwarf2_cu *cu)
14842 {
14843 /* Map section offsets of fields to their field index. Note the
14844 field index here does not take the number of baseclasses into
14845 account. */
14846 offset_map_type offset_map;
14847 for (int i = 0; i < fip->fields.size (); ++i)
14848 offset_map[fip->fields[i].offset] = i;
14849
14850 struct objfile *objfile = cu->per_objfile->objfile;
14851 gdb::array_view<variant_part> parts
14852 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14853 fip->variant_parts);
14854
14855 struct dynamic_prop prop;
14856 prop.kind = PROP_VARIANT_PARTS;
14857 prop.data.variant_parts
14858 = ((gdb::array_view<variant_part> *)
14859 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14860
14861 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14862 }
14863
14864 /* Create the vector of fields, and attach it to the type. */
14865
14866 static void
14867 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14868 struct dwarf2_cu *cu)
14869 {
14870 int nfields = fip->nfields ();
14871
14872 /* Record the field count, allocate space for the array of fields,
14873 and create blank accessibility bitfields if necessary. */
14874 type->set_num_fields (nfields);
14875 type->set_fields
14876 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14877
14878 if (fip->non_public_fields && cu->language != language_ada)
14879 {
14880 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14881
14882 TYPE_FIELD_PRIVATE_BITS (type) =
14883 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14884 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14885
14886 TYPE_FIELD_PROTECTED_BITS (type) =
14887 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14888 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14889
14890 TYPE_FIELD_IGNORE_BITS (type) =
14891 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14892 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14893 }
14894
14895 /* If the type has baseclasses, allocate and clear a bit vector for
14896 TYPE_FIELD_VIRTUAL_BITS. */
14897 if (!fip->baseclasses.empty () && cu->language != language_ada)
14898 {
14899 int num_bytes = B_BYTES (fip->baseclasses.size ());
14900 unsigned char *pointer;
14901
14902 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14903 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14904 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14905 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14906 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14907 }
14908
14909 if (!fip->variant_parts.empty ())
14910 add_variant_property (fip, type, cu);
14911
14912 /* Copy the saved-up fields into the field vector. */
14913 for (int i = 0; i < nfields; ++i)
14914 {
14915 struct nextfield &field
14916 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14917 : fip->fields[i - fip->baseclasses.size ()]);
14918
14919 type->field (i) = field.field;
14920 switch (field.accessibility)
14921 {
14922 case DW_ACCESS_private:
14923 if (cu->language != language_ada)
14924 SET_TYPE_FIELD_PRIVATE (type, i);
14925 break;
14926
14927 case DW_ACCESS_protected:
14928 if (cu->language != language_ada)
14929 SET_TYPE_FIELD_PROTECTED (type, i);
14930 break;
14931
14932 case DW_ACCESS_public:
14933 break;
14934
14935 default:
14936 /* Unknown accessibility. Complain and treat it as public. */
14937 {
14938 complaint (_("unsupported accessibility %d"),
14939 field.accessibility);
14940 }
14941 break;
14942 }
14943 if (i < fip->baseclasses.size ())
14944 {
14945 switch (field.virtuality)
14946 {
14947 case DW_VIRTUALITY_virtual:
14948 case DW_VIRTUALITY_pure_virtual:
14949 if (cu->language == language_ada)
14950 error (_("unexpected virtuality in component of Ada type"));
14951 SET_TYPE_FIELD_VIRTUAL (type, i);
14952 break;
14953 }
14954 }
14955 }
14956 }
14957
14958 /* Return true if this member function is a constructor, false
14959 otherwise. */
14960
14961 static int
14962 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14963 {
14964 const char *fieldname;
14965 const char *type_name;
14966 int len;
14967
14968 if (die->parent == NULL)
14969 return 0;
14970
14971 if (die->parent->tag != DW_TAG_structure_type
14972 && die->parent->tag != DW_TAG_union_type
14973 && die->parent->tag != DW_TAG_class_type)
14974 return 0;
14975
14976 fieldname = dwarf2_name (die, cu);
14977 type_name = dwarf2_name (die->parent, cu);
14978 if (fieldname == NULL || type_name == NULL)
14979 return 0;
14980
14981 len = strlen (fieldname);
14982 return (strncmp (fieldname, type_name, len) == 0
14983 && (type_name[len] == '\0' || type_name[len] == '<'));
14984 }
14985
14986 /* Check if the given VALUE is a recognized enum
14987 dwarf_defaulted_attribute constant according to DWARF5 spec,
14988 Table 7.24. */
14989
14990 static bool
14991 is_valid_DW_AT_defaulted (ULONGEST value)
14992 {
14993 switch (value)
14994 {
14995 case DW_DEFAULTED_no:
14996 case DW_DEFAULTED_in_class:
14997 case DW_DEFAULTED_out_of_class:
14998 return true;
14999 }
15000
15001 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15002 return false;
15003 }
15004
15005 /* Add a member function to the proper fieldlist. */
15006
15007 static void
15008 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15009 struct type *type, struct dwarf2_cu *cu)
15010 {
15011 struct objfile *objfile = cu->per_objfile->objfile;
15012 struct attribute *attr;
15013 int i;
15014 struct fnfieldlist *flp = nullptr;
15015 struct fn_field *fnp;
15016 const char *fieldname;
15017 struct type *this_type;
15018 enum dwarf_access_attribute accessibility;
15019
15020 if (cu->language == language_ada)
15021 error (_("unexpected member function in Ada type"));
15022
15023 /* Get name of member function. */
15024 fieldname = dwarf2_name (die, cu);
15025 if (fieldname == NULL)
15026 return;
15027
15028 /* Look up member function name in fieldlist. */
15029 for (i = 0; i < fip->fnfieldlists.size (); i++)
15030 {
15031 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15032 {
15033 flp = &fip->fnfieldlists[i];
15034 break;
15035 }
15036 }
15037
15038 /* Create a new fnfieldlist if necessary. */
15039 if (flp == nullptr)
15040 {
15041 fip->fnfieldlists.emplace_back ();
15042 flp = &fip->fnfieldlists.back ();
15043 flp->name = fieldname;
15044 i = fip->fnfieldlists.size () - 1;
15045 }
15046
15047 /* Create a new member function field and add it to the vector of
15048 fnfieldlists. */
15049 flp->fnfields.emplace_back ();
15050 fnp = &flp->fnfields.back ();
15051
15052 /* Delay processing of the physname until later. */
15053 if (cu->language == language_cplus)
15054 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15055 die, cu);
15056 else
15057 {
15058 const char *physname = dwarf2_physname (fieldname, die, cu);
15059 fnp->physname = physname ? physname : "";
15060 }
15061
15062 fnp->type = alloc_type (objfile);
15063 this_type = read_type_die (die, cu);
15064 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15065 {
15066 int nparams = this_type->num_fields ();
15067
15068 /* TYPE is the domain of this method, and THIS_TYPE is the type
15069 of the method itself (TYPE_CODE_METHOD). */
15070 smash_to_method_type (fnp->type, type,
15071 TYPE_TARGET_TYPE (this_type),
15072 this_type->fields (),
15073 this_type->num_fields (),
15074 TYPE_VARARGS (this_type));
15075
15076 /* Handle static member functions.
15077 Dwarf2 has no clean way to discern C++ static and non-static
15078 member functions. G++ helps GDB by marking the first
15079 parameter for non-static member functions (which is the this
15080 pointer) as artificial. We obtain this information from
15081 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15082 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15083 fnp->voffset = VOFFSET_STATIC;
15084 }
15085 else
15086 complaint (_("member function type missing for '%s'"),
15087 dwarf2_full_name (fieldname, die, cu));
15088
15089 /* Get fcontext from DW_AT_containing_type if present. */
15090 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15091 fnp->fcontext = die_containing_type (die, cu);
15092
15093 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15094 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15095
15096 /* Get accessibility. */
15097 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15098 if (attr != nullptr)
15099 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15100 else
15101 accessibility = dwarf2_default_access_attribute (die, cu);
15102 switch (accessibility)
15103 {
15104 case DW_ACCESS_private:
15105 fnp->is_private = 1;
15106 break;
15107 case DW_ACCESS_protected:
15108 fnp->is_protected = 1;
15109 break;
15110 }
15111
15112 /* Check for artificial methods. */
15113 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15114 if (attr && DW_UNSND (attr) != 0)
15115 fnp->is_artificial = 1;
15116
15117 /* Check for defaulted methods. */
15118 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15119 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15120 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15121
15122 /* Check for deleted methods. */
15123 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15124 if (attr != nullptr && DW_UNSND (attr) != 0)
15125 fnp->is_deleted = 1;
15126
15127 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15128
15129 /* Get index in virtual function table if it is a virtual member
15130 function. For older versions of GCC, this is an offset in the
15131 appropriate virtual table, as specified by DW_AT_containing_type.
15132 For everyone else, it is an expression to be evaluated relative
15133 to the object address. */
15134
15135 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15136 if (attr != nullptr)
15137 {
15138 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15139 {
15140 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15141 {
15142 /* Old-style GCC. */
15143 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15144 }
15145 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15146 || (DW_BLOCK (attr)->size > 1
15147 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15148 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15149 {
15150 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15151 if ((fnp->voffset % cu->header.addr_size) != 0)
15152 dwarf2_complex_location_expr_complaint ();
15153 else
15154 fnp->voffset /= cu->header.addr_size;
15155 fnp->voffset += 2;
15156 }
15157 else
15158 dwarf2_complex_location_expr_complaint ();
15159
15160 if (!fnp->fcontext)
15161 {
15162 /* If there is no `this' field and no DW_AT_containing_type,
15163 we cannot actually find a base class context for the
15164 vtable! */
15165 if (this_type->num_fields () == 0
15166 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15167 {
15168 complaint (_("cannot determine context for virtual member "
15169 "function \"%s\" (offset %s)"),
15170 fieldname, sect_offset_str (die->sect_off));
15171 }
15172 else
15173 {
15174 fnp->fcontext
15175 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15176 }
15177 }
15178 }
15179 else if (attr->form_is_section_offset ())
15180 {
15181 dwarf2_complex_location_expr_complaint ();
15182 }
15183 else
15184 {
15185 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15186 fieldname);
15187 }
15188 }
15189 else
15190 {
15191 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15192 if (attr && DW_UNSND (attr))
15193 {
15194 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15195 complaint (_("Member function \"%s\" (offset %s) is virtual "
15196 "but the vtable offset is not specified"),
15197 fieldname, sect_offset_str (die->sect_off));
15198 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15199 TYPE_CPLUS_DYNAMIC (type) = 1;
15200 }
15201 }
15202 }
15203
15204 /* Create the vector of member function fields, and attach it to the type. */
15205
15206 static void
15207 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15208 struct dwarf2_cu *cu)
15209 {
15210 if (cu->language == language_ada)
15211 error (_("unexpected member functions in Ada type"));
15212
15213 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15214 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15215 TYPE_ALLOC (type,
15216 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15217
15218 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15219 {
15220 struct fnfieldlist &nf = fip->fnfieldlists[i];
15221 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15222
15223 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15224 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15225 fn_flp->fn_fields = (struct fn_field *)
15226 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15227
15228 for (int k = 0; k < nf.fnfields.size (); ++k)
15229 fn_flp->fn_fields[k] = nf.fnfields[k];
15230 }
15231
15232 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15233 }
15234
15235 /* Returns non-zero if NAME is the name of a vtable member in CU's
15236 language, zero otherwise. */
15237 static int
15238 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15239 {
15240 static const char vptr[] = "_vptr";
15241
15242 /* Look for the C++ form of the vtable. */
15243 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15244 return 1;
15245
15246 return 0;
15247 }
15248
15249 /* GCC outputs unnamed structures that are really pointers to member
15250 functions, with the ABI-specified layout. If TYPE describes
15251 such a structure, smash it into a member function type.
15252
15253 GCC shouldn't do this; it should just output pointer to member DIEs.
15254 This is GCC PR debug/28767. */
15255
15256 static void
15257 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15258 {
15259 struct type *pfn_type, *self_type, *new_type;
15260
15261 /* Check for a structure with no name and two children. */
15262 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15263 return;
15264
15265 /* Check for __pfn and __delta members. */
15266 if (TYPE_FIELD_NAME (type, 0) == NULL
15267 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15268 || TYPE_FIELD_NAME (type, 1) == NULL
15269 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15270 return;
15271
15272 /* Find the type of the method. */
15273 pfn_type = TYPE_FIELD_TYPE (type, 0);
15274 if (pfn_type == NULL
15275 || pfn_type->code () != TYPE_CODE_PTR
15276 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15277 return;
15278
15279 /* Look for the "this" argument. */
15280 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15281 if (pfn_type->num_fields () == 0
15282 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15283 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15284 return;
15285
15286 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15287 new_type = alloc_type (objfile);
15288 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15289 pfn_type->fields (), pfn_type->num_fields (),
15290 TYPE_VARARGS (pfn_type));
15291 smash_to_methodptr_type (type, new_type);
15292 }
15293
15294 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15295 appropriate error checking and issuing complaints if there is a
15296 problem. */
15297
15298 static ULONGEST
15299 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15300 {
15301 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15302
15303 if (attr == nullptr)
15304 return 0;
15305
15306 if (!attr->form_is_constant ())
15307 {
15308 complaint (_("DW_AT_alignment must have constant form"
15309 " - DIE at %s [in module %s]"),
15310 sect_offset_str (die->sect_off),
15311 objfile_name (cu->per_objfile->objfile));
15312 return 0;
15313 }
15314
15315 ULONGEST align;
15316 if (attr->form == DW_FORM_sdata)
15317 {
15318 LONGEST val = DW_SND (attr);
15319 if (val < 0)
15320 {
15321 complaint (_("DW_AT_alignment value must not be negative"
15322 " - DIE at %s [in module %s]"),
15323 sect_offset_str (die->sect_off),
15324 objfile_name (cu->per_objfile->objfile));
15325 return 0;
15326 }
15327 align = val;
15328 }
15329 else
15330 align = DW_UNSND (attr);
15331
15332 if (align == 0)
15333 {
15334 complaint (_("DW_AT_alignment value must not be zero"
15335 " - DIE at %s [in module %s]"),
15336 sect_offset_str (die->sect_off),
15337 objfile_name (cu->per_objfile->objfile));
15338 return 0;
15339 }
15340 if ((align & (align - 1)) != 0)
15341 {
15342 complaint (_("DW_AT_alignment value must be a power of 2"
15343 " - DIE at %s [in module %s]"),
15344 sect_offset_str (die->sect_off),
15345 objfile_name (cu->per_objfile->objfile));
15346 return 0;
15347 }
15348
15349 return align;
15350 }
15351
15352 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15353 the alignment for TYPE. */
15354
15355 static void
15356 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15357 struct type *type)
15358 {
15359 if (!set_type_align (type, get_alignment (cu, die)))
15360 complaint (_("DW_AT_alignment value too large"
15361 " - DIE at %s [in module %s]"),
15362 sect_offset_str (die->sect_off),
15363 objfile_name (cu->per_objfile->objfile));
15364 }
15365
15366 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15367 constant for a type, according to DWARF5 spec, Table 5.5. */
15368
15369 static bool
15370 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15371 {
15372 switch (value)
15373 {
15374 case DW_CC_normal:
15375 case DW_CC_pass_by_reference:
15376 case DW_CC_pass_by_value:
15377 return true;
15378
15379 default:
15380 complaint (_("unrecognized DW_AT_calling_convention value "
15381 "(%s) for a type"), pulongest (value));
15382 return false;
15383 }
15384 }
15385
15386 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15387 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15388 also according to GNU-specific values (see include/dwarf2.h). */
15389
15390 static bool
15391 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15392 {
15393 switch (value)
15394 {
15395 case DW_CC_normal:
15396 case DW_CC_program:
15397 case DW_CC_nocall:
15398 return true;
15399
15400 case DW_CC_GNU_renesas_sh:
15401 case DW_CC_GNU_borland_fastcall_i386:
15402 case DW_CC_GDB_IBM_OpenCL:
15403 return true;
15404
15405 default:
15406 complaint (_("unrecognized DW_AT_calling_convention value "
15407 "(%s) for a subroutine"), pulongest (value));
15408 return false;
15409 }
15410 }
15411
15412 /* Called when we find the DIE that starts a structure or union scope
15413 (definition) to create a type for the structure or union. Fill in
15414 the type's name and general properties; the members will not be
15415 processed until process_structure_scope. A symbol table entry for
15416 the type will also not be done until process_structure_scope (assuming
15417 the type has a name).
15418
15419 NOTE: we need to call these functions regardless of whether or not the
15420 DIE has a DW_AT_name attribute, since it might be an anonymous
15421 structure or union. This gets the type entered into our set of
15422 user defined types. */
15423
15424 static struct type *
15425 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15426 {
15427 struct objfile *objfile = cu->per_objfile->objfile;
15428 struct type *type;
15429 struct attribute *attr;
15430 const char *name;
15431
15432 /* If the definition of this type lives in .debug_types, read that type.
15433 Don't follow DW_AT_specification though, that will take us back up
15434 the chain and we want to go down. */
15435 attr = die->attr (DW_AT_signature);
15436 if (attr != nullptr)
15437 {
15438 type = get_DW_AT_signature_type (die, attr, cu);
15439
15440 /* The type's CU may not be the same as CU.
15441 Ensure TYPE is recorded with CU in die_type_hash. */
15442 return set_die_type (die, type, cu);
15443 }
15444
15445 type = alloc_type (objfile);
15446 INIT_CPLUS_SPECIFIC (type);
15447
15448 name = dwarf2_name (die, cu);
15449 if (name != NULL)
15450 {
15451 if (cu->language == language_cplus
15452 || cu->language == language_d
15453 || cu->language == language_rust)
15454 {
15455 const char *full_name = dwarf2_full_name (name, die, cu);
15456
15457 /* dwarf2_full_name might have already finished building the DIE's
15458 type. If so, there is no need to continue. */
15459 if (get_die_type (die, cu) != NULL)
15460 return get_die_type (die, cu);
15461
15462 type->set_name (full_name);
15463 }
15464 else
15465 {
15466 /* The name is already allocated along with this objfile, so
15467 we don't need to duplicate it for the type. */
15468 type->set_name (name);
15469 }
15470 }
15471
15472 if (die->tag == DW_TAG_structure_type)
15473 {
15474 type->set_code (TYPE_CODE_STRUCT);
15475 }
15476 else if (die->tag == DW_TAG_union_type)
15477 {
15478 type->set_code (TYPE_CODE_UNION);
15479 }
15480 else
15481 {
15482 type->set_code (TYPE_CODE_STRUCT);
15483 }
15484
15485 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15486 TYPE_DECLARED_CLASS (type) = 1;
15487
15488 /* Store the calling convention in the type if it's available in
15489 the die. Otherwise the calling convention remains set to
15490 the default value DW_CC_normal. */
15491 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15492 if (attr != nullptr
15493 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15494 {
15495 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15496 TYPE_CPLUS_CALLING_CONVENTION (type)
15497 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15498 }
15499
15500 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15501 if (attr != nullptr)
15502 {
15503 if (attr->form_is_constant ())
15504 TYPE_LENGTH (type) = DW_UNSND (attr);
15505 else
15506 {
15507 struct dynamic_prop prop;
15508 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15509 cu->per_cu->addr_type ()))
15510 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15511 TYPE_LENGTH (type) = 0;
15512 }
15513 }
15514 else
15515 {
15516 TYPE_LENGTH (type) = 0;
15517 }
15518
15519 maybe_set_alignment (cu, die, type);
15520
15521 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15522 {
15523 /* ICC<14 does not output the required DW_AT_declaration on
15524 incomplete types, but gives them a size of zero. */
15525 TYPE_STUB (type) = 1;
15526 }
15527 else
15528 TYPE_STUB_SUPPORTED (type) = 1;
15529
15530 if (die_is_declaration (die, cu))
15531 TYPE_STUB (type) = 1;
15532 else if (attr == NULL && die->child == NULL
15533 && producer_is_realview (cu->producer))
15534 /* RealView does not output the required DW_AT_declaration
15535 on incomplete types. */
15536 TYPE_STUB (type) = 1;
15537
15538 /* We need to add the type field to the die immediately so we don't
15539 infinitely recurse when dealing with pointers to the structure
15540 type within the structure itself. */
15541 set_die_type (die, type, cu);
15542
15543 /* set_die_type should be already done. */
15544 set_descriptive_type (type, die, cu);
15545
15546 return type;
15547 }
15548
15549 static void handle_struct_member_die
15550 (struct die_info *child_die,
15551 struct type *type,
15552 struct field_info *fi,
15553 std::vector<struct symbol *> *template_args,
15554 struct dwarf2_cu *cu);
15555
15556 /* A helper for handle_struct_member_die that handles
15557 DW_TAG_variant_part. */
15558
15559 static void
15560 handle_variant_part (struct die_info *die, struct type *type,
15561 struct field_info *fi,
15562 std::vector<struct symbol *> *template_args,
15563 struct dwarf2_cu *cu)
15564 {
15565 variant_part_builder *new_part;
15566 if (fi->current_variant_part == nullptr)
15567 {
15568 fi->variant_parts.emplace_back ();
15569 new_part = &fi->variant_parts.back ();
15570 }
15571 else if (!fi->current_variant_part->processing_variant)
15572 {
15573 complaint (_("nested DW_TAG_variant_part seen "
15574 "- DIE at %s [in module %s]"),
15575 sect_offset_str (die->sect_off),
15576 objfile_name (cu->per_objfile->objfile));
15577 return;
15578 }
15579 else
15580 {
15581 variant_field &current = fi->current_variant_part->variants.back ();
15582 current.variant_parts.emplace_back ();
15583 new_part = &current.variant_parts.back ();
15584 }
15585
15586 /* When we recurse, we want callees to add to this new variant
15587 part. */
15588 scoped_restore save_current_variant_part
15589 = make_scoped_restore (&fi->current_variant_part, new_part);
15590
15591 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15592 if (discr == NULL)
15593 {
15594 /* It's a univariant form, an extension we support. */
15595 }
15596 else if (discr->form_is_ref ())
15597 {
15598 struct dwarf2_cu *target_cu = cu;
15599 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15600
15601 new_part->discriminant_offset = target_die->sect_off;
15602 }
15603 else
15604 {
15605 complaint (_("DW_AT_discr does not have DIE reference form"
15606 " - DIE at %s [in module %s]"),
15607 sect_offset_str (die->sect_off),
15608 objfile_name (cu->per_objfile->objfile));
15609 }
15610
15611 for (die_info *child_die = die->child;
15612 child_die != NULL;
15613 child_die = child_die->sibling)
15614 handle_struct_member_die (child_die, type, fi, template_args, cu);
15615 }
15616
15617 /* A helper for handle_struct_member_die that handles
15618 DW_TAG_variant. */
15619
15620 static void
15621 handle_variant (struct die_info *die, struct type *type,
15622 struct field_info *fi,
15623 std::vector<struct symbol *> *template_args,
15624 struct dwarf2_cu *cu)
15625 {
15626 if (fi->current_variant_part == nullptr)
15627 {
15628 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15629 "- DIE at %s [in module %s]"),
15630 sect_offset_str (die->sect_off),
15631 objfile_name (cu->per_objfile->objfile));
15632 return;
15633 }
15634 if (fi->current_variant_part->processing_variant)
15635 {
15636 complaint (_("nested DW_TAG_variant seen "
15637 "- DIE at %s [in module %s]"),
15638 sect_offset_str (die->sect_off),
15639 objfile_name (cu->per_objfile->objfile));
15640 return;
15641 }
15642
15643 scoped_restore save_processing_variant
15644 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15645 true);
15646
15647 fi->current_variant_part->variants.emplace_back ();
15648 variant_field &variant = fi->current_variant_part->variants.back ();
15649 variant.first_field = fi->fields.size ();
15650
15651 /* In a variant we want to get the discriminant and also add a
15652 field for our sole member child. */
15653 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15654 if (discr == nullptr)
15655 {
15656 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15657 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15658 variant.default_branch = true;
15659 else
15660 variant.discr_list_data = DW_BLOCK (discr);
15661 }
15662 else
15663 variant.discriminant_value = DW_UNSND (discr);
15664
15665 for (die_info *variant_child = die->child;
15666 variant_child != NULL;
15667 variant_child = variant_child->sibling)
15668 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15669
15670 variant.last_field = fi->fields.size ();
15671 }
15672
15673 /* A helper for process_structure_scope that handles a single member
15674 DIE. */
15675
15676 static void
15677 handle_struct_member_die (struct die_info *child_die, struct type *type,
15678 struct field_info *fi,
15679 std::vector<struct symbol *> *template_args,
15680 struct dwarf2_cu *cu)
15681 {
15682 if (child_die->tag == DW_TAG_member
15683 || child_die->tag == DW_TAG_variable)
15684 {
15685 /* NOTE: carlton/2002-11-05: A C++ static data member
15686 should be a DW_TAG_member that is a declaration, but
15687 all versions of G++ as of this writing (so through at
15688 least 3.2.1) incorrectly generate DW_TAG_variable
15689 tags for them instead. */
15690 dwarf2_add_field (fi, child_die, cu);
15691 }
15692 else if (child_die->tag == DW_TAG_subprogram)
15693 {
15694 /* Rust doesn't have member functions in the C++ sense.
15695 However, it does emit ordinary functions as children
15696 of a struct DIE. */
15697 if (cu->language == language_rust)
15698 read_func_scope (child_die, cu);
15699 else
15700 {
15701 /* C++ member function. */
15702 dwarf2_add_member_fn (fi, child_die, type, cu);
15703 }
15704 }
15705 else if (child_die->tag == DW_TAG_inheritance)
15706 {
15707 /* C++ base class field. */
15708 dwarf2_add_field (fi, child_die, cu);
15709 }
15710 else if (type_can_define_types (child_die))
15711 dwarf2_add_type_defn (fi, child_die, cu);
15712 else if (child_die->tag == DW_TAG_template_type_param
15713 || child_die->tag == DW_TAG_template_value_param)
15714 {
15715 struct symbol *arg = new_symbol (child_die, NULL, cu);
15716
15717 if (arg != NULL)
15718 template_args->push_back (arg);
15719 }
15720 else if (child_die->tag == DW_TAG_variant_part)
15721 handle_variant_part (child_die, type, fi, template_args, cu);
15722 else if (child_die->tag == DW_TAG_variant)
15723 handle_variant (child_die, type, fi, template_args, cu);
15724 }
15725
15726 /* Finish creating a structure or union type, including filling in
15727 its members and creating a symbol for it. */
15728
15729 static void
15730 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15731 {
15732 struct objfile *objfile = cu->per_objfile->objfile;
15733 struct die_info *child_die;
15734 struct type *type;
15735
15736 type = get_die_type (die, cu);
15737 if (type == NULL)
15738 type = read_structure_type (die, cu);
15739
15740 bool has_template_parameters = false;
15741 if (die->child != NULL && ! die_is_declaration (die, cu))
15742 {
15743 struct field_info fi;
15744 std::vector<struct symbol *> template_args;
15745
15746 child_die = die->child;
15747
15748 while (child_die && child_die->tag)
15749 {
15750 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15751 child_die = child_die->sibling;
15752 }
15753
15754 /* Attach template arguments to type. */
15755 if (!template_args.empty ())
15756 {
15757 has_template_parameters = true;
15758 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15759 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15760 TYPE_TEMPLATE_ARGUMENTS (type)
15761 = XOBNEWVEC (&objfile->objfile_obstack,
15762 struct symbol *,
15763 TYPE_N_TEMPLATE_ARGUMENTS (type));
15764 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15765 template_args.data (),
15766 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15767 * sizeof (struct symbol *)));
15768 }
15769
15770 /* Attach fields and member functions to the type. */
15771 if (fi.nfields () > 0)
15772 dwarf2_attach_fields_to_type (&fi, type, cu);
15773 if (!fi.fnfieldlists.empty ())
15774 {
15775 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15776
15777 /* Get the type which refers to the base class (possibly this
15778 class itself) which contains the vtable pointer for the current
15779 class from the DW_AT_containing_type attribute. This use of
15780 DW_AT_containing_type is a GNU extension. */
15781
15782 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15783 {
15784 struct type *t = die_containing_type (die, cu);
15785
15786 set_type_vptr_basetype (type, t);
15787 if (type == t)
15788 {
15789 int i;
15790
15791 /* Our own class provides vtbl ptr. */
15792 for (i = t->num_fields () - 1;
15793 i >= TYPE_N_BASECLASSES (t);
15794 --i)
15795 {
15796 const char *fieldname = TYPE_FIELD_NAME (t, i);
15797
15798 if (is_vtable_name (fieldname, cu))
15799 {
15800 set_type_vptr_fieldno (type, i);
15801 break;
15802 }
15803 }
15804
15805 /* Complain if virtual function table field not found. */
15806 if (i < TYPE_N_BASECLASSES (t))
15807 complaint (_("virtual function table pointer "
15808 "not found when defining class '%s'"),
15809 type->name () ? type->name () : "");
15810 }
15811 else
15812 {
15813 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15814 }
15815 }
15816 else if (cu->producer
15817 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15818 {
15819 /* The IBM XLC compiler does not provide direct indication
15820 of the containing type, but the vtable pointer is
15821 always named __vfp. */
15822
15823 int i;
15824
15825 for (i = type->num_fields () - 1;
15826 i >= TYPE_N_BASECLASSES (type);
15827 --i)
15828 {
15829 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15830 {
15831 set_type_vptr_fieldno (type, i);
15832 set_type_vptr_basetype (type, type);
15833 break;
15834 }
15835 }
15836 }
15837 }
15838
15839 /* Copy fi.typedef_field_list linked list elements content into the
15840 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15841 if (!fi.typedef_field_list.empty ())
15842 {
15843 int count = fi.typedef_field_list.size ();
15844
15845 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15846 TYPE_TYPEDEF_FIELD_ARRAY (type)
15847 = ((struct decl_field *)
15848 TYPE_ALLOC (type,
15849 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15850 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15851
15852 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15853 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15854 }
15855
15856 /* Copy fi.nested_types_list linked list elements content into the
15857 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15858 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15859 {
15860 int count = fi.nested_types_list.size ();
15861
15862 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15863 TYPE_NESTED_TYPES_ARRAY (type)
15864 = ((struct decl_field *)
15865 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15866 TYPE_NESTED_TYPES_COUNT (type) = count;
15867
15868 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15869 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15870 }
15871 }
15872
15873 quirk_gcc_member_function_pointer (type, objfile);
15874 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15875 cu->rust_unions.push_back (type);
15876
15877 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15878 snapshots) has been known to create a die giving a declaration
15879 for a class that has, as a child, a die giving a definition for a
15880 nested class. So we have to process our children even if the
15881 current die is a declaration. Normally, of course, a declaration
15882 won't have any children at all. */
15883
15884 child_die = die->child;
15885
15886 while (child_die != NULL && child_die->tag)
15887 {
15888 if (child_die->tag == DW_TAG_member
15889 || child_die->tag == DW_TAG_variable
15890 || child_die->tag == DW_TAG_inheritance
15891 || child_die->tag == DW_TAG_template_value_param
15892 || child_die->tag == DW_TAG_template_type_param)
15893 {
15894 /* Do nothing. */
15895 }
15896 else
15897 process_die (child_die, cu);
15898
15899 child_die = child_die->sibling;
15900 }
15901
15902 /* Do not consider external references. According to the DWARF standard,
15903 these DIEs are identified by the fact that they have no byte_size
15904 attribute, and a declaration attribute. */
15905 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15906 || !die_is_declaration (die, cu)
15907 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15908 {
15909 struct symbol *sym = new_symbol (die, type, cu);
15910
15911 if (has_template_parameters)
15912 {
15913 struct symtab *symtab;
15914 if (sym != nullptr)
15915 symtab = symbol_symtab (sym);
15916 else if (cu->line_header != nullptr)
15917 {
15918 /* Any related symtab will do. */
15919 symtab
15920 = cu->line_header->file_names ()[0].symtab;
15921 }
15922 else
15923 {
15924 symtab = nullptr;
15925 complaint (_("could not find suitable "
15926 "symtab for template parameter"
15927 " - DIE at %s [in module %s]"),
15928 sect_offset_str (die->sect_off),
15929 objfile_name (objfile));
15930 }
15931
15932 if (symtab != nullptr)
15933 {
15934 /* Make sure that the symtab is set on the new symbols.
15935 Even though they don't appear in this symtab directly,
15936 other parts of gdb assume that symbols do, and this is
15937 reasonably true. */
15938 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15939 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15940 }
15941 }
15942 }
15943 }
15944
15945 /* Assuming DIE is an enumeration type, and TYPE is its associated
15946 type, update TYPE using some information only available in DIE's
15947 children. In particular, the fields are computed. */
15948
15949 static void
15950 update_enumeration_type_from_children (struct die_info *die,
15951 struct type *type,
15952 struct dwarf2_cu *cu)
15953 {
15954 struct die_info *child_die;
15955 int unsigned_enum = 1;
15956 int flag_enum = 1;
15957
15958 auto_obstack obstack;
15959 std::vector<struct field> fields;
15960
15961 for (child_die = die->child;
15962 child_die != NULL && child_die->tag;
15963 child_die = child_die->sibling)
15964 {
15965 struct attribute *attr;
15966 LONGEST value;
15967 const gdb_byte *bytes;
15968 struct dwarf2_locexpr_baton *baton;
15969 const char *name;
15970
15971 if (child_die->tag != DW_TAG_enumerator)
15972 continue;
15973
15974 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15975 if (attr == NULL)
15976 continue;
15977
15978 name = dwarf2_name (child_die, cu);
15979 if (name == NULL)
15980 name = "<anonymous enumerator>";
15981
15982 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15983 &value, &bytes, &baton);
15984 if (value < 0)
15985 {
15986 unsigned_enum = 0;
15987 flag_enum = 0;
15988 }
15989 else
15990 {
15991 if (count_one_bits_ll (value) >= 2)
15992 flag_enum = 0;
15993 }
15994
15995 fields.emplace_back ();
15996 struct field &field = fields.back ();
15997 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
15998 SET_FIELD_ENUMVAL (field, value);
15999 }
16000
16001 if (!fields.empty ())
16002 {
16003 type->set_num_fields (fields.size ());
16004 type->set_fields
16005 ((struct field *)
16006 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16007 memcpy (type->fields (), fields.data (),
16008 sizeof (struct field) * fields.size ());
16009 }
16010
16011 if (unsigned_enum)
16012 TYPE_UNSIGNED (type) = 1;
16013 if (flag_enum)
16014 TYPE_FLAG_ENUM (type) = 1;
16015 }
16016
16017 /* Given a DW_AT_enumeration_type die, set its type. We do not
16018 complete the type's fields yet, or create any symbols. */
16019
16020 static struct type *
16021 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16022 {
16023 struct objfile *objfile = cu->per_objfile->objfile;
16024 struct type *type;
16025 struct attribute *attr;
16026 const char *name;
16027
16028 /* If the definition of this type lives in .debug_types, read that type.
16029 Don't follow DW_AT_specification though, that will take us back up
16030 the chain and we want to go down. */
16031 attr = die->attr (DW_AT_signature);
16032 if (attr != nullptr)
16033 {
16034 type = get_DW_AT_signature_type (die, attr, cu);
16035
16036 /* The type's CU may not be the same as CU.
16037 Ensure TYPE is recorded with CU in die_type_hash. */
16038 return set_die_type (die, type, cu);
16039 }
16040
16041 type = alloc_type (objfile);
16042
16043 type->set_code (TYPE_CODE_ENUM);
16044 name = dwarf2_full_name (NULL, die, cu);
16045 if (name != NULL)
16046 type->set_name (name);
16047
16048 attr = dwarf2_attr (die, DW_AT_type, cu);
16049 if (attr != NULL)
16050 {
16051 struct type *underlying_type = die_type (die, cu);
16052
16053 TYPE_TARGET_TYPE (type) = underlying_type;
16054 }
16055
16056 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16057 if (attr != nullptr)
16058 {
16059 TYPE_LENGTH (type) = DW_UNSND (attr);
16060 }
16061 else
16062 {
16063 TYPE_LENGTH (type) = 0;
16064 }
16065
16066 maybe_set_alignment (cu, die, type);
16067
16068 /* The enumeration DIE can be incomplete. In Ada, any type can be
16069 declared as private in the package spec, and then defined only
16070 inside the package body. Such types are known as Taft Amendment
16071 Types. When another package uses such a type, an incomplete DIE
16072 may be generated by the compiler. */
16073 if (die_is_declaration (die, cu))
16074 TYPE_STUB (type) = 1;
16075
16076 /* If this type has an underlying type that is not a stub, then we
16077 may use its attributes. We always use the "unsigned" attribute
16078 in this situation, because ordinarily we guess whether the type
16079 is unsigned -- but the guess can be wrong and the underlying type
16080 can tell us the reality. However, we defer to a local size
16081 attribute if one exists, because this lets the compiler override
16082 the underlying type if needed. */
16083 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16084 {
16085 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16086 underlying_type = check_typedef (underlying_type);
16087 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16088 if (TYPE_LENGTH (type) == 0)
16089 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16090 if (TYPE_RAW_ALIGN (type) == 0
16091 && TYPE_RAW_ALIGN (underlying_type) != 0)
16092 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16093 }
16094
16095 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16096
16097 set_die_type (die, type, cu);
16098
16099 /* Finish the creation of this type by using the enum's children.
16100 Note that, as usual, this must come after set_die_type to avoid
16101 infinite recursion when trying to compute the names of the
16102 enumerators. */
16103 update_enumeration_type_from_children (die, type, cu);
16104
16105 return type;
16106 }
16107
16108 /* Given a pointer to a die which begins an enumeration, process all
16109 the dies that define the members of the enumeration, and create the
16110 symbol for the enumeration type.
16111
16112 NOTE: We reverse the order of the element list. */
16113
16114 static void
16115 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16116 {
16117 struct type *this_type;
16118
16119 this_type = get_die_type (die, cu);
16120 if (this_type == NULL)
16121 this_type = read_enumeration_type (die, cu);
16122
16123 if (die->child != NULL)
16124 {
16125 struct die_info *child_die;
16126 const char *name;
16127
16128 child_die = die->child;
16129 while (child_die && child_die->tag)
16130 {
16131 if (child_die->tag != DW_TAG_enumerator)
16132 {
16133 process_die (child_die, cu);
16134 }
16135 else
16136 {
16137 name = dwarf2_name (child_die, cu);
16138 if (name)
16139 new_symbol (child_die, this_type, cu);
16140 }
16141
16142 child_die = child_die->sibling;
16143 }
16144 }
16145
16146 /* If we are reading an enum from a .debug_types unit, and the enum
16147 is a declaration, and the enum is not the signatured type in the
16148 unit, then we do not want to add a symbol for it. Adding a
16149 symbol would in some cases obscure the true definition of the
16150 enum, giving users an incomplete type when the definition is
16151 actually available. Note that we do not want to do this for all
16152 enums which are just declarations, because C++0x allows forward
16153 enum declarations. */
16154 if (cu->per_cu->is_debug_types
16155 && die_is_declaration (die, cu))
16156 {
16157 struct signatured_type *sig_type;
16158
16159 sig_type = (struct signatured_type *) cu->per_cu;
16160 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16161 if (sig_type->type_offset_in_section != die->sect_off)
16162 return;
16163 }
16164
16165 new_symbol (die, this_type, cu);
16166 }
16167
16168 /* Extract all information from a DW_TAG_array_type DIE and put it in
16169 the DIE's type field. For now, this only handles one dimensional
16170 arrays. */
16171
16172 static struct type *
16173 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16174 {
16175 struct objfile *objfile = cu->per_objfile->objfile;
16176 struct die_info *child_die;
16177 struct type *type;
16178 struct type *element_type, *range_type, *index_type;
16179 struct attribute *attr;
16180 const char *name;
16181 struct dynamic_prop *byte_stride_prop = NULL;
16182 unsigned int bit_stride = 0;
16183
16184 element_type = die_type (die, cu);
16185
16186 /* The die_type call above may have already set the type for this DIE. */
16187 type = get_die_type (die, cu);
16188 if (type)
16189 return type;
16190
16191 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16192 if (attr != NULL)
16193 {
16194 int stride_ok;
16195 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
16196
16197 byte_stride_prop
16198 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16199 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16200 prop_type);
16201 if (!stride_ok)
16202 {
16203 complaint (_("unable to read array DW_AT_byte_stride "
16204 " - DIE at %s [in module %s]"),
16205 sect_offset_str (die->sect_off),
16206 objfile_name (cu->per_objfile->objfile));
16207 /* Ignore this attribute. We will likely not be able to print
16208 arrays of this type correctly, but there is little we can do
16209 to help if we cannot read the attribute's value. */
16210 byte_stride_prop = NULL;
16211 }
16212 }
16213
16214 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16215 if (attr != NULL)
16216 bit_stride = DW_UNSND (attr);
16217
16218 /* Irix 6.2 native cc creates array types without children for
16219 arrays with unspecified length. */
16220 if (die->child == NULL)
16221 {
16222 index_type = objfile_type (objfile)->builtin_int;
16223 range_type = create_static_range_type (NULL, index_type, 0, -1);
16224 type = create_array_type_with_stride (NULL, element_type, range_type,
16225 byte_stride_prop, bit_stride);
16226 return set_die_type (die, type, cu);
16227 }
16228
16229 std::vector<struct type *> range_types;
16230 child_die = die->child;
16231 while (child_die && child_die->tag)
16232 {
16233 if (child_die->tag == DW_TAG_subrange_type)
16234 {
16235 struct type *child_type = read_type_die (child_die, cu);
16236
16237 if (child_type != NULL)
16238 {
16239 /* The range type was succesfully read. Save it for the
16240 array type creation. */
16241 range_types.push_back (child_type);
16242 }
16243 }
16244 child_die = child_die->sibling;
16245 }
16246
16247 /* Dwarf2 dimensions are output from left to right, create the
16248 necessary array types in backwards order. */
16249
16250 type = element_type;
16251
16252 if (read_array_order (die, cu) == DW_ORD_col_major)
16253 {
16254 int i = 0;
16255
16256 while (i < range_types.size ())
16257 type = create_array_type_with_stride (NULL, type, range_types[i++],
16258 byte_stride_prop, bit_stride);
16259 }
16260 else
16261 {
16262 size_t ndim = range_types.size ();
16263 while (ndim-- > 0)
16264 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16265 byte_stride_prop, bit_stride);
16266 }
16267
16268 /* Understand Dwarf2 support for vector types (like they occur on
16269 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16270 array type. This is not part of the Dwarf2/3 standard yet, but a
16271 custom vendor extension. The main difference between a regular
16272 array and the vector variant is that vectors are passed by value
16273 to functions. */
16274 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16275 if (attr != nullptr)
16276 make_vector_type (type);
16277
16278 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16279 implementation may choose to implement triple vectors using this
16280 attribute. */
16281 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16282 if (attr != nullptr)
16283 {
16284 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16285 TYPE_LENGTH (type) = DW_UNSND (attr);
16286 else
16287 complaint (_("DW_AT_byte_size for array type smaller "
16288 "than the total size of elements"));
16289 }
16290
16291 name = dwarf2_name (die, cu);
16292 if (name)
16293 type->set_name (name);
16294
16295 maybe_set_alignment (cu, die, type);
16296
16297 /* Install the type in the die. */
16298 set_die_type (die, type, cu);
16299
16300 /* set_die_type should be already done. */
16301 set_descriptive_type (type, die, cu);
16302
16303 return type;
16304 }
16305
16306 static enum dwarf_array_dim_ordering
16307 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16308 {
16309 struct attribute *attr;
16310
16311 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16312
16313 if (attr != nullptr)
16314 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16315
16316 /* GNU F77 is a special case, as at 08/2004 array type info is the
16317 opposite order to the dwarf2 specification, but data is still
16318 laid out as per normal fortran.
16319
16320 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16321 version checking. */
16322
16323 if (cu->language == language_fortran
16324 && cu->producer && strstr (cu->producer, "GNU F77"))
16325 {
16326 return DW_ORD_row_major;
16327 }
16328
16329 switch (cu->language_defn->la_array_ordering)
16330 {
16331 case array_column_major:
16332 return DW_ORD_col_major;
16333 case array_row_major:
16334 default:
16335 return DW_ORD_row_major;
16336 };
16337 }
16338
16339 /* Extract all information from a DW_TAG_set_type DIE and put it in
16340 the DIE's type field. */
16341
16342 static struct type *
16343 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16344 {
16345 struct type *domain_type, *set_type;
16346 struct attribute *attr;
16347
16348 domain_type = die_type (die, cu);
16349
16350 /* The die_type call above may have already set the type for this DIE. */
16351 set_type = get_die_type (die, cu);
16352 if (set_type)
16353 return set_type;
16354
16355 set_type = create_set_type (NULL, domain_type);
16356
16357 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16358 if (attr != nullptr)
16359 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16360
16361 maybe_set_alignment (cu, die, set_type);
16362
16363 return set_die_type (die, set_type, cu);
16364 }
16365
16366 /* A helper for read_common_block that creates a locexpr baton.
16367 SYM is the symbol which we are marking as computed.
16368 COMMON_DIE is the DIE for the common block.
16369 COMMON_LOC is the location expression attribute for the common
16370 block itself.
16371 MEMBER_LOC is the location expression attribute for the particular
16372 member of the common block that we are processing.
16373 CU is the CU from which the above come. */
16374
16375 static void
16376 mark_common_block_symbol_computed (struct symbol *sym,
16377 struct die_info *common_die,
16378 struct attribute *common_loc,
16379 struct attribute *member_loc,
16380 struct dwarf2_cu *cu)
16381 {
16382 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16383 struct objfile *objfile = per_objfile->objfile;
16384 struct dwarf2_locexpr_baton *baton;
16385 gdb_byte *ptr;
16386 unsigned int cu_off;
16387 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16388 LONGEST offset = 0;
16389
16390 gdb_assert (common_loc && member_loc);
16391 gdb_assert (common_loc->form_is_block ());
16392 gdb_assert (member_loc->form_is_block ()
16393 || member_loc->form_is_constant ());
16394
16395 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16396 baton->per_objfile = per_objfile;
16397 baton->per_cu = cu->per_cu;
16398 gdb_assert (baton->per_cu);
16399
16400 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16401
16402 if (member_loc->form_is_constant ())
16403 {
16404 offset = member_loc->constant_value (0);
16405 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16406 }
16407 else
16408 baton->size += DW_BLOCK (member_loc)->size;
16409
16410 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16411 baton->data = ptr;
16412
16413 *ptr++ = DW_OP_call4;
16414 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16415 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16416 ptr += 4;
16417
16418 if (member_loc->form_is_constant ())
16419 {
16420 *ptr++ = DW_OP_addr;
16421 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16422 ptr += cu->header.addr_size;
16423 }
16424 else
16425 {
16426 /* We have to copy the data here, because DW_OP_call4 will only
16427 use a DW_AT_location attribute. */
16428 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16429 ptr += DW_BLOCK (member_loc)->size;
16430 }
16431
16432 *ptr++ = DW_OP_plus;
16433 gdb_assert (ptr - baton->data == baton->size);
16434
16435 SYMBOL_LOCATION_BATON (sym) = baton;
16436 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16437 }
16438
16439 /* Create appropriate locally-scoped variables for all the
16440 DW_TAG_common_block entries. Also create a struct common_block
16441 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16442 is used to separate the common blocks name namespace from regular
16443 variable names. */
16444
16445 static void
16446 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16447 {
16448 struct attribute *attr;
16449
16450 attr = dwarf2_attr (die, DW_AT_location, cu);
16451 if (attr != nullptr)
16452 {
16453 /* Support the .debug_loc offsets. */
16454 if (attr->form_is_block ())
16455 {
16456 /* Ok. */
16457 }
16458 else if (attr->form_is_section_offset ())
16459 {
16460 dwarf2_complex_location_expr_complaint ();
16461 attr = NULL;
16462 }
16463 else
16464 {
16465 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16466 "common block member");
16467 attr = NULL;
16468 }
16469 }
16470
16471 if (die->child != NULL)
16472 {
16473 struct objfile *objfile = cu->per_objfile->objfile;
16474 struct die_info *child_die;
16475 size_t n_entries = 0, size;
16476 struct common_block *common_block;
16477 struct symbol *sym;
16478
16479 for (child_die = die->child;
16480 child_die && child_die->tag;
16481 child_die = child_die->sibling)
16482 ++n_entries;
16483
16484 size = (sizeof (struct common_block)
16485 + (n_entries - 1) * sizeof (struct symbol *));
16486 common_block
16487 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16488 size);
16489 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16490 common_block->n_entries = 0;
16491
16492 for (child_die = die->child;
16493 child_die && child_die->tag;
16494 child_die = child_die->sibling)
16495 {
16496 /* Create the symbol in the DW_TAG_common_block block in the current
16497 symbol scope. */
16498 sym = new_symbol (child_die, NULL, cu);
16499 if (sym != NULL)
16500 {
16501 struct attribute *member_loc;
16502
16503 common_block->contents[common_block->n_entries++] = sym;
16504
16505 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16506 cu);
16507 if (member_loc)
16508 {
16509 /* GDB has handled this for a long time, but it is
16510 not specified by DWARF. It seems to have been
16511 emitted by gfortran at least as recently as:
16512 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16513 complaint (_("Variable in common block has "
16514 "DW_AT_data_member_location "
16515 "- DIE at %s [in module %s]"),
16516 sect_offset_str (child_die->sect_off),
16517 objfile_name (objfile));
16518
16519 if (member_loc->form_is_section_offset ())
16520 dwarf2_complex_location_expr_complaint ();
16521 else if (member_loc->form_is_constant ()
16522 || member_loc->form_is_block ())
16523 {
16524 if (attr != nullptr)
16525 mark_common_block_symbol_computed (sym, die, attr,
16526 member_loc, cu);
16527 }
16528 else
16529 dwarf2_complex_location_expr_complaint ();
16530 }
16531 }
16532 }
16533
16534 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16535 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16536 }
16537 }
16538
16539 /* Create a type for a C++ namespace. */
16540
16541 static struct type *
16542 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16543 {
16544 struct objfile *objfile = cu->per_objfile->objfile;
16545 const char *previous_prefix, *name;
16546 int is_anonymous;
16547 struct type *type;
16548
16549 /* For extensions, reuse the type of the original namespace. */
16550 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16551 {
16552 struct die_info *ext_die;
16553 struct dwarf2_cu *ext_cu = cu;
16554
16555 ext_die = dwarf2_extension (die, &ext_cu);
16556 type = read_type_die (ext_die, ext_cu);
16557
16558 /* EXT_CU may not be the same as CU.
16559 Ensure TYPE is recorded with CU in die_type_hash. */
16560 return set_die_type (die, type, cu);
16561 }
16562
16563 name = namespace_name (die, &is_anonymous, cu);
16564
16565 /* Now build the name of the current namespace. */
16566
16567 previous_prefix = determine_prefix (die, cu);
16568 if (previous_prefix[0] != '\0')
16569 name = typename_concat (&objfile->objfile_obstack,
16570 previous_prefix, name, 0, cu);
16571
16572 /* Create the type. */
16573 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16574
16575 return set_die_type (die, type, cu);
16576 }
16577
16578 /* Read a namespace scope. */
16579
16580 static void
16581 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16582 {
16583 struct objfile *objfile = cu->per_objfile->objfile;
16584 int is_anonymous;
16585
16586 /* Add a symbol associated to this if we haven't seen the namespace
16587 before. Also, add a using directive if it's an anonymous
16588 namespace. */
16589
16590 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16591 {
16592 struct type *type;
16593
16594 type = read_type_die (die, cu);
16595 new_symbol (die, type, cu);
16596
16597 namespace_name (die, &is_anonymous, cu);
16598 if (is_anonymous)
16599 {
16600 const char *previous_prefix = determine_prefix (die, cu);
16601
16602 std::vector<const char *> excludes;
16603 add_using_directive (using_directives (cu),
16604 previous_prefix, type->name (), NULL,
16605 NULL, excludes, 0, &objfile->objfile_obstack);
16606 }
16607 }
16608
16609 if (die->child != NULL)
16610 {
16611 struct die_info *child_die = die->child;
16612
16613 while (child_die && child_die->tag)
16614 {
16615 process_die (child_die, cu);
16616 child_die = child_die->sibling;
16617 }
16618 }
16619 }
16620
16621 /* Read a Fortran module as type. This DIE can be only a declaration used for
16622 imported module. Still we need that type as local Fortran "use ... only"
16623 declaration imports depend on the created type in determine_prefix. */
16624
16625 static struct type *
16626 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16627 {
16628 struct objfile *objfile = cu->per_objfile->objfile;
16629 const char *module_name;
16630 struct type *type;
16631
16632 module_name = dwarf2_name (die, cu);
16633 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16634
16635 return set_die_type (die, type, cu);
16636 }
16637
16638 /* Read a Fortran module. */
16639
16640 static void
16641 read_module (struct die_info *die, struct dwarf2_cu *cu)
16642 {
16643 struct die_info *child_die = die->child;
16644 struct type *type;
16645
16646 type = read_type_die (die, cu);
16647 new_symbol (die, type, cu);
16648
16649 while (child_die && child_die->tag)
16650 {
16651 process_die (child_die, cu);
16652 child_die = child_die->sibling;
16653 }
16654 }
16655
16656 /* Return the name of the namespace represented by DIE. Set
16657 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16658 namespace. */
16659
16660 static const char *
16661 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16662 {
16663 struct die_info *current_die;
16664 const char *name = NULL;
16665
16666 /* Loop through the extensions until we find a name. */
16667
16668 for (current_die = die;
16669 current_die != NULL;
16670 current_die = dwarf2_extension (die, &cu))
16671 {
16672 /* We don't use dwarf2_name here so that we can detect the absence
16673 of a name -> anonymous namespace. */
16674 name = dwarf2_string_attr (die, DW_AT_name, cu);
16675
16676 if (name != NULL)
16677 break;
16678 }
16679
16680 /* Is it an anonymous namespace? */
16681
16682 *is_anonymous = (name == NULL);
16683 if (*is_anonymous)
16684 name = CP_ANONYMOUS_NAMESPACE_STR;
16685
16686 return name;
16687 }
16688
16689 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16690 the user defined type vector. */
16691
16692 static struct type *
16693 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16694 {
16695 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16696 struct comp_unit_head *cu_header = &cu->header;
16697 struct type *type;
16698 struct attribute *attr_byte_size;
16699 struct attribute *attr_address_class;
16700 int byte_size, addr_class;
16701 struct type *target_type;
16702
16703 target_type = die_type (die, cu);
16704
16705 /* The die_type call above may have already set the type for this DIE. */
16706 type = get_die_type (die, cu);
16707 if (type)
16708 return type;
16709
16710 type = lookup_pointer_type (target_type);
16711
16712 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16713 if (attr_byte_size)
16714 byte_size = DW_UNSND (attr_byte_size);
16715 else
16716 byte_size = cu_header->addr_size;
16717
16718 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16719 if (attr_address_class)
16720 addr_class = DW_UNSND (attr_address_class);
16721 else
16722 addr_class = DW_ADDR_none;
16723
16724 ULONGEST alignment = get_alignment (cu, die);
16725
16726 /* If the pointer size, alignment, or address class is different
16727 than the default, create a type variant marked as such and set
16728 the length accordingly. */
16729 if (TYPE_LENGTH (type) != byte_size
16730 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16731 && alignment != TYPE_RAW_ALIGN (type))
16732 || addr_class != DW_ADDR_none)
16733 {
16734 if (gdbarch_address_class_type_flags_p (gdbarch))
16735 {
16736 int type_flags;
16737
16738 type_flags = gdbarch_address_class_type_flags
16739 (gdbarch, byte_size, addr_class);
16740 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16741 == 0);
16742 type = make_type_with_address_space (type, type_flags);
16743 }
16744 else if (TYPE_LENGTH (type) != byte_size)
16745 {
16746 complaint (_("invalid pointer size %d"), byte_size);
16747 }
16748 else if (TYPE_RAW_ALIGN (type) != alignment)
16749 {
16750 complaint (_("Invalid DW_AT_alignment"
16751 " - DIE at %s [in module %s]"),
16752 sect_offset_str (die->sect_off),
16753 objfile_name (cu->per_objfile->objfile));
16754 }
16755 else
16756 {
16757 /* Should we also complain about unhandled address classes? */
16758 }
16759 }
16760
16761 TYPE_LENGTH (type) = byte_size;
16762 set_type_align (type, alignment);
16763 return set_die_type (die, type, cu);
16764 }
16765
16766 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16767 the user defined type vector. */
16768
16769 static struct type *
16770 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16771 {
16772 struct type *type;
16773 struct type *to_type;
16774 struct type *domain;
16775
16776 to_type = die_type (die, cu);
16777 domain = die_containing_type (die, cu);
16778
16779 /* The calls above may have already set the type for this DIE. */
16780 type = get_die_type (die, cu);
16781 if (type)
16782 return type;
16783
16784 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16785 type = lookup_methodptr_type (to_type);
16786 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16787 {
16788 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16789
16790 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16791 to_type->fields (), to_type->num_fields (),
16792 TYPE_VARARGS (to_type));
16793 type = lookup_methodptr_type (new_type);
16794 }
16795 else
16796 type = lookup_memberptr_type (to_type, domain);
16797
16798 return set_die_type (die, type, cu);
16799 }
16800
16801 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16802 the user defined type vector. */
16803
16804 static struct type *
16805 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16806 enum type_code refcode)
16807 {
16808 struct comp_unit_head *cu_header = &cu->header;
16809 struct type *type, *target_type;
16810 struct attribute *attr;
16811
16812 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16813
16814 target_type = die_type (die, cu);
16815
16816 /* The die_type call above may have already set the type for this DIE. */
16817 type = get_die_type (die, cu);
16818 if (type)
16819 return type;
16820
16821 type = lookup_reference_type (target_type, refcode);
16822 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16823 if (attr != nullptr)
16824 {
16825 TYPE_LENGTH (type) = DW_UNSND (attr);
16826 }
16827 else
16828 {
16829 TYPE_LENGTH (type) = cu_header->addr_size;
16830 }
16831 maybe_set_alignment (cu, die, type);
16832 return set_die_type (die, type, cu);
16833 }
16834
16835 /* Add the given cv-qualifiers to the element type of the array. GCC
16836 outputs DWARF type qualifiers that apply to an array, not the
16837 element type. But GDB relies on the array element type to carry
16838 the cv-qualifiers. This mimics section 6.7.3 of the C99
16839 specification. */
16840
16841 static struct type *
16842 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16843 struct type *base_type, int cnst, int voltl)
16844 {
16845 struct type *el_type, *inner_array;
16846
16847 base_type = copy_type (base_type);
16848 inner_array = base_type;
16849
16850 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16851 {
16852 TYPE_TARGET_TYPE (inner_array) =
16853 copy_type (TYPE_TARGET_TYPE (inner_array));
16854 inner_array = TYPE_TARGET_TYPE (inner_array);
16855 }
16856
16857 el_type = TYPE_TARGET_TYPE (inner_array);
16858 cnst |= TYPE_CONST (el_type);
16859 voltl |= TYPE_VOLATILE (el_type);
16860 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16861
16862 return set_die_type (die, base_type, cu);
16863 }
16864
16865 static struct type *
16866 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16867 {
16868 struct type *base_type, *cv_type;
16869
16870 base_type = die_type (die, cu);
16871
16872 /* The die_type call above may have already set the type for this DIE. */
16873 cv_type = get_die_type (die, cu);
16874 if (cv_type)
16875 return cv_type;
16876
16877 /* In case the const qualifier is applied to an array type, the element type
16878 is so qualified, not the array type (section 6.7.3 of C99). */
16879 if (base_type->code () == TYPE_CODE_ARRAY)
16880 return add_array_cv_type (die, cu, base_type, 1, 0);
16881
16882 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16883 return set_die_type (die, cv_type, cu);
16884 }
16885
16886 static struct type *
16887 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
16888 {
16889 struct type *base_type, *cv_type;
16890
16891 base_type = die_type (die, cu);
16892
16893 /* The die_type call above may have already set the type for this DIE. */
16894 cv_type = get_die_type (die, cu);
16895 if (cv_type)
16896 return cv_type;
16897
16898 /* In case the volatile qualifier is applied to an array type, the
16899 element type is so qualified, not the array type (section 6.7.3
16900 of C99). */
16901 if (base_type->code () == TYPE_CODE_ARRAY)
16902 return add_array_cv_type (die, cu, base_type, 0, 1);
16903
16904 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16905 return set_die_type (die, cv_type, cu);
16906 }
16907
16908 /* Handle DW_TAG_restrict_type. */
16909
16910 static struct type *
16911 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16912 {
16913 struct type *base_type, *cv_type;
16914
16915 base_type = die_type (die, cu);
16916
16917 /* The die_type call above may have already set the type for this DIE. */
16918 cv_type = get_die_type (die, cu);
16919 if (cv_type)
16920 return cv_type;
16921
16922 cv_type = make_restrict_type (base_type);
16923 return set_die_type (die, cv_type, cu);
16924 }
16925
16926 /* Handle DW_TAG_atomic_type. */
16927
16928 static struct type *
16929 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16930 {
16931 struct type *base_type, *cv_type;
16932
16933 base_type = die_type (die, cu);
16934
16935 /* The die_type call above may have already set the type for this DIE. */
16936 cv_type = get_die_type (die, cu);
16937 if (cv_type)
16938 return cv_type;
16939
16940 cv_type = make_atomic_type (base_type);
16941 return set_die_type (die, cv_type, cu);
16942 }
16943
16944 /* Extract all information from a DW_TAG_string_type DIE and add to
16945 the user defined type vector. It isn't really a user defined type,
16946 but it behaves like one, with other DIE's using an AT_user_def_type
16947 attribute to reference it. */
16948
16949 static struct type *
16950 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16951 {
16952 struct objfile *objfile = cu->per_objfile->objfile;
16953 struct gdbarch *gdbarch = objfile->arch ();
16954 struct type *type, *range_type, *index_type, *char_type;
16955 struct attribute *attr;
16956 struct dynamic_prop prop;
16957 bool length_is_constant = true;
16958 LONGEST length;
16959
16960 /* There are a couple of places where bit sizes might be made use of
16961 when parsing a DW_TAG_string_type, however, no producer that we know
16962 of make use of these. Handling bit sizes that are a multiple of the
16963 byte size is easy enough, but what about other bit sizes? Lets deal
16964 with that problem when we have to. Warn about these attributes being
16965 unsupported, then parse the type and ignore them like we always
16966 have. */
16967 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16968 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16969 {
16970 static bool warning_printed = false;
16971 if (!warning_printed)
16972 {
16973 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16974 "currently supported on DW_TAG_string_type."));
16975 warning_printed = true;
16976 }
16977 }
16978
16979 attr = dwarf2_attr (die, DW_AT_string_length, cu);
16980 if (attr != nullptr && !attr->form_is_constant ())
16981 {
16982 /* The string length describes the location at which the length of
16983 the string can be found. The size of the length field can be
16984 specified with one of the attributes below. */
16985 struct type *prop_type;
16986 struct attribute *len
16987 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16988 if (len == nullptr)
16989 len = dwarf2_attr (die, DW_AT_byte_size, cu);
16990 if (len != nullptr && len->form_is_constant ())
16991 {
16992 /* Pass 0 as the default as we know this attribute is constant
16993 and the default value will not be returned. */
16994 LONGEST sz = len->constant_value (0);
16995 prop_type = cu->per_cu->int_type (sz, true);
16996 }
16997 else
16998 {
16999 /* If the size is not specified then we assume it is the size of
17000 an address on this target. */
17001 prop_type = cu->per_cu->addr_sized_int_type (true);
17002 }
17003
17004 /* Convert the attribute into a dynamic property. */
17005 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17006 length = 1;
17007 else
17008 length_is_constant = false;
17009 }
17010 else if (attr != nullptr)
17011 {
17012 /* This DW_AT_string_length just contains the length with no
17013 indirection. There's no need to create a dynamic property in this
17014 case. Pass 0 for the default value as we know it will not be
17015 returned in this case. */
17016 length = attr->constant_value (0);
17017 }
17018 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17019 {
17020 /* We don't currently support non-constant byte sizes for strings. */
17021 length = attr->constant_value (1);
17022 }
17023 else
17024 {
17025 /* Use 1 as a fallback length if we have nothing else. */
17026 length = 1;
17027 }
17028
17029 index_type = objfile_type (objfile)->builtin_int;
17030 if (length_is_constant)
17031 range_type = create_static_range_type (NULL, index_type, 1, length);
17032 else
17033 {
17034 struct dynamic_prop low_bound;
17035
17036 low_bound.kind = PROP_CONST;
17037 low_bound.data.const_val = 1;
17038 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17039 }
17040 char_type = language_string_char_type (cu->language_defn, gdbarch);
17041 type = create_string_type (NULL, char_type, range_type);
17042
17043 return set_die_type (die, type, cu);
17044 }
17045
17046 /* Assuming that DIE corresponds to a function, returns nonzero
17047 if the function is prototyped. */
17048
17049 static int
17050 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17051 {
17052 struct attribute *attr;
17053
17054 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17055 if (attr && (DW_UNSND (attr) != 0))
17056 return 1;
17057
17058 /* The DWARF standard implies that the DW_AT_prototyped attribute
17059 is only meaningful for C, but the concept also extends to other
17060 languages that allow unprototyped functions (Eg: Objective C).
17061 For all other languages, assume that functions are always
17062 prototyped. */
17063 if (cu->language != language_c
17064 && cu->language != language_objc
17065 && cu->language != language_opencl)
17066 return 1;
17067
17068 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17069 prototyped and unprototyped functions; default to prototyped,
17070 since that is more common in modern code (and RealView warns
17071 about unprototyped functions). */
17072 if (producer_is_realview (cu->producer))
17073 return 1;
17074
17075 return 0;
17076 }
17077
17078 /* Handle DIES due to C code like:
17079
17080 struct foo
17081 {
17082 int (*funcp)(int a, long l);
17083 int b;
17084 };
17085
17086 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17087
17088 static struct type *
17089 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17090 {
17091 struct objfile *objfile = cu->per_objfile->objfile;
17092 struct type *type; /* Type that this function returns. */
17093 struct type *ftype; /* Function that returns above type. */
17094 struct attribute *attr;
17095
17096 type = die_type (die, cu);
17097
17098 /* The die_type call above may have already set the type for this DIE. */
17099 ftype = get_die_type (die, cu);
17100 if (ftype)
17101 return ftype;
17102
17103 ftype = lookup_function_type (type);
17104
17105 if (prototyped_function_p (die, cu))
17106 TYPE_PROTOTYPED (ftype) = 1;
17107
17108 /* Store the calling convention in the type if it's available in
17109 the subroutine die. Otherwise set the calling convention to
17110 the default value DW_CC_normal. */
17111 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17112 if (attr != nullptr
17113 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17114 TYPE_CALLING_CONVENTION (ftype)
17115 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17116 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17117 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17118 else
17119 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17120
17121 /* Record whether the function returns normally to its caller or not
17122 if the DWARF producer set that information. */
17123 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17124 if (attr && (DW_UNSND (attr) != 0))
17125 TYPE_NO_RETURN (ftype) = 1;
17126
17127 /* We need to add the subroutine type to the die immediately so
17128 we don't infinitely recurse when dealing with parameters
17129 declared as the same subroutine type. */
17130 set_die_type (die, ftype, cu);
17131
17132 if (die->child != NULL)
17133 {
17134 struct type *void_type = objfile_type (objfile)->builtin_void;
17135 struct die_info *child_die;
17136 int nparams, iparams;
17137
17138 /* Count the number of parameters.
17139 FIXME: GDB currently ignores vararg functions, but knows about
17140 vararg member functions. */
17141 nparams = 0;
17142 child_die = die->child;
17143 while (child_die && child_die->tag)
17144 {
17145 if (child_die->tag == DW_TAG_formal_parameter)
17146 nparams++;
17147 else if (child_die->tag == DW_TAG_unspecified_parameters)
17148 TYPE_VARARGS (ftype) = 1;
17149 child_die = child_die->sibling;
17150 }
17151
17152 /* Allocate storage for parameters and fill them in. */
17153 ftype->set_num_fields (nparams);
17154 ftype->set_fields
17155 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17156
17157 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17158 even if we error out during the parameters reading below. */
17159 for (iparams = 0; iparams < nparams; iparams++)
17160 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17161
17162 iparams = 0;
17163 child_die = die->child;
17164 while (child_die && child_die->tag)
17165 {
17166 if (child_die->tag == DW_TAG_formal_parameter)
17167 {
17168 struct type *arg_type;
17169
17170 /* DWARF version 2 has no clean way to discern C++
17171 static and non-static member functions. G++ helps
17172 GDB by marking the first parameter for non-static
17173 member functions (which is the this pointer) as
17174 artificial. We pass this information to
17175 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17176
17177 DWARF version 3 added DW_AT_object_pointer, which GCC
17178 4.5 does not yet generate. */
17179 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17180 if (attr != nullptr)
17181 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17182 else
17183 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17184 arg_type = die_type (child_die, cu);
17185
17186 /* RealView does not mark THIS as const, which the testsuite
17187 expects. GCC marks THIS as const in method definitions,
17188 but not in the class specifications (GCC PR 43053). */
17189 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17190 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17191 {
17192 int is_this = 0;
17193 struct dwarf2_cu *arg_cu = cu;
17194 const char *name = dwarf2_name (child_die, cu);
17195
17196 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17197 if (attr != nullptr)
17198 {
17199 /* If the compiler emits this, use it. */
17200 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17201 is_this = 1;
17202 }
17203 else if (name && strcmp (name, "this") == 0)
17204 /* Function definitions will have the argument names. */
17205 is_this = 1;
17206 else if (name == NULL && iparams == 0)
17207 /* Declarations may not have the names, so like
17208 elsewhere in GDB, assume an artificial first
17209 argument is "this". */
17210 is_this = 1;
17211
17212 if (is_this)
17213 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17214 arg_type, 0);
17215 }
17216
17217 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17218 iparams++;
17219 }
17220 child_die = child_die->sibling;
17221 }
17222 }
17223
17224 return ftype;
17225 }
17226
17227 static struct type *
17228 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17229 {
17230 struct objfile *objfile = cu->per_objfile->objfile;
17231 const char *name = NULL;
17232 struct type *this_type, *target_type;
17233
17234 name = dwarf2_full_name (NULL, die, cu);
17235 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17236 TYPE_TARGET_STUB (this_type) = 1;
17237 set_die_type (die, this_type, cu);
17238 target_type = die_type (die, cu);
17239 if (target_type != this_type)
17240 TYPE_TARGET_TYPE (this_type) = target_type;
17241 else
17242 {
17243 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17244 spec and cause infinite loops in GDB. */
17245 complaint (_("Self-referential DW_TAG_typedef "
17246 "- DIE at %s [in module %s]"),
17247 sect_offset_str (die->sect_off), objfile_name (objfile));
17248 TYPE_TARGET_TYPE (this_type) = NULL;
17249 }
17250 if (name == NULL)
17251 {
17252 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17253 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17254 Handle these by just returning the target type, rather than
17255 constructing an anonymous typedef type and trying to handle this
17256 elsewhere. */
17257 set_die_type (die, target_type, cu);
17258 return target_type;
17259 }
17260 return this_type;
17261 }
17262
17263 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17264 (which may be different from NAME) to the architecture back-end to allow
17265 it to guess the correct format if necessary. */
17266
17267 static struct type *
17268 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17269 const char *name_hint, enum bfd_endian byte_order)
17270 {
17271 struct gdbarch *gdbarch = objfile->arch ();
17272 const struct floatformat **format;
17273 struct type *type;
17274
17275 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17276 if (format)
17277 type = init_float_type (objfile, bits, name, format, byte_order);
17278 else
17279 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17280
17281 return type;
17282 }
17283
17284 /* Allocate an integer type of size BITS and name NAME. */
17285
17286 static struct type *
17287 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17288 int bits, int unsigned_p, const char *name)
17289 {
17290 struct type *type;
17291
17292 /* Versions of Intel's C Compiler generate an integer type called "void"
17293 instead of using DW_TAG_unspecified_type. This has been seen on
17294 at least versions 14, 17, and 18. */
17295 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17296 && strcmp (name, "void") == 0)
17297 type = objfile_type (objfile)->builtin_void;
17298 else
17299 type = init_integer_type (objfile, bits, unsigned_p, name);
17300
17301 return type;
17302 }
17303
17304 /* Initialise and return a floating point type of size BITS suitable for
17305 use as a component of a complex number. The NAME_HINT is passed through
17306 when initialising the floating point type and is the name of the complex
17307 type.
17308
17309 As DWARF doesn't currently provide an explicit name for the components
17310 of a complex number, but it can be helpful to have these components
17311 named, we try to select a suitable name based on the size of the
17312 component. */
17313 static struct type *
17314 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17315 struct objfile *objfile,
17316 int bits, const char *name_hint,
17317 enum bfd_endian byte_order)
17318 {
17319 gdbarch *gdbarch = objfile->arch ();
17320 struct type *tt = nullptr;
17321
17322 /* Try to find a suitable floating point builtin type of size BITS.
17323 We're going to use the name of this type as the name for the complex
17324 target type that we are about to create. */
17325 switch (cu->language)
17326 {
17327 case language_fortran:
17328 switch (bits)
17329 {
17330 case 32:
17331 tt = builtin_f_type (gdbarch)->builtin_real;
17332 break;
17333 case 64:
17334 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17335 break;
17336 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17337 case 128:
17338 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17339 break;
17340 }
17341 break;
17342 default:
17343 switch (bits)
17344 {
17345 case 32:
17346 tt = builtin_type (gdbarch)->builtin_float;
17347 break;
17348 case 64:
17349 tt = builtin_type (gdbarch)->builtin_double;
17350 break;
17351 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17352 case 128:
17353 tt = builtin_type (gdbarch)->builtin_long_double;
17354 break;
17355 }
17356 break;
17357 }
17358
17359 /* If the type we found doesn't match the size we were looking for, then
17360 pretend we didn't find a type at all, the complex target type we
17361 create will then be nameless. */
17362 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17363 tt = nullptr;
17364
17365 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17366 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17367 }
17368
17369 /* Find a representation of a given base type and install
17370 it in the TYPE field of the die. */
17371
17372 static struct type *
17373 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17374 {
17375 struct objfile *objfile = cu->per_objfile->objfile;
17376 struct type *type;
17377 struct attribute *attr;
17378 int encoding = 0, bits = 0;
17379 const char *name;
17380 gdbarch *arch;
17381
17382 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17383 if (attr != nullptr)
17384 encoding = DW_UNSND (attr);
17385 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17386 if (attr != nullptr)
17387 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17388 name = dwarf2_name (die, cu);
17389 if (!name)
17390 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17391
17392 arch = objfile->arch ();
17393 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17394
17395 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17396 if (attr)
17397 {
17398 int endianity = DW_UNSND (attr);
17399
17400 switch (endianity)
17401 {
17402 case DW_END_big:
17403 byte_order = BFD_ENDIAN_BIG;
17404 break;
17405 case DW_END_little:
17406 byte_order = BFD_ENDIAN_LITTLE;
17407 break;
17408 default:
17409 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17410 break;
17411 }
17412 }
17413
17414 switch (encoding)
17415 {
17416 case DW_ATE_address:
17417 /* Turn DW_ATE_address into a void * pointer. */
17418 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17419 type = init_pointer_type (objfile, bits, name, type);
17420 break;
17421 case DW_ATE_boolean:
17422 type = init_boolean_type (objfile, bits, 1, name);
17423 break;
17424 case DW_ATE_complex_float:
17425 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17426 byte_order);
17427 if (type->code () == TYPE_CODE_ERROR)
17428 {
17429 if (name == nullptr)
17430 {
17431 struct obstack *obstack
17432 = &cu->per_objfile->objfile->objfile_obstack;
17433 name = obconcat (obstack, "_Complex ", type->name (),
17434 nullptr);
17435 }
17436 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17437 }
17438 else
17439 type = init_complex_type (name, type);
17440 break;
17441 case DW_ATE_decimal_float:
17442 type = init_decfloat_type (objfile, bits, name);
17443 break;
17444 case DW_ATE_float:
17445 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17446 break;
17447 case DW_ATE_signed:
17448 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17449 break;
17450 case DW_ATE_unsigned:
17451 if (cu->language == language_fortran
17452 && name
17453 && startswith (name, "character("))
17454 type = init_character_type (objfile, bits, 1, name);
17455 else
17456 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17457 break;
17458 case DW_ATE_signed_char:
17459 if (cu->language == language_ada || cu->language == language_m2
17460 || cu->language == language_pascal
17461 || cu->language == language_fortran)
17462 type = init_character_type (objfile, bits, 0, name);
17463 else
17464 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17465 break;
17466 case DW_ATE_unsigned_char:
17467 if (cu->language == language_ada || cu->language == language_m2
17468 || cu->language == language_pascal
17469 || cu->language == language_fortran
17470 || cu->language == language_rust)
17471 type = init_character_type (objfile, bits, 1, name);
17472 else
17473 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17474 break;
17475 case DW_ATE_UTF:
17476 {
17477 if (bits == 16)
17478 type = builtin_type (arch)->builtin_char16;
17479 else if (bits == 32)
17480 type = builtin_type (arch)->builtin_char32;
17481 else
17482 {
17483 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17484 bits);
17485 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17486 }
17487 return set_die_type (die, type, cu);
17488 }
17489 break;
17490
17491 default:
17492 complaint (_("unsupported DW_AT_encoding: '%s'"),
17493 dwarf_type_encoding_name (encoding));
17494 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17495 break;
17496 }
17497
17498 if (name && strcmp (name, "char") == 0)
17499 TYPE_NOSIGN (type) = 1;
17500
17501 maybe_set_alignment (cu, die, type);
17502
17503 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17504
17505 return set_die_type (die, type, cu);
17506 }
17507
17508 /* Parse dwarf attribute if it's a block, reference or constant and put the
17509 resulting value of the attribute into struct bound_prop.
17510 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17511
17512 static int
17513 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17514 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17515 struct type *default_type)
17516 {
17517 struct dwarf2_property_baton *baton;
17518 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17519 struct objfile *objfile = per_objfile->objfile;
17520 struct obstack *obstack = &objfile->objfile_obstack;
17521
17522 gdb_assert (default_type != NULL);
17523
17524 if (attr == NULL || prop == NULL)
17525 return 0;
17526
17527 if (attr->form_is_block ())
17528 {
17529 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17530 baton->property_type = default_type;
17531 baton->locexpr.per_cu = cu->per_cu;
17532 baton->locexpr.per_objfile = per_objfile;
17533 baton->locexpr.size = DW_BLOCK (attr)->size;
17534 baton->locexpr.data = DW_BLOCK (attr)->data;
17535 switch (attr->name)
17536 {
17537 case DW_AT_string_length:
17538 baton->locexpr.is_reference = true;
17539 break;
17540 default:
17541 baton->locexpr.is_reference = false;
17542 break;
17543 }
17544 prop->data.baton = baton;
17545 prop->kind = PROP_LOCEXPR;
17546 gdb_assert (prop->data.baton != NULL);
17547 }
17548 else if (attr->form_is_ref ())
17549 {
17550 struct dwarf2_cu *target_cu = cu;
17551 struct die_info *target_die;
17552 struct attribute *target_attr;
17553
17554 target_die = follow_die_ref (die, attr, &target_cu);
17555 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17556 if (target_attr == NULL)
17557 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17558 target_cu);
17559 if (target_attr == NULL)
17560 return 0;
17561
17562 switch (target_attr->name)
17563 {
17564 case DW_AT_location:
17565 if (target_attr->form_is_section_offset ())
17566 {
17567 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17568 baton->property_type = die_type (target_die, target_cu);
17569 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17570 prop->data.baton = baton;
17571 prop->kind = PROP_LOCLIST;
17572 gdb_assert (prop->data.baton != NULL);
17573 }
17574 else if (target_attr->form_is_block ())
17575 {
17576 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17577 baton->property_type = die_type (target_die, target_cu);
17578 baton->locexpr.per_cu = cu->per_cu;
17579 baton->locexpr.per_objfile = per_objfile;
17580 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17581 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17582 baton->locexpr.is_reference = true;
17583 prop->data.baton = baton;
17584 prop->kind = PROP_LOCEXPR;
17585 gdb_assert (prop->data.baton != NULL);
17586 }
17587 else
17588 {
17589 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17590 "dynamic property");
17591 return 0;
17592 }
17593 break;
17594 case DW_AT_data_member_location:
17595 {
17596 LONGEST offset;
17597
17598 if (!handle_data_member_location (target_die, target_cu,
17599 &offset))
17600 return 0;
17601
17602 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17603 baton->property_type = read_type_die (target_die->parent,
17604 target_cu);
17605 baton->offset_info.offset = offset;
17606 baton->offset_info.type = die_type (target_die, target_cu);
17607 prop->data.baton = baton;
17608 prop->kind = PROP_ADDR_OFFSET;
17609 break;
17610 }
17611 }
17612 }
17613 else if (attr->form_is_constant ())
17614 {
17615 prop->data.const_val = attr->constant_value (0);
17616 prop->kind = PROP_CONST;
17617 }
17618 else
17619 {
17620 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17621 dwarf2_name (die, cu));
17622 return 0;
17623 }
17624
17625 return 1;
17626 }
17627
17628 /* See read.h. */
17629
17630 struct type *
17631 dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
17632 {
17633 struct objfile *objfile = dwarf2_per_objfile->objfile;
17634 struct type *int_type;
17635
17636 /* Helper macro to examine the various builtin types. */
17637 #define TRY_TYPE(F) \
17638 int_type = (unsigned_p \
17639 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17640 : objfile_type (objfile)->builtin_ ## F); \
17641 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17642 return int_type
17643
17644 TRY_TYPE (char);
17645 TRY_TYPE (short);
17646 TRY_TYPE (int);
17647 TRY_TYPE (long);
17648 TRY_TYPE (long_long);
17649
17650 #undef TRY_TYPE
17651
17652 gdb_assert_not_reached ("unable to find suitable integer type");
17653 }
17654
17655 /* See read.h. */
17656
17657 struct type *
17658 dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
17659 {
17660 int addr_size = this->addr_size ();
17661 return int_type (addr_size, unsigned_p);
17662 }
17663
17664 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17665 present (which is valid) then compute the default type based on the
17666 compilation units address size. */
17667
17668 static struct type *
17669 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17670 {
17671 struct type *index_type = die_type (die, cu);
17672
17673 /* Dwarf-2 specifications explicitly allows to create subrange types
17674 without specifying a base type.
17675 In that case, the base type must be set to the type of
17676 the lower bound, upper bound or count, in that order, if any of these
17677 three attributes references an object that has a type.
17678 If no base type is found, the Dwarf-2 specifications say that
17679 a signed integer type of size equal to the size of an address should
17680 be used.
17681 For the following C code: `extern char gdb_int [];'
17682 GCC produces an empty range DIE.
17683 FIXME: muller/2010-05-28: Possible references to object for low bound,
17684 high bound or count are not yet handled by this code. */
17685 if (index_type->code () == TYPE_CODE_VOID)
17686 index_type = cu->per_cu->addr_sized_int_type (false);
17687
17688 return index_type;
17689 }
17690
17691 /* Read the given DW_AT_subrange DIE. */
17692
17693 static struct type *
17694 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17695 {
17696 struct type *base_type, *orig_base_type;
17697 struct type *range_type;
17698 struct attribute *attr;
17699 struct dynamic_prop low, high;
17700 int low_default_is_valid;
17701 int high_bound_is_count = 0;
17702 const char *name;
17703 ULONGEST negative_mask;
17704
17705 orig_base_type = read_subrange_index_type (die, cu);
17706
17707 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17708 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17709 creating the range type, but we use the result of check_typedef
17710 when examining properties of the type. */
17711 base_type = check_typedef (orig_base_type);
17712
17713 /* The die_type call above may have already set the type for this DIE. */
17714 range_type = get_die_type (die, cu);
17715 if (range_type)
17716 return range_type;
17717
17718 low.kind = PROP_CONST;
17719 high.kind = PROP_CONST;
17720 high.data.const_val = 0;
17721
17722 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17723 omitting DW_AT_lower_bound. */
17724 switch (cu->language)
17725 {
17726 case language_c:
17727 case language_cplus:
17728 low.data.const_val = 0;
17729 low_default_is_valid = 1;
17730 break;
17731 case language_fortran:
17732 low.data.const_val = 1;
17733 low_default_is_valid = 1;
17734 break;
17735 case language_d:
17736 case language_objc:
17737 case language_rust:
17738 low.data.const_val = 0;
17739 low_default_is_valid = (cu->header.version >= 4);
17740 break;
17741 case language_ada:
17742 case language_m2:
17743 case language_pascal:
17744 low.data.const_val = 1;
17745 low_default_is_valid = (cu->header.version >= 4);
17746 break;
17747 default:
17748 low.data.const_val = 0;
17749 low_default_is_valid = 0;
17750 break;
17751 }
17752
17753 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17754 if (attr != nullptr)
17755 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17756 else if (!low_default_is_valid)
17757 complaint (_("Missing DW_AT_lower_bound "
17758 "- DIE at %s [in module %s]"),
17759 sect_offset_str (die->sect_off),
17760 objfile_name (cu->per_objfile->objfile));
17761
17762 struct attribute *attr_ub, *attr_count;
17763 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17764 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17765 {
17766 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17767 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17768 {
17769 /* If bounds are constant do the final calculation here. */
17770 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17771 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17772 else
17773 high_bound_is_count = 1;
17774 }
17775 else
17776 {
17777 if (attr_ub != NULL)
17778 complaint (_("Unresolved DW_AT_upper_bound "
17779 "- DIE at %s [in module %s]"),
17780 sect_offset_str (die->sect_off),
17781 objfile_name (cu->per_objfile->objfile));
17782 if (attr_count != NULL)
17783 complaint (_("Unresolved DW_AT_count "
17784 "- DIE at %s [in module %s]"),
17785 sect_offset_str (die->sect_off),
17786 objfile_name (cu->per_objfile->objfile));
17787 }
17788 }
17789
17790 LONGEST bias = 0;
17791 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17792 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17793 bias = bias_attr->constant_value (0);
17794
17795 /* Normally, the DWARF producers are expected to use a signed
17796 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17797 But this is unfortunately not always the case, as witnessed
17798 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17799 is used instead. To work around that ambiguity, we treat
17800 the bounds as signed, and thus sign-extend their values, when
17801 the base type is signed. */
17802 negative_mask =
17803 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17804 if (low.kind == PROP_CONST
17805 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17806 low.data.const_val |= negative_mask;
17807 if (high.kind == PROP_CONST
17808 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17809 high.data.const_val |= negative_mask;
17810
17811 /* Check for bit and byte strides. */
17812 struct dynamic_prop byte_stride_prop;
17813 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17814 if (attr_byte_stride != nullptr)
17815 {
17816 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17817 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17818 prop_type);
17819 }
17820
17821 struct dynamic_prop bit_stride_prop;
17822 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17823 if (attr_bit_stride != nullptr)
17824 {
17825 /* It only makes sense to have either a bit or byte stride. */
17826 if (attr_byte_stride != nullptr)
17827 {
17828 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17829 "- DIE at %s [in module %s]"),
17830 sect_offset_str (die->sect_off),
17831 objfile_name (cu->per_objfile->objfile));
17832 attr_bit_stride = nullptr;
17833 }
17834 else
17835 {
17836 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
17837 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17838 prop_type);
17839 }
17840 }
17841
17842 if (attr_byte_stride != nullptr
17843 || attr_bit_stride != nullptr)
17844 {
17845 bool byte_stride_p = (attr_byte_stride != nullptr);
17846 struct dynamic_prop *stride
17847 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17848
17849 range_type
17850 = create_range_type_with_stride (NULL, orig_base_type, &low,
17851 &high, bias, stride, byte_stride_p);
17852 }
17853 else
17854 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17855
17856 if (high_bound_is_count)
17857 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17858
17859 /* Ada expects an empty array on no boundary attributes. */
17860 if (attr == NULL && cu->language != language_ada)
17861 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17862
17863 name = dwarf2_name (die, cu);
17864 if (name)
17865 range_type->set_name (name);
17866
17867 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17868 if (attr != nullptr)
17869 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17870
17871 maybe_set_alignment (cu, die, range_type);
17872
17873 set_die_type (die, range_type, cu);
17874
17875 /* set_die_type should be already done. */
17876 set_descriptive_type (range_type, die, cu);
17877
17878 return range_type;
17879 }
17880
17881 static struct type *
17882 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17883 {
17884 struct type *type;
17885
17886 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17887 type->set_name (dwarf2_name (die, cu));
17888
17889 /* In Ada, an unspecified type is typically used when the description
17890 of the type is deferred to a different unit. When encountering
17891 such a type, we treat it as a stub, and try to resolve it later on,
17892 when needed. */
17893 if (cu->language == language_ada)
17894 TYPE_STUB (type) = 1;
17895
17896 return set_die_type (die, type, cu);
17897 }
17898
17899 /* Read a single die and all its descendents. Set the die's sibling
17900 field to NULL; set other fields in the die correctly, and set all
17901 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17902 location of the info_ptr after reading all of those dies. PARENT
17903 is the parent of the die in question. */
17904
17905 static struct die_info *
17906 read_die_and_children (const struct die_reader_specs *reader,
17907 const gdb_byte *info_ptr,
17908 const gdb_byte **new_info_ptr,
17909 struct die_info *parent)
17910 {
17911 struct die_info *die;
17912 const gdb_byte *cur_ptr;
17913
17914 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17915 if (die == NULL)
17916 {
17917 *new_info_ptr = cur_ptr;
17918 return NULL;
17919 }
17920 store_in_ref_table (die, reader->cu);
17921
17922 if (die->has_children)
17923 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17924 else
17925 {
17926 die->child = NULL;
17927 *new_info_ptr = cur_ptr;
17928 }
17929
17930 die->sibling = NULL;
17931 die->parent = parent;
17932 return die;
17933 }
17934
17935 /* Read a die, all of its descendents, and all of its siblings; set
17936 all of the fields of all of the dies correctly. Arguments are as
17937 in read_die_and_children. */
17938
17939 static struct die_info *
17940 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17941 const gdb_byte *info_ptr,
17942 const gdb_byte **new_info_ptr,
17943 struct die_info *parent)
17944 {
17945 struct die_info *first_die, *last_sibling;
17946 const gdb_byte *cur_ptr;
17947
17948 cur_ptr = info_ptr;
17949 first_die = last_sibling = NULL;
17950
17951 while (1)
17952 {
17953 struct die_info *die
17954 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17955
17956 if (die == NULL)
17957 {
17958 *new_info_ptr = cur_ptr;
17959 return first_die;
17960 }
17961
17962 if (!first_die)
17963 first_die = die;
17964 else
17965 last_sibling->sibling = die;
17966
17967 last_sibling = die;
17968 }
17969 }
17970
17971 /* Read a die, all of its descendents, and all of its siblings; set
17972 all of the fields of all of the dies correctly. Arguments are as
17973 in read_die_and_children.
17974 This the main entry point for reading a DIE and all its children. */
17975
17976 static struct die_info *
17977 read_die_and_siblings (const struct die_reader_specs *reader,
17978 const gdb_byte *info_ptr,
17979 const gdb_byte **new_info_ptr,
17980 struct die_info *parent)
17981 {
17982 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17983 new_info_ptr, parent);
17984
17985 if (dwarf_die_debug)
17986 {
17987 fprintf_unfiltered (gdb_stdlog,
17988 "Read die from %s@0x%x of %s:\n",
17989 reader->die_section->get_name (),
17990 (unsigned) (info_ptr - reader->die_section->buffer),
17991 bfd_get_filename (reader->abfd));
17992 dump_die (die, dwarf_die_debug);
17993 }
17994
17995 return die;
17996 }
17997
17998 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17999 attributes.
18000 The caller is responsible for filling in the extra attributes
18001 and updating (*DIEP)->num_attrs.
18002 Set DIEP to point to a newly allocated die with its information,
18003 except for its child, sibling, and parent fields. */
18004
18005 static const gdb_byte *
18006 read_full_die_1 (const struct die_reader_specs *reader,
18007 struct die_info **diep, const gdb_byte *info_ptr,
18008 int num_extra_attrs)
18009 {
18010 unsigned int abbrev_number, bytes_read, i;
18011 struct abbrev_info *abbrev;
18012 struct die_info *die;
18013 struct dwarf2_cu *cu = reader->cu;
18014 bfd *abfd = reader->abfd;
18015
18016 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18017 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18018 info_ptr += bytes_read;
18019 if (!abbrev_number)
18020 {
18021 *diep = NULL;
18022 return info_ptr;
18023 }
18024
18025 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18026 if (!abbrev)
18027 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18028 abbrev_number,
18029 bfd_get_filename (abfd));
18030
18031 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18032 die->sect_off = sect_off;
18033 die->tag = abbrev->tag;
18034 die->abbrev = abbrev_number;
18035 die->has_children = abbrev->has_children;
18036
18037 /* Make the result usable.
18038 The caller needs to update num_attrs after adding the extra
18039 attributes. */
18040 die->num_attrs = abbrev->num_attrs;
18041
18042 std::vector<int> indexes_that_need_reprocess;
18043 for (i = 0; i < abbrev->num_attrs; ++i)
18044 {
18045 bool need_reprocess;
18046 info_ptr =
18047 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18048 info_ptr, &need_reprocess);
18049 if (need_reprocess)
18050 indexes_that_need_reprocess.push_back (i);
18051 }
18052
18053 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18054 if (attr != nullptr)
18055 cu->str_offsets_base = DW_UNSND (attr);
18056
18057 attr = die->attr (DW_AT_loclists_base);
18058 if (attr != nullptr)
18059 cu->loclist_base = DW_UNSND (attr);
18060
18061 auto maybe_addr_base = die->addr_base ();
18062 if (maybe_addr_base.has_value ())
18063 cu->addr_base = *maybe_addr_base;
18064 for (int index : indexes_that_need_reprocess)
18065 read_attribute_reprocess (reader, &die->attrs[index]);
18066 *diep = die;
18067 return info_ptr;
18068 }
18069
18070 /* Read a die and all its attributes.
18071 Set DIEP to point to a newly allocated die with its information,
18072 except for its child, sibling, and parent fields. */
18073
18074 static const gdb_byte *
18075 read_full_die (const struct die_reader_specs *reader,
18076 struct die_info **diep, const gdb_byte *info_ptr)
18077 {
18078 const gdb_byte *result;
18079
18080 result = read_full_die_1 (reader, diep, info_ptr, 0);
18081
18082 if (dwarf_die_debug)
18083 {
18084 fprintf_unfiltered (gdb_stdlog,
18085 "Read die from %s@0x%x of %s:\n",
18086 reader->die_section->get_name (),
18087 (unsigned) (info_ptr - reader->die_section->buffer),
18088 bfd_get_filename (reader->abfd));
18089 dump_die (*diep, dwarf_die_debug);
18090 }
18091
18092 return result;
18093 }
18094 \f
18095
18096 /* Returns nonzero if TAG represents a type that we might generate a partial
18097 symbol for. */
18098
18099 static int
18100 is_type_tag_for_partial (int tag)
18101 {
18102 switch (tag)
18103 {
18104 #if 0
18105 /* Some types that would be reasonable to generate partial symbols for,
18106 that we don't at present. */
18107 case DW_TAG_array_type:
18108 case DW_TAG_file_type:
18109 case DW_TAG_ptr_to_member_type:
18110 case DW_TAG_set_type:
18111 case DW_TAG_string_type:
18112 case DW_TAG_subroutine_type:
18113 #endif
18114 case DW_TAG_base_type:
18115 case DW_TAG_class_type:
18116 case DW_TAG_interface_type:
18117 case DW_TAG_enumeration_type:
18118 case DW_TAG_structure_type:
18119 case DW_TAG_subrange_type:
18120 case DW_TAG_typedef:
18121 case DW_TAG_union_type:
18122 return 1;
18123 default:
18124 return 0;
18125 }
18126 }
18127
18128 /* Load all DIEs that are interesting for partial symbols into memory. */
18129
18130 static struct partial_die_info *
18131 load_partial_dies (const struct die_reader_specs *reader,
18132 const gdb_byte *info_ptr, int building_psymtab)
18133 {
18134 struct dwarf2_cu *cu = reader->cu;
18135 struct objfile *objfile = cu->per_objfile->objfile;
18136 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18137 unsigned int bytes_read;
18138 unsigned int load_all = 0;
18139 int nesting_level = 1;
18140
18141 parent_die = NULL;
18142 last_die = NULL;
18143
18144 gdb_assert (cu->per_cu != NULL);
18145 if (cu->per_cu->load_all_dies)
18146 load_all = 1;
18147
18148 cu->partial_dies
18149 = htab_create_alloc_ex (cu->header.length / 12,
18150 partial_die_hash,
18151 partial_die_eq,
18152 NULL,
18153 &cu->comp_unit_obstack,
18154 hashtab_obstack_allocate,
18155 dummy_obstack_deallocate);
18156
18157 while (1)
18158 {
18159 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18160
18161 /* A NULL abbrev means the end of a series of children. */
18162 if (abbrev == NULL)
18163 {
18164 if (--nesting_level == 0)
18165 return first_die;
18166
18167 info_ptr += bytes_read;
18168 last_die = parent_die;
18169 parent_die = parent_die->die_parent;
18170 continue;
18171 }
18172
18173 /* Check for template arguments. We never save these; if
18174 they're seen, we just mark the parent, and go on our way. */
18175 if (parent_die != NULL
18176 && cu->language == language_cplus
18177 && (abbrev->tag == DW_TAG_template_type_param
18178 || abbrev->tag == DW_TAG_template_value_param))
18179 {
18180 parent_die->has_template_arguments = 1;
18181
18182 if (!load_all)
18183 {
18184 /* We don't need a partial DIE for the template argument. */
18185 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18186 continue;
18187 }
18188 }
18189
18190 /* We only recurse into c++ subprograms looking for template arguments.
18191 Skip their other children. */
18192 if (!load_all
18193 && cu->language == language_cplus
18194 && parent_die != NULL
18195 && parent_die->tag == DW_TAG_subprogram)
18196 {
18197 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18198 continue;
18199 }
18200
18201 /* Check whether this DIE is interesting enough to save. Normally
18202 we would not be interested in members here, but there may be
18203 later variables referencing them via DW_AT_specification (for
18204 static members). */
18205 if (!load_all
18206 && !is_type_tag_for_partial (abbrev->tag)
18207 && abbrev->tag != DW_TAG_constant
18208 && abbrev->tag != DW_TAG_enumerator
18209 && abbrev->tag != DW_TAG_subprogram
18210 && abbrev->tag != DW_TAG_inlined_subroutine
18211 && abbrev->tag != DW_TAG_lexical_block
18212 && abbrev->tag != DW_TAG_variable
18213 && abbrev->tag != DW_TAG_namespace
18214 && abbrev->tag != DW_TAG_module
18215 && abbrev->tag != DW_TAG_member
18216 && abbrev->tag != DW_TAG_imported_unit
18217 && abbrev->tag != DW_TAG_imported_declaration)
18218 {
18219 /* Otherwise we skip to the next sibling, if any. */
18220 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18221 continue;
18222 }
18223
18224 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18225 abbrev);
18226
18227 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18228
18229 /* This two-pass algorithm for processing partial symbols has a
18230 high cost in cache pressure. Thus, handle some simple cases
18231 here which cover the majority of C partial symbols. DIEs
18232 which neither have specification tags in them, nor could have
18233 specification tags elsewhere pointing at them, can simply be
18234 processed and discarded.
18235
18236 This segment is also optional; scan_partial_symbols and
18237 add_partial_symbol will handle these DIEs if we chain
18238 them in normally. When compilers which do not emit large
18239 quantities of duplicate debug information are more common,
18240 this code can probably be removed. */
18241
18242 /* Any complete simple types at the top level (pretty much all
18243 of them, for a language without namespaces), can be processed
18244 directly. */
18245 if (parent_die == NULL
18246 && pdi.has_specification == 0
18247 && pdi.is_declaration == 0
18248 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18249 || pdi.tag == DW_TAG_base_type
18250 || pdi.tag == DW_TAG_subrange_type))
18251 {
18252 if (building_psymtab && pdi.name != NULL)
18253 add_psymbol_to_list (pdi.name, false,
18254 VAR_DOMAIN, LOC_TYPEDEF, -1,
18255 psymbol_placement::STATIC,
18256 0, cu->language, objfile);
18257 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18258 continue;
18259 }
18260
18261 /* The exception for DW_TAG_typedef with has_children above is
18262 a workaround of GCC PR debug/47510. In the case of this complaint
18263 type_name_or_error will error on such types later.
18264
18265 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18266 it could not find the child DIEs referenced later, this is checked
18267 above. In correct DWARF DW_TAG_typedef should have no children. */
18268
18269 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18270 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18271 "- DIE at %s [in module %s]"),
18272 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18273
18274 /* If we're at the second level, and we're an enumerator, and
18275 our parent has no specification (meaning possibly lives in a
18276 namespace elsewhere), then we can add the partial symbol now
18277 instead of queueing it. */
18278 if (pdi.tag == DW_TAG_enumerator
18279 && parent_die != NULL
18280 && parent_die->die_parent == NULL
18281 && parent_die->tag == DW_TAG_enumeration_type
18282 && parent_die->has_specification == 0)
18283 {
18284 if (pdi.name == NULL)
18285 complaint (_("malformed enumerator DIE ignored"));
18286 else if (building_psymtab)
18287 add_psymbol_to_list (pdi.name, false,
18288 VAR_DOMAIN, LOC_CONST, -1,
18289 cu->language == language_cplus
18290 ? psymbol_placement::GLOBAL
18291 : psymbol_placement::STATIC,
18292 0, cu->language, objfile);
18293
18294 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18295 continue;
18296 }
18297
18298 struct partial_die_info *part_die
18299 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18300
18301 /* We'll save this DIE so link it in. */
18302 part_die->die_parent = parent_die;
18303 part_die->die_sibling = NULL;
18304 part_die->die_child = NULL;
18305
18306 if (last_die && last_die == parent_die)
18307 last_die->die_child = part_die;
18308 else if (last_die)
18309 last_die->die_sibling = part_die;
18310
18311 last_die = part_die;
18312
18313 if (first_die == NULL)
18314 first_die = part_die;
18315
18316 /* Maybe add the DIE to the hash table. Not all DIEs that we
18317 find interesting need to be in the hash table, because we
18318 also have the parent/sibling/child chains; only those that we
18319 might refer to by offset later during partial symbol reading.
18320
18321 For now this means things that might have be the target of a
18322 DW_AT_specification, DW_AT_abstract_origin, or
18323 DW_AT_extension. DW_AT_extension will refer only to
18324 namespaces; DW_AT_abstract_origin refers to functions (and
18325 many things under the function DIE, but we do not recurse
18326 into function DIEs during partial symbol reading) and
18327 possibly variables as well; DW_AT_specification refers to
18328 declarations. Declarations ought to have the DW_AT_declaration
18329 flag. It happens that GCC forgets to put it in sometimes, but
18330 only for functions, not for types.
18331
18332 Adding more things than necessary to the hash table is harmless
18333 except for the performance cost. Adding too few will result in
18334 wasted time in find_partial_die, when we reread the compilation
18335 unit with load_all_dies set. */
18336
18337 if (load_all
18338 || abbrev->tag == DW_TAG_constant
18339 || abbrev->tag == DW_TAG_subprogram
18340 || abbrev->tag == DW_TAG_variable
18341 || abbrev->tag == DW_TAG_namespace
18342 || part_die->is_declaration)
18343 {
18344 void **slot;
18345
18346 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18347 to_underlying (part_die->sect_off),
18348 INSERT);
18349 *slot = part_die;
18350 }
18351
18352 /* For some DIEs we want to follow their children (if any). For C
18353 we have no reason to follow the children of structures; for other
18354 languages we have to, so that we can get at method physnames
18355 to infer fully qualified class names, for DW_AT_specification,
18356 and for C++ template arguments. For C++, we also look one level
18357 inside functions to find template arguments (if the name of the
18358 function does not already contain the template arguments).
18359
18360 For Ada and Fortran, we need to scan the children of subprograms
18361 and lexical blocks as well because these languages allow the
18362 definition of nested entities that could be interesting for the
18363 debugger, such as nested subprograms for instance. */
18364 if (last_die->has_children
18365 && (load_all
18366 || last_die->tag == DW_TAG_namespace
18367 || last_die->tag == DW_TAG_module
18368 || last_die->tag == DW_TAG_enumeration_type
18369 || (cu->language == language_cplus
18370 && last_die->tag == DW_TAG_subprogram
18371 && (last_die->name == NULL
18372 || strchr (last_die->name, '<') == NULL))
18373 || (cu->language != language_c
18374 && (last_die->tag == DW_TAG_class_type
18375 || last_die->tag == DW_TAG_interface_type
18376 || last_die->tag == DW_TAG_structure_type
18377 || last_die->tag == DW_TAG_union_type))
18378 || ((cu->language == language_ada
18379 || cu->language == language_fortran)
18380 && (last_die->tag == DW_TAG_subprogram
18381 || last_die->tag == DW_TAG_lexical_block))))
18382 {
18383 nesting_level++;
18384 parent_die = last_die;
18385 continue;
18386 }
18387
18388 /* Otherwise we skip to the next sibling, if any. */
18389 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18390
18391 /* Back to the top, do it again. */
18392 }
18393 }
18394
18395 partial_die_info::partial_die_info (sect_offset sect_off_,
18396 struct abbrev_info *abbrev)
18397 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18398 {
18399 }
18400
18401 /* Read a minimal amount of information into the minimal die structure.
18402 INFO_PTR should point just after the initial uleb128 of a DIE. */
18403
18404 const gdb_byte *
18405 partial_die_info::read (const struct die_reader_specs *reader,
18406 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18407 {
18408 struct dwarf2_cu *cu = reader->cu;
18409 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18410 unsigned int i;
18411 int has_low_pc_attr = 0;
18412 int has_high_pc_attr = 0;
18413 int high_pc_relative = 0;
18414
18415 for (i = 0; i < abbrev.num_attrs; ++i)
18416 {
18417 attribute attr;
18418 bool need_reprocess;
18419 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18420 info_ptr, &need_reprocess);
18421 /* String and address offsets that need to do the reprocessing have
18422 already been read at this point, so there is no need to wait until
18423 the loop terminates to do the reprocessing. */
18424 if (need_reprocess)
18425 read_attribute_reprocess (reader, &attr);
18426 /* Store the data if it is of an attribute we want to keep in a
18427 partial symbol table. */
18428 switch (attr.name)
18429 {
18430 case DW_AT_name:
18431 switch (tag)
18432 {
18433 case DW_TAG_compile_unit:
18434 case DW_TAG_partial_unit:
18435 case DW_TAG_type_unit:
18436 /* Compilation units have a DW_AT_name that is a filename, not
18437 a source language identifier. */
18438 case DW_TAG_enumeration_type:
18439 case DW_TAG_enumerator:
18440 /* These tags always have simple identifiers already; no need
18441 to canonicalize them. */
18442 name = DW_STRING (&attr);
18443 break;
18444 default:
18445 {
18446 struct objfile *objfile = dwarf2_per_objfile->objfile;
18447
18448 name
18449 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18450 }
18451 break;
18452 }
18453 break;
18454 case DW_AT_linkage_name:
18455 case DW_AT_MIPS_linkage_name:
18456 /* Note that both forms of linkage name might appear. We
18457 assume they will be the same, and we only store the last
18458 one we see. */
18459 linkage_name = attr.value_as_string ();
18460 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18461 See https://github.com/rust-lang/rust/issues/32925. */
18462 if (cu->language == language_rust && linkage_name != NULL
18463 && strchr (linkage_name, '{') != NULL)
18464 linkage_name = NULL;
18465 break;
18466 case DW_AT_low_pc:
18467 has_low_pc_attr = 1;
18468 lowpc = attr.value_as_address ();
18469 break;
18470 case DW_AT_high_pc:
18471 has_high_pc_attr = 1;
18472 highpc = attr.value_as_address ();
18473 if (cu->header.version >= 4 && attr.form_is_constant ())
18474 high_pc_relative = 1;
18475 break;
18476 case DW_AT_location:
18477 /* Support the .debug_loc offsets. */
18478 if (attr.form_is_block ())
18479 {
18480 d.locdesc = DW_BLOCK (&attr);
18481 }
18482 else if (attr.form_is_section_offset ())
18483 {
18484 dwarf2_complex_location_expr_complaint ();
18485 }
18486 else
18487 {
18488 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18489 "partial symbol information");
18490 }
18491 break;
18492 case DW_AT_external:
18493 is_external = DW_UNSND (&attr);
18494 break;
18495 case DW_AT_declaration:
18496 is_declaration = DW_UNSND (&attr);
18497 break;
18498 case DW_AT_type:
18499 has_type = 1;
18500 break;
18501 case DW_AT_abstract_origin:
18502 case DW_AT_specification:
18503 case DW_AT_extension:
18504 has_specification = 1;
18505 spec_offset = attr.get_ref_die_offset ();
18506 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18507 || cu->per_cu->is_dwz);
18508 break;
18509 case DW_AT_sibling:
18510 /* Ignore absolute siblings, they might point outside of
18511 the current compile unit. */
18512 if (attr.form == DW_FORM_ref_addr)
18513 complaint (_("ignoring absolute DW_AT_sibling"));
18514 else
18515 {
18516 const gdb_byte *buffer = reader->buffer;
18517 sect_offset off = attr.get_ref_die_offset ();
18518 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18519
18520 if (sibling_ptr < info_ptr)
18521 complaint (_("DW_AT_sibling points backwards"));
18522 else if (sibling_ptr > reader->buffer_end)
18523 reader->die_section->overflow_complaint ();
18524 else
18525 sibling = sibling_ptr;
18526 }
18527 break;
18528 case DW_AT_byte_size:
18529 has_byte_size = 1;
18530 break;
18531 case DW_AT_const_value:
18532 has_const_value = 1;
18533 break;
18534 case DW_AT_calling_convention:
18535 /* DWARF doesn't provide a way to identify a program's source-level
18536 entry point. DW_AT_calling_convention attributes are only meant
18537 to describe functions' calling conventions.
18538
18539 However, because it's a necessary piece of information in
18540 Fortran, and before DWARF 4 DW_CC_program was the only
18541 piece of debugging information whose definition refers to
18542 a 'main program' at all, several compilers marked Fortran
18543 main programs with DW_CC_program --- even when those
18544 functions use the standard calling conventions.
18545
18546 Although DWARF now specifies a way to provide this
18547 information, we support this practice for backward
18548 compatibility. */
18549 if (DW_UNSND (&attr) == DW_CC_program
18550 && cu->language == language_fortran)
18551 main_subprogram = 1;
18552 break;
18553 case DW_AT_inline:
18554 if (DW_UNSND (&attr) == DW_INL_inlined
18555 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18556 may_be_inlined = 1;
18557 break;
18558
18559 case DW_AT_import:
18560 if (tag == DW_TAG_imported_unit)
18561 {
18562 d.sect_off = attr.get_ref_die_offset ();
18563 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18564 || cu->per_cu->is_dwz);
18565 }
18566 break;
18567
18568 case DW_AT_main_subprogram:
18569 main_subprogram = DW_UNSND (&attr);
18570 break;
18571
18572 case DW_AT_ranges:
18573 {
18574 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18575 but that requires a full DIE, so instead we just
18576 reimplement it. */
18577 int need_ranges_base = tag != DW_TAG_compile_unit;
18578 unsigned int ranges_offset = (DW_UNSND (&attr)
18579 + (need_ranges_base
18580 ? cu->ranges_base
18581 : 0));
18582
18583 /* Value of the DW_AT_ranges attribute is the offset in the
18584 .debug_ranges section. */
18585 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18586 nullptr))
18587 has_pc_info = 1;
18588 }
18589 break;
18590
18591 default:
18592 break;
18593 }
18594 }
18595
18596 /* For Ada, if both the name and the linkage name appear, we prefer
18597 the latter. This lets "catch exception" work better, regardless
18598 of the order in which the name and linkage name were emitted.
18599 Really, though, this is just a workaround for the fact that gdb
18600 doesn't store both the name and the linkage name. */
18601 if (cu->language == language_ada && linkage_name != nullptr)
18602 name = linkage_name;
18603
18604 if (high_pc_relative)
18605 highpc += lowpc;
18606
18607 if (has_low_pc_attr && has_high_pc_attr)
18608 {
18609 /* When using the GNU linker, .gnu.linkonce. sections are used to
18610 eliminate duplicate copies of functions and vtables and such.
18611 The linker will arbitrarily choose one and discard the others.
18612 The AT_*_pc values for such functions refer to local labels in
18613 these sections. If the section from that file was discarded, the
18614 labels are not in the output, so the relocs get a value of 0.
18615 If this is a discarded function, mark the pc bounds as invalid,
18616 so that GDB will ignore it. */
18617 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18618 {
18619 struct objfile *objfile = dwarf2_per_objfile->objfile;
18620 struct gdbarch *gdbarch = objfile->arch ();
18621
18622 complaint (_("DW_AT_low_pc %s is zero "
18623 "for DIE at %s [in module %s]"),
18624 paddress (gdbarch, lowpc),
18625 sect_offset_str (sect_off),
18626 objfile_name (objfile));
18627 }
18628 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18629 else if (lowpc >= highpc)
18630 {
18631 struct objfile *objfile = dwarf2_per_objfile->objfile;
18632 struct gdbarch *gdbarch = objfile->arch ();
18633
18634 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18635 "for DIE at %s [in module %s]"),
18636 paddress (gdbarch, lowpc),
18637 paddress (gdbarch, highpc),
18638 sect_offset_str (sect_off),
18639 objfile_name (objfile));
18640 }
18641 else
18642 has_pc_info = 1;
18643 }
18644
18645 return info_ptr;
18646 }
18647
18648 /* Find a cached partial DIE at OFFSET in CU. */
18649
18650 struct partial_die_info *
18651 dwarf2_cu::find_partial_die (sect_offset sect_off)
18652 {
18653 struct partial_die_info *lookup_die = NULL;
18654 struct partial_die_info part_die (sect_off);
18655
18656 lookup_die = ((struct partial_die_info *)
18657 htab_find_with_hash (partial_dies, &part_die,
18658 to_underlying (sect_off)));
18659
18660 return lookup_die;
18661 }
18662
18663 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18664 except in the case of .debug_types DIEs which do not reference
18665 outside their CU (they do however referencing other types via
18666 DW_FORM_ref_sig8). */
18667
18668 static const struct cu_partial_die_info
18669 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18670 {
18671 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18672 struct objfile *objfile = dwarf2_per_objfile->objfile;
18673 struct dwarf2_per_cu_data *per_cu = NULL;
18674 struct partial_die_info *pd = NULL;
18675
18676 if (offset_in_dwz == cu->per_cu->is_dwz
18677 && cu->header.offset_in_cu_p (sect_off))
18678 {
18679 pd = cu->find_partial_die (sect_off);
18680 if (pd != NULL)
18681 return { cu, pd };
18682 /* We missed recording what we needed.
18683 Load all dies and try again. */
18684 per_cu = cu->per_cu;
18685 }
18686 else
18687 {
18688 /* TUs don't reference other CUs/TUs (except via type signatures). */
18689 if (cu->per_cu->is_debug_types)
18690 {
18691 error (_("Dwarf Error: Type Unit at offset %s contains"
18692 " external reference to offset %s [in module %s].\n"),
18693 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18694 bfd_get_filename (objfile->obfd));
18695 }
18696 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18697 dwarf2_per_objfile);
18698
18699 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18700 load_partial_comp_unit (per_cu, cu->per_objfile);
18701
18702 per_cu->cu->last_used = 0;
18703 pd = per_cu->cu->find_partial_die (sect_off);
18704 }
18705
18706 /* If we didn't find it, and not all dies have been loaded,
18707 load them all and try again. */
18708
18709 if (pd == NULL && per_cu->load_all_dies == 0)
18710 {
18711 per_cu->load_all_dies = 1;
18712
18713 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18714 THIS_CU->cu may already be in use. So we can't just free it and
18715 replace its DIEs with the ones we read in. Instead, we leave those
18716 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18717 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18718 set. */
18719 load_partial_comp_unit (per_cu, cu->per_objfile);
18720
18721 pd = per_cu->cu->find_partial_die (sect_off);
18722 }
18723
18724 if (pd == NULL)
18725 internal_error (__FILE__, __LINE__,
18726 _("could not find partial DIE %s "
18727 "in cache [from module %s]\n"),
18728 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18729 return { per_cu->cu, pd };
18730 }
18731
18732 /* See if we can figure out if the class lives in a namespace. We do
18733 this by looking for a member function; its demangled name will
18734 contain namespace info, if there is any. */
18735
18736 static void
18737 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18738 struct dwarf2_cu *cu)
18739 {
18740 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18741 what template types look like, because the demangler
18742 frequently doesn't give the same name as the debug info. We
18743 could fix this by only using the demangled name to get the
18744 prefix (but see comment in read_structure_type). */
18745
18746 struct partial_die_info *real_pdi;
18747 struct partial_die_info *child_pdi;
18748
18749 /* If this DIE (this DIE's specification, if any) has a parent, then
18750 we should not do this. We'll prepend the parent's fully qualified
18751 name when we create the partial symbol. */
18752
18753 real_pdi = struct_pdi;
18754 while (real_pdi->has_specification)
18755 {
18756 auto res = find_partial_die (real_pdi->spec_offset,
18757 real_pdi->spec_is_dwz, cu);
18758 real_pdi = res.pdi;
18759 cu = res.cu;
18760 }
18761
18762 if (real_pdi->die_parent != NULL)
18763 return;
18764
18765 for (child_pdi = struct_pdi->die_child;
18766 child_pdi != NULL;
18767 child_pdi = child_pdi->die_sibling)
18768 {
18769 if (child_pdi->tag == DW_TAG_subprogram
18770 && child_pdi->linkage_name != NULL)
18771 {
18772 gdb::unique_xmalloc_ptr<char> actual_class_name
18773 (language_class_name_from_physname (cu->language_defn,
18774 child_pdi->linkage_name));
18775 if (actual_class_name != NULL)
18776 {
18777 struct objfile *objfile = cu->per_objfile->objfile;
18778 struct_pdi->name = objfile->intern (actual_class_name.get ());
18779 }
18780 break;
18781 }
18782 }
18783 }
18784
18785 /* Return true if a DIE with TAG may have the DW_AT_const_value
18786 attribute. */
18787
18788 static bool
18789 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18790 {
18791 switch (tag)
18792 {
18793 case DW_TAG_constant:
18794 case DW_TAG_enumerator:
18795 case DW_TAG_formal_parameter:
18796 case DW_TAG_template_value_param:
18797 case DW_TAG_variable:
18798 return true;
18799 }
18800
18801 return false;
18802 }
18803
18804 void
18805 partial_die_info::fixup (struct dwarf2_cu *cu)
18806 {
18807 /* Once we've fixed up a die, there's no point in doing so again.
18808 This also avoids a memory leak if we were to call
18809 guess_partial_die_structure_name multiple times. */
18810 if (fixup_called)
18811 return;
18812
18813 /* If we found a reference attribute and the DIE has no name, try
18814 to find a name in the referred to DIE. */
18815
18816 if (name == NULL && has_specification)
18817 {
18818 struct partial_die_info *spec_die;
18819
18820 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18821 spec_die = res.pdi;
18822 cu = res.cu;
18823
18824 spec_die->fixup (cu);
18825
18826 if (spec_die->name)
18827 {
18828 name = spec_die->name;
18829
18830 /* Copy DW_AT_external attribute if it is set. */
18831 if (spec_die->is_external)
18832 is_external = spec_die->is_external;
18833 }
18834 }
18835
18836 if (!has_const_value && has_specification
18837 && can_have_DW_AT_const_value_p (tag))
18838 {
18839 struct partial_die_info *spec_die;
18840
18841 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18842 spec_die = res.pdi;
18843 cu = res.cu;
18844
18845 spec_die->fixup (cu);
18846
18847 if (spec_die->has_const_value)
18848 {
18849 /* Copy DW_AT_const_value attribute if it is set. */
18850 has_const_value = spec_die->has_const_value;
18851 }
18852 }
18853
18854 /* Set default names for some unnamed DIEs. */
18855
18856 if (name == NULL && tag == DW_TAG_namespace)
18857 name = CP_ANONYMOUS_NAMESPACE_STR;
18858
18859 /* If there is no parent die to provide a namespace, and there are
18860 children, see if we can determine the namespace from their linkage
18861 name. */
18862 if (cu->language == language_cplus
18863 && !cu->per_objfile->per_bfd->types.empty ()
18864 && die_parent == NULL
18865 && has_children
18866 && (tag == DW_TAG_class_type
18867 || tag == DW_TAG_structure_type
18868 || tag == DW_TAG_union_type))
18869 guess_partial_die_structure_name (this, cu);
18870
18871 /* GCC might emit a nameless struct or union that has a linkage
18872 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18873 if (name == NULL
18874 && (tag == DW_TAG_class_type
18875 || tag == DW_TAG_interface_type
18876 || tag == DW_TAG_structure_type
18877 || tag == DW_TAG_union_type)
18878 && linkage_name != NULL)
18879 {
18880 gdb::unique_xmalloc_ptr<char> demangled
18881 (gdb_demangle (linkage_name, DMGL_TYPES));
18882 if (demangled != nullptr)
18883 {
18884 const char *base;
18885
18886 /* Strip any leading namespaces/classes, keep only the base name.
18887 DW_AT_name for named DIEs does not contain the prefixes. */
18888 base = strrchr (demangled.get (), ':');
18889 if (base && base > demangled.get () && base[-1] == ':')
18890 base++;
18891 else
18892 base = demangled.get ();
18893
18894 struct objfile *objfile = cu->per_objfile->objfile;
18895 name = objfile->intern (base);
18896 }
18897 }
18898
18899 fixup_called = 1;
18900 }
18901
18902 /* Read the .debug_loclists header contents from the given SECTION in the
18903 HEADER. */
18904 static void
18905 read_loclist_header (struct loclist_header *header,
18906 struct dwarf2_section_info *section)
18907 {
18908 unsigned int bytes_read;
18909 bfd *abfd = section->get_bfd_owner ();
18910 const gdb_byte *info_ptr = section->buffer;
18911 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18912 info_ptr += bytes_read;
18913 header->version = read_2_bytes (abfd, info_ptr);
18914 info_ptr += 2;
18915 header->addr_size = read_1_byte (abfd, info_ptr);
18916 info_ptr += 1;
18917 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18918 info_ptr += 1;
18919 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18920 }
18921
18922 /* Return the DW_AT_loclists_base value for the CU. */
18923 static ULONGEST
18924 lookup_loclist_base (struct dwarf2_cu *cu)
18925 {
18926 /* For the .dwo unit, the loclist_base points to the first offset following
18927 the header. The header consists of the following entities-
18928 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18929 bit format)
18930 2. version (2 bytes)
18931 3. address size (1 byte)
18932 4. segment selector size (1 byte)
18933 5. offset entry count (4 bytes)
18934 These sizes are derived as per the DWARFv5 standard. */
18935 if (cu->dwo_unit != nullptr)
18936 {
18937 if (cu->header.initial_length_size == 4)
18938 return LOCLIST_HEADER_SIZE32;
18939 return LOCLIST_HEADER_SIZE64;
18940 }
18941 return cu->loclist_base;
18942 }
18943
18944 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18945 array of offsets in the .debug_loclists section. */
18946 static CORE_ADDR
18947 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18948 {
18949 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18950 struct objfile *objfile = dwarf2_per_objfile->objfile;
18951 bfd *abfd = objfile->obfd;
18952 ULONGEST loclist_base = lookup_loclist_base (cu);
18953 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18954
18955 section->read (objfile);
18956 if (section->buffer == NULL)
18957 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18958 "section [in module %s]"), objfile_name (objfile));
18959 struct loclist_header header;
18960 read_loclist_header (&header, section);
18961 if (loclist_index >= header.offset_entry_count)
18962 complaint (_("DW_FORM_loclistx pointing outside of "
18963 ".debug_loclists offset array [in module %s]"),
18964 objfile_name (objfile));
18965 if (loclist_base + loclist_index * cu->header.offset_size
18966 >= section->size)
18967 complaint (_("DW_FORM_loclistx pointing outside of "
18968 ".debug_loclists section [in module %s]"),
18969 objfile_name (objfile));
18970 const gdb_byte *info_ptr
18971 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18972
18973 if (cu->header.offset_size == 4)
18974 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18975 else
18976 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18977 }
18978
18979 /* Process the attributes that had to be skipped in the first round. These
18980 attributes are the ones that need str_offsets_base or addr_base attributes.
18981 They could not have been processed in the first round, because at the time
18982 the values of str_offsets_base or addr_base may not have been known. */
18983 static void
18984 read_attribute_reprocess (const struct die_reader_specs *reader,
18985 struct attribute *attr)
18986 {
18987 struct dwarf2_cu *cu = reader->cu;
18988 switch (attr->form)
18989 {
18990 case DW_FORM_addrx:
18991 case DW_FORM_GNU_addr_index:
18992 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18993 break;
18994 case DW_FORM_loclistx:
18995 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18996 break;
18997 case DW_FORM_strx:
18998 case DW_FORM_strx1:
18999 case DW_FORM_strx2:
19000 case DW_FORM_strx3:
19001 case DW_FORM_strx4:
19002 case DW_FORM_GNU_str_index:
19003 {
19004 unsigned int str_index = DW_UNSND (attr);
19005 if (reader->dwo_file != NULL)
19006 {
19007 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19008 DW_STRING_IS_CANONICAL (attr) = 0;
19009 }
19010 else
19011 {
19012 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19013 DW_STRING_IS_CANONICAL (attr) = 0;
19014 }
19015 break;
19016 }
19017 default:
19018 gdb_assert_not_reached (_("Unexpected DWARF form."));
19019 }
19020 }
19021
19022 /* Read an attribute value described by an attribute form. */
19023
19024 static const gdb_byte *
19025 read_attribute_value (const struct die_reader_specs *reader,
19026 struct attribute *attr, unsigned form,
19027 LONGEST implicit_const, const gdb_byte *info_ptr,
19028 bool *need_reprocess)
19029 {
19030 struct dwarf2_cu *cu = reader->cu;
19031 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19032 struct objfile *objfile = dwarf2_per_objfile->objfile;
19033 bfd *abfd = reader->abfd;
19034 struct comp_unit_head *cu_header = &cu->header;
19035 unsigned int bytes_read;
19036 struct dwarf_block *blk;
19037 *need_reprocess = false;
19038
19039 attr->form = (enum dwarf_form) form;
19040 switch (form)
19041 {
19042 case DW_FORM_ref_addr:
19043 if (cu->header.version == 2)
19044 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19045 &bytes_read);
19046 else
19047 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19048 &bytes_read);
19049 info_ptr += bytes_read;
19050 break;
19051 case DW_FORM_GNU_ref_alt:
19052 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19053 info_ptr += bytes_read;
19054 break;
19055 case DW_FORM_addr:
19056 {
19057 struct gdbarch *gdbarch = objfile->arch ();
19058 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19059 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19060 info_ptr += bytes_read;
19061 }
19062 break;
19063 case DW_FORM_block2:
19064 blk = dwarf_alloc_block (cu);
19065 blk->size = read_2_bytes (abfd, info_ptr);
19066 info_ptr += 2;
19067 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19068 info_ptr += blk->size;
19069 DW_BLOCK (attr) = blk;
19070 break;
19071 case DW_FORM_block4:
19072 blk = dwarf_alloc_block (cu);
19073 blk->size = read_4_bytes (abfd, info_ptr);
19074 info_ptr += 4;
19075 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19076 info_ptr += blk->size;
19077 DW_BLOCK (attr) = blk;
19078 break;
19079 case DW_FORM_data2:
19080 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19081 info_ptr += 2;
19082 break;
19083 case DW_FORM_data4:
19084 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19085 info_ptr += 4;
19086 break;
19087 case DW_FORM_data8:
19088 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19089 info_ptr += 8;
19090 break;
19091 case DW_FORM_data16:
19092 blk = dwarf_alloc_block (cu);
19093 blk->size = 16;
19094 blk->data = read_n_bytes (abfd, info_ptr, 16);
19095 info_ptr += 16;
19096 DW_BLOCK (attr) = blk;
19097 break;
19098 case DW_FORM_sec_offset:
19099 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19100 info_ptr += bytes_read;
19101 break;
19102 case DW_FORM_loclistx:
19103 {
19104 *need_reprocess = true;
19105 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19106 info_ptr += bytes_read;
19107 }
19108 break;
19109 case DW_FORM_string:
19110 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19111 DW_STRING_IS_CANONICAL (attr) = 0;
19112 info_ptr += bytes_read;
19113 break;
19114 case DW_FORM_strp:
19115 if (!cu->per_cu->is_dwz)
19116 {
19117 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19118 abfd, info_ptr, cu_header,
19119 &bytes_read);
19120 DW_STRING_IS_CANONICAL (attr) = 0;
19121 info_ptr += bytes_read;
19122 break;
19123 }
19124 /* FALLTHROUGH */
19125 case DW_FORM_line_strp:
19126 if (!cu->per_cu->is_dwz)
19127 {
19128 DW_STRING (attr)
19129 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19130 &bytes_read);
19131 DW_STRING_IS_CANONICAL (attr) = 0;
19132 info_ptr += bytes_read;
19133 break;
19134 }
19135 /* FALLTHROUGH */
19136 case DW_FORM_GNU_strp_alt:
19137 {
19138 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19139 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19140 &bytes_read);
19141
19142 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19143 DW_STRING_IS_CANONICAL (attr) = 0;
19144 info_ptr += bytes_read;
19145 }
19146 break;
19147 case DW_FORM_exprloc:
19148 case DW_FORM_block:
19149 blk = dwarf_alloc_block (cu);
19150 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19151 info_ptr += bytes_read;
19152 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19153 info_ptr += blk->size;
19154 DW_BLOCK (attr) = blk;
19155 break;
19156 case DW_FORM_block1:
19157 blk = dwarf_alloc_block (cu);
19158 blk->size = read_1_byte (abfd, info_ptr);
19159 info_ptr += 1;
19160 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19161 info_ptr += blk->size;
19162 DW_BLOCK (attr) = blk;
19163 break;
19164 case DW_FORM_data1:
19165 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19166 info_ptr += 1;
19167 break;
19168 case DW_FORM_flag:
19169 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19170 info_ptr += 1;
19171 break;
19172 case DW_FORM_flag_present:
19173 DW_UNSND (attr) = 1;
19174 break;
19175 case DW_FORM_sdata:
19176 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19177 info_ptr += bytes_read;
19178 break;
19179 case DW_FORM_udata:
19180 case DW_FORM_rnglistx:
19181 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19182 info_ptr += bytes_read;
19183 break;
19184 case DW_FORM_ref1:
19185 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19186 + read_1_byte (abfd, info_ptr));
19187 info_ptr += 1;
19188 break;
19189 case DW_FORM_ref2:
19190 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19191 + read_2_bytes (abfd, info_ptr));
19192 info_ptr += 2;
19193 break;
19194 case DW_FORM_ref4:
19195 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19196 + read_4_bytes (abfd, info_ptr));
19197 info_ptr += 4;
19198 break;
19199 case DW_FORM_ref8:
19200 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19201 + read_8_bytes (abfd, info_ptr));
19202 info_ptr += 8;
19203 break;
19204 case DW_FORM_ref_sig8:
19205 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19206 info_ptr += 8;
19207 break;
19208 case DW_FORM_ref_udata:
19209 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19210 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19211 info_ptr += bytes_read;
19212 break;
19213 case DW_FORM_indirect:
19214 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19215 info_ptr += bytes_read;
19216 if (form == DW_FORM_implicit_const)
19217 {
19218 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19219 info_ptr += bytes_read;
19220 }
19221 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19222 info_ptr, need_reprocess);
19223 break;
19224 case DW_FORM_implicit_const:
19225 DW_SND (attr) = implicit_const;
19226 break;
19227 case DW_FORM_addrx:
19228 case DW_FORM_GNU_addr_index:
19229 *need_reprocess = true;
19230 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19231 info_ptr += bytes_read;
19232 break;
19233 case DW_FORM_strx:
19234 case DW_FORM_strx1:
19235 case DW_FORM_strx2:
19236 case DW_FORM_strx3:
19237 case DW_FORM_strx4:
19238 case DW_FORM_GNU_str_index:
19239 {
19240 ULONGEST str_index;
19241 if (form == DW_FORM_strx1)
19242 {
19243 str_index = read_1_byte (abfd, info_ptr);
19244 info_ptr += 1;
19245 }
19246 else if (form == DW_FORM_strx2)
19247 {
19248 str_index = read_2_bytes (abfd, info_ptr);
19249 info_ptr += 2;
19250 }
19251 else if (form == DW_FORM_strx3)
19252 {
19253 str_index = read_3_bytes (abfd, info_ptr);
19254 info_ptr += 3;
19255 }
19256 else if (form == DW_FORM_strx4)
19257 {
19258 str_index = read_4_bytes (abfd, info_ptr);
19259 info_ptr += 4;
19260 }
19261 else
19262 {
19263 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19264 info_ptr += bytes_read;
19265 }
19266 *need_reprocess = true;
19267 DW_UNSND (attr) = str_index;
19268 }
19269 break;
19270 default:
19271 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19272 dwarf_form_name (form),
19273 bfd_get_filename (abfd));
19274 }
19275
19276 /* Super hack. */
19277 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19278 attr->form = DW_FORM_GNU_ref_alt;
19279
19280 /* We have seen instances where the compiler tried to emit a byte
19281 size attribute of -1 which ended up being encoded as an unsigned
19282 0xffffffff. Although 0xffffffff is technically a valid size value,
19283 an object of this size seems pretty unlikely so we can relatively
19284 safely treat these cases as if the size attribute was invalid and
19285 treat them as zero by default. */
19286 if (attr->name == DW_AT_byte_size
19287 && form == DW_FORM_data4
19288 && DW_UNSND (attr) >= 0xffffffff)
19289 {
19290 complaint
19291 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19292 hex_string (DW_UNSND (attr)));
19293 DW_UNSND (attr) = 0;
19294 }
19295
19296 return info_ptr;
19297 }
19298
19299 /* Read an attribute described by an abbreviated attribute. */
19300
19301 static const gdb_byte *
19302 read_attribute (const struct die_reader_specs *reader,
19303 struct attribute *attr, struct attr_abbrev *abbrev,
19304 const gdb_byte *info_ptr, bool *need_reprocess)
19305 {
19306 attr->name = abbrev->name;
19307 return read_attribute_value (reader, attr, abbrev->form,
19308 abbrev->implicit_const, info_ptr,
19309 need_reprocess);
19310 }
19311
19312 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19313
19314 static const char *
19315 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19316 LONGEST str_offset)
19317 {
19318 return dwarf2_per_objfile->per_bfd->str.read_string
19319 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19320 }
19321
19322 /* Return pointer to string at .debug_str offset as read from BUF.
19323 BUF is assumed to be in a compilation unit described by CU_HEADER.
19324 Return *BYTES_READ_PTR count of bytes read from BUF. */
19325
19326 static const char *
19327 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19328 const gdb_byte *buf,
19329 const struct comp_unit_head *cu_header,
19330 unsigned int *bytes_read_ptr)
19331 {
19332 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19333
19334 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19335 }
19336
19337 /* See read.h. */
19338
19339 const char *
19340 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19341 const struct comp_unit_head *cu_header,
19342 unsigned int *bytes_read_ptr)
19343 {
19344 bfd *abfd = objfile->obfd;
19345 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19346
19347 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19348 }
19349
19350 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19351 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19352 ADDR_SIZE is the size of addresses from the CU header. */
19353
19354 static CORE_ADDR
19355 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19356 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19357 int addr_size)
19358 {
19359 struct objfile *objfile = dwarf2_per_objfile->objfile;
19360 bfd *abfd = objfile->obfd;
19361 const gdb_byte *info_ptr;
19362 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19363
19364 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19365 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19366 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19367 objfile_name (objfile));
19368 if (addr_base_or_zero + addr_index * addr_size
19369 >= dwarf2_per_objfile->per_bfd->addr.size)
19370 error (_("DW_FORM_addr_index pointing outside of "
19371 ".debug_addr section [in module %s]"),
19372 objfile_name (objfile));
19373 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19374 + addr_base_or_zero + addr_index * addr_size);
19375 if (addr_size == 4)
19376 return bfd_get_32 (abfd, info_ptr);
19377 else
19378 return bfd_get_64 (abfd, info_ptr);
19379 }
19380
19381 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19382
19383 static CORE_ADDR
19384 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19385 {
19386 return read_addr_index_1 (cu->per_objfile, addr_index,
19387 cu->addr_base, cu->header.addr_size);
19388 }
19389
19390 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19391
19392 static CORE_ADDR
19393 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19394 unsigned int *bytes_read)
19395 {
19396 bfd *abfd = cu->per_objfile->objfile->obfd;
19397 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19398
19399 return read_addr_index (cu, addr_index);
19400 }
19401
19402 /* See read.h. */
19403
19404 CORE_ADDR
19405 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
19406 {
19407 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
19408 struct dwarf2_cu *cu = per_cu->cu;
19409 gdb::optional<ULONGEST> addr_base;
19410 int addr_size;
19411
19412 /* We need addr_base and addr_size.
19413 If we don't have PER_CU->cu, we have to get it.
19414 Nasty, but the alternative is storing the needed info in PER_CU,
19415 which at this point doesn't seem justified: it's not clear how frequently
19416 it would get used and it would increase the size of every PER_CU.
19417 Entry points like dwarf2_per_cu_addr_size do a similar thing
19418 so we're not in uncharted territory here.
19419 Alas we need to be a bit more complicated as addr_base is contained
19420 in the DIE.
19421
19422 We don't need to read the entire CU(/TU).
19423 We just need the header and top level die.
19424
19425 IWBN to use the aging mechanism to let us lazily later discard the CU.
19426 For now we skip this optimization. */
19427
19428 if (cu != NULL)
19429 {
19430 addr_base = cu->addr_base;
19431 addr_size = cu->header.addr_size;
19432 }
19433 else
19434 {
19435 cutu_reader reader (per_cu, dwarf2_per_objfile, NULL, 0, false);
19436 addr_base = reader.cu->addr_base;
19437 addr_size = reader.cu->header.addr_size;
19438 }
19439
19440 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19441 addr_size);
19442 }
19443
19444 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19445 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19446 DWO file. */
19447
19448 static const char *
19449 read_str_index (struct dwarf2_cu *cu,
19450 struct dwarf2_section_info *str_section,
19451 struct dwarf2_section_info *str_offsets_section,
19452 ULONGEST str_offsets_base, ULONGEST str_index)
19453 {
19454 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19455 struct objfile *objfile = dwarf2_per_objfile->objfile;
19456 const char *objf_name = objfile_name (objfile);
19457 bfd *abfd = objfile->obfd;
19458 const gdb_byte *info_ptr;
19459 ULONGEST str_offset;
19460 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19461
19462 str_section->read (objfile);
19463 str_offsets_section->read (objfile);
19464 if (str_section->buffer == NULL)
19465 error (_("%s used without %s section"
19466 " in CU at offset %s [in module %s]"),
19467 form_name, str_section->get_name (),
19468 sect_offset_str (cu->header.sect_off), objf_name);
19469 if (str_offsets_section->buffer == NULL)
19470 error (_("%s used without %s section"
19471 " in CU at offset %s [in module %s]"),
19472 form_name, str_section->get_name (),
19473 sect_offset_str (cu->header.sect_off), objf_name);
19474 info_ptr = (str_offsets_section->buffer
19475 + str_offsets_base
19476 + str_index * cu->header.offset_size);
19477 if (cu->header.offset_size == 4)
19478 str_offset = bfd_get_32 (abfd, info_ptr);
19479 else
19480 str_offset = bfd_get_64 (abfd, info_ptr);
19481 if (str_offset >= str_section->size)
19482 error (_("Offset from %s pointing outside of"
19483 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19484 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19485 return (const char *) (str_section->buffer + str_offset);
19486 }
19487
19488 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19489
19490 static const char *
19491 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19492 {
19493 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19494 ? reader->cu->header.addr_size : 0;
19495 return read_str_index (reader->cu,
19496 &reader->dwo_file->sections.str,
19497 &reader->dwo_file->sections.str_offsets,
19498 str_offsets_base, str_index);
19499 }
19500
19501 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19502
19503 static const char *
19504 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19505 {
19506 struct objfile *objfile = cu->per_objfile->objfile;
19507 const char *objf_name = objfile_name (objfile);
19508 static const char form_name[] = "DW_FORM_GNU_str_index";
19509 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19510
19511 if (!cu->str_offsets_base.has_value ())
19512 error (_("%s used in Fission stub without %s"
19513 " in CU at offset 0x%lx [in module %s]"),
19514 form_name, str_offsets_attr_name,
19515 (long) cu->header.offset_size, objf_name);
19516
19517 return read_str_index (cu,
19518 &cu->per_objfile->per_bfd->str,
19519 &cu->per_objfile->per_bfd->str_offsets,
19520 *cu->str_offsets_base, str_index);
19521 }
19522
19523 /* Return the length of an LEB128 number in BUF. */
19524
19525 static int
19526 leb128_size (const gdb_byte *buf)
19527 {
19528 const gdb_byte *begin = buf;
19529 gdb_byte byte;
19530
19531 while (1)
19532 {
19533 byte = *buf++;
19534 if ((byte & 128) == 0)
19535 return buf - begin;
19536 }
19537 }
19538
19539 static void
19540 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19541 {
19542 switch (lang)
19543 {
19544 case DW_LANG_C89:
19545 case DW_LANG_C99:
19546 case DW_LANG_C11:
19547 case DW_LANG_C:
19548 case DW_LANG_UPC:
19549 cu->language = language_c;
19550 break;
19551 case DW_LANG_Java:
19552 case DW_LANG_C_plus_plus:
19553 case DW_LANG_C_plus_plus_11:
19554 case DW_LANG_C_plus_plus_14:
19555 cu->language = language_cplus;
19556 break;
19557 case DW_LANG_D:
19558 cu->language = language_d;
19559 break;
19560 case DW_LANG_Fortran77:
19561 case DW_LANG_Fortran90:
19562 case DW_LANG_Fortran95:
19563 case DW_LANG_Fortran03:
19564 case DW_LANG_Fortran08:
19565 cu->language = language_fortran;
19566 break;
19567 case DW_LANG_Go:
19568 cu->language = language_go;
19569 break;
19570 case DW_LANG_Mips_Assembler:
19571 cu->language = language_asm;
19572 break;
19573 case DW_LANG_Ada83:
19574 case DW_LANG_Ada95:
19575 cu->language = language_ada;
19576 break;
19577 case DW_LANG_Modula2:
19578 cu->language = language_m2;
19579 break;
19580 case DW_LANG_Pascal83:
19581 cu->language = language_pascal;
19582 break;
19583 case DW_LANG_ObjC:
19584 cu->language = language_objc;
19585 break;
19586 case DW_LANG_Rust:
19587 case DW_LANG_Rust_old:
19588 cu->language = language_rust;
19589 break;
19590 case DW_LANG_Cobol74:
19591 case DW_LANG_Cobol85:
19592 default:
19593 cu->language = language_minimal;
19594 break;
19595 }
19596 cu->language_defn = language_def (cu->language);
19597 }
19598
19599 /* Return the named attribute or NULL if not there. */
19600
19601 static struct attribute *
19602 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19603 {
19604 for (;;)
19605 {
19606 unsigned int i;
19607 struct attribute *spec = NULL;
19608
19609 for (i = 0; i < die->num_attrs; ++i)
19610 {
19611 if (die->attrs[i].name == name)
19612 return &die->attrs[i];
19613 if (die->attrs[i].name == DW_AT_specification
19614 || die->attrs[i].name == DW_AT_abstract_origin)
19615 spec = &die->attrs[i];
19616 }
19617
19618 if (!spec)
19619 break;
19620
19621 die = follow_die_ref (die, spec, &cu);
19622 }
19623
19624 return NULL;
19625 }
19626
19627 /* Return the string associated with a string-typed attribute, or NULL if it
19628 is either not found or is of an incorrect type. */
19629
19630 static const char *
19631 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19632 {
19633 struct attribute *attr;
19634 const char *str = NULL;
19635
19636 attr = dwarf2_attr (die, name, cu);
19637
19638 if (attr != NULL)
19639 {
19640 str = attr->value_as_string ();
19641 if (str == nullptr)
19642 complaint (_("string type expected for attribute %s for "
19643 "DIE at %s in module %s"),
19644 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19645 objfile_name (cu->per_objfile->objfile));
19646 }
19647
19648 return str;
19649 }
19650
19651 /* Return the dwo name or NULL if not present. If present, it is in either
19652 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19653 static const char *
19654 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19655 {
19656 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19657 if (dwo_name == nullptr)
19658 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19659 return dwo_name;
19660 }
19661
19662 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19663 and holds a non-zero value. This function should only be used for
19664 DW_FORM_flag or DW_FORM_flag_present attributes. */
19665
19666 static int
19667 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19668 {
19669 struct attribute *attr = dwarf2_attr (die, name, cu);
19670
19671 return (attr && DW_UNSND (attr));
19672 }
19673
19674 static int
19675 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19676 {
19677 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19678 which value is non-zero. However, we have to be careful with
19679 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19680 (via dwarf2_flag_true_p) follows this attribute. So we may
19681 end up accidently finding a declaration attribute that belongs
19682 to a different DIE referenced by the specification attribute,
19683 even though the given DIE does not have a declaration attribute. */
19684 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19685 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19686 }
19687
19688 /* Return the die giving the specification for DIE, if there is
19689 one. *SPEC_CU is the CU containing DIE on input, and the CU
19690 containing the return value on output. If there is no
19691 specification, but there is an abstract origin, that is
19692 returned. */
19693
19694 static struct die_info *
19695 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19696 {
19697 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19698 *spec_cu);
19699
19700 if (spec_attr == NULL)
19701 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19702
19703 if (spec_attr == NULL)
19704 return NULL;
19705 else
19706 return follow_die_ref (die, spec_attr, spec_cu);
19707 }
19708
19709 /* Stub for free_line_header to match void * callback types. */
19710
19711 static void
19712 free_line_header_voidp (void *arg)
19713 {
19714 struct line_header *lh = (struct line_header *) arg;
19715
19716 delete lh;
19717 }
19718
19719 /* A convenience function to find the proper .debug_line section for a CU. */
19720
19721 static struct dwarf2_section_info *
19722 get_debug_line_section (struct dwarf2_cu *cu)
19723 {
19724 struct dwarf2_section_info *section;
19725 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19726
19727 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19728 DWO file. */
19729 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19730 section = &cu->dwo_unit->dwo_file->sections.line;
19731 else if (cu->per_cu->is_dwz)
19732 {
19733 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19734
19735 section = &dwz->line;
19736 }
19737 else
19738 section = &dwarf2_per_objfile->per_bfd->line;
19739
19740 return section;
19741 }
19742
19743 /* Read the statement program header starting at OFFSET in
19744 .debug_line, or .debug_line.dwo. Return a pointer
19745 to a struct line_header, allocated using xmalloc.
19746 Returns NULL if there is a problem reading the header, e.g., if it
19747 has a version we don't understand.
19748
19749 NOTE: the strings in the include directory and file name tables of
19750 the returned object point into the dwarf line section buffer,
19751 and must not be freed. */
19752
19753 static line_header_up
19754 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19755 {
19756 struct dwarf2_section_info *section;
19757 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19758
19759 section = get_debug_line_section (cu);
19760 section->read (dwarf2_per_objfile->objfile);
19761 if (section->buffer == NULL)
19762 {
19763 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19764 complaint (_("missing .debug_line.dwo section"));
19765 else
19766 complaint (_("missing .debug_line section"));
19767 return 0;
19768 }
19769
19770 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19771 dwarf2_per_objfile, section,
19772 &cu->header);
19773 }
19774
19775 /* Subroutine of dwarf_decode_lines to simplify it.
19776 Return the file name of the psymtab for the given file_entry.
19777 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19778 If space for the result is malloc'd, *NAME_HOLDER will be set.
19779 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19780
19781 static const char *
19782 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19783 const dwarf2_psymtab *pst,
19784 const char *comp_dir,
19785 gdb::unique_xmalloc_ptr<char> *name_holder)
19786 {
19787 const char *include_name = fe.name;
19788 const char *include_name_to_compare = include_name;
19789 const char *pst_filename;
19790 int file_is_pst;
19791
19792 const char *dir_name = fe.include_dir (lh);
19793
19794 gdb::unique_xmalloc_ptr<char> hold_compare;
19795 if (!IS_ABSOLUTE_PATH (include_name)
19796 && (dir_name != NULL || comp_dir != NULL))
19797 {
19798 /* Avoid creating a duplicate psymtab for PST.
19799 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19800 Before we do the comparison, however, we need to account
19801 for DIR_NAME and COMP_DIR.
19802 First prepend dir_name (if non-NULL). If we still don't
19803 have an absolute path prepend comp_dir (if non-NULL).
19804 However, the directory we record in the include-file's
19805 psymtab does not contain COMP_DIR (to match the
19806 corresponding symtab(s)).
19807
19808 Example:
19809
19810 bash$ cd /tmp
19811 bash$ gcc -g ./hello.c
19812 include_name = "hello.c"
19813 dir_name = "."
19814 DW_AT_comp_dir = comp_dir = "/tmp"
19815 DW_AT_name = "./hello.c"
19816
19817 */
19818
19819 if (dir_name != NULL)
19820 {
19821 name_holder->reset (concat (dir_name, SLASH_STRING,
19822 include_name, (char *) NULL));
19823 include_name = name_holder->get ();
19824 include_name_to_compare = include_name;
19825 }
19826 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19827 {
19828 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19829 include_name, (char *) NULL));
19830 include_name_to_compare = hold_compare.get ();
19831 }
19832 }
19833
19834 pst_filename = pst->filename;
19835 gdb::unique_xmalloc_ptr<char> copied_name;
19836 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19837 {
19838 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19839 pst_filename, (char *) NULL));
19840 pst_filename = copied_name.get ();
19841 }
19842
19843 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19844
19845 if (file_is_pst)
19846 return NULL;
19847 return include_name;
19848 }
19849
19850 /* State machine to track the state of the line number program. */
19851
19852 class lnp_state_machine
19853 {
19854 public:
19855 /* Initialize a machine state for the start of a line number
19856 program. */
19857 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19858 bool record_lines_p);
19859
19860 file_entry *current_file ()
19861 {
19862 /* lh->file_names is 0-based, but the file name numbers in the
19863 statement program are 1-based. */
19864 return m_line_header->file_name_at (m_file);
19865 }
19866
19867 /* Record the line in the state machine. END_SEQUENCE is true if
19868 we're processing the end of a sequence. */
19869 void record_line (bool end_sequence);
19870
19871 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19872 nop-out rest of the lines in this sequence. */
19873 void check_line_address (struct dwarf2_cu *cu,
19874 const gdb_byte *line_ptr,
19875 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19876
19877 void handle_set_discriminator (unsigned int discriminator)
19878 {
19879 m_discriminator = discriminator;
19880 m_line_has_non_zero_discriminator |= discriminator != 0;
19881 }
19882
19883 /* Handle DW_LNE_set_address. */
19884 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19885 {
19886 m_op_index = 0;
19887 address += baseaddr;
19888 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19889 }
19890
19891 /* Handle DW_LNS_advance_pc. */
19892 void handle_advance_pc (CORE_ADDR adjust);
19893
19894 /* Handle a special opcode. */
19895 void handle_special_opcode (unsigned char op_code);
19896
19897 /* Handle DW_LNS_advance_line. */
19898 void handle_advance_line (int line_delta)
19899 {
19900 advance_line (line_delta);
19901 }
19902
19903 /* Handle DW_LNS_set_file. */
19904 void handle_set_file (file_name_index file);
19905
19906 /* Handle DW_LNS_negate_stmt. */
19907 void handle_negate_stmt ()
19908 {
19909 m_is_stmt = !m_is_stmt;
19910 }
19911
19912 /* Handle DW_LNS_const_add_pc. */
19913 void handle_const_add_pc ();
19914
19915 /* Handle DW_LNS_fixed_advance_pc. */
19916 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19917 {
19918 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19919 m_op_index = 0;
19920 }
19921
19922 /* Handle DW_LNS_copy. */
19923 void handle_copy ()
19924 {
19925 record_line (false);
19926 m_discriminator = 0;
19927 }
19928
19929 /* Handle DW_LNE_end_sequence. */
19930 void handle_end_sequence ()
19931 {
19932 m_currently_recording_lines = true;
19933 }
19934
19935 private:
19936 /* Advance the line by LINE_DELTA. */
19937 void advance_line (int line_delta)
19938 {
19939 m_line += line_delta;
19940
19941 if (line_delta != 0)
19942 m_line_has_non_zero_discriminator = m_discriminator != 0;
19943 }
19944
19945 struct dwarf2_cu *m_cu;
19946
19947 gdbarch *m_gdbarch;
19948
19949 /* True if we're recording lines.
19950 Otherwise we're building partial symtabs and are just interested in
19951 finding include files mentioned by the line number program. */
19952 bool m_record_lines_p;
19953
19954 /* The line number header. */
19955 line_header *m_line_header;
19956
19957 /* These are part of the standard DWARF line number state machine,
19958 and initialized according to the DWARF spec. */
19959
19960 unsigned char m_op_index = 0;
19961 /* The line table index of the current file. */
19962 file_name_index m_file = 1;
19963 unsigned int m_line = 1;
19964
19965 /* These are initialized in the constructor. */
19966
19967 CORE_ADDR m_address;
19968 bool m_is_stmt;
19969 unsigned int m_discriminator;
19970
19971 /* Additional bits of state we need to track. */
19972
19973 /* The last file that we called dwarf2_start_subfile for.
19974 This is only used for TLLs. */
19975 unsigned int m_last_file = 0;
19976 /* The last file a line number was recorded for. */
19977 struct subfile *m_last_subfile = NULL;
19978
19979 /* When true, record the lines we decode. */
19980 bool m_currently_recording_lines = false;
19981
19982 /* The last line number that was recorded, used to coalesce
19983 consecutive entries for the same line. This can happen, for
19984 example, when discriminators are present. PR 17276. */
19985 unsigned int m_last_line = 0;
19986 bool m_line_has_non_zero_discriminator = false;
19987 };
19988
19989 void
19990 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19991 {
19992 CORE_ADDR addr_adj = (((m_op_index + adjust)
19993 / m_line_header->maximum_ops_per_instruction)
19994 * m_line_header->minimum_instruction_length);
19995 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19996 m_op_index = ((m_op_index + adjust)
19997 % m_line_header->maximum_ops_per_instruction);
19998 }
19999
20000 void
20001 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20002 {
20003 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20004 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20005 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20006 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20007 / m_line_header->maximum_ops_per_instruction)
20008 * m_line_header->minimum_instruction_length);
20009 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20010 m_op_index = ((m_op_index + adj_opcode_d)
20011 % m_line_header->maximum_ops_per_instruction);
20012
20013 int line_delta = m_line_header->line_base + adj_opcode_r;
20014 advance_line (line_delta);
20015 record_line (false);
20016 m_discriminator = 0;
20017 }
20018
20019 void
20020 lnp_state_machine::handle_set_file (file_name_index file)
20021 {
20022 m_file = file;
20023
20024 const file_entry *fe = current_file ();
20025 if (fe == NULL)
20026 dwarf2_debug_line_missing_file_complaint ();
20027 else if (m_record_lines_p)
20028 {
20029 const char *dir = fe->include_dir (m_line_header);
20030
20031 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20032 m_line_has_non_zero_discriminator = m_discriminator != 0;
20033 dwarf2_start_subfile (m_cu, fe->name, dir);
20034 }
20035 }
20036
20037 void
20038 lnp_state_machine::handle_const_add_pc ()
20039 {
20040 CORE_ADDR adjust
20041 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20042
20043 CORE_ADDR addr_adj
20044 = (((m_op_index + adjust)
20045 / m_line_header->maximum_ops_per_instruction)
20046 * m_line_header->minimum_instruction_length);
20047
20048 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20049 m_op_index = ((m_op_index + adjust)
20050 % m_line_header->maximum_ops_per_instruction);
20051 }
20052
20053 /* Return non-zero if we should add LINE to the line number table.
20054 LINE is the line to add, LAST_LINE is the last line that was added,
20055 LAST_SUBFILE is the subfile for LAST_LINE.
20056 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20057 had a non-zero discriminator.
20058
20059 We have to be careful in the presence of discriminators.
20060 E.g., for this line:
20061
20062 for (i = 0; i < 100000; i++);
20063
20064 clang can emit four line number entries for that one line,
20065 each with a different discriminator.
20066 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20067
20068 However, we want gdb to coalesce all four entries into one.
20069 Otherwise the user could stepi into the middle of the line and
20070 gdb would get confused about whether the pc really was in the
20071 middle of the line.
20072
20073 Things are further complicated by the fact that two consecutive
20074 line number entries for the same line is a heuristic used by gcc
20075 to denote the end of the prologue. So we can't just discard duplicate
20076 entries, we have to be selective about it. The heuristic we use is
20077 that we only collapse consecutive entries for the same line if at least
20078 one of those entries has a non-zero discriminator. PR 17276.
20079
20080 Note: Addresses in the line number state machine can never go backwards
20081 within one sequence, thus this coalescing is ok. */
20082
20083 static int
20084 dwarf_record_line_p (struct dwarf2_cu *cu,
20085 unsigned int line, unsigned int last_line,
20086 int line_has_non_zero_discriminator,
20087 struct subfile *last_subfile)
20088 {
20089 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20090 return 1;
20091 if (line != last_line)
20092 return 1;
20093 /* Same line for the same file that we've seen already.
20094 As a last check, for pr 17276, only record the line if the line
20095 has never had a non-zero discriminator. */
20096 if (!line_has_non_zero_discriminator)
20097 return 1;
20098 return 0;
20099 }
20100
20101 /* Use the CU's builder to record line number LINE beginning at
20102 address ADDRESS in the line table of subfile SUBFILE. */
20103
20104 static void
20105 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20106 unsigned int line, CORE_ADDR address, bool is_stmt,
20107 struct dwarf2_cu *cu)
20108 {
20109 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20110
20111 if (dwarf_line_debug)
20112 {
20113 fprintf_unfiltered (gdb_stdlog,
20114 "Recording line %u, file %s, address %s\n",
20115 line, lbasename (subfile->name),
20116 paddress (gdbarch, address));
20117 }
20118
20119 if (cu != nullptr)
20120 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20121 }
20122
20123 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20124 Mark the end of a set of line number records.
20125 The arguments are the same as for dwarf_record_line_1.
20126 If SUBFILE is NULL the request is ignored. */
20127
20128 static void
20129 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20130 CORE_ADDR address, struct dwarf2_cu *cu)
20131 {
20132 if (subfile == NULL)
20133 return;
20134
20135 if (dwarf_line_debug)
20136 {
20137 fprintf_unfiltered (gdb_stdlog,
20138 "Finishing current line, file %s, address %s\n",
20139 lbasename (subfile->name),
20140 paddress (gdbarch, address));
20141 }
20142
20143 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20144 }
20145
20146 void
20147 lnp_state_machine::record_line (bool end_sequence)
20148 {
20149 if (dwarf_line_debug)
20150 {
20151 fprintf_unfiltered (gdb_stdlog,
20152 "Processing actual line %u: file %u,"
20153 " address %s, is_stmt %u, discrim %u%s\n",
20154 m_line, m_file,
20155 paddress (m_gdbarch, m_address),
20156 m_is_stmt, m_discriminator,
20157 (end_sequence ? "\t(end sequence)" : ""));
20158 }
20159
20160 file_entry *fe = current_file ();
20161
20162 if (fe == NULL)
20163 dwarf2_debug_line_missing_file_complaint ();
20164 /* For now we ignore lines not starting on an instruction boundary.
20165 But not when processing end_sequence for compatibility with the
20166 previous version of the code. */
20167 else if (m_op_index == 0 || end_sequence)
20168 {
20169 fe->included_p = 1;
20170 if (m_record_lines_p)
20171 {
20172 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20173 || end_sequence)
20174 {
20175 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20176 m_currently_recording_lines ? m_cu : nullptr);
20177 }
20178
20179 if (!end_sequence)
20180 {
20181 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20182
20183 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20184 m_line_has_non_zero_discriminator,
20185 m_last_subfile))
20186 {
20187 buildsym_compunit *builder = m_cu->get_builder ();
20188 dwarf_record_line_1 (m_gdbarch,
20189 builder->get_current_subfile (),
20190 m_line, m_address, is_stmt,
20191 m_currently_recording_lines ? m_cu : nullptr);
20192 }
20193 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20194 m_last_line = m_line;
20195 }
20196 }
20197 }
20198 }
20199
20200 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20201 line_header *lh, bool record_lines_p)
20202 {
20203 m_cu = cu;
20204 m_gdbarch = arch;
20205 m_record_lines_p = record_lines_p;
20206 m_line_header = lh;
20207
20208 m_currently_recording_lines = true;
20209
20210 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20211 was a line entry for it so that the backend has a chance to adjust it
20212 and also record it in case it needs it. This is currently used by MIPS
20213 code, cf. `mips_adjust_dwarf2_line'. */
20214 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20215 m_is_stmt = lh->default_is_stmt;
20216 m_discriminator = 0;
20217 }
20218
20219 void
20220 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20221 const gdb_byte *line_ptr,
20222 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20223 {
20224 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20225 the pc range of the CU. However, we restrict the test to only ADDRESS
20226 values of zero to preserve GDB's previous behaviour which is to handle
20227 the specific case of a function being GC'd by the linker. */
20228
20229 if (address == 0 && address < unrelocated_lowpc)
20230 {
20231 /* This line table is for a function which has been
20232 GCd by the linker. Ignore it. PR gdb/12528 */
20233
20234 struct objfile *objfile = cu->per_objfile->objfile;
20235 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20236
20237 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20238 line_offset, objfile_name (objfile));
20239 m_currently_recording_lines = false;
20240 /* Note: m_currently_recording_lines is left as false until we see
20241 DW_LNE_end_sequence. */
20242 }
20243 }
20244
20245 /* Subroutine of dwarf_decode_lines to simplify it.
20246 Process the line number information in LH.
20247 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20248 program in order to set included_p for every referenced header. */
20249
20250 static void
20251 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20252 const int decode_for_pst_p, CORE_ADDR lowpc)
20253 {
20254 const gdb_byte *line_ptr, *extended_end;
20255 const gdb_byte *line_end;
20256 unsigned int bytes_read, extended_len;
20257 unsigned char op_code, extended_op;
20258 CORE_ADDR baseaddr;
20259 struct objfile *objfile = cu->per_objfile->objfile;
20260 bfd *abfd = objfile->obfd;
20261 struct gdbarch *gdbarch = objfile->arch ();
20262 /* True if we're recording line info (as opposed to building partial
20263 symtabs and just interested in finding include files mentioned by
20264 the line number program). */
20265 bool record_lines_p = !decode_for_pst_p;
20266
20267 baseaddr = objfile->text_section_offset ();
20268
20269 line_ptr = lh->statement_program_start;
20270 line_end = lh->statement_program_end;
20271
20272 /* Read the statement sequences until there's nothing left. */
20273 while (line_ptr < line_end)
20274 {
20275 /* The DWARF line number program state machine. Reset the state
20276 machine at the start of each sequence. */
20277 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20278 bool end_sequence = false;
20279
20280 if (record_lines_p)
20281 {
20282 /* Start a subfile for the current file of the state
20283 machine. */
20284 const file_entry *fe = state_machine.current_file ();
20285
20286 if (fe != NULL)
20287 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20288 }
20289
20290 /* Decode the table. */
20291 while (line_ptr < line_end && !end_sequence)
20292 {
20293 op_code = read_1_byte (abfd, line_ptr);
20294 line_ptr += 1;
20295
20296 if (op_code >= lh->opcode_base)
20297 {
20298 /* Special opcode. */
20299 state_machine.handle_special_opcode (op_code);
20300 }
20301 else switch (op_code)
20302 {
20303 case DW_LNS_extended_op:
20304 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20305 &bytes_read);
20306 line_ptr += bytes_read;
20307 extended_end = line_ptr + extended_len;
20308 extended_op = read_1_byte (abfd, line_ptr);
20309 line_ptr += 1;
20310 switch (extended_op)
20311 {
20312 case DW_LNE_end_sequence:
20313 state_machine.handle_end_sequence ();
20314 end_sequence = true;
20315 break;
20316 case DW_LNE_set_address:
20317 {
20318 CORE_ADDR address
20319 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20320 line_ptr += bytes_read;
20321
20322 state_machine.check_line_address (cu, line_ptr,
20323 lowpc - baseaddr, address);
20324 state_machine.handle_set_address (baseaddr, address);
20325 }
20326 break;
20327 case DW_LNE_define_file:
20328 {
20329 const char *cur_file;
20330 unsigned int mod_time, length;
20331 dir_index dindex;
20332
20333 cur_file = read_direct_string (abfd, line_ptr,
20334 &bytes_read);
20335 line_ptr += bytes_read;
20336 dindex = (dir_index)
20337 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20338 line_ptr += bytes_read;
20339 mod_time =
20340 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20341 line_ptr += bytes_read;
20342 length =
20343 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20344 line_ptr += bytes_read;
20345 lh->add_file_name (cur_file, dindex, mod_time, length);
20346 }
20347 break;
20348 case DW_LNE_set_discriminator:
20349 {
20350 /* The discriminator is not interesting to the
20351 debugger; just ignore it. We still need to
20352 check its value though:
20353 if there are consecutive entries for the same
20354 (non-prologue) line we want to coalesce them.
20355 PR 17276. */
20356 unsigned int discr
20357 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20358 line_ptr += bytes_read;
20359
20360 state_machine.handle_set_discriminator (discr);
20361 }
20362 break;
20363 default:
20364 complaint (_("mangled .debug_line section"));
20365 return;
20366 }
20367 /* Make sure that we parsed the extended op correctly. If e.g.
20368 we expected a different address size than the producer used,
20369 we may have read the wrong number of bytes. */
20370 if (line_ptr != extended_end)
20371 {
20372 complaint (_("mangled .debug_line section"));
20373 return;
20374 }
20375 break;
20376 case DW_LNS_copy:
20377 state_machine.handle_copy ();
20378 break;
20379 case DW_LNS_advance_pc:
20380 {
20381 CORE_ADDR adjust
20382 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20383 line_ptr += bytes_read;
20384
20385 state_machine.handle_advance_pc (adjust);
20386 }
20387 break;
20388 case DW_LNS_advance_line:
20389 {
20390 int line_delta
20391 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20392 line_ptr += bytes_read;
20393
20394 state_machine.handle_advance_line (line_delta);
20395 }
20396 break;
20397 case DW_LNS_set_file:
20398 {
20399 file_name_index file
20400 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20401 &bytes_read);
20402 line_ptr += bytes_read;
20403
20404 state_machine.handle_set_file (file);
20405 }
20406 break;
20407 case DW_LNS_set_column:
20408 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20409 line_ptr += bytes_read;
20410 break;
20411 case DW_LNS_negate_stmt:
20412 state_machine.handle_negate_stmt ();
20413 break;
20414 case DW_LNS_set_basic_block:
20415 break;
20416 /* Add to the address register of the state machine the
20417 address increment value corresponding to special opcode
20418 255. I.e., this value is scaled by the minimum
20419 instruction length since special opcode 255 would have
20420 scaled the increment. */
20421 case DW_LNS_const_add_pc:
20422 state_machine.handle_const_add_pc ();
20423 break;
20424 case DW_LNS_fixed_advance_pc:
20425 {
20426 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20427 line_ptr += 2;
20428
20429 state_machine.handle_fixed_advance_pc (addr_adj);
20430 }
20431 break;
20432 default:
20433 {
20434 /* Unknown standard opcode, ignore it. */
20435 int i;
20436
20437 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20438 {
20439 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20440 line_ptr += bytes_read;
20441 }
20442 }
20443 }
20444 }
20445
20446 if (!end_sequence)
20447 dwarf2_debug_line_missing_end_sequence_complaint ();
20448
20449 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20450 in which case we still finish recording the last line). */
20451 state_machine.record_line (true);
20452 }
20453 }
20454
20455 /* Decode the Line Number Program (LNP) for the given line_header
20456 structure and CU. The actual information extracted and the type
20457 of structures created from the LNP depends on the value of PST.
20458
20459 1. If PST is NULL, then this procedure uses the data from the program
20460 to create all necessary symbol tables, and their linetables.
20461
20462 2. If PST is not NULL, this procedure reads the program to determine
20463 the list of files included by the unit represented by PST, and
20464 builds all the associated partial symbol tables.
20465
20466 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20467 It is used for relative paths in the line table.
20468 NOTE: When processing partial symtabs (pst != NULL),
20469 comp_dir == pst->dirname.
20470
20471 NOTE: It is important that psymtabs have the same file name (via strcmp)
20472 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20473 symtab we don't use it in the name of the psymtabs we create.
20474 E.g. expand_line_sal requires this when finding psymtabs to expand.
20475 A good testcase for this is mb-inline.exp.
20476
20477 LOWPC is the lowest address in CU (or 0 if not known).
20478
20479 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20480 for its PC<->lines mapping information. Otherwise only the filename
20481 table is read in. */
20482
20483 static void
20484 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20485 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20486 CORE_ADDR lowpc, int decode_mapping)
20487 {
20488 struct objfile *objfile = cu->per_objfile->objfile;
20489 const int decode_for_pst_p = (pst != NULL);
20490
20491 if (decode_mapping)
20492 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20493
20494 if (decode_for_pst_p)
20495 {
20496 /* Now that we're done scanning the Line Header Program, we can
20497 create the psymtab of each included file. */
20498 for (auto &file_entry : lh->file_names ())
20499 if (file_entry.included_p == 1)
20500 {
20501 gdb::unique_xmalloc_ptr<char> name_holder;
20502 const char *include_name =
20503 psymtab_include_file_name (lh, file_entry, pst,
20504 comp_dir, &name_holder);
20505 if (include_name != NULL)
20506 dwarf2_create_include_psymtab (include_name, pst, objfile);
20507 }
20508 }
20509 else
20510 {
20511 /* Make sure a symtab is created for every file, even files
20512 which contain only variables (i.e. no code with associated
20513 line numbers). */
20514 buildsym_compunit *builder = cu->get_builder ();
20515 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20516
20517 for (auto &fe : lh->file_names ())
20518 {
20519 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20520 if (builder->get_current_subfile ()->symtab == NULL)
20521 {
20522 builder->get_current_subfile ()->symtab
20523 = allocate_symtab (cust,
20524 builder->get_current_subfile ()->name);
20525 }
20526 fe.symtab = builder->get_current_subfile ()->symtab;
20527 }
20528 }
20529 }
20530
20531 /* Start a subfile for DWARF. FILENAME is the name of the file and
20532 DIRNAME the name of the source directory which contains FILENAME
20533 or NULL if not known.
20534 This routine tries to keep line numbers from identical absolute and
20535 relative file names in a common subfile.
20536
20537 Using the `list' example from the GDB testsuite, which resides in
20538 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20539 of /srcdir/list0.c yields the following debugging information for list0.c:
20540
20541 DW_AT_name: /srcdir/list0.c
20542 DW_AT_comp_dir: /compdir
20543 files.files[0].name: list0.h
20544 files.files[0].dir: /srcdir
20545 files.files[1].name: list0.c
20546 files.files[1].dir: /srcdir
20547
20548 The line number information for list0.c has to end up in a single
20549 subfile, so that `break /srcdir/list0.c:1' works as expected.
20550 start_subfile will ensure that this happens provided that we pass the
20551 concatenation of files.files[1].dir and files.files[1].name as the
20552 subfile's name. */
20553
20554 static void
20555 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20556 const char *dirname)
20557 {
20558 gdb::unique_xmalloc_ptr<char> copy;
20559
20560 /* In order not to lose the line information directory,
20561 we concatenate it to the filename when it makes sense.
20562 Note that the Dwarf3 standard says (speaking of filenames in line
20563 information): ``The directory index is ignored for file names
20564 that represent full path names''. Thus ignoring dirname in the
20565 `else' branch below isn't an issue. */
20566
20567 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20568 {
20569 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20570 filename = copy.get ();
20571 }
20572
20573 cu->get_builder ()->start_subfile (filename);
20574 }
20575
20576 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20577 buildsym_compunit constructor. */
20578
20579 struct compunit_symtab *
20580 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20581 CORE_ADDR low_pc)
20582 {
20583 gdb_assert (m_builder == nullptr);
20584
20585 m_builder.reset (new struct buildsym_compunit
20586 (per_cu->dwarf2_per_objfile->objfile,
20587 name, comp_dir, language, low_pc));
20588
20589 list_in_scope = get_builder ()->get_file_symbols ();
20590
20591 get_builder ()->record_debugformat ("DWARF 2");
20592 get_builder ()->record_producer (producer);
20593
20594 processing_has_namespace_info = false;
20595
20596 return get_builder ()->get_compunit_symtab ();
20597 }
20598
20599 static void
20600 var_decode_location (struct attribute *attr, struct symbol *sym,
20601 struct dwarf2_cu *cu)
20602 {
20603 struct objfile *objfile = cu->per_objfile->objfile;
20604 struct comp_unit_head *cu_header = &cu->header;
20605
20606 /* NOTE drow/2003-01-30: There used to be a comment and some special
20607 code here to turn a symbol with DW_AT_external and a
20608 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20609 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20610 with some versions of binutils) where shared libraries could have
20611 relocations against symbols in their debug information - the
20612 minimal symbol would have the right address, but the debug info
20613 would not. It's no longer necessary, because we will explicitly
20614 apply relocations when we read in the debug information now. */
20615
20616 /* A DW_AT_location attribute with no contents indicates that a
20617 variable has been optimized away. */
20618 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20619 {
20620 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20621 return;
20622 }
20623
20624 /* Handle one degenerate form of location expression specially, to
20625 preserve GDB's previous behavior when section offsets are
20626 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20627 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20628
20629 if (attr->form_is_block ()
20630 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20631 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20632 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20633 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20634 && (DW_BLOCK (attr)->size
20635 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20636 {
20637 unsigned int dummy;
20638
20639 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20640 SET_SYMBOL_VALUE_ADDRESS
20641 (sym, cu->header.read_address (objfile->obfd,
20642 DW_BLOCK (attr)->data + 1,
20643 &dummy));
20644 else
20645 SET_SYMBOL_VALUE_ADDRESS
20646 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20647 &dummy));
20648 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20649 fixup_symbol_section (sym, objfile);
20650 SET_SYMBOL_VALUE_ADDRESS
20651 (sym,
20652 SYMBOL_VALUE_ADDRESS (sym)
20653 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20654 return;
20655 }
20656
20657 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20658 expression evaluator, and use LOC_COMPUTED only when necessary
20659 (i.e. when the value of a register or memory location is
20660 referenced, or a thread-local block, etc.). Then again, it might
20661 not be worthwhile. I'm assuming that it isn't unless performance
20662 or memory numbers show me otherwise. */
20663
20664 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20665
20666 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20667 cu->has_loclist = true;
20668 }
20669
20670 /* Given a pointer to a DWARF information entry, figure out if we need
20671 to make a symbol table entry for it, and if so, create a new entry
20672 and return a pointer to it.
20673 If TYPE is NULL, determine symbol type from the die, otherwise
20674 used the passed type.
20675 If SPACE is not NULL, use it to hold the new symbol. If it is
20676 NULL, allocate a new symbol on the objfile's obstack. */
20677
20678 static struct symbol *
20679 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20680 struct symbol *space)
20681 {
20682 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20683 struct objfile *objfile = dwarf2_per_objfile->objfile;
20684 struct gdbarch *gdbarch = objfile->arch ();
20685 struct symbol *sym = NULL;
20686 const char *name;
20687 struct attribute *attr = NULL;
20688 struct attribute *attr2 = NULL;
20689 CORE_ADDR baseaddr;
20690 struct pending **list_to_add = NULL;
20691
20692 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20693
20694 baseaddr = objfile->text_section_offset ();
20695
20696 name = dwarf2_name (die, cu);
20697 if (name)
20698 {
20699 int suppress_add = 0;
20700
20701 if (space)
20702 sym = space;
20703 else
20704 sym = new (&objfile->objfile_obstack) symbol;
20705 OBJSTAT (objfile, n_syms++);
20706
20707 /* Cache this symbol's name and the name's demangled form (if any). */
20708 sym->set_language (cu->language, &objfile->objfile_obstack);
20709 /* Fortran does not have mangling standard and the mangling does differ
20710 between gfortran, iFort etc. */
20711 const char *physname
20712 = (cu->language == language_fortran
20713 ? dwarf2_full_name (name, die, cu)
20714 : dwarf2_physname (name, die, cu));
20715 const char *linkagename = dw2_linkage_name (die, cu);
20716
20717 if (linkagename == nullptr || cu->language == language_ada)
20718 sym->set_linkage_name (physname);
20719 else
20720 {
20721 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20722 sym->set_linkage_name (linkagename);
20723 }
20724
20725 /* Default assumptions.
20726 Use the passed type or decode it from the die. */
20727 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20728 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20729 if (type != NULL)
20730 SYMBOL_TYPE (sym) = type;
20731 else
20732 SYMBOL_TYPE (sym) = die_type (die, cu);
20733 attr = dwarf2_attr (die,
20734 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20735 cu);
20736 if (attr != nullptr)
20737 {
20738 SYMBOL_LINE (sym) = DW_UNSND (attr);
20739 }
20740
20741 attr = dwarf2_attr (die,
20742 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20743 cu);
20744 if (attr != nullptr)
20745 {
20746 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20747 struct file_entry *fe;
20748
20749 if (cu->line_header != NULL)
20750 fe = cu->line_header->file_name_at (file_index);
20751 else
20752 fe = NULL;
20753
20754 if (fe == NULL)
20755 complaint (_("file index out of range"));
20756 else
20757 symbol_set_symtab (sym, fe->symtab);
20758 }
20759
20760 switch (die->tag)
20761 {
20762 case DW_TAG_label:
20763 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20764 if (attr != nullptr)
20765 {
20766 CORE_ADDR addr;
20767
20768 addr = attr->value_as_address ();
20769 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20770 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20771 }
20772 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20773 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20774 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20775 add_symbol_to_list (sym, cu->list_in_scope);
20776 break;
20777 case DW_TAG_subprogram:
20778 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20779 finish_block. */
20780 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20781 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20782 if ((attr2 && (DW_UNSND (attr2) != 0))
20783 || cu->language == language_ada
20784 || cu->language == language_fortran)
20785 {
20786 /* Subprograms marked external are stored as a global symbol.
20787 Ada and Fortran subprograms, whether marked external or
20788 not, are always stored as a global symbol, because we want
20789 to be able to access them globally. For instance, we want
20790 to be able to break on a nested subprogram without having
20791 to specify the context. */
20792 list_to_add = cu->get_builder ()->get_global_symbols ();
20793 }
20794 else
20795 {
20796 list_to_add = cu->list_in_scope;
20797 }
20798 break;
20799 case DW_TAG_inlined_subroutine:
20800 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20801 finish_block. */
20802 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20803 SYMBOL_INLINED (sym) = 1;
20804 list_to_add = cu->list_in_scope;
20805 break;
20806 case DW_TAG_template_value_param:
20807 suppress_add = 1;
20808 /* Fall through. */
20809 case DW_TAG_constant:
20810 case DW_TAG_variable:
20811 case DW_TAG_member:
20812 /* Compilation with minimal debug info may result in
20813 variables with missing type entries. Change the
20814 misleading `void' type to something sensible. */
20815 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20816 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20817
20818 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20819 /* In the case of DW_TAG_member, we should only be called for
20820 static const members. */
20821 if (die->tag == DW_TAG_member)
20822 {
20823 /* dwarf2_add_field uses die_is_declaration,
20824 so we do the same. */
20825 gdb_assert (die_is_declaration (die, cu));
20826 gdb_assert (attr);
20827 }
20828 if (attr != nullptr)
20829 {
20830 dwarf2_const_value (attr, sym, cu);
20831 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20832 if (!suppress_add)
20833 {
20834 if (attr2 && (DW_UNSND (attr2) != 0))
20835 list_to_add = cu->get_builder ()->get_global_symbols ();
20836 else
20837 list_to_add = cu->list_in_scope;
20838 }
20839 break;
20840 }
20841 attr = dwarf2_attr (die, DW_AT_location, cu);
20842 if (attr != nullptr)
20843 {
20844 var_decode_location (attr, sym, cu);
20845 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20846
20847 /* Fortran explicitly imports any global symbols to the local
20848 scope by DW_TAG_common_block. */
20849 if (cu->language == language_fortran && die->parent
20850 && die->parent->tag == DW_TAG_common_block)
20851 attr2 = NULL;
20852
20853 if (SYMBOL_CLASS (sym) == LOC_STATIC
20854 && SYMBOL_VALUE_ADDRESS (sym) == 0
20855 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20856 {
20857 /* When a static variable is eliminated by the linker,
20858 the corresponding debug information is not stripped
20859 out, but the variable address is set to null;
20860 do not add such variables into symbol table. */
20861 }
20862 else if (attr2 && (DW_UNSND (attr2) != 0))
20863 {
20864 if (SYMBOL_CLASS (sym) == LOC_STATIC
20865 && (objfile->flags & OBJF_MAINLINE) == 0
20866 && dwarf2_per_objfile->per_bfd->can_copy)
20867 {
20868 /* A global static variable might be subject to
20869 copy relocation. We first check for a local
20870 minsym, though, because maybe the symbol was
20871 marked hidden, in which case this would not
20872 apply. */
20873 bound_minimal_symbol found
20874 = (lookup_minimal_symbol_linkage
20875 (sym->linkage_name (), objfile));
20876 if (found.minsym != nullptr)
20877 sym->maybe_copied = 1;
20878 }
20879
20880 /* A variable with DW_AT_external is never static,
20881 but it may be block-scoped. */
20882 list_to_add
20883 = ((cu->list_in_scope
20884 == cu->get_builder ()->get_file_symbols ())
20885 ? cu->get_builder ()->get_global_symbols ()
20886 : cu->list_in_scope);
20887 }
20888 else
20889 list_to_add = cu->list_in_scope;
20890 }
20891 else
20892 {
20893 /* We do not know the address of this symbol.
20894 If it is an external symbol and we have type information
20895 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20896 The address of the variable will then be determined from
20897 the minimal symbol table whenever the variable is
20898 referenced. */
20899 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20900
20901 /* Fortran explicitly imports any global symbols to the local
20902 scope by DW_TAG_common_block. */
20903 if (cu->language == language_fortran && die->parent
20904 && die->parent->tag == DW_TAG_common_block)
20905 {
20906 /* SYMBOL_CLASS doesn't matter here because
20907 read_common_block is going to reset it. */
20908 if (!suppress_add)
20909 list_to_add = cu->list_in_scope;
20910 }
20911 else if (attr2 && (DW_UNSND (attr2) != 0)
20912 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20913 {
20914 /* A variable with DW_AT_external is never static, but it
20915 may be block-scoped. */
20916 list_to_add
20917 = ((cu->list_in_scope
20918 == cu->get_builder ()->get_file_symbols ())
20919 ? cu->get_builder ()->get_global_symbols ()
20920 : cu->list_in_scope);
20921
20922 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20923 }
20924 else if (!die_is_declaration (die, cu))
20925 {
20926 /* Use the default LOC_OPTIMIZED_OUT class. */
20927 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20928 if (!suppress_add)
20929 list_to_add = cu->list_in_scope;
20930 }
20931 }
20932 break;
20933 case DW_TAG_formal_parameter:
20934 {
20935 /* If we are inside a function, mark this as an argument. If
20936 not, we might be looking at an argument to an inlined function
20937 when we do not have enough information to show inlined frames;
20938 pretend it's a local variable in that case so that the user can
20939 still see it. */
20940 struct context_stack *curr
20941 = cu->get_builder ()->get_current_context_stack ();
20942 if (curr != nullptr && curr->name != nullptr)
20943 SYMBOL_IS_ARGUMENT (sym) = 1;
20944 attr = dwarf2_attr (die, DW_AT_location, cu);
20945 if (attr != nullptr)
20946 {
20947 var_decode_location (attr, sym, cu);
20948 }
20949 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20950 if (attr != nullptr)
20951 {
20952 dwarf2_const_value (attr, sym, cu);
20953 }
20954
20955 list_to_add = cu->list_in_scope;
20956 }
20957 break;
20958 case DW_TAG_unspecified_parameters:
20959 /* From varargs functions; gdb doesn't seem to have any
20960 interest in this information, so just ignore it for now.
20961 (FIXME?) */
20962 break;
20963 case DW_TAG_template_type_param:
20964 suppress_add = 1;
20965 /* Fall through. */
20966 case DW_TAG_class_type:
20967 case DW_TAG_interface_type:
20968 case DW_TAG_structure_type:
20969 case DW_TAG_union_type:
20970 case DW_TAG_set_type:
20971 case DW_TAG_enumeration_type:
20972 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20973 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
20974
20975 {
20976 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
20977 really ever be static objects: otherwise, if you try
20978 to, say, break of a class's method and you're in a file
20979 which doesn't mention that class, it won't work unless
20980 the check for all static symbols in lookup_symbol_aux
20981 saves you. See the OtherFileClass tests in
20982 gdb.c++/namespace.exp. */
20983
20984 if (!suppress_add)
20985 {
20986 buildsym_compunit *builder = cu->get_builder ();
20987 list_to_add
20988 = (cu->list_in_scope == builder->get_file_symbols ()
20989 && cu->language == language_cplus
20990 ? builder->get_global_symbols ()
20991 : cu->list_in_scope);
20992
20993 /* The semantics of C++ state that "struct foo {
20994 ... }" also defines a typedef for "foo". */
20995 if (cu->language == language_cplus
20996 || cu->language == language_ada
20997 || cu->language == language_d
20998 || cu->language == language_rust)
20999 {
21000 /* The symbol's name is already allocated along
21001 with this objfile, so we don't need to
21002 duplicate it for the type. */
21003 if (SYMBOL_TYPE (sym)->name () == 0)
21004 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21005 }
21006 }
21007 }
21008 break;
21009 case DW_TAG_typedef:
21010 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21011 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21012 list_to_add = cu->list_in_scope;
21013 break;
21014 case DW_TAG_base_type:
21015 case DW_TAG_subrange_type:
21016 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21017 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21018 list_to_add = cu->list_in_scope;
21019 break;
21020 case DW_TAG_enumerator:
21021 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21022 if (attr != nullptr)
21023 {
21024 dwarf2_const_value (attr, sym, cu);
21025 }
21026 {
21027 /* NOTE: carlton/2003-11-10: See comment above in the
21028 DW_TAG_class_type, etc. block. */
21029
21030 list_to_add
21031 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21032 && cu->language == language_cplus
21033 ? cu->get_builder ()->get_global_symbols ()
21034 : cu->list_in_scope);
21035 }
21036 break;
21037 case DW_TAG_imported_declaration:
21038 case DW_TAG_namespace:
21039 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21040 list_to_add = cu->get_builder ()->get_global_symbols ();
21041 break;
21042 case DW_TAG_module:
21043 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21044 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21045 list_to_add = cu->get_builder ()->get_global_symbols ();
21046 break;
21047 case DW_TAG_common_block:
21048 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21049 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21050 add_symbol_to_list (sym, cu->list_in_scope);
21051 break;
21052 default:
21053 /* Not a tag we recognize. Hopefully we aren't processing
21054 trash data, but since we must specifically ignore things
21055 we don't recognize, there is nothing else we should do at
21056 this point. */
21057 complaint (_("unsupported tag: '%s'"),
21058 dwarf_tag_name (die->tag));
21059 break;
21060 }
21061
21062 if (suppress_add)
21063 {
21064 sym->hash_next = objfile->template_symbols;
21065 objfile->template_symbols = sym;
21066 list_to_add = NULL;
21067 }
21068
21069 if (list_to_add != NULL)
21070 add_symbol_to_list (sym, list_to_add);
21071
21072 /* For the benefit of old versions of GCC, check for anonymous
21073 namespaces based on the demangled name. */
21074 if (!cu->processing_has_namespace_info
21075 && cu->language == language_cplus)
21076 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21077 }
21078 return (sym);
21079 }
21080
21081 /* Given an attr with a DW_FORM_dataN value in host byte order,
21082 zero-extend it as appropriate for the symbol's type. The DWARF
21083 standard (v4) is not entirely clear about the meaning of using
21084 DW_FORM_dataN for a constant with a signed type, where the type is
21085 wider than the data. The conclusion of a discussion on the DWARF
21086 list was that this is unspecified. We choose to always zero-extend
21087 because that is the interpretation long in use by GCC. */
21088
21089 static gdb_byte *
21090 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21091 struct dwarf2_cu *cu, LONGEST *value, int bits)
21092 {
21093 struct objfile *objfile = cu->per_objfile->objfile;
21094 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21095 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21096 LONGEST l = DW_UNSND (attr);
21097
21098 if (bits < sizeof (*value) * 8)
21099 {
21100 l &= ((LONGEST) 1 << bits) - 1;
21101 *value = l;
21102 }
21103 else if (bits == sizeof (*value) * 8)
21104 *value = l;
21105 else
21106 {
21107 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21108 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21109 return bytes;
21110 }
21111
21112 return NULL;
21113 }
21114
21115 /* Read a constant value from an attribute. Either set *VALUE, or if
21116 the value does not fit in *VALUE, set *BYTES - either already
21117 allocated on the objfile obstack, or newly allocated on OBSTACK,
21118 or, set *BATON, if we translated the constant to a location
21119 expression. */
21120
21121 static void
21122 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21123 const char *name, struct obstack *obstack,
21124 struct dwarf2_cu *cu,
21125 LONGEST *value, const gdb_byte **bytes,
21126 struct dwarf2_locexpr_baton **baton)
21127 {
21128 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21129 struct objfile *objfile = per_objfile->objfile;
21130 struct comp_unit_head *cu_header = &cu->header;
21131 struct dwarf_block *blk;
21132 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21133 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21134
21135 *value = 0;
21136 *bytes = NULL;
21137 *baton = NULL;
21138
21139 switch (attr->form)
21140 {
21141 case DW_FORM_addr:
21142 case DW_FORM_addrx:
21143 case DW_FORM_GNU_addr_index:
21144 {
21145 gdb_byte *data;
21146
21147 if (TYPE_LENGTH (type) != cu_header->addr_size)
21148 dwarf2_const_value_length_mismatch_complaint (name,
21149 cu_header->addr_size,
21150 TYPE_LENGTH (type));
21151 /* Symbols of this form are reasonably rare, so we just
21152 piggyback on the existing location code rather than writing
21153 a new implementation of symbol_computed_ops. */
21154 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21155 (*baton)->per_objfile = per_objfile;
21156 (*baton)->per_cu = cu->per_cu;
21157 gdb_assert ((*baton)->per_cu);
21158
21159 (*baton)->size = 2 + cu_header->addr_size;
21160 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21161 (*baton)->data = data;
21162
21163 data[0] = DW_OP_addr;
21164 store_unsigned_integer (&data[1], cu_header->addr_size,
21165 byte_order, DW_ADDR (attr));
21166 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21167 }
21168 break;
21169 case DW_FORM_string:
21170 case DW_FORM_strp:
21171 case DW_FORM_strx:
21172 case DW_FORM_GNU_str_index:
21173 case DW_FORM_GNU_strp_alt:
21174 /* DW_STRING is already allocated on the objfile obstack, point
21175 directly to it. */
21176 *bytes = (const gdb_byte *) DW_STRING (attr);
21177 break;
21178 case DW_FORM_block1:
21179 case DW_FORM_block2:
21180 case DW_FORM_block4:
21181 case DW_FORM_block:
21182 case DW_FORM_exprloc:
21183 case DW_FORM_data16:
21184 blk = DW_BLOCK (attr);
21185 if (TYPE_LENGTH (type) != blk->size)
21186 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21187 TYPE_LENGTH (type));
21188 *bytes = blk->data;
21189 break;
21190
21191 /* The DW_AT_const_value attributes are supposed to carry the
21192 symbol's value "represented as it would be on the target
21193 architecture." By the time we get here, it's already been
21194 converted to host endianness, so we just need to sign- or
21195 zero-extend it as appropriate. */
21196 case DW_FORM_data1:
21197 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21198 break;
21199 case DW_FORM_data2:
21200 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21201 break;
21202 case DW_FORM_data4:
21203 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21204 break;
21205 case DW_FORM_data8:
21206 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21207 break;
21208
21209 case DW_FORM_sdata:
21210 case DW_FORM_implicit_const:
21211 *value = DW_SND (attr);
21212 break;
21213
21214 case DW_FORM_udata:
21215 *value = DW_UNSND (attr);
21216 break;
21217
21218 default:
21219 complaint (_("unsupported const value attribute form: '%s'"),
21220 dwarf_form_name (attr->form));
21221 *value = 0;
21222 break;
21223 }
21224 }
21225
21226
21227 /* Copy constant value from an attribute to a symbol. */
21228
21229 static void
21230 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21231 struct dwarf2_cu *cu)
21232 {
21233 struct objfile *objfile = cu->per_objfile->objfile;
21234 LONGEST value;
21235 const gdb_byte *bytes;
21236 struct dwarf2_locexpr_baton *baton;
21237
21238 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21239 sym->print_name (),
21240 &objfile->objfile_obstack, cu,
21241 &value, &bytes, &baton);
21242
21243 if (baton != NULL)
21244 {
21245 SYMBOL_LOCATION_BATON (sym) = baton;
21246 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21247 }
21248 else if (bytes != NULL)
21249 {
21250 SYMBOL_VALUE_BYTES (sym) = bytes;
21251 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21252 }
21253 else
21254 {
21255 SYMBOL_VALUE (sym) = value;
21256 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21257 }
21258 }
21259
21260 /* Return the type of the die in question using its DW_AT_type attribute. */
21261
21262 static struct type *
21263 die_type (struct die_info *die, struct dwarf2_cu *cu)
21264 {
21265 struct attribute *type_attr;
21266
21267 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21268 if (!type_attr)
21269 {
21270 struct objfile *objfile = cu->per_objfile->objfile;
21271 /* A missing DW_AT_type represents a void type. */
21272 return objfile_type (objfile)->builtin_void;
21273 }
21274
21275 return lookup_die_type (die, type_attr, cu);
21276 }
21277
21278 /* True iff CU's producer generates GNAT Ada auxiliary information
21279 that allows to find parallel types through that information instead
21280 of having to do expensive parallel lookups by type name. */
21281
21282 static int
21283 need_gnat_info (struct dwarf2_cu *cu)
21284 {
21285 /* Assume that the Ada compiler was GNAT, which always produces
21286 the auxiliary information. */
21287 return (cu->language == language_ada);
21288 }
21289
21290 /* Return the auxiliary type of the die in question using its
21291 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21292 attribute is not present. */
21293
21294 static struct type *
21295 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21296 {
21297 struct attribute *type_attr;
21298
21299 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21300 if (!type_attr)
21301 return NULL;
21302
21303 return lookup_die_type (die, type_attr, cu);
21304 }
21305
21306 /* If DIE has a descriptive_type attribute, then set the TYPE's
21307 descriptive type accordingly. */
21308
21309 static void
21310 set_descriptive_type (struct type *type, struct die_info *die,
21311 struct dwarf2_cu *cu)
21312 {
21313 struct type *descriptive_type = die_descriptive_type (die, cu);
21314
21315 if (descriptive_type)
21316 {
21317 ALLOCATE_GNAT_AUX_TYPE (type);
21318 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21319 }
21320 }
21321
21322 /* Return the containing type of the die in question using its
21323 DW_AT_containing_type attribute. */
21324
21325 static struct type *
21326 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21327 {
21328 struct attribute *type_attr;
21329 struct objfile *objfile = cu->per_objfile->objfile;
21330
21331 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21332 if (!type_attr)
21333 error (_("Dwarf Error: Problem turning containing type into gdb type "
21334 "[in module %s]"), objfile_name (objfile));
21335
21336 return lookup_die_type (die, type_attr, cu);
21337 }
21338
21339 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21340
21341 static struct type *
21342 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21343 {
21344 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21345 struct objfile *objfile = dwarf2_per_objfile->objfile;
21346 char *saved;
21347
21348 std::string message
21349 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21350 objfile_name (objfile),
21351 sect_offset_str (cu->header.sect_off),
21352 sect_offset_str (die->sect_off));
21353 saved = obstack_strdup (&objfile->objfile_obstack, message);
21354
21355 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21356 }
21357
21358 /* Look up the type of DIE in CU using its type attribute ATTR.
21359 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21360 DW_AT_containing_type.
21361 If there is no type substitute an error marker. */
21362
21363 static struct type *
21364 lookup_die_type (struct die_info *die, const struct attribute *attr,
21365 struct dwarf2_cu *cu)
21366 {
21367 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21368 struct objfile *objfile = dwarf2_per_objfile->objfile;
21369 struct type *this_type;
21370
21371 gdb_assert (attr->name == DW_AT_type
21372 || attr->name == DW_AT_GNAT_descriptive_type
21373 || attr->name == DW_AT_containing_type);
21374
21375 /* First see if we have it cached. */
21376
21377 if (attr->form == DW_FORM_GNU_ref_alt)
21378 {
21379 struct dwarf2_per_cu_data *per_cu;
21380 sect_offset sect_off = attr->get_ref_die_offset ();
21381
21382 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21383 dwarf2_per_objfile);
21384 this_type = get_die_type_at_offset (sect_off, per_cu);
21385 }
21386 else if (attr->form_is_ref ())
21387 {
21388 sect_offset sect_off = attr->get_ref_die_offset ();
21389
21390 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
21391 }
21392 else if (attr->form == DW_FORM_ref_sig8)
21393 {
21394 ULONGEST signature = DW_SIGNATURE (attr);
21395
21396 return get_signatured_type (die, signature, cu);
21397 }
21398 else
21399 {
21400 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21401 " at %s [in module %s]"),
21402 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21403 objfile_name (objfile));
21404 return build_error_marker_type (cu, die);
21405 }
21406
21407 /* If not cached we need to read it in. */
21408
21409 if (this_type == NULL)
21410 {
21411 struct die_info *type_die = NULL;
21412 struct dwarf2_cu *type_cu = cu;
21413
21414 if (attr->form_is_ref ())
21415 type_die = follow_die_ref (die, attr, &type_cu);
21416 if (type_die == NULL)
21417 return build_error_marker_type (cu, die);
21418 /* If we find the type now, it's probably because the type came
21419 from an inter-CU reference and the type's CU got expanded before
21420 ours. */
21421 this_type = read_type_die (type_die, type_cu);
21422 }
21423
21424 /* If we still don't have a type use an error marker. */
21425
21426 if (this_type == NULL)
21427 return build_error_marker_type (cu, die);
21428
21429 return this_type;
21430 }
21431
21432 /* Return the type in DIE, CU.
21433 Returns NULL for invalid types.
21434
21435 This first does a lookup in die_type_hash,
21436 and only reads the die in if necessary.
21437
21438 NOTE: This can be called when reading in partial or full symbols. */
21439
21440 static struct type *
21441 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21442 {
21443 struct type *this_type;
21444
21445 this_type = get_die_type (die, cu);
21446 if (this_type)
21447 return this_type;
21448
21449 return read_type_die_1 (die, cu);
21450 }
21451
21452 /* Read the type in DIE, CU.
21453 Returns NULL for invalid types. */
21454
21455 static struct type *
21456 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21457 {
21458 struct type *this_type = NULL;
21459
21460 switch (die->tag)
21461 {
21462 case DW_TAG_class_type:
21463 case DW_TAG_interface_type:
21464 case DW_TAG_structure_type:
21465 case DW_TAG_union_type:
21466 this_type = read_structure_type (die, cu);
21467 break;
21468 case DW_TAG_enumeration_type:
21469 this_type = read_enumeration_type (die, cu);
21470 break;
21471 case DW_TAG_subprogram:
21472 case DW_TAG_subroutine_type:
21473 case DW_TAG_inlined_subroutine:
21474 this_type = read_subroutine_type (die, cu);
21475 break;
21476 case DW_TAG_array_type:
21477 this_type = read_array_type (die, cu);
21478 break;
21479 case DW_TAG_set_type:
21480 this_type = read_set_type (die, cu);
21481 break;
21482 case DW_TAG_pointer_type:
21483 this_type = read_tag_pointer_type (die, cu);
21484 break;
21485 case DW_TAG_ptr_to_member_type:
21486 this_type = read_tag_ptr_to_member_type (die, cu);
21487 break;
21488 case DW_TAG_reference_type:
21489 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21490 break;
21491 case DW_TAG_rvalue_reference_type:
21492 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21493 break;
21494 case DW_TAG_const_type:
21495 this_type = read_tag_const_type (die, cu);
21496 break;
21497 case DW_TAG_volatile_type:
21498 this_type = read_tag_volatile_type (die, cu);
21499 break;
21500 case DW_TAG_restrict_type:
21501 this_type = read_tag_restrict_type (die, cu);
21502 break;
21503 case DW_TAG_string_type:
21504 this_type = read_tag_string_type (die, cu);
21505 break;
21506 case DW_TAG_typedef:
21507 this_type = read_typedef (die, cu);
21508 break;
21509 case DW_TAG_subrange_type:
21510 this_type = read_subrange_type (die, cu);
21511 break;
21512 case DW_TAG_base_type:
21513 this_type = read_base_type (die, cu);
21514 break;
21515 case DW_TAG_unspecified_type:
21516 this_type = read_unspecified_type (die, cu);
21517 break;
21518 case DW_TAG_namespace:
21519 this_type = read_namespace_type (die, cu);
21520 break;
21521 case DW_TAG_module:
21522 this_type = read_module_type (die, cu);
21523 break;
21524 case DW_TAG_atomic_type:
21525 this_type = read_tag_atomic_type (die, cu);
21526 break;
21527 default:
21528 complaint (_("unexpected tag in read_type_die: '%s'"),
21529 dwarf_tag_name (die->tag));
21530 break;
21531 }
21532
21533 return this_type;
21534 }
21535
21536 /* See if we can figure out if the class lives in a namespace. We do
21537 this by looking for a member function; its demangled name will
21538 contain namespace info, if there is any.
21539 Return the computed name or NULL.
21540 Space for the result is allocated on the objfile's obstack.
21541 This is the full-die version of guess_partial_die_structure_name.
21542 In this case we know DIE has no useful parent. */
21543
21544 static const char *
21545 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21546 {
21547 struct die_info *spec_die;
21548 struct dwarf2_cu *spec_cu;
21549 struct die_info *child;
21550 struct objfile *objfile = cu->per_objfile->objfile;
21551
21552 spec_cu = cu;
21553 spec_die = die_specification (die, &spec_cu);
21554 if (spec_die != NULL)
21555 {
21556 die = spec_die;
21557 cu = spec_cu;
21558 }
21559
21560 for (child = die->child;
21561 child != NULL;
21562 child = child->sibling)
21563 {
21564 if (child->tag == DW_TAG_subprogram)
21565 {
21566 const char *linkage_name = dw2_linkage_name (child, cu);
21567
21568 if (linkage_name != NULL)
21569 {
21570 gdb::unique_xmalloc_ptr<char> actual_name
21571 (language_class_name_from_physname (cu->language_defn,
21572 linkage_name));
21573 const char *name = NULL;
21574
21575 if (actual_name != NULL)
21576 {
21577 const char *die_name = dwarf2_name (die, cu);
21578
21579 if (die_name != NULL
21580 && strcmp (die_name, actual_name.get ()) != 0)
21581 {
21582 /* Strip off the class name from the full name.
21583 We want the prefix. */
21584 int die_name_len = strlen (die_name);
21585 int actual_name_len = strlen (actual_name.get ());
21586 const char *ptr = actual_name.get ();
21587
21588 /* Test for '::' as a sanity check. */
21589 if (actual_name_len > die_name_len + 2
21590 && ptr[actual_name_len - die_name_len - 1] == ':')
21591 name = obstack_strndup (
21592 &objfile->per_bfd->storage_obstack,
21593 ptr, actual_name_len - die_name_len - 2);
21594 }
21595 }
21596 return name;
21597 }
21598 }
21599 }
21600
21601 return NULL;
21602 }
21603
21604 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21605 prefix part in such case. See
21606 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21607
21608 static const char *
21609 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21610 {
21611 struct attribute *attr;
21612 const char *base;
21613
21614 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21615 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21616 return NULL;
21617
21618 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21619 return NULL;
21620
21621 attr = dw2_linkage_name_attr (die, cu);
21622 if (attr == NULL || DW_STRING (attr) == NULL)
21623 return NULL;
21624
21625 /* dwarf2_name had to be already called. */
21626 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21627
21628 /* Strip the base name, keep any leading namespaces/classes. */
21629 base = strrchr (DW_STRING (attr), ':');
21630 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21631 return "";
21632
21633 struct objfile *objfile = cu->per_objfile->objfile;
21634 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21635 DW_STRING (attr),
21636 &base[-1] - DW_STRING (attr));
21637 }
21638
21639 /* Return the name of the namespace/class that DIE is defined within,
21640 or "" if we can't tell. The caller should not xfree the result.
21641
21642 For example, if we're within the method foo() in the following
21643 code:
21644
21645 namespace N {
21646 class C {
21647 void foo () {
21648 }
21649 };
21650 }
21651
21652 then determine_prefix on foo's die will return "N::C". */
21653
21654 static const char *
21655 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21656 {
21657 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21658 struct die_info *parent, *spec_die;
21659 struct dwarf2_cu *spec_cu;
21660 struct type *parent_type;
21661 const char *retval;
21662
21663 if (cu->language != language_cplus
21664 && cu->language != language_fortran && cu->language != language_d
21665 && cu->language != language_rust)
21666 return "";
21667
21668 retval = anonymous_struct_prefix (die, cu);
21669 if (retval)
21670 return retval;
21671
21672 /* We have to be careful in the presence of DW_AT_specification.
21673 For example, with GCC 3.4, given the code
21674
21675 namespace N {
21676 void foo() {
21677 // Definition of N::foo.
21678 }
21679 }
21680
21681 then we'll have a tree of DIEs like this:
21682
21683 1: DW_TAG_compile_unit
21684 2: DW_TAG_namespace // N
21685 3: DW_TAG_subprogram // declaration of N::foo
21686 4: DW_TAG_subprogram // definition of N::foo
21687 DW_AT_specification // refers to die #3
21688
21689 Thus, when processing die #4, we have to pretend that we're in
21690 the context of its DW_AT_specification, namely the contex of die
21691 #3. */
21692 spec_cu = cu;
21693 spec_die = die_specification (die, &spec_cu);
21694 if (spec_die == NULL)
21695 parent = die->parent;
21696 else
21697 {
21698 parent = spec_die->parent;
21699 cu = spec_cu;
21700 }
21701
21702 if (parent == NULL)
21703 return "";
21704 else if (parent->building_fullname)
21705 {
21706 const char *name;
21707 const char *parent_name;
21708
21709 /* It has been seen on RealView 2.2 built binaries,
21710 DW_TAG_template_type_param types actually _defined_ as
21711 children of the parent class:
21712
21713 enum E {};
21714 template class <class Enum> Class{};
21715 Class<enum E> class_e;
21716
21717 1: DW_TAG_class_type (Class)
21718 2: DW_TAG_enumeration_type (E)
21719 3: DW_TAG_enumerator (enum1:0)
21720 3: DW_TAG_enumerator (enum2:1)
21721 ...
21722 2: DW_TAG_template_type_param
21723 DW_AT_type DW_FORM_ref_udata (E)
21724
21725 Besides being broken debug info, it can put GDB into an
21726 infinite loop. Consider:
21727
21728 When we're building the full name for Class<E>, we'll start
21729 at Class, and go look over its template type parameters,
21730 finding E. We'll then try to build the full name of E, and
21731 reach here. We're now trying to build the full name of E,
21732 and look over the parent DIE for containing scope. In the
21733 broken case, if we followed the parent DIE of E, we'd again
21734 find Class, and once again go look at its template type
21735 arguments, etc., etc. Simply don't consider such parent die
21736 as source-level parent of this die (it can't be, the language
21737 doesn't allow it), and break the loop here. */
21738 name = dwarf2_name (die, cu);
21739 parent_name = dwarf2_name (parent, cu);
21740 complaint (_("template param type '%s' defined within parent '%s'"),
21741 name ? name : "<unknown>",
21742 parent_name ? parent_name : "<unknown>");
21743 return "";
21744 }
21745 else
21746 switch (parent->tag)
21747 {
21748 case DW_TAG_namespace:
21749 parent_type = read_type_die (parent, cu);
21750 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21751 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21752 Work around this problem here. */
21753 if (cu->language == language_cplus
21754 && strcmp (parent_type->name (), "::") == 0)
21755 return "";
21756 /* We give a name to even anonymous namespaces. */
21757 return parent_type->name ();
21758 case DW_TAG_class_type:
21759 case DW_TAG_interface_type:
21760 case DW_TAG_structure_type:
21761 case DW_TAG_union_type:
21762 case DW_TAG_module:
21763 parent_type = read_type_die (parent, cu);
21764 if (parent_type->name () != NULL)
21765 return parent_type->name ();
21766 else
21767 /* An anonymous structure is only allowed non-static data
21768 members; no typedefs, no member functions, et cetera.
21769 So it does not need a prefix. */
21770 return "";
21771 case DW_TAG_compile_unit:
21772 case DW_TAG_partial_unit:
21773 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21774 if (cu->language == language_cplus
21775 && !dwarf2_per_objfile->per_bfd->types.empty ()
21776 && die->child != NULL
21777 && (die->tag == DW_TAG_class_type
21778 || die->tag == DW_TAG_structure_type
21779 || die->tag == DW_TAG_union_type))
21780 {
21781 const char *name = guess_full_die_structure_name (die, cu);
21782 if (name != NULL)
21783 return name;
21784 }
21785 return "";
21786 case DW_TAG_subprogram:
21787 /* Nested subroutines in Fortran get a prefix with the name
21788 of the parent's subroutine. */
21789 if (cu->language == language_fortran)
21790 {
21791 if ((die->tag == DW_TAG_subprogram)
21792 && (dwarf2_name (parent, cu) != NULL))
21793 return dwarf2_name (parent, cu);
21794 }
21795 return determine_prefix (parent, cu);
21796 case DW_TAG_enumeration_type:
21797 parent_type = read_type_die (parent, cu);
21798 if (TYPE_DECLARED_CLASS (parent_type))
21799 {
21800 if (parent_type->name () != NULL)
21801 return parent_type->name ();
21802 return "";
21803 }
21804 /* Fall through. */
21805 default:
21806 return determine_prefix (parent, cu);
21807 }
21808 }
21809
21810 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21811 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21812 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21813 an obconcat, otherwise allocate storage for the result. The CU argument is
21814 used to determine the language and hence, the appropriate separator. */
21815
21816 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21817
21818 static char *
21819 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21820 int physname, struct dwarf2_cu *cu)
21821 {
21822 const char *lead = "";
21823 const char *sep;
21824
21825 if (suffix == NULL || suffix[0] == '\0'
21826 || prefix == NULL || prefix[0] == '\0')
21827 sep = "";
21828 else if (cu->language == language_d)
21829 {
21830 /* For D, the 'main' function could be defined in any module, but it
21831 should never be prefixed. */
21832 if (strcmp (suffix, "D main") == 0)
21833 {
21834 prefix = "";
21835 sep = "";
21836 }
21837 else
21838 sep = ".";
21839 }
21840 else if (cu->language == language_fortran && physname)
21841 {
21842 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21843 DW_AT_MIPS_linkage_name is preferred and used instead. */
21844
21845 lead = "__";
21846 sep = "_MOD_";
21847 }
21848 else
21849 sep = "::";
21850
21851 if (prefix == NULL)
21852 prefix = "";
21853 if (suffix == NULL)
21854 suffix = "";
21855
21856 if (obs == NULL)
21857 {
21858 char *retval
21859 = ((char *)
21860 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21861
21862 strcpy (retval, lead);
21863 strcat (retval, prefix);
21864 strcat (retval, sep);
21865 strcat (retval, suffix);
21866 return retval;
21867 }
21868 else
21869 {
21870 /* We have an obstack. */
21871 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21872 }
21873 }
21874
21875 /* Get name of a die, return NULL if not found. */
21876
21877 static const char *
21878 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21879 struct objfile *objfile)
21880 {
21881 if (name && cu->language == language_cplus)
21882 {
21883 gdb::unique_xmalloc_ptr<char> canon_name
21884 = cp_canonicalize_string (name);
21885
21886 if (canon_name != nullptr)
21887 name = objfile->intern (canon_name.get ());
21888 }
21889
21890 return name;
21891 }
21892
21893 /* Get name of a die, return NULL if not found.
21894 Anonymous namespaces are converted to their magic string. */
21895
21896 static const char *
21897 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21898 {
21899 struct attribute *attr;
21900 struct objfile *objfile = cu->per_objfile->objfile;
21901
21902 attr = dwarf2_attr (die, DW_AT_name, cu);
21903 if ((!attr || !DW_STRING (attr))
21904 && die->tag != DW_TAG_namespace
21905 && die->tag != DW_TAG_class_type
21906 && die->tag != DW_TAG_interface_type
21907 && die->tag != DW_TAG_structure_type
21908 && die->tag != DW_TAG_union_type)
21909 return NULL;
21910
21911 switch (die->tag)
21912 {
21913 case DW_TAG_compile_unit:
21914 case DW_TAG_partial_unit:
21915 /* Compilation units have a DW_AT_name that is a filename, not
21916 a source language identifier. */
21917 case DW_TAG_enumeration_type:
21918 case DW_TAG_enumerator:
21919 /* These tags always have simple identifiers already; no need
21920 to canonicalize them. */
21921 return DW_STRING (attr);
21922
21923 case DW_TAG_namespace:
21924 if (attr != NULL && DW_STRING (attr) != NULL)
21925 return DW_STRING (attr);
21926 return CP_ANONYMOUS_NAMESPACE_STR;
21927
21928 case DW_TAG_class_type:
21929 case DW_TAG_interface_type:
21930 case DW_TAG_structure_type:
21931 case DW_TAG_union_type:
21932 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21933 structures or unions. These were of the form "._%d" in GCC 4.1,
21934 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21935 and GCC 4.4. We work around this problem by ignoring these. */
21936 if (attr && DW_STRING (attr)
21937 && (startswith (DW_STRING (attr), "._")
21938 || startswith (DW_STRING (attr), "<anonymous")))
21939 return NULL;
21940
21941 /* GCC might emit a nameless typedef that has a linkage name. See
21942 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21943 if (!attr || DW_STRING (attr) == NULL)
21944 {
21945 attr = dw2_linkage_name_attr (die, cu);
21946 if (attr == NULL || DW_STRING (attr) == NULL)
21947 return NULL;
21948
21949 /* Avoid demangling DW_STRING (attr) the second time on a second
21950 call for the same DIE. */
21951 if (!DW_STRING_IS_CANONICAL (attr))
21952 {
21953 gdb::unique_xmalloc_ptr<char> demangled
21954 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21955 if (demangled == nullptr)
21956 return nullptr;
21957
21958 DW_STRING (attr) = objfile->intern (demangled.get ());
21959 DW_STRING_IS_CANONICAL (attr) = 1;
21960 }
21961
21962 /* Strip any leading namespaces/classes, keep only the base name.
21963 DW_AT_name for named DIEs does not contain the prefixes. */
21964 const char *base = strrchr (DW_STRING (attr), ':');
21965 if (base && base > DW_STRING (attr) && base[-1] == ':')
21966 return &base[1];
21967 else
21968 return DW_STRING (attr);
21969 }
21970 break;
21971
21972 default:
21973 break;
21974 }
21975
21976 if (!DW_STRING_IS_CANONICAL (attr))
21977 {
21978 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21979 objfile);
21980 DW_STRING_IS_CANONICAL (attr) = 1;
21981 }
21982 return DW_STRING (attr);
21983 }
21984
21985 /* Return the die that this die in an extension of, or NULL if there
21986 is none. *EXT_CU is the CU containing DIE on input, and the CU
21987 containing the return value on output. */
21988
21989 static struct die_info *
21990 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
21991 {
21992 struct attribute *attr;
21993
21994 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
21995 if (attr == NULL)
21996 return NULL;
21997
21998 return follow_die_ref (die, attr, ext_cu);
21999 }
22000
22001 static void
22002 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22003 {
22004 unsigned int i;
22005
22006 print_spaces (indent, f);
22007 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22008 dwarf_tag_name (die->tag), die->abbrev,
22009 sect_offset_str (die->sect_off));
22010
22011 if (die->parent != NULL)
22012 {
22013 print_spaces (indent, f);
22014 fprintf_unfiltered (f, " parent at offset: %s\n",
22015 sect_offset_str (die->parent->sect_off));
22016 }
22017
22018 print_spaces (indent, f);
22019 fprintf_unfiltered (f, " has children: %s\n",
22020 dwarf_bool_name (die->child != NULL));
22021
22022 print_spaces (indent, f);
22023 fprintf_unfiltered (f, " attributes:\n");
22024
22025 for (i = 0; i < die->num_attrs; ++i)
22026 {
22027 print_spaces (indent, f);
22028 fprintf_unfiltered (f, " %s (%s) ",
22029 dwarf_attr_name (die->attrs[i].name),
22030 dwarf_form_name (die->attrs[i].form));
22031
22032 switch (die->attrs[i].form)
22033 {
22034 case DW_FORM_addr:
22035 case DW_FORM_addrx:
22036 case DW_FORM_GNU_addr_index:
22037 fprintf_unfiltered (f, "address: ");
22038 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22039 break;
22040 case DW_FORM_block2:
22041 case DW_FORM_block4:
22042 case DW_FORM_block:
22043 case DW_FORM_block1:
22044 fprintf_unfiltered (f, "block: size %s",
22045 pulongest (DW_BLOCK (&die->attrs[i])->size));
22046 break;
22047 case DW_FORM_exprloc:
22048 fprintf_unfiltered (f, "expression: size %s",
22049 pulongest (DW_BLOCK (&die->attrs[i])->size));
22050 break;
22051 case DW_FORM_data16:
22052 fprintf_unfiltered (f, "constant of 16 bytes");
22053 break;
22054 case DW_FORM_ref_addr:
22055 fprintf_unfiltered (f, "ref address: ");
22056 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22057 break;
22058 case DW_FORM_GNU_ref_alt:
22059 fprintf_unfiltered (f, "alt ref address: ");
22060 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22061 break;
22062 case DW_FORM_ref1:
22063 case DW_FORM_ref2:
22064 case DW_FORM_ref4:
22065 case DW_FORM_ref8:
22066 case DW_FORM_ref_udata:
22067 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22068 (long) (DW_UNSND (&die->attrs[i])));
22069 break;
22070 case DW_FORM_data1:
22071 case DW_FORM_data2:
22072 case DW_FORM_data4:
22073 case DW_FORM_data8:
22074 case DW_FORM_udata:
22075 case DW_FORM_sdata:
22076 fprintf_unfiltered (f, "constant: %s",
22077 pulongest (DW_UNSND (&die->attrs[i])));
22078 break;
22079 case DW_FORM_sec_offset:
22080 fprintf_unfiltered (f, "section offset: %s",
22081 pulongest (DW_UNSND (&die->attrs[i])));
22082 break;
22083 case DW_FORM_ref_sig8:
22084 fprintf_unfiltered (f, "signature: %s",
22085 hex_string (DW_SIGNATURE (&die->attrs[i])));
22086 break;
22087 case DW_FORM_string:
22088 case DW_FORM_strp:
22089 case DW_FORM_line_strp:
22090 case DW_FORM_strx:
22091 case DW_FORM_GNU_str_index:
22092 case DW_FORM_GNU_strp_alt:
22093 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22094 DW_STRING (&die->attrs[i])
22095 ? DW_STRING (&die->attrs[i]) : "",
22096 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22097 break;
22098 case DW_FORM_flag:
22099 if (DW_UNSND (&die->attrs[i]))
22100 fprintf_unfiltered (f, "flag: TRUE");
22101 else
22102 fprintf_unfiltered (f, "flag: FALSE");
22103 break;
22104 case DW_FORM_flag_present:
22105 fprintf_unfiltered (f, "flag: TRUE");
22106 break;
22107 case DW_FORM_indirect:
22108 /* The reader will have reduced the indirect form to
22109 the "base form" so this form should not occur. */
22110 fprintf_unfiltered (f,
22111 "unexpected attribute form: DW_FORM_indirect");
22112 break;
22113 case DW_FORM_implicit_const:
22114 fprintf_unfiltered (f, "constant: %s",
22115 plongest (DW_SND (&die->attrs[i])));
22116 break;
22117 default:
22118 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22119 die->attrs[i].form);
22120 break;
22121 }
22122 fprintf_unfiltered (f, "\n");
22123 }
22124 }
22125
22126 static void
22127 dump_die_for_error (struct die_info *die)
22128 {
22129 dump_die_shallow (gdb_stderr, 0, die);
22130 }
22131
22132 static void
22133 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22134 {
22135 int indent = level * 4;
22136
22137 gdb_assert (die != NULL);
22138
22139 if (level >= max_level)
22140 return;
22141
22142 dump_die_shallow (f, indent, die);
22143
22144 if (die->child != NULL)
22145 {
22146 print_spaces (indent, f);
22147 fprintf_unfiltered (f, " Children:");
22148 if (level + 1 < max_level)
22149 {
22150 fprintf_unfiltered (f, "\n");
22151 dump_die_1 (f, level + 1, max_level, die->child);
22152 }
22153 else
22154 {
22155 fprintf_unfiltered (f,
22156 " [not printed, max nesting level reached]\n");
22157 }
22158 }
22159
22160 if (die->sibling != NULL && level > 0)
22161 {
22162 dump_die_1 (f, level, max_level, die->sibling);
22163 }
22164 }
22165
22166 /* This is called from the pdie macro in gdbinit.in.
22167 It's not static so gcc will keep a copy callable from gdb. */
22168
22169 void
22170 dump_die (struct die_info *die, int max_level)
22171 {
22172 dump_die_1 (gdb_stdlog, 0, max_level, die);
22173 }
22174
22175 static void
22176 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22177 {
22178 void **slot;
22179
22180 slot = htab_find_slot_with_hash (cu->die_hash, die,
22181 to_underlying (die->sect_off),
22182 INSERT);
22183
22184 *slot = die;
22185 }
22186
22187 /* Follow reference or signature attribute ATTR of SRC_DIE.
22188 On entry *REF_CU is the CU of SRC_DIE.
22189 On exit *REF_CU is the CU of the result. */
22190
22191 static struct die_info *
22192 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22193 struct dwarf2_cu **ref_cu)
22194 {
22195 struct die_info *die;
22196
22197 if (attr->form_is_ref ())
22198 die = follow_die_ref (src_die, attr, ref_cu);
22199 else if (attr->form == DW_FORM_ref_sig8)
22200 die = follow_die_sig (src_die, attr, ref_cu);
22201 else
22202 {
22203 dump_die_for_error (src_die);
22204 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22205 objfile_name ((*ref_cu)->per_objfile->objfile));
22206 }
22207
22208 return die;
22209 }
22210
22211 /* Follow reference OFFSET.
22212 On entry *REF_CU is the CU of the source die referencing OFFSET.
22213 On exit *REF_CU is the CU of the result.
22214 Returns NULL if OFFSET is invalid. */
22215
22216 static struct die_info *
22217 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22218 struct dwarf2_cu **ref_cu)
22219 {
22220 struct die_info temp_die;
22221 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22222 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22223
22224 gdb_assert (cu->per_cu != NULL);
22225
22226 target_cu = cu;
22227
22228 if (cu->per_cu->is_debug_types)
22229 {
22230 /* .debug_types CUs cannot reference anything outside their CU.
22231 If they need to, they have to reference a signatured type via
22232 DW_FORM_ref_sig8. */
22233 if (!cu->header.offset_in_cu_p (sect_off))
22234 return NULL;
22235 }
22236 else if (offset_in_dwz != cu->per_cu->is_dwz
22237 || !cu->header.offset_in_cu_p (sect_off))
22238 {
22239 struct dwarf2_per_cu_data *per_cu;
22240
22241 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22242 dwarf2_per_objfile);
22243
22244 /* If necessary, add it to the queue and load its DIEs. */
22245 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
22246 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22247
22248 target_cu = per_cu->cu;
22249 }
22250 else if (cu->dies == NULL)
22251 {
22252 /* We're loading full DIEs during partial symbol reading. */
22253 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22254 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22255 language_minimal);
22256 }
22257
22258 *ref_cu = target_cu;
22259 temp_die.sect_off = sect_off;
22260
22261 if (target_cu != cu)
22262 target_cu->ancestor = cu;
22263
22264 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22265 &temp_die,
22266 to_underlying (sect_off));
22267 }
22268
22269 /* Follow reference attribute ATTR of SRC_DIE.
22270 On entry *REF_CU is the CU of SRC_DIE.
22271 On exit *REF_CU is the CU of the result. */
22272
22273 static struct die_info *
22274 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22275 struct dwarf2_cu **ref_cu)
22276 {
22277 sect_offset sect_off = attr->get_ref_die_offset ();
22278 struct dwarf2_cu *cu = *ref_cu;
22279 struct die_info *die;
22280
22281 die = follow_die_offset (sect_off,
22282 (attr->form == DW_FORM_GNU_ref_alt
22283 || cu->per_cu->is_dwz),
22284 ref_cu);
22285 if (!die)
22286 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22287 "at %s [in module %s]"),
22288 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22289 objfile_name (cu->per_objfile->objfile));
22290
22291 return die;
22292 }
22293
22294 /* See read.h. */
22295
22296 struct dwarf2_locexpr_baton
22297 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22298 dwarf2_per_cu_data *per_cu,
22299 CORE_ADDR (*get_frame_pc) (void *baton),
22300 void *baton, bool resolve_abstract_p)
22301 {
22302 struct dwarf2_cu *cu;
22303 struct die_info *die;
22304 struct attribute *attr;
22305 struct dwarf2_locexpr_baton retval;
22306 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22307 struct objfile *objfile = dwarf2_per_objfile->objfile;
22308
22309 if (per_cu->cu == NULL)
22310 load_cu (per_cu, dwarf2_per_objfile, false);
22311 cu = per_cu->cu;
22312 if (cu == NULL)
22313 {
22314 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22315 Instead just throw an error, not much else we can do. */
22316 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22317 sect_offset_str (sect_off), objfile_name (objfile));
22318 }
22319
22320 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22321 if (!die)
22322 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22323 sect_offset_str (sect_off), objfile_name (objfile));
22324
22325 attr = dwarf2_attr (die, DW_AT_location, cu);
22326 if (!attr && resolve_abstract_p
22327 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22328 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22329 {
22330 CORE_ADDR pc = (*get_frame_pc) (baton);
22331 CORE_ADDR baseaddr = objfile->text_section_offset ();
22332 struct gdbarch *gdbarch = objfile->arch ();
22333
22334 for (const auto &cand_off
22335 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22336 {
22337 struct dwarf2_cu *cand_cu = cu;
22338 struct die_info *cand
22339 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22340 if (!cand
22341 || !cand->parent
22342 || cand->parent->tag != DW_TAG_subprogram)
22343 continue;
22344
22345 CORE_ADDR pc_low, pc_high;
22346 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22347 if (pc_low == ((CORE_ADDR) -1))
22348 continue;
22349 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22350 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22351 if (!(pc_low <= pc && pc < pc_high))
22352 continue;
22353
22354 die = cand;
22355 attr = dwarf2_attr (die, DW_AT_location, cu);
22356 break;
22357 }
22358 }
22359
22360 if (!attr)
22361 {
22362 /* DWARF: "If there is no such attribute, then there is no effect.".
22363 DATA is ignored if SIZE is 0. */
22364
22365 retval.data = NULL;
22366 retval.size = 0;
22367 }
22368 else if (attr->form_is_section_offset ())
22369 {
22370 struct dwarf2_loclist_baton loclist_baton;
22371 CORE_ADDR pc = (*get_frame_pc) (baton);
22372 size_t size;
22373
22374 fill_in_loclist_baton (cu, &loclist_baton, attr);
22375
22376 retval.data = dwarf2_find_location_expression (&loclist_baton,
22377 &size, pc);
22378 retval.size = size;
22379 }
22380 else
22381 {
22382 if (!attr->form_is_block ())
22383 error (_("Dwarf Error: DIE at %s referenced in module %s "
22384 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22385 sect_offset_str (sect_off), objfile_name (objfile));
22386
22387 retval.data = DW_BLOCK (attr)->data;
22388 retval.size = DW_BLOCK (attr)->size;
22389 }
22390 retval.per_objfile = dwarf2_per_objfile;
22391 retval.per_cu = cu->per_cu;
22392
22393 age_cached_comp_units (dwarf2_per_objfile);
22394
22395 return retval;
22396 }
22397
22398 /* See read.h. */
22399
22400 struct dwarf2_locexpr_baton
22401 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22402 dwarf2_per_cu_data *per_cu,
22403 CORE_ADDR (*get_frame_pc) (void *baton),
22404 void *baton)
22405 {
22406 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22407
22408 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
22409 }
22410
22411 /* Write a constant of a given type as target-ordered bytes into
22412 OBSTACK. */
22413
22414 static const gdb_byte *
22415 write_constant_as_bytes (struct obstack *obstack,
22416 enum bfd_endian byte_order,
22417 struct type *type,
22418 ULONGEST value,
22419 LONGEST *len)
22420 {
22421 gdb_byte *result;
22422
22423 *len = TYPE_LENGTH (type);
22424 result = (gdb_byte *) obstack_alloc (obstack, *len);
22425 store_unsigned_integer (result, *len, byte_order, value);
22426
22427 return result;
22428 }
22429
22430 /* See read.h. */
22431
22432 const gdb_byte *
22433 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22434 dwarf2_per_cu_data *per_cu,
22435 obstack *obstack,
22436 LONGEST *len)
22437 {
22438 struct dwarf2_cu *cu;
22439 struct die_info *die;
22440 struct attribute *attr;
22441 const gdb_byte *result = NULL;
22442 struct type *type;
22443 LONGEST value;
22444 enum bfd_endian byte_order;
22445 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
22446
22447 if (per_cu->cu == NULL)
22448 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22449 cu = per_cu->cu;
22450 if (cu == NULL)
22451 {
22452 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22453 Instead just throw an error, not much else we can do. */
22454 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22455 sect_offset_str (sect_off), objfile_name (objfile));
22456 }
22457
22458 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22459 if (!die)
22460 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22461 sect_offset_str (sect_off), objfile_name (objfile));
22462
22463 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22464 if (attr == NULL)
22465 return NULL;
22466
22467 byte_order = (bfd_big_endian (objfile->obfd)
22468 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22469
22470 switch (attr->form)
22471 {
22472 case DW_FORM_addr:
22473 case DW_FORM_addrx:
22474 case DW_FORM_GNU_addr_index:
22475 {
22476 gdb_byte *tem;
22477
22478 *len = cu->header.addr_size;
22479 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22480 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22481 result = tem;
22482 }
22483 break;
22484 case DW_FORM_string:
22485 case DW_FORM_strp:
22486 case DW_FORM_strx:
22487 case DW_FORM_GNU_str_index:
22488 case DW_FORM_GNU_strp_alt:
22489 /* DW_STRING is already allocated on the objfile obstack, point
22490 directly to it. */
22491 result = (const gdb_byte *) DW_STRING (attr);
22492 *len = strlen (DW_STRING (attr));
22493 break;
22494 case DW_FORM_block1:
22495 case DW_FORM_block2:
22496 case DW_FORM_block4:
22497 case DW_FORM_block:
22498 case DW_FORM_exprloc:
22499 case DW_FORM_data16:
22500 result = DW_BLOCK (attr)->data;
22501 *len = DW_BLOCK (attr)->size;
22502 break;
22503
22504 /* The DW_AT_const_value attributes are supposed to carry the
22505 symbol's value "represented as it would be on the target
22506 architecture." By the time we get here, it's already been
22507 converted to host endianness, so we just need to sign- or
22508 zero-extend it as appropriate. */
22509 case DW_FORM_data1:
22510 type = die_type (die, cu);
22511 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22512 if (result == NULL)
22513 result = write_constant_as_bytes (obstack, byte_order,
22514 type, value, len);
22515 break;
22516 case DW_FORM_data2:
22517 type = die_type (die, cu);
22518 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22519 if (result == NULL)
22520 result = write_constant_as_bytes (obstack, byte_order,
22521 type, value, len);
22522 break;
22523 case DW_FORM_data4:
22524 type = die_type (die, cu);
22525 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22526 if (result == NULL)
22527 result = write_constant_as_bytes (obstack, byte_order,
22528 type, value, len);
22529 break;
22530 case DW_FORM_data8:
22531 type = die_type (die, cu);
22532 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22533 if (result == NULL)
22534 result = write_constant_as_bytes (obstack, byte_order,
22535 type, value, len);
22536 break;
22537
22538 case DW_FORM_sdata:
22539 case DW_FORM_implicit_const:
22540 type = die_type (die, cu);
22541 result = write_constant_as_bytes (obstack, byte_order,
22542 type, DW_SND (attr), len);
22543 break;
22544
22545 case DW_FORM_udata:
22546 type = die_type (die, cu);
22547 result = write_constant_as_bytes (obstack, byte_order,
22548 type, DW_UNSND (attr), len);
22549 break;
22550
22551 default:
22552 complaint (_("unsupported const value attribute form: '%s'"),
22553 dwarf_form_name (attr->form));
22554 break;
22555 }
22556
22557 return result;
22558 }
22559
22560 /* See read.h. */
22561
22562 struct type *
22563 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22564 dwarf2_per_cu_data *per_cu)
22565 {
22566 struct dwarf2_cu *cu;
22567 struct die_info *die;
22568
22569 if (per_cu->cu == NULL)
22570 load_cu (per_cu, per_cu->dwarf2_per_objfile, false);
22571 cu = per_cu->cu;
22572 if (!cu)
22573 return NULL;
22574
22575 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22576 if (!die)
22577 return NULL;
22578
22579 return die_type (die, cu);
22580 }
22581
22582 /* See read.h. */
22583
22584 struct type *
22585 dwarf2_get_die_type (cu_offset die_offset,
22586 struct dwarf2_per_cu_data *per_cu)
22587 {
22588 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22589 return get_die_type_at_offset (die_offset_sect, per_cu);
22590 }
22591
22592 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22593 On entry *REF_CU is the CU of SRC_DIE.
22594 On exit *REF_CU is the CU of the result.
22595 Returns NULL if the referenced DIE isn't found. */
22596
22597 static struct die_info *
22598 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22599 struct dwarf2_cu **ref_cu)
22600 {
22601 struct die_info temp_die;
22602 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22603 struct die_info *die;
22604
22605 /* While it might be nice to assert sig_type->type == NULL here,
22606 we can get here for DW_AT_imported_declaration where we need
22607 the DIE not the type. */
22608
22609 /* If necessary, add it to the queue and load its DIEs. */
22610
22611 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
22612 read_signatured_type (sig_type, (*ref_cu)->per_objfile);
22613
22614 sig_cu = sig_type->per_cu.cu;
22615 gdb_assert (sig_cu != NULL);
22616 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22617 temp_die.sect_off = sig_type->type_offset_in_section;
22618 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22619 to_underlying (temp_die.sect_off));
22620 if (die)
22621 {
22622 struct dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22623
22624 /* For .gdb_index version 7 keep track of included TUs.
22625 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22626 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22627 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22628 {
22629 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22630 }
22631
22632 *ref_cu = sig_cu;
22633 if (sig_cu != cu)
22634 sig_cu->ancestor = cu;
22635
22636 return die;
22637 }
22638
22639 return NULL;
22640 }
22641
22642 /* Follow signatured type referenced by ATTR in SRC_DIE.
22643 On entry *REF_CU is the CU of SRC_DIE.
22644 On exit *REF_CU is the CU of the result.
22645 The result is the DIE of the type.
22646 If the referenced type cannot be found an error is thrown. */
22647
22648 static struct die_info *
22649 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22650 struct dwarf2_cu **ref_cu)
22651 {
22652 ULONGEST signature = DW_SIGNATURE (attr);
22653 struct signatured_type *sig_type;
22654 struct die_info *die;
22655
22656 gdb_assert (attr->form == DW_FORM_ref_sig8);
22657
22658 sig_type = lookup_signatured_type (*ref_cu, signature);
22659 /* sig_type will be NULL if the signatured type is missing from
22660 the debug info. */
22661 if (sig_type == NULL)
22662 {
22663 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22664 " from DIE at %s [in module %s]"),
22665 hex_string (signature), sect_offset_str (src_die->sect_off),
22666 objfile_name ((*ref_cu)->per_objfile->objfile));
22667 }
22668
22669 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22670 if (die == NULL)
22671 {
22672 dump_die_for_error (src_die);
22673 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22674 " from DIE at %s [in module %s]"),
22675 hex_string (signature), sect_offset_str (src_die->sect_off),
22676 objfile_name ((*ref_cu)->per_objfile->objfile));
22677 }
22678
22679 return die;
22680 }
22681
22682 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22683 reading in and processing the type unit if necessary. */
22684
22685 static struct type *
22686 get_signatured_type (struct die_info *die, ULONGEST signature,
22687 struct dwarf2_cu *cu)
22688 {
22689 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22690 struct signatured_type *sig_type;
22691 struct dwarf2_cu *type_cu;
22692 struct die_info *type_die;
22693 struct type *type;
22694
22695 sig_type = lookup_signatured_type (cu, signature);
22696 /* sig_type will be NULL if the signatured type is missing from
22697 the debug info. */
22698 if (sig_type == NULL)
22699 {
22700 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22701 " from DIE at %s [in module %s]"),
22702 hex_string (signature), sect_offset_str (die->sect_off),
22703 objfile_name (dwarf2_per_objfile->objfile));
22704 return build_error_marker_type (cu, die);
22705 }
22706
22707 /* If we already know the type we're done. */
22708 if (sig_type->type != NULL)
22709 return sig_type->type;
22710
22711 type_cu = cu;
22712 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22713 if (type_die != NULL)
22714 {
22715 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22716 is created. This is important, for example, because for c++ classes
22717 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22718 type = read_type_die (type_die, type_cu);
22719 if (type == NULL)
22720 {
22721 complaint (_("Dwarf Error: Cannot build signatured type %s"
22722 " referenced from DIE at %s [in module %s]"),
22723 hex_string (signature), sect_offset_str (die->sect_off),
22724 objfile_name (dwarf2_per_objfile->objfile));
22725 type = build_error_marker_type (cu, die);
22726 }
22727 }
22728 else
22729 {
22730 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22731 " from DIE at %s [in module %s]"),
22732 hex_string (signature), sect_offset_str (die->sect_off),
22733 objfile_name (dwarf2_per_objfile->objfile));
22734 type = build_error_marker_type (cu, die);
22735 }
22736 sig_type->type = type;
22737
22738 return type;
22739 }
22740
22741 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22742 reading in and processing the type unit if necessary. */
22743
22744 static struct type *
22745 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22746 struct dwarf2_cu *cu) /* ARI: editCase function */
22747 {
22748 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22749 if (attr->form_is_ref ())
22750 {
22751 struct dwarf2_cu *type_cu = cu;
22752 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22753
22754 return read_type_die (type_die, type_cu);
22755 }
22756 else if (attr->form == DW_FORM_ref_sig8)
22757 {
22758 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22759 }
22760 else
22761 {
22762 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22763
22764 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22765 " at %s [in module %s]"),
22766 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22767 objfile_name (dwarf2_per_objfile->objfile));
22768 return build_error_marker_type (cu, die);
22769 }
22770 }
22771
22772 /* Load the DIEs associated with type unit PER_CU into memory. */
22773
22774 static void
22775 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22776 dwarf2_per_objfile *per_objfile)
22777 {
22778 struct signatured_type *sig_type;
22779
22780 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22781 gdb_assert (! per_cu->type_unit_group_p ());
22782
22783 /* We have the per_cu, but we need the signatured_type.
22784 Fortunately this is an easy translation. */
22785 gdb_assert (per_cu->is_debug_types);
22786 sig_type = (struct signatured_type *) per_cu;
22787
22788 gdb_assert (per_cu->cu == NULL);
22789
22790 read_signatured_type (sig_type, per_objfile);
22791
22792 gdb_assert (per_cu->cu != NULL);
22793 }
22794
22795 /* Read in a signatured type and build its CU and DIEs.
22796 If the type is a stub for the real type in a DWO file,
22797 read in the real type from the DWO file as well. */
22798
22799 static void
22800 read_signatured_type (signatured_type *sig_type,
22801 dwarf2_per_objfile *per_objfile)
22802 {
22803 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22804
22805 gdb_assert (per_cu->is_debug_types);
22806 gdb_assert (per_cu->cu == NULL);
22807
22808 cutu_reader reader (per_cu, per_objfile, NULL, 0, false);
22809
22810 if (!reader.dummy_p)
22811 {
22812 struct dwarf2_cu *cu = reader.cu;
22813 const gdb_byte *info_ptr = reader.info_ptr;
22814
22815 gdb_assert (cu->die_hash == NULL);
22816 cu->die_hash =
22817 htab_create_alloc_ex (cu->header.length / 12,
22818 die_hash,
22819 die_eq,
22820 NULL,
22821 &cu->comp_unit_obstack,
22822 hashtab_obstack_allocate,
22823 dummy_obstack_deallocate);
22824
22825 if (reader.comp_unit_die->has_children)
22826 reader.comp_unit_die->child
22827 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22828 reader.comp_unit_die);
22829 cu->dies = reader.comp_unit_die;
22830 /* comp_unit_die is not stored in die_hash, no need. */
22831
22832 /* We try not to read any attributes in this function, because
22833 not all CUs needed for references have been loaded yet, and
22834 symbol table processing isn't initialized. But we have to
22835 set the CU language, or we won't be able to build types
22836 correctly. Similarly, if we do not read the producer, we can
22837 not apply producer-specific interpretation. */
22838 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22839
22840 reader.keep ();
22841 }
22842
22843 sig_type->per_cu.tu_read = 1;
22844 }
22845
22846 /* Decode simple location descriptions.
22847 Given a pointer to a dwarf block that defines a location, compute
22848 the location and return the value. If COMPUTED is non-null, it is
22849 set to true to indicate that decoding was successful, and false
22850 otherwise. If COMPUTED is null, then this function may emit a
22851 complaint. */
22852
22853 static CORE_ADDR
22854 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22855 {
22856 struct objfile *objfile = cu->per_objfile->objfile;
22857 size_t i;
22858 size_t size = blk->size;
22859 const gdb_byte *data = blk->data;
22860 CORE_ADDR stack[64];
22861 int stacki;
22862 unsigned int bytes_read, unsnd;
22863 gdb_byte op;
22864
22865 if (computed != nullptr)
22866 *computed = false;
22867
22868 i = 0;
22869 stacki = 0;
22870 stack[stacki] = 0;
22871 stack[++stacki] = 0;
22872
22873 while (i < size)
22874 {
22875 op = data[i++];
22876 switch (op)
22877 {
22878 case DW_OP_lit0:
22879 case DW_OP_lit1:
22880 case DW_OP_lit2:
22881 case DW_OP_lit3:
22882 case DW_OP_lit4:
22883 case DW_OP_lit5:
22884 case DW_OP_lit6:
22885 case DW_OP_lit7:
22886 case DW_OP_lit8:
22887 case DW_OP_lit9:
22888 case DW_OP_lit10:
22889 case DW_OP_lit11:
22890 case DW_OP_lit12:
22891 case DW_OP_lit13:
22892 case DW_OP_lit14:
22893 case DW_OP_lit15:
22894 case DW_OP_lit16:
22895 case DW_OP_lit17:
22896 case DW_OP_lit18:
22897 case DW_OP_lit19:
22898 case DW_OP_lit20:
22899 case DW_OP_lit21:
22900 case DW_OP_lit22:
22901 case DW_OP_lit23:
22902 case DW_OP_lit24:
22903 case DW_OP_lit25:
22904 case DW_OP_lit26:
22905 case DW_OP_lit27:
22906 case DW_OP_lit28:
22907 case DW_OP_lit29:
22908 case DW_OP_lit30:
22909 case DW_OP_lit31:
22910 stack[++stacki] = op - DW_OP_lit0;
22911 break;
22912
22913 case DW_OP_reg0:
22914 case DW_OP_reg1:
22915 case DW_OP_reg2:
22916 case DW_OP_reg3:
22917 case DW_OP_reg4:
22918 case DW_OP_reg5:
22919 case DW_OP_reg6:
22920 case DW_OP_reg7:
22921 case DW_OP_reg8:
22922 case DW_OP_reg9:
22923 case DW_OP_reg10:
22924 case DW_OP_reg11:
22925 case DW_OP_reg12:
22926 case DW_OP_reg13:
22927 case DW_OP_reg14:
22928 case DW_OP_reg15:
22929 case DW_OP_reg16:
22930 case DW_OP_reg17:
22931 case DW_OP_reg18:
22932 case DW_OP_reg19:
22933 case DW_OP_reg20:
22934 case DW_OP_reg21:
22935 case DW_OP_reg22:
22936 case DW_OP_reg23:
22937 case DW_OP_reg24:
22938 case DW_OP_reg25:
22939 case DW_OP_reg26:
22940 case DW_OP_reg27:
22941 case DW_OP_reg28:
22942 case DW_OP_reg29:
22943 case DW_OP_reg30:
22944 case DW_OP_reg31:
22945 stack[++stacki] = op - DW_OP_reg0;
22946 if (i < size)
22947 {
22948 if (computed == nullptr)
22949 dwarf2_complex_location_expr_complaint ();
22950 else
22951 return 0;
22952 }
22953 break;
22954
22955 case DW_OP_regx:
22956 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22957 i += bytes_read;
22958 stack[++stacki] = unsnd;
22959 if (i < size)
22960 {
22961 if (computed == nullptr)
22962 dwarf2_complex_location_expr_complaint ();
22963 else
22964 return 0;
22965 }
22966 break;
22967
22968 case DW_OP_addr:
22969 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22970 &bytes_read);
22971 i += bytes_read;
22972 break;
22973
22974 case DW_OP_const1u:
22975 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22976 i += 1;
22977 break;
22978
22979 case DW_OP_const1s:
22980 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22981 i += 1;
22982 break;
22983
22984 case DW_OP_const2u:
22985 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22986 i += 2;
22987 break;
22988
22989 case DW_OP_const2s:
22990 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22991 i += 2;
22992 break;
22993
22994 case DW_OP_const4u:
22995 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22996 i += 4;
22997 break;
22998
22999 case DW_OP_const4s:
23000 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23001 i += 4;
23002 break;
23003
23004 case DW_OP_const8u:
23005 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23006 i += 8;
23007 break;
23008
23009 case DW_OP_constu:
23010 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23011 &bytes_read);
23012 i += bytes_read;
23013 break;
23014
23015 case DW_OP_consts:
23016 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23017 i += bytes_read;
23018 break;
23019
23020 case DW_OP_dup:
23021 stack[stacki + 1] = stack[stacki];
23022 stacki++;
23023 break;
23024
23025 case DW_OP_plus:
23026 stack[stacki - 1] += stack[stacki];
23027 stacki--;
23028 break;
23029
23030 case DW_OP_plus_uconst:
23031 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23032 &bytes_read);
23033 i += bytes_read;
23034 break;
23035
23036 case DW_OP_minus:
23037 stack[stacki - 1] -= stack[stacki];
23038 stacki--;
23039 break;
23040
23041 case DW_OP_deref:
23042 /* If we're not the last op, then we definitely can't encode
23043 this using GDB's address_class enum. This is valid for partial
23044 global symbols, although the variable's address will be bogus
23045 in the psymtab. */
23046 if (i < size)
23047 {
23048 if (computed == nullptr)
23049 dwarf2_complex_location_expr_complaint ();
23050 else
23051 return 0;
23052 }
23053 break;
23054
23055 case DW_OP_GNU_push_tls_address:
23056 case DW_OP_form_tls_address:
23057 /* The top of the stack has the offset from the beginning
23058 of the thread control block at which the variable is located. */
23059 /* Nothing should follow this operator, so the top of stack would
23060 be returned. */
23061 /* This is valid for partial global symbols, but the variable's
23062 address will be bogus in the psymtab. Make it always at least
23063 non-zero to not look as a variable garbage collected by linker
23064 which have DW_OP_addr 0. */
23065 if (i < size)
23066 {
23067 if (computed == nullptr)
23068 dwarf2_complex_location_expr_complaint ();
23069 else
23070 return 0;
23071 }
23072 stack[stacki]++;
23073 break;
23074
23075 case DW_OP_GNU_uninit:
23076 if (computed != nullptr)
23077 return 0;
23078 break;
23079
23080 case DW_OP_addrx:
23081 case DW_OP_GNU_addr_index:
23082 case DW_OP_GNU_const_index:
23083 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23084 &bytes_read);
23085 i += bytes_read;
23086 break;
23087
23088 default:
23089 if (computed == nullptr)
23090 {
23091 const char *name = get_DW_OP_name (op);
23092
23093 if (name)
23094 complaint (_("unsupported stack op: '%s'"),
23095 name);
23096 else
23097 complaint (_("unsupported stack op: '%02x'"),
23098 op);
23099 }
23100
23101 return (stack[stacki]);
23102 }
23103
23104 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23105 outside of the allocated space. Also enforce minimum>0. */
23106 if (stacki >= ARRAY_SIZE (stack) - 1)
23107 {
23108 if (computed == nullptr)
23109 complaint (_("location description stack overflow"));
23110 return 0;
23111 }
23112
23113 if (stacki <= 0)
23114 {
23115 if (computed == nullptr)
23116 complaint (_("location description stack underflow"));
23117 return 0;
23118 }
23119 }
23120
23121 if (computed != nullptr)
23122 *computed = true;
23123 return (stack[stacki]);
23124 }
23125
23126 /* memory allocation interface */
23127
23128 static struct dwarf_block *
23129 dwarf_alloc_block (struct dwarf2_cu *cu)
23130 {
23131 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23132 }
23133
23134 static struct die_info *
23135 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23136 {
23137 struct die_info *die;
23138 size_t size = sizeof (struct die_info);
23139
23140 if (num_attrs > 1)
23141 size += (num_attrs - 1) * sizeof (struct attribute);
23142
23143 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23144 memset (die, 0, sizeof (struct die_info));
23145 return (die);
23146 }
23147
23148 \f
23149
23150 /* Macro support. */
23151
23152 /* An overload of dwarf_decode_macros that finds the correct section
23153 and ensures it is read in before calling the other overload. */
23154
23155 static void
23156 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23157 int section_is_gnu)
23158 {
23159 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23160 struct objfile *objfile = dwarf2_per_objfile->objfile;
23161 const struct line_header *lh = cu->line_header;
23162 unsigned int offset_size = cu->header.offset_size;
23163 struct dwarf2_section_info *section;
23164 const char *section_name;
23165
23166 if (cu->dwo_unit != nullptr)
23167 {
23168 if (section_is_gnu)
23169 {
23170 section = &cu->dwo_unit->dwo_file->sections.macro;
23171 section_name = ".debug_macro.dwo";
23172 }
23173 else
23174 {
23175 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23176 section_name = ".debug_macinfo.dwo";
23177 }
23178 }
23179 else
23180 {
23181 if (section_is_gnu)
23182 {
23183 section = &dwarf2_per_objfile->per_bfd->macro;
23184 section_name = ".debug_macro";
23185 }
23186 else
23187 {
23188 section = &dwarf2_per_objfile->per_bfd->macinfo;
23189 section_name = ".debug_macinfo";
23190 }
23191 }
23192
23193 section->read (objfile);
23194 if (section->buffer == nullptr)
23195 {
23196 complaint (_("missing %s section"), section_name);
23197 return;
23198 }
23199
23200 buildsym_compunit *builder = cu->get_builder ();
23201
23202 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23203 offset_size, offset, section_is_gnu);
23204 }
23205
23206 /* Return the .debug_loc section to use for CU.
23207 For DWO files use .debug_loc.dwo. */
23208
23209 static struct dwarf2_section_info *
23210 cu_debug_loc_section (struct dwarf2_cu *cu)
23211 {
23212 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23213
23214 if (cu->dwo_unit)
23215 {
23216 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23217
23218 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23219 }
23220 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23221 : &dwarf2_per_objfile->per_bfd->loc);
23222 }
23223
23224 /* A helper function that fills in a dwarf2_loclist_baton. */
23225
23226 static void
23227 fill_in_loclist_baton (struct dwarf2_cu *cu,
23228 struct dwarf2_loclist_baton *baton,
23229 const struct attribute *attr)
23230 {
23231 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23232 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23233
23234 section->read (dwarf2_per_objfile->objfile);
23235
23236 baton->per_objfile = dwarf2_per_objfile;
23237 baton->per_cu = cu->per_cu;
23238 gdb_assert (baton->per_cu);
23239 /* We don't know how long the location list is, but make sure we
23240 don't run off the edge of the section. */
23241 baton->size = section->size - DW_UNSND (attr);
23242 baton->data = section->buffer + DW_UNSND (attr);
23243 if (cu->base_address.has_value ())
23244 baton->base_address = *cu->base_address;
23245 else
23246 baton->base_address = 0;
23247 baton->from_dwo = cu->dwo_unit != NULL;
23248 }
23249
23250 static void
23251 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23252 struct dwarf2_cu *cu, int is_block)
23253 {
23254 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23255 struct objfile *objfile = dwarf2_per_objfile->objfile;
23256 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23257
23258 if (attr->form_is_section_offset ()
23259 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23260 the section. If so, fall through to the complaint in the
23261 other branch. */
23262 && DW_UNSND (attr) < section->get_size (objfile))
23263 {
23264 struct dwarf2_loclist_baton *baton;
23265
23266 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23267
23268 fill_in_loclist_baton (cu, baton, attr);
23269
23270 if (!cu->base_address.has_value ())
23271 complaint (_("Location list used without "
23272 "specifying the CU base address."));
23273
23274 SYMBOL_ACLASS_INDEX (sym) = (is_block
23275 ? dwarf2_loclist_block_index
23276 : dwarf2_loclist_index);
23277 SYMBOL_LOCATION_BATON (sym) = baton;
23278 }
23279 else
23280 {
23281 struct dwarf2_locexpr_baton *baton;
23282
23283 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23284 baton->per_objfile = dwarf2_per_objfile;
23285 baton->per_cu = cu->per_cu;
23286 gdb_assert (baton->per_cu);
23287
23288 if (attr->form_is_block ())
23289 {
23290 /* Note that we're just copying the block's data pointer
23291 here, not the actual data. We're still pointing into the
23292 info_buffer for SYM's objfile; right now we never release
23293 that buffer, but when we do clean up properly this may
23294 need to change. */
23295 baton->size = DW_BLOCK (attr)->size;
23296 baton->data = DW_BLOCK (attr)->data;
23297 }
23298 else
23299 {
23300 dwarf2_invalid_attrib_class_complaint ("location description",
23301 sym->natural_name ());
23302 baton->size = 0;
23303 }
23304
23305 SYMBOL_ACLASS_INDEX (sym) = (is_block
23306 ? dwarf2_locexpr_block_index
23307 : dwarf2_locexpr_index);
23308 SYMBOL_LOCATION_BATON (sym) = baton;
23309 }
23310 }
23311
23312 /* See read.h. */
23313
23314 struct objfile *
23315 dwarf2_per_cu_data::objfile () const
23316 {
23317 struct objfile *objfile = dwarf2_per_objfile->objfile;
23318
23319 /* Return the master objfile, so that we can report and look up the
23320 correct file containing this variable. */
23321 if (objfile->separate_debug_objfile_backlink)
23322 objfile = objfile->separate_debug_objfile_backlink;
23323
23324 return objfile;
23325 }
23326
23327 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23328 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23329 CU_HEADERP first. */
23330
23331 static const struct comp_unit_head *
23332 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
23333 const struct dwarf2_per_cu_data *per_cu)
23334 {
23335 const gdb_byte *info_ptr;
23336
23337 if (per_cu->cu)
23338 return &per_cu->cu->header;
23339
23340 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
23341
23342 memset (cu_headerp, 0, sizeof (*cu_headerp));
23343 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23344 rcuh_kind::COMPILE);
23345
23346 return cu_headerp;
23347 }
23348
23349 /* See read.h. */
23350
23351 int
23352 dwarf2_per_cu_data::addr_size () const
23353 {
23354 struct comp_unit_head cu_header_local;
23355 const struct comp_unit_head *cu_headerp;
23356
23357 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23358
23359 return cu_headerp->addr_size;
23360 }
23361
23362 /* See read.h. */
23363
23364 int
23365 dwarf2_per_cu_data::offset_size () const
23366 {
23367 struct comp_unit_head cu_header_local;
23368 const struct comp_unit_head *cu_headerp;
23369
23370 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23371
23372 return cu_headerp->offset_size;
23373 }
23374
23375 /* See read.h. */
23376
23377 int
23378 dwarf2_per_cu_data::ref_addr_size () const
23379 {
23380 struct comp_unit_head cu_header_local;
23381 const struct comp_unit_head *cu_headerp;
23382
23383 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
23384
23385 if (cu_headerp->version == 2)
23386 return cu_headerp->addr_size;
23387 else
23388 return cu_headerp->offset_size;
23389 }
23390
23391 /* See read.h. */
23392
23393 CORE_ADDR
23394 dwarf2_per_cu_data::text_offset () const
23395 {
23396 struct objfile *objfile = dwarf2_per_objfile->objfile;
23397
23398 return objfile->text_section_offset ();
23399 }
23400
23401 /* See read.h. */
23402
23403 struct type *
23404 dwarf2_per_cu_data::addr_type () const
23405 {
23406 struct objfile *objfile = dwarf2_per_objfile->objfile;
23407 struct type *void_type = objfile_type (objfile)->builtin_void;
23408 struct type *addr_type = lookup_pointer_type (void_type);
23409 int addr_size = this->addr_size ();
23410
23411 if (TYPE_LENGTH (addr_type) == addr_size)
23412 return addr_type;
23413
23414 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23415 return addr_type;
23416 }
23417
23418 /* A helper function for dwarf2_find_containing_comp_unit that returns
23419 the index of the result, and that searches a vector. It will
23420 return a result even if the offset in question does not actually
23421 occur in any CU. This is separate so that it can be unit
23422 tested. */
23423
23424 static int
23425 dwarf2_find_containing_comp_unit
23426 (sect_offset sect_off,
23427 unsigned int offset_in_dwz,
23428 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23429 {
23430 int low, high;
23431
23432 low = 0;
23433 high = all_comp_units.size () - 1;
23434 while (high > low)
23435 {
23436 struct dwarf2_per_cu_data *mid_cu;
23437 int mid = low + (high - low) / 2;
23438
23439 mid_cu = all_comp_units[mid];
23440 if (mid_cu->is_dwz > offset_in_dwz
23441 || (mid_cu->is_dwz == offset_in_dwz
23442 && mid_cu->sect_off + mid_cu->length > sect_off))
23443 high = mid;
23444 else
23445 low = mid + 1;
23446 }
23447 gdb_assert (low == high);
23448 return low;
23449 }
23450
23451 /* Locate the .debug_info compilation unit from CU's objfile which contains
23452 the DIE at OFFSET. Raises an error on failure. */
23453
23454 static struct dwarf2_per_cu_data *
23455 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23456 unsigned int offset_in_dwz,
23457 struct dwarf2_per_objfile *dwarf2_per_objfile)
23458 {
23459 int low
23460 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23461 dwarf2_per_objfile->per_bfd->all_comp_units);
23462 struct dwarf2_per_cu_data *this_cu
23463 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23464
23465 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23466 {
23467 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23468 error (_("Dwarf Error: could not find partial DIE containing "
23469 "offset %s [in module %s]"),
23470 sect_offset_str (sect_off),
23471 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23472
23473 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23474 <= sect_off);
23475 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23476 }
23477 else
23478 {
23479 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23480 && sect_off >= this_cu->sect_off + this_cu->length)
23481 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23482 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23483 return this_cu;
23484 }
23485 }
23486
23487 #if GDB_SELF_TEST
23488
23489 namespace selftests {
23490 namespace find_containing_comp_unit {
23491
23492 static void
23493 run_test ()
23494 {
23495 struct dwarf2_per_cu_data one {};
23496 struct dwarf2_per_cu_data two {};
23497 struct dwarf2_per_cu_data three {};
23498 struct dwarf2_per_cu_data four {};
23499
23500 one.length = 5;
23501 two.sect_off = sect_offset (one.length);
23502 two.length = 7;
23503
23504 three.length = 5;
23505 three.is_dwz = 1;
23506 four.sect_off = sect_offset (three.length);
23507 four.length = 7;
23508 four.is_dwz = 1;
23509
23510 std::vector<dwarf2_per_cu_data *> units;
23511 units.push_back (&one);
23512 units.push_back (&two);
23513 units.push_back (&three);
23514 units.push_back (&four);
23515
23516 int result;
23517
23518 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23519 SELF_CHECK (units[result] == &one);
23520 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23521 SELF_CHECK (units[result] == &one);
23522 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23523 SELF_CHECK (units[result] == &two);
23524
23525 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23526 SELF_CHECK (units[result] == &three);
23527 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23528 SELF_CHECK (units[result] == &three);
23529 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23530 SELF_CHECK (units[result] == &four);
23531 }
23532
23533 }
23534 }
23535
23536 #endif /* GDB_SELF_TEST */
23537
23538 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23539
23540 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23541 dwarf2_per_objfile *per_objfile)
23542 : per_cu (per_cu),
23543 per_objfile (per_objfile),
23544 mark (false),
23545 has_loclist (false),
23546 checked_producer (false),
23547 producer_is_gxx_lt_4_6 (false),
23548 producer_is_gcc_lt_4_3 (false),
23549 producer_is_icc (false),
23550 producer_is_icc_lt_14 (false),
23551 producer_is_codewarrior (false),
23552 processing_has_namespace_info (false)
23553 {
23554 per_cu->cu = this;
23555 }
23556
23557 /* Destroy a dwarf2_cu. */
23558
23559 dwarf2_cu::~dwarf2_cu ()
23560 {
23561 per_cu->cu = NULL;
23562 }
23563
23564 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23565
23566 static void
23567 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23568 enum language pretend_language)
23569 {
23570 struct attribute *attr;
23571
23572 /* Set the language we're debugging. */
23573 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23574 if (attr != nullptr)
23575 set_cu_language (DW_UNSND (attr), cu);
23576 else
23577 {
23578 cu->language = pretend_language;
23579 cu->language_defn = language_def (cu->language);
23580 }
23581
23582 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23583 }
23584
23585 /* Increase the age counter on each cached compilation unit, and free
23586 any that are too old. */
23587
23588 static void
23589 age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
23590 {
23591 struct dwarf2_per_cu_data *per_cu, **last_chain;
23592
23593 dwarf2_clear_marks (dwarf2_per_objfile->per_bfd->read_in_chain);
23594 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23595 while (per_cu != NULL)
23596 {
23597 per_cu->cu->last_used ++;
23598 if (per_cu->cu->last_used <= dwarf_max_cache_age)
23599 dwarf2_mark (per_cu->cu);
23600 per_cu = per_cu->cu->read_in_chain;
23601 }
23602
23603 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23604 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23605 while (per_cu != NULL)
23606 {
23607 struct dwarf2_per_cu_data *next_cu;
23608
23609 next_cu = per_cu->cu->read_in_chain;
23610
23611 if (!per_cu->cu->mark)
23612 {
23613 delete per_cu->cu;
23614 *last_chain = next_cu;
23615 }
23616 else
23617 last_chain = &per_cu->cu->read_in_chain;
23618
23619 per_cu = next_cu;
23620 }
23621 }
23622
23623 /* Remove a single compilation unit from the cache. */
23624
23625 static void
23626 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
23627 {
23628 struct dwarf2_per_cu_data *per_cu, **last_chain;
23629 struct dwarf2_per_objfile *dwarf2_per_objfile
23630 = target_per_cu->dwarf2_per_objfile;
23631
23632 per_cu = dwarf2_per_objfile->per_bfd->read_in_chain;
23633 last_chain = &dwarf2_per_objfile->per_bfd->read_in_chain;
23634 while (per_cu != NULL)
23635 {
23636 struct dwarf2_per_cu_data *next_cu;
23637
23638 next_cu = per_cu->cu->read_in_chain;
23639
23640 if (per_cu == target_per_cu)
23641 {
23642 delete per_cu->cu;
23643 per_cu->cu = NULL;
23644 *last_chain = next_cu;
23645 break;
23646 }
23647 else
23648 last_chain = &per_cu->cu->read_in_chain;
23649
23650 per_cu = next_cu;
23651 }
23652 }
23653
23654 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23655 We store these in a hash table separate from the DIEs, and preserve them
23656 when the DIEs are flushed out of cache.
23657
23658 The CU "per_cu" pointer is needed because offset alone is not enough to
23659 uniquely identify the type. A file may have multiple .debug_types sections,
23660 or the type may come from a DWO file. Furthermore, while it's more logical
23661 to use per_cu->section+offset, with Fission the section with the data is in
23662 the DWO file but we don't know that section at the point we need it.
23663 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23664 because we can enter the lookup routine, get_die_type_at_offset, from
23665 outside this file, and thus won't necessarily have PER_CU->cu.
23666 Fortunately, PER_CU is stable for the life of the objfile. */
23667
23668 struct dwarf2_per_cu_offset_and_type
23669 {
23670 const struct dwarf2_per_cu_data *per_cu;
23671 sect_offset sect_off;
23672 struct type *type;
23673 };
23674
23675 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23676
23677 static hashval_t
23678 per_cu_offset_and_type_hash (const void *item)
23679 {
23680 const struct dwarf2_per_cu_offset_and_type *ofs
23681 = (const struct dwarf2_per_cu_offset_and_type *) item;
23682
23683 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23684 }
23685
23686 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23687
23688 static int
23689 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23690 {
23691 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23692 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23693 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23694 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23695
23696 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23697 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23698 }
23699
23700 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23701 table if necessary. For convenience, return TYPE.
23702
23703 The DIEs reading must have careful ordering to:
23704 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23705 reading current DIE.
23706 * Not trying to dereference contents of still incompletely read in types
23707 while reading in other DIEs.
23708 * Enable referencing still incompletely read in types just by a pointer to
23709 the type without accessing its fields.
23710
23711 Therefore caller should follow these rules:
23712 * Try to fetch any prerequisite types we may need to build this DIE type
23713 before building the type and calling set_die_type.
23714 * After building type call set_die_type for current DIE as soon as
23715 possible before fetching more types to complete the current type.
23716 * Make the type as complete as possible before fetching more types. */
23717
23718 static struct type *
23719 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23720 {
23721 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23722 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23723 struct objfile *objfile = dwarf2_per_objfile->objfile;
23724 struct attribute *attr;
23725 struct dynamic_prop prop;
23726
23727 /* For Ada types, make sure that the gnat-specific data is always
23728 initialized (if not already set). There are a few types where
23729 we should not be doing so, because the type-specific area is
23730 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23731 where the type-specific area is used to store the floatformat).
23732 But this is not a problem, because the gnat-specific information
23733 is actually not needed for these types. */
23734 if (need_gnat_info (cu)
23735 && type->code () != TYPE_CODE_FUNC
23736 && type->code () != TYPE_CODE_FLT
23737 && type->code () != TYPE_CODE_METHODPTR
23738 && type->code () != TYPE_CODE_MEMBERPTR
23739 && type->code () != TYPE_CODE_METHOD
23740 && !HAVE_GNAT_AUX_INFO (type))
23741 INIT_GNAT_SPECIFIC (type);
23742
23743 /* Read DW_AT_allocated and set in type. */
23744 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23745 if (attr != NULL && attr->form_is_block ())
23746 {
23747 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23748 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23749 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23750 }
23751 else if (attr != NULL)
23752 {
23753 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23754 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23755 sect_offset_str (die->sect_off));
23756 }
23757
23758 /* Read DW_AT_associated and set in type. */
23759 attr = dwarf2_attr (die, DW_AT_associated, cu);
23760 if (attr != NULL && attr->form_is_block ())
23761 {
23762 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
23763 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23764 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23765 }
23766 else if (attr != NULL)
23767 {
23768 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23769 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23770 sect_offset_str (die->sect_off));
23771 }
23772
23773 /* Read DW_AT_data_location and set in type. */
23774 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23775 if (attr_to_dynamic_prop (attr, die, cu, &prop,
23776 cu->per_cu->addr_type ()))
23777 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23778
23779 if (dwarf2_per_objfile->die_type_hash == NULL)
23780 dwarf2_per_objfile->die_type_hash
23781 = htab_up (htab_create_alloc (127,
23782 per_cu_offset_and_type_hash,
23783 per_cu_offset_and_type_eq,
23784 NULL, xcalloc, xfree));
23785
23786 ofs.per_cu = cu->per_cu;
23787 ofs.sect_off = die->sect_off;
23788 ofs.type = type;
23789 slot = (struct dwarf2_per_cu_offset_and_type **)
23790 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23791 if (*slot)
23792 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23793 sect_offset_str (die->sect_off));
23794 *slot = XOBNEW (&objfile->objfile_obstack,
23795 struct dwarf2_per_cu_offset_and_type);
23796 **slot = ofs;
23797 return type;
23798 }
23799
23800 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23801 or return NULL if the die does not have a saved type. */
23802
23803 static struct type *
23804 get_die_type_at_offset (sect_offset sect_off,
23805 struct dwarf2_per_cu_data *per_cu)
23806 {
23807 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23808 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23809
23810 if (dwarf2_per_objfile->die_type_hash == NULL)
23811 return NULL;
23812
23813 ofs.per_cu = per_cu;
23814 ofs.sect_off = sect_off;
23815 slot = ((struct dwarf2_per_cu_offset_and_type *)
23816 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23817 if (slot)
23818 return slot->type;
23819 else
23820 return NULL;
23821 }
23822
23823 /* Look up the type for DIE in CU in die_type_hash,
23824 or return NULL if DIE does not have a saved type. */
23825
23826 static struct type *
23827 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23828 {
23829 return get_die_type_at_offset (die->sect_off, cu->per_cu);
23830 }
23831
23832 /* Add a dependence relationship from CU to REF_PER_CU. */
23833
23834 static void
23835 dwarf2_add_dependence (struct dwarf2_cu *cu,
23836 struct dwarf2_per_cu_data *ref_per_cu)
23837 {
23838 void **slot;
23839
23840 if (cu->dependencies == NULL)
23841 cu->dependencies
23842 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23843 NULL, &cu->comp_unit_obstack,
23844 hashtab_obstack_allocate,
23845 dummy_obstack_deallocate);
23846
23847 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23848 if (*slot == NULL)
23849 *slot = ref_per_cu;
23850 }
23851
23852 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23853 Set the mark field in every compilation unit in the
23854 cache that we must keep because we are keeping CU. */
23855
23856 static int
23857 dwarf2_mark_helper (void **slot, void *data)
23858 {
23859 struct dwarf2_per_cu_data *per_cu;
23860
23861 per_cu = (struct dwarf2_per_cu_data *) *slot;
23862
23863 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23864 reading of the chain. As such dependencies remain valid it is not much
23865 useful to track and undo them during QUIT cleanups. */
23866 if (per_cu->cu == NULL)
23867 return 1;
23868
23869 if (per_cu->cu->mark)
23870 return 1;
23871 per_cu->cu->mark = true;
23872
23873 if (per_cu->cu->dependencies != NULL)
23874 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23875
23876 return 1;
23877 }
23878
23879 /* Set the mark field in CU and in every other compilation unit in the
23880 cache that we must keep because we are keeping CU. */
23881
23882 static void
23883 dwarf2_mark (struct dwarf2_cu *cu)
23884 {
23885 if (cu->mark)
23886 return;
23887 cu->mark = true;
23888 if (cu->dependencies != NULL)
23889 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
23890 }
23891
23892 static void
23893 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23894 {
23895 while (per_cu)
23896 {
23897 per_cu->cu->mark = false;
23898 per_cu = per_cu->cu->read_in_chain;
23899 }
23900 }
23901
23902 /* Trivial hash function for partial_die_info: the hash value of a DIE
23903 is its offset in .debug_info for this objfile. */
23904
23905 static hashval_t
23906 partial_die_hash (const void *item)
23907 {
23908 const struct partial_die_info *part_die
23909 = (const struct partial_die_info *) item;
23910
23911 return to_underlying (part_die->sect_off);
23912 }
23913
23914 /* Trivial comparison function for partial_die_info structures: two DIEs
23915 are equal if they have the same offset. */
23916
23917 static int
23918 partial_die_eq (const void *item_lhs, const void *item_rhs)
23919 {
23920 const struct partial_die_info *part_die_lhs
23921 = (const struct partial_die_info *) item_lhs;
23922 const struct partial_die_info *part_die_rhs
23923 = (const struct partial_die_info *) item_rhs;
23924
23925 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23926 }
23927
23928 struct cmd_list_element *set_dwarf_cmdlist;
23929 struct cmd_list_element *show_dwarf_cmdlist;
23930
23931 static void
23932 show_check_physname (struct ui_file *file, int from_tty,
23933 struct cmd_list_element *c, const char *value)
23934 {
23935 fprintf_filtered (file,
23936 _("Whether to check \"physname\" is %s.\n"),
23937 value);
23938 }
23939
23940 void _initialize_dwarf2_read ();
23941 void
23942 _initialize_dwarf2_read ()
23943 {
23944 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23945 Set DWARF specific variables.\n\
23946 Configure DWARF variables such as the cache size."),
23947 &set_dwarf_cmdlist, "maintenance set dwarf ",
23948 0/*allow-unknown*/, &maintenance_set_cmdlist);
23949
23950 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23951 Show DWARF specific variables.\n\
23952 Show DWARF variables such as the cache size."),
23953 &show_dwarf_cmdlist, "maintenance show dwarf ",
23954 0/*allow-unknown*/, &maintenance_show_cmdlist);
23955
23956 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23957 &dwarf_max_cache_age, _("\
23958 Set the upper bound on the age of cached DWARF compilation units."), _("\
23959 Show the upper bound on the age of cached DWARF compilation units."), _("\
23960 A higher limit means that cached compilation units will be stored\n\
23961 in memory longer, and more total memory will be used. Zero disables\n\
23962 caching, which can slow down startup."),
23963 NULL,
23964 show_dwarf_max_cache_age,
23965 &set_dwarf_cmdlist,
23966 &show_dwarf_cmdlist);
23967
23968 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23969 Set debugging of the DWARF reader."), _("\
23970 Show debugging of the DWARF reader."), _("\
23971 When enabled (non-zero), debugging messages are printed during DWARF\n\
23972 reading and symtab expansion. A value of 1 (one) provides basic\n\
23973 information. A value greater than 1 provides more verbose information."),
23974 NULL,
23975 NULL,
23976 &setdebuglist, &showdebuglist);
23977
23978 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23979 Set debugging of the DWARF DIE reader."), _("\
23980 Show debugging of the DWARF DIE reader."), _("\
23981 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23982 The value is the maximum depth to print."),
23983 NULL,
23984 NULL,
23985 &setdebuglist, &showdebuglist);
23986
23987 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23988 Set debugging of the dwarf line reader."), _("\
23989 Show debugging of the dwarf line reader."), _("\
23990 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23991 A value of 1 (one) provides basic information.\n\
23992 A value greater than 1 provides more verbose information."),
23993 NULL,
23994 NULL,
23995 &setdebuglist, &showdebuglist);
23996
23997 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23998 Set cross-checking of \"physname\" code against demangler."), _("\
23999 Show cross-checking of \"physname\" code against demangler."), _("\
24000 When enabled, GDB's internal \"physname\" code is checked against\n\
24001 the demangler."),
24002 NULL, show_check_physname,
24003 &setdebuglist, &showdebuglist);
24004
24005 add_setshow_boolean_cmd ("use-deprecated-index-sections",
24006 no_class, &use_deprecated_index_sections, _("\
24007 Set whether to use deprecated gdb_index sections."), _("\
24008 Show whether to use deprecated gdb_index sections."), _("\
24009 When enabled, deprecated .gdb_index sections are used anyway.\n\
24010 Normally they are ignored either because of a missing feature or\n\
24011 performance issue.\n\
24012 Warning: This option must be enabled before gdb reads the file."),
24013 NULL,
24014 NULL,
24015 &setlist, &showlist);
24016
24017 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24018 &dwarf2_locexpr_funcs);
24019 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24020 &dwarf2_loclist_funcs);
24021
24022 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24023 &dwarf2_block_frame_base_locexpr_funcs);
24024 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24025 &dwarf2_block_frame_base_loclist_funcs);
24026
24027 #if GDB_SELF_TEST
24028 selftests::register_test ("dw2_expand_symtabs_matching",
24029 selftests::dw2_expand_symtabs_matching::run_test);
24030 selftests::register_test ("dwarf2_find_containing_comp_unit",
24031 selftests::find_containing_comp_unit::run_test);
24032 #endif
24033 }