Make mapped_debug_names independent of objfile
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "dwarf2/read.h"
33 #include "dwarf2/abbrev.h"
34 #include "dwarf2/attribute.h"
35 #include "dwarf2/comp-unit.h"
36 #include "dwarf2/index-cache.h"
37 #include "dwarf2/index-common.h"
38 #include "dwarf2/leb.h"
39 #include "dwarf2/line-header.h"
40 #include "dwarf2/dwz.h"
41 #include "dwarf2/macro.h"
42 #include "dwarf2/die.h"
43 #include "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
168 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
169
170 /* Return whether the name at IDX in the symbol table should be
171 ignored. */
172 virtual bool symbol_name_slot_invalid (offset_type idx) const
173 {
174 return false;
175 }
176
177 /* Build the symbol name component sorted vector, if we haven't
178 yet. */
179 void build_name_components (dwarf2_per_objfile *per_objfile);
180
181 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
182 possible matches for LN_NO_PARAMS in the name component
183 vector. */
184 std::pair<std::vector<name_component>::const_iterator,
185 std::vector<name_component>::const_iterator>
186 find_name_components_bounds (const lookup_name_info &ln_no_params,
187 enum language lang,
188 dwarf2_per_objfile *per_objfile) const;
189
190 /* Prevent deleting/destroying via a base class pointer. */
191 protected:
192 ~mapped_index_base() = default;
193 };
194
195 /* A description of the mapped index. The file format is described in
196 a comment by the code that writes the index. */
197 struct mapped_index final : public mapped_index_base
198 {
199 /* A slot/bucket in the symbol table hash. */
200 struct symbol_table_slot
201 {
202 const offset_type name;
203 const offset_type vec;
204 };
205
206 /* Index data format version. */
207 int version = 0;
208
209 /* The address table data. */
210 gdb::array_view<const gdb_byte> address_table;
211
212 /* The symbol table, implemented as a hash table. */
213 gdb::array_view<symbol_table_slot> symbol_table;
214
215 /* A pointer to the constant pool. */
216 const char *constant_pool = nullptr;
217
218 bool symbol_name_slot_invalid (offset_type idx) const override
219 {
220 const auto &bucket = this->symbol_table[idx];
221 return bucket.name == 0 && bucket.vec == 0;
222 }
223
224 /* Convenience method to get at the name of the symbol at IDX in the
225 symbol table. */
226 const char *symbol_name_at
227 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
228 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
229
230 size_t symbol_name_count () const override
231 { return this->symbol_table.size (); }
232 };
233
234 /* A description of the mapped .debug_names.
235 Uninitialized map has CU_COUNT 0. */
236 struct mapped_debug_names final : public mapped_index_base
237 {
238 bfd_endian dwarf5_byte_order;
239 bool dwarf5_is_dwarf64;
240 bool augmentation_is_gdb;
241 uint8_t offset_size;
242 uint32_t cu_count = 0;
243 uint32_t tu_count, bucket_count, name_count;
244 const gdb_byte *cu_table_reordered, *tu_table_reordered;
245 const uint32_t *bucket_table_reordered, *hash_table_reordered;
246 const gdb_byte *name_table_string_offs_reordered;
247 const gdb_byte *name_table_entry_offs_reordered;
248 const gdb_byte *entry_pool;
249
250 struct index_val
251 {
252 ULONGEST dwarf_tag;
253 struct attr
254 {
255 /* Attribute name DW_IDX_*. */
256 ULONGEST dw_idx;
257
258 /* Attribute form DW_FORM_*. */
259 ULONGEST form;
260
261 /* Value if FORM is DW_FORM_implicit_const. */
262 LONGEST implicit_const;
263 };
264 std::vector<attr> attr_vec;
265 };
266
267 std::unordered_map<ULONGEST, index_val> abbrev_map;
268
269 const char *namei_to_name
270 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
271
272 /* Implementation of the mapped_index_base virtual interface, for
273 the name_components cache. */
274
275 const char *symbol_name_at
276 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
277 { return namei_to_name (idx, per_objfile); }
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
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
423 /* Return a type that is a generic pointer type, the size of which
424 matches the address size given in the compilation unit header for
425 this CU. */
426 struct type *addr_type () const;
427
428 /* Find an integer type the same size as the address size given in
429 the compilation unit header for this CU. UNSIGNED_P controls if
430 the integer is unsigned or not. */
431 struct type *addr_sized_int_type (bool unsigned_p) const;
432
433 /* The header of the compilation unit. */
434 struct comp_unit_head header {};
435
436 /* Base address of this compilation unit. */
437 gdb::optional<CORE_ADDR> base_address;
438
439 /* The language we are debugging. */
440 enum language language = language_unknown;
441 const struct language_defn *language_defn = nullptr;
442
443 const char *producer = nullptr;
444
445 private:
446 /* The symtab builder for this CU. This is only non-NULL when full
447 symbols are being read. */
448 std::unique_ptr<buildsym_compunit> m_builder;
449
450 public:
451 /* The generic symbol table building routines have separate lists for
452 file scope symbols and all all other scopes (local scopes). So
453 we need to select the right one to pass to add_symbol_to_list().
454 We do it by keeping a pointer to the correct list in list_in_scope.
455
456 FIXME: The original dwarf code just treated the file scope as the
457 first local scope, and all other local scopes as nested local
458 scopes, and worked fine. Check to see if we really need to
459 distinguish these in buildsym.c. */
460 struct pending **list_in_scope = nullptr;
461
462 /* Hash table holding all the loaded partial DIEs
463 with partial_die->offset.SECT_OFF as hash. */
464 htab_t partial_dies = nullptr;
465
466 /* Storage for things with the same lifetime as this read-in compilation
467 unit, including partial DIEs. */
468 auto_obstack comp_unit_obstack;
469
470 /* Backlink to our per_cu entry. */
471 struct dwarf2_per_cu_data *per_cu;
472
473 /* The dwarf2_per_objfile that owns this. */
474 struct dwarf2_per_objfile *per_objfile;
475
476 /* How many compilation units ago was this CU last referenced? */
477 int last_used = 0;
478
479 /* A hash table of DIE cu_offset for following references with
480 die_info->offset.sect_off as hash. */
481 htab_t die_hash = nullptr;
482
483 /* Full DIEs if read in. */
484 struct die_info *dies = nullptr;
485
486 /* A set of pointers to dwarf2_per_cu_data objects for compilation
487 units referenced by this one. Only set during full symbol processing;
488 partial symbol tables do not have dependencies. */
489 htab_t dependencies = nullptr;
490
491 /* Header data from the line table, during full symbol processing. */
492 struct line_header *line_header = nullptr;
493 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
494 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
495 this is the DW_TAG_compile_unit die for this CU. We'll hold on
496 to the line header as long as this DIE is being processed. See
497 process_die_scope. */
498 die_info *line_header_die_owner = nullptr;
499
500 /* A list of methods which need to have physnames computed
501 after all type information has been read. */
502 std::vector<delayed_method_info> method_list;
503
504 /* To be copied to symtab->call_site_htab. */
505 htab_t call_site_htab = nullptr;
506
507 /* Non-NULL if this CU came from a DWO file.
508 There is an invariant here that is important to remember:
509 Except for attributes copied from the top level DIE in the "main"
510 (or "stub") file in preparation for reading the DWO file
511 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
512 Either there isn't a DWO file (in which case this is NULL and the point
513 is moot), or there is and either we're not going to read it (in which
514 case this is NULL) or there is and we are reading it (in which case this
515 is non-NULL). */
516 struct dwo_unit *dwo_unit = nullptr;
517
518 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
519 Note this value comes from the Fission stub CU/TU's DIE. */
520 gdb::optional<ULONGEST> addr_base;
521
522 /* The DW_AT_rnglists_base attribute if present.
523 Note this value comes from the Fission stub CU/TU's DIE.
524 Also note that the value is zero in the non-DWO case so this value can
525 be used without needing to know whether DWO files are in use or not.
526 N.B. This does not apply to DW_AT_ranges appearing in
527 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
528 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
529 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
530 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
531 ULONGEST ranges_base = 0;
532
533 /* The DW_AT_loclists_base attribute if present. */
534 ULONGEST loclist_base = 0;
535
536 /* When reading debug info generated by older versions of rustc, we
537 have to rewrite some union types to be struct types with a
538 variant part. This rewriting must be done after the CU is fully
539 read in, because otherwise at the point of rewriting some struct
540 type might not have been fully processed. So, we keep a list of
541 all such types here and process them after expansion. */
542 std::vector<struct type *> rust_unions;
543
544 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
545 files, the value is implicitly zero. For DWARF 5 version DWO files, the
546 value is often implicit and is the size of the header of
547 .debug_str_offsets section (8 or 4, depending on the address size). */
548 gdb::optional<ULONGEST> str_offsets_base;
549
550 /* Mark used when releasing cached dies. */
551 bool mark : 1;
552
553 /* This CU references .debug_loc. See the symtab->locations_valid field.
554 This test is imperfect as there may exist optimized debug code not using
555 any location list and still facing inlining issues if handled as
556 unoptimized code. For a future better test see GCC PR other/32998. */
557 bool has_loclist : 1;
558
559 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
560 if all the producer_is_* fields are valid. This information is cached
561 because profiling CU expansion showed excessive time spent in
562 producer_is_gxx_lt_4_6. */
563 bool checked_producer : 1;
564 bool producer_is_gxx_lt_4_6 : 1;
565 bool producer_is_gcc_lt_4_3 : 1;
566 bool producer_is_icc : 1;
567 bool producer_is_icc_lt_14 : 1;
568 bool producer_is_codewarrior : 1;
569
570 /* When true, the file that we're processing is known to have
571 debugging info for C++ namespaces. GCC 3.3.x did not produce
572 this information, but later versions do. */
573
574 bool processing_has_namespace_info : 1;
575
576 struct partial_die_info *find_partial_die (sect_offset sect_off);
577
578 /* If this CU was inherited by another CU (via specification,
579 abstract_origin, etc), this is the ancestor CU. */
580 dwarf2_cu *ancestor;
581
582 /* Get the buildsym_compunit for this CU. */
583 buildsym_compunit *get_builder ()
584 {
585 /* If this CU has a builder associated with it, use that. */
586 if (m_builder != nullptr)
587 return m_builder.get ();
588
589 /* Otherwise, search ancestors for a valid builder. */
590 if (ancestor != nullptr)
591 return ancestor->get_builder ();
592
593 return nullptr;
594 }
595 };
596
597 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
598 This includes type_unit_group and quick_file_names. */
599
600 struct stmt_list_hash
601 {
602 /* The DWO unit this table is from or NULL if there is none. */
603 struct dwo_unit *dwo_unit;
604
605 /* Offset in .debug_line or .debug_line.dwo. */
606 sect_offset line_sect_off;
607 };
608
609 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
610 an object of this type. This contains elements of type unit groups
611 that can be shared across objfiles. The non-shareable parts are in
612 type_unit_group_unshareable. */
613
614 struct type_unit_group
615 {
616 /* dwarf2read.c's main "handle" on a TU symtab.
617 To simplify things we create an artificial CU that "includes" all the
618 type units using this stmt_list so that the rest of the code still has
619 a "per_cu" handle on the symtab. */
620 struct dwarf2_per_cu_data per_cu;
621
622 /* The TUs that share this DW_AT_stmt_list entry.
623 This is added to while parsing type units to build partial symtabs,
624 and is deleted afterwards and not used again. */
625 std::vector<signatured_type *> *tus;
626
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
629 };
630
631 /* These sections are what may appear in a (real or virtual) DWO file. */
632
633 struct dwo_sections
634 {
635 struct dwarf2_section_info abbrev;
636 struct dwarf2_section_info line;
637 struct dwarf2_section_info loc;
638 struct dwarf2_section_info loclists;
639 struct dwarf2_section_info macinfo;
640 struct dwarf2_section_info macro;
641 struct dwarf2_section_info str;
642 struct dwarf2_section_info str_offsets;
643 /* In the case of a virtual DWO file, these two are unused. */
644 struct dwarf2_section_info info;
645 std::vector<dwarf2_section_info> types;
646 };
647
648 /* CUs/TUs in DWP/DWO files. */
649
650 struct dwo_unit
651 {
652 /* Backlink to the containing struct dwo_file. */
653 struct dwo_file *dwo_file;
654
655 /* The "id" that distinguishes this CU/TU.
656 .debug_info calls this "dwo_id", .debug_types calls this "signature".
657 Since signatures came first, we stick with it for consistency. */
658 ULONGEST signature;
659
660 /* The section this CU/TU lives in, in the DWO file. */
661 struct dwarf2_section_info *section;
662
663 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
664 sect_offset sect_off;
665 unsigned int length;
666
667 /* For types, offset in the type's DIE of the type defined by this TU. */
668 cu_offset type_offset_in_tu;
669 };
670
671 /* include/dwarf2.h defines the DWP section codes.
672 It defines a max value but it doesn't define a min value, which we
673 use for error checking, so provide one. */
674
675 enum dwp_v2_section_ids
676 {
677 DW_SECT_MIN = 1
678 };
679
680 /* Data for one DWO file.
681
682 This includes virtual DWO files (a virtual DWO file is a DWO file as it
683 appears in a DWP file). DWP files don't really have DWO files per se -
684 comdat folding of types "loses" the DWO file they came from, and from
685 a high level view DWP files appear to contain a mass of random types.
686 However, to maintain consistency with the non-DWP case we pretend DWP
687 files contain virtual DWO files, and we assign each TU with one virtual
688 DWO file (generally based on the line and abbrev section offsets -
689 a heuristic that seems to work in practice). */
690
691 struct dwo_file
692 {
693 dwo_file () = default;
694 DISABLE_COPY_AND_ASSIGN (dwo_file);
695
696 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
697 For virtual DWO files the name is constructed from the section offsets
698 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
699 from related CU+TUs. */
700 const char *dwo_name = nullptr;
701
702 /* The DW_AT_comp_dir attribute. */
703 const char *comp_dir = nullptr;
704
705 /* The bfd, when the file is open. Otherwise this is NULL.
706 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
707 gdb_bfd_ref_ptr dbfd;
708
709 /* The sections that make up this DWO file.
710 Remember that for virtual DWO files in DWP V2, these are virtual
711 sections (for lack of a better name). */
712 struct dwo_sections sections {};
713
714 /* The CUs in the file.
715 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
716 an extension to handle LLVM's Link Time Optimization output (where
717 multiple source files may be compiled into a single object/dwo pair). */
718 htab_up cus;
719
720 /* Table of TUs in the file.
721 Each element is a struct dwo_unit. */
722 htab_up tus;
723 };
724
725 /* These sections are what may appear in a DWP file. */
726
727 struct dwp_sections
728 {
729 /* These are used by both DWP version 1 and 2. */
730 struct dwarf2_section_info str;
731 struct dwarf2_section_info cu_index;
732 struct dwarf2_section_info tu_index;
733
734 /* These are only used by DWP version 2 files.
735 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
736 sections are referenced by section number, and are not recorded here.
737 In DWP version 2 there is at most one copy of all these sections, each
738 section being (effectively) comprised of the concatenation of all of the
739 individual sections that exist in the version 1 format.
740 To keep the code simple we treat each of these concatenated pieces as a
741 section itself (a virtual section?). */
742 struct dwarf2_section_info abbrev;
743 struct dwarf2_section_info info;
744 struct dwarf2_section_info line;
745 struct dwarf2_section_info loc;
746 struct dwarf2_section_info macinfo;
747 struct dwarf2_section_info macro;
748 struct dwarf2_section_info str_offsets;
749 struct dwarf2_section_info types;
750 };
751
752 /* These sections are what may appear in a virtual DWO file in DWP version 1.
753 A virtual DWO file is a DWO file as it appears in a DWP file. */
754
755 struct virtual_v1_dwo_sections
756 {
757 struct dwarf2_section_info abbrev;
758 struct dwarf2_section_info line;
759 struct dwarf2_section_info loc;
760 struct dwarf2_section_info macinfo;
761 struct dwarf2_section_info macro;
762 struct dwarf2_section_info str_offsets;
763 /* Each DWP hash table entry records one CU or one TU.
764 That is recorded here, and copied to dwo_unit.section. */
765 struct dwarf2_section_info info_or_types;
766 };
767
768 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
769 In version 2, the sections of the DWO files are concatenated together
770 and stored in one section of that name. Thus each ELF section contains
771 several "virtual" sections. */
772
773 struct virtual_v2_dwo_sections
774 {
775 bfd_size_type abbrev_offset;
776 bfd_size_type abbrev_size;
777
778 bfd_size_type line_offset;
779 bfd_size_type line_size;
780
781 bfd_size_type loc_offset;
782 bfd_size_type loc_size;
783
784 bfd_size_type macinfo_offset;
785 bfd_size_type macinfo_size;
786
787 bfd_size_type macro_offset;
788 bfd_size_type macro_size;
789
790 bfd_size_type str_offsets_offset;
791 bfd_size_type str_offsets_size;
792
793 /* Each DWP hash table entry records one CU or one TU.
794 That is recorded here, and copied to dwo_unit.section. */
795 bfd_size_type info_or_types_offset;
796 bfd_size_type info_or_types_size;
797 };
798
799 /* Contents of DWP hash tables. */
800
801 struct dwp_hash_table
802 {
803 uint32_t version, nr_columns;
804 uint32_t nr_units, nr_slots;
805 const gdb_byte *hash_table, *unit_table;
806 union
807 {
808 struct
809 {
810 const gdb_byte *indices;
811 } v1;
812 struct
813 {
814 /* This is indexed by column number and gives the id of the section
815 in that column. */
816 #define MAX_NR_V2_DWO_SECTIONS \
817 (1 /* .debug_info or .debug_types */ \
818 + 1 /* .debug_abbrev */ \
819 + 1 /* .debug_line */ \
820 + 1 /* .debug_loc */ \
821 + 1 /* .debug_str_offsets */ \
822 + 1 /* .debug_macro or .debug_macinfo */)
823 int section_ids[MAX_NR_V2_DWO_SECTIONS];
824 const gdb_byte *offsets;
825 const gdb_byte *sizes;
826 } v2;
827 } section_pool;
828 };
829
830 /* Data for one DWP file. */
831
832 struct dwp_file
833 {
834 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
835 : name (name_),
836 dbfd (std::move (abfd))
837 {
838 }
839
840 /* Name of the file. */
841 const char *name;
842
843 /* File format version. */
844 int version = 0;
845
846 /* The bfd. */
847 gdb_bfd_ref_ptr dbfd;
848
849 /* Section info for this file. */
850 struct dwp_sections sections {};
851
852 /* Table of CUs in the file. */
853 const struct dwp_hash_table *cus = nullptr;
854
855 /* Table of TUs in the file. */
856 const struct dwp_hash_table *tus = nullptr;
857
858 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
859 htab_up loaded_cus;
860 htab_up loaded_tus;
861
862 /* Table to map ELF section numbers to their sections.
863 This is only needed for the DWP V1 file format. */
864 unsigned int num_sections = 0;
865 asection **elf_sections = nullptr;
866 };
867
868 /* Struct used to pass misc. parameters to read_die_and_children, et
869 al. which are used for both .debug_info and .debug_types dies.
870 All parameters here are unchanging for the life of the call. This
871 struct exists to abstract away the constant parameters of die reading. */
872
873 struct die_reader_specs
874 {
875 /* The bfd of die_section. */
876 bfd* abfd;
877
878 /* The CU of the DIE we are parsing. */
879 struct dwarf2_cu *cu;
880
881 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
882 struct dwo_file *dwo_file;
883
884 /* The section the die comes from.
885 This is either .debug_info or .debug_types, or the .dwo variants. */
886 struct dwarf2_section_info *die_section;
887
888 /* die_section->buffer. */
889 const gdb_byte *buffer;
890
891 /* The end of the buffer. */
892 const gdb_byte *buffer_end;
893
894 /* The abbreviation table to use when reading the DIEs. */
895 struct abbrev_table *abbrev_table;
896 };
897
898 /* A subclass of die_reader_specs that holds storage and has complex
899 constructor and destructor behavior. */
900
901 class cutu_reader : public die_reader_specs
902 {
903 public:
904
905 cutu_reader (dwarf2_per_cu_data *this_cu,
906 dwarf2_per_objfile *per_objfile,
907 struct abbrev_table *abbrev_table,
908 dwarf2_cu *existing_cu,
909 bool skip_partial);
910
911 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 dwarf2_per_objfile *per_objfile,
913 struct dwarf2_cu *parent_cu = nullptr,
914 struct dwo_file *dwo_file = nullptr);
915
916 DISABLE_COPY_AND_ASSIGN (cutu_reader);
917
918 const gdb_byte *info_ptr = nullptr;
919 struct die_info *comp_unit_die = nullptr;
920 bool dummy_p = false;
921
922 /* Release the new CU, putting it on the chain. This cannot be done
923 for dummy CUs. */
924 void keep ();
925
926 private:
927 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
928 dwarf2_per_objfile *per_objfile,
929 dwarf2_cu *existing_cu);
930
931 struct dwarf2_per_cu_data *m_this_cu;
932 std::unique_ptr<dwarf2_cu> m_new_cu;
933
934 /* The ordinary abbreviation table. */
935 abbrev_table_up m_abbrev_table_holder;
936
937 /* The DWO abbreviation table. */
938 abbrev_table_up m_dwo_abbrev_table;
939 };
940
941 /* When we construct a partial symbol table entry we only
942 need this much information. */
943 struct partial_die_info : public allocate_on_obstack
944 {
945 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
946
947 /* Disable assign but still keep copy ctor, which is needed
948 load_partial_dies. */
949 partial_die_info& operator=(const partial_die_info& rhs) = delete;
950
951 /* Adjust the partial die before generating a symbol for it. This
952 function may set the is_external flag or change the DIE's
953 name. */
954 void fixup (struct dwarf2_cu *cu);
955
956 /* Read a minimal amount of information into the minimal die
957 structure. */
958 const gdb_byte *read (const struct die_reader_specs *reader,
959 const struct abbrev_info &abbrev,
960 const gdb_byte *info_ptr);
961
962 /* Offset of this DIE. */
963 const sect_offset sect_off;
964
965 /* DWARF-2 tag for this DIE. */
966 const ENUM_BITFIELD(dwarf_tag) tag : 16;
967
968 /* Assorted flags describing the data found in this DIE. */
969 const unsigned int has_children : 1;
970
971 unsigned int is_external : 1;
972 unsigned int is_declaration : 1;
973 unsigned int has_type : 1;
974 unsigned int has_specification : 1;
975 unsigned int has_pc_info : 1;
976 unsigned int may_be_inlined : 1;
977
978 /* This DIE has been marked DW_AT_main_subprogram. */
979 unsigned int main_subprogram : 1;
980
981 /* Flag set if the SCOPE field of this structure has been
982 computed. */
983 unsigned int scope_set : 1;
984
985 /* Flag set if the DIE has a byte_size attribute. */
986 unsigned int has_byte_size : 1;
987
988 /* Flag set if the DIE has a DW_AT_const_value attribute. */
989 unsigned int has_const_value : 1;
990
991 /* Flag set if any of the DIE's children are template arguments. */
992 unsigned int has_template_arguments : 1;
993
994 /* Flag set if fixup has been called on this die. */
995 unsigned int fixup_called : 1;
996
997 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
998 unsigned int is_dwz : 1;
999
1000 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1001 unsigned int spec_is_dwz : 1;
1002
1003 /* The name of this DIE. Normally the value of DW_AT_name, but
1004 sometimes a default name for unnamed DIEs. */
1005 const char *name = nullptr;
1006
1007 /* The linkage name, if present. */
1008 const char *linkage_name = nullptr;
1009
1010 /* The scope to prepend to our children. This is generally
1011 allocated on the comp_unit_obstack, so will disappear
1012 when this compilation unit leaves the cache. */
1013 const char *scope = nullptr;
1014
1015 /* Some data associated with the partial DIE. The tag determines
1016 which field is live. */
1017 union
1018 {
1019 /* The location description associated with this DIE, if any. */
1020 struct dwarf_block *locdesc;
1021 /* The offset of an import, for DW_TAG_imported_unit. */
1022 sect_offset sect_off;
1023 } d {};
1024
1025 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1026 CORE_ADDR lowpc = 0;
1027 CORE_ADDR highpc = 0;
1028
1029 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1030 DW_AT_sibling, if any. */
1031 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1032 could return DW_AT_sibling values to its caller load_partial_dies. */
1033 const gdb_byte *sibling = nullptr;
1034
1035 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1036 DW_AT_specification (or DW_AT_abstract_origin or
1037 DW_AT_extension). */
1038 sect_offset spec_offset {};
1039
1040 /* Pointers to this DIE's parent, first child, and next sibling,
1041 if any. */
1042 struct partial_die_info *die_parent = nullptr;
1043 struct partial_die_info *die_child = nullptr;
1044 struct partial_die_info *die_sibling = nullptr;
1045
1046 friend struct partial_die_info *
1047 dwarf2_cu::find_partial_die (sect_offset sect_off);
1048
1049 private:
1050 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1051 partial_die_info (sect_offset sect_off)
1052 : partial_die_info (sect_off, DW_TAG_padding, 0)
1053 {
1054 }
1055
1056 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1057 int has_children_)
1058 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1059 {
1060 is_external = 0;
1061 is_declaration = 0;
1062 has_type = 0;
1063 has_specification = 0;
1064 has_pc_info = 0;
1065 may_be_inlined = 0;
1066 main_subprogram = 0;
1067 scope_set = 0;
1068 has_byte_size = 0;
1069 has_const_value = 0;
1070 has_template_arguments = 0;
1071 fixup_called = 0;
1072 is_dwz = 0;
1073 spec_is_dwz = 0;
1074 }
1075 };
1076
1077 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1078 but this would require a corresponding change in unpack_field_as_long
1079 and friends. */
1080 static int bits_per_byte = 8;
1081
1082 struct variant_part_builder;
1083
1084 /* When reading a variant, we track a bit more information about the
1085 field, and store it in an object of this type. */
1086
1087 struct variant_field
1088 {
1089 int first_field = -1;
1090 int last_field = -1;
1091
1092 /* A variant can contain other variant parts. */
1093 std::vector<variant_part_builder> variant_parts;
1094
1095 /* If we see a DW_TAG_variant, then this will be set if this is the
1096 default branch. */
1097 bool default_branch = false;
1098 /* If we see a DW_AT_discr_value, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value = 0;
1101 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1102 data. */
1103 struct dwarf_block *discr_list_data = nullptr;
1104 };
1105
1106 /* This represents a DW_TAG_variant_part. */
1107
1108 struct variant_part_builder
1109 {
1110 /* The offset of the discriminant field. */
1111 sect_offset discriminant_offset {};
1112
1113 /* Variants that are direct children of this variant part. */
1114 std::vector<variant_field> variants;
1115
1116 /* True if we're currently reading a variant. */
1117 bool processing_variant = false;
1118 };
1119
1120 struct nextfield
1121 {
1122 int accessibility = 0;
1123 int virtuality = 0;
1124 /* Variant parts need to find the discriminant, which is a DIE
1125 reference. We track the section offset of each field to make
1126 this link. */
1127 sect_offset offset;
1128 struct field field {};
1129 };
1130
1131 struct fnfieldlist
1132 {
1133 const char *name = nullptr;
1134 std::vector<struct fn_field> fnfields;
1135 };
1136
1137 /* The routines that read and process dies for a C struct or C++ class
1138 pass lists of data member fields and lists of member function fields
1139 in an instance of a field_info structure, as defined below. */
1140 struct field_info
1141 {
1142 /* List of data member and baseclasses fields. */
1143 std::vector<struct nextfield> fields;
1144 std::vector<struct nextfield> baseclasses;
1145
1146 /* Set if the accessibility of one of the fields is not public. */
1147 int non_public_fields = 0;
1148
1149 /* Member function fieldlist array, contains name of possibly overloaded
1150 member function, number of overloaded member functions and a pointer
1151 to the head of the member function field chain. */
1152 std::vector<struct fnfieldlist> fnfieldlists;
1153
1154 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1155 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1156 std::vector<struct decl_field> typedef_field_list;
1157
1158 /* Nested types defined by this class and the number of elements in this
1159 list. */
1160 std::vector<struct decl_field> nested_types_list;
1161
1162 /* If non-null, this is the variant part we are currently
1163 reading. */
1164 variant_part_builder *current_variant_part = nullptr;
1165 /* This holds all the top-level variant parts attached to the type
1166 we're reading. */
1167 std::vector<variant_part_builder> variant_parts;
1168
1169 /* Return the total number of fields (including baseclasses). */
1170 int nfields () const
1171 {
1172 return fields.size () + baseclasses.size ();
1173 }
1174 };
1175
1176 /* Loaded secondary compilation units are kept in memory until they
1177 have not been referenced for the processing of this many
1178 compilation units. Set this to zero to disable caching. Cache
1179 sizes of up to at least twenty will improve startup time for
1180 typical inter-CU-reference binaries, at an obvious memory cost. */
1181 static int dwarf_max_cache_age = 5;
1182 static void
1183 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1184 struct cmd_list_element *c, const char *value)
1185 {
1186 fprintf_filtered (file, _("The upper bound on the age of cached "
1187 "DWARF compilation units is %s.\n"),
1188 value);
1189 }
1190 \f
1191 /* local function prototypes */
1192
1193 static void dwarf2_find_base_address (struct die_info *die,
1194 struct dwarf2_cu *cu);
1195
1196 static dwarf2_psymtab *create_partial_symtab
1197 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1198 const char *name);
1199
1200 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1201 const gdb_byte *info_ptr,
1202 struct die_info *type_unit_die);
1203
1204 static void dwarf2_build_psymtabs_hard
1205 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1206
1207 static void scan_partial_symbols (struct partial_die_info *,
1208 CORE_ADDR *, CORE_ADDR *,
1209 int, struct dwarf2_cu *);
1210
1211 static void add_partial_symbol (struct partial_die_info *,
1212 struct dwarf2_cu *);
1213
1214 static void add_partial_namespace (struct partial_die_info *pdi,
1215 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1216 int set_addrmap, struct dwarf2_cu *cu);
1217
1218 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1219 CORE_ADDR *highpc, int set_addrmap,
1220 struct dwarf2_cu *cu);
1221
1222 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1223 struct dwarf2_cu *cu);
1224
1225 static void add_partial_subprogram (struct partial_die_info *pdi,
1226 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1227 int need_pc, struct dwarf2_cu *cu);
1228
1229 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1230
1231 static struct partial_die_info *load_partial_dies
1232 (const struct die_reader_specs *, const gdb_byte *, int);
1233
1234 /* A pair of partial_die_info and compilation unit. */
1235 struct cu_partial_die_info
1236 {
1237 /* The compilation unit of the partial_die_info. */
1238 struct dwarf2_cu *cu;
1239 /* A partial_die_info. */
1240 struct partial_die_info *pdi;
1241
1242 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1243 : cu (cu),
1244 pdi (pdi)
1245 { /* Nothing. */ }
1246
1247 private:
1248 cu_partial_die_info () = delete;
1249 };
1250
1251 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1252 struct dwarf2_cu *);
1253
1254 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1255 struct attribute *, struct attr_abbrev *,
1256 const gdb_byte *, bool *need_reprocess);
1257
1258 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1259 struct attribute *attr);
1260
1261 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1262
1263 static sect_offset read_abbrev_offset
1264 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1265 struct dwarf2_section_info *, sect_offset);
1266
1267 static const char *read_indirect_string
1268 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1269 const struct comp_unit_head *, unsigned int *);
1270
1271 static const char *read_indirect_string_at_offset
1272 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
1273
1274 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1275 const gdb_byte *,
1276 unsigned int *);
1277
1278 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1279 ULONGEST str_index);
1280
1281 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1282 ULONGEST str_index);
1283
1284 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1285
1286 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1287 struct dwarf2_cu *);
1288
1289 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1290 struct dwarf2_cu *cu);
1291
1292 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1293
1294 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1295 struct dwarf2_cu *cu);
1296
1297 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1298
1299 static struct die_info *die_specification (struct die_info *die,
1300 struct dwarf2_cu **);
1301
1302 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1303 struct dwarf2_cu *cu);
1304
1305 static void dwarf_decode_lines (struct line_header *, const char *,
1306 struct dwarf2_cu *, dwarf2_psymtab *,
1307 CORE_ADDR, int decode_mapping);
1308
1309 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1310 const char *);
1311
1312 static struct symbol *new_symbol (struct die_info *, struct type *,
1313 struct dwarf2_cu *, struct symbol * = NULL);
1314
1315 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1316 struct dwarf2_cu *);
1317
1318 static void dwarf2_const_value_attr (const struct attribute *attr,
1319 struct type *type,
1320 const char *name,
1321 struct obstack *obstack,
1322 struct dwarf2_cu *cu, LONGEST *value,
1323 const gdb_byte **bytes,
1324 struct dwarf2_locexpr_baton **baton);
1325
1326 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1327
1328 static int need_gnat_info (struct dwarf2_cu *);
1329
1330 static struct type *die_descriptive_type (struct die_info *,
1331 struct dwarf2_cu *);
1332
1333 static void set_descriptive_type (struct type *, struct die_info *,
1334 struct dwarf2_cu *);
1335
1336 static struct type *die_containing_type (struct die_info *,
1337 struct dwarf2_cu *);
1338
1339 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1340 struct dwarf2_cu *);
1341
1342 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1343
1344 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1345
1346 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1347
1348 static char *typename_concat (struct obstack *obs, const char *prefix,
1349 const char *suffix, int physname,
1350 struct dwarf2_cu *cu);
1351
1352 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1353
1354 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1355
1356 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1357
1358 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1359
1360 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1361
1362 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1363
1364 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1365 struct dwarf2_cu *, dwarf2_psymtab *);
1366
1367 /* Return the .debug_loclists section to use for cu. */
1368 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1369
1370 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1371 values. Keep the items ordered with increasing constraints compliance. */
1372 enum pc_bounds_kind
1373 {
1374 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1375 PC_BOUNDS_NOT_PRESENT,
1376
1377 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1378 were present but they do not form a valid range of PC addresses. */
1379 PC_BOUNDS_INVALID,
1380
1381 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1382 PC_BOUNDS_RANGES,
1383
1384 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1385 PC_BOUNDS_HIGH_LOW,
1386 };
1387
1388 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1389 CORE_ADDR *, CORE_ADDR *,
1390 struct dwarf2_cu *,
1391 dwarf2_psymtab *);
1392
1393 static void get_scope_pc_bounds (struct die_info *,
1394 CORE_ADDR *, CORE_ADDR *,
1395 struct dwarf2_cu *);
1396
1397 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1398 CORE_ADDR, struct dwarf2_cu *);
1399
1400 static void dwarf2_add_field (struct field_info *, struct die_info *,
1401 struct dwarf2_cu *);
1402
1403 static void dwarf2_attach_fields_to_type (struct field_info *,
1404 struct type *, struct dwarf2_cu *);
1405
1406 static void dwarf2_add_member_fn (struct field_info *,
1407 struct die_info *, struct type *,
1408 struct dwarf2_cu *);
1409
1410 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1411 struct type *,
1412 struct dwarf2_cu *);
1413
1414 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1415
1416 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1417
1418 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1419
1420 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1421
1422 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1423
1424 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1425
1426 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1427
1428 static struct type *read_module_type (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
1431 static const char *namespace_name (struct die_info *die,
1432 int *is_anonymous, struct dwarf2_cu *);
1433
1434 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1435
1436 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1437 bool * = nullptr);
1438
1439 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1440 struct dwarf2_cu *);
1441
1442 static struct die_info *read_die_and_siblings_1
1443 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1444 struct die_info *);
1445
1446 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1447 const gdb_byte *info_ptr,
1448 const gdb_byte **new_info_ptr,
1449 struct die_info *parent);
1450
1451 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1452 struct die_info **, const gdb_byte *,
1453 int);
1454
1455 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1456 struct die_info **, const gdb_byte *);
1457
1458 static void process_die (struct die_info *, struct dwarf2_cu *);
1459
1460 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1461 struct objfile *);
1462
1463 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1464
1465 static const char *dwarf2_full_name (const char *name,
1466 struct die_info *die,
1467 struct dwarf2_cu *cu);
1468
1469 static const char *dwarf2_physname (const char *name, struct die_info *die,
1470 struct dwarf2_cu *cu);
1471
1472 static struct die_info *dwarf2_extension (struct die_info *die,
1473 struct dwarf2_cu **);
1474
1475 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1476
1477 static void dump_die_for_error (struct die_info *);
1478
1479 static void dump_die_1 (struct ui_file *, int level, int max_level,
1480 struct die_info *);
1481
1482 /*static*/ void dump_die (struct die_info *, int max_level);
1483
1484 static void store_in_ref_table (struct die_info *,
1485 struct dwarf2_cu *);
1486
1487 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1488 const struct attribute *,
1489 struct dwarf2_cu **);
1490
1491 static struct die_info *follow_die_ref (struct die_info *,
1492 const struct attribute *,
1493 struct dwarf2_cu **);
1494
1495 static struct die_info *follow_die_sig (struct die_info *,
1496 const struct attribute *,
1497 struct dwarf2_cu **);
1498
1499 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1500 struct dwarf2_cu *);
1501
1502 static struct type *get_DW_AT_signature_type (struct die_info *,
1503 const struct attribute *,
1504 struct dwarf2_cu *);
1505
1506 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1507 dwarf2_per_objfile *per_objfile);
1508
1509 static void read_signatured_type (signatured_type *sig_type,
1510 dwarf2_per_objfile *per_objfile);
1511
1512 static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
1514 struct dynamic_prop *prop, struct type *type);
1515
1516 /* memory allocation interface */
1517
1518 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1519
1520 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1521
1522 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1523
1524 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
1526 const struct attribute *attr);
1527
1528 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1529 struct symbol *sym,
1530 struct dwarf2_cu *cu,
1531 int is_block);
1532
1533 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
1536
1537 static hashval_t partial_die_hash (const void *item);
1538
1539 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
1541 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
1544
1545 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
1548
1549 static struct type *set_die_type (struct die_info *, struct type *,
1550 struct dwarf2_cu *);
1551
1552 static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1553
1554 static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1555
1556 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1557 dwarf2_per_objfile *per_objfile,
1558 bool skip_partial,
1559 enum language pretend_language);
1560
1561 static void process_full_comp_unit (dwarf2_cu *cu,
1562 enum language pretend_language);
1563
1564 static void process_full_type_unit (dwarf2_cu *cu,
1565 enum language pretend_language);
1566
1567 static void dwarf2_add_dependence (struct dwarf2_cu *,
1568 struct dwarf2_per_cu_data *);
1569
1570 static void dwarf2_mark (struct dwarf2_cu *);
1571
1572 static struct type *get_die_type_at_offset (sect_offset,
1573 dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile);
1575
1576 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1577
1578 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1579 dwarf2_per_objfile *per_objfile,
1580 enum language pretend_language);
1581
1582 static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
1583
1584 /* Class, the destructor of which frees all allocated queue entries. This
1585 will only have work to do if an error was thrown while processing the
1586 dwarf. If no error was thrown then the queue entries should have all
1587 been processed, and freed, as we went along. */
1588
1589 class dwarf2_queue_guard
1590 {
1591 public:
1592 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1593 : m_per_objfile (per_objfile)
1594 {
1595 }
1596
1597 /* Free any entries remaining on the queue. There should only be
1598 entries left if we hit an error while processing the dwarf. */
1599 ~dwarf2_queue_guard ()
1600 {
1601 /* Ensure that no memory is allocated by the queue. */
1602 std::queue<dwarf2_queue_item> empty;
1603 std::swap (m_per_objfile->per_bfd->queue, empty);
1604 }
1605
1606 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1607
1608 private:
1609 dwarf2_per_objfile *m_per_objfile;
1610 };
1611
1612 dwarf2_queue_item::~dwarf2_queue_item ()
1613 {
1614 /* Anything still marked queued is likely to be in an
1615 inconsistent state, so discard it. */
1616 if (per_cu->queued)
1617 {
1618 per_objfile->remove_cu (per_cu);
1619 per_cu->queued = 0;
1620 }
1621 }
1622
1623 /* The return type of find_file_and_directory. Note, the enclosed
1624 string pointers are only valid while this object is valid. */
1625
1626 struct file_and_directory
1627 {
1628 /* The filename. This is never NULL. */
1629 const char *name;
1630
1631 /* The compilation directory. NULL if not known. If we needed to
1632 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1633 points directly to the DW_AT_comp_dir string attribute owned by
1634 the obstack that owns the DIE. */
1635 const char *comp_dir;
1636
1637 /* If we needed to build a new string for comp_dir, this is what
1638 owns the storage. */
1639 std::string comp_dir_storage;
1640 };
1641
1642 static file_and_directory find_file_and_directory (struct die_info *die,
1643 struct dwarf2_cu *cu);
1644
1645 static htab_up allocate_signatured_type_table ();
1646
1647 static htab_up allocate_dwo_unit_table ();
1648
1649 static struct dwo_unit *lookup_dwo_unit_in_dwp
1650 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1651 struct dwp_file *dwp_file, const char *comp_dir,
1652 ULONGEST signature, int is_debug_types);
1653
1654 static struct dwp_file *get_dwp_file
1655 (struct dwarf2_per_objfile *dwarf2_per_objfile);
1656
1657 static struct dwo_unit *lookup_dwo_comp_unit
1658 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1659 ULONGEST signature);
1660
1661 static struct dwo_unit *lookup_dwo_type_unit
1662 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1663
1664 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1665
1666 /* A unique pointer to a dwo_file. */
1667
1668 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1669
1670 static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
1671
1672 static void check_producer (struct dwarf2_cu *cu);
1673
1674 static void free_line_header_voidp (void *arg);
1675 \f
1676 /* Various complaints about symbol reading that don't abort the process. */
1677
1678 static void
1679 dwarf2_debug_line_missing_file_complaint (void)
1680 {
1681 complaint (_(".debug_line section has line data without a file"));
1682 }
1683
1684 static void
1685 dwarf2_debug_line_missing_end_sequence_complaint (void)
1686 {
1687 complaint (_(".debug_line section has line "
1688 "program sequence without an end"));
1689 }
1690
1691 static void
1692 dwarf2_complex_location_expr_complaint (void)
1693 {
1694 complaint (_("location expression too complex"));
1695 }
1696
1697 static void
1698 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1699 int arg3)
1700 {
1701 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1702 arg1, arg2, arg3);
1703 }
1704
1705 static void
1706 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1707 {
1708 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1709 arg1, arg2);
1710 }
1711
1712 /* Hash function for line_header_hash. */
1713
1714 static hashval_t
1715 line_header_hash (const struct line_header *ofs)
1716 {
1717 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1718 }
1719
1720 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1721
1722 static hashval_t
1723 line_header_hash_voidp (const void *item)
1724 {
1725 const struct line_header *ofs = (const struct line_header *) item;
1726
1727 return line_header_hash (ofs);
1728 }
1729
1730 /* Equality function for line_header_hash. */
1731
1732 static int
1733 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1734 {
1735 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1736 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1737
1738 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1739 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1740 }
1741
1742 \f
1743
1744 /* See declaration. */
1745
1746 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1747 bool can_copy_)
1748 : obfd (obfd),
1749 can_copy (can_copy_)
1750 {
1751 if (names == NULL)
1752 names = &dwarf2_elf_names;
1753
1754 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1755 locate_sections (obfd, sec, *names);
1756 }
1757
1758 dwarf2_per_bfd::~dwarf2_per_bfd ()
1759 {
1760 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1761 per_cu->imported_symtabs_free ();
1762
1763 for (signatured_type *sig_type : all_type_units)
1764 sig_type->per_cu.imported_symtabs_free ();
1765
1766 /* Everything else should be on this->obstack. */
1767 }
1768
1769 /* See read.h. */
1770
1771 void
1772 dwarf2_per_objfile::remove_all_cus ()
1773 {
1774 for (auto pair : m_dwarf2_cus)
1775 delete pair.second;
1776
1777 m_dwarf2_cus.clear ();
1778 }
1779
1780 /* A helper class that calls free_cached_comp_units on
1781 destruction. */
1782
1783 class free_cached_comp_units
1784 {
1785 public:
1786
1787 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1788 : m_per_objfile (per_objfile)
1789 {
1790 }
1791
1792 ~free_cached_comp_units ()
1793 {
1794 m_per_objfile->remove_all_cus ();
1795 }
1796
1797 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1798
1799 private:
1800
1801 dwarf2_per_objfile *m_per_objfile;
1802 };
1803
1804 /* See read.h. */
1805
1806 bool
1807 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1808 {
1809 gdb_assert (per_cu->index < this->m_symtabs.size ());
1810
1811 return this->m_symtabs[per_cu->index] != nullptr;
1812 }
1813
1814 /* See read.h. */
1815
1816 compunit_symtab *
1817 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1818 {
1819 gdb_assert (per_cu->index < this->m_symtabs.size ());
1820
1821 return this->m_symtabs[per_cu->index];
1822 }
1823
1824 /* See read.h. */
1825
1826 void
1827 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1828 compunit_symtab *symtab)
1829 {
1830 gdb_assert (per_cu->index < this->m_symtabs.size ());
1831 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1832
1833 this->m_symtabs[per_cu->index] = symtab;
1834 }
1835
1836 /* Try to locate the sections we need for DWARF 2 debugging
1837 information and return true if we have enough to do something.
1838 NAMES points to the dwarf2 section names, or is NULL if the standard
1839 ELF names are used. CAN_COPY is true for formats where symbol
1840 interposition is possible and so symbol values must follow copy
1841 relocation rules. */
1842
1843 int
1844 dwarf2_has_info (struct objfile *objfile,
1845 const struct dwarf2_debug_sections *names,
1846 bool can_copy)
1847 {
1848 if (objfile->flags & OBJF_READNEVER)
1849 return 0;
1850
1851 struct dwarf2_per_objfile *dwarf2_per_objfile
1852 = get_dwarf2_per_objfile (objfile);
1853
1854 if (dwarf2_per_objfile == NULL)
1855 {
1856 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1857 sharing yet). */
1858 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1859
1860 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1861 }
1862
1863 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1864 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1865 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1866 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
1867 }
1868
1869 /* When loading sections, we look either for uncompressed section or for
1870 compressed section names. */
1871
1872 static int
1873 section_is_p (const char *section_name,
1874 const struct dwarf2_section_names *names)
1875 {
1876 if (names->normal != NULL
1877 && strcmp (section_name, names->normal) == 0)
1878 return 1;
1879 if (names->compressed != NULL
1880 && strcmp (section_name, names->compressed) == 0)
1881 return 1;
1882 return 0;
1883 }
1884
1885 /* See declaration. */
1886
1887 void
1888 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1889 const dwarf2_debug_sections &names)
1890 {
1891 flagword aflag = bfd_section_flags (sectp);
1892
1893 if ((aflag & SEC_HAS_CONTENTS) == 0)
1894 {
1895 }
1896 else if (elf_section_data (sectp)->this_hdr.sh_size
1897 > bfd_get_file_size (abfd))
1898 {
1899 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1900 warning (_("Discarding section %s which has a section size (%s"
1901 ") larger than the file size [in module %s]"),
1902 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1903 bfd_get_filename (abfd));
1904 }
1905 else if (section_is_p (sectp->name, &names.info))
1906 {
1907 this->info.s.section = sectp;
1908 this->info.size = bfd_section_size (sectp);
1909 }
1910 else if (section_is_p (sectp->name, &names.abbrev))
1911 {
1912 this->abbrev.s.section = sectp;
1913 this->abbrev.size = bfd_section_size (sectp);
1914 }
1915 else if (section_is_p (sectp->name, &names.line))
1916 {
1917 this->line.s.section = sectp;
1918 this->line.size = bfd_section_size (sectp);
1919 }
1920 else if (section_is_p (sectp->name, &names.loc))
1921 {
1922 this->loc.s.section = sectp;
1923 this->loc.size = bfd_section_size (sectp);
1924 }
1925 else if (section_is_p (sectp->name, &names.loclists))
1926 {
1927 this->loclists.s.section = sectp;
1928 this->loclists.size = bfd_section_size (sectp);
1929 }
1930 else if (section_is_p (sectp->name, &names.macinfo))
1931 {
1932 this->macinfo.s.section = sectp;
1933 this->macinfo.size = bfd_section_size (sectp);
1934 }
1935 else if (section_is_p (sectp->name, &names.macro))
1936 {
1937 this->macro.s.section = sectp;
1938 this->macro.size = bfd_section_size (sectp);
1939 }
1940 else if (section_is_p (sectp->name, &names.str))
1941 {
1942 this->str.s.section = sectp;
1943 this->str.size = bfd_section_size (sectp);
1944 }
1945 else if (section_is_p (sectp->name, &names.str_offsets))
1946 {
1947 this->str_offsets.s.section = sectp;
1948 this->str_offsets.size = bfd_section_size (sectp);
1949 }
1950 else if (section_is_p (sectp->name, &names.line_str))
1951 {
1952 this->line_str.s.section = sectp;
1953 this->line_str.size = bfd_section_size (sectp);
1954 }
1955 else if (section_is_p (sectp->name, &names.addr))
1956 {
1957 this->addr.s.section = sectp;
1958 this->addr.size = bfd_section_size (sectp);
1959 }
1960 else if (section_is_p (sectp->name, &names.frame))
1961 {
1962 this->frame.s.section = sectp;
1963 this->frame.size = bfd_section_size (sectp);
1964 }
1965 else if (section_is_p (sectp->name, &names.eh_frame))
1966 {
1967 this->eh_frame.s.section = sectp;
1968 this->eh_frame.size = bfd_section_size (sectp);
1969 }
1970 else if (section_is_p (sectp->name, &names.ranges))
1971 {
1972 this->ranges.s.section = sectp;
1973 this->ranges.size = bfd_section_size (sectp);
1974 }
1975 else if (section_is_p (sectp->name, &names.rnglists))
1976 {
1977 this->rnglists.s.section = sectp;
1978 this->rnglists.size = bfd_section_size (sectp);
1979 }
1980 else if (section_is_p (sectp->name, &names.types))
1981 {
1982 struct dwarf2_section_info type_section;
1983
1984 memset (&type_section, 0, sizeof (type_section));
1985 type_section.s.section = sectp;
1986 type_section.size = bfd_section_size (sectp);
1987
1988 this->types.push_back (type_section);
1989 }
1990 else if (section_is_p (sectp->name, &names.gdb_index))
1991 {
1992 this->gdb_index.s.section = sectp;
1993 this->gdb_index.size = bfd_section_size (sectp);
1994 }
1995 else if (section_is_p (sectp->name, &names.debug_names))
1996 {
1997 this->debug_names.s.section = sectp;
1998 this->debug_names.size = bfd_section_size (sectp);
1999 }
2000 else if (section_is_p (sectp->name, &names.debug_aranges))
2001 {
2002 this->debug_aranges.s.section = sectp;
2003 this->debug_aranges.size = bfd_section_size (sectp);
2004 }
2005
2006 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2007 && bfd_section_vma (sectp) == 0)
2008 this->has_section_at_zero = true;
2009 }
2010
2011 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2012 SECTION_NAME. */
2013
2014 void
2015 dwarf2_get_section_info (struct objfile *objfile,
2016 enum dwarf2_section_enum sect,
2017 asection **sectp, const gdb_byte **bufp,
2018 bfd_size_type *sizep)
2019 {
2020 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
2021 struct dwarf2_section_info *info;
2022
2023 /* We may see an objfile without any DWARF, in which case we just
2024 return nothing. */
2025 if (data == NULL)
2026 {
2027 *sectp = NULL;
2028 *bufp = NULL;
2029 *sizep = 0;
2030 return;
2031 }
2032 switch (sect)
2033 {
2034 case DWARF2_DEBUG_FRAME:
2035 info = &data->per_bfd->frame;
2036 break;
2037 case DWARF2_EH_FRAME:
2038 info = &data->per_bfd->eh_frame;
2039 break;
2040 default:
2041 gdb_assert_not_reached ("unexpected section");
2042 }
2043
2044 info->read (objfile);
2045
2046 *sectp = info->get_bfd_section ();
2047 *bufp = info->buffer;
2048 *sizep = info->size;
2049 }
2050
2051 /* A helper function to find the sections for a .dwz file. */
2052
2053 static void
2054 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2055 {
2056 struct dwz_file *dwz_file = (struct dwz_file *) arg;
2057
2058 /* Note that we only support the standard ELF names, because .dwz
2059 is ELF-only (at the time of writing). */
2060 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2061 {
2062 dwz_file->abbrev.s.section = sectp;
2063 dwz_file->abbrev.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2066 {
2067 dwz_file->info.s.section = sectp;
2068 dwz_file->info.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2071 {
2072 dwz_file->str.s.section = sectp;
2073 dwz_file->str.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2076 {
2077 dwz_file->line.s.section = sectp;
2078 dwz_file->line.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2081 {
2082 dwz_file->macro.s.section = sectp;
2083 dwz_file->macro.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2086 {
2087 dwz_file->gdb_index.s.section = sectp;
2088 dwz_file->gdb_index.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2091 {
2092 dwz_file->debug_names.s.section = sectp;
2093 dwz_file->debug_names.size = bfd_section_size (sectp);
2094 }
2095 }
2096
2097 /* See dwarf2read.h. */
2098
2099 struct dwz_file *
2100 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2101 {
2102 const char *filename;
2103 bfd_size_type buildid_len_arg;
2104 size_t buildid_len;
2105 bfd_byte *buildid;
2106
2107 if (per_bfd->dwz_file != NULL)
2108 return per_bfd->dwz_file.get ();
2109
2110 bfd_set_error (bfd_error_no_error);
2111 gdb::unique_xmalloc_ptr<char> data
2112 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2113 &buildid_len_arg, &buildid));
2114 if (data == NULL)
2115 {
2116 if (bfd_get_error () == bfd_error_no_error)
2117 return NULL;
2118 error (_("could not read '.gnu_debugaltlink' section: %s"),
2119 bfd_errmsg (bfd_get_error ()));
2120 }
2121
2122 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2123
2124 buildid_len = (size_t) buildid_len_arg;
2125
2126 filename = data.get ();
2127
2128 std::string abs_storage;
2129 if (!IS_ABSOLUTE_PATH (filename))
2130 {
2131 gdb::unique_xmalloc_ptr<char> abs
2132 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2133
2134 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
2135 filename = abs_storage.c_str ();
2136 }
2137
2138 /* First try the file name given in the section. If that doesn't
2139 work, try to use the build-id instead. */
2140 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
2141 if (dwz_bfd != NULL)
2142 {
2143 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2144 dwz_bfd.reset (nullptr);
2145 }
2146
2147 if (dwz_bfd == NULL)
2148 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2149
2150 if (dwz_bfd == nullptr)
2151 {
2152 gdb::unique_xmalloc_ptr<char> alt_filename;
2153 const char *origname = bfd_get_filename (per_bfd->obfd);
2154
2155 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2156 buildid_len,
2157 origname,
2158 &alt_filename));
2159
2160 if (fd.get () >= 0)
2161 {
2162 /* File successfully retrieved from server. */
2163 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2164
2165 if (dwz_bfd == nullptr)
2166 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2167 alt_filename.get ());
2168 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2169 dwz_bfd.reset (nullptr);
2170 }
2171 }
2172
2173 if (dwz_bfd == NULL)
2174 error (_("could not find '.gnu_debugaltlink' file for %s"),
2175 bfd_get_filename (per_bfd->obfd));
2176
2177 std::unique_ptr<struct dwz_file> result
2178 (new struct dwz_file (std::move (dwz_bfd)));
2179
2180 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2181 result.get ());
2182
2183 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2184 per_bfd->dwz_file = std::move (result);
2185 return per_bfd->dwz_file.get ();
2186 }
2187 \f
2188 /* DWARF quick_symbols_functions support. */
2189
2190 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2191 unique line tables, so we maintain a separate table of all .debug_line
2192 derived entries to support the sharing.
2193 All the quick functions need is the list of file names. We discard the
2194 line_header when we're done and don't need to record it here. */
2195 struct quick_file_names
2196 {
2197 /* The data used to construct the hash key. */
2198 struct stmt_list_hash hash;
2199
2200 /* The number of entries in file_names, real_names. */
2201 unsigned int num_file_names;
2202
2203 /* The file names from the line table, after being run through
2204 file_full_name. */
2205 const char **file_names;
2206
2207 /* The file names from the line table after being run through
2208 gdb_realpath. These are computed lazily. */
2209 const char **real_names;
2210 };
2211
2212 /* When using the index (and thus not using psymtabs), each CU has an
2213 object of this type. This is used to hold information needed by
2214 the various "quick" methods. */
2215 struct dwarf2_per_cu_quick_data
2216 {
2217 /* The file table. This can be NULL if there was no file table
2218 or it's currently not read in.
2219 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2220 struct quick_file_names *file_names;
2221
2222 /* A temporary mark bit used when iterating over all CUs in
2223 expand_symtabs_matching. */
2224 unsigned int mark : 1;
2225
2226 /* True if we've tried to read the file table and found there isn't one.
2227 There will be no point in trying to read it again next time. */
2228 unsigned int no_file_data : 1;
2229 };
2230
2231 /* Utility hash function for a stmt_list_hash. */
2232
2233 static hashval_t
2234 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2235 {
2236 hashval_t v = 0;
2237
2238 if (stmt_list_hash->dwo_unit != NULL)
2239 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2240 v += to_underlying (stmt_list_hash->line_sect_off);
2241 return v;
2242 }
2243
2244 /* Utility equality function for a stmt_list_hash. */
2245
2246 static int
2247 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2248 const struct stmt_list_hash *rhs)
2249 {
2250 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2251 return 0;
2252 if (lhs->dwo_unit != NULL
2253 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2254 return 0;
2255
2256 return lhs->line_sect_off == rhs->line_sect_off;
2257 }
2258
2259 /* Hash function for a quick_file_names. */
2260
2261 static hashval_t
2262 hash_file_name_entry (const void *e)
2263 {
2264 const struct quick_file_names *file_data
2265 = (const struct quick_file_names *) e;
2266
2267 return hash_stmt_list_entry (&file_data->hash);
2268 }
2269
2270 /* Equality function for a quick_file_names. */
2271
2272 static int
2273 eq_file_name_entry (const void *a, const void *b)
2274 {
2275 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2276 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2277
2278 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2279 }
2280
2281 /* Delete function for a quick_file_names. */
2282
2283 static void
2284 delete_file_name_entry (void *e)
2285 {
2286 struct quick_file_names *file_data = (struct quick_file_names *) e;
2287 int i;
2288
2289 for (i = 0; i < file_data->num_file_names; ++i)
2290 {
2291 xfree ((void*) file_data->file_names[i]);
2292 if (file_data->real_names)
2293 xfree ((void*) file_data->real_names[i]);
2294 }
2295
2296 /* The space for the struct itself lives on the obstack, so we don't
2297 free it here. */
2298 }
2299
2300 /* Create a quick_file_names hash table. */
2301
2302 static htab_up
2303 create_quick_file_names_table (unsigned int nr_initial_entries)
2304 {
2305 return htab_up (htab_create_alloc (nr_initial_entries,
2306 hash_file_name_entry, eq_file_name_entry,
2307 delete_file_name_entry, xcalloc, xfree));
2308 }
2309
2310 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2311 function is unrelated to symtabs, symtab would have to be created afterwards.
2312 You should call age_cached_comp_units after processing the CU. */
2313
2314 static dwarf2_cu *
2315 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2316 bool skip_partial)
2317 {
2318 if (per_cu->is_debug_types)
2319 load_full_type_unit (per_cu, per_objfile);
2320 else
2321 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
2322
2323 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2324 if (cu == nullptr)
2325 return nullptr; /* Dummy CU. */
2326
2327 dwarf2_find_base_address (cu->dies, cu);
2328
2329 return cu;
2330 }
2331
2332 /* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2333
2334 static void
2335 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2336 dwarf2_per_objfile *dwarf2_per_objfile,
2337 bool skip_partial)
2338 {
2339 /* Skip type_unit_groups, reading the type units they contain
2340 is handled elsewhere. */
2341 if (per_cu->type_unit_group_p ())
2342 return;
2343
2344 /* The destructor of dwarf2_queue_guard frees any entries left on
2345 the queue. After this point we're guaranteed to leave this function
2346 with the dwarf queue empty. */
2347 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
2348
2349 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2350 {
2351 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
2352 dwarf2_cu *cu = load_cu (per_cu, dwarf2_per_objfile, skip_partial);
2353
2354 /* If we just loaded a CU from a DWO, and we're working with an index
2355 that may badly handle TUs, load all the TUs in that DWO as well.
2356 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2357 if (!per_cu->is_debug_types
2358 && cu != NULL
2359 && cu->dwo_unit != NULL
2360 && dwarf2_per_objfile->per_bfd->index_table != NULL
2361 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
2362 /* DWP files aren't supported yet. */
2363 && get_dwp_file (dwarf2_per_objfile) == NULL)
2364 queue_and_load_all_dwo_tus (cu);
2365 }
2366
2367 process_queue (dwarf2_per_objfile);
2368
2369 /* Age the cache, releasing compilation units that have not
2370 been used recently. */
2371 dwarf2_per_objfile->age_comp_units ();
2372 }
2373
2374 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2375 the per-objfile for which this symtab is instantiated.
2376
2377 Returns the resulting symbol table. */
2378
2379 static struct compunit_symtab *
2380 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2381 dwarf2_per_objfile *dwarf2_per_objfile,
2382 bool skip_partial)
2383 {
2384 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
2385
2386 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
2387 {
2388 free_cached_comp_units freer (dwarf2_per_objfile);
2389 scoped_restore decrementer = increment_reading_symtab ();
2390 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
2391 process_cu_includes (dwarf2_per_objfile);
2392 }
2393
2394 return dwarf2_per_objfile->get_symtab (per_cu);
2395 }
2396
2397 /* See declaration. */
2398
2399 dwarf2_per_cu_data *
2400 dwarf2_per_bfd::get_cutu (int index)
2401 {
2402 if (index >= this->all_comp_units.size ())
2403 {
2404 index -= this->all_comp_units.size ();
2405 gdb_assert (index < this->all_type_units.size ());
2406 return &this->all_type_units[index]->per_cu;
2407 }
2408
2409 return this->all_comp_units[index];
2410 }
2411
2412 /* See declaration. */
2413
2414 dwarf2_per_cu_data *
2415 dwarf2_per_bfd::get_cu (int index)
2416 {
2417 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2418
2419 return this->all_comp_units[index];
2420 }
2421
2422 /* See declaration. */
2423
2424 signatured_type *
2425 dwarf2_per_bfd::get_tu (int index)
2426 {
2427 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2428
2429 return this->all_type_units[index];
2430 }
2431
2432 /* See read.h. */
2433
2434 dwarf2_per_cu_data *
2435 dwarf2_per_bfd::allocate_per_cu ()
2436 {
2437 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2438 result->per_bfd = this;
2439 result->index = m_num_psymtabs++;
2440 return result;
2441 }
2442
2443 /* See read.h. */
2444
2445 signatured_type *
2446 dwarf2_per_bfd::allocate_signatured_type ()
2447 {
2448 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2449 result->per_cu.per_bfd = this;
2450 result->per_cu.index = m_num_psymtabs++;
2451 return result;
2452 }
2453
2454 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2455 obstack, and constructed with the specified field values. */
2456
2457 static dwarf2_per_cu_data *
2458 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2459 struct dwarf2_section_info *section,
2460 int is_dwz,
2461 sect_offset sect_off, ULONGEST length)
2462 {
2463 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2464 the_cu->sect_off = sect_off;
2465 the_cu->length = length;
2466 the_cu->section = section;
2467 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2468 struct dwarf2_per_cu_quick_data);
2469 the_cu->is_dwz = is_dwz;
2470 return the_cu;
2471 }
2472
2473 /* A helper for create_cus_from_index that handles a given list of
2474 CUs. */
2475
2476 static void
2477 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2478 const gdb_byte *cu_list, offset_type n_elements,
2479 struct dwarf2_section_info *section,
2480 int is_dwz)
2481 {
2482 for (offset_type i = 0; i < n_elements; i += 2)
2483 {
2484 gdb_static_assert (sizeof (ULONGEST) >= 8);
2485
2486 sect_offset sect_off
2487 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2488 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2489 cu_list += 2 * 8;
2490
2491 dwarf2_per_cu_data *per_cu
2492 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2493 length);
2494 per_bfd->all_comp_units.push_back (per_cu);
2495 }
2496 }
2497
2498 /* Read the CU list from the mapped index, and use it to create all
2499 the CU objects for PER_BFD. */
2500
2501 static void
2502 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2503 const gdb_byte *cu_list, offset_type cu_list_elements,
2504 const gdb_byte *dwz_list, offset_type dwz_elements)
2505 {
2506 gdb_assert (per_bfd->all_comp_units.empty ());
2507 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2508
2509 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2510 &per_bfd->info, 0);
2511
2512 if (dwz_elements == 0)
2513 return;
2514
2515 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2516 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2517 &dwz->info, 1);
2518 }
2519
2520 /* Create the signatured type hash table from the index. */
2521
2522 static void
2523 create_signatured_type_table_from_index
2524 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2525 const gdb_byte *bytes, offset_type elements)
2526 {
2527 gdb_assert (per_bfd->all_type_units.empty ());
2528 per_bfd->all_type_units.reserve (elements / 3);
2529
2530 htab_up sig_types_hash = allocate_signatured_type_table ();
2531
2532 for (offset_type i = 0; i < elements; i += 3)
2533 {
2534 struct signatured_type *sig_type;
2535 ULONGEST signature;
2536 void **slot;
2537 cu_offset type_offset_in_tu;
2538
2539 gdb_static_assert (sizeof (ULONGEST) >= 8);
2540 sect_offset sect_off
2541 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2542 type_offset_in_tu
2543 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2544 BFD_ENDIAN_LITTLE);
2545 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2546 bytes += 3 * 8;
2547
2548 sig_type = per_bfd->allocate_signatured_type ();
2549 sig_type->signature = signature;
2550 sig_type->type_offset_in_tu = type_offset_in_tu;
2551 sig_type->per_cu.is_debug_types = 1;
2552 sig_type->per_cu.section = section;
2553 sig_type->per_cu.sect_off = sect_off;
2554 sig_type->per_cu.v.quick
2555 = OBSTACK_ZALLOC (&per_bfd->obstack,
2556 struct dwarf2_per_cu_quick_data);
2557
2558 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2559 *slot = sig_type;
2560
2561 per_bfd->all_type_units.push_back (sig_type);
2562 }
2563
2564 per_bfd->signatured_types = std::move (sig_types_hash);
2565 }
2566
2567 /* Create the signatured type hash table from .debug_names. */
2568
2569 static void
2570 create_signatured_type_table_from_debug_names
2571 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2572 const mapped_debug_names &map,
2573 struct dwarf2_section_info *section,
2574 struct dwarf2_section_info *abbrev_section)
2575 {
2576 struct objfile *objfile = dwarf2_per_objfile->objfile;
2577
2578 section->read (objfile);
2579 abbrev_section->read (objfile);
2580
2581 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2582 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2583
2584 htab_up sig_types_hash = allocate_signatured_type_table ();
2585
2586 for (uint32_t i = 0; i < map.tu_count; ++i)
2587 {
2588 struct signatured_type *sig_type;
2589 void **slot;
2590
2591 sect_offset sect_off
2592 = (sect_offset) (extract_unsigned_integer
2593 (map.tu_table_reordered + i * map.offset_size,
2594 map.offset_size,
2595 map.dwarf5_byte_order));
2596
2597 comp_unit_head cu_header;
2598 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2599 abbrev_section,
2600 section->buffer + to_underlying (sect_off),
2601 rcuh_kind::TYPE);
2602
2603 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
2604 sig_type->signature = cu_header.signature;
2605 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2606 sig_type->per_cu.is_debug_types = 1;
2607 sig_type->per_cu.section = section;
2608 sig_type->per_cu.sect_off = sect_off;
2609 sig_type->per_cu.v.quick
2610 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
2611 struct dwarf2_per_cu_quick_data);
2612
2613 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2614 *slot = sig_type;
2615
2616 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
2617 }
2618
2619 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2620 }
2621
2622 /* Read the address map data from the mapped index, and use it to
2623 populate the objfile's psymtabs_addrmap. */
2624
2625 static void
2626 create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2627 struct mapped_index *index)
2628 {
2629 struct objfile *objfile = dwarf2_per_objfile->objfile;
2630 struct gdbarch *gdbarch = objfile->arch ();
2631 const gdb_byte *iter, *end;
2632 struct addrmap *mutable_map;
2633 CORE_ADDR baseaddr;
2634
2635 auto_obstack temp_obstack;
2636
2637 mutable_map = addrmap_create_mutable (&temp_obstack);
2638
2639 iter = index->address_table.data ();
2640 end = iter + index->address_table.size ();
2641
2642 baseaddr = objfile->text_section_offset ();
2643
2644 while (iter < end)
2645 {
2646 ULONGEST hi, lo, cu_index;
2647 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2648 iter += 8;
2649 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2650 iter += 8;
2651 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2652 iter += 4;
2653
2654 if (lo > hi)
2655 {
2656 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2657 hex_string (lo), hex_string (hi));
2658 continue;
2659 }
2660
2661 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
2662 {
2663 complaint (_(".gdb_index address table has invalid CU number %u"),
2664 (unsigned) cu_index);
2665 continue;
2666 }
2667
2668 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2669 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2670 addrmap_set_empty (mutable_map, lo, hi - 1,
2671 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
2672 }
2673
2674 objfile->partial_symtabs->psymtabs_addrmap
2675 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2676 }
2677
2678 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2679 populate the objfile's psymtabs_addrmap. */
2680
2681 static void
2682 create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
2683 struct dwarf2_section_info *section)
2684 {
2685 struct objfile *objfile = dwarf2_per_objfile->objfile;
2686 bfd *abfd = objfile->obfd;
2687 struct gdbarch *gdbarch = objfile->arch ();
2688 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2689
2690 auto_obstack temp_obstack;
2691 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2692
2693 std::unordered_map<sect_offset,
2694 dwarf2_per_cu_data *,
2695 gdb::hash_enum<sect_offset>>
2696 debug_info_offset_to_per_cu;
2697 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
2698 {
2699 const auto insertpair
2700 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2701 if (!insertpair.second)
2702 {
2703 warning (_("Section .debug_aranges in %s has duplicate "
2704 "debug_info_offset %s, ignoring .debug_aranges."),
2705 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2706 return;
2707 }
2708 }
2709
2710 section->read (objfile);
2711
2712 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2713
2714 const gdb_byte *addr = section->buffer;
2715
2716 while (addr < section->buffer + section->size)
2717 {
2718 const gdb_byte *const entry_addr = addr;
2719 unsigned int bytes_read;
2720
2721 const LONGEST entry_length = read_initial_length (abfd, addr,
2722 &bytes_read);
2723 addr += bytes_read;
2724
2725 const gdb_byte *const entry_end = addr + entry_length;
2726 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2727 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2728 if (addr + entry_length > section->buffer + section->size)
2729 {
2730 warning (_("Section .debug_aranges in %s entry at offset %s "
2731 "length %s exceeds section length %s, "
2732 "ignoring .debug_aranges."),
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
2735 plongest (bytes_read + entry_length),
2736 pulongest (section->size));
2737 return;
2738 }
2739
2740 /* The version number. */
2741 const uint16_t version = read_2_bytes (abfd, addr);
2742 addr += 2;
2743 if (version != 2)
2744 {
2745 warning (_("Section .debug_aranges in %s entry at offset %s "
2746 "has unsupported version %d, ignoring .debug_aranges."),
2747 objfile_name (objfile),
2748 plongest (entry_addr - section->buffer), version);
2749 return;
2750 }
2751
2752 const uint64_t debug_info_offset
2753 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2754 addr += offset_size;
2755 const auto per_cu_it
2756 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2757 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2758 {
2759 warning (_("Section .debug_aranges in %s entry at offset %s "
2760 "debug_info_offset %s does not exists, "
2761 "ignoring .debug_aranges."),
2762 objfile_name (objfile),
2763 plongest (entry_addr - section->buffer),
2764 pulongest (debug_info_offset));
2765 return;
2766 }
2767 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2768
2769 const uint8_t address_size = *addr++;
2770 if (address_size < 1 || address_size > 8)
2771 {
2772 warning (_("Section .debug_aranges in %s entry at offset %s "
2773 "address_size %u is invalid, ignoring .debug_aranges."),
2774 objfile_name (objfile),
2775 plongest (entry_addr - section->buffer), address_size);
2776 return;
2777 }
2778
2779 const uint8_t segment_selector_size = *addr++;
2780 if (segment_selector_size != 0)
2781 {
2782 warning (_("Section .debug_aranges in %s entry at offset %s "
2783 "segment_selector_size %u is not supported, "
2784 "ignoring .debug_aranges."),
2785 objfile_name (objfile),
2786 plongest (entry_addr - section->buffer),
2787 segment_selector_size);
2788 return;
2789 }
2790
2791 /* Must pad to an alignment boundary that is twice the address
2792 size. It is undocumented by the DWARF standard but GCC does
2793 use it. */
2794 for (size_t padding = ((-(addr - section->buffer))
2795 & (2 * address_size - 1));
2796 padding > 0; padding--)
2797 if (*addr++ != 0)
2798 {
2799 warning (_("Section .debug_aranges in %s entry at offset %s "
2800 "padding is not zero, ignoring .debug_aranges."),
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer));
2803 return;
2804 }
2805
2806 for (;;)
2807 {
2808 if (addr + 2 * address_size > entry_end)
2809 {
2810 warning (_("Section .debug_aranges in %s entry at offset %s "
2811 "address list is not properly terminated, "
2812 "ignoring .debug_aranges."),
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer));
2815 return;
2816 }
2817 ULONGEST start = extract_unsigned_integer (addr, address_size,
2818 dwarf5_byte_order);
2819 addr += address_size;
2820 ULONGEST length = extract_unsigned_integer (addr, address_size,
2821 dwarf5_byte_order);
2822 addr += address_size;
2823 if (start == 0 && length == 0)
2824 break;
2825 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
2826 {
2827 /* Symbol was eliminated due to a COMDAT group. */
2828 continue;
2829 }
2830 ULONGEST end = start + length;
2831 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2832 - baseaddr);
2833 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2834 - baseaddr);
2835 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2836 }
2837 }
2838
2839 objfile->partial_symtabs->psymtabs_addrmap
2840 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2841 }
2842
2843 /* Find a slot in the mapped index INDEX for the object named NAME.
2844 If NAME is found, set *VEC_OUT to point to the CU vector in the
2845 constant pool and return true. If NAME cannot be found, return
2846 false. */
2847
2848 static bool
2849 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2850 offset_type **vec_out)
2851 {
2852 offset_type hash;
2853 offset_type slot, step;
2854 int (*cmp) (const char *, const char *);
2855
2856 gdb::unique_xmalloc_ptr<char> without_params;
2857 if (current_language->la_language == language_cplus
2858 || current_language->la_language == language_fortran
2859 || current_language->la_language == language_d)
2860 {
2861 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2862 not contain any. */
2863
2864 if (strchr (name, '(') != NULL)
2865 {
2866 without_params = cp_remove_params (name);
2867
2868 if (without_params != NULL)
2869 name = without_params.get ();
2870 }
2871 }
2872
2873 /* Index version 4 did not support case insensitive searches. But the
2874 indices for case insensitive languages are built in lowercase, therefore
2875 simulate our NAME being searched is also lowercased. */
2876 hash = mapped_index_string_hash ((index->version == 4
2877 && case_sensitivity == case_sensitive_off
2878 ? 5 : index->version),
2879 name);
2880
2881 slot = hash & (index->symbol_table.size () - 1);
2882 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
2883 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2884
2885 for (;;)
2886 {
2887 const char *str;
2888
2889 const auto &bucket = index->symbol_table[slot];
2890 if (bucket.name == 0 && bucket.vec == 0)
2891 return false;
2892
2893 str = index->constant_pool + MAYBE_SWAP (bucket.name);
2894 if (!cmp (name, str))
2895 {
2896 *vec_out = (offset_type *) (index->constant_pool
2897 + MAYBE_SWAP (bucket.vec));
2898 return true;
2899 }
2900
2901 slot = (slot + step) & (index->symbol_table.size () - 1);
2902 }
2903 }
2904
2905 /* A helper function that reads the .gdb_index from BUFFER and fills
2906 in MAP. FILENAME is the name of the file containing the data;
2907 it is used for error reporting. DEPRECATED_OK is true if it is
2908 ok to use deprecated sections.
2909
2910 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2911 out parameters that are filled in with information about the CU and
2912 TU lists in the section.
2913
2914 Returns true if all went well, false otherwise. */
2915
2916 static bool
2917 read_gdb_index_from_buffer (const char *filename,
2918 bool deprecated_ok,
2919 gdb::array_view<const gdb_byte> buffer,
2920 struct mapped_index *map,
2921 const gdb_byte **cu_list,
2922 offset_type *cu_list_elements,
2923 const gdb_byte **types_list,
2924 offset_type *types_list_elements)
2925 {
2926 const gdb_byte *addr = &buffer[0];
2927
2928 /* Version check. */
2929 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2930 /* Versions earlier than 3 emitted every copy of a psymbol. This
2931 causes the index to behave very poorly for certain requests. Version 3
2932 contained incomplete addrmap. So, it seems better to just ignore such
2933 indices. */
2934 if (version < 4)
2935 {
2936 static int warning_printed = 0;
2937 if (!warning_printed)
2938 {
2939 warning (_("Skipping obsolete .gdb_index section in %s."),
2940 filename);
2941 warning_printed = 1;
2942 }
2943 return 0;
2944 }
2945 /* Index version 4 uses a different hash function than index version
2946 5 and later.
2947
2948 Versions earlier than 6 did not emit psymbols for inlined
2949 functions. Using these files will cause GDB not to be able to
2950 set breakpoints on inlined functions by name, so we ignore these
2951 indices unless the user has done
2952 "set use-deprecated-index-sections on". */
2953 if (version < 6 && !deprecated_ok)
2954 {
2955 static int warning_printed = 0;
2956 if (!warning_printed)
2957 {
2958 warning (_("\
2959 Skipping deprecated .gdb_index section in %s.\n\
2960 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2961 to use the section anyway."),
2962 filename);
2963 warning_printed = 1;
2964 }
2965 return 0;
2966 }
2967 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2968 of the TU (for symbols coming from TUs),
2969 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2970 Plus gold-generated indices can have duplicate entries for global symbols,
2971 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2972 These are just performance bugs, and we can't distinguish gdb-generated
2973 indices from gold-generated ones, so issue no warning here. */
2974
2975 /* Indexes with higher version than the one supported by GDB may be no
2976 longer backward compatible. */
2977 if (version > 8)
2978 return 0;
2979
2980 map->version = version;
2981
2982 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
2983
2984 int i = 0;
2985 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2986 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2987 / 8);
2988 ++i;
2989
2990 *types_list = addr + MAYBE_SWAP (metadata[i]);
2991 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2992 - MAYBE_SWAP (metadata[i]))
2993 / 8);
2994 ++i;
2995
2996 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2997 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2998 map->address_table
2999 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3000 ++i;
3001
3002 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3003 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3004 map->symbol_table
3005 = gdb::array_view<mapped_index::symbol_table_slot>
3006 ((mapped_index::symbol_table_slot *) symbol_table,
3007 (mapped_index::symbol_table_slot *) symbol_table_end);
3008
3009 ++i;
3010 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3011
3012 return 1;
3013 }
3014
3015 /* Callback types for dwarf2_read_gdb_index. */
3016
3017 typedef gdb::function_view
3018 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3019 get_gdb_index_contents_ftype;
3020 typedef gdb::function_view
3021 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3022 get_gdb_index_contents_dwz_ftype;
3023
3024 /* Read .gdb_index. If everything went ok, initialize the "quick"
3025 elements of all the CUs and return 1. Otherwise, return 0. */
3026
3027 static int
3028 dwarf2_read_gdb_index
3029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3030 get_gdb_index_contents_ftype get_gdb_index_contents,
3031 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3032 {
3033 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3034 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3035 struct dwz_file *dwz;
3036 struct objfile *objfile = dwarf2_per_objfile->objfile;
3037
3038 gdb::array_view<const gdb_byte> main_index_contents
3039 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
3040
3041 if (main_index_contents.empty ())
3042 return 0;
3043
3044 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3045 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3046 use_deprecated_index_sections,
3047 main_index_contents, map.get (), &cu_list,
3048 &cu_list_elements, &types_list,
3049 &types_list_elements))
3050 return 0;
3051
3052 /* Don't use the index if it's empty. */
3053 if (map->symbol_table.empty ())
3054 return 0;
3055
3056 /* If there is a .dwz file, read it so we can get its CU list as
3057 well. */
3058 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
3059 if (dwz != NULL)
3060 {
3061 struct mapped_index dwz_map;
3062 const gdb_byte *dwz_types_ignore;
3063 offset_type dwz_types_elements_ignore;
3064
3065 gdb::array_view<const gdb_byte> dwz_index_content
3066 = get_gdb_index_contents_dwz (objfile, dwz);
3067
3068 if (dwz_index_content.empty ())
3069 return 0;
3070
3071 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3072 1, dwz_index_content, &dwz_map,
3073 &dwz_list, &dwz_list_elements,
3074 &dwz_types_ignore,
3075 &dwz_types_elements_ignore))
3076 {
3077 warning (_("could not read '.gdb_index' section from %s; skipping"),
3078 bfd_get_filename (dwz->dwz_bfd.get ()));
3079 return 0;
3080 }
3081 }
3082
3083 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
3084 dwz_list, dwz_list_elements);
3085
3086 if (types_list_elements)
3087 {
3088 /* We can only handle a single .debug_types when we have an
3089 index. */
3090 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
3091 return 0;
3092
3093 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
3094
3095 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3096 section, types_list,
3097 types_list_elements);
3098 }
3099
3100 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
3101
3102 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3103 dwarf2_per_objfile->per_bfd->using_index = 1;
3104 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3105 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
3106
3107 return 1;
3108 }
3109
3110 /* die_reader_func for dw2_get_file_names. */
3111
3112 static void
3113 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3114 const gdb_byte *info_ptr,
3115 struct die_info *comp_unit_die)
3116 {
3117 struct dwarf2_cu *cu = reader->cu;
3118 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3119 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3120 struct dwarf2_per_cu_data *lh_cu;
3121 struct attribute *attr;
3122 void **slot;
3123 struct quick_file_names *qfn;
3124
3125 gdb_assert (! this_cu->is_debug_types);
3126
3127 /* Our callers never want to match partial units -- instead they
3128 will match the enclosing full CU. */
3129 if (comp_unit_die->tag == DW_TAG_partial_unit)
3130 {
3131 this_cu->v.quick->no_file_data = 1;
3132 return;
3133 }
3134
3135 lh_cu = this_cu;
3136 slot = NULL;
3137
3138 line_header_up lh;
3139 sect_offset line_offset {};
3140
3141 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3142 if (attr != nullptr)
3143 {
3144 struct quick_file_names find_entry;
3145
3146 line_offset = (sect_offset) DW_UNSND (attr);
3147
3148 /* We may have already read in this line header (TU line header sharing).
3149 If we have we're done. */
3150 find_entry.hash.dwo_unit = cu->dwo_unit;
3151 find_entry.hash.line_sect_off = line_offset;
3152 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3153 &find_entry, INSERT);
3154 if (*slot != NULL)
3155 {
3156 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3157 return;
3158 }
3159
3160 lh = dwarf_decode_line_header (line_offset, cu);
3161 }
3162 if (lh == NULL)
3163 {
3164 lh_cu->v.quick->no_file_data = 1;
3165 return;
3166 }
3167
3168 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
3169 qfn->hash.dwo_unit = cu->dwo_unit;
3170 qfn->hash.line_sect_off = line_offset;
3171 gdb_assert (slot != NULL);
3172 *slot = qfn;
3173
3174 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3175
3176 int offset = 0;
3177 if (strcmp (fnd.name, "<unknown>") != 0)
3178 ++offset;
3179
3180 qfn->num_file_names = offset + lh->file_names_size ();
3181 qfn->file_names =
3182 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
3183 qfn->num_file_names);
3184 if (offset != 0)
3185 qfn->file_names[0] = xstrdup (fnd.name);
3186 for (int i = 0; i < lh->file_names_size (); ++i)
3187 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3188 fnd.comp_dir).release ();
3189 qfn->real_names = NULL;
3190
3191 lh_cu->v.quick->file_names = qfn;
3192 }
3193
3194 /* A helper for the "quick" functions which attempts to read the line
3195 table for THIS_CU. */
3196
3197 static struct quick_file_names *
3198 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3199 dwarf2_per_objfile *per_objfile)
3200 {
3201 /* This should never be called for TUs. */
3202 gdb_assert (! this_cu->is_debug_types);
3203 /* Nor type unit groups. */
3204 gdb_assert (! this_cu->type_unit_group_p ());
3205
3206 if (this_cu->v.quick->file_names != NULL)
3207 return this_cu->v.quick->file_names;
3208 /* If we know there is no line data, no point in looking again. */
3209 if (this_cu->v.quick->no_file_data)
3210 return NULL;
3211
3212 cutu_reader reader (this_cu, per_objfile);
3213 if (!reader.dummy_p)
3214 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3215
3216 if (this_cu->v.quick->no_file_data)
3217 return NULL;
3218 return this_cu->v.quick->file_names;
3219 }
3220
3221 /* A helper for the "quick" functions which computes and caches the
3222 real path for a given file name from the line table. */
3223
3224 static const char *
3225 dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
3226 struct quick_file_names *qfn, int index)
3227 {
3228 if (qfn->real_names == NULL)
3229 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
3230 qfn->num_file_names, const char *);
3231
3232 if (qfn->real_names[index] == NULL)
3233 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3234
3235 return qfn->real_names[index];
3236 }
3237
3238 static struct symtab *
3239 dw2_find_last_source_symtab (struct objfile *objfile)
3240 {
3241 struct dwarf2_per_objfile *dwarf2_per_objfile
3242 = get_dwarf2_per_objfile (objfile);
3243 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
3244 compunit_symtab *cust
3245 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
3246
3247 if (cust == NULL)
3248 return NULL;
3249
3250 return compunit_primary_filetab (cust);
3251 }
3252
3253 /* Traversal function for dw2_forget_cached_source_info. */
3254
3255 static int
3256 dw2_free_cached_file_names (void **slot, void *info)
3257 {
3258 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3259
3260 if (file_data->real_names)
3261 {
3262 int i;
3263
3264 for (i = 0; i < file_data->num_file_names; ++i)
3265 {
3266 xfree ((void*) file_data->real_names[i]);
3267 file_data->real_names[i] = NULL;
3268 }
3269 }
3270
3271 return 1;
3272 }
3273
3274 static void
3275 dw2_forget_cached_source_info (struct objfile *objfile)
3276 {
3277 struct dwarf2_per_objfile *dwarf2_per_objfile
3278 = get_dwarf2_per_objfile (objfile);
3279
3280 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
3281 dw2_free_cached_file_names, NULL);
3282 }
3283
3284 /* Helper function for dw2_map_symtabs_matching_filename that expands
3285 the symtabs and calls the iterator. */
3286
3287 static int
3288 dw2_map_expand_apply (struct objfile *objfile,
3289 struct dwarf2_per_cu_data *per_cu,
3290 const char *name, const char *real_path,
3291 gdb::function_view<bool (symtab *)> callback)
3292 {
3293 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3294
3295 /* Don't visit already-expanded CUs. */
3296 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3297 if (per_objfile->symtab_set_p (per_cu))
3298 return 0;
3299
3300 /* This may expand more than one symtab, and we want to iterate over
3301 all of them. */
3302 dw2_instantiate_symtab (per_cu, per_objfile, false);
3303
3304 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3305 last_made, callback);
3306 }
3307
3308 /* Implementation of the map_symtabs_matching_filename method. */
3309
3310 static bool
3311 dw2_map_symtabs_matching_filename
3312 (struct objfile *objfile, const char *name, const char *real_path,
3313 gdb::function_view<bool (symtab *)> callback)
3314 {
3315 const char *name_basename = lbasename (name);
3316 struct dwarf2_per_objfile *dwarf2_per_objfile
3317 = get_dwarf2_per_objfile (objfile);
3318
3319 /* The rule is CUs specify all the files, including those used by
3320 any TU, so there's no need to scan TUs here. */
3321
3322 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3323 {
3324 /* We only need to look at symtabs not already expanded. */
3325 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3326 continue;
3327
3328 quick_file_names *file_data
3329 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3330 if (file_data == NULL)
3331 continue;
3332
3333 for (int j = 0; j < file_data->num_file_names; ++j)
3334 {
3335 const char *this_name = file_data->file_names[j];
3336 const char *this_real_name;
3337
3338 if (compare_filenames_for_search (this_name, name))
3339 {
3340 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3341 callback))
3342 return true;
3343 continue;
3344 }
3345
3346 /* Before we invoke realpath, which can get expensive when many
3347 files are involved, do a quick comparison of the basenames. */
3348 if (! basenames_may_differ
3349 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3350 continue;
3351
3352 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3353 file_data, j);
3354 if (compare_filenames_for_search (this_real_name, name))
3355 {
3356 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3357 callback))
3358 return true;
3359 continue;
3360 }
3361
3362 if (real_path != NULL)
3363 {
3364 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3365 gdb_assert (IS_ABSOLUTE_PATH (name));
3366 if (this_real_name != NULL
3367 && FILENAME_CMP (real_path, this_real_name) == 0)
3368 {
3369 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3370 callback))
3371 return true;
3372 continue;
3373 }
3374 }
3375 }
3376 }
3377
3378 return false;
3379 }
3380
3381 /* Struct used to manage iterating over all CUs looking for a symbol. */
3382
3383 struct dw2_symtab_iterator
3384 {
3385 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3386 struct dwarf2_per_objfile *dwarf2_per_objfile;
3387 /* If set, only look for symbols that match that block. Valid values are
3388 GLOBAL_BLOCK and STATIC_BLOCK. */
3389 gdb::optional<block_enum> block_index;
3390 /* The kind of symbol we're looking for. */
3391 domain_enum domain;
3392 /* The list of CUs from the index entry of the symbol,
3393 or NULL if not found. */
3394 offset_type *vec;
3395 /* The next element in VEC to look at. */
3396 int next;
3397 /* The number of elements in VEC, or zero if there is no match. */
3398 int length;
3399 /* Have we seen a global version of the symbol?
3400 If so we can ignore all further global instances.
3401 This is to work around gold/15646, inefficient gold-generated
3402 indices. */
3403 int global_seen;
3404 };
3405
3406 /* Initialize the index symtab iterator ITER. */
3407
3408 static void
3409 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3410 struct dwarf2_per_objfile *dwarf2_per_objfile,
3411 gdb::optional<block_enum> block_index,
3412 domain_enum domain,
3413 const char *name)
3414 {
3415 iter->dwarf2_per_objfile = dwarf2_per_objfile;
3416 iter->block_index = block_index;
3417 iter->domain = domain;
3418 iter->next = 0;
3419 iter->global_seen = 0;
3420
3421 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
3422
3423 /* index is NULL if OBJF_READNOW. */
3424 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
3425 iter->length = MAYBE_SWAP (*iter->vec);
3426 else
3427 {
3428 iter->vec = NULL;
3429 iter->length = 0;
3430 }
3431 }
3432
3433 /* Return the next matching CU or NULL if there are no more. */
3434
3435 static struct dwarf2_per_cu_data *
3436 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3437 {
3438 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3439
3440 for ( ; iter->next < iter->length; ++iter->next)
3441 {
3442 offset_type cu_index_and_attrs =
3443 MAYBE_SWAP (iter->vec[iter->next + 1]);
3444 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3445 gdb_index_symbol_kind symbol_kind =
3446 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3447 /* Only check the symbol attributes if they're present.
3448 Indices prior to version 7 don't record them,
3449 and indices >= 7 may elide them for certain symbols
3450 (gold does this). */
3451 int attrs_valid =
3452 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
3453 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3454
3455 /* Don't crash on bad data. */
3456 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3457 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3458 {
3459 complaint (_(".gdb_index entry has bad CU index"
3460 " [in module %s]"),
3461 objfile_name (dwarf2_per_objfile->objfile));
3462 continue;
3463 }
3464
3465 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3466
3467 /* Skip if already read in. */
3468 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3469 continue;
3470
3471 /* Check static vs global. */
3472 if (attrs_valid)
3473 {
3474 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3475
3476 if (iter->block_index.has_value ())
3477 {
3478 bool want_static = *iter->block_index == STATIC_BLOCK;
3479
3480 if (is_static != want_static)
3481 continue;
3482 }
3483
3484 /* Work around gold/15646. */
3485 if (!is_static && iter->global_seen)
3486 continue;
3487 if (!is_static)
3488 iter->global_seen = 1;
3489 }
3490
3491 /* Only check the symbol's kind if it has one. */
3492 if (attrs_valid)
3493 {
3494 switch (iter->domain)
3495 {
3496 case VAR_DOMAIN:
3497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3498 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3499 /* Some types are also in VAR_DOMAIN. */
3500 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3501 continue;
3502 break;
3503 case STRUCT_DOMAIN:
3504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3505 continue;
3506 break;
3507 case LABEL_DOMAIN:
3508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3509 continue;
3510 break;
3511 case MODULE_DOMAIN:
3512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3513 continue;
3514 break;
3515 default:
3516 break;
3517 }
3518 }
3519
3520 ++iter->next;
3521 return per_cu;
3522 }
3523
3524 return NULL;
3525 }
3526
3527 static struct compunit_symtab *
3528 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3529 const char *name, domain_enum domain)
3530 {
3531 struct compunit_symtab *stab_best = NULL;
3532 struct dwarf2_per_objfile *dwarf2_per_objfile
3533 = get_dwarf2_per_objfile (objfile);
3534
3535 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3536
3537 struct dw2_symtab_iterator iter;
3538 struct dwarf2_per_cu_data *per_cu;
3539
3540 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
3541
3542 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3543 {
3544 struct symbol *sym, *with_opaque = NULL;
3545 struct compunit_symtab *stab
3546 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3547 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3548 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3549
3550 sym = block_find_symbol (block, name, domain,
3551 block_find_non_opaque_type_preferred,
3552 &with_opaque);
3553
3554 /* Some caution must be observed with overloaded functions
3555 and methods, since the index will not contain any overload
3556 information (but NAME might contain it). */
3557
3558 if (sym != NULL
3559 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3560 return stab;
3561 if (with_opaque != NULL
3562 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3563 stab_best = stab;
3564
3565 /* Keep looking through other CUs. */
3566 }
3567
3568 return stab_best;
3569 }
3570
3571 static void
3572 dw2_print_stats (struct objfile *objfile)
3573 {
3574 struct dwarf2_per_objfile *dwarf2_per_objfile
3575 = get_dwarf2_per_objfile (objfile);
3576 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3577 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3578 int count = 0;
3579
3580 for (int i = 0; i < total; ++i)
3581 {
3582 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3583
3584 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
3585 ++count;
3586 }
3587 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3588 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3589 }
3590
3591 /* This dumps minimal information about the index.
3592 It is called via "mt print objfiles".
3593 One use is to verify .gdb_index has been loaded by the
3594 gdb.dwarf2/gdb-index.exp testcase. */
3595
3596 static void
3597 dw2_dump (struct objfile *objfile)
3598 {
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601
3602 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
3603 printf_filtered (".gdb_index:");
3604 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
3605 {
3606 printf_filtered (" version %d\n",
3607 dwarf2_per_objfile->per_bfd->index_table->version);
3608 }
3609 else
3610 printf_filtered (" faked for \"readnow\"\n");
3611 printf_filtered ("\n");
3612 }
3613
3614 static void
3615 dw2_expand_symtabs_for_function (struct objfile *objfile,
3616 const char *func_name)
3617 {
3618 struct dwarf2_per_objfile *dwarf2_per_objfile
3619 = get_dwarf2_per_objfile (objfile);
3620
3621 struct dw2_symtab_iterator iter;
3622 struct dwarf2_per_cu_data *per_cu;
3623
3624 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
3625
3626 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3627 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3628
3629 }
3630
3631 static void
3632 dw2_expand_all_symtabs (struct objfile *objfile)
3633 {
3634 struct dwarf2_per_objfile *dwarf2_per_objfile
3635 = get_dwarf2_per_objfile (objfile);
3636 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3637 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
3638
3639 for (int i = 0; i < total_units; ++i)
3640 {
3641 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
3642
3643 /* We don't want to directly expand a partial CU, because if we
3644 read it with the wrong language, then assertion failures can
3645 be triggered later on. See PR symtab/23010. So, tell
3646 dw2_instantiate_symtab to skip partial CUs -- any important
3647 partial CU will be read via DW_TAG_imported_unit anyway. */
3648 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
3649 }
3650 }
3651
3652 static void
3653 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3654 const char *fullname)
3655 {
3656 struct dwarf2_per_objfile *dwarf2_per_objfile
3657 = get_dwarf2_per_objfile (objfile);
3658
3659 /* We don't need to consider type units here.
3660 This is only called for examining code, e.g. expand_line_sal.
3661 There can be an order of magnitude (or more) more type units
3662 than comp units, and we avoid them if we can. */
3663
3664 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3665 {
3666 /* We only need to look at symtabs not already expanded. */
3667 if (dwarf2_per_objfile->symtab_set_p (per_cu))
3668 continue;
3669
3670 quick_file_names *file_data
3671 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
3672 if (file_data == NULL)
3673 continue;
3674
3675 for (int j = 0; j < file_data->num_file_names; ++j)
3676 {
3677 const char *this_fullname = file_data->file_names[j];
3678
3679 if (filename_cmp (this_fullname, fullname) == 0)
3680 {
3681 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
3682 break;
3683 }
3684 }
3685 }
3686 }
3687
3688 static void
3689 dw2_expand_symtabs_matching_symbol
3690 (mapped_index_base &index,
3691 const lookup_name_info &lookup_name_in,
3692 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3693 enum search_domain kind,
3694 gdb::function_view<bool (offset_type)> match_callback,
3695 dwarf2_per_objfile *per_objfile);
3696
3697 static void
3698 dw2_expand_symtabs_matching_one
3699 (dwarf2_per_cu_data *per_cu,
3700 dwarf2_per_objfile *per_objfile,
3701 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3702 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3703
3704 static void
3705 dw2_map_matching_symbols
3706 (struct objfile *objfile,
3707 const lookup_name_info &name, domain_enum domain,
3708 int global,
3709 gdb::function_view<symbol_found_callback_ftype> callback,
3710 symbol_compare_ftype *ordered_compare)
3711 {
3712 /* Used for Ada. */
3713 struct dwarf2_per_objfile *dwarf2_per_objfile
3714 = get_dwarf2_per_objfile (objfile);
3715
3716 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3717
3718 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
3719 {
3720 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3721 here though if the current language is Ada for a non-Ada objfile
3722 using GNU index. */
3723 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3724
3725 const char *match_name = name.ada ().lookup_name ().c_str ();
3726 auto matcher = [&] (const char *symname)
3727 {
3728 if (ordered_compare == nullptr)
3729 return true;
3730 return ordered_compare (symname, match_name) == 0;
3731 };
3732
3733 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3734 [&] (offset_type namei)
3735 {
3736 struct dw2_symtab_iterator iter;
3737 struct dwarf2_per_cu_data *per_cu;
3738
3739 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3740 match_name);
3741 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3742 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3743 nullptr);
3744 return true;
3745 }, dwarf2_per_objfile);
3746 }
3747 else
3748 {
3749 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3750 proceed assuming all symtabs have been read in. */
3751 }
3752
3753 for (compunit_symtab *cust : objfile->compunits ())
3754 {
3755 const struct block *block;
3756
3757 if (cust == NULL)
3758 continue;
3759 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3760 if (!iterate_over_symbols_terminated (block, name,
3761 domain, callback))
3762 return;
3763 }
3764 }
3765
3766 /* Starting from a search name, return the string that finds the upper
3767 bound of all strings that start with SEARCH_NAME in a sorted name
3768 list. Returns the empty string to indicate that the upper bound is
3769 the end of the list. */
3770
3771 static std::string
3772 make_sort_after_prefix_name (const char *search_name)
3773 {
3774 /* When looking to complete "func", we find the upper bound of all
3775 symbols that start with "func" by looking for where we'd insert
3776 the closest string that would follow "func" in lexicographical
3777 order. Usually, that's "func"-with-last-character-incremented,
3778 i.e. "fund". Mind non-ASCII characters, though. Usually those
3779 will be UTF-8 multi-byte sequences, but we can't be certain.
3780 Especially mind the 0xff character, which is a valid character in
3781 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3782 rule out compilers allowing it in identifiers. Note that
3783 conveniently, strcmp/strcasecmp are specified to compare
3784 characters interpreted as unsigned char. So what we do is treat
3785 the whole string as a base 256 number composed of a sequence of
3786 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3787 to 0, and carries 1 to the following more-significant position.
3788 If the very first character in SEARCH_NAME ends up incremented
3789 and carries/overflows, then the upper bound is the end of the
3790 list. The string after the empty string is also the empty
3791 string.
3792
3793 Some examples of this operation:
3794
3795 SEARCH_NAME => "+1" RESULT
3796
3797 "abc" => "abd"
3798 "ab\xff" => "ac"
3799 "\xff" "a" "\xff" => "\xff" "b"
3800 "\xff" => ""
3801 "\xff\xff" => ""
3802 "" => ""
3803
3804 Then, with these symbols for example:
3805
3806 func
3807 func1
3808 fund
3809
3810 completing "func" looks for symbols between "func" and
3811 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3812 which finds "func" and "func1", but not "fund".
3813
3814 And with:
3815
3816 funcÿ (Latin1 'ÿ' [0xff])
3817 funcÿ1
3818 fund
3819
3820 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3821 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3822
3823 And with:
3824
3825 ÿÿ (Latin1 'ÿ' [0xff])
3826 ÿÿ1
3827
3828 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3829 the end of the list.
3830 */
3831 std::string after = search_name;
3832 while (!after.empty () && (unsigned char) after.back () == 0xff)
3833 after.pop_back ();
3834 if (!after.empty ())
3835 after.back () = (unsigned char) after.back () + 1;
3836 return after;
3837 }
3838
3839 /* See declaration. */
3840
3841 std::pair<std::vector<name_component>::const_iterator,
3842 std::vector<name_component>::const_iterator>
3843 mapped_index_base::find_name_components_bounds
3844 (const lookup_name_info &lookup_name_without_params, language lang,
3845 dwarf2_per_objfile *per_objfile) const
3846 {
3847 auto *name_cmp
3848 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3849
3850 const char *lang_name
3851 = lookup_name_without_params.language_lookup_name (lang);
3852
3853 /* Comparison function object for lower_bound that matches against a
3854 given symbol name. */
3855 auto lookup_compare_lower = [&] (const name_component &elem,
3856 const char *name)
3857 {
3858 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3859 const char *elem_name = elem_qualified + elem.name_offset;
3860 return name_cmp (elem_name, name) < 0;
3861 };
3862
3863 /* Comparison function object for upper_bound that matches against a
3864 given symbol name. */
3865 auto lookup_compare_upper = [&] (const char *name,
3866 const name_component &elem)
3867 {
3868 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3869 const char *elem_name = elem_qualified + elem.name_offset;
3870 return name_cmp (name, elem_name) < 0;
3871 };
3872
3873 auto begin = this->name_components.begin ();
3874 auto end = this->name_components.end ();
3875
3876 /* Find the lower bound. */
3877 auto lower = [&] ()
3878 {
3879 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3880 return begin;
3881 else
3882 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3883 } ();
3884
3885 /* Find the upper bound. */
3886 auto upper = [&] ()
3887 {
3888 if (lookup_name_without_params.completion_mode ())
3889 {
3890 /* In completion mode, we want UPPER to point past all
3891 symbols names that have the same prefix. I.e., with
3892 these symbols, and completing "func":
3893
3894 function << lower bound
3895 function1
3896 other_function << upper bound
3897
3898 We find the upper bound by looking for the insertion
3899 point of "func"-with-last-character-incremented,
3900 i.e. "fund". */
3901 std::string after = make_sort_after_prefix_name (lang_name);
3902 if (after.empty ())
3903 return end;
3904 return std::lower_bound (lower, end, after.c_str (),
3905 lookup_compare_lower);
3906 }
3907 else
3908 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3909 } ();
3910
3911 return {lower, upper};
3912 }
3913
3914 /* See declaration. */
3915
3916 void
3917 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3918 {
3919 if (!this->name_components.empty ())
3920 return;
3921
3922 this->name_components_casing = case_sensitivity;
3923 auto *name_cmp
3924 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3925
3926 /* The code below only knows how to break apart components of C++
3927 symbol names (and other languages that use '::' as
3928 namespace/module separator) and Ada symbol names. */
3929 auto count = this->symbol_name_count ();
3930 for (offset_type idx = 0; idx < count; idx++)
3931 {
3932 if (this->symbol_name_slot_invalid (idx))
3933 continue;
3934
3935 const char *name = this->symbol_name_at (idx, per_objfile);
3936
3937 /* Add each name component to the name component table. */
3938 unsigned int previous_len = 0;
3939
3940 if (strstr (name, "::") != nullptr)
3941 {
3942 for (unsigned int current_len = cp_find_first_component (name);
3943 name[current_len] != '\0';
3944 current_len += cp_find_first_component (name + current_len))
3945 {
3946 gdb_assert (name[current_len] == ':');
3947 this->name_components.push_back ({previous_len, idx});
3948 /* Skip the '::'. */
3949 current_len += 2;
3950 previous_len = current_len;
3951 }
3952 }
3953 else
3954 {
3955 /* Handle the Ada encoded (aka mangled) form here. */
3956 for (const char *iter = strstr (name, "__");
3957 iter != nullptr;
3958 iter = strstr (iter, "__"))
3959 {
3960 this->name_components.push_back ({previous_len, idx});
3961 iter += 2;
3962 previous_len = iter - name;
3963 }
3964 }
3965
3966 this->name_components.push_back ({previous_len, idx});
3967 }
3968
3969 /* Sort name_components elements by name. */
3970 auto name_comp_compare = [&] (const name_component &left,
3971 const name_component &right)
3972 {
3973 const char *left_qualified
3974 = this->symbol_name_at (left.idx, per_objfile);
3975 const char *right_qualified
3976 = this->symbol_name_at (right.idx, per_objfile);
3977
3978 const char *left_name = left_qualified + left.name_offset;
3979 const char *right_name = right_qualified + right.name_offset;
3980
3981 return name_cmp (left_name, right_name) < 0;
3982 };
3983
3984 std::sort (this->name_components.begin (),
3985 this->name_components.end (),
3986 name_comp_compare);
3987 }
3988
3989 /* Helper for dw2_expand_symtabs_matching that works with a
3990 mapped_index_base instead of the containing objfile. This is split
3991 to a separate function in order to be able to unit test the
3992 name_components matching using a mock mapped_index_base. For each
3993 symbol name that matches, calls MATCH_CALLBACK, passing it the
3994 symbol's index in the mapped_index_base symbol table. */
3995
3996 static void
3997 dw2_expand_symtabs_matching_symbol
3998 (mapped_index_base &index,
3999 const lookup_name_info &lookup_name_in,
4000 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4001 enum search_domain kind,
4002 gdb::function_view<bool (offset_type)> match_callback,
4003 dwarf2_per_objfile *per_objfile)
4004 {
4005 lookup_name_info lookup_name_without_params
4006 = lookup_name_in.make_ignore_params ();
4007
4008 /* Build the symbol name component sorted vector, if we haven't
4009 yet. */
4010 index.build_name_components (per_objfile);
4011
4012 /* The same symbol may appear more than once in the range though.
4013 E.g., if we're looking for symbols that complete "w", and we have
4014 a symbol named "w1::w2", we'll find the two name components for
4015 that same symbol in the range. To be sure we only call the
4016 callback once per symbol, we first collect the symbol name
4017 indexes that matched in a temporary vector and ignore
4018 duplicates. */
4019 std::vector<offset_type> matches;
4020
4021 struct name_and_matcher
4022 {
4023 symbol_name_matcher_ftype *matcher;
4024 const char *name;
4025
4026 bool operator== (const name_and_matcher &other) const
4027 {
4028 return matcher == other.matcher && strcmp (name, other.name) == 0;
4029 }
4030 };
4031
4032 /* A vector holding all the different symbol name matchers, for all
4033 languages. */
4034 std::vector<name_and_matcher> matchers;
4035
4036 for (int i = 0; i < nr_languages; i++)
4037 {
4038 enum language lang_e = (enum language) i;
4039
4040 const language_defn *lang = language_def (lang_e);
4041 symbol_name_matcher_ftype *name_matcher
4042 = get_symbol_name_matcher (lang, lookup_name_without_params);
4043
4044 name_and_matcher key {
4045 name_matcher,
4046 lookup_name_without_params.language_lookup_name (lang_e)
4047 };
4048
4049 /* Don't insert the same comparison routine more than once.
4050 Note that we do this linear walk. This is not a problem in
4051 practice because the number of supported languages is
4052 low. */
4053 if (std::find (matchers.begin (), matchers.end (), key)
4054 != matchers.end ())
4055 continue;
4056 matchers.push_back (std::move (key));
4057
4058 auto bounds
4059 = index.find_name_components_bounds (lookup_name_without_params,
4060 lang_e, per_objfile);
4061
4062 /* Now for each symbol name in range, check to see if we have a name
4063 match, and if so, call the MATCH_CALLBACK callback. */
4064
4065 for (; bounds.first != bounds.second; ++bounds.first)
4066 {
4067 const char *qualified
4068 = index.symbol_name_at (bounds.first->idx, per_objfile);
4069
4070 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4071 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4072 continue;
4073
4074 matches.push_back (bounds.first->idx);
4075 }
4076 }
4077
4078 std::sort (matches.begin (), matches.end ());
4079
4080 /* Finally call the callback, once per match. */
4081 ULONGEST prev = -1;
4082 for (offset_type idx : matches)
4083 {
4084 if (prev != idx)
4085 {
4086 if (!match_callback (idx))
4087 break;
4088 prev = idx;
4089 }
4090 }
4091
4092 /* Above we use a type wider than idx's for 'prev', since 0 and
4093 (offset_type)-1 are both possible values. */
4094 static_assert (sizeof (prev) > sizeof (offset_type), "");
4095 }
4096
4097 #if GDB_SELF_TEST
4098
4099 namespace selftests { namespace dw2_expand_symtabs_matching {
4100
4101 /* A mock .gdb_index/.debug_names-like name index table, enough to
4102 exercise dw2_expand_symtabs_matching_symbol, which works with the
4103 mapped_index_base interface. Builds an index from the symbol list
4104 passed as parameter to the constructor. */
4105 class mock_mapped_index : public mapped_index_base
4106 {
4107 public:
4108 mock_mapped_index (gdb::array_view<const char *> symbols)
4109 : m_symbol_table (symbols)
4110 {}
4111
4112 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4113
4114 /* Return the number of names in the symbol table. */
4115 size_t symbol_name_count () const override
4116 {
4117 return m_symbol_table.size ();
4118 }
4119
4120 /* Get the name of the symbol at IDX in the symbol table. */
4121 const char *symbol_name_at
4122 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4123 {
4124 return m_symbol_table[idx];
4125 }
4126
4127 private:
4128 gdb::array_view<const char *> m_symbol_table;
4129 };
4130
4131 /* Convenience function that converts a NULL pointer to a "<null>"
4132 string, to pass to print routines. */
4133
4134 static const char *
4135 string_or_null (const char *str)
4136 {
4137 return str != NULL ? str : "<null>";
4138 }
4139
4140 /* Check if a lookup_name_info built from
4141 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4142 index. EXPECTED_LIST is the list of expected matches, in expected
4143 matching order. If no match expected, then an empty list is
4144 specified. Returns true on success. On failure prints a warning
4145 indicating the file:line that failed, and returns false. */
4146
4147 static bool
4148 check_match (const char *file, int line,
4149 mock_mapped_index &mock_index,
4150 const char *name, symbol_name_match_type match_type,
4151 bool completion_mode,
4152 std::initializer_list<const char *> expected_list,
4153 dwarf2_per_objfile *per_objfile)
4154 {
4155 lookup_name_info lookup_name (name, match_type, completion_mode);
4156
4157 bool matched = true;
4158
4159 auto mismatch = [&] (const char *expected_str,
4160 const char *got)
4161 {
4162 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4163 "expected=\"%s\", got=\"%s\"\n"),
4164 file, line,
4165 (match_type == symbol_name_match_type::FULL
4166 ? "FULL" : "WILD"),
4167 name, string_or_null (expected_str), string_or_null (got));
4168 matched = false;
4169 };
4170
4171 auto expected_it = expected_list.begin ();
4172 auto expected_end = expected_list.end ();
4173
4174 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4175 NULL, ALL_DOMAIN,
4176 [&] (offset_type idx)
4177 {
4178 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4179 const char *expected_str
4180 = expected_it == expected_end ? NULL : *expected_it++;
4181
4182 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4183 mismatch (expected_str, matched_name);
4184 return true;
4185 }, per_objfile);
4186
4187 const char *expected_str
4188 = expected_it == expected_end ? NULL : *expected_it++;
4189 if (expected_str != NULL)
4190 mismatch (expected_str, NULL);
4191
4192 return matched;
4193 }
4194
4195 /* The symbols added to the mock mapped_index for testing (in
4196 canonical form). */
4197 static const char *test_symbols[] = {
4198 "function",
4199 "std::bar",
4200 "std::zfunction",
4201 "std::zfunction2",
4202 "w1::w2",
4203 "ns::foo<char*>",
4204 "ns::foo<int>",
4205 "ns::foo<long>",
4206 "ns2::tmpl<int>::foo2",
4207 "(anonymous namespace)::A::B::C",
4208
4209 /* These are used to check that the increment-last-char in the
4210 matching algorithm for completion doesn't match "t1_fund" when
4211 completing "t1_func". */
4212 "t1_func",
4213 "t1_func1",
4214 "t1_fund",
4215 "t1_fund1",
4216
4217 /* A UTF-8 name with multi-byte sequences to make sure that
4218 cp-name-parser understands this as a single identifier ("função"
4219 is "function" in PT). */
4220 u8"u8função",
4221
4222 /* \377 (0xff) is Latin1 'ÿ'. */
4223 "yfunc\377",
4224
4225 /* \377 (0xff) is Latin1 'ÿ'. */
4226 "\377",
4227 "\377\377123",
4228
4229 /* A name with all sorts of complications. Starts with "z" to make
4230 it easier for the completion tests below. */
4231 #define Z_SYM_NAME \
4232 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4233 "::tuple<(anonymous namespace)::ui*, " \
4234 "std::default_delete<(anonymous namespace)::ui>, void>"
4235
4236 Z_SYM_NAME
4237 };
4238
4239 /* Returns true if the mapped_index_base::find_name_component_bounds
4240 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4241 in completion mode. */
4242
4243 static bool
4244 check_find_bounds_finds (mapped_index_base &index,
4245 const char *search_name,
4246 gdb::array_view<const char *> expected_syms,
4247 dwarf2_per_objfile *per_objfile)
4248 {
4249 lookup_name_info lookup_name (search_name,
4250 symbol_name_match_type::FULL, true);
4251
4252 auto bounds = index.find_name_components_bounds (lookup_name,
4253 language_cplus,
4254 per_objfile);
4255
4256 size_t distance = std::distance (bounds.first, bounds.second);
4257 if (distance != expected_syms.size ())
4258 return false;
4259
4260 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4261 {
4262 auto nc_elem = bounds.first + exp_elem;
4263 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4264 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4265 return false;
4266 }
4267
4268 return true;
4269 }
4270
4271 /* Test the lower-level mapped_index::find_name_component_bounds
4272 method. */
4273
4274 static void
4275 test_mapped_index_find_name_component_bounds ()
4276 {
4277 mock_mapped_index mock_index (test_symbols);
4278
4279 mock_index.build_name_components (NULL /* per_objfile */);
4280
4281 /* Test the lower-level mapped_index::find_name_component_bounds
4282 method in completion mode. */
4283 {
4284 static const char *expected_syms[] = {
4285 "t1_func",
4286 "t1_func1",
4287 };
4288
4289 SELF_CHECK (check_find_bounds_finds
4290 (mock_index, "t1_func", expected_syms,
4291 NULL /* per_objfile */));
4292 }
4293
4294 /* Check that the increment-last-char in the name matching algorithm
4295 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4296 {
4297 static const char *expected_syms1[] = {
4298 "\377",
4299 "\377\377123",
4300 };
4301 SELF_CHECK (check_find_bounds_finds
4302 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4303
4304 static const char *expected_syms2[] = {
4305 "\377\377123",
4306 };
4307 SELF_CHECK (check_find_bounds_finds
4308 (mock_index, "\377\377", expected_syms2,
4309 NULL /* per_objfile */));
4310 }
4311 }
4312
4313 /* Test dw2_expand_symtabs_matching_symbol. */
4314
4315 static void
4316 test_dw2_expand_symtabs_matching_symbol ()
4317 {
4318 mock_mapped_index mock_index (test_symbols);
4319
4320 /* We let all tests run until the end even if some fails, for debug
4321 convenience. */
4322 bool any_mismatch = false;
4323
4324 /* Create the expected symbols list (an initializer_list). Needed
4325 because lists have commas, and we need to pass them to CHECK,
4326 which is a macro. */
4327 #define EXPECT(...) { __VA_ARGS__ }
4328
4329 /* Wrapper for check_match that passes down the current
4330 __FILE__/__LINE__. */
4331 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4332 any_mismatch |= !check_match (__FILE__, __LINE__, \
4333 mock_index, \
4334 NAME, MATCH_TYPE, COMPLETION_MODE, \
4335 EXPECTED_LIST, NULL)
4336
4337 /* Identity checks. */
4338 for (const char *sym : test_symbols)
4339 {
4340 /* Should be able to match all existing symbols. */
4341 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4342 EXPECT (sym));
4343
4344 /* Should be able to match all existing symbols with
4345 parameters. */
4346 std::string with_params = std::string (sym) + "(int)";
4347 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4348 EXPECT (sym));
4349
4350 /* Should be able to match all existing symbols with
4351 parameters and qualifiers. */
4352 with_params = std::string (sym) + " ( int ) const";
4353 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4354 EXPECT (sym));
4355
4356 /* This should really find sym, but cp-name-parser.y doesn't
4357 know about lvalue/rvalue qualifiers yet. */
4358 with_params = std::string (sym) + " ( int ) &&";
4359 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4360 {});
4361 }
4362
4363 /* Check that the name matching algorithm for completion doesn't get
4364 confused with Latin1 'ÿ' / 0xff. */
4365 {
4366 static const char str[] = "\377";
4367 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4368 EXPECT ("\377", "\377\377123"));
4369 }
4370
4371 /* Check that the increment-last-char in the matching algorithm for
4372 completion doesn't match "t1_fund" when completing "t1_func". */
4373 {
4374 static const char str[] = "t1_func";
4375 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4376 EXPECT ("t1_func", "t1_func1"));
4377 }
4378
4379 /* Check that completion mode works at each prefix of the expected
4380 symbol name. */
4381 {
4382 static const char str[] = "function(int)";
4383 size_t len = strlen (str);
4384 std::string lookup;
4385
4386 for (size_t i = 1; i < len; i++)
4387 {
4388 lookup.assign (str, i);
4389 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4390 EXPECT ("function"));
4391 }
4392 }
4393
4394 /* While "w" is a prefix of both components, the match function
4395 should still only be called once. */
4396 {
4397 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4398 EXPECT ("w1::w2"));
4399 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4400 EXPECT ("w1::w2"));
4401 }
4402
4403 /* Same, with a "complicated" symbol. */
4404 {
4405 static const char str[] = Z_SYM_NAME;
4406 size_t len = strlen (str);
4407 std::string lookup;
4408
4409 for (size_t i = 1; i < len; i++)
4410 {
4411 lookup.assign (str, i);
4412 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4413 EXPECT (Z_SYM_NAME));
4414 }
4415 }
4416
4417 /* In FULL mode, an incomplete symbol doesn't match. */
4418 {
4419 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4420 {});
4421 }
4422
4423 /* A complete symbol with parameters matches any overload, since the
4424 index has no overload info. */
4425 {
4426 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4427 EXPECT ("std::zfunction", "std::zfunction2"));
4428 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4429 EXPECT ("std::zfunction", "std::zfunction2"));
4430 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4431 EXPECT ("std::zfunction", "std::zfunction2"));
4432 }
4433
4434 /* Check that whitespace is ignored appropriately. A symbol with a
4435 template argument list. */
4436 {
4437 static const char expected[] = "ns::foo<int>";
4438 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4439 EXPECT (expected));
4440 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4441 EXPECT (expected));
4442 }
4443
4444 /* Check that whitespace is ignored appropriately. A symbol with a
4445 template argument list that includes a pointer. */
4446 {
4447 static const char expected[] = "ns::foo<char*>";
4448 /* Try both completion and non-completion modes. */
4449 static const bool completion_mode[2] = {false, true};
4450 for (size_t i = 0; i < 2; i++)
4451 {
4452 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4453 completion_mode[i], EXPECT (expected));
4454 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4455 completion_mode[i], EXPECT (expected));
4456
4457 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4458 completion_mode[i], EXPECT (expected));
4459 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4460 completion_mode[i], EXPECT (expected));
4461 }
4462 }
4463
4464 {
4465 /* Check method qualifiers are ignored. */
4466 static const char expected[] = "ns::foo<char*>";
4467 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4468 symbol_name_match_type::FULL, true, EXPECT (expected));
4469 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4470 symbol_name_match_type::FULL, true, EXPECT (expected));
4471 CHECK_MATCH ("foo < char * > ( int ) const",
4472 symbol_name_match_type::WILD, true, EXPECT (expected));
4473 CHECK_MATCH ("foo < char * > ( int ) &&",
4474 symbol_name_match_type::WILD, true, EXPECT (expected));
4475 }
4476
4477 /* Test lookup names that don't match anything. */
4478 {
4479 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4480 {});
4481
4482 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4483 {});
4484 }
4485
4486 /* Some wild matching tests, exercising "(anonymous namespace)",
4487 which should not be confused with a parameter list. */
4488 {
4489 static const char *syms[] = {
4490 "A::B::C",
4491 "B::C",
4492 "C",
4493 "A :: B :: C ( int )",
4494 "B :: C ( int )",
4495 "C ( int )",
4496 };
4497
4498 for (const char *s : syms)
4499 {
4500 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4501 EXPECT ("(anonymous namespace)::A::B::C"));
4502 }
4503 }
4504
4505 {
4506 static const char expected[] = "ns2::tmpl<int>::foo2";
4507 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4508 EXPECT (expected));
4509 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4510 EXPECT (expected));
4511 }
4512
4513 SELF_CHECK (!any_mismatch);
4514
4515 #undef EXPECT
4516 #undef CHECK_MATCH
4517 }
4518
4519 static void
4520 run_test ()
4521 {
4522 test_mapped_index_find_name_component_bounds ();
4523 test_dw2_expand_symtabs_matching_symbol ();
4524 }
4525
4526 }} // namespace selftests::dw2_expand_symtabs_matching
4527
4528 #endif /* GDB_SELF_TEST */
4529
4530 /* If FILE_MATCHER is NULL or if PER_CU has
4531 dwarf2_per_cu_quick_data::MARK set (see
4532 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4533 EXPANSION_NOTIFY on it. */
4534
4535 static void
4536 dw2_expand_symtabs_matching_one
4537 (dwarf2_per_cu_data *per_cu,
4538 dwarf2_per_objfile *per_objfile,
4539 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4540 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4541 {
4542 if (file_matcher == NULL || per_cu->v.quick->mark)
4543 {
4544 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4545
4546 compunit_symtab *symtab
4547 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4548 gdb_assert (symtab != nullptr);
4549
4550 if (expansion_notify != NULL && symtab_was_null)
4551 expansion_notify (symtab);
4552 }
4553 }
4554
4555 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4556 matched, to expand corresponding CUs that were marked. IDX is the
4557 index of the symbol name that matched. */
4558
4559 static void
4560 dw2_expand_marked_cus
4561 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
4562 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4563 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4564 search_domain kind)
4565 {
4566 offset_type *vec, vec_len, vec_idx;
4567 bool global_seen = false;
4568 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4569
4570 vec = (offset_type *) (index.constant_pool
4571 + MAYBE_SWAP (index.symbol_table[idx].vec));
4572 vec_len = MAYBE_SWAP (vec[0]);
4573 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4574 {
4575 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4576 /* This value is only valid for index versions >= 7. */
4577 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4578 gdb_index_symbol_kind symbol_kind =
4579 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4580 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4581 /* Only check the symbol attributes if they're present.
4582 Indices prior to version 7 don't record them,
4583 and indices >= 7 may elide them for certain symbols
4584 (gold does this). */
4585 int attrs_valid =
4586 (index.version >= 7
4587 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4588
4589 /* Work around gold/15646. */
4590 if (attrs_valid)
4591 {
4592 if (!is_static && global_seen)
4593 continue;
4594 if (!is_static)
4595 global_seen = true;
4596 }
4597
4598 /* Only check the symbol's kind if it has one. */
4599 if (attrs_valid)
4600 {
4601 switch (kind)
4602 {
4603 case VARIABLES_DOMAIN:
4604 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4605 continue;
4606 break;
4607 case FUNCTIONS_DOMAIN:
4608 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4609 continue;
4610 break;
4611 case TYPES_DOMAIN:
4612 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4613 continue;
4614 break;
4615 case MODULES_DOMAIN:
4616 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4617 continue;
4618 break;
4619 default:
4620 break;
4621 }
4622 }
4623
4624 /* Don't crash on bad data. */
4625 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4626 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
4627 {
4628 complaint (_(".gdb_index entry has bad CU index"
4629 " [in module %s]"),
4630 objfile_name (dwarf2_per_objfile->objfile));
4631 continue;
4632 }
4633
4634 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
4635 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4636 expansion_notify);
4637 }
4638 }
4639
4640 /* If FILE_MATCHER is non-NULL, set all the
4641 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4642 that match FILE_MATCHER. */
4643
4644 static void
4645 dw_expand_symtabs_matching_file_matcher
4646 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4647 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4648 {
4649 if (file_matcher == NULL)
4650 return;
4651
4652 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4653 htab_eq_pointer,
4654 NULL, xcalloc, xfree));
4655 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4656 htab_eq_pointer,
4657 NULL, xcalloc, xfree));
4658
4659 /* The rule is CUs specify all the files, including those used by
4660 any TU, so there's no need to scan TUs here. */
4661
4662 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4663 {
4664 QUIT;
4665
4666 per_cu->v.quick->mark = 0;
4667
4668 /* We only need to look at symtabs not already expanded. */
4669 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4670 continue;
4671
4672 quick_file_names *file_data
4673 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4674 if (file_data == NULL)
4675 continue;
4676
4677 if (htab_find (visited_not_found.get (), file_data) != NULL)
4678 continue;
4679 else if (htab_find (visited_found.get (), file_data) != NULL)
4680 {
4681 per_cu->v.quick->mark = 1;
4682 continue;
4683 }
4684
4685 for (int j = 0; j < file_data->num_file_names; ++j)
4686 {
4687 const char *this_real_name;
4688
4689 if (file_matcher (file_data->file_names[j], false))
4690 {
4691 per_cu->v.quick->mark = 1;
4692 break;
4693 }
4694
4695 /* Before we invoke realpath, which can get expensive when many
4696 files are involved, do a quick comparison of the basenames. */
4697 if (!basenames_may_differ
4698 && !file_matcher (lbasename (file_data->file_names[j]),
4699 true))
4700 continue;
4701
4702 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4703 file_data, j);
4704 if (file_matcher (this_real_name, false))
4705 {
4706 per_cu->v.quick->mark = 1;
4707 break;
4708 }
4709 }
4710
4711 void **slot = htab_find_slot (per_cu->v.quick->mark
4712 ? visited_found.get ()
4713 : visited_not_found.get (),
4714 file_data, INSERT);
4715 *slot = file_data;
4716 }
4717 }
4718
4719 static void
4720 dw2_expand_symtabs_matching
4721 (struct objfile *objfile,
4722 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4723 const lookup_name_info *lookup_name,
4724 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4725 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4726 enum search_domain kind)
4727 {
4728 struct dwarf2_per_objfile *dwarf2_per_objfile
4729 = get_dwarf2_per_objfile (objfile);
4730
4731 /* index_table is NULL if OBJF_READNOW. */
4732 if (!dwarf2_per_objfile->per_bfd->index_table)
4733 return;
4734
4735 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
4736
4737 if (symbol_matcher == NULL && lookup_name == NULL)
4738 {
4739 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4740 {
4741 QUIT;
4742
4743 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4744 file_matcher, expansion_notify);
4745 }
4746 return;
4747 }
4748
4749 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
4750
4751 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4752 symbol_matcher,
4753 kind, [&] (offset_type idx)
4754 {
4755 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
4756 expansion_notify, kind);
4757 return true;
4758 }, dwarf2_per_objfile);
4759 }
4760
4761 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4762 symtab. */
4763
4764 static struct compunit_symtab *
4765 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4766 CORE_ADDR pc)
4767 {
4768 int i;
4769
4770 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4771 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4772 return cust;
4773
4774 if (cust->includes == NULL)
4775 return NULL;
4776
4777 for (i = 0; cust->includes[i]; ++i)
4778 {
4779 struct compunit_symtab *s = cust->includes[i];
4780
4781 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4782 if (s != NULL)
4783 return s;
4784 }
4785
4786 return NULL;
4787 }
4788
4789 static struct compunit_symtab *
4790 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4791 struct bound_minimal_symbol msymbol,
4792 CORE_ADDR pc,
4793 struct obj_section *section,
4794 int warn_if_readin)
4795 {
4796 struct dwarf2_per_cu_data *data;
4797 struct compunit_symtab *result;
4798
4799 if (!objfile->partial_symtabs->psymtabs_addrmap)
4800 return NULL;
4801
4802 CORE_ADDR baseaddr = objfile->text_section_offset ();
4803 data = (struct dwarf2_per_cu_data *) addrmap_find
4804 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
4805 if (!data)
4806 return NULL;
4807
4808 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4809 if (warn_if_readin && per_objfile->symtab_set_p (data))
4810 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4811 paddress (objfile->arch (), pc));
4812
4813 result = recursively_find_pc_sect_compunit_symtab
4814 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4815
4816 gdb_assert (result != NULL);
4817 return result;
4818 }
4819
4820 static void
4821 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4822 void *data, int need_fullname)
4823 {
4824 struct dwarf2_per_objfile *dwarf2_per_objfile
4825 = get_dwarf2_per_objfile (objfile);
4826
4827 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
4828 {
4829 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
4830
4831 htab_up visited (htab_create_alloc (10,
4832 htab_hash_pointer, htab_eq_pointer,
4833 NULL, xcalloc, xfree));
4834
4835 /* The rule is CUs specify all the files, including those used
4836 by any TU, so there's no need to scan TUs here. We can
4837 ignore file names coming from already-expanded CUs. */
4838
4839 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4840 {
4841 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4842 {
4843 void **slot = htab_find_slot (visited.get (),
4844 per_cu->v.quick->file_names,
4845 INSERT);
4846
4847 *slot = per_cu->v.quick->file_names;
4848 }
4849 }
4850
4851 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
4852 {
4853 /* We only need to look at symtabs not already expanded. */
4854 if (dwarf2_per_objfile->symtab_set_p (per_cu))
4855 continue;
4856
4857 quick_file_names *file_data
4858 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
4859 if (file_data == NULL)
4860 continue;
4861
4862 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4863 if (*slot)
4864 {
4865 /* Already visited. */
4866 continue;
4867 }
4868 *slot = file_data;
4869
4870 for (int j = 0; j < file_data->num_file_names; ++j)
4871 {
4872 const char *filename = file_data->file_names[j];
4873 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
4874 }
4875 }
4876 }
4877
4878 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4879 {
4880 gdb::unique_xmalloc_ptr<char> this_real_name;
4881
4882 if (need_fullname)
4883 this_real_name = gdb_realpath (filename);
4884 (*fun) (filename, this_real_name.get (), data);
4885 });
4886 }
4887
4888 static int
4889 dw2_has_symbols (struct objfile *objfile)
4890 {
4891 return 1;
4892 }
4893
4894 const struct quick_symbol_functions dwarf2_gdb_index_functions =
4895 {
4896 dw2_has_symbols,
4897 dw2_find_last_source_symtab,
4898 dw2_forget_cached_source_info,
4899 dw2_map_symtabs_matching_filename,
4900 dw2_lookup_symbol,
4901 NULL,
4902 dw2_print_stats,
4903 dw2_dump,
4904 dw2_expand_symtabs_for_function,
4905 dw2_expand_all_symtabs,
4906 dw2_expand_symtabs_with_fullname,
4907 dw2_map_matching_symbols,
4908 dw2_expand_symtabs_matching,
4909 dw2_find_pc_sect_compunit_symtab,
4910 NULL,
4911 dw2_map_symbol_filenames
4912 };
4913
4914 /* DWARF-5 debug_names reader. */
4915
4916 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4917 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4918
4919 /* A helper function that reads the .debug_names section in SECTION
4920 and fills in MAP. FILENAME is the name of the file containing the
4921 section; it is used for error reporting.
4922
4923 Returns true if all went well, false otherwise. */
4924
4925 static bool
4926 read_debug_names_from_section (struct objfile *objfile,
4927 const char *filename,
4928 struct dwarf2_section_info *section,
4929 mapped_debug_names &map)
4930 {
4931 if (section->empty ())
4932 return false;
4933
4934 /* Older elfutils strip versions could keep the section in the main
4935 executable while splitting it for the separate debug info file. */
4936 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4937 return false;
4938
4939 section->read (objfile);
4940
4941 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4942
4943 const gdb_byte *addr = section->buffer;
4944
4945 bfd *const abfd = section->get_bfd_owner ();
4946
4947 unsigned int bytes_read;
4948 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4949 addr += bytes_read;
4950
4951 map.dwarf5_is_dwarf64 = bytes_read != 4;
4952 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4953 if (bytes_read + length != section->size)
4954 {
4955 /* There may be multiple per-CU indices. */
4956 warning (_("Section .debug_names in %s length %s does not match "
4957 "section length %s, ignoring .debug_names."),
4958 filename, plongest (bytes_read + length),
4959 pulongest (section->size));
4960 return false;
4961 }
4962
4963 /* The version number. */
4964 uint16_t version = read_2_bytes (abfd, addr);
4965 addr += 2;
4966 if (version != 5)
4967 {
4968 warning (_("Section .debug_names in %s has unsupported version %d, "
4969 "ignoring .debug_names."),
4970 filename, version);
4971 return false;
4972 }
4973
4974 /* Padding. */
4975 uint16_t padding = read_2_bytes (abfd, addr);
4976 addr += 2;
4977 if (padding != 0)
4978 {
4979 warning (_("Section .debug_names in %s has unsupported padding %d, "
4980 "ignoring .debug_names."),
4981 filename, padding);
4982 return false;
4983 }
4984
4985 /* comp_unit_count - The number of CUs in the CU list. */
4986 map.cu_count = read_4_bytes (abfd, addr);
4987 addr += 4;
4988
4989 /* local_type_unit_count - The number of TUs in the local TU
4990 list. */
4991 map.tu_count = read_4_bytes (abfd, addr);
4992 addr += 4;
4993
4994 /* foreign_type_unit_count - The number of TUs in the foreign TU
4995 list. */
4996 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4997 addr += 4;
4998 if (foreign_tu_count != 0)
4999 {
5000 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5001 "ignoring .debug_names."),
5002 filename, static_cast<unsigned long> (foreign_tu_count));
5003 return false;
5004 }
5005
5006 /* bucket_count - The number of hash buckets in the hash lookup
5007 table. */
5008 map.bucket_count = read_4_bytes (abfd, addr);
5009 addr += 4;
5010
5011 /* name_count - The number of unique names in the index. */
5012 map.name_count = read_4_bytes (abfd, addr);
5013 addr += 4;
5014
5015 /* abbrev_table_size - The size in bytes of the abbreviations
5016 table. */
5017 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5018 addr += 4;
5019
5020 /* augmentation_string_size - The size in bytes of the augmentation
5021 string. This value is rounded up to a multiple of 4. */
5022 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5023 addr += 4;
5024 map.augmentation_is_gdb = ((augmentation_string_size
5025 == sizeof (dwarf5_augmentation))
5026 && memcmp (addr, dwarf5_augmentation,
5027 sizeof (dwarf5_augmentation)) == 0);
5028 augmentation_string_size += (-augmentation_string_size) & 3;
5029 addr += augmentation_string_size;
5030
5031 /* List of CUs */
5032 map.cu_table_reordered = addr;
5033 addr += map.cu_count * map.offset_size;
5034
5035 /* List of Local TUs */
5036 map.tu_table_reordered = addr;
5037 addr += map.tu_count * map.offset_size;
5038
5039 /* Hash Lookup Table */
5040 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5041 addr += map.bucket_count * 4;
5042 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5043 addr += map.name_count * 4;
5044
5045 /* Name Table */
5046 map.name_table_string_offs_reordered = addr;
5047 addr += map.name_count * map.offset_size;
5048 map.name_table_entry_offs_reordered = addr;
5049 addr += map.name_count * map.offset_size;
5050
5051 const gdb_byte *abbrev_table_start = addr;
5052 for (;;)
5053 {
5054 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5055 addr += bytes_read;
5056 if (index_num == 0)
5057 break;
5058
5059 const auto insertpair
5060 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5061 if (!insertpair.second)
5062 {
5063 warning (_("Section .debug_names in %s has duplicate index %s, "
5064 "ignoring .debug_names."),
5065 filename, pulongest (index_num));
5066 return false;
5067 }
5068 mapped_debug_names::index_val &indexval = insertpair.first->second;
5069 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5070 addr += bytes_read;
5071
5072 for (;;)
5073 {
5074 mapped_debug_names::index_val::attr attr;
5075 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5076 addr += bytes_read;
5077 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5078 addr += bytes_read;
5079 if (attr.form == DW_FORM_implicit_const)
5080 {
5081 attr.implicit_const = read_signed_leb128 (abfd, addr,
5082 &bytes_read);
5083 addr += bytes_read;
5084 }
5085 if (attr.dw_idx == 0 && attr.form == 0)
5086 break;
5087 indexval.attr_vec.push_back (std::move (attr));
5088 }
5089 }
5090 if (addr != abbrev_table_start + abbrev_table_size)
5091 {
5092 warning (_("Section .debug_names in %s has abbreviation_table "
5093 "of size %s vs. written as %u, ignoring .debug_names."),
5094 filename, plongest (addr - abbrev_table_start),
5095 abbrev_table_size);
5096 return false;
5097 }
5098 map.entry_pool = addr;
5099
5100 return true;
5101 }
5102
5103 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5104 list. */
5105
5106 static void
5107 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5108 const mapped_debug_names &map,
5109 dwarf2_section_info &section,
5110 bool is_dwz)
5111 {
5112 if (!map.augmentation_is_gdb)
5113 {
5114 for (uint32_t i = 0; i < map.cu_count; ++i)
5115 {
5116 sect_offset sect_off
5117 = (sect_offset) (extract_unsigned_integer
5118 (map.cu_table_reordered + i * map.offset_size,
5119 map.offset_size,
5120 map.dwarf5_byte_order));
5121 /* We don't know the length of the CU, because the CU list in a
5122 .debug_names index can be incomplete, so we can't use the start of
5123 the next CU as end of this CU. We create the CUs here with length 0,
5124 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5125 dwarf2_per_cu_data *per_cu
5126 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5127 per_bfd->all_comp_units.push_back (per_cu);
5128 }
5129 }
5130
5131 sect_offset sect_off_prev;
5132 for (uint32_t i = 0; i <= map.cu_count; ++i)
5133 {
5134 sect_offset sect_off_next;
5135 if (i < map.cu_count)
5136 {
5137 sect_off_next
5138 = (sect_offset) (extract_unsigned_integer
5139 (map.cu_table_reordered + i * map.offset_size,
5140 map.offset_size,
5141 map.dwarf5_byte_order));
5142 }
5143 else
5144 sect_off_next = (sect_offset) section.size;
5145 if (i >= 1)
5146 {
5147 const ULONGEST length = sect_off_next - sect_off_prev;
5148 dwarf2_per_cu_data *per_cu
5149 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5150 sect_off_prev, length);
5151 per_bfd->all_comp_units.push_back (per_cu);
5152 }
5153 sect_off_prev = sect_off_next;
5154 }
5155 }
5156
5157 /* Read the CU list from the mapped index, and use it to create all
5158 the CU objects for this dwarf2_per_objfile. */
5159
5160 static void
5161 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5162 const mapped_debug_names &map,
5163 const mapped_debug_names &dwz_map)
5164 {
5165 gdb_assert (per_bfd->all_comp_units.empty ());
5166 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5167
5168 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5169 false /* is_dwz */);
5170
5171 if (dwz_map.cu_count == 0)
5172 return;
5173
5174 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5175 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5176 true /* is_dwz */);
5177 }
5178
5179 /* Read .debug_names. If everything went ok, initialize the "quick"
5180 elements of all the CUs and return true. Otherwise, return false. */
5181
5182 static bool
5183 dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
5184 {
5185 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5186 mapped_debug_names dwz_map;
5187 struct objfile *objfile = dwarf2_per_objfile->objfile;
5188
5189 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5190 &dwarf2_per_objfile->per_bfd->debug_names,
5191 *map))
5192 return false;
5193
5194 /* Don't use the index if it's empty. */
5195 if (map->name_count == 0)
5196 return false;
5197
5198 /* If there is a .dwz file, read it so we can get its CU list as
5199 well. */
5200 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
5201 if (dwz != NULL)
5202 {
5203 if (!read_debug_names_from_section (objfile,
5204 bfd_get_filename (dwz->dwz_bfd.get ()),
5205 &dwz->debug_names, dwz_map))
5206 {
5207 warning (_("could not read '.debug_names' section from %s; skipping"),
5208 bfd_get_filename (dwz->dwz_bfd.get ()));
5209 return false;
5210 }
5211 }
5212
5213 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
5214
5215 if (map->tu_count != 0)
5216 {
5217 /* We can only handle a single .debug_types when we have an
5218 index. */
5219 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
5220 return false;
5221
5222 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
5223
5224 create_signatured_type_table_from_debug_names
5225 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
5226 }
5227
5228 create_addrmap_from_aranges (dwarf2_per_objfile,
5229 &dwarf2_per_objfile->per_bfd->debug_aranges);
5230
5231 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5232 dwarf2_per_objfile->per_bfd->using_index = 1;
5233 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5234 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5235
5236 return true;
5237 }
5238
5239 /* Type used to manage iterating over all CUs looking for a symbol for
5240 .debug_names. */
5241
5242 class dw2_debug_names_iterator
5243 {
5244 public:
5245 dw2_debug_names_iterator (const mapped_debug_names &map,
5246 gdb::optional<block_enum> block_index,
5247 domain_enum domain,
5248 const char *name, dwarf2_per_objfile *per_objfile)
5249 : m_map (map), m_block_index (block_index), m_domain (domain),
5250 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5251 m_per_objfile (per_objfile)
5252 {}
5253
5254 dw2_debug_names_iterator (const mapped_debug_names &map,
5255 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5256 : m_map (map),
5257 m_search (search),
5258 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5259 m_per_objfile (per_objfile)
5260 {}
5261
5262 dw2_debug_names_iterator (const mapped_debug_names &map,
5263 block_enum block_index, domain_enum domain,
5264 uint32_t namei, dwarf2_per_objfile *per_objfile)
5265 : m_map (map), m_block_index (block_index), m_domain (domain),
5266 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5267 m_per_objfile (per_objfile)
5268 {}
5269
5270 /* Return the next matching CU or NULL if there are no more. */
5271 dwarf2_per_cu_data *next ();
5272
5273 private:
5274 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5275 const char *name, dwarf2_per_objfile *per_objfile);
5276 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5277 uint32_t namei, dwarf2_per_objfile *per_objfile);
5278
5279 /* The internalized form of .debug_names. */
5280 const mapped_debug_names &m_map;
5281
5282 /* If set, only look for symbols that match that block. Valid values are
5283 GLOBAL_BLOCK and STATIC_BLOCK. */
5284 const gdb::optional<block_enum> m_block_index;
5285
5286 /* The kind of symbol we're looking for. */
5287 const domain_enum m_domain = UNDEF_DOMAIN;
5288 const search_domain m_search = ALL_DOMAIN;
5289
5290 /* The list of CUs from the index entry of the symbol, or NULL if
5291 not found. */
5292 const gdb_byte *m_addr;
5293
5294 dwarf2_per_objfile *m_per_objfile;
5295 };
5296
5297 const char *
5298 mapped_debug_names::namei_to_name
5299 (uint32_t namei, dwarf2_per_objfile *dwarf2_per_objfile) const
5300 {
5301 const ULONGEST namei_string_offs
5302 = extract_unsigned_integer ((name_table_string_offs_reordered
5303 + namei * offset_size),
5304 offset_size,
5305 dwarf5_byte_order);
5306 return read_indirect_string_at_offset (dwarf2_per_objfile,
5307 namei_string_offs);
5308 }
5309
5310 /* Find a slot in .debug_names for the object named NAME. If NAME is
5311 found, return pointer to its pool data. If NAME cannot be found,
5312 return NULL. */
5313
5314 const gdb_byte *
5315 dw2_debug_names_iterator::find_vec_in_debug_names
5316 (const mapped_debug_names &map, const char *name, dwarf2_per_objfile *per_objfile)
5317 {
5318 int (*cmp) (const char *, const char *);
5319
5320 gdb::unique_xmalloc_ptr<char> without_params;
5321 if (current_language->la_language == language_cplus
5322 || current_language->la_language == language_fortran
5323 || current_language->la_language == language_d)
5324 {
5325 /* NAME is already canonical. Drop any qualifiers as
5326 .debug_names does not contain any. */
5327
5328 if (strchr (name, '(') != NULL)
5329 {
5330 without_params = cp_remove_params (name);
5331 if (without_params != NULL)
5332 name = without_params.get ();
5333 }
5334 }
5335
5336 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5337
5338 const uint32_t full_hash = dwarf5_djb_hash (name);
5339 uint32_t namei
5340 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5341 (map.bucket_table_reordered
5342 + (full_hash % map.bucket_count)), 4,
5343 map.dwarf5_byte_order);
5344 if (namei == 0)
5345 return NULL;
5346 --namei;
5347 if (namei >= map.name_count)
5348 {
5349 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5350 "[in module %s]"),
5351 namei, map.name_count,
5352 objfile_name (per_objfile->objfile));
5353 return NULL;
5354 }
5355
5356 for (;;)
5357 {
5358 const uint32_t namei_full_hash
5359 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5360 (map.hash_table_reordered + namei), 4,
5361 map.dwarf5_byte_order);
5362 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5363 return NULL;
5364
5365 if (full_hash == namei_full_hash)
5366 {
5367 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5368
5369 #if 0 /* An expensive sanity check. */
5370 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5371 {
5372 complaint (_("Wrong .debug_names hash for string at index %u "
5373 "[in module %s]"),
5374 namei, objfile_name (dwarf2_per_objfile->objfile));
5375 return NULL;
5376 }
5377 #endif
5378
5379 if (cmp (namei_string, name) == 0)
5380 {
5381 const ULONGEST namei_entry_offs
5382 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5383 + namei * map.offset_size),
5384 map.offset_size, map.dwarf5_byte_order);
5385 return map.entry_pool + namei_entry_offs;
5386 }
5387 }
5388
5389 ++namei;
5390 if (namei >= map.name_count)
5391 return NULL;
5392 }
5393 }
5394
5395 const gdb_byte *
5396 dw2_debug_names_iterator::find_vec_in_debug_names
5397 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5398 {
5399 if (namei >= map.name_count)
5400 {
5401 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5402 "[in module %s]"),
5403 namei, map.name_count,
5404 objfile_name (per_objfile->objfile));
5405 return NULL;
5406 }
5407
5408 const ULONGEST namei_entry_offs
5409 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5410 + namei * map.offset_size),
5411 map.offset_size, map.dwarf5_byte_order);
5412 return map.entry_pool + namei_entry_offs;
5413 }
5414
5415 /* See dw2_debug_names_iterator. */
5416
5417 dwarf2_per_cu_data *
5418 dw2_debug_names_iterator::next ()
5419 {
5420 if (m_addr == NULL)
5421 return NULL;
5422
5423 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5424 struct objfile *objfile = m_per_objfile->objfile;
5425 bfd *const abfd = objfile->obfd;
5426
5427 again:
5428
5429 unsigned int bytes_read;
5430 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5431 m_addr += bytes_read;
5432 if (abbrev == 0)
5433 return NULL;
5434
5435 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5436 if (indexval_it == m_map.abbrev_map.cend ())
5437 {
5438 complaint (_("Wrong .debug_names undefined abbrev code %s "
5439 "[in module %s]"),
5440 pulongest (abbrev), objfile_name (objfile));
5441 return NULL;
5442 }
5443 const mapped_debug_names::index_val &indexval = indexval_it->second;
5444 enum class symbol_linkage {
5445 unknown,
5446 static_,
5447 extern_,
5448 } symbol_linkage_ = symbol_linkage::unknown;
5449 dwarf2_per_cu_data *per_cu = NULL;
5450 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5451 {
5452 ULONGEST ull;
5453 switch (attr.form)
5454 {
5455 case DW_FORM_implicit_const:
5456 ull = attr.implicit_const;
5457 break;
5458 case DW_FORM_flag_present:
5459 ull = 1;
5460 break;
5461 case DW_FORM_udata:
5462 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5463 m_addr += bytes_read;
5464 break;
5465 case DW_FORM_ref4:
5466 ull = read_4_bytes (abfd, m_addr);
5467 m_addr += 4;
5468 break;
5469 case DW_FORM_ref8:
5470 ull = read_8_bytes (abfd, m_addr);
5471 m_addr += 8;
5472 break;
5473 case DW_FORM_ref_sig8:
5474 ull = read_8_bytes (abfd, m_addr);
5475 m_addr += 8;
5476 break;
5477 default:
5478 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5479 dwarf_form_name (attr.form),
5480 objfile_name (objfile));
5481 return NULL;
5482 }
5483 switch (attr.dw_idx)
5484 {
5485 case DW_IDX_compile_unit:
5486 /* Don't crash on bad data. */
5487 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5488 {
5489 complaint (_(".debug_names entry has bad CU index %s"
5490 " [in module %s]"),
5491 pulongest (ull),
5492 objfile_name (objfile));
5493 continue;
5494 }
5495 per_cu = per_bfd->get_cutu (ull);
5496 break;
5497 case DW_IDX_type_unit:
5498 /* Don't crash on bad data. */
5499 if (ull >= per_bfd->all_type_units.size ())
5500 {
5501 complaint (_(".debug_names entry has bad TU index %s"
5502 " [in module %s]"),
5503 pulongest (ull),
5504 objfile_name (objfile));
5505 continue;
5506 }
5507 per_cu = &per_bfd->get_tu (ull)->per_cu;
5508 break;
5509 case DW_IDX_die_offset:
5510 /* In a per-CU index (as opposed to a per-module index), index
5511 entries without CU attribute implicitly refer to the single CU. */
5512 if (per_cu == NULL)
5513 per_cu = per_bfd->get_cu (0);
5514 break;
5515 case DW_IDX_GNU_internal:
5516 if (!m_map.augmentation_is_gdb)
5517 break;
5518 symbol_linkage_ = symbol_linkage::static_;
5519 break;
5520 case DW_IDX_GNU_external:
5521 if (!m_map.augmentation_is_gdb)
5522 break;
5523 symbol_linkage_ = symbol_linkage::extern_;
5524 break;
5525 }
5526 }
5527
5528 /* Skip if already read in. */
5529 if (m_per_objfile->symtab_set_p (per_cu))
5530 goto again;
5531
5532 /* Check static vs global. */
5533 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5534 {
5535 const bool want_static = *m_block_index == STATIC_BLOCK;
5536 const bool symbol_is_static =
5537 symbol_linkage_ == symbol_linkage::static_;
5538 if (want_static != symbol_is_static)
5539 goto again;
5540 }
5541
5542 /* Match dw2_symtab_iter_next, symbol_kind
5543 and debug_names::psymbol_tag. */
5544 switch (m_domain)
5545 {
5546 case VAR_DOMAIN:
5547 switch (indexval.dwarf_tag)
5548 {
5549 case DW_TAG_variable:
5550 case DW_TAG_subprogram:
5551 /* Some types are also in VAR_DOMAIN. */
5552 case DW_TAG_typedef:
5553 case DW_TAG_structure_type:
5554 break;
5555 default:
5556 goto again;
5557 }
5558 break;
5559 case STRUCT_DOMAIN:
5560 switch (indexval.dwarf_tag)
5561 {
5562 case DW_TAG_typedef:
5563 case DW_TAG_structure_type:
5564 break;
5565 default:
5566 goto again;
5567 }
5568 break;
5569 case LABEL_DOMAIN:
5570 switch (indexval.dwarf_tag)
5571 {
5572 case 0:
5573 case DW_TAG_variable:
5574 break;
5575 default:
5576 goto again;
5577 }
5578 break;
5579 case MODULE_DOMAIN:
5580 switch (indexval.dwarf_tag)
5581 {
5582 case DW_TAG_module:
5583 break;
5584 default:
5585 goto again;
5586 }
5587 break;
5588 default:
5589 break;
5590 }
5591
5592 /* Match dw2_expand_symtabs_matching, symbol_kind and
5593 debug_names::psymbol_tag. */
5594 switch (m_search)
5595 {
5596 case VARIABLES_DOMAIN:
5597 switch (indexval.dwarf_tag)
5598 {
5599 case DW_TAG_variable:
5600 break;
5601 default:
5602 goto again;
5603 }
5604 break;
5605 case FUNCTIONS_DOMAIN:
5606 switch (indexval.dwarf_tag)
5607 {
5608 case DW_TAG_subprogram:
5609 break;
5610 default:
5611 goto again;
5612 }
5613 break;
5614 case TYPES_DOMAIN:
5615 switch (indexval.dwarf_tag)
5616 {
5617 case DW_TAG_typedef:
5618 case DW_TAG_structure_type:
5619 break;
5620 default:
5621 goto again;
5622 }
5623 break;
5624 case MODULES_DOMAIN:
5625 switch (indexval.dwarf_tag)
5626 {
5627 case DW_TAG_module:
5628 break;
5629 default:
5630 goto again;
5631 }
5632 default:
5633 break;
5634 }
5635
5636 return per_cu;
5637 }
5638
5639 static struct compunit_symtab *
5640 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5641 const char *name, domain_enum domain)
5642 {
5643 struct dwarf2_per_objfile *dwarf2_per_objfile
5644 = get_dwarf2_per_objfile (objfile);
5645
5646 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
5647 if (!mapp)
5648 {
5649 /* index is NULL if OBJF_READNOW. */
5650 return NULL;
5651 }
5652 const auto &map = *mapp;
5653
5654 dw2_debug_names_iterator iter (map, block_index, domain, name,
5655 dwarf2_per_objfile);
5656
5657 struct compunit_symtab *stab_best = NULL;
5658 struct dwarf2_per_cu_data *per_cu;
5659 while ((per_cu = iter.next ()) != NULL)
5660 {
5661 struct symbol *sym, *with_opaque = NULL;
5662 compunit_symtab *stab
5663 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5664 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5665 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5666
5667 sym = block_find_symbol (block, name, domain,
5668 block_find_non_opaque_type_preferred,
5669 &with_opaque);
5670
5671 /* Some caution must be observed with overloaded functions and
5672 methods, since the index will not contain any overload
5673 information (but NAME might contain it). */
5674
5675 if (sym != NULL
5676 && strcmp_iw (sym->search_name (), name) == 0)
5677 return stab;
5678 if (with_opaque != NULL
5679 && strcmp_iw (with_opaque->search_name (), name) == 0)
5680 stab_best = stab;
5681
5682 /* Keep looking through other CUs. */
5683 }
5684
5685 return stab_best;
5686 }
5687
5688 /* This dumps minimal information about .debug_names. It is called
5689 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5690 uses this to verify that .debug_names has been loaded. */
5691
5692 static void
5693 dw2_debug_names_dump (struct objfile *objfile)
5694 {
5695 struct dwarf2_per_objfile *dwarf2_per_objfile
5696 = get_dwarf2_per_objfile (objfile);
5697
5698 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
5699 printf_filtered (".debug_names:");
5700 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5701 printf_filtered (" exists\n");
5702 else
5703 printf_filtered (" faked for \"readnow\"\n");
5704 printf_filtered ("\n");
5705 }
5706
5707 static void
5708 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5709 const char *func_name)
5710 {
5711 struct dwarf2_per_objfile *dwarf2_per_objfile
5712 = get_dwarf2_per_objfile (objfile);
5713
5714 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5715 if (dwarf2_per_objfile->per_bfd->debug_names_table)
5716 {
5717 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5718
5719 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5720 dwarf2_per_objfile);
5721
5722 struct dwarf2_per_cu_data *per_cu;
5723 while ((per_cu = iter.next ()) != NULL)
5724 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
5725 }
5726 }
5727
5728 static void
5729 dw2_debug_names_map_matching_symbols
5730 (struct objfile *objfile,
5731 const lookup_name_info &name, domain_enum domain,
5732 int global,
5733 gdb::function_view<symbol_found_callback_ftype> callback,
5734 symbol_compare_ftype *ordered_compare)
5735 {
5736 struct dwarf2_per_objfile *dwarf2_per_objfile
5737 = get_dwarf2_per_objfile (objfile);
5738
5739 /* debug_names_table is NULL if OBJF_READNOW. */
5740 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5741 return;
5742
5743 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5744 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5745
5746 const char *match_name = name.ada ().lookup_name ().c_str ();
5747 auto matcher = [&] (const char *symname)
5748 {
5749 if (ordered_compare == nullptr)
5750 return true;
5751 return ordered_compare (symname, match_name) == 0;
5752 };
5753
5754 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5755 [&] (offset_type namei)
5756 {
5757 /* The name was matched, now expand corresponding CUs that were
5758 marked. */
5759 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5760 dwarf2_per_objfile);
5761
5762 struct dwarf2_per_cu_data *per_cu;
5763 while ((per_cu = iter.next ()) != NULL)
5764 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5765 nullptr);
5766 return true;
5767 }, dwarf2_per_objfile);
5768
5769 /* It's a shame we couldn't do this inside the
5770 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5771 that have already been expanded. Instead, this loop matches what
5772 the psymtab code does. */
5773 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5774 {
5775 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5776 if (symtab != nullptr)
5777 {
5778 const struct block *block
5779 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5780 if (!iterate_over_symbols_terminated (block, name,
5781 domain, callback))
5782 break;
5783 }
5784 }
5785 }
5786
5787 static void
5788 dw2_debug_names_expand_symtabs_matching
5789 (struct objfile *objfile,
5790 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5791 const lookup_name_info *lookup_name,
5792 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5793 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5794 enum search_domain kind)
5795 {
5796 struct dwarf2_per_objfile *dwarf2_per_objfile
5797 = get_dwarf2_per_objfile (objfile);
5798
5799 /* debug_names_table is NULL if OBJF_READNOW. */
5800 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
5801 return;
5802
5803 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
5804
5805 if (symbol_matcher == NULL && lookup_name == NULL)
5806 {
5807 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
5808 {
5809 QUIT;
5810
5811 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5812 file_matcher, expansion_notify);
5813 }
5814 return;
5815 }
5816
5817 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
5818
5819 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5820 symbol_matcher,
5821 kind, [&] (offset_type namei)
5822 {
5823 /* The name was matched, now expand corresponding CUs that were
5824 marked. */
5825 dw2_debug_names_iterator iter (map, kind, namei, dwarf2_per_objfile);
5826
5827 struct dwarf2_per_cu_data *per_cu;
5828 while ((per_cu = iter.next ()) != NULL)
5829 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5830 file_matcher, expansion_notify);
5831 return true;
5832 }, dwarf2_per_objfile);
5833 }
5834
5835 const struct quick_symbol_functions dwarf2_debug_names_functions =
5836 {
5837 dw2_has_symbols,
5838 dw2_find_last_source_symtab,
5839 dw2_forget_cached_source_info,
5840 dw2_map_symtabs_matching_filename,
5841 dw2_debug_names_lookup_symbol,
5842 NULL,
5843 dw2_print_stats,
5844 dw2_debug_names_dump,
5845 dw2_debug_names_expand_symtabs_for_function,
5846 dw2_expand_all_symtabs,
5847 dw2_expand_symtabs_with_fullname,
5848 dw2_debug_names_map_matching_symbols,
5849 dw2_debug_names_expand_symtabs_matching,
5850 dw2_find_pc_sect_compunit_symtab,
5851 NULL,
5852 dw2_map_symbol_filenames
5853 };
5854
5855 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5856 to either a dwarf2_per_bfd or dwz_file object. */
5857
5858 template <typename T>
5859 static gdb::array_view<const gdb_byte>
5860 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5861 {
5862 dwarf2_section_info *section = &section_owner->gdb_index;
5863
5864 if (section->empty ())
5865 return {};
5866
5867 /* Older elfutils strip versions could keep the section in the main
5868 executable while splitting it for the separate debug info file. */
5869 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5870 return {};
5871
5872 section->read (obj);
5873
5874 /* dwarf2_section_info::size is a bfd_size_type, while
5875 gdb::array_view works with size_t. On 32-bit hosts, with
5876 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5877 is 32-bit. So we need an explicit narrowing conversion here.
5878 This is fine, because it's impossible to allocate or mmap an
5879 array/buffer larger than what size_t can represent. */
5880 return gdb::make_array_view (section->buffer, section->size);
5881 }
5882
5883 /* Lookup the index cache for the contents of the index associated to
5884 DWARF2_OBJ. */
5885
5886 static gdb::array_view<const gdb_byte>
5887 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5888 {
5889 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5890 if (build_id == nullptr)
5891 return {};
5892
5893 return global_index_cache.lookup_gdb_index (build_id,
5894 &dwarf2_per_bfd->index_cache_res);
5895 }
5896
5897 /* Same as the above, but for DWZ. */
5898
5899 static gdb::array_view<const gdb_byte>
5900 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5901 {
5902 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5903 if (build_id == nullptr)
5904 return {};
5905
5906 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5907 }
5908
5909 /* See symfile.h. */
5910
5911 bool
5912 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
5913 {
5914 struct dwarf2_per_objfile *dwarf2_per_objfile
5915 = get_dwarf2_per_objfile (objfile);
5916
5917 /* If we're about to read full symbols, don't bother with the
5918 indices. In this case we also don't care if some other debug
5919 format is making psymtabs, because they are all about to be
5920 expanded anyway. */
5921 if ((objfile->flags & OBJF_READNOW))
5922 {
5923 dwarf2_per_objfile->per_bfd->using_index = 1;
5924 create_all_comp_units (dwarf2_per_objfile);
5925 create_all_type_units (dwarf2_per_objfile);
5926 dwarf2_per_objfile->per_bfd->quick_file_names_table
5927 = create_quick_file_names_table
5928 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
5929 dwarf2_per_objfile->resize_symtabs ();
5930
5931 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5932 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
5933 {
5934 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
5935
5936 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
5937 struct dwarf2_per_cu_quick_data);
5938 }
5939
5940 /* Return 1 so that gdb sees the "quick" functions. However,
5941 these functions will be no-ops because we will have expanded
5942 all symtabs. */
5943 *index_kind = dw_index_kind::GDB_INDEX;
5944 return true;
5945 }
5946
5947 if (dwarf2_read_debug_names (dwarf2_per_objfile))
5948 {
5949 *index_kind = dw_index_kind::DEBUG_NAMES;
5950 dwarf2_per_objfile->resize_symtabs ();
5951 return true;
5952 }
5953
5954 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5955 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5956 get_gdb_index_contents_from_section<dwz_file>))
5957 {
5958 *index_kind = dw_index_kind::GDB_INDEX;
5959 dwarf2_per_objfile->resize_symtabs ();
5960 return true;
5961 }
5962
5963 /* ... otherwise, try to find the index in the index cache. */
5964 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5965 get_gdb_index_contents_from_cache,
5966 get_gdb_index_contents_from_cache_dwz))
5967 {
5968 global_index_cache.hit ();
5969 *index_kind = dw_index_kind::GDB_INDEX;
5970 dwarf2_per_objfile->resize_symtabs ();
5971 return true;
5972 }
5973
5974 global_index_cache.miss ();
5975 return false;
5976 }
5977
5978 \f
5979
5980 /* Build a partial symbol table. */
5981
5982 void
5983 dwarf2_build_psymtabs (struct objfile *objfile)
5984 {
5985 struct dwarf2_per_objfile *dwarf2_per_objfile
5986 = get_dwarf2_per_objfile (objfile);
5987
5988 init_psymbol_list (objfile, 1024);
5989
5990 try
5991 {
5992 /* This isn't really ideal: all the data we allocate on the
5993 objfile's obstack is still uselessly kept around. However,
5994 freeing it seems unsafe. */
5995 psymtab_discarder psymtabs (objfile);
5996 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
5997 psymtabs.keep ();
5998
5999 dwarf2_per_objfile->resize_symtabs ();
6000
6001 /* (maybe) store an index in the cache. */
6002 global_index_cache.store (dwarf2_per_objfile);
6003 }
6004 catch (const gdb_exception_error &except)
6005 {
6006 exception_print (gdb_stderr, except);
6007 }
6008 }
6009
6010 /* Find the base address of the compilation unit for range lists and
6011 location lists. It will normally be specified by DW_AT_low_pc.
6012 In DWARF-3 draft 4, the base address could be overridden by
6013 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6014 compilation units with discontinuous ranges. */
6015
6016 static void
6017 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6018 {
6019 struct attribute *attr;
6020
6021 cu->base_address.reset ();
6022
6023 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6024 if (attr != nullptr)
6025 cu->base_address = attr->value_as_address ();
6026 else
6027 {
6028 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6029 if (attr != nullptr)
6030 cu->base_address = attr->value_as_address ();
6031 }
6032 }
6033
6034 /* Helper function that returns the proper abbrev section for
6035 THIS_CU. */
6036
6037 static struct dwarf2_section_info *
6038 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6039 {
6040 struct dwarf2_section_info *abbrev;
6041 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6042
6043 if (this_cu->is_dwz)
6044 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6045 else
6046 abbrev = &per_bfd->abbrev;
6047
6048 return abbrev;
6049 }
6050
6051 /* Fetch the abbreviation table offset from a comp or type unit header. */
6052
6053 static sect_offset
6054 read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6055 struct dwarf2_section_info *section,
6056 sect_offset sect_off)
6057 {
6058 bfd *abfd = section->get_bfd_owner ();
6059 const gdb_byte *info_ptr;
6060 unsigned int initial_length_size, offset_size;
6061 uint16_t version;
6062
6063 section->read (dwarf2_per_objfile->objfile);
6064 info_ptr = section->buffer + to_underlying (sect_off);
6065 read_initial_length (abfd, info_ptr, &initial_length_size);
6066 offset_size = initial_length_size == 4 ? 4 : 8;
6067 info_ptr += initial_length_size;
6068
6069 version = read_2_bytes (abfd, info_ptr);
6070 info_ptr += 2;
6071 if (version >= 5)
6072 {
6073 /* Skip unit type and address size. */
6074 info_ptr += 2;
6075 }
6076
6077 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6078 }
6079
6080 /* A partial symtab that is used only for include files. */
6081 struct dwarf2_include_psymtab : public partial_symtab
6082 {
6083 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6084 : partial_symtab (filename, objfile)
6085 {
6086 }
6087
6088 void read_symtab (struct objfile *objfile) override
6089 {
6090 /* It's an include file, no symbols to read for it.
6091 Everything is in the includer symtab. */
6092
6093 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6094 expansion of the includer psymtab. We use the dependencies[0] field to
6095 model the includer. But if we go the regular route of calling
6096 expand_psymtab here, and having expand_psymtab call expand_dependencies
6097 to expand the includer, we'll only use expand_psymtab on the includer
6098 (making it a non-toplevel psymtab), while if we expand the includer via
6099 another path, we'll use read_symtab (making it a toplevel psymtab).
6100 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6101 psymtab, and trigger read_symtab on the includer here directly. */
6102 includer ()->read_symtab (objfile);
6103 }
6104
6105 void expand_psymtab (struct objfile *objfile) override
6106 {
6107 /* This is not called by read_symtab, and should not be called by any
6108 expand_dependencies. */
6109 gdb_assert (false);
6110 }
6111
6112 bool readin_p (struct objfile *objfile) const override
6113 {
6114 return includer ()->readin_p (objfile);
6115 }
6116
6117 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6118 {
6119 return nullptr;
6120 }
6121
6122 private:
6123 partial_symtab *includer () const
6124 {
6125 /* An include psymtab has exactly one dependency: the psymtab that
6126 includes it. */
6127 gdb_assert (this->number_of_dependencies == 1);
6128 return this->dependencies[0];
6129 }
6130 };
6131
6132 /* Allocate a new partial symtab for file named NAME and mark this new
6133 partial symtab as being an include of PST. */
6134
6135 static void
6136 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6137 struct objfile *objfile)
6138 {
6139 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6140
6141 if (!IS_ABSOLUTE_PATH (subpst->filename))
6142 subpst->dirname = pst->dirname;
6143
6144 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6145 subpst->dependencies[0] = pst;
6146 subpst->number_of_dependencies = 1;
6147 }
6148
6149 /* Read the Line Number Program data and extract the list of files
6150 included by the source file represented by PST. Build an include
6151 partial symtab for each of these included files. */
6152
6153 static void
6154 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6155 struct die_info *die,
6156 dwarf2_psymtab *pst)
6157 {
6158 line_header_up lh;
6159 struct attribute *attr;
6160
6161 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6162 if (attr != nullptr)
6163 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
6164 if (lh == NULL)
6165 return; /* No linetable, so no includes. */
6166
6167 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6168 that we pass in the raw text_low here; that is ok because we're
6169 only decoding the line table to make include partial symtabs, and
6170 so the addresses aren't really used. */
6171 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6172 pst->raw_text_low (), 1);
6173 }
6174
6175 static hashval_t
6176 hash_signatured_type (const void *item)
6177 {
6178 const struct signatured_type *sig_type
6179 = (const struct signatured_type *) item;
6180
6181 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6182 return sig_type->signature;
6183 }
6184
6185 static int
6186 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6187 {
6188 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6189 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6190
6191 return lhs->signature == rhs->signature;
6192 }
6193
6194 /* Allocate a hash table for signatured types. */
6195
6196 static htab_up
6197 allocate_signatured_type_table ()
6198 {
6199 return htab_up (htab_create_alloc (41,
6200 hash_signatured_type,
6201 eq_signatured_type,
6202 NULL, xcalloc, xfree));
6203 }
6204
6205 /* A helper function to add a signatured type CU to a table. */
6206
6207 static int
6208 add_signatured_type_cu_to_table (void **slot, void *datum)
6209 {
6210 struct signatured_type *sigt = (struct signatured_type *) *slot;
6211 std::vector<signatured_type *> *all_type_units
6212 = (std::vector<signatured_type *> *) datum;
6213
6214 all_type_units->push_back (sigt);
6215
6216 return 1;
6217 }
6218
6219 /* A helper for create_debug_types_hash_table. Read types from SECTION
6220 and fill them into TYPES_HTAB. It will process only type units,
6221 therefore DW_UT_type. */
6222
6223 static void
6224 create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6225 struct dwo_file *dwo_file,
6226 dwarf2_section_info *section, htab_up &types_htab,
6227 rcuh_kind section_kind)
6228 {
6229 struct objfile *objfile = dwarf2_per_objfile->objfile;
6230 struct dwarf2_section_info *abbrev_section;
6231 bfd *abfd;
6232 const gdb_byte *info_ptr, *end_ptr;
6233
6234 abbrev_section = (dwo_file != NULL
6235 ? &dwo_file->sections.abbrev
6236 : &dwarf2_per_objfile->per_bfd->abbrev);
6237
6238 if (dwarf_read_debug)
6239 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6240 section->get_name (),
6241 abbrev_section->get_file_name ());
6242
6243 section->read (objfile);
6244 info_ptr = section->buffer;
6245
6246 if (info_ptr == NULL)
6247 return;
6248
6249 /* We can't set abfd until now because the section may be empty or
6250 not present, in which case the bfd is unknown. */
6251 abfd = section->get_bfd_owner ();
6252
6253 /* We don't use cutu_reader here because we don't need to read
6254 any dies: the signature is in the header. */
6255
6256 end_ptr = info_ptr + section->size;
6257 while (info_ptr < end_ptr)
6258 {
6259 struct signatured_type *sig_type;
6260 struct dwo_unit *dwo_tu;
6261 void **slot;
6262 const gdb_byte *ptr = info_ptr;
6263 struct comp_unit_head header;
6264 unsigned int length;
6265
6266 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6267
6268 /* Initialize it due to a false compiler warning. */
6269 header.signature = -1;
6270 header.type_cu_offset_in_tu = (cu_offset) -1;
6271
6272 /* We need to read the type's signature in order to build the hash
6273 table, but we don't need anything else just yet. */
6274
6275 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
6276 abbrev_section, ptr, section_kind);
6277
6278 length = header.get_length ();
6279
6280 /* Skip dummy type units. */
6281 if (ptr >= info_ptr + length
6282 || peek_abbrev_code (abfd, ptr) == 0
6283 || header.unit_type != DW_UT_type)
6284 {
6285 info_ptr += length;
6286 continue;
6287 }
6288
6289 if (types_htab == NULL)
6290 {
6291 if (dwo_file)
6292 types_htab = allocate_dwo_unit_table ();
6293 else
6294 types_htab = allocate_signatured_type_table ();
6295 }
6296
6297 if (dwo_file)
6298 {
6299 sig_type = NULL;
6300 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6301 struct dwo_unit);
6302 dwo_tu->dwo_file = dwo_file;
6303 dwo_tu->signature = header.signature;
6304 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6305 dwo_tu->section = section;
6306 dwo_tu->sect_off = sect_off;
6307 dwo_tu->length = length;
6308 }
6309 else
6310 {
6311 /* N.B.: type_offset is not usable if this type uses a DWO file.
6312 The real type_offset is in the DWO file. */
6313 dwo_tu = NULL;
6314 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6315 sig_type->signature = header.signature;
6316 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6317 sig_type->per_cu.is_debug_types = 1;
6318 sig_type->per_cu.section = section;
6319 sig_type->per_cu.sect_off = sect_off;
6320 sig_type->per_cu.length = length;
6321 }
6322
6323 slot = htab_find_slot (types_htab.get (),
6324 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6325 INSERT);
6326 gdb_assert (slot != NULL);
6327 if (*slot != NULL)
6328 {
6329 sect_offset dup_sect_off;
6330
6331 if (dwo_file)
6332 {
6333 const struct dwo_unit *dup_tu
6334 = (const struct dwo_unit *) *slot;
6335
6336 dup_sect_off = dup_tu->sect_off;
6337 }
6338 else
6339 {
6340 const struct signatured_type *dup_tu
6341 = (const struct signatured_type *) *slot;
6342
6343 dup_sect_off = dup_tu->per_cu.sect_off;
6344 }
6345
6346 complaint (_("debug type entry at offset %s is duplicate to"
6347 " the entry at offset %s, signature %s"),
6348 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6349 hex_string (header.signature));
6350 }
6351 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6352
6353 if (dwarf_read_debug > 1)
6354 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6355 sect_offset_str (sect_off),
6356 hex_string (header.signature));
6357
6358 info_ptr += length;
6359 }
6360 }
6361
6362 /* Create the hash table of all entries in the .debug_types
6363 (or .debug_types.dwo) section(s).
6364 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6365 otherwise it is NULL.
6366
6367 The result is a pointer to the hash table or NULL if there are no types.
6368
6369 Note: This function processes DWO files only, not DWP files. */
6370
6371 static void
6372 create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6373 struct dwo_file *dwo_file,
6374 gdb::array_view<dwarf2_section_info> type_sections,
6375 htab_up &types_htab)
6376 {
6377 for (dwarf2_section_info &section : type_sections)
6378 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
6379 types_htab, rcuh_kind::TYPE);
6380 }
6381
6382 /* Create the hash table of all entries in the .debug_types section,
6383 and initialize all_type_units.
6384 The result is zero if there is an error (e.g. missing .debug_types section),
6385 otherwise non-zero. */
6386
6387 static int
6388 create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6389 {
6390 htab_up types_htab;
6391
6392 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6393 &dwarf2_per_objfile->per_bfd->info, types_htab,
6394 rcuh_kind::COMPILE);
6395 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6396 dwarf2_per_objfile->per_bfd->types, types_htab);
6397 if (types_htab == NULL)
6398 {
6399 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
6400 return 0;
6401 }
6402
6403 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
6404
6405 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6406 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6407 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
6408
6409 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6410 add_signatured_type_cu_to_table,
6411 &dwarf2_per_objfile->per_bfd->all_type_units);
6412
6413 return 1;
6414 }
6415
6416 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6417 If SLOT is non-NULL, it is the entry to use in the hash table.
6418 Otherwise we find one. */
6419
6420 static struct signatured_type *
6421 add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6422 void **slot)
6423 {
6424 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6425 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6426 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6427
6428 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
6429
6430 dwarf2_per_objfile->resize_symtabs ();
6431
6432 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6433 sig_type->signature = sig;
6434 sig_type->per_cu.is_debug_types = 1;
6435 if (dwarf2_per_objfile->per_bfd->using_index)
6436 {
6437 sig_type->per_cu.v.quick =
6438 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6439 struct dwarf2_per_cu_quick_data);
6440 }
6441
6442 if (slot == NULL)
6443 {
6444 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6445 sig_type, INSERT);
6446 }
6447 gdb_assert (*slot == NULL);
6448 *slot = sig_type;
6449 /* The rest of sig_type must be filled in by the caller. */
6450 return sig_type;
6451 }
6452
6453 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6454 Fill in SIG_ENTRY with DWO_ENTRY. */
6455
6456 static void
6457 fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
6458 struct signatured_type *sig_entry,
6459 struct dwo_unit *dwo_entry)
6460 {
6461 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6462
6463 /* Make sure we're not clobbering something we don't expect to. */
6464 gdb_assert (! sig_entry->per_cu.queued);
6465 gdb_assert (dwarf2_per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6466 if (per_bfd->using_index)
6467 {
6468 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6469 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6470 }
6471 else
6472 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6473 gdb_assert (sig_entry->signature == dwo_entry->signature);
6474 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6475 gdb_assert (sig_entry->type_unit_group == NULL);
6476 gdb_assert (sig_entry->dwo_unit == NULL);
6477
6478 sig_entry->per_cu.section = dwo_entry->section;
6479 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6480 sig_entry->per_cu.length = dwo_entry->length;
6481 sig_entry->per_cu.reading_dwo_directly = 1;
6482 sig_entry->per_cu.per_bfd = per_bfd;
6483 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6484 sig_entry->dwo_unit = dwo_entry;
6485 }
6486
6487 /* Subroutine of lookup_signatured_type.
6488 If we haven't read the TU yet, create the signatured_type data structure
6489 for a TU to be read in directly from a DWO file, bypassing the stub.
6490 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6491 using .gdb_index, then when reading a CU we want to stay in the DWO file
6492 containing that CU. Otherwise we could end up reading several other DWO
6493 files (due to comdat folding) to process the transitive closure of all the
6494 mentioned TUs, and that can be slow. The current DWO file will have every
6495 type signature that it needs.
6496 We only do this for .gdb_index because in the psymtab case we already have
6497 to read all the DWOs to build the type unit groups. */
6498
6499 static struct signatured_type *
6500 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6501 {
6502 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6503 struct dwo_file *dwo_file;
6504 struct dwo_unit find_dwo_entry, *dwo_entry;
6505 struct signatured_type find_sig_entry, *sig_entry;
6506 void **slot;
6507
6508 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6509
6510 /* If TU skeletons have been removed then we may not have read in any
6511 TUs yet. */
6512 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6513 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6514
6515 /* We only ever need to read in one copy of a signatured type.
6516 Use the global signatured_types array to do our own comdat-folding
6517 of types. If this is the first time we're reading this TU, and
6518 the TU has an entry in .gdb_index, replace the recorded data from
6519 .gdb_index with this TU. */
6520
6521 find_sig_entry.signature = sig;
6522 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6523 &find_sig_entry, INSERT);
6524 sig_entry = (struct signatured_type *) *slot;
6525
6526 /* We can get here with the TU already read, *or* in the process of being
6527 read. Don't reassign the global entry to point to this DWO if that's
6528 the case. Also note that if the TU is already being read, it may not
6529 have come from a DWO, the program may be a mix of Fission-compiled
6530 code and non-Fission-compiled code. */
6531
6532 /* Have we already tried to read this TU?
6533 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6534 needn't exist in the global table yet). */
6535 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6536 return sig_entry;
6537
6538 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6539 dwo_unit of the TU itself. */
6540 dwo_file = cu->dwo_unit->dwo_file;
6541
6542 /* Ok, this is the first time we're reading this TU. */
6543 if (dwo_file->tus == NULL)
6544 return NULL;
6545 find_dwo_entry.signature = sig;
6546 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6547 &find_dwo_entry);
6548 if (dwo_entry == NULL)
6549 return NULL;
6550
6551 /* If the global table doesn't have an entry for this TU, add one. */
6552 if (sig_entry == NULL)
6553 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6554
6555 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6556 sig_entry->per_cu.tu_read = 1;
6557 return sig_entry;
6558 }
6559
6560 /* Subroutine of lookup_signatured_type.
6561 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6562 then try the DWP file. If the TU stub (skeleton) has been removed then
6563 it won't be in .gdb_index. */
6564
6565 static struct signatured_type *
6566 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6567 {
6568 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6569 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
6570 struct dwo_unit *dwo_entry;
6571 struct signatured_type find_sig_entry, *sig_entry;
6572 void **slot;
6573
6574 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
6575 gdb_assert (dwp_file != NULL);
6576
6577 /* If TU skeletons have been removed then we may not have read in any
6578 TUs yet. */
6579 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6580 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6581
6582 find_sig_entry.signature = sig;
6583 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6584 &find_sig_entry, INSERT);
6585 sig_entry = (struct signatured_type *) *slot;
6586
6587 /* Have we already tried to read this TU?
6588 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6589 needn't exist in the global table yet). */
6590 if (sig_entry != NULL)
6591 return sig_entry;
6592
6593 if (dwp_file->tus == NULL)
6594 return NULL;
6595 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
6596 sig, 1 /* is_debug_types */);
6597 if (dwo_entry == NULL)
6598 return NULL;
6599
6600 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6601 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
6602
6603 return sig_entry;
6604 }
6605
6606 /* Lookup a signature based type for DW_FORM_ref_sig8.
6607 Returns NULL if signature SIG is not present in the table.
6608 It is up to the caller to complain about this. */
6609
6610 static struct signatured_type *
6611 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6612 {
6613 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6614
6615 if (cu->dwo_unit
6616 && dwarf2_per_objfile->per_bfd->using_index)
6617 {
6618 /* We're in a DWO/DWP file, and we're using .gdb_index.
6619 These cases require special processing. */
6620 if (get_dwp_file (dwarf2_per_objfile) == NULL)
6621 return lookup_dwo_signatured_type (cu, sig);
6622 else
6623 return lookup_dwp_signatured_type (cu, sig);
6624 }
6625 else
6626 {
6627 struct signatured_type find_entry, *entry;
6628
6629 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6630 return NULL;
6631 find_entry.signature = sig;
6632 entry = ((struct signatured_type *)
6633 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6634 &find_entry));
6635 return entry;
6636 }
6637 }
6638
6639 /* Low level DIE reading support. */
6640
6641 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6642
6643 static void
6644 init_cu_die_reader (struct die_reader_specs *reader,
6645 struct dwarf2_cu *cu,
6646 struct dwarf2_section_info *section,
6647 struct dwo_file *dwo_file,
6648 struct abbrev_table *abbrev_table)
6649 {
6650 gdb_assert (section->readin && section->buffer != NULL);
6651 reader->abfd = section->get_bfd_owner ();
6652 reader->cu = cu;
6653 reader->dwo_file = dwo_file;
6654 reader->die_section = section;
6655 reader->buffer = section->buffer;
6656 reader->buffer_end = section->buffer + section->size;
6657 reader->abbrev_table = abbrev_table;
6658 }
6659
6660 /* Subroutine of cutu_reader to simplify it.
6661 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6662 There's just a lot of work to do, and cutu_reader is big enough
6663 already.
6664
6665 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6666 from it to the DIE in the DWO. If NULL we are skipping the stub.
6667 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6668 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6669 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6670 STUB_COMP_DIR may be non-NULL.
6671 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6672 are filled in with the info of the DIE from the DWO file.
6673 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6674 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6675 kept around for at least as long as *RESULT_READER.
6676
6677 The result is non-zero if a valid (non-dummy) DIE was found. */
6678
6679 static int
6680 read_cutu_die_from_dwo (dwarf2_cu *cu,
6681 struct dwo_unit *dwo_unit,
6682 struct die_info *stub_comp_unit_die,
6683 const char *stub_comp_dir,
6684 struct die_reader_specs *result_reader,
6685 const gdb_byte **result_info_ptr,
6686 struct die_info **result_comp_unit_die,
6687 abbrev_table_up *result_dwo_abbrev_table)
6688 {
6689 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6690 dwarf2_per_cu_data *per_cu = cu->per_cu;
6691 struct objfile *objfile = dwarf2_per_objfile->objfile;
6692 bfd *abfd;
6693 const gdb_byte *begin_info_ptr, *info_ptr;
6694 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6695 int i,num_extra_attrs;
6696 struct dwarf2_section_info *dwo_abbrev_section;
6697 struct die_info *comp_unit_die;
6698
6699 /* At most one of these may be provided. */
6700 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6701
6702 /* These attributes aren't processed until later:
6703 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6704 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6705 referenced later. However, these attributes are found in the stub
6706 which we won't have later. In order to not impose this complication
6707 on the rest of the code, we read them here and copy them to the
6708 DWO CU/TU die. */
6709
6710 stmt_list = NULL;
6711 low_pc = NULL;
6712 high_pc = NULL;
6713 ranges = NULL;
6714 comp_dir = NULL;
6715
6716 if (stub_comp_unit_die != NULL)
6717 {
6718 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6719 DWO file. */
6720 if (!per_cu->is_debug_types)
6721 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6722 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6723 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6724 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6725 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6726
6727 cu->addr_base = stub_comp_unit_die->addr_base ();
6728
6729 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6730 here (if needed). We need the value before we can process
6731 DW_AT_ranges. */
6732 cu->ranges_base = stub_comp_unit_die->ranges_base ();
6733 }
6734 else if (stub_comp_dir != NULL)
6735 {
6736 /* Reconstruct the comp_dir attribute to simplify the code below. */
6737 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
6738 comp_dir->name = DW_AT_comp_dir;
6739 comp_dir->form = DW_FORM_string;
6740 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6741 DW_STRING (comp_dir) = stub_comp_dir;
6742 }
6743
6744 /* Set up for reading the DWO CU/TU. */
6745 cu->dwo_unit = dwo_unit;
6746 dwarf2_section_info *section = dwo_unit->section;
6747 section->read (objfile);
6748 abfd = section->get_bfd_owner ();
6749 begin_info_ptr = info_ptr = (section->buffer
6750 + to_underlying (dwo_unit->sect_off));
6751 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6752
6753 if (per_cu->is_debug_types)
6754 {
6755 signatured_type *sig_type = (struct signatured_type *) per_cu;
6756
6757 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6758 &cu->header, section,
6759 dwo_abbrev_section,
6760 info_ptr, rcuh_kind::TYPE);
6761 /* This is not an assert because it can be caused by bad debug info. */
6762 if (sig_type->signature != cu->header.signature)
6763 {
6764 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6765 " TU at offset %s [in module %s]"),
6766 hex_string (sig_type->signature),
6767 hex_string (cu->header.signature),
6768 sect_offset_str (dwo_unit->sect_off),
6769 bfd_get_filename (abfd));
6770 }
6771 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6772 /* For DWOs coming from DWP files, we don't know the CU length
6773 nor the type's offset in the TU until now. */
6774 dwo_unit->length = cu->header.get_length ();
6775 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6776
6777 /* Establish the type offset that can be used to lookup the type.
6778 For DWO files, we don't know it until now. */
6779 sig_type->type_offset_in_section
6780 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6781 }
6782 else
6783 {
6784 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6785 &cu->header, section,
6786 dwo_abbrev_section,
6787 info_ptr, rcuh_kind::COMPILE);
6788 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6789 /* For DWOs coming from DWP files, we don't know the CU length
6790 until now. */
6791 dwo_unit->length = cu->header.get_length ();
6792 }
6793
6794 *result_dwo_abbrev_table
6795 = abbrev_table::read (objfile, dwo_abbrev_section,
6796 cu->header.abbrev_sect_off);
6797 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6798 result_dwo_abbrev_table->get ());
6799
6800 /* Read in the die, but leave space to copy over the attributes
6801 from the stub. This has the benefit of simplifying the rest of
6802 the code - all the work to maintain the illusion of a single
6803 DW_TAG_{compile,type}_unit DIE is done here. */
6804 num_extra_attrs = ((stmt_list != NULL)
6805 + (low_pc != NULL)
6806 + (high_pc != NULL)
6807 + (ranges != NULL)
6808 + (comp_dir != NULL));
6809 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6810 num_extra_attrs);
6811
6812 /* Copy over the attributes from the stub to the DIE we just read in. */
6813 comp_unit_die = *result_comp_unit_die;
6814 i = comp_unit_die->num_attrs;
6815 if (stmt_list != NULL)
6816 comp_unit_die->attrs[i++] = *stmt_list;
6817 if (low_pc != NULL)
6818 comp_unit_die->attrs[i++] = *low_pc;
6819 if (high_pc != NULL)
6820 comp_unit_die->attrs[i++] = *high_pc;
6821 if (ranges != NULL)
6822 comp_unit_die->attrs[i++] = *ranges;
6823 if (comp_dir != NULL)
6824 comp_unit_die->attrs[i++] = *comp_dir;
6825 comp_unit_die->num_attrs += num_extra_attrs;
6826
6827 if (dwarf_die_debug)
6828 {
6829 fprintf_unfiltered (gdb_stdlog,
6830 "Read die from %s@0x%x of %s:\n",
6831 section->get_name (),
6832 (unsigned) (begin_info_ptr - section->buffer),
6833 bfd_get_filename (abfd));
6834 dump_die (comp_unit_die, dwarf_die_debug);
6835 }
6836
6837 /* Skip dummy compilation units. */
6838 if (info_ptr >= begin_info_ptr + dwo_unit->length
6839 || peek_abbrev_code (abfd, info_ptr) == 0)
6840 return 0;
6841
6842 *result_info_ptr = info_ptr;
6843 return 1;
6844 }
6845
6846 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6847 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6848 signature is part of the header. */
6849 static gdb::optional<ULONGEST>
6850 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6851 {
6852 if (cu->header.version >= 5)
6853 return cu->header.signature;
6854 struct attribute *attr;
6855 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6856 if (attr == nullptr)
6857 return gdb::optional<ULONGEST> ();
6858 return DW_UNSND (attr);
6859 }
6860
6861 /* Subroutine of cutu_reader to simplify it.
6862 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6863 Returns NULL if the specified DWO unit cannot be found. */
6864
6865 static struct dwo_unit *
6866 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6867 {
6868 dwarf2_per_cu_data *per_cu = cu->per_cu;
6869 struct dwo_unit *dwo_unit;
6870 const char *comp_dir;
6871
6872 gdb_assert (cu != NULL);
6873
6874 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6875 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6876 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6877
6878 if (per_cu->is_debug_types)
6879 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6880 else
6881 {
6882 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6883
6884 if (!signature.has_value ())
6885 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6886 " [in module %s]"),
6887 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6888
6889 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6890 }
6891
6892 return dwo_unit;
6893 }
6894
6895 /* Subroutine of cutu_reader to simplify it.
6896 See it for a description of the parameters.
6897 Read a TU directly from a DWO file, bypassing the stub. */
6898
6899 void
6900 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6901 dwarf2_per_objfile *per_objfile,
6902 dwarf2_cu *existing_cu)
6903 {
6904 struct signatured_type *sig_type;
6905
6906 /* Verify we can do the following downcast, and that we have the
6907 data we need. */
6908 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6909 sig_type = (struct signatured_type *) this_cu;
6910 gdb_assert (sig_type->dwo_unit != NULL);
6911
6912 dwarf2_cu *cu;
6913
6914 if (existing_cu != nullptr)
6915 {
6916 cu = existing_cu;
6917 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6918 /* There's no need to do the rereading_dwo_cu handling that
6919 cutu_reader does since we don't read the stub. */
6920 }
6921 else
6922 {
6923 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6924 in per_objfile yet. */
6925 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6926 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6927 cu = m_new_cu.get ();
6928 }
6929
6930 /* A future optimization, if needed, would be to use an existing
6931 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6932 could share abbrev tables. */
6933
6934 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6935 NULL /* stub_comp_unit_die */,
6936 sig_type->dwo_unit->dwo_file->comp_dir,
6937 this, &info_ptr,
6938 &comp_unit_die,
6939 &m_dwo_abbrev_table) == 0)
6940 {
6941 /* Dummy die. */
6942 dummy_p = true;
6943 }
6944 }
6945
6946 /* Initialize a CU (or TU) and read its DIEs.
6947 If the CU defers to a DWO file, read the DWO file as well.
6948
6949 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6950 Otherwise the table specified in the comp unit header is read in and used.
6951 This is an optimization for when we already have the abbrev table.
6952
6953 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6954 allocated. */
6955
6956 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6957 dwarf2_per_objfile *dwarf2_per_objfile,
6958 struct abbrev_table *abbrev_table,
6959 dwarf2_cu *existing_cu,
6960 bool skip_partial)
6961 : die_reader_specs {},
6962 m_this_cu (this_cu)
6963 {
6964 struct objfile *objfile = dwarf2_per_objfile->objfile;
6965 struct dwarf2_section_info *section = this_cu->section;
6966 bfd *abfd = section->get_bfd_owner ();
6967 const gdb_byte *begin_info_ptr;
6968 struct signatured_type *sig_type = NULL;
6969 struct dwarf2_section_info *abbrev_section;
6970 /* Non-zero if CU currently points to a DWO file and we need to
6971 reread it. When this happens we need to reread the skeleton die
6972 before we can reread the DWO file (this only applies to CUs, not TUs). */
6973 int rereading_dwo_cu = 0;
6974
6975 if (dwarf_die_debug)
6976 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6977 this_cu->is_debug_types ? "type" : "comp",
6978 sect_offset_str (this_cu->sect_off));
6979
6980 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6981 file (instead of going through the stub), short-circuit all of this. */
6982 if (this_cu->reading_dwo_directly)
6983 {
6984 /* Narrow down the scope of possibilities to have to understand. */
6985 gdb_assert (this_cu->is_debug_types);
6986 gdb_assert (abbrev_table == NULL);
6987 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, existing_cu);
6988 return;
6989 }
6990
6991 /* This is cheap if the section is already read in. */
6992 section->read (objfile);
6993
6994 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6995
6996 abbrev_section = get_abbrev_section_for_cu (this_cu);
6997
6998 dwarf2_cu *cu;
6999
7000 if (existing_cu != nullptr)
7001 {
7002 cu = existing_cu;
7003 /* If this CU is from a DWO file we need to start over, we need to
7004 refetch the attributes from the skeleton CU.
7005 This could be optimized by retrieving those attributes from when we
7006 were here the first time: the previous comp_unit_die was stored in
7007 comp_unit_obstack. But there's no data yet that we need this
7008 optimization. */
7009 if (cu->dwo_unit != NULL)
7010 rereading_dwo_cu = 1;
7011 }
7012 else
7013 {
7014 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7015 in per_objfile yet. */
7016 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
7017 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7018 cu = m_new_cu.get ();
7019 }
7020
7021 /* Get the header. */
7022 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7023 {
7024 /* We already have the header, there's no need to read it in again. */
7025 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7026 }
7027 else
7028 {
7029 if (this_cu->is_debug_types)
7030 {
7031 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7032 &cu->header, section,
7033 abbrev_section, info_ptr,
7034 rcuh_kind::TYPE);
7035
7036 /* Since per_cu is the first member of struct signatured_type,
7037 we can go from a pointer to one to a pointer to the other. */
7038 sig_type = (struct signatured_type *) this_cu;
7039 gdb_assert (sig_type->signature == cu->header.signature);
7040 gdb_assert (sig_type->type_offset_in_tu
7041 == cu->header.type_cu_offset_in_tu);
7042 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7043
7044 /* LENGTH has not been set yet for type units if we're
7045 using .gdb_index. */
7046 this_cu->length = cu->header.get_length ();
7047
7048 /* Establish the type offset that can be used to lookup the type. */
7049 sig_type->type_offset_in_section =
7050 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7051
7052 this_cu->dwarf_version = cu->header.version;
7053 }
7054 else
7055 {
7056 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7057 &cu->header, section,
7058 abbrev_section,
7059 info_ptr,
7060 rcuh_kind::COMPILE);
7061
7062 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7063 if (this_cu->length == 0)
7064 this_cu->length = cu->header.get_length ();
7065 else
7066 gdb_assert (this_cu->length == cu->header.get_length ());
7067 this_cu->dwarf_version = cu->header.version;
7068 }
7069 }
7070
7071 /* Skip dummy compilation units. */
7072 if (info_ptr >= begin_info_ptr + this_cu->length
7073 || peek_abbrev_code (abfd, info_ptr) == 0)
7074 {
7075 dummy_p = true;
7076 return;
7077 }
7078
7079 /* If we don't have them yet, read the abbrevs for this compilation unit.
7080 And if we need to read them now, make sure they're freed when we're
7081 done. */
7082 if (abbrev_table != NULL)
7083 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7084 else
7085 {
7086 m_abbrev_table_holder
7087 = abbrev_table::read (objfile, abbrev_section,
7088 cu->header.abbrev_sect_off);
7089 abbrev_table = m_abbrev_table_holder.get ();
7090 }
7091
7092 /* Read the top level CU/TU die. */
7093 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7094 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7095
7096 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7097 {
7098 dummy_p = true;
7099 return;
7100 }
7101
7102 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7103 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7104 table from the DWO file and pass the ownership over to us. It will be
7105 referenced from READER, so we must make sure to free it after we're done
7106 with READER.
7107
7108 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7109 DWO CU, that this test will fail (the attribute will not be present). */
7110 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7111 if (dwo_name != nullptr)
7112 {
7113 struct dwo_unit *dwo_unit;
7114 struct die_info *dwo_comp_unit_die;
7115
7116 if (comp_unit_die->has_children)
7117 {
7118 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7119 " has children (offset %s) [in module %s]"),
7120 sect_offset_str (this_cu->sect_off),
7121 bfd_get_filename (abfd));
7122 }
7123 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7124 if (dwo_unit != NULL)
7125 {
7126 if (read_cutu_die_from_dwo (cu, dwo_unit,
7127 comp_unit_die, NULL,
7128 this, &info_ptr,
7129 &dwo_comp_unit_die,
7130 &m_dwo_abbrev_table) == 0)
7131 {
7132 /* Dummy die. */
7133 dummy_p = true;
7134 return;
7135 }
7136 comp_unit_die = dwo_comp_unit_die;
7137 }
7138 else
7139 {
7140 /* Yikes, we couldn't find the rest of the DIE, we only have
7141 the stub. A complaint has already been logged. There's
7142 not much more we can do except pass on the stub DIE to
7143 die_reader_func. We don't want to throw an error on bad
7144 debug info. */
7145 }
7146 }
7147 }
7148
7149 void
7150 cutu_reader::keep ()
7151 {
7152 /* Done, clean up. */
7153 gdb_assert (!dummy_p);
7154 if (m_new_cu != NULL)
7155 {
7156 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7157 now. */
7158 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7159 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7160 }
7161 }
7162
7163 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7164 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7165 assumed to have already done the lookup to find the DWO file).
7166
7167 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7168 THIS_CU->is_debug_types, but nothing else.
7169
7170 We fill in THIS_CU->length.
7171
7172 THIS_CU->cu is always freed when done.
7173 This is done in order to not leave THIS_CU->cu in a state where we have
7174 to care whether it refers to the "main" CU or the DWO CU.
7175
7176 When parent_cu is passed, it is used to provide a default value for
7177 str_offsets_base and addr_base from the parent. */
7178
7179 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7180 dwarf2_per_objfile *dwarf2_per_objfile,
7181 struct dwarf2_cu *parent_cu,
7182 struct dwo_file *dwo_file)
7183 : die_reader_specs {},
7184 m_this_cu (this_cu)
7185 {
7186 struct objfile *objfile = dwarf2_per_objfile->objfile;
7187 struct dwarf2_section_info *section = this_cu->section;
7188 bfd *abfd = section->get_bfd_owner ();
7189 struct dwarf2_section_info *abbrev_section;
7190 const gdb_byte *begin_info_ptr, *info_ptr;
7191
7192 if (dwarf_die_debug)
7193 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7194 this_cu->is_debug_types ? "type" : "comp",
7195 sect_offset_str (this_cu->sect_off));
7196
7197 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
7198
7199 abbrev_section = (dwo_file != NULL
7200 ? &dwo_file->sections.abbrev
7201 : get_abbrev_section_for_cu (this_cu));
7202
7203 /* This is cheap if the section is already read in. */
7204 section->read (objfile);
7205
7206 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
7207
7208 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7209 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7210 &m_new_cu->header, section,
7211 abbrev_section, info_ptr,
7212 (this_cu->is_debug_types
7213 ? rcuh_kind::TYPE
7214 : rcuh_kind::COMPILE));
7215
7216 if (parent_cu != nullptr)
7217 {
7218 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7219 m_new_cu->addr_base = parent_cu->addr_base;
7220 }
7221 this_cu->length = m_new_cu->header.get_length ();
7222
7223 /* Skip dummy compilation units. */
7224 if (info_ptr >= begin_info_ptr + this_cu->length
7225 || peek_abbrev_code (abfd, info_ptr) == 0)
7226 {
7227 dummy_p = true;
7228 return;
7229 }
7230
7231 m_abbrev_table_holder
7232 = abbrev_table::read (objfile, abbrev_section,
7233 m_new_cu->header.abbrev_sect_off);
7234
7235 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7236 m_abbrev_table_holder.get ());
7237 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7238 }
7239
7240 \f
7241 /* Type Unit Groups.
7242
7243 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7244 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7245 so that all types coming from the same compilation (.o file) are grouped
7246 together. A future step could be to put the types in the same symtab as
7247 the CU the types ultimately came from. */
7248
7249 static hashval_t
7250 hash_type_unit_group (const void *item)
7251 {
7252 const struct type_unit_group *tu_group
7253 = (const struct type_unit_group *) item;
7254
7255 return hash_stmt_list_entry (&tu_group->hash);
7256 }
7257
7258 static int
7259 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7260 {
7261 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7262 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7263
7264 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7265 }
7266
7267 /* Allocate a hash table for type unit groups. */
7268
7269 static htab_up
7270 allocate_type_unit_groups_table ()
7271 {
7272 return htab_up (htab_create_alloc (3,
7273 hash_type_unit_group,
7274 eq_type_unit_group,
7275 NULL, xcalloc, xfree));
7276 }
7277
7278 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7279 partial symtabs. We combine several TUs per psymtab to not let the size
7280 of any one psymtab grow too big. */
7281 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7282 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7283
7284 /* Helper routine for get_type_unit_group.
7285 Create the type_unit_group object used to hold one or more TUs. */
7286
7287 static struct type_unit_group *
7288 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7289 {
7290 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7291 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
7292 struct dwarf2_per_cu_data *per_cu;
7293 struct type_unit_group *tu_group;
7294
7295 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
7296 struct type_unit_group);
7297 per_cu = &tu_group->per_cu;
7298 per_cu->per_bfd = per_bfd;
7299
7300 if (per_bfd->using_index)
7301 {
7302 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7303 struct dwarf2_per_cu_quick_data);
7304 }
7305 else
7306 {
7307 unsigned int line_offset = to_underlying (line_offset_struct);
7308 dwarf2_psymtab *pst;
7309 std::string name;
7310
7311 /* Give the symtab a useful name for debug purposes. */
7312 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7313 name = string_printf ("<type_units_%d>",
7314 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7315 else
7316 name = string_printf ("<type_units_at_0x%x>", line_offset);
7317
7318 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
7319 pst->anonymous = true;
7320 }
7321
7322 tu_group->hash.dwo_unit = cu->dwo_unit;
7323 tu_group->hash.line_sect_off = line_offset_struct;
7324
7325 return tu_group;
7326 }
7327
7328 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7329 STMT_LIST is a DW_AT_stmt_list attribute. */
7330
7331 static struct type_unit_group *
7332 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7333 {
7334 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
7335 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7336 struct type_unit_group *tu_group;
7337 void **slot;
7338 unsigned int line_offset;
7339 struct type_unit_group type_unit_group_for_lookup;
7340
7341 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7342 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7343
7344 /* Do we need to create a new group, or can we use an existing one? */
7345
7346 if (stmt_list)
7347 {
7348 line_offset = DW_UNSND (stmt_list);
7349 ++tu_stats->nr_symtab_sharers;
7350 }
7351 else
7352 {
7353 /* Ugh, no stmt_list. Rare, but we have to handle it.
7354 We can do various things here like create one group per TU or
7355 spread them over multiple groups to split up the expansion work.
7356 To avoid worst case scenarios (too many groups or too large groups)
7357 we, umm, group them in bunches. */
7358 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7359 | (tu_stats->nr_stmt_less_type_units
7360 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7361 ++tu_stats->nr_stmt_less_type_units;
7362 }
7363
7364 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7365 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7366 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7367 &type_unit_group_for_lookup, INSERT);
7368 if (*slot != NULL)
7369 {
7370 tu_group = (struct type_unit_group *) *slot;
7371 gdb_assert (tu_group != NULL);
7372 }
7373 else
7374 {
7375 sect_offset line_offset_struct = (sect_offset) line_offset;
7376 tu_group = create_type_unit_group (cu, line_offset_struct);
7377 *slot = tu_group;
7378 ++tu_stats->nr_symtabs;
7379 }
7380
7381 return tu_group;
7382 }
7383 \f
7384 /* Partial symbol tables. */
7385
7386 /* Create a psymtab named NAME and assign it to PER_CU.
7387
7388 The caller must fill in the following details:
7389 dirname, textlow, texthigh. */
7390
7391 static dwarf2_psymtab *
7392 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7393 dwarf2_per_objfile *per_objfile,
7394 const char *name)
7395 {
7396 struct objfile *objfile = per_objfile->objfile;
7397 dwarf2_psymtab *pst;
7398
7399 pst = new dwarf2_psymtab (name, objfile, per_cu);
7400
7401 pst->psymtabs_addrmap_supported = true;
7402
7403 /* This is the glue that links PST into GDB's symbol API. */
7404 per_cu->v.psymtab = pst;
7405
7406 return pst;
7407 }
7408
7409 /* DIE reader function for process_psymtab_comp_unit. */
7410
7411 static void
7412 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7413 const gdb_byte *info_ptr,
7414 struct die_info *comp_unit_die,
7415 enum language pretend_language)
7416 {
7417 struct dwarf2_cu *cu = reader->cu;
7418 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7419 struct objfile *objfile = per_objfile->objfile;
7420 struct gdbarch *gdbarch = objfile->arch ();
7421 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7422 CORE_ADDR baseaddr;
7423 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7424 dwarf2_psymtab *pst;
7425 enum pc_bounds_kind cu_bounds_kind;
7426 const char *filename;
7427
7428 gdb_assert (! per_cu->is_debug_types);
7429
7430 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7431
7432 /* Allocate a new partial symbol table structure. */
7433 gdb::unique_xmalloc_ptr<char> debug_filename;
7434 static const char artificial[] = "<artificial>";
7435 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7436 if (filename == NULL)
7437 filename = "";
7438 else if (strcmp (filename, artificial) == 0)
7439 {
7440 debug_filename.reset (concat (artificial, "@",
7441 sect_offset_str (per_cu->sect_off),
7442 (char *) NULL));
7443 filename = debug_filename.get ();
7444 }
7445
7446 pst = create_partial_symtab (per_cu, per_objfile, filename);
7447
7448 /* This must be done before calling dwarf2_build_include_psymtabs. */
7449 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7450
7451 baseaddr = objfile->text_section_offset ();
7452
7453 dwarf2_find_base_address (comp_unit_die, cu);
7454
7455 /* Possibly set the default values of LOWPC and HIGHPC from
7456 `DW_AT_ranges'. */
7457 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7458 &best_highpc, cu, pst);
7459 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7460 {
7461 CORE_ADDR low
7462 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7463 - baseaddr);
7464 CORE_ADDR high
7465 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7466 - baseaddr - 1);
7467 /* Store the contiguous range if it is not empty; it can be
7468 empty for CUs with no code. */
7469 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7470 low, high, pst);
7471 }
7472
7473 /* Check if comp unit has_children.
7474 If so, read the rest of the partial symbols from this comp unit.
7475 If not, there's no more debug_info for this comp unit. */
7476 if (comp_unit_die->has_children)
7477 {
7478 struct partial_die_info *first_die;
7479 CORE_ADDR lowpc, highpc;
7480
7481 lowpc = ((CORE_ADDR) -1);
7482 highpc = ((CORE_ADDR) 0);
7483
7484 first_die = load_partial_dies (reader, info_ptr, 1);
7485
7486 scan_partial_symbols (first_die, &lowpc, &highpc,
7487 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7488
7489 /* If we didn't find a lowpc, set it to highpc to avoid
7490 complaints from `maint check'. */
7491 if (lowpc == ((CORE_ADDR) -1))
7492 lowpc = highpc;
7493
7494 /* If the compilation unit didn't have an explicit address range,
7495 then use the information extracted from its child dies. */
7496 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7497 {
7498 best_lowpc = lowpc;
7499 best_highpc = highpc;
7500 }
7501 }
7502 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7503 best_lowpc + baseaddr)
7504 - baseaddr);
7505 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7506 best_highpc + baseaddr)
7507 - baseaddr);
7508
7509 end_psymtab_common (objfile, pst);
7510
7511 if (!cu->per_cu->imported_symtabs_empty ())
7512 {
7513 int i;
7514 int len = cu->per_cu->imported_symtabs_size ();
7515
7516 /* Fill in 'dependencies' here; we fill in 'users' in a
7517 post-pass. */
7518 pst->number_of_dependencies = len;
7519 pst->dependencies
7520 = objfile->partial_symtabs->allocate_dependencies (len);
7521 for (i = 0; i < len; ++i)
7522 {
7523 pst->dependencies[i]
7524 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7525 }
7526
7527 cu->per_cu->imported_symtabs_free ();
7528 }
7529
7530 /* Get the list of files included in the current compilation unit,
7531 and build a psymtab for each of them. */
7532 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7533
7534 if (dwarf_read_debug)
7535 fprintf_unfiltered (gdb_stdlog,
7536 "Psymtab for %s unit @%s: %s - %s"
7537 ", %d global, %d static syms\n",
7538 per_cu->is_debug_types ? "type" : "comp",
7539 sect_offset_str (per_cu->sect_off),
7540 paddress (gdbarch, pst->text_low (objfile)),
7541 paddress (gdbarch, pst->text_high (objfile)),
7542 pst->n_global_syms, pst->n_static_syms);
7543 }
7544
7545 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7546 Process compilation unit THIS_CU for a psymtab. */
7547
7548 static void
7549 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7550 dwarf2_per_objfile *per_objfile,
7551 bool want_partial_unit,
7552 enum language pretend_language)
7553 {
7554 /* If this compilation unit was already read in, free the
7555 cached copy in order to read it in again. This is
7556 necessary because we skipped some symbols when we first
7557 read in the compilation unit (see load_partial_dies).
7558 This problem could be avoided, but the benefit is unclear. */
7559 per_objfile->remove_cu (this_cu);
7560
7561 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7562
7563 switch (reader.comp_unit_die->tag)
7564 {
7565 case DW_TAG_compile_unit:
7566 this_cu->unit_type = DW_UT_compile;
7567 break;
7568 case DW_TAG_partial_unit:
7569 this_cu->unit_type = DW_UT_partial;
7570 break;
7571 default:
7572 abort ();
7573 }
7574
7575 if (reader.dummy_p)
7576 {
7577 /* Nothing. */
7578 }
7579 else if (this_cu->is_debug_types)
7580 build_type_psymtabs_reader (&reader, reader.info_ptr,
7581 reader.comp_unit_die);
7582 else if (want_partial_unit
7583 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7584 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7585 reader.comp_unit_die,
7586 pretend_language);
7587
7588 this_cu->lang = reader.cu->language;
7589
7590 /* Age out any secondary CUs. */
7591 per_objfile->age_comp_units ();
7592 }
7593
7594 /* Reader function for build_type_psymtabs. */
7595
7596 static void
7597 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7598 const gdb_byte *info_ptr,
7599 struct die_info *type_unit_die)
7600 {
7601 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
7602 struct objfile *objfile = dwarf2_per_objfile->objfile;
7603 struct dwarf2_cu *cu = reader->cu;
7604 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7605 struct signatured_type *sig_type;
7606 struct type_unit_group *tu_group;
7607 struct attribute *attr;
7608 struct partial_die_info *first_die;
7609 CORE_ADDR lowpc, highpc;
7610 dwarf2_psymtab *pst;
7611
7612 gdb_assert (per_cu->is_debug_types);
7613 sig_type = (struct signatured_type *) per_cu;
7614
7615 if (! type_unit_die->has_children)
7616 return;
7617
7618 attr = type_unit_die->attr (DW_AT_stmt_list);
7619 tu_group = get_type_unit_group (cu, attr);
7620
7621 if (tu_group->tus == nullptr)
7622 tu_group->tus = new std::vector<signatured_type *>;
7623 tu_group->tus->push_back (sig_type);
7624
7625 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7626 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
7627 pst->anonymous = true;
7628
7629 first_die = load_partial_dies (reader, info_ptr, 1);
7630
7631 lowpc = (CORE_ADDR) -1;
7632 highpc = (CORE_ADDR) 0;
7633 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7634
7635 end_psymtab_common (objfile, pst);
7636 }
7637
7638 /* Struct used to sort TUs by their abbreviation table offset. */
7639
7640 struct tu_abbrev_offset
7641 {
7642 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7643 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7644 {}
7645
7646 signatured_type *sig_type;
7647 sect_offset abbrev_offset;
7648 };
7649
7650 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7651
7652 static bool
7653 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7654 const struct tu_abbrev_offset &b)
7655 {
7656 return a.abbrev_offset < b.abbrev_offset;
7657 }
7658
7659 /* Efficiently read all the type units.
7660 This does the bulk of the work for build_type_psymtabs.
7661
7662 The efficiency is because we sort TUs by the abbrev table they use and
7663 only read each abbrev table once. In one program there are 200K TUs
7664 sharing 8K abbrev tables.
7665
7666 The main purpose of this function is to support building the
7667 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7668 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7669 can collapse the search space by grouping them by stmt_list.
7670 The savings can be significant, in the same program from above the 200K TUs
7671 share 8K stmt_list tables.
7672
7673 FUNC is expected to call get_type_unit_group, which will create the
7674 struct type_unit_group if necessary and add it to
7675 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7676
7677 static void
7678 build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
7679 {
7680 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7681 abbrev_table_up abbrev_table;
7682 sect_offset abbrev_offset;
7683
7684 /* It's up to the caller to not call us multiple times. */
7685 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
7686
7687 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
7688 return;
7689
7690 /* TUs typically share abbrev tables, and there can be way more TUs than
7691 abbrev tables. Sort by abbrev table to reduce the number of times we
7692 read each abbrev table in.
7693 Alternatives are to punt or to maintain a cache of abbrev tables.
7694 This is simpler and efficient enough for now.
7695
7696 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7697 symtab to use). Typically TUs with the same abbrev offset have the same
7698 stmt_list value too so in practice this should work well.
7699
7700 The basic algorithm here is:
7701
7702 sort TUs by abbrev table
7703 for each TU with same abbrev table:
7704 read abbrev table if first user
7705 read TU top level DIE
7706 [IWBN if DWO skeletons had DW_AT_stmt_list]
7707 call FUNC */
7708
7709 if (dwarf_read_debug)
7710 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7711
7712 /* Sort in a separate table to maintain the order of all_type_units
7713 for .gdb_index: TU indices directly index all_type_units. */
7714 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7715 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
7716
7717 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
7718 sorted_by_abbrev.emplace_back
7719 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7720 sig_type->per_cu.section,
7721 sig_type->per_cu.sect_off));
7722
7723 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7724 sort_tu_by_abbrev_offset);
7725
7726 abbrev_offset = (sect_offset) ~(unsigned) 0;
7727
7728 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7729 {
7730 /* Switch to the next abbrev table if necessary. */
7731 if (abbrev_table == NULL
7732 || tu.abbrev_offset != abbrev_offset)
7733 {
7734 abbrev_offset = tu.abbrev_offset;
7735 abbrev_table =
7736 abbrev_table::read (dwarf2_per_objfile->objfile,
7737 &dwarf2_per_objfile->per_bfd->abbrev,
7738 abbrev_offset);
7739 ++tu_stats->nr_uniq_abbrev_tables;
7740 }
7741
7742 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
7743 abbrev_table.get (), nullptr, false);
7744 if (!reader.dummy_p)
7745 build_type_psymtabs_reader (&reader, reader.info_ptr,
7746 reader.comp_unit_die);
7747 }
7748 }
7749
7750 /* Print collected type unit statistics. */
7751
7752 static void
7753 print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
7754 {
7755 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
7756
7757 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
7758 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7759 dwarf2_per_objfile->per_bfd->all_type_units.size ());
7760 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7761 tu_stats->nr_uniq_abbrev_tables);
7762 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7763 tu_stats->nr_symtabs);
7764 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7765 tu_stats->nr_symtab_sharers);
7766 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7767 tu_stats->nr_stmt_less_type_units);
7768 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7769 tu_stats->nr_all_type_units_reallocs);
7770 }
7771
7772 /* Traversal function for build_type_psymtabs. */
7773
7774 static int
7775 build_type_psymtab_dependencies (void **slot, void *info)
7776 {
7777 struct dwarf2_per_objfile *dwarf2_per_objfile
7778 = (struct dwarf2_per_objfile *) info;
7779 struct objfile *objfile = dwarf2_per_objfile->objfile;
7780 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7781 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7782 dwarf2_psymtab *pst = per_cu->v.psymtab;
7783 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7784 int i;
7785
7786 gdb_assert (len > 0);
7787 gdb_assert (per_cu->type_unit_group_p ());
7788
7789 pst->number_of_dependencies = len;
7790 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
7791 for (i = 0; i < len; ++i)
7792 {
7793 struct signatured_type *iter = tu_group->tus->at (i);
7794 gdb_assert (iter->per_cu.is_debug_types);
7795 pst->dependencies[i] = iter->per_cu.v.psymtab;
7796 iter->type_unit_group = tu_group;
7797 }
7798
7799 delete tu_group->tus;
7800 tu_group->tus = nullptr;
7801
7802 return 1;
7803 }
7804
7805 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7806 Build partial symbol tables for the .debug_types comp-units. */
7807
7808 static void
7809 build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
7810 {
7811 if (! create_all_type_units (dwarf2_per_objfile))
7812 return;
7813
7814 build_type_psymtabs_1 (dwarf2_per_objfile);
7815 }
7816
7817 /* Traversal function for process_skeletonless_type_unit.
7818 Read a TU in a DWO file and build partial symbols for it. */
7819
7820 static int
7821 process_skeletonless_type_unit (void **slot, void *info)
7822 {
7823 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7824 struct dwarf2_per_objfile *dwarf2_per_objfile
7825 = (struct dwarf2_per_objfile *) info;
7826 struct signatured_type find_entry, *entry;
7827
7828 /* If this TU doesn't exist in the global table, add it and read it in. */
7829
7830 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7831 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7832
7833 find_entry.signature = dwo_unit->signature;
7834 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
7835 &find_entry, INSERT);
7836 /* If we've already seen this type there's nothing to do. What's happening
7837 is we're doing our own version of comdat-folding here. */
7838 if (*slot != NULL)
7839 return 1;
7840
7841 /* This does the job that create_all_type_units would have done for
7842 this TU. */
7843 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7844 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
7845 *slot = entry;
7846
7847 /* This does the job that build_type_psymtabs_1 would have done. */
7848 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, nullptr, nullptr,
7849 false);
7850 if (!reader.dummy_p)
7851 build_type_psymtabs_reader (&reader, reader.info_ptr,
7852 reader.comp_unit_die);
7853
7854 return 1;
7855 }
7856
7857 /* Traversal function for process_skeletonless_type_units. */
7858
7859 static int
7860 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7861 {
7862 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7863
7864 if (dwo_file->tus != NULL)
7865 htab_traverse_noresize (dwo_file->tus.get (),
7866 process_skeletonless_type_unit, info);
7867
7868 return 1;
7869 }
7870
7871 /* Scan all TUs of DWO files, verifying we've processed them.
7872 This is needed in case a TU was emitted without its skeleton.
7873 Note: This can't be done until we know what all the DWO files are. */
7874
7875 static void
7876 process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
7877 {
7878 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7879 if (get_dwp_file (dwarf2_per_objfile) == NULL
7880 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
7881 {
7882 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
7883 process_dwo_file_for_skeletonless_type_units,
7884 dwarf2_per_objfile);
7885 }
7886 }
7887
7888 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7889
7890 static void
7891 set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
7892 {
7893 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7894 {
7895 dwarf2_psymtab *pst = per_cu->v.psymtab;
7896
7897 if (pst == NULL)
7898 continue;
7899
7900 for (int j = 0; j < pst->number_of_dependencies; ++j)
7901 {
7902 /* Set the 'user' field only if it is not already set. */
7903 if (pst->dependencies[j]->user == NULL)
7904 pst->dependencies[j]->user = pst;
7905 }
7906 }
7907 }
7908
7909 /* Build the partial symbol table by doing a quick pass through the
7910 .debug_info and .debug_abbrev sections. */
7911
7912 static void
7913 dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
7914 {
7915 struct objfile *objfile = dwarf2_per_objfile->objfile;
7916
7917 if (dwarf_read_debug)
7918 {
7919 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
7920 objfile_name (objfile));
7921 }
7922
7923 scoped_restore restore_reading_psyms
7924 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
7925 true);
7926
7927 dwarf2_per_objfile->per_bfd->info.read (objfile);
7928
7929 /* Any cached compilation units will be linked by the per-objfile
7930 read_in_chain. Make sure to free them when we're done. */
7931 free_cached_comp_units freer (dwarf2_per_objfile);
7932
7933 build_type_psymtabs (dwarf2_per_objfile);
7934
7935 create_all_comp_units (dwarf2_per_objfile);
7936
7937 /* Create a temporary address map on a temporary obstack. We later
7938 copy this to the final obstack. */
7939 auto_obstack temp_obstack;
7940
7941 scoped_restore save_psymtabs_addrmap
7942 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
7943 addrmap_create_mutable (&temp_obstack));
7944
7945 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
7946 {
7947 if (per_cu->v.psymtab != NULL)
7948 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7949 continue;
7950 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7951 language_minimal);
7952 }
7953
7954 /* This has to wait until we read the CUs, we need the list of DWOs. */
7955 process_skeletonless_type_units (dwarf2_per_objfile);
7956
7957 /* Now that all TUs have been processed we can fill in the dependencies. */
7958 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
7959 {
7960 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
7961 build_type_psymtab_dependencies, dwarf2_per_objfile);
7962 }
7963
7964 if (dwarf_read_debug)
7965 print_tu_stats (dwarf2_per_objfile);
7966
7967 set_partial_user (dwarf2_per_objfile);
7968
7969 objfile->partial_symtabs->psymtabs_addrmap
7970 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
7971 objfile->partial_symtabs->obstack ());
7972 /* At this point we want to keep the address map. */
7973 save_psymtabs_addrmap.release ();
7974
7975 if (dwarf_read_debug)
7976 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
7977 objfile_name (objfile));
7978 }
7979
7980 /* Load the partial DIEs for a secondary CU into memory.
7981 This is also used when rereading a primary CU with load_all_dies. */
7982
7983 static void
7984 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7985 dwarf2_per_objfile *per_objfile,
7986 dwarf2_cu *existing_cu)
7987 {
7988 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7989
7990 if (!reader.dummy_p)
7991 {
7992 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7993 language_minimal);
7994
7995 /* Check if comp unit has_children.
7996 If so, read the rest of the partial symbols from this comp unit.
7997 If not, there's no more debug_info for this comp unit. */
7998 if (reader.comp_unit_die->has_children)
7999 load_partial_dies (&reader, reader.info_ptr, 0);
8000
8001 reader.keep ();
8002 }
8003 }
8004
8005 static void
8006 read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
8007 struct dwarf2_section_info *section,
8008 struct dwarf2_section_info *abbrev_section,
8009 unsigned int is_dwz)
8010 {
8011 const gdb_byte *info_ptr;
8012 struct objfile *objfile = dwarf2_per_objfile->objfile;
8013
8014 if (dwarf_read_debug)
8015 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
8016 section->get_name (),
8017 section->get_file_name ());
8018
8019 section->read (objfile);
8020
8021 info_ptr = section->buffer;
8022
8023 while (info_ptr < section->buffer + section->size)
8024 {
8025 struct dwarf2_per_cu_data *this_cu;
8026
8027 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8028
8029 comp_unit_head cu_header;
8030 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8031 abbrev_section, info_ptr,
8032 rcuh_kind::COMPILE);
8033
8034 /* Save the compilation unit for later lookup. */
8035 if (cu_header.unit_type != DW_UT_type)
8036 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
8037 else
8038 {
8039 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
8040 sig_type->signature = cu_header.signature;
8041 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8042 this_cu = &sig_type->per_cu;
8043 }
8044 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8045 this_cu->sect_off = sect_off;
8046 this_cu->length = cu_header.length + cu_header.initial_length_size;
8047 this_cu->is_dwz = is_dwz;
8048 this_cu->section = section;
8049
8050 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8051
8052 info_ptr = info_ptr + this_cu->length;
8053 }
8054 }
8055
8056 /* Create a list of all compilation units in OBJFILE.
8057 This is only done for -readnow and building partial symtabs. */
8058
8059 static void
8060 create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
8061 {
8062 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8063 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8064 &dwarf2_per_objfile->per_bfd->abbrev, 0);
8065
8066 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8067 if (dwz != NULL)
8068 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
8069 1);
8070 }
8071
8072 /* Process all loaded DIEs for compilation unit CU, starting at
8073 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8074 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8075 DW_AT_ranges). See the comments of add_partial_subprogram on how
8076 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8077
8078 static void
8079 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8080 CORE_ADDR *highpc, int set_addrmap,
8081 struct dwarf2_cu *cu)
8082 {
8083 struct partial_die_info *pdi;
8084
8085 /* Now, march along the PDI's, descending into ones which have
8086 interesting children but skipping the children of the other ones,
8087 until we reach the end of the compilation unit. */
8088
8089 pdi = first_die;
8090
8091 while (pdi != NULL)
8092 {
8093 pdi->fixup (cu);
8094
8095 /* Anonymous namespaces or modules have no name but have interesting
8096 children, so we need to look at them. Ditto for anonymous
8097 enums. */
8098
8099 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
8100 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8101 || pdi->tag == DW_TAG_imported_unit
8102 || pdi->tag == DW_TAG_inlined_subroutine)
8103 {
8104 switch (pdi->tag)
8105 {
8106 case DW_TAG_subprogram:
8107 case DW_TAG_inlined_subroutine:
8108 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8109 break;
8110 case DW_TAG_constant:
8111 case DW_TAG_variable:
8112 case DW_TAG_typedef:
8113 case DW_TAG_union_type:
8114 if (!pdi->is_declaration
8115 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8116 {
8117 add_partial_symbol (pdi, cu);
8118 }
8119 break;
8120 case DW_TAG_class_type:
8121 case DW_TAG_interface_type:
8122 case DW_TAG_structure_type:
8123 if (!pdi->is_declaration)
8124 {
8125 add_partial_symbol (pdi, cu);
8126 }
8127 if ((cu->language == language_rust
8128 || cu->language == language_cplus) && pdi->has_children)
8129 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8130 set_addrmap, cu);
8131 break;
8132 case DW_TAG_enumeration_type:
8133 if (!pdi->is_declaration)
8134 add_partial_enumeration (pdi, cu);
8135 break;
8136 case DW_TAG_base_type:
8137 case DW_TAG_subrange_type:
8138 /* File scope base type definitions are added to the partial
8139 symbol table. */
8140 add_partial_symbol (pdi, cu);
8141 break;
8142 case DW_TAG_namespace:
8143 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8144 break;
8145 case DW_TAG_module:
8146 if (!pdi->is_declaration)
8147 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8148 break;
8149 case DW_TAG_imported_unit:
8150 {
8151 struct dwarf2_per_cu_data *per_cu;
8152
8153 /* For now we don't handle imported units in type units. */
8154 if (cu->per_cu->is_debug_types)
8155 {
8156 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8157 " supported in type units [in module %s]"),
8158 objfile_name (cu->per_objfile->objfile));
8159 }
8160
8161 per_cu = dwarf2_find_containing_comp_unit
8162 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8163
8164 /* Go read the partial unit, if needed. */
8165 if (per_cu->v.psymtab == NULL)
8166 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8167 cu->language);
8168
8169 cu->per_cu->imported_symtabs_push (per_cu);
8170 }
8171 break;
8172 case DW_TAG_imported_declaration:
8173 add_partial_symbol (pdi, cu);
8174 break;
8175 default:
8176 break;
8177 }
8178 }
8179
8180 /* If the die has a sibling, skip to the sibling. */
8181
8182 pdi = pdi->die_sibling;
8183 }
8184 }
8185
8186 /* Functions used to compute the fully scoped name of a partial DIE.
8187
8188 Normally, this is simple. For C++, the parent DIE's fully scoped
8189 name is concatenated with "::" and the partial DIE's name.
8190 Enumerators are an exception; they use the scope of their parent
8191 enumeration type, i.e. the name of the enumeration type is not
8192 prepended to the enumerator.
8193
8194 There are two complexities. One is DW_AT_specification; in this
8195 case "parent" means the parent of the target of the specification,
8196 instead of the direct parent of the DIE. The other is compilers
8197 which do not emit DW_TAG_namespace; in this case we try to guess
8198 the fully qualified name of structure types from their members'
8199 linkage names. This must be done using the DIE's children rather
8200 than the children of any DW_AT_specification target. We only need
8201 to do this for structures at the top level, i.e. if the target of
8202 any DW_AT_specification (if any; otherwise the DIE itself) does not
8203 have a parent. */
8204
8205 /* Compute the scope prefix associated with PDI's parent, in
8206 compilation unit CU. The result will be allocated on CU's
8207 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8208 field. NULL is returned if no prefix is necessary. */
8209 static const char *
8210 partial_die_parent_scope (struct partial_die_info *pdi,
8211 struct dwarf2_cu *cu)
8212 {
8213 const char *grandparent_scope;
8214 struct partial_die_info *parent, *real_pdi;
8215
8216 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8217 then this means the parent of the specification DIE. */
8218
8219 real_pdi = pdi;
8220 while (real_pdi->has_specification)
8221 {
8222 auto res = find_partial_die (real_pdi->spec_offset,
8223 real_pdi->spec_is_dwz, cu);
8224 real_pdi = res.pdi;
8225 cu = res.cu;
8226 }
8227
8228 parent = real_pdi->die_parent;
8229 if (parent == NULL)
8230 return NULL;
8231
8232 if (parent->scope_set)
8233 return parent->scope;
8234
8235 parent->fixup (cu);
8236
8237 grandparent_scope = partial_die_parent_scope (parent, cu);
8238
8239 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8240 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8241 Work around this problem here. */
8242 if (cu->language == language_cplus
8243 && parent->tag == DW_TAG_namespace
8244 && strcmp (parent->name, "::") == 0
8245 && grandparent_scope == NULL)
8246 {
8247 parent->scope = NULL;
8248 parent->scope_set = 1;
8249 return NULL;
8250 }
8251
8252 /* Nested subroutines in Fortran get a prefix. */
8253 if (pdi->tag == DW_TAG_enumerator)
8254 /* Enumerators should not get the name of the enumeration as a prefix. */
8255 parent->scope = grandparent_scope;
8256 else if (parent->tag == DW_TAG_namespace
8257 || parent->tag == DW_TAG_module
8258 || parent->tag == DW_TAG_structure_type
8259 || parent->tag == DW_TAG_class_type
8260 || parent->tag == DW_TAG_interface_type
8261 || parent->tag == DW_TAG_union_type
8262 || parent->tag == DW_TAG_enumeration_type
8263 || (cu->language == language_fortran
8264 && parent->tag == DW_TAG_subprogram
8265 && pdi->tag == DW_TAG_subprogram))
8266 {
8267 if (grandparent_scope == NULL)
8268 parent->scope = parent->name;
8269 else
8270 parent->scope = typename_concat (&cu->comp_unit_obstack,
8271 grandparent_scope,
8272 parent->name, 0, cu);
8273 }
8274 else
8275 {
8276 /* FIXME drow/2004-04-01: What should we be doing with
8277 function-local names? For partial symbols, we should probably be
8278 ignoring them. */
8279 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8280 dwarf_tag_name (parent->tag),
8281 sect_offset_str (pdi->sect_off));
8282 parent->scope = grandparent_scope;
8283 }
8284
8285 parent->scope_set = 1;
8286 return parent->scope;
8287 }
8288
8289 /* Return the fully scoped name associated with PDI, from compilation unit
8290 CU. The result will be allocated with malloc. */
8291
8292 static gdb::unique_xmalloc_ptr<char>
8293 partial_die_full_name (struct partial_die_info *pdi,
8294 struct dwarf2_cu *cu)
8295 {
8296 const char *parent_scope;
8297
8298 /* If this is a template instantiation, we can not work out the
8299 template arguments from partial DIEs. So, unfortunately, we have
8300 to go through the full DIEs. At least any work we do building
8301 types here will be reused if full symbols are loaded later. */
8302 if (pdi->has_template_arguments)
8303 {
8304 pdi->fixup (cu);
8305
8306 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8307 {
8308 struct die_info *die;
8309 struct attribute attr;
8310 struct dwarf2_cu *ref_cu = cu;
8311
8312 /* DW_FORM_ref_addr is using section offset. */
8313 attr.name = (enum dwarf_attribute) 0;
8314 attr.form = DW_FORM_ref_addr;
8315 attr.u.unsnd = to_underlying (pdi->sect_off);
8316 die = follow_die_ref (NULL, &attr, &ref_cu);
8317
8318 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8319 }
8320 }
8321
8322 parent_scope = partial_die_parent_scope (pdi, cu);
8323 if (parent_scope == NULL)
8324 return NULL;
8325 else
8326 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8327 pdi->name, 0, cu));
8328 }
8329
8330 static void
8331 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8332 {
8333 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
8334 struct objfile *objfile = dwarf2_per_objfile->objfile;
8335 struct gdbarch *gdbarch = objfile->arch ();
8336 CORE_ADDR addr = 0;
8337 const char *actual_name = NULL;
8338 CORE_ADDR baseaddr;
8339
8340 baseaddr = objfile->text_section_offset ();
8341
8342 gdb::unique_xmalloc_ptr<char> built_actual_name
8343 = partial_die_full_name (pdi, cu);
8344 if (built_actual_name != NULL)
8345 actual_name = built_actual_name.get ();
8346
8347 if (actual_name == NULL)
8348 actual_name = pdi->name;
8349
8350 partial_symbol psymbol;
8351 memset (&psymbol, 0, sizeof (psymbol));
8352 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8353 psymbol.ginfo.section = -1;
8354
8355 /* The code below indicates that the psymbol should be installed by
8356 setting this. */
8357 gdb::optional<psymbol_placement> where;
8358
8359 switch (pdi->tag)
8360 {
8361 case DW_TAG_inlined_subroutine:
8362 case DW_TAG_subprogram:
8363 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8364 - baseaddr);
8365 if (pdi->is_external
8366 || cu->language == language_ada
8367 || (cu->language == language_fortran
8368 && pdi->die_parent != NULL
8369 && pdi->die_parent->tag == DW_TAG_subprogram))
8370 {
8371 /* Normally, only "external" DIEs are part of the global scope.
8372 But in Ada and Fortran, we want to be able to access nested
8373 procedures globally. So all Ada and Fortran subprograms are
8374 stored in the global scope. */
8375 where = psymbol_placement::GLOBAL;
8376 }
8377 else
8378 where = psymbol_placement::STATIC;
8379
8380 psymbol.domain = VAR_DOMAIN;
8381 psymbol.aclass = LOC_BLOCK;
8382 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8383 psymbol.ginfo.value.address = addr;
8384
8385 if (pdi->main_subprogram && actual_name != NULL)
8386 set_objfile_main_name (objfile, actual_name, cu->language);
8387 break;
8388 case DW_TAG_constant:
8389 psymbol.domain = VAR_DOMAIN;
8390 psymbol.aclass = LOC_STATIC;
8391 where = (pdi->is_external
8392 ? psymbol_placement::GLOBAL
8393 : psymbol_placement::STATIC);
8394 break;
8395 case DW_TAG_variable:
8396 if (pdi->d.locdesc)
8397 addr = decode_locdesc (pdi->d.locdesc, cu);
8398
8399 if (pdi->d.locdesc
8400 && addr == 0
8401 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
8402 {
8403 /* A global or static variable may also have been stripped
8404 out by the linker if unused, in which case its address
8405 will be nullified; do not add such variables into partial
8406 symbol table then. */
8407 }
8408 else if (pdi->is_external)
8409 {
8410 /* Global Variable.
8411 Don't enter into the minimal symbol tables as there is
8412 a minimal symbol table entry from the ELF symbols already.
8413 Enter into partial symbol table if it has a location
8414 descriptor or a type.
8415 If the location descriptor is missing, new_symbol will create
8416 a LOC_UNRESOLVED symbol, the address of the variable will then
8417 be determined from the minimal symbol table whenever the variable
8418 is referenced.
8419 The address for the partial symbol table entry is not
8420 used by GDB, but it comes in handy for debugging partial symbol
8421 table building. */
8422
8423 if (pdi->d.locdesc || pdi->has_type)
8424 {
8425 psymbol.domain = VAR_DOMAIN;
8426 psymbol.aclass = LOC_STATIC;
8427 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8428 psymbol.ginfo.value.address = addr;
8429 where = psymbol_placement::GLOBAL;
8430 }
8431 }
8432 else
8433 {
8434 int has_loc = pdi->d.locdesc != NULL;
8435
8436 /* Static Variable. Skip symbols whose value we cannot know (those
8437 without location descriptors or constant values). */
8438 if (!has_loc && !pdi->has_const_value)
8439 return;
8440
8441 psymbol.domain = VAR_DOMAIN;
8442 psymbol.aclass = LOC_STATIC;
8443 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8444 if (has_loc)
8445 psymbol.ginfo.value.address = addr;
8446 where = psymbol_placement::STATIC;
8447 }
8448 break;
8449 case DW_TAG_typedef:
8450 case DW_TAG_base_type:
8451 case DW_TAG_subrange_type:
8452 psymbol.domain = VAR_DOMAIN;
8453 psymbol.aclass = LOC_TYPEDEF;
8454 where = psymbol_placement::STATIC;
8455 break;
8456 case DW_TAG_imported_declaration:
8457 case DW_TAG_namespace:
8458 psymbol.domain = VAR_DOMAIN;
8459 psymbol.aclass = LOC_TYPEDEF;
8460 where = psymbol_placement::GLOBAL;
8461 break;
8462 case DW_TAG_module:
8463 /* With Fortran 77 there might be a "BLOCK DATA" module
8464 available without any name. If so, we skip the module as it
8465 doesn't bring any value. */
8466 if (actual_name != nullptr)
8467 {
8468 psymbol.domain = MODULE_DOMAIN;
8469 psymbol.aclass = LOC_TYPEDEF;
8470 where = psymbol_placement::GLOBAL;
8471 }
8472 break;
8473 case DW_TAG_class_type:
8474 case DW_TAG_interface_type:
8475 case DW_TAG_structure_type:
8476 case DW_TAG_union_type:
8477 case DW_TAG_enumeration_type:
8478 /* Skip external references. The DWARF standard says in the section
8479 about "Structure, Union, and Class Type Entries": "An incomplete
8480 structure, union or class type is represented by a structure,
8481 union or class entry that does not have a byte size attribute
8482 and that has a DW_AT_declaration attribute." */
8483 if (!pdi->has_byte_size && pdi->is_declaration)
8484 return;
8485
8486 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8487 static vs. global. */
8488 psymbol.domain = STRUCT_DOMAIN;
8489 psymbol.aclass = LOC_TYPEDEF;
8490 where = (cu->language == language_cplus
8491 ? psymbol_placement::GLOBAL
8492 : psymbol_placement::STATIC);
8493 break;
8494 case DW_TAG_enumerator:
8495 psymbol.domain = VAR_DOMAIN;
8496 psymbol.aclass = LOC_CONST;
8497 where = (cu->language == language_cplus
8498 ? psymbol_placement::GLOBAL
8499 : psymbol_placement::STATIC);
8500 break;
8501 default:
8502 break;
8503 }
8504
8505 if (where.has_value ())
8506 {
8507 if (built_actual_name != nullptr)
8508 actual_name = objfile->intern (actual_name);
8509 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8510 psymbol.ginfo.set_linkage_name (actual_name);
8511 else
8512 {
8513 psymbol.ginfo.set_demangled_name (actual_name,
8514 &objfile->objfile_obstack);
8515 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8516 }
8517 add_psymbol_to_list (psymbol, *where, objfile);
8518 }
8519 }
8520
8521 /* Read a partial die corresponding to a namespace; also, add a symbol
8522 corresponding to that namespace to the symbol table. NAMESPACE is
8523 the name of the enclosing namespace. */
8524
8525 static void
8526 add_partial_namespace (struct partial_die_info *pdi,
8527 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8528 int set_addrmap, struct dwarf2_cu *cu)
8529 {
8530 /* Add a symbol for the namespace. */
8531
8532 add_partial_symbol (pdi, cu);
8533
8534 /* Now scan partial symbols in that namespace. */
8535
8536 if (pdi->has_children)
8537 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8538 }
8539
8540 /* Read a partial die corresponding to a Fortran module. */
8541
8542 static void
8543 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8544 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8545 {
8546 /* Add a symbol for the namespace. */
8547
8548 add_partial_symbol (pdi, cu);
8549
8550 /* Now scan partial symbols in that module. */
8551
8552 if (pdi->has_children)
8553 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8554 }
8555
8556 /* Read a partial die corresponding to a subprogram or an inlined
8557 subprogram and create a partial symbol for that subprogram.
8558 When the CU language allows it, this routine also defines a partial
8559 symbol for each nested subprogram that this subprogram contains.
8560 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8561 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8562
8563 PDI may also be a lexical block, in which case we simply search
8564 recursively for subprograms defined inside that lexical block.
8565 Again, this is only performed when the CU language allows this
8566 type of definitions. */
8567
8568 static void
8569 add_partial_subprogram (struct partial_die_info *pdi,
8570 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8571 int set_addrmap, struct dwarf2_cu *cu)
8572 {
8573 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8574 {
8575 if (pdi->has_pc_info)
8576 {
8577 if (pdi->lowpc < *lowpc)
8578 *lowpc = pdi->lowpc;
8579 if (pdi->highpc > *highpc)
8580 *highpc = pdi->highpc;
8581 if (set_addrmap)
8582 {
8583 struct objfile *objfile = cu->per_objfile->objfile;
8584 struct gdbarch *gdbarch = objfile->arch ();
8585 CORE_ADDR baseaddr;
8586 CORE_ADDR this_highpc;
8587 CORE_ADDR this_lowpc;
8588
8589 baseaddr = objfile->text_section_offset ();
8590 this_lowpc
8591 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8592 pdi->lowpc + baseaddr)
8593 - baseaddr);
8594 this_highpc
8595 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8596 pdi->highpc + baseaddr)
8597 - baseaddr);
8598 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8599 this_lowpc, this_highpc - 1,
8600 cu->per_cu->v.psymtab);
8601 }
8602 }
8603
8604 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8605 {
8606 if (!pdi->is_declaration)
8607 /* Ignore subprogram DIEs that do not have a name, they are
8608 illegal. Do not emit a complaint at this point, we will
8609 do so when we convert this psymtab into a symtab. */
8610 if (pdi->name)
8611 add_partial_symbol (pdi, cu);
8612 }
8613 }
8614
8615 if (! pdi->has_children)
8616 return;
8617
8618 if (cu->language == language_ada || cu->language == language_fortran)
8619 {
8620 pdi = pdi->die_child;
8621 while (pdi != NULL)
8622 {
8623 pdi->fixup (cu);
8624 if (pdi->tag == DW_TAG_subprogram
8625 || pdi->tag == DW_TAG_inlined_subroutine
8626 || pdi->tag == DW_TAG_lexical_block)
8627 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8628 pdi = pdi->die_sibling;
8629 }
8630 }
8631 }
8632
8633 /* Read a partial die corresponding to an enumeration type. */
8634
8635 static void
8636 add_partial_enumeration (struct partial_die_info *enum_pdi,
8637 struct dwarf2_cu *cu)
8638 {
8639 struct partial_die_info *pdi;
8640
8641 if (enum_pdi->name != NULL)
8642 add_partial_symbol (enum_pdi, cu);
8643
8644 pdi = enum_pdi->die_child;
8645 while (pdi)
8646 {
8647 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
8648 complaint (_("malformed enumerator DIE ignored"));
8649 else
8650 add_partial_symbol (pdi, cu);
8651 pdi = pdi->die_sibling;
8652 }
8653 }
8654
8655 /* Return the initial uleb128 in the die at INFO_PTR. */
8656
8657 static unsigned int
8658 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8659 {
8660 unsigned int bytes_read;
8661
8662 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8663 }
8664
8665 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8666 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8667
8668 Return the corresponding abbrev, or NULL if the number is zero (indicating
8669 an empty DIE). In either case *BYTES_READ will be set to the length of
8670 the initial number. */
8671
8672 static struct abbrev_info *
8673 peek_die_abbrev (const die_reader_specs &reader,
8674 const gdb_byte *info_ptr, unsigned int *bytes_read)
8675 {
8676 dwarf2_cu *cu = reader.cu;
8677 bfd *abfd = cu->per_objfile->objfile->obfd;
8678 unsigned int abbrev_number
8679 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8680
8681 if (abbrev_number == 0)
8682 return NULL;
8683
8684 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8685 if (!abbrev)
8686 {
8687 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8688 " at offset %s [in module %s]"),
8689 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8690 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8691 }
8692
8693 return abbrev;
8694 }
8695
8696 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8697 Returns a pointer to the end of a series of DIEs, terminated by an empty
8698 DIE. Any children of the skipped DIEs will also be skipped. */
8699
8700 static const gdb_byte *
8701 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8702 {
8703 while (1)
8704 {
8705 unsigned int bytes_read;
8706 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8707
8708 if (abbrev == NULL)
8709 return info_ptr + bytes_read;
8710 else
8711 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8712 }
8713 }
8714
8715 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8716 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8717 abbrev corresponding to that skipped uleb128 should be passed in
8718 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8719 children. */
8720
8721 static const gdb_byte *
8722 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8723 struct abbrev_info *abbrev)
8724 {
8725 unsigned int bytes_read;
8726 struct attribute attr;
8727 bfd *abfd = reader->abfd;
8728 struct dwarf2_cu *cu = reader->cu;
8729 const gdb_byte *buffer = reader->buffer;
8730 const gdb_byte *buffer_end = reader->buffer_end;
8731 unsigned int form, i;
8732
8733 for (i = 0; i < abbrev->num_attrs; i++)
8734 {
8735 /* The only abbrev we care about is DW_AT_sibling. */
8736 if (abbrev->attrs[i].name == DW_AT_sibling)
8737 {
8738 bool ignored;
8739 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8740 &ignored);
8741 if (attr.form == DW_FORM_ref_addr)
8742 complaint (_("ignoring absolute DW_AT_sibling"));
8743 else
8744 {
8745 sect_offset off = attr.get_ref_die_offset ();
8746 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8747
8748 if (sibling_ptr < info_ptr)
8749 complaint (_("DW_AT_sibling points backwards"));
8750 else if (sibling_ptr > reader->buffer_end)
8751 reader->die_section->overflow_complaint ();
8752 else
8753 return sibling_ptr;
8754 }
8755 }
8756
8757 /* If it isn't DW_AT_sibling, skip this attribute. */
8758 form = abbrev->attrs[i].form;
8759 skip_attribute:
8760 switch (form)
8761 {
8762 case DW_FORM_ref_addr:
8763 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8764 and later it is offset sized. */
8765 if (cu->header.version == 2)
8766 info_ptr += cu->header.addr_size;
8767 else
8768 info_ptr += cu->header.offset_size;
8769 break;
8770 case DW_FORM_GNU_ref_alt:
8771 info_ptr += cu->header.offset_size;
8772 break;
8773 case DW_FORM_addr:
8774 info_ptr += cu->header.addr_size;
8775 break;
8776 case DW_FORM_data1:
8777 case DW_FORM_ref1:
8778 case DW_FORM_flag:
8779 case DW_FORM_strx1:
8780 info_ptr += 1;
8781 break;
8782 case DW_FORM_flag_present:
8783 case DW_FORM_implicit_const:
8784 break;
8785 case DW_FORM_data2:
8786 case DW_FORM_ref2:
8787 case DW_FORM_strx2:
8788 info_ptr += 2;
8789 break;
8790 case DW_FORM_strx3:
8791 info_ptr += 3;
8792 break;
8793 case DW_FORM_data4:
8794 case DW_FORM_ref4:
8795 case DW_FORM_strx4:
8796 info_ptr += 4;
8797 break;
8798 case DW_FORM_data8:
8799 case DW_FORM_ref8:
8800 case DW_FORM_ref_sig8:
8801 info_ptr += 8;
8802 break;
8803 case DW_FORM_data16:
8804 info_ptr += 16;
8805 break;
8806 case DW_FORM_string:
8807 read_direct_string (abfd, info_ptr, &bytes_read);
8808 info_ptr += bytes_read;
8809 break;
8810 case DW_FORM_sec_offset:
8811 case DW_FORM_strp:
8812 case DW_FORM_GNU_strp_alt:
8813 info_ptr += cu->header.offset_size;
8814 break;
8815 case DW_FORM_exprloc:
8816 case DW_FORM_block:
8817 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8818 info_ptr += bytes_read;
8819 break;
8820 case DW_FORM_block1:
8821 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8822 break;
8823 case DW_FORM_block2:
8824 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8825 break;
8826 case DW_FORM_block4:
8827 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8828 break;
8829 case DW_FORM_addrx:
8830 case DW_FORM_strx:
8831 case DW_FORM_sdata:
8832 case DW_FORM_udata:
8833 case DW_FORM_ref_udata:
8834 case DW_FORM_GNU_addr_index:
8835 case DW_FORM_GNU_str_index:
8836 case DW_FORM_rnglistx:
8837 case DW_FORM_loclistx:
8838 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8839 break;
8840 case DW_FORM_indirect:
8841 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8842 info_ptr += bytes_read;
8843 /* We need to continue parsing from here, so just go back to
8844 the top. */
8845 goto skip_attribute;
8846
8847 default:
8848 error (_("Dwarf Error: Cannot handle %s "
8849 "in DWARF reader [in module %s]"),
8850 dwarf_form_name (form),
8851 bfd_get_filename (abfd));
8852 }
8853 }
8854
8855 if (abbrev->has_children)
8856 return skip_children (reader, info_ptr);
8857 else
8858 return info_ptr;
8859 }
8860
8861 /* Locate ORIG_PDI's sibling.
8862 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8863
8864 static const gdb_byte *
8865 locate_pdi_sibling (const struct die_reader_specs *reader,
8866 struct partial_die_info *orig_pdi,
8867 const gdb_byte *info_ptr)
8868 {
8869 /* Do we know the sibling already? */
8870
8871 if (orig_pdi->sibling)
8872 return orig_pdi->sibling;
8873
8874 /* Are there any children to deal with? */
8875
8876 if (!orig_pdi->has_children)
8877 return info_ptr;
8878
8879 /* Skip the children the long way. */
8880
8881 return skip_children (reader, info_ptr);
8882 }
8883
8884 /* Expand this partial symbol table into a full symbol table. SELF is
8885 not NULL. */
8886
8887 void
8888 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8889 {
8890 struct dwarf2_per_objfile *dwarf2_per_objfile
8891 = get_dwarf2_per_objfile (objfile);
8892
8893 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8894
8895 /* If this psymtab is constructed from a debug-only objfile, the
8896 has_section_at_zero flag will not necessarily be correct. We
8897 can get the correct value for this flag by looking at the data
8898 associated with the (presumably stripped) associated objfile. */
8899 if (objfile->separate_debug_objfile_backlink)
8900 {
8901 struct dwarf2_per_objfile *dpo_backlink
8902 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8903
8904 dwarf2_per_objfile->per_bfd->has_section_at_zero
8905 = dpo_backlink->per_bfd->has_section_at_zero;
8906 }
8907
8908 expand_psymtab (objfile);
8909
8910 process_cu_includes (dwarf2_per_objfile);
8911 }
8912 \f
8913 /* Reading in full CUs. */
8914
8915 /* Add PER_CU to the queue. */
8916
8917 static void
8918 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8919 dwarf2_per_objfile *per_objfile,
8920 enum language pretend_language)
8921 {
8922 per_cu->queued = 1;
8923 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
8924 }
8925
8926 /* If PER_CU is not yet queued, add it to the queue.
8927 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8928 dependency.
8929 The result is non-zero if PER_CU was queued, otherwise the result is zero
8930 meaning either PER_CU is already queued or it is already loaded.
8931
8932 N.B. There is an invariant here that if a CU is queued then it is loaded.
8933 The caller is required to load PER_CU if we return non-zero. */
8934
8935 static int
8936 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8937 dwarf2_per_cu_data *per_cu,
8938 dwarf2_per_objfile *per_objfile,
8939 enum language pretend_language)
8940 {
8941 /* We may arrive here during partial symbol reading, if we need full
8942 DIEs to process an unusual case (e.g. template arguments). Do
8943 not queue PER_CU, just tell our caller to load its DIEs. */
8944 if (per_cu->per_bfd->reading_partial_symbols)
8945 {
8946 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8947
8948 if (cu == NULL || cu->dies == NULL)
8949 return 1;
8950 return 0;
8951 }
8952
8953 /* Mark the dependence relation so that we don't flush PER_CU
8954 too early. */
8955 if (dependent_cu != NULL)
8956 dwarf2_add_dependence (dependent_cu, per_cu);
8957
8958 /* If it's already on the queue, we have nothing to do. */
8959 if (per_cu->queued)
8960 return 0;
8961
8962 /* If the compilation unit is already loaded, just mark it as
8963 used. */
8964 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8965 if (cu != nullptr)
8966 {
8967 cu->last_used = 0;
8968 return 0;
8969 }
8970
8971 /* Add it to the queue. */
8972 queue_comp_unit (per_cu, per_objfile, pretend_language);
8973
8974 return 1;
8975 }
8976
8977 /* Process the queue. */
8978
8979 static void
8980 process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
8981 {
8982 if (dwarf_read_debug)
8983 {
8984 fprintf_unfiltered (gdb_stdlog,
8985 "Expanding one or more symtabs of objfile %s ...\n",
8986 objfile_name (dwarf2_per_objfile->objfile));
8987 }
8988
8989 /* The queue starts out with one item, but following a DIE reference
8990 may load a new CU, adding it to the end of the queue. */
8991 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
8992 {
8993 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
8994 dwarf2_per_cu_data *per_cu = item.per_cu;
8995
8996 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
8997 {
8998 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
8999
9000 /* Skip dummy CUs. */
9001 if (cu != nullptr)
9002 {
9003 unsigned int debug_print_threshold;
9004 char buf[100];
9005
9006 if (per_cu->is_debug_types)
9007 {
9008 struct signatured_type *sig_type =
9009 (struct signatured_type *) per_cu;
9010
9011 sprintf (buf, "TU %s at offset %s",
9012 hex_string (sig_type->signature),
9013 sect_offset_str (per_cu->sect_off));
9014 /* There can be 100s of TUs.
9015 Only print them in verbose mode. */
9016 debug_print_threshold = 2;
9017 }
9018 else
9019 {
9020 sprintf (buf, "CU at offset %s",
9021 sect_offset_str (per_cu->sect_off));
9022 debug_print_threshold = 1;
9023 }
9024
9025 if (dwarf_read_debug >= debug_print_threshold)
9026 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
9027
9028 if (per_cu->is_debug_types)
9029 process_full_type_unit (cu, item.pretend_language);
9030 else
9031 process_full_comp_unit (cu, item.pretend_language);
9032
9033 if (dwarf_read_debug >= debug_print_threshold)
9034 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9035 }
9036 }
9037
9038 per_cu->queued = 0;
9039 dwarf2_per_objfile->per_bfd->queue.pop ();
9040 }
9041
9042 if (dwarf_read_debug)
9043 {
9044 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
9045 objfile_name (dwarf2_per_objfile->objfile));
9046 }
9047 }
9048
9049 /* Read in full symbols for PST, and anything it depends on. */
9050
9051 void
9052 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9053 {
9054 gdb_assert (!readin_p (objfile));
9055
9056 expand_dependencies (objfile);
9057
9058 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9059 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9060 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9061 }
9062
9063 /* See psympriv.h. */
9064
9065 bool
9066 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9067 {
9068 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9069 return per_objfile->symtab_set_p (per_cu_data);
9070 }
9071
9072 /* See psympriv.h. */
9073
9074 compunit_symtab *
9075 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9076 {
9077 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9078 return per_objfile->get_symtab (per_cu_data);
9079 }
9080
9081 /* Trivial hash function for die_info: the hash value of a DIE
9082 is its offset in .debug_info for this objfile. */
9083
9084 static hashval_t
9085 die_hash (const void *item)
9086 {
9087 const struct die_info *die = (const struct die_info *) item;
9088
9089 return to_underlying (die->sect_off);
9090 }
9091
9092 /* Trivial comparison function for die_info structures: two DIEs
9093 are equal if they have the same offset. */
9094
9095 static int
9096 die_eq (const void *item_lhs, const void *item_rhs)
9097 {
9098 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9099 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9100
9101 return die_lhs->sect_off == die_rhs->sect_off;
9102 }
9103
9104 /* Load the DIEs associated with PER_CU into memory. */
9105
9106 static void
9107 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9108 dwarf2_per_objfile *per_objfile,
9109 bool skip_partial,
9110 enum language pretend_language)
9111 {
9112 gdb_assert (! this_cu->is_debug_types);
9113
9114 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9115 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9116 if (reader.dummy_p)
9117 return;
9118
9119 struct dwarf2_cu *cu = reader.cu;
9120 const gdb_byte *info_ptr = reader.info_ptr;
9121
9122 gdb_assert (cu->die_hash == NULL);
9123 cu->die_hash =
9124 htab_create_alloc_ex (cu->header.length / 12,
9125 die_hash,
9126 die_eq,
9127 NULL,
9128 &cu->comp_unit_obstack,
9129 hashtab_obstack_allocate,
9130 dummy_obstack_deallocate);
9131
9132 if (reader.comp_unit_die->has_children)
9133 reader.comp_unit_die->child
9134 = read_die_and_siblings (&reader, reader.info_ptr,
9135 &info_ptr, reader.comp_unit_die);
9136 cu->dies = reader.comp_unit_die;
9137 /* comp_unit_die is not stored in die_hash, no need. */
9138
9139 /* We try not to read any attributes in this function, because not
9140 all CUs needed for references have been loaded yet, and symbol
9141 table processing isn't initialized. But we have to set the CU language,
9142 or we won't be able to build types correctly.
9143 Similarly, if we do not read the producer, we can not apply
9144 producer-specific interpretation. */
9145 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9146
9147 reader.keep ();
9148 }
9149
9150 /* Add a DIE to the delayed physname list. */
9151
9152 static void
9153 add_to_method_list (struct type *type, int fnfield_index, int index,
9154 const char *name, struct die_info *die,
9155 struct dwarf2_cu *cu)
9156 {
9157 struct delayed_method_info mi;
9158 mi.type = type;
9159 mi.fnfield_index = fnfield_index;
9160 mi.index = index;
9161 mi.name = name;
9162 mi.die = die;
9163 cu->method_list.push_back (mi);
9164 }
9165
9166 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9167 "const" / "volatile". If so, decrements LEN by the length of the
9168 modifier and return true. Otherwise return false. */
9169
9170 template<size_t N>
9171 static bool
9172 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9173 {
9174 size_t mod_len = sizeof (mod) - 1;
9175 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9176 {
9177 len -= mod_len;
9178 return true;
9179 }
9180 return false;
9181 }
9182
9183 /* Compute the physnames of any methods on the CU's method list.
9184
9185 The computation of method physnames is delayed in order to avoid the
9186 (bad) condition that one of the method's formal parameters is of an as yet
9187 incomplete type. */
9188
9189 static void
9190 compute_delayed_physnames (struct dwarf2_cu *cu)
9191 {
9192 /* Only C++ delays computing physnames. */
9193 if (cu->method_list.empty ())
9194 return;
9195 gdb_assert (cu->language == language_cplus);
9196
9197 for (const delayed_method_info &mi : cu->method_list)
9198 {
9199 const char *physname;
9200 struct fn_fieldlist *fn_flp
9201 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9202 physname = dwarf2_physname (mi.name, mi.die, cu);
9203 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9204 = physname ? physname : "";
9205
9206 /* Since there's no tag to indicate whether a method is a
9207 const/volatile overload, extract that information out of the
9208 demangled name. */
9209 if (physname != NULL)
9210 {
9211 size_t len = strlen (physname);
9212
9213 while (1)
9214 {
9215 if (physname[len] == ')') /* shortcut */
9216 break;
9217 else if (check_modifier (physname, len, " const"))
9218 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9219 else if (check_modifier (physname, len, " volatile"))
9220 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9221 else
9222 break;
9223 }
9224 }
9225 }
9226
9227 /* The list is no longer needed. */
9228 cu->method_list.clear ();
9229 }
9230
9231 /* Go objects should be embedded in a DW_TAG_module DIE,
9232 and it's not clear if/how imported objects will appear.
9233 To keep Go support simple until that's worked out,
9234 go back through what we've read and create something usable.
9235 We could do this while processing each DIE, and feels kinda cleaner,
9236 but that way is more invasive.
9237 This is to, for example, allow the user to type "p var" or "b main"
9238 without having to specify the package name, and allow lookups
9239 of module.object to work in contexts that use the expression
9240 parser. */
9241
9242 static void
9243 fixup_go_packaging (struct dwarf2_cu *cu)
9244 {
9245 gdb::unique_xmalloc_ptr<char> package_name;
9246 struct pending *list;
9247 int i;
9248
9249 for (list = *cu->get_builder ()->get_global_symbols ();
9250 list != NULL;
9251 list = list->next)
9252 {
9253 for (i = 0; i < list->nsyms; ++i)
9254 {
9255 struct symbol *sym = list->symbol[i];
9256
9257 if (sym->language () == language_go
9258 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9259 {
9260 gdb::unique_xmalloc_ptr<char> this_package_name
9261 (go_symbol_package_name (sym));
9262
9263 if (this_package_name == NULL)
9264 continue;
9265 if (package_name == NULL)
9266 package_name = std::move (this_package_name);
9267 else
9268 {
9269 struct objfile *objfile = cu->per_objfile->objfile;
9270 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9271 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9272 (symbol_symtab (sym) != NULL
9273 ? symtab_to_filename_for_display
9274 (symbol_symtab (sym))
9275 : objfile_name (objfile)),
9276 this_package_name.get (), package_name.get ());
9277 }
9278 }
9279 }
9280 }
9281
9282 if (package_name != NULL)
9283 {
9284 struct objfile *objfile = cu->per_objfile->objfile;
9285 const char *saved_package_name = objfile->intern (package_name.get ());
9286 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9287 saved_package_name);
9288 struct symbol *sym;
9289
9290 sym = new (&objfile->objfile_obstack) symbol;
9291 sym->set_language (language_go, &objfile->objfile_obstack);
9292 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9293 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9294 e.g., "main" finds the "main" module and not C's main(). */
9295 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9296 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9297 SYMBOL_TYPE (sym) = type;
9298
9299 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9300 }
9301 }
9302
9303 /* Allocate a fully-qualified name consisting of the two parts on the
9304 obstack. */
9305
9306 static const char *
9307 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9308 {
9309 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9310 }
9311
9312 /* A helper that allocates a variant part to attach to a Rust enum
9313 type. OBSTACK is where the results should be allocated. TYPE is
9314 the type we're processing. DISCRIMINANT_INDEX is the index of the
9315 discriminant. It must be the index of one of the fields of TYPE.
9316 DEFAULT_INDEX is the index of the default field; or -1 if there is
9317 no default. RANGES is indexed by "effective" field number (the
9318 field index, but omitting the discriminant and default fields) and
9319 must hold the discriminant values used by the variants. Note that
9320 RANGES must have a lifetime at least as long as OBSTACK -- either
9321 already allocated on it, or static. */
9322
9323 static void
9324 alloc_rust_variant (struct obstack *obstack, struct type *type,
9325 int discriminant_index, int default_index,
9326 gdb::array_view<discriminant_range> ranges)
9327 {
9328 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9329 must be handled by the caller. */
9330 gdb_assert (discriminant_index >= 0
9331 && discriminant_index < type->num_fields ());
9332 gdb_assert (default_index == -1
9333 || (default_index >= 0 && default_index < type->num_fields ()));
9334
9335 /* We have one variant for each non-discriminant field. */
9336 int n_variants = type->num_fields () - 1;
9337
9338 variant *variants = new (obstack) variant[n_variants];
9339 int var_idx = 0;
9340 int range_idx = 0;
9341 for (int i = 0; i < type->num_fields (); ++i)
9342 {
9343 if (i == discriminant_index)
9344 continue;
9345
9346 variants[var_idx].first_field = i;
9347 variants[var_idx].last_field = i + 1;
9348
9349 /* The default field does not need a range, but other fields do.
9350 We skipped the discriminant above. */
9351 if (i != default_index)
9352 {
9353 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9354 ++range_idx;
9355 }
9356
9357 ++var_idx;
9358 }
9359
9360 gdb_assert (range_idx == ranges.size ());
9361 gdb_assert (var_idx == n_variants);
9362
9363 variant_part *part = new (obstack) variant_part;
9364 part->discriminant_index = discriminant_index;
9365 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9366 discriminant_index));
9367 part->variants = gdb::array_view<variant> (variants, n_variants);
9368
9369 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9370 gdb::array_view<variant_part> *prop_value
9371 = new (storage) gdb::array_view<variant_part> (part, 1);
9372
9373 struct dynamic_prop prop;
9374 prop.kind = PROP_VARIANT_PARTS;
9375 prop.data.variant_parts = prop_value;
9376
9377 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9378 }
9379
9380 /* Some versions of rustc emitted enums in an unusual way.
9381
9382 Ordinary enums were emitted as unions. The first element of each
9383 structure in the union was named "RUST$ENUM$DISR". This element
9384 held the discriminant.
9385
9386 These versions of Rust also implemented the "non-zero"
9387 optimization. When the enum had two values, and one is empty and
9388 the other holds a pointer that cannot be zero, the pointer is used
9389 as the discriminant, with a zero value meaning the empty variant.
9390 Here, the union's first member is of the form
9391 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9392 where the fieldnos are the indices of the fields that should be
9393 traversed in order to find the field (which may be several fields deep)
9394 and the variantname is the name of the variant of the case when the
9395 field is zero.
9396
9397 This function recognizes whether TYPE is of one of these forms,
9398 and, if so, smashes it to be a variant type. */
9399
9400 static void
9401 quirk_rust_enum (struct type *type, struct objfile *objfile)
9402 {
9403 gdb_assert (type->code () == TYPE_CODE_UNION);
9404
9405 /* We don't need to deal with empty enums. */
9406 if (type->num_fields () == 0)
9407 return;
9408
9409 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9410 if (type->num_fields () == 1
9411 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9412 {
9413 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9414
9415 /* Decode the field name to find the offset of the
9416 discriminant. */
9417 ULONGEST bit_offset = 0;
9418 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9419 while (name[0] >= '0' && name[0] <= '9')
9420 {
9421 char *tail;
9422 unsigned long index = strtoul (name, &tail, 10);
9423 name = tail;
9424 if (*name != '$'
9425 || index >= field_type->num_fields ()
9426 || (TYPE_FIELD_LOC_KIND (field_type, index)
9427 != FIELD_LOC_KIND_BITPOS))
9428 {
9429 complaint (_("Could not parse Rust enum encoding string \"%s\""
9430 "[in module %s]"),
9431 TYPE_FIELD_NAME (type, 0),
9432 objfile_name (objfile));
9433 return;
9434 }
9435 ++name;
9436
9437 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9438 field_type = TYPE_FIELD_TYPE (field_type, index);
9439 }
9440
9441 /* Smash this type to be a structure type. We have to do this
9442 because the type has already been recorded. */
9443 type->set_code (TYPE_CODE_STRUCT);
9444 type->set_num_fields (3);
9445 /* Save the field we care about. */
9446 struct field saved_field = type->field (0);
9447 type->set_fields
9448 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9449
9450 /* Put the discriminant at index 0. */
9451 TYPE_FIELD_TYPE (type, 0) = field_type;
9452 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9453 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9454 SET_FIELD_BITPOS (type->field (0), bit_offset);
9455
9456 /* The order of fields doesn't really matter, so put the real
9457 field at index 1 and the data-less field at index 2. */
9458 type->field (1) = saved_field;
9459 TYPE_FIELD_NAME (type, 1)
9460 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
9461 TYPE_FIELD_TYPE (type, 1)->set_name
9462 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9463 TYPE_FIELD_NAME (type, 1)));
9464
9465 const char *dataless_name
9466 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9467 name);
9468 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9469 dataless_name);
9470 TYPE_FIELD_TYPE (type, 2) = dataless_type;
9471 /* NAME points into the original discriminant name, which
9472 already has the correct lifetime. */
9473 TYPE_FIELD_NAME (type, 2) = name;
9474 SET_FIELD_BITPOS (type->field (2), 0);
9475
9476 /* Indicate that this is a variant type. */
9477 static discriminant_range ranges[1] = { { 0, 0 } };
9478 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9479 }
9480 /* A union with a single anonymous field is probably an old-style
9481 univariant enum. */
9482 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9483 {
9484 /* Smash this type to be a structure type. We have to do this
9485 because the type has already been recorded. */
9486 type->set_code (TYPE_CODE_STRUCT);
9487
9488 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9489 const char *variant_name
9490 = rust_last_path_segment (field_type->name ());
9491 TYPE_FIELD_NAME (type, 0) = variant_name;
9492 field_type->set_name
9493 (rust_fully_qualify (&objfile->objfile_obstack,
9494 type->name (), variant_name));
9495 }
9496 else
9497 {
9498 struct type *disr_type = nullptr;
9499 for (int i = 0; i < type->num_fields (); ++i)
9500 {
9501 disr_type = TYPE_FIELD_TYPE (type, i);
9502
9503 if (disr_type->code () != TYPE_CODE_STRUCT)
9504 {
9505 /* All fields of a true enum will be structs. */
9506 return;
9507 }
9508 else if (disr_type->num_fields () == 0)
9509 {
9510 /* Could be data-less variant, so keep going. */
9511 disr_type = nullptr;
9512 }
9513 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9514 "RUST$ENUM$DISR") != 0)
9515 {
9516 /* Not a Rust enum. */
9517 return;
9518 }
9519 else
9520 {
9521 /* Found one. */
9522 break;
9523 }
9524 }
9525
9526 /* If we got here without a discriminant, then it's probably
9527 just a union. */
9528 if (disr_type == nullptr)
9529 return;
9530
9531 /* Smash this type to be a structure type. We have to do this
9532 because the type has already been recorded. */
9533 type->set_code (TYPE_CODE_STRUCT);
9534
9535 /* Make space for the discriminant field. */
9536 struct field *disr_field = &disr_type->field (0);
9537 field *new_fields
9538 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9539 * sizeof (struct field)));
9540 memcpy (new_fields + 1, type->fields (),
9541 type->num_fields () * sizeof (struct field));
9542 type->set_fields (new_fields);
9543 type->set_num_fields (type->num_fields () + 1);
9544
9545 /* Install the discriminant at index 0 in the union. */
9546 type->field (0) = *disr_field;
9547 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9548 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9549
9550 /* We need a way to find the correct discriminant given a
9551 variant name. For convenience we build a map here. */
9552 struct type *enum_type = FIELD_TYPE (*disr_field);
9553 std::unordered_map<std::string, ULONGEST> discriminant_map;
9554 for (int i = 0; i < enum_type->num_fields (); ++i)
9555 {
9556 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9557 {
9558 const char *name
9559 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9560 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9561 }
9562 }
9563
9564 int n_fields = type->num_fields ();
9565 /* We don't need a range entry for the discriminant, but we do
9566 need one for every other field, as there is no default
9567 variant. */
9568 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9569 discriminant_range,
9570 n_fields - 1);
9571 /* Skip the discriminant here. */
9572 for (int i = 1; i < n_fields; ++i)
9573 {
9574 /* Find the final word in the name of this variant's type.
9575 That name can be used to look up the correct
9576 discriminant. */
9577 const char *variant_name
9578 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
9579
9580 auto iter = discriminant_map.find (variant_name);
9581 if (iter != discriminant_map.end ())
9582 {
9583 ranges[i].low = iter->second;
9584 ranges[i].high = iter->second;
9585 }
9586
9587 /* Remove the discriminant field, if it exists. */
9588 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
9589 if (sub_type->num_fields () > 0)
9590 {
9591 sub_type->set_num_fields (sub_type->num_fields () - 1);
9592 sub_type->set_fields (sub_type->fields () + 1);
9593 }
9594 TYPE_FIELD_NAME (type, i) = variant_name;
9595 sub_type->set_name
9596 (rust_fully_qualify (&objfile->objfile_obstack,
9597 type->name (), variant_name));
9598 }
9599
9600 /* Indicate that this is a variant type. */
9601 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9602 gdb::array_view<discriminant_range> (ranges,
9603 n_fields - 1));
9604 }
9605 }
9606
9607 /* Rewrite some Rust unions to be structures with variants parts. */
9608
9609 static void
9610 rust_union_quirks (struct dwarf2_cu *cu)
9611 {
9612 gdb_assert (cu->language == language_rust);
9613 for (type *type_ : cu->rust_unions)
9614 quirk_rust_enum (type_, cu->per_objfile->objfile);
9615 /* We don't need this any more. */
9616 cu->rust_unions.clear ();
9617 }
9618
9619 /* See read.h. */
9620
9621 type_unit_group_unshareable *
9622 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9623 {
9624 auto iter = this->m_type_units.find (tu_group);
9625 if (iter != this->m_type_units.end ())
9626 return iter->second.get ();
9627
9628 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9629 type_unit_group_unshareable *result = uniq.get ();
9630 this->m_type_units[tu_group] = std::move (uniq);
9631 return result;
9632 }
9633
9634 struct type *
9635 dwarf2_per_objfile::get_type_for_signatured_type
9636 (signatured_type *sig_type) const
9637 {
9638 auto iter = this->m_type_map.find (sig_type);
9639 if (iter == this->m_type_map.end ())
9640 return nullptr;
9641
9642 return iter->second;
9643 }
9644
9645 void dwarf2_per_objfile::set_type_for_signatured_type
9646 (signatured_type *sig_type, struct type *type)
9647 {
9648 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9649
9650 this->m_type_map[sig_type] = type;
9651 }
9652
9653 /* A helper function for computing the list of all symbol tables
9654 included by PER_CU. */
9655
9656 static void
9657 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9658 htab_t all_children, htab_t all_type_symtabs,
9659 dwarf2_per_cu_data *per_cu,
9660 dwarf2_per_objfile *per_objfile,
9661 struct compunit_symtab *immediate_parent)
9662 {
9663 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9664 if (*slot != NULL)
9665 {
9666 /* This inclusion and its children have been processed. */
9667 return;
9668 }
9669
9670 *slot = per_cu;
9671
9672 /* Only add a CU if it has a symbol table. */
9673 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9674 if (cust != NULL)
9675 {
9676 /* If this is a type unit only add its symbol table if we haven't
9677 seen it yet (type unit per_cu's can share symtabs). */
9678 if (per_cu->is_debug_types)
9679 {
9680 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9681 if (*slot == NULL)
9682 {
9683 *slot = cust;
9684 result->push_back (cust);
9685 if (cust->user == NULL)
9686 cust->user = immediate_parent;
9687 }
9688 }
9689 else
9690 {
9691 result->push_back (cust);
9692 if (cust->user == NULL)
9693 cust->user = immediate_parent;
9694 }
9695 }
9696
9697 if (!per_cu->imported_symtabs_empty ())
9698 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9699 {
9700 recursively_compute_inclusions (result, all_children,
9701 all_type_symtabs, ptr, per_objfile,
9702 cust);
9703 }
9704 }
9705
9706 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9707 PER_CU. */
9708
9709 static void
9710 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9711 dwarf2_per_objfile *per_objfile)
9712 {
9713 gdb_assert (! per_cu->is_debug_types);
9714
9715 if (!per_cu->imported_symtabs_empty ())
9716 {
9717 int len;
9718 std::vector<compunit_symtab *> result_symtabs;
9719 htab_t all_children, all_type_symtabs;
9720 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9721
9722 /* If we don't have a symtab, we can just skip this case. */
9723 if (cust == NULL)
9724 return;
9725
9726 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9727 NULL, xcalloc, xfree);
9728 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9729 NULL, xcalloc, xfree);
9730
9731 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9732 {
9733 recursively_compute_inclusions (&result_symtabs, all_children,
9734 all_type_symtabs, ptr, per_objfile,
9735 cust);
9736 }
9737
9738 /* Now we have a transitive closure of all the included symtabs. */
9739 len = result_symtabs.size ();
9740 cust->includes
9741 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9742 struct compunit_symtab *, len + 1);
9743 memcpy (cust->includes, result_symtabs.data (),
9744 len * sizeof (compunit_symtab *));
9745 cust->includes[len] = NULL;
9746
9747 htab_delete (all_children);
9748 htab_delete (all_type_symtabs);
9749 }
9750 }
9751
9752 /* Compute the 'includes' field for the symtabs of all the CUs we just
9753 read. */
9754
9755 static void
9756 process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
9757 {
9758 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
9759 {
9760 if (! iter->is_debug_types)
9761 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
9762 }
9763
9764 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
9765 }
9766
9767 /* Generate full symbol information for CU, whose DIEs have
9768 already been loaded into memory. */
9769
9770 static void
9771 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9772 {
9773 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9774 struct objfile *objfile = dwarf2_per_objfile->objfile;
9775 struct gdbarch *gdbarch = objfile->arch ();
9776 CORE_ADDR lowpc, highpc;
9777 struct compunit_symtab *cust;
9778 CORE_ADDR baseaddr;
9779 struct block *static_block;
9780 CORE_ADDR addr;
9781
9782 baseaddr = objfile->text_section_offset ();
9783
9784 /* Clear the list here in case something was left over. */
9785 cu->method_list.clear ();
9786
9787 cu->language = pretend_language;
9788 cu->language_defn = language_def (cu->language);
9789
9790 /* Do line number decoding in read_file_scope () */
9791 process_die (cu->dies, cu);
9792
9793 /* For now fudge the Go package. */
9794 if (cu->language == language_go)
9795 fixup_go_packaging (cu);
9796
9797 /* Now that we have processed all the DIEs in the CU, all the types
9798 should be complete, and it should now be safe to compute all of the
9799 physnames. */
9800 compute_delayed_physnames (cu);
9801
9802 if (cu->language == language_rust)
9803 rust_union_quirks (cu);
9804
9805 /* Some compilers don't define a DW_AT_high_pc attribute for the
9806 compilation unit. If the DW_AT_high_pc is missing, synthesize
9807 it, by scanning the DIE's below the compilation unit. */
9808 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9809
9810 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9811 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9812
9813 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9814 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9815 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9816 addrmap to help ensure it has an accurate map of pc values belonging to
9817 this comp unit. */
9818 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9819
9820 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9821 SECT_OFF_TEXT (objfile),
9822 0);
9823
9824 if (cust != NULL)
9825 {
9826 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9827
9828 /* Set symtab language to language from DW_AT_language. If the
9829 compilation is from a C file generated by language preprocessors, do
9830 not set the language if it was already deduced by start_subfile. */
9831 if (!(cu->language == language_c
9832 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9833 COMPUNIT_FILETABS (cust)->language = cu->language;
9834
9835 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9836 produce DW_AT_location with location lists but it can be possibly
9837 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9838 there were bugs in prologue debug info, fixed later in GCC-4.5
9839 by "unwind info for epilogues" patch (which is not directly related).
9840
9841 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9842 needed, it would be wrong due to missing DW_AT_producer there.
9843
9844 Still one can confuse GDB by using non-standard GCC compilation
9845 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9846 */
9847 if (cu->has_loclist && gcc_4_minor >= 5)
9848 cust->locations_valid = 1;
9849
9850 if (gcc_4_minor >= 5)
9851 cust->epilogue_unwind_valid = 1;
9852
9853 cust->call_site_htab = cu->call_site_htab;
9854 }
9855
9856 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9857
9858 /* Push it for inclusion processing later. */
9859 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9860
9861 /* Not needed any more. */
9862 cu->reset_builder ();
9863 }
9864
9865 /* Generate full symbol information for type unit CU, whose DIEs have
9866 already been loaded into memory. */
9867
9868 static void
9869 process_full_type_unit (dwarf2_cu *cu,
9870 enum language pretend_language)
9871 {
9872 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9873 struct objfile *objfile = dwarf2_per_objfile->objfile;
9874 struct compunit_symtab *cust;
9875 struct signatured_type *sig_type;
9876
9877 gdb_assert (cu->per_cu->is_debug_types);
9878 sig_type = (struct signatured_type *) cu->per_cu;
9879
9880 /* Clear the list here in case something was left over. */
9881 cu->method_list.clear ();
9882
9883 cu->language = pretend_language;
9884 cu->language_defn = language_def (cu->language);
9885
9886 /* The symbol tables are set up in read_type_unit_scope. */
9887 process_die (cu->dies, cu);
9888
9889 /* For now fudge the Go package. */
9890 if (cu->language == language_go)
9891 fixup_go_packaging (cu);
9892
9893 /* Now that we have processed all the DIEs in the CU, all the types
9894 should be complete, and it should now be safe to compute all of the
9895 physnames. */
9896 compute_delayed_physnames (cu);
9897
9898 if (cu->language == language_rust)
9899 rust_union_quirks (cu);
9900
9901 /* TUs share symbol tables.
9902 If this is the first TU to use this symtab, complete the construction
9903 of it with end_expandable_symtab. Otherwise, complete the addition of
9904 this TU's symbols to the existing symtab. */
9905 type_unit_group_unshareable *tug_unshare =
9906 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9907 if (tug_unshare->compunit_symtab == NULL)
9908 {
9909 buildsym_compunit *builder = cu->get_builder ();
9910 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9911 tug_unshare->compunit_symtab = cust;
9912
9913 if (cust != NULL)
9914 {
9915 /* Set symtab language to language from DW_AT_language. If the
9916 compilation is from a C file generated by language preprocessors,
9917 do not set the language if it was already deduced by
9918 start_subfile. */
9919 if (!(cu->language == language_c
9920 && COMPUNIT_FILETABS (cust)->language != language_c))
9921 COMPUNIT_FILETABS (cust)->language = cu->language;
9922 }
9923 }
9924 else
9925 {
9926 cu->get_builder ()->augment_type_symtab ();
9927 cust = tug_unshare->compunit_symtab;
9928 }
9929
9930 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
9931
9932 /* Not needed any more. */
9933 cu->reset_builder ();
9934 }
9935
9936 /* Process an imported unit DIE. */
9937
9938 static void
9939 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9940 {
9941 struct attribute *attr;
9942
9943 /* For now we don't handle imported units in type units. */
9944 if (cu->per_cu->is_debug_types)
9945 {
9946 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9947 " supported in type units [in module %s]"),
9948 objfile_name (cu->per_objfile->objfile));
9949 }
9950
9951 attr = dwarf2_attr (die, DW_AT_import, cu);
9952 if (attr != NULL)
9953 {
9954 sect_offset sect_off = attr->get_ref_die_offset ();
9955 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9956 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9957 dwarf2_per_cu_data *per_cu
9958 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9959
9960 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9961 into another compilation unit, at root level. Regard this as a hint,
9962 and ignore it. */
9963 if (die->parent && die->parent->parent == NULL
9964 && per_cu->unit_type == DW_UT_compile
9965 && per_cu->lang == language_cplus)
9966 return;
9967
9968 /* If necessary, add it to the queue and load its DIEs. */
9969 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9970 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
9971
9972 cu->per_cu->imported_symtabs_push (per_cu);
9973 }
9974 }
9975
9976 /* RAII object that represents a process_die scope: i.e.,
9977 starts/finishes processing a DIE. */
9978 class process_die_scope
9979 {
9980 public:
9981 process_die_scope (die_info *die, dwarf2_cu *cu)
9982 : m_die (die), m_cu (cu)
9983 {
9984 /* We should only be processing DIEs not already in process. */
9985 gdb_assert (!m_die->in_process);
9986 m_die->in_process = true;
9987 }
9988
9989 ~process_die_scope ()
9990 {
9991 m_die->in_process = false;
9992
9993 /* If we're done processing the DIE for the CU that owns the line
9994 header, we don't need the line header anymore. */
9995 if (m_cu->line_header_die_owner == m_die)
9996 {
9997 delete m_cu->line_header;
9998 m_cu->line_header = NULL;
9999 m_cu->line_header_die_owner = NULL;
10000 }
10001 }
10002
10003 private:
10004 die_info *m_die;
10005 dwarf2_cu *m_cu;
10006 };
10007
10008 /* Process a die and its children. */
10009
10010 static void
10011 process_die (struct die_info *die, struct dwarf2_cu *cu)
10012 {
10013 process_die_scope scope (die, cu);
10014
10015 switch (die->tag)
10016 {
10017 case DW_TAG_padding:
10018 break;
10019 case DW_TAG_compile_unit:
10020 case DW_TAG_partial_unit:
10021 read_file_scope (die, cu);
10022 break;
10023 case DW_TAG_type_unit:
10024 read_type_unit_scope (die, cu);
10025 break;
10026 case DW_TAG_subprogram:
10027 /* Nested subprograms in Fortran get a prefix. */
10028 if (cu->language == language_fortran
10029 && die->parent != NULL
10030 && die->parent->tag == DW_TAG_subprogram)
10031 cu->processing_has_namespace_info = true;
10032 /* Fall through. */
10033 case DW_TAG_inlined_subroutine:
10034 read_func_scope (die, cu);
10035 break;
10036 case DW_TAG_lexical_block:
10037 case DW_TAG_try_block:
10038 case DW_TAG_catch_block:
10039 read_lexical_block_scope (die, cu);
10040 break;
10041 case DW_TAG_call_site:
10042 case DW_TAG_GNU_call_site:
10043 read_call_site_scope (die, cu);
10044 break;
10045 case DW_TAG_class_type:
10046 case DW_TAG_interface_type:
10047 case DW_TAG_structure_type:
10048 case DW_TAG_union_type:
10049 process_structure_scope (die, cu);
10050 break;
10051 case DW_TAG_enumeration_type:
10052 process_enumeration_scope (die, cu);
10053 break;
10054
10055 /* These dies have a type, but processing them does not create
10056 a symbol or recurse to process the children. Therefore we can
10057 read them on-demand through read_type_die. */
10058 case DW_TAG_subroutine_type:
10059 case DW_TAG_set_type:
10060 case DW_TAG_array_type:
10061 case DW_TAG_pointer_type:
10062 case DW_TAG_ptr_to_member_type:
10063 case DW_TAG_reference_type:
10064 case DW_TAG_rvalue_reference_type:
10065 case DW_TAG_string_type:
10066 break;
10067
10068 case DW_TAG_base_type:
10069 case DW_TAG_subrange_type:
10070 case DW_TAG_typedef:
10071 /* Add a typedef symbol for the type definition, if it has a
10072 DW_AT_name. */
10073 new_symbol (die, read_type_die (die, cu), cu);
10074 break;
10075 case DW_TAG_common_block:
10076 read_common_block (die, cu);
10077 break;
10078 case DW_TAG_common_inclusion:
10079 break;
10080 case DW_TAG_namespace:
10081 cu->processing_has_namespace_info = true;
10082 read_namespace (die, cu);
10083 break;
10084 case DW_TAG_module:
10085 cu->processing_has_namespace_info = true;
10086 read_module (die, cu);
10087 break;
10088 case DW_TAG_imported_declaration:
10089 cu->processing_has_namespace_info = true;
10090 if (read_namespace_alias (die, cu))
10091 break;
10092 /* The declaration is not a global namespace alias. */
10093 /* Fall through. */
10094 case DW_TAG_imported_module:
10095 cu->processing_has_namespace_info = true;
10096 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10097 || cu->language != language_fortran))
10098 complaint (_("Tag '%s' has unexpected children"),
10099 dwarf_tag_name (die->tag));
10100 read_import_statement (die, cu);
10101 break;
10102
10103 case DW_TAG_imported_unit:
10104 process_imported_unit_die (die, cu);
10105 break;
10106
10107 case DW_TAG_variable:
10108 read_variable (die, cu);
10109 break;
10110
10111 default:
10112 new_symbol (die, NULL, cu);
10113 break;
10114 }
10115 }
10116 \f
10117 /* DWARF name computation. */
10118
10119 /* A helper function for dwarf2_compute_name which determines whether DIE
10120 needs to have the name of the scope prepended to the name listed in the
10121 die. */
10122
10123 static int
10124 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10125 {
10126 struct attribute *attr;
10127
10128 switch (die->tag)
10129 {
10130 case DW_TAG_namespace:
10131 case DW_TAG_typedef:
10132 case DW_TAG_class_type:
10133 case DW_TAG_interface_type:
10134 case DW_TAG_structure_type:
10135 case DW_TAG_union_type:
10136 case DW_TAG_enumeration_type:
10137 case DW_TAG_enumerator:
10138 case DW_TAG_subprogram:
10139 case DW_TAG_inlined_subroutine:
10140 case DW_TAG_member:
10141 case DW_TAG_imported_declaration:
10142 return 1;
10143
10144 case DW_TAG_variable:
10145 case DW_TAG_constant:
10146 /* We only need to prefix "globally" visible variables. These include
10147 any variable marked with DW_AT_external or any variable that
10148 lives in a namespace. [Variables in anonymous namespaces
10149 require prefixing, but they are not DW_AT_external.] */
10150
10151 if (dwarf2_attr (die, DW_AT_specification, cu))
10152 {
10153 struct dwarf2_cu *spec_cu = cu;
10154
10155 return die_needs_namespace (die_specification (die, &spec_cu),
10156 spec_cu);
10157 }
10158
10159 attr = dwarf2_attr (die, DW_AT_external, cu);
10160 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10161 && die->parent->tag != DW_TAG_module)
10162 return 0;
10163 /* A variable in a lexical block of some kind does not need a
10164 namespace, even though in C++ such variables may be external
10165 and have a mangled name. */
10166 if (die->parent->tag == DW_TAG_lexical_block
10167 || die->parent->tag == DW_TAG_try_block
10168 || die->parent->tag == DW_TAG_catch_block
10169 || die->parent->tag == DW_TAG_subprogram)
10170 return 0;
10171 return 1;
10172
10173 default:
10174 return 0;
10175 }
10176 }
10177
10178 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10179 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10180 defined for the given DIE. */
10181
10182 static struct attribute *
10183 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10184 {
10185 struct attribute *attr;
10186
10187 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10188 if (attr == NULL)
10189 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10190
10191 return attr;
10192 }
10193
10194 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10195 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10196 defined for the given DIE. */
10197
10198 static const char *
10199 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10200 {
10201 const char *linkage_name;
10202
10203 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10204 if (linkage_name == NULL)
10205 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10206
10207 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10208 See https://github.com/rust-lang/rust/issues/32925. */
10209 if (cu->language == language_rust && linkage_name != NULL
10210 && strchr (linkage_name, '{') != NULL)
10211 linkage_name = NULL;
10212
10213 return linkage_name;
10214 }
10215
10216 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10217 compute the physname for the object, which include a method's:
10218 - formal parameters (C++),
10219 - receiver type (Go),
10220
10221 The term "physname" is a bit confusing.
10222 For C++, for example, it is the demangled name.
10223 For Go, for example, it's the mangled name.
10224
10225 For Ada, return the DIE's linkage name rather than the fully qualified
10226 name. PHYSNAME is ignored..
10227
10228 The result is allocated on the objfile->per_bfd's obstack and
10229 canonicalized. */
10230
10231 static const char *
10232 dwarf2_compute_name (const char *name,
10233 struct die_info *die, struct dwarf2_cu *cu,
10234 int physname)
10235 {
10236 struct objfile *objfile = cu->per_objfile->objfile;
10237
10238 if (name == NULL)
10239 name = dwarf2_name (die, cu);
10240
10241 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10242 but otherwise compute it by typename_concat inside GDB.
10243 FIXME: Actually this is not really true, or at least not always true.
10244 It's all very confusing. compute_and_set_names doesn't try to demangle
10245 Fortran names because there is no mangling standard. So new_symbol
10246 will set the demangled name to the result of dwarf2_full_name, and it is
10247 the demangled name that GDB uses if it exists. */
10248 if (cu->language == language_ada
10249 || (cu->language == language_fortran && physname))
10250 {
10251 /* For Ada unit, we prefer the linkage name over the name, as
10252 the former contains the exported name, which the user expects
10253 to be able to reference. Ideally, we want the user to be able
10254 to reference this entity using either natural or linkage name,
10255 but we haven't started looking at this enhancement yet. */
10256 const char *linkage_name = dw2_linkage_name (die, cu);
10257
10258 if (linkage_name != NULL)
10259 return linkage_name;
10260 }
10261
10262 /* These are the only languages we know how to qualify names in. */
10263 if (name != NULL
10264 && (cu->language == language_cplus
10265 || cu->language == language_fortran || cu->language == language_d
10266 || cu->language == language_rust))
10267 {
10268 if (die_needs_namespace (die, cu))
10269 {
10270 const char *prefix;
10271 const char *canonical_name = NULL;
10272
10273 string_file buf;
10274
10275 prefix = determine_prefix (die, cu);
10276 if (*prefix != '\0')
10277 {
10278 gdb::unique_xmalloc_ptr<char> prefixed_name
10279 (typename_concat (NULL, prefix, name, physname, cu));
10280
10281 buf.puts (prefixed_name.get ());
10282 }
10283 else
10284 buf.puts (name);
10285
10286 /* Template parameters may be specified in the DIE's DW_AT_name, or
10287 as children with DW_TAG_template_type_param or
10288 DW_TAG_value_type_param. If the latter, add them to the name
10289 here. If the name already has template parameters, then
10290 skip this step; some versions of GCC emit both, and
10291 it is more efficient to use the pre-computed name.
10292
10293 Something to keep in mind about this process: it is very
10294 unlikely, or in some cases downright impossible, to produce
10295 something that will match the mangled name of a function.
10296 If the definition of the function has the same debug info,
10297 we should be able to match up with it anyway. But fallbacks
10298 using the minimal symbol, for instance to find a method
10299 implemented in a stripped copy of libstdc++, will not work.
10300 If we do not have debug info for the definition, we will have to
10301 match them up some other way.
10302
10303 When we do name matching there is a related problem with function
10304 templates; two instantiated function templates are allowed to
10305 differ only by their return types, which we do not add here. */
10306
10307 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10308 {
10309 struct attribute *attr;
10310 struct die_info *child;
10311 int first = 1;
10312
10313 die->building_fullname = 1;
10314
10315 for (child = die->child; child != NULL; child = child->sibling)
10316 {
10317 struct type *type;
10318 LONGEST value;
10319 const gdb_byte *bytes;
10320 struct dwarf2_locexpr_baton *baton;
10321 struct value *v;
10322
10323 if (child->tag != DW_TAG_template_type_param
10324 && child->tag != DW_TAG_template_value_param)
10325 continue;
10326
10327 if (first)
10328 {
10329 buf.puts ("<");
10330 first = 0;
10331 }
10332 else
10333 buf.puts (", ");
10334
10335 attr = dwarf2_attr (child, DW_AT_type, cu);
10336 if (attr == NULL)
10337 {
10338 complaint (_("template parameter missing DW_AT_type"));
10339 buf.puts ("UNKNOWN_TYPE");
10340 continue;
10341 }
10342 type = die_type (child, cu);
10343
10344 if (child->tag == DW_TAG_template_type_param)
10345 {
10346 c_print_type (type, "", &buf, -1, 0, cu->language,
10347 &type_print_raw_options);
10348 continue;
10349 }
10350
10351 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10352 if (attr == NULL)
10353 {
10354 complaint (_("template parameter missing "
10355 "DW_AT_const_value"));
10356 buf.puts ("UNKNOWN_VALUE");
10357 continue;
10358 }
10359
10360 dwarf2_const_value_attr (attr, type, name,
10361 &cu->comp_unit_obstack, cu,
10362 &value, &bytes, &baton);
10363
10364 if (TYPE_NOSIGN (type))
10365 /* GDB prints characters as NUMBER 'CHAR'. If that's
10366 changed, this can use value_print instead. */
10367 c_printchar (value, type, &buf);
10368 else
10369 {
10370 struct value_print_options opts;
10371
10372 if (baton != NULL)
10373 v = dwarf2_evaluate_loc_desc (type, NULL,
10374 baton->data,
10375 baton->size,
10376 baton->per_cu,
10377 baton->per_objfile);
10378 else if (bytes != NULL)
10379 {
10380 v = allocate_value (type);
10381 memcpy (value_contents_writeable (v), bytes,
10382 TYPE_LENGTH (type));
10383 }
10384 else
10385 v = value_from_longest (type, value);
10386
10387 /* Specify decimal so that we do not depend on
10388 the radix. */
10389 get_formatted_print_options (&opts, 'd');
10390 opts.raw = 1;
10391 value_print (v, &buf, &opts);
10392 release_value (v);
10393 }
10394 }
10395
10396 die->building_fullname = 0;
10397
10398 if (!first)
10399 {
10400 /* Close the argument list, with a space if necessary
10401 (nested templates). */
10402 if (!buf.empty () && buf.string ().back () == '>')
10403 buf.puts (" >");
10404 else
10405 buf.puts (">");
10406 }
10407 }
10408
10409 /* For C++ methods, append formal parameter type
10410 information, if PHYSNAME. */
10411
10412 if (physname && die->tag == DW_TAG_subprogram
10413 && cu->language == language_cplus)
10414 {
10415 struct type *type = read_type_die (die, cu);
10416
10417 c_type_print_args (type, &buf, 1, cu->language,
10418 &type_print_raw_options);
10419
10420 if (cu->language == language_cplus)
10421 {
10422 /* Assume that an artificial first parameter is
10423 "this", but do not crash if it is not. RealView
10424 marks unnamed (and thus unused) parameters as
10425 artificial; there is no way to differentiate
10426 the two cases. */
10427 if (type->num_fields () > 0
10428 && TYPE_FIELD_ARTIFICIAL (type, 0)
10429 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
10430 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10431 0))))
10432 buf.puts (" const");
10433 }
10434 }
10435
10436 const std::string &intermediate_name = buf.string ();
10437
10438 if (cu->language == language_cplus)
10439 canonical_name
10440 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10441 objfile);
10442
10443 /* If we only computed INTERMEDIATE_NAME, or if
10444 INTERMEDIATE_NAME is already canonical, then we need to
10445 intern it. */
10446 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10447 name = objfile->intern (intermediate_name);
10448 else
10449 name = canonical_name;
10450 }
10451 }
10452
10453 return name;
10454 }
10455
10456 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10457 If scope qualifiers are appropriate they will be added. The result
10458 will be allocated on the storage_obstack, or NULL if the DIE does
10459 not have a name. NAME may either be from a previous call to
10460 dwarf2_name or NULL.
10461
10462 The output string will be canonicalized (if C++). */
10463
10464 static const char *
10465 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10466 {
10467 return dwarf2_compute_name (name, die, cu, 0);
10468 }
10469
10470 /* Construct a physname for the given DIE in CU. NAME may either be
10471 from a previous call to dwarf2_name or NULL. The result will be
10472 allocated on the objfile_objstack or NULL if the DIE does not have a
10473 name.
10474
10475 The output string will be canonicalized (if C++). */
10476
10477 static const char *
10478 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10479 {
10480 struct objfile *objfile = cu->per_objfile->objfile;
10481 const char *retval, *mangled = NULL, *canon = NULL;
10482 int need_copy = 1;
10483
10484 /* In this case dwarf2_compute_name is just a shortcut not building anything
10485 on its own. */
10486 if (!die_needs_namespace (die, cu))
10487 return dwarf2_compute_name (name, die, cu, 1);
10488
10489 if (cu->language != language_rust)
10490 mangled = dw2_linkage_name (die, cu);
10491
10492 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10493 has computed. */
10494 gdb::unique_xmalloc_ptr<char> demangled;
10495 if (mangled != NULL)
10496 {
10497
10498 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10499 {
10500 /* Do nothing (do not demangle the symbol name). */
10501 }
10502 else if (cu->language == language_go)
10503 {
10504 /* This is a lie, but we already lie to the caller new_symbol.
10505 new_symbol assumes we return the mangled name.
10506 This just undoes that lie until things are cleaned up. */
10507 }
10508 else
10509 {
10510 /* Use DMGL_RET_DROP for C++ template functions to suppress
10511 their return type. It is easier for GDB users to search
10512 for such functions as `name(params)' than `long name(params)'.
10513 In such case the minimal symbol names do not match the full
10514 symbol names but for template functions there is never a need
10515 to look up their definition from their declaration so
10516 the only disadvantage remains the minimal symbol variant
10517 `long name(params)' does not have the proper inferior type. */
10518 demangled.reset (gdb_demangle (mangled,
10519 (DMGL_PARAMS | DMGL_ANSI
10520 | DMGL_RET_DROP)));
10521 }
10522 if (demangled)
10523 canon = demangled.get ();
10524 else
10525 {
10526 canon = mangled;
10527 need_copy = 0;
10528 }
10529 }
10530
10531 if (canon == NULL || check_physname)
10532 {
10533 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10534
10535 if (canon != NULL && strcmp (physname, canon) != 0)
10536 {
10537 /* It may not mean a bug in GDB. The compiler could also
10538 compute DW_AT_linkage_name incorrectly. But in such case
10539 GDB would need to be bug-to-bug compatible. */
10540
10541 complaint (_("Computed physname <%s> does not match demangled <%s> "
10542 "(from linkage <%s>) - DIE at %s [in module %s]"),
10543 physname, canon, mangled, sect_offset_str (die->sect_off),
10544 objfile_name (objfile));
10545
10546 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10547 is available here - over computed PHYSNAME. It is safer
10548 against both buggy GDB and buggy compilers. */
10549
10550 retval = canon;
10551 }
10552 else
10553 {
10554 retval = physname;
10555 need_copy = 0;
10556 }
10557 }
10558 else
10559 retval = canon;
10560
10561 if (need_copy)
10562 retval = objfile->intern (retval);
10563
10564 return retval;
10565 }
10566
10567 /* Inspect DIE in CU for a namespace alias. If one exists, record
10568 a new symbol for it.
10569
10570 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10571
10572 static int
10573 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10574 {
10575 struct attribute *attr;
10576
10577 /* If the die does not have a name, this is not a namespace
10578 alias. */
10579 attr = dwarf2_attr (die, DW_AT_name, cu);
10580 if (attr != NULL)
10581 {
10582 int num;
10583 struct die_info *d = die;
10584 struct dwarf2_cu *imported_cu = cu;
10585
10586 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10587 keep inspecting DIEs until we hit the underlying import. */
10588 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10589 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10590 {
10591 attr = dwarf2_attr (d, DW_AT_import, cu);
10592 if (attr == NULL)
10593 break;
10594
10595 d = follow_die_ref (d, attr, &imported_cu);
10596 if (d->tag != DW_TAG_imported_declaration)
10597 break;
10598 }
10599
10600 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10601 {
10602 complaint (_("DIE at %s has too many recursively imported "
10603 "declarations"), sect_offset_str (d->sect_off));
10604 return 0;
10605 }
10606
10607 if (attr != NULL)
10608 {
10609 struct type *type;
10610 sect_offset sect_off = attr->get_ref_die_offset ();
10611
10612 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10613 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10614 {
10615 /* This declaration is a global namespace alias. Add
10616 a symbol for it whose type is the aliased namespace. */
10617 new_symbol (die, type, cu);
10618 return 1;
10619 }
10620 }
10621 }
10622
10623 return 0;
10624 }
10625
10626 /* Return the using directives repository (global or local?) to use in the
10627 current context for CU.
10628
10629 For Ada, imported declarations can materialize renamings, which *may* be
10630 global. However it is impossible (for now?) in DWARF to distinguish
10631 "external" imported declarations and "static" ones. As all imported
10632 declarations seem to be static in all other languages, make them all CU-wide
10633 global only in Ada. */
10634
10635 static struct using_direct **
10636 using_directives (struct dwarf2_cu *cu)
10637 {
10638 if (cu->language == language_ada
10639 && cu->get_builder ()->outermost_context_p ())
10640 return cu->get_builder ()->get_global_using_directives ();
10641 else
10642 return cu->get_builder ()->get_local_using_directives ();
10643 }
10644
10645 /* Read the import statement specified by the given die and record it. */
10646
10647 static void
10648 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10649 {
10650 struct objfile *objfile = cu->per_objfile->objfile;
10651 struct attribute *import_attr;
10652 struct die_info *imported_die, *child_die;
10653 struct dwarf2_cu *imported_cu;
10654 const char *imported_name;
10655 const char *imported_name_prefix;
10656 const char *canonical_name;
10657 const char *import_alias;
10658 const char *imported_declaration = NULL;
10659 const char *import_prefix;
10660 std::vector<const char *> excludes;
10661
10662 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10663 if (import_attr == NULL)
10664 {
10665 complaint (_("Tag '%s' has no DW_AT_import"),
10666 dwarf_tag_name (die->tag));
10667 return;
10668 }
10669
10670 imported_cu = cu;
10671 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10672 imported_name = dwarf2_name (imported_die, imported_cu);
10673 if (imported_name == NULL)
10674 {
10675 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10676
10677 The import in the following code:
10678 namespace A
10679 {
10680 typedef int B;
10681 }
10682
10683 int main ()
10684 {
10685 using A::B;
10686 B b;
10687 return b;
10688 }
10689
10690 ...
10691 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10692 <52> DW_AT_decl_file : 1
10693 <53> DW_AT_decl_line : 6
10694 <54> DW_AT_import : <0x75>
10695 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10696 <59> DW_AT_name : B
10697 <5b> DW_AT_decl_file : 1
10698 <5c> DW_AT_decl_line : 2
10699 <5d> DW_AT_type : <0x6e>
10700 ...
10701 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10702 <76> DW_AT_byte_size : 4
10703 <77> DW_AT_encoding : 5 (signed)
10704
10705 imports the wrong die ( 0x75 instead of 0x58 ).
10706 This case will be ignored until the gcc bug is fixed. */
10707 return;
10708 }
10709
10710 /* Figure out the local name after import. */
10711 import_alias = dwarf2_name (die, cu);
10712
10713 /* Figure out where the statement is being imported to. */
10714 import_prefix = determine_prefix (die, cu);
10715
10716 /* Figure out what the scope of the imported die is and prepend it
10717 to the name of the imported die. */
10718 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10719
10720 if (imported_die->tag != DW_TAG_namespace
10721 && imported_die->tag != DW_TAG_module)
10722 {
10723 imported_declaration = imported_name;
10724 canonical_name = imported_name_prefix;
10725 }
10726 else if (strlen (imported_name_prefix) > 0)
10727 canonical_name = obconcat (&objfile->objfile_obstack,
10728 imported_name_prefix,
10729 (cu->language == language_d ? "." : "::"),
10730 imported_name, (char *) NULL);
10731 else
10732 canonical_name = imported_name;
10733
10734 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10735 for (child_die = die->child; child_die && child_die->tag;
10736 child_die = child_die->sibling)
10737 {
10738 /* DWARF-4: A Fortran use statement with a “rename list” may be
10739 represented by an imported module entry with an import attribute
10740 referring to the module and owned entries corresponding to those
10741 entities that are renamed as part of being imported. */
10742
10743 if (child_die->tag != DW_TAG_imported_declaration)
10744 {
10745 complaint (_("child DW_TAG_imported_declaration expected "
10746 "- DIE at %s [in module %s]"),
10747 sect_offset_str (child_die->sect_off),
10748 objfile_name (objfile));
10749 continue;
10750 }
10751
10752 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10753 if (import_attr == NULL)
10754 {
10755 complaint (_("Tag '%s' has no DW_AT_import"),
10756 dwarf_tag_name (child_die->tag));
10757 continue;
10758 }
10759
10760 imported_cu = cu;
10761 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10762 &imported_cu);
10763 imported_name = dwarf2_name (imported_die, imported_cu);
10764 if (imported_name == NULL)
10765 {
10766 complaint (_("child DW_TAG_imported_declaration has unknown "
10767 "imported name - DIE at %s [in module %s]"),
10768 sect_offset_str (child_die->sect_off),
10769 objfile_name (objfile));
10770 continue;
10771 }
10772
10773 excludes.push_back (imported_name);
10774
10775 process_die (child_die, cu);
10776 }
10777
10778 add_using_directive (using_directives (cu),
10779 import_prefix,
10780 canonical_name,
10781 import_alias,
10782 imported_declaration,
10783 excludes,
10784 0,
10785 &objfile->objfile_obstack);
10786 }
10787
10788 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10789 types, but gives them a size of zero. Starting with version 14,
10790 ICC is compatible with GCC. */
10791
10792 static bool
10793 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10794 {
10795 if (!cu->checked_producer)
10796 check_producer (cu);
10797
10798 return cu->producer_is_icc_lt_14;
10799 }
10800
10801 /* ICC generates a DW_AT_type for C void functions. This was observed on
10802 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10803 which says that void functions should not have a DW_AT_type. */
10804
10805 static bool
10806 producer_is_icc (struct dwarf2_cu *cu)
10807 {
10808 if (!cu->checked_producer)
10809 check_producer (cu);
10810
10811 return cu->producer_is_icc;
10812 }
10813
10814 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10815 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10816 this, it was first present in GCC release 4.3.0. */
10817
10818 static bool
10819 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10820 {
10821 if (!cu->checked_producer)
10822 check_producer (cu);
10823
10824 return cu->producer_is_gcc_lt_4_3;
10825 }
10826
10827 static file_and_directory
10828 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10829 {
10830 file_and_directory res;
10831
10832 /* Find the filename. Do not use dwarf2_name here, since the filename
10833 is not a source language identifier. */
10834 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10835 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10836
10837 if (res.comp_dir == NULL
10838 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10839 && IS_ABSOLUTE_PATH (res.name))
10840 {
10841 res.comp_dir_storage = ldirname (res.name);
10842 if (!res.comp_dir_storage.empty ())
10843 res.comp_dir = res.comp_dir_storage.c_str ();
10844 }
10845 if (res.comp_dir != NULL)
10846 {
10847 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10848 directory, get rid of it. */
10849 const char *cp = strchr (res.comp_dir, ':');
10850
10851 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10852 res.comp_dir = cp + 1;
10853 }
10854
10855 if (res.name == NULL)
10856 res.name = "<unknown>";
10857
10858 return res;
10859 }
10860
10861 /* Handle DW_AT_stmt_list for a compilation unit.
10862 DIE is the DW_TAG_compile_unit die for CU.
10863 COMP_DIR is the compilation directory. LOWPC is passed to
10864 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10865
10866 static void
10867 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10868 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10869 {
10870 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10871 struct attribute *attr;
10872 struct line_header line_header_local;
10873 hashval_t line_header_local_hash;
10874 void **slot;
10875 int decode_mapping;
10876
10877 gdb_assert (! cu->per_cu->is_debug_types);
10878
10879 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10880 if (attr == NULL)
10881 return;
10882
10883 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
10884
10885 /* The line header hash table is only created if needed (it exists to
10886 prevent redundant reading of the line table for partial_units).
10887 If we're given a partial_unit, we'll need it. If we're given a
10888 compile_unit, then use the line header hash table if it's already
10889 created, but don't create one just yet. */
10890
10891 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
10892 && die->tag == DW_TAG_partial_unit)
10893 {
10894 dwarf2_per_objfile->per_bfd->line_header_hash
10895 .reset (htab_create_alloc (127, line_header_hash_voidp,
10896 line_header_eq_voidp,
10897 free_line_header_voidp,
10898 xcalloc, xfree));
10899 }
10900
10901 line_header_local.sect_off = line_offset;
10902 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10903 line_header_local_hash = line_header_hash (&line_header_local);
10904 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
10905 {
10906 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10907 &line_header_local,
10908 line_header_local_hash, NO_INSERT);
10909
10910 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10911 is not present in *SLOT (since if there is something in *SLOT then
10912 it will be for a partial_unit). */
10913 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10914 {
10915 gdb_assert (*slot != NULL);
10916 cu->line_header = (struct line_header *) *slot;
10917 return;
10918 }
10919 }
10920
10921 /* dwarf_decode_line_header does not yet provide sufficient information.
10922 We always have to call also dwarf_decode_lines for it. */
10923 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10924 if (lh == NULL)
10925 return;
10926
10927 cu->line_header = lh.release ();
10928 cu->line_header_die_owner = die;
10929
10930 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
10931 slot = NULL;
10932 else
10933 {
10934 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
10935 &line_header_local,
10936 line_header_local_hash, INSERT);
10937 gdb_assert (slot != NULL);
10938 }
10939 if (slot != NULL && *slot == NULL)
10940 {
10941 /* This newly decoded line number information unit will be owned
10942 by line_header_hash hash table. */
10943 *slot = cu->line_header;
10944 cu->line_header_die_owner = NULL;
10945 }
10946 else
10947 {
10948 /* We cannot free any current entry in (*slot) as that struct line_header
10949 may be already used by multiple CUs. Create only temporary decoded
10950 line_header for this CU - it may happen at most once for each line
10951 number information unit. And if we're not using line_header_hash
10952 then this is what we want as well. */
10953 gdb_assert (die->tag != DW_TAG_partial_unit);
10954 }
10955 decode_mapping = (die->tag != DW_TAG_partial_unit);
10956 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10957 decode_mapping);
10958
10959 }
10960
10961 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10962
10963 static void
10964 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10965 {
10966 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10967 struct objfile *objfile = dwarf2_per_objfile->objfile;
10968 struct gdbarch *gdbarch = objfile->arch ();
10969 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10970 CORE_ADDR highpc = ((CORE_ADDR) 0);
10971 struct attribute *attr;
10972 struct die_info *child_die;
10973 CORE_ADDR baseaddr;
10974
10975 prepare_one_comp_unit (cu, die, cu->language);
10976 baseaddr = objfile->text_section_offset ();
10977
10978 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10979
10980 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10981 from finish_block. */
10982 if (lowpc == ((CORE_ADDR) -1))
10983 lowpc = highpc;
10984 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10985
10986 file_and_directory fnd = find_file_and_directory (die, cu);
10987
10988 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10989 standardised yet. As a workaround for the language detection we fall
10990 back to the DW_AT_producer string. */
10991 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10992 cu->language = language_opencl;
10993
10994 /* Similar hack for Go. */
10995 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10996 set_cu_language (DW_LANG_Go, cu);
10997
10998 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10999
11000 /* Decode line number information if present. We do this before
11001 processing child DIEs, so that the line header table is available
11002 for DW_AT_decl_file. */
11003 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11004
11005 /* Process all dies in compilation unit. */
11006 if (die->child != NULL)
11007 {
11008 child_die = die->child;
11009 while (child_die && child_die->tag)
11010 {
11011 process_die (child_die, cu);
11012 child_die = child_die->sibling;
11013 }
11014 }
11015
11016 /* Decode macro information, if present. Dwarf 2 macro information
11017 refers to information in the line number info statement program
11018 header, so we can only read it if we've read the header
11019 successfully. */
11020 attr = dwarf2_attr (die, DW_AT_macros, cu);
11021 if (attr == NULL)
11022 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11023 if (attr && cu->line_header)
11024 {
11025 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11026 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11027
11028 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
11029 }
11030 else
11031 {
11032 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11033 if (attr && cu->line_header)
11034 {
11035 unsigned int macro_offset = DW_UNSND (attr);
11036
11037 dwarf_decode_macros (cu, macro_offset, 0);
11038 }
11039 }
11040 }
11041
11042 void
11043 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11044 {
11045 struct type_unit_group *tu_group;
11046 int first_time;
11047 struct attribute *attr;
11048 unsigned int i;
11049 struct signatured_type *sig_type;
11050
11051 gdb_assert (per_cu->is_debug_types);
11052 sig_type = (struct signatured_type *) per_cu;
11053
11054 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11055
11056 /* If we're using .gdb_index (includes -readnow) then
11057 per_cu->type_unit_group may not have been set up yet. */
11058 if (sig_type->type_unit_group == NULL)
11059 sig_type->type_unit_group = get_type_unit_group (this, attr);
11060 tu_group = sig_type->type_unit_group;
11061
11062 /* If we've already processed this stmt_list there's no real need to
11063 do it again, we could fake it and just recreate the part we need
11064 (file name,index -> symtab mapping). If data shows this optimization
11065 is useful we can do it then. */
11066 type_unit_group_unshareable *tug_unshare
11067 = per_objfile->get_type_unit_group_unshareable (tu_group);
11068 first_time = tug_unshare->compunit_symtab == NULL;
11069
11070 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11071 debug info. */
11072 line_header_up lh;
11073 if (attr != NULL)
11074 {
11075 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
11076 lh = dwarf_decode_line_header (line_offset, this);
11077 }
11078 if (lh == NULL)
11079 {
11080 if (first_time)
11081 start_symtab ("", NULL, 0);
11082 else
11083 {
11084 gdb_assert (tug_unshare->symtabs == NULL);
11085 gdb_assert (m_builder == nullptr);
11086 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11087 m_builder.reset (new struct buildsym_compunit
11088 (COMPUNIT_OBJFILE (cust), "",
11089 COMPUNIT_DIRNAME (cust),
11090 compunit_language (cust),
11091 0, cust));
11092 list_in_scope = get_builder ()->get_file_symbols ();
11093 }
11094 return;
11095 }
11096
11097 line_header = lh.release ();
11098 line_header_die_owner = die;
11099
11100 if (first_time)
11101 {
11102 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11103
11104 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11105 still initializing it, and our caller (a few levels up)
11106 process_full_type_unit still needs to know if this is the first
11107 time. */
11108
11109 tug_unshare->symtabs
11110 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11111 struct symtab *, line_header->file_names_size ());
11112
11113 auto &file_names = line_header->file_names ();
11114 for (i = 0; i < file_names.size (); ++i)
11115 {
11116 file_entry &fe = file_names[i];
11117 dwarf2_start_subfile (this, fe.name,
11118 fe.include_dir (line_header));
11119 buildsym_compunit *b = get_builder ();
11120 if (b->get_current_subfile ()->symtab == NULL)
11121 {
11122 /* NOTE: start_subfile will recognize when it's been
11123 passed a file it has already seen. So we can't
11124 assume there's a simple mapping from
11125 cu->line_header->file_names to subfiles, plus
11126 cu->line_header->file_names may contain dups. */
11127 b->get_current_subfile ()->symtab
11128 = allocate_symtab (cust, b->get_current_subfile ()->name);
11129 }
11130
11131 fe.symtab = b->get_current_subfile ()->symtab;
11132 tug_unshare->symtabs[i] = fe.symtab;
11133 }
11134 }
11135 else
11136 {
11137 gdb_assert (m_builder == nullptr);
11138 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11139 m_builder.reset (new struct buildsym_compunit
11140 (COMPUNIT_OBJFILE (cust), "",
11141 COMPUNIT_DIRNAME (cust),
11142 compunit_language (cust),
11143 0, cust));
11144 list_in_scope = get_builder ()->get_file_symbols ();
11145
11146 auto &file_names = line_header->file_names ();
11147 for (i = 0; i < file_names.size (); ++i)
11148 {
11149 file_entry &fe = file_names[i];
11150 fe.symtab = tug_unshare->symtabs[i];
11151 }
11152 }
11153
11154 /* The main symtab is allocated last. Type units don't have DW_AT_name
11155 so they don't have a "real" (so to speak) symtab anyway.
11156 There is later code that will assign the main symtab to all symbols
11157 that don't have one. We need to handle the case of a symbol with a
11158 missing symtab (DW_AT_decl_file) anyway. */
11159 }
11160
11161 /* Process DW_TAG_type_unit.
11162 For TUs we want to skip the first top level sibling if it's not the
11163 actual type being defined by this TU. In this case the first top
11164 level sibling is there to provide context only. */
11165
11166 static void
11167 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11168 {
11169 struct die_info *child_die;
11170
11171 prepare_one_comp_unit (cu, die, language_minimal);
11172
11173 /* Initialize (or reinitialize) the machinery for building symtabs.
11174 We do this before processing child DIEs, so that the line header table
11175 is available for DW_AT_decl_file. */
11176 cu->setup_type_unit_groups (die);
11177
11178 if (die->child != NULL)
11179 {
11180 child_die = die->child;
11181 while (child_die && child_die->tag)
11182 {
11183 process_die (child_die, cu);
11184 child_die = child_die->sibling;
11185 }
11186 }
11187 }
11188 \f
11189 /* DWO/DWP files.
11190
11191 http://gcc.gnu.org/wiki/DebugFission
11192 http://gcc.gnu.org/wiki/DebugFissionDWP
11193
11194 To simplify handling of both DWO files ("object" files with the DWARF info)
11195 and DWP files (a file with the DWOs packaged up into one file), we treat
11196 DWP files as having a collection of virtual DWO files. */
11197
11198 static hashval_t
11199 hash_dwo_file (const void *item)
11200 {
11201 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11202 hashval_t hash;
11203
11204 hash = htab_hash_string (dwo_file->dwo_name);
11205 if (dwo_file->comp_dir != NULL)
11206 hash += htab_hash_string (dwo_file->comp_dir);
11207 return hash;
11208 }
11209
11210 static int
11211 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11212 {
11213 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11214 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11215
11216 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11217 return 0;
11218 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11219 return lhs->comp_dir == rhs->comp_dir;
11220 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11221 }
11222
11223 /* Allocate a hash table for DWO files. */
11224
11225 static htab_up
11226 allocate_dwo_file_hash_table ()
11227 {
11228 auto delete_dwo_file = [] (void *item)
11229 {
11230 struct dwo_file *dwo_file = (struct dwo_file *) item;
11231
11232 delete dwo_file;
11233 };
11234
11235 return htab_up (htab_create_alloc (41,
11236 hash_dwo_file,
11237 eq_dwo_file,
11238 delete_dwo_file,
11239 xcalloc, xfree));
11240 }
11241
11242 /* Lookup DWO file DWO_NAME. */
11243
11244 static void **
11245 lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11246 const char *dwo_name,
11247 const char *comp_dir)
11248 {
11249 struct dwo_file find_entry;
11250 void **slot;
11251
11252 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11253 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11254
11255 find_entry.dwo_name = dwo_name;
11256 find_entry.comp_dir = comp_dir;
11257 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
11258 INSERT);
11259
11260 return slot;
11261 }
11262
11263 static hashval_t
11264 hash_dwo_unit (const void *item)
11265 {
11266 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11267
11268 /* This drops the top 32 bits of the id, but is ok for a hash. */
11269 return dwo_unit->signature;
11270 }
11271
11272 static int
11273 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11274 {
11275 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11276 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11277
11278 /* The signature is assumed to be unique within the DWO file.
11279 So while object file CU dwo_id's always have the value zero,
11280 that's OK, assuming each object file DWO file has only one CU,
11281 and that's the rule for now. */
11282 return lhs->signature == rhs->signature;
11283 }
11284
11285 /* Allocate a hash table for DWO CUs,TUs.
11286 There is one of these tables for each of CUs,TUs for each DWO file. */
11287
11288 static htab_up
11289 allocate_dwo_unit_table ()
11290 {
11291 /* Start out with a pretty small number.
11292 Generally DWO files contain only one CU and maybe some TUs. */
11293 return htab_up (htab_create_alloc (3,
11294 hash_dwo_unit,
11295 eq_dwo_unit,
11296 NULL, xcalloc, xfree));
11297 }
11298
11299 /* die_reader_func for create_dwo_cu. */
11300
11301 static void
11302 create_dwo_cu_reader (const struct die_reader_specs *reader,
11303 const gdb_byte *info_ptr,
11304 struct die_info *comp_unit_die,
11305 struct dwo_file *dwo_file,
11306 struct dwo_unit *dwo_unit)
11307 {
11308 struct dwarf2_cu *cu = reader->cu;
11309 sect_offset sect_off = cu->per_cu->sect_off;
11310 struct dwarf2_section_info *section = cu->per_cu->section;
11311
11312 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11313 if (!signature.has_value ())
11314 {
11315 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11316 " its dwo_id [in module %s]"),
11317 sect_offset_str (sect_off), dwo_file->dwo_name);
11318 return;
11319 }
11320
11321 dwo_unit->dwo_file = dwo_file;
11322 dwo_unit->signature = *signature;
11323 dwo_unit->section = section;
11324 dwo_unit->sect_off = sect_off;
11325 dwo_unit->length = cu->per_cu->length;
11326
11327 if (dwarf_read_debug)
11328 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11329 sect_offset_str (sect_off),
11330 hex_string (dwo_unit->signature));
11331 }
11332
11333 /* Create the dwo_units for the CUs in a DWO_FILE.
11334 Note: This function processes DWO files only, not DWP files. */
11335
11336 static void
11337 create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11338 dwarf2_cu *cu, struct dwo_file &dwo_file,
11339 dwarf2_section_info &section, htab_up &cus_htab)
11340 {
11341 struct objfile *objfile = dwarf2_per_objfile->objfile;
11342 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
11343 const gdb_byte *info_ptr, *end_ptr;
11344
11345 section.read (objfile);
11346 info_ptr = section.buffer;
11347
11348 if (info_ptr == NULL)
11349 return;
11350
11351 if (dwarf_read_debug)
11352 {
11353 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
11354 section.get_name (),
11355 section.get_file_name ());
11356 }
11357
11358 end_ptr = info_ptr + section.size;
11359 while (info_ptr < end_ptr)
11360 {
11361 struct dwarf2_per_cu_data per_cu;
11362 struct dwo_unit read_unit {};
11363 struct dwo_unit *dwo_unit;
11364 void **slot;
11365 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11366
11367 memset (&per_cu, 0, sizeof (per_cu));
11368 per_cu.per_bfd = per_bfd;
11369 per_cu.is_debug_types = 0;
11370 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11371 per_cu.section = &section;
11372
11373 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
11374 if (!reader.dummy_p)
11375 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11376 &dwo_file, &read_unit);
11377 info_ptr += per_cu.length;
11378
11379 // If the unit could not be parsed, skip it.
11380 if (read_unit.dwo_file == NULL)
11381 continue;
11382
11383 if (cus_htab == NULL)
11384 cus_htab = allocate_dwo_unit_table ();
11385
11386 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11387 struct dwo_unit);
11388 *dwo_unit = read_unit;
11389 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11390 gdb_assert (slot != NULL);
11391 if (*slot != NULL)
11392 {
11393 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11394 sect_offset dup_sect_off = dup_cu->sect_off;
11395
11396 complaint (_("debug cu entry at offset %s is duplicate to"
11397 " the entry at offset %s, signature %s"),
11398 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11399 hex_string (dwo_unit->signature));
11400 }
11401 *slot = (void *)dwo_unit;
11402 }
11403 }
11404
11405 /* DWP file .debug_{cu,tu}_index section format:
11406 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11407
11408 DWP Version 1:
11409
11410 Both index sections have the same format, and serve to map a 64-bit
11411 signature to a set of section numbers. Each section begins with a header,
11412 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11413 indexes, and a pool of 32-bit section numbers. The index sections will be
11414 aligned at 8-byte boundaries in the file.
11415
11416 The index section header consists of:
11417
11418 V, 32 bit version number
11419 -, 32 bits unused
11420 N, 32 bit number of compilation units or type units in the index
11421 M, 32 bit number of slots in the hash table
11422
11423 Numbers are recorded using the byte order of the application binary.
11424
11425 The hash table begins at offset 16 in the section, and consists of an array
11426 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11427 order of the application binary). Unused slots in the hash table are 0.
11428 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11429
11430 The parallel table begins immediately after the hash table
11431 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11432 array of 32-bit indexes (using the byte order of the application binary),
11433 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11434 table contains a 32-bit index into the pool of section numbers. For unused
11435 hash table slots, the corresponding entry in the parallel table will be 0.
11436
11437 The pool of section numbers begins immediately following the hash table
11438 (at offset 16 + 12 * M from the beginning of the section). The pool of
11439 section numbers consists of an array of 32-bit words (using the byte order
11440 of the application binary). Each item in the array is indexed starting
11441 from 0. The hash table entry provides the index of the first section
11442 number in the set. Additional section numbers in the set follow, and the
11443 set is terminated by a 0 entry (section number 0 is not used in ELF).
11444
11445 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11446 section must be the first entry in the set, and the .debug_abbrev.dwo must
11447 be the second entry. Other members of the set may follow in any order.
11448
11449 ---
11450
11451 DWP Version 2:
11452
11453 DWP Version 2 combines all the .debug_info, etc. sections into one,
11454 and the entries in the index tables are now offsets into these sections.
11455 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11456 section.
11457
11458 Index Section Contents:
11459 Header
11460 Hash Table of Signatures dwp_hash_table.hash_table
11461 Parallel Table of Indices dwp_hash_table.unit_table
11462 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11463 Table of Section Sizes dwp_hash_table.v2.sizes
11464
11465 The index section header consists of:
11466
11467 V, 32 bit version number
11468 L, 32 bit number of columns in the table of section offsets
11469 N, 32 bit number of compilation units or type units in the index
11470 M, 32 bit number of slots in the hash table
11471
11472 Numbers are recorded using the byte order of the application binary.
11473
11474 The hash table has the same format as version 1.
11475 The parallel table of indices has the same format as version 1,
11476 except that the entries are origin-1 indices into the table of sections
11477 offsets and the table of section sizes.
11478
11479 The table of offsets begins immediately following the parallel table
11480 (at offset 16 + 12 * M from the beginning of the section). The table is
11481 a two-dimensional array of 32-bit words (using the byte order of the
11482 application binary), with L columns and N+1 rows, in row-major order.
11483 Each row in the array is indexed starting from 0. The first row provides
11484 a key to the remaining rows: each column in this row provides an identifier
11485 for a debug section, and the offsets in the same column of subsequent rows
11486 refer to that section. The section identifiers are:
11487
11488 DW_SECT_INFO 1 .debug_info.dwo
11489 DW_SECT_TYPES 2 .debug_types.dwo
11490 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11491 DW_SECT_LINE 4 .debug_line.dwo
11492 DW_SECT_LOC 5 .debug_loc.dwo
11493 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11494 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11495 DW_SECT_MACRO 8 .debug_macro.dwo
11496
11497 The offsets provided by the CU and TU index sections are the base offsets
11498 for the contributions made by each CU or TU to the corresponding section
11499 in the package file. Each CU and TU header contains an abbrev_offset
11500 field, used to find the abbreviations table for that CU or TU within the
11501 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11502 be interpreted as relative to the base offset given in the index section.
11503 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11504 should be interpreted as relative to the base offset for .debug_line.dwo,
11505 and offsets into other debug sections obtained from DWARF attributes should
11506 also be interpreted as relative to the corresponding base offset.
11507
11508 The table of sizes begins immediately following the table of offsets.
11509 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11510 with L columns and N rows, in row-major order. Each row in the array is
11511 indexed starting from 1 (row 0 is shared by the two tables).
11512
11513 ---
11514
11515 Hash table lookup is handled the same in version 1 and 2:
11516
11517 We assume that N and M will not exceed 2^32 - 1.
11518 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11519
11520 Given a 64-bit compilation unit signature or a type signature S, an entry
11521 in the hash table is located as follows:
11522
11523 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11524 the low-order k bits all set to 1.
11525
11526 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11527
11528 3) If the hash table entry at index H matches the signature, use that
11529 entry. If the hash table entry at index H is unused (all zeroes),
11530 terminate the search: the signature is not present in the table.
11531
11532 4) Let H = (H + H') modulo M. Repeat at Step 3.
11533
11534 Because M > N and H' and M are relatively prime, the search is guaranteed
11535 to stop at an unused slot or find the match. */
11536
11537 /* Create a hash table to map DWO IDs to their CU/TU entry in
11538 .debug_{info,types}.dwo in DWP_FILE.
11539 Returns NULL if there isn't one.
11540 Note: This function processes DWP files only, not DWO files. */
11541
11542 static struct dwp_hash_table *
11543 create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11544 struct dwp_file *dwp_file, int is_debug_types)
11545 {
11546 struct objfile *objfile = dwarf2_per_objfile->objfile;
11547 bfd *dbfd = dwp_file->dbfd.get ();
11548 const gdb_byte *index_ptr, *index_end;
11549 struct dwarf2_section_info *index;
11550 uint32_t version, nr_columns, nr_units, nr_slots;
11551 struct dwp_hash_table *htab;
11552
11553 if (is_debug_types)
11554 index = &dwp_file->sections.tu_index;
11555 else
11556 index = &dwp_file->sections.cu_index;
11557
11558 if (index->empty ())
11559 return NULL;
11560 index->read (objfile);
11561
11562 index_ptr = index->buffer;
11563 index_end = index_ptr + index->size;
11564
11565 version = read_4_bytes (dbfd, index_ptr);
11566 index_ptr += 4;
11567 if (version == 2)
11568 nr_columns = read_4_bytes (dbfd, index_ptr);
11569 else
11570 nr_columns = 0;
11571 index_ptr += 4;
11572 nr_units = read_4_bytes (dbfd, index_ptr);
11573 index_ptr += 4;
11574 nr_slots = read_4_bytes (dbfd, index_ptr);
11575 index_ptr += 4;
11576
11577 if (version != 1 && version != 2)
11578 {
11579 error (_("Dwarf Error: unsupported DWP file version (%s)"
11580 " [in module %s]"),
11581 pulongest (version), dwp_file->name);
11582 }
11583 if (nr_slots != (nr_slots & -nr_slots))
11584 {
11585 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11586 " is not power of 2 [in module %s]"),
11587 pulongest (nr_slots), dwp_file->name);
11588 }
11589
11590 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
11591 htab->version = version;
11592 htab->nr_columns = nr_columns;
11593 htab->nr_units = nr_units;
11594 htab->nr_slots = nr_slots;
11595 htab->hash_table = index_ptr;
11596 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11597
11598 /* Exit early if the table is empty. */
11599 if (nr_slots == 0 || nr_units == 0
11600 || (version == 2 && nr_columns == 0))
11601 {
11602 /* All must be zero. */
11603 if (nr_slots != 0 || nr_units != 0
11604 || (version == 2 && nr_columns != 0))
11605 {
11606 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11607 " all zero [in modules %s]"),
11608 dwp_file->name);
11609 }
11610 return htab;
11611 }
11612
11613 if (version == 1)
11614 {
11615 htab->section_pool.v1.indices =
11616 htab->unit_table + sizeof (uint32_t) * nr_slots;
11617 /* It's harder to decide whether the section is too small in v1.
11618 V1 is deprecated anyway so we punt. */
11619 }
11620 else
11621 {
11622 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11623 int *ids = htab->section_pool.v2.section_ids;
11624 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11625 /* Reverse map for error checking. */
11626 int ids_seen[DW_SECT_MAX + 1];
11627 int i;
11628
11629 if (nr_columns < 2)
11630 {
11631 error (_("Dwarf Error: bad DWP hash table, too few columns"
11632 " in section table [in module %s]"),
11633 dwp_file->name);
11634 }
11635 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11636 {
11637 error (_("Dwarf Error: bad DWP hash table, too many columns"
11638 " in section table [in module %s]"),
11639 dwp_file->name);
11640 }
11641 memset (ids, 255, sizeof_ids);
11642 memset (ids_seen, 255, sizeof (ids_seen));
11643 for (i = 0; i < nr_columns; ++i)
11644 {
11645 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11646
11647 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11648 {
11649 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11650 " in section table [in module %s]"),
11651 id, dwp_file->name);
11652 }
11653 if (ids_seen[id] != -1)
11654 {
11655 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11656 " id %d in section table [in module %s]"),
11657 id, dwp_file->name);
11658 }
11659 ids_seen[id] = i;
11660 ids[i] = id;
11661 }
11662 /* Must have exactly one info or types section. */
11663 if (((ids_seen[DW_SECT_INFO] != -1)
11664 + (ids_seen[DW_SECT_TYPES] != -1))
11665 != 1)
11666 {
11667 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11668 " DWO info/types section [in module %s]"),
11669 dwp_file->name);
11670 }
11671 /* Must have an abbrev section. */
11672 if (ids_seen[DW_SECT_ABBREV] == -1)
11673 {
11674 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11675 " section [in module %s]"),
11676 dwp_file->name);
11677 }
11678 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11679 htab->section_pool.v2.sizes =
11680 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11681 * nr_units * nr_columns);
11682 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11683 * nr_units * nr_columns))
11684 > index_end)
11685 {
11686 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11687 " [in module %s]"),
11688 dwp_file->name);
11689 }
11690 }
11691
11692 return htab;
11693 }
11694
11695 /* Update SECTIONS with the data from SECTP.
11696
11697 This function is like the other "locate" section routines that are
11698 passed to bfd_map_over_sections, but in this context the sections to
11699 read comes from the DWP V1 hash table, not the full ELF section table.
11700
11701 The result is non-zero for success, or zero if an error was found. */
11702
11703 static int
11704 locate_v1_virtual_dwo_sections (asection *sectp,
11705 struct virtual_v1_dwo_sections *sections)
11706 {
11707 const struct dwop_section_names *names = &dwop_section_names;
11708
11709 if (section_is_p (sectp->name, &names->abbrev_dwo))
11710 {
11711 /* There can be only one. */
11712 if (sections->abbrev.s.section != NULL)
11713 return 0;
11714 sections->abbrev.s.section = sectp;
11715 sections->abbrev.size = bfd_section_size (sectp);
11716 }
11717 else if (section_is_p (sectp->name, &names->info_dwo)
11718 || section_is_p (sectp->name, &names->types_dwo))
11719 {
11720 /* There can be only one. */
11721 if (sections->info_or_types.s.section != NULL)
11722 return 0;
11723 sections->info_or_types.s.section = sectp;
11724 sections->info_or_types.size = bfd_section_size (sectp);
11725 }
11726 else if (section_is_p (sectp->name, &names->line_dwo))
11727 {
11728 /* There can be only one. */
11729 if (sections->line.s.section != NULL)
11730 return 0;
11731 sections->line.s.section = sectp;
11732 sections->line.size = bfd_section_size (sectp);
11733 }
11734 else if (section_is_p (sectp->name, &names->loc_dwo))
11735 {
11736 /* There can be only one. */
11737 if (sections->loc.s.section != NULL)
11738 return 0;
11739 sections->loc.s.section = sectp;
11740 sections->loc.size = bfd_section_size (sectp);
11741 }
11742 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11743 {
11744 /* There can be only one. */
11745 if (sections->macinfo.s.section != NULL)
11746 return 0;
11747 sections->macinfo.s.section = sectp;
11748 sections->macinfo.size = bfd_section_size (sectp);
11749 }
11750 else if (section_is_p (sectp->name, &names->macro_dwo))
11751 {
11752 /* There can be only one. */
11753 if (sections->macro.s.section != NULL)
11754 return 0;
11755 sections->macro.s.section = sectp;
11756 sections->macro.size = bfd_section_size (sectp);
11757 }
11758 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11759 {
11760 /* There can be only one. */
11761 if (sections->str_offsets.s.section != NULL)
11762 return 0;
11763 sections->str_offsets.s.section = sectp;
11764 sections->str_offsets.size = bfd_section_size (sectp);
11765 }
11766 else
11767 {
11768 /* No other kind of section is valid. */
11769 return 0;
11770 }
11771
11772 return 1;
11773 }
11774
11775 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11776 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11777 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11778 This is for DWP version 1 files. */
11779
11780 static struct dwo_unit *
11781 create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11782 struct dwp_file *dwp_file,
11783 uint32_t unit_index,
11784 const char *comp_dir,
11785 ULONGEST signature, int is_debug_types)
11786 {
11787 const struct dwp_hash_table *dwp_htab =
11788 is_debug_types ? dwp_file->tus : dwp_file->cus;
11789 bfd *dbfd = dwp_file->dbfd.get ();
11790 const char *kind = is_debug_types ? "TU" : "CU";
11791 struct dwo_file *dwo_file;
11792 struct dwo_unit *dwo_unit;
11793 struct virtual_v1_dwo_sections sections;
11794 void **dwo_file_slot;
11795 int i;
11796
11797 gdb_assert (dwp_file->version == 1);
11798
11799 if (dwarf_read_debug)
11800 {
11801 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
11802 kind,
11803 pulongest (unit_index), hex_string (signature),
11804 dwp_file->name);
11805 }
11806
11807 /* Fetch the sections of this DWO unit.
11808 Put a limit on the number of sections we look for so that bad data
11809 doesn't cause us to loop forever. */
11810
11811 #define MAX_NR_V1_DWO_SECTIONS \
11812 (1 /* .debug_info or .debug_types */ \
11813 + 1 /* .debug_abbrev */ \
11814 + 1 /* .debug_line */ \
11815 + 1 /* .debug_loc */ \
11816 + 1 /* .debug_str_offsets */ \
11817 + 1 /* .debug_macro or .debug_macinfo */ \
11818 + 1 /* trailing zero */)
11819
11820 memset (&sections, 0, sizeof (sections));
11821
11822 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11823 {
11824 asection *sectp;
11825 uint32_t section_nr =
11826 read_4_bytes (dbfd,
11827 dwp_htab->section_pool.v1.indices
11828 + (unit_index + i) * sizeof (uint32_t));
11829
11830 if (section_nr == 0)
11831 break;
11832 if (section_nr >= dwp_file->num_sections)
11833 {
11834 error (_("Dwarf Error: bad DWP hash table, section number too large"
11835 " [in module %s]"),
11836 dwp_file->name);
11837 }
11838
11839 sectp = dwp_file->elf_sections[section_nr];
11840 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11841 {
11842 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11843 " [in module %s]"),
11844 dwp_file->name);
11845 }
11846 }
11847
11848 if (i < 2
11849 || sections.info_or_types.empty ()
11850 || sections.abbrev.empty ())
11851 {
11852 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11853 " [in module %s]"),
11854 dwp_file->name);
11855 }
11856 if (i == MAX_NR_V1_DWO_SECTIONS)
11857 {
11858 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11859 " [in module %s]"),
11860 dwp_file->name);
11861 }
11862
11863 /* It's easier for the rest of the code if we fake a struct dwo_file and
11864 have dwo_unit "live" in that. At least for now.
11865
11866 The DWP file can be made up of a random collection of CUs and TUs.
11867 However, for each CU + set of TUs that came from the same original DWO
11868 file, we can combine them back into a virtual DWO file to save space
11869 (fewer struct dwo_file objects to allocate). Remember that for really
11870 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11871
11872 std::string virtual_dwo_name =
11873 string_printf ("virtual-dwo/%d-%d-%d-%d",
11874 sections.abbrev.get_id (),
11875 sections.line.get_id (),
11876 sections.loc.get_id (),
11877 sections.str_offsets.get_id ());
11878 /* Can we use an existing virtual DWO file? */
11879 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11880 virtual_dwo_name.c_str (),
11881 comp_dir);
11882 /* Create one if necessary. */
11883 if (*dwo_file_slot == NULL)
11884 {
11885 if (dwarf_read_debug)
11886 {
11887 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
11888 virtual_dwo_name.c_str ());
11889 }
11890 dwo_file = new struct dwo_file;
11891 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
11892 dwo_file->comp_dir = comp_dir;
11893 dwo_file->sections.abbrev = sections.abbrev;
11894 dwo_file->sections.line = sections.line;
11895 dwo_file->sections.loc = sections.loc;
11896 dwo_file->sections.macinfo = sections.macinfo;
11897 dwo_file->sections.macro = sections.macro;
11898 dwo_file->sections.str_offsets = sections.str_offsets;
11899 /* The "str" section is global to the entire DWP file. */
11900 dwo_file->sections.str = dwp_file->sections.str;
11901 /* The info or types section is assigned below to dwo_unit,
11902 there's no need to record it in dwo_file.
11903 Also, we can't simply record type sections in dwo_file because
11904 we record a pointer into the vector in dwo_unit. As we collect more
11905 types we'll grow the vector and eventually have to reallocate space
11906 for it, invalidating all copies of pointers into the previous
11907 contents. */
11908 *dwo_file_slot = dwo_file;
11909 }
11910 else
11911 {
11912 if (dwarf_read_debug)
11913 {
11914 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
11915 virtual_dwo_name.c_str ());
11916 }
11917 dwo_file = (struct dwo_file *) *dwo_file_slot;
11918 }
11919
11920 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
11921 dwo_unit->dwo_file = dwo_file;
11922 dwo_unit->signature = signature;
11923 dwo_unit->section =
11924 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11925 *dwo_unit->section = sections.info_or_types;
11926 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11927
11928 return dwo_unit;
11929 }
11930
11931 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11932 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11933 piece within that section used by a TU/CU, return a virtual section
11934 of just that piece. */
11935
11936 static struct dwarf2_section_info
11937 create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11938 struct dwarf2_section_info *section,
11939 bfd_size_type offset, bfd_size_type size)
11940 {
11941 struct dwarf2_section_info result;
11942 asection *sectp;
11943
11944 gdb_assert (section != NULL);
11945 gdb_assert (!section->is_virtual);
11946
11947 memset (&result, 0, sizeof (result));
11948 result.s.containing_section = section;
11949 result.is_virtual = true;
11950
11951 if (size == 0)
11952 return result;
11953
11954 sectp = section->get_bfd_section ();
11955
11956 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11957 bounds of the real section. This is a pretty-rare event, so just
11958 flag an error (easier) instead of a warning and trying to cope. */
11959 if (sectp == NULL
11960 || offset + size > bfd_section_size (sectp))
11961 {
11962 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11963 " in section %s [in module %s]"),
11964 sectp ? bfd_section_name (sectp) : "<unknown>",
11965 objfile_name (dwarf2_per_objfile->objfile));
11966 }
11967
11968 result.virtual_offset = offset;
11969 result.size = size;
11970 return result;
11971 }
11972
11973 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11974 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11975 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11976 This is for DWP version 2 files. */
11977
11978 static struct dwo_unit *
11979 create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11980 struct dwp_file *dwp_file,
11981 uint32_t unit_index,
11982 const char *comp_dir,
11983 ULONGEST signature, int is_debug_types)
11984 {
11985 const struct dwp_hash_table *dwp_htab =
11986 is_debug_types ? dwp_file->tus : dwp_file->cus;
11987 bfd *dbfd = dwp_file->dbfd.get ();
11988 const char *kind = is_debug_types ? "TU" : "CU";
11989 struct dwo_file *dwo_file;
11990 struct dwo_unit *dwo_unit;
11991 struct virtual_v2_dwo_sections sections;
11992 void **dwo_file_slot;
11993 int i;
11994
11995 gdb_assert (dwp_file->version == 2);
11996
11997 if (dwarf_read_debug)
11998 {
11999 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12000 kind,
12001 pulongest (unit_index), hex_string (signature),
12002 dwp_file->name);
12003 }
12004
12005 /* Fetch the section offsets of this DWO unit. */
12006
12007 memset (&sections, 0, sizeof (sections));
12008
12009 for (i = 0; i < dwp_htab->nr_columns; ++i)
12010 {
12011 uint32_t offset = read_4_bytes (dbfd,
12012 dwp_htab->section_pool.v2.offsets
12013 + (((unit_index - 1) * dwp_htab->nr_columns
12014 + i)
12015 * sizeof (uint32_t)));
12016 uint32_t size = read_4_bytes (dbfd,
12017 dwp_htab->section_pool.v2.sizes
12018 + (((unit_index - 1) * dwp_htab->nr_columns
12019 + i)
12020 * sizeof (uint32_t)));
12021
12022 switch (dwp_htab->section_pool.v2.section_ids[i])
12023 {
12024 case DW_SECT_INFO:
12025 case DW_SECT_TYPES:
12026 sections.info_or_types_offset = offset;
12027 sections.info_or_types_size = size;
12028 break;
12029 case DW_SECT_ABBREV:
12030 sections.abbrev_offset = offset;
12031 sections.abbrev_size = size;
12032 break;
12033 case DW_SECT_LINE:
12034 sections.line_offset = offset;
12035 sections.line_size = size;
12036 break;
12037 case DW_SECT_LOC:
12038 sections.loc_offset = offset;
12039 sections.loc_size = size;
12040 break;
12041 case DW_SECT_STR_OFFSETS:
12042 sections.str_offsets_offset = offset;
12043 sections.str_offsets_size = size;
12044 break;
12045 case DW_SECT_MACINFO:
12046 sections.macinfo_offset = offset;
12047 sections.macinfo_size = size;
12048 break;
12049 case DW_SECT_MACRO:
12050 sections.macro_offset = offset;
12051 sections.macro_size = size;
12052 break;
12053 }
12054 }
12055
12056 /* It's easier for the rest of the code if we fake a struct dwo_file and
12057 have dwo_unit "live" in that. At least for now.
12058
12059 The DWP file can be made up of a random collection of CUs and TUs.
12060 However, for each CU + set of TUs that came from the same original DWO
12061 file, we can combine them back into a virtual DWO file to save space
12062 (fewer struct dwo_file objects to allocate). Remember that for really
12063 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12064
12065 std::string virtual_dwo_name =
12066 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12067 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12068 (long) (sections.line_size ? sections.line_offset : 0),
12069 (long) (sections.loc_size ? sections.loc_offset : 0),
12070 (long) (sections.str_offsets_size
12071 ? sections.str_offsets_offset : 0));
12072 /* Can we use an existing virtual DWO file? */
12073 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12074 virtual_dwo_name.c_str (),
12075 comp_dir);
12076 /* Create one if necessary. */
12077 if (*dwo_file_slot == NULL)
12078 {
12079 if (dwarf_read_debug)
12080 {
12081 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
12082 virtual_dwo_name.c_str ());
12083 }
12084 dwo_file = new struct dwo_file;
12085 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
12086 dwo_file->comp_dir = comp_dir;
12087 dwo_file->sections.abbrev =
12088 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
12089 sections.abbrev_offset, sections.abbrev_size);
12090 dwo_file->sections.line =
12091 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
12092 sections.line_offset, sections.line_size);
12093 dwo_file->sections.loc =
12094 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
12095 sections.loc_offset, sections.loc_size);
12096 dwo_file->sections.macinfo =
12097 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
12098 sections.macinfo_offset, sections.macinfo_size);
12099 dwo_file->sections.macro =
12100 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
12101 sections.macro_offset, sections.macro_size);
12102 dwo_file->sections.str_offsets =
12103 create_dwp_v2_section (dwarf2_per_objfile,
12104 &dwp_file->sections.str_offsets,
12105 sections.str_offsets_offset,
12106 sections.str_offsets_size);
12107 /* The "str" section is global to the entire DWP file. */
12108 dwo_file->sections.str = dwp_file->sections.str;
12109 /* The info or types section is assigned below to dwo_unit,
12110 there's no need to record it in dwo_file.
12111 Also, we can't simply record type sections in dwo_file because
12112 we record a pointer into the vector in dwo_unit. As we collect more
12113 types we'll grow the vector and eventually have to reallocate space
12114 for it, invalidating all copies of pointers into the previous
12115 contents. */
12116 *dwo_file_slot = dwo_file;
12117 }
12118 else
12119 {
12120 if (dwarf_read_debug)
12121 {
12122 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
12123 virtual_dwo_name.c_str ());
12124 }
12125 dwo_file = (struct dwo_file *) *dwo_file_slot;
12126 }
12127
12128 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
12129 dwo_unit->dwo_file = dwo_file;
12130 dwo_unit->signature = signature;
12131 dwo_unit->section =
12132 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12133 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12134 is_debug_types
12135 ? &dwp_file->sections.types
12136 : &dwp_file->sections.info,
12137 sections.info_or_types_offset,
12138 sections.info_or_types_size);
12139 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12140
12141 return dwo_unit;
12142 }
12143
12144 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12145 Returns NULL if the signature isn't found. */
12146
12147 static struct dwo_unit *
12148 lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12149 struct dwp_file *dwp_file, const char *comp_dir,
12150 ULONGEST signature, int is_debug_types)
12151 {
12152 const struct dwp_hash_table *dwp_htab =
12153 is_debug_types ? dwp_file->tus : dwp_file->cus;
12154 bfd *dbfd = dwp_file->dbfd.get ();
12155 uint32_t mask = dwp_htab->nr_slots - 1;
12156 uint32_t hash = signature & mask;
12157 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12158 unsigned int i;
12159 void **slot;
12160 struct dwo_unit find_dwo_cu;
12161
12162 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12163 find_dwo_cu.signature = signature;
12164 slot = htab_find_slot (is_debug_types
12165 ? dwp_file->loaded_tus.get ()
12166 : dwp_file->loaded_cus.get (),
12167 &find_dwo_cu, INSERT);
12168
12169 if (*slot != NULL)
12170 return (struct dwo_unit *) *slot;
12171
12172 /* Use a for loop so that we don't loop forever on bad debug info. */
12173 for (i = 0; i < dwp_htab->nr_slots; ++i)
12174 {
12175 ULONGEST signature_in_table;
12176
12177 signature_in_table =
12178 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12179 if (signature_in_table == signature)
12180 {
12181 uint32_t unit_index =
12182 read_4_bytes (dbfd,
12183 dwp_htab->unit_table + hash * sizeof (uint32_t));
12184
12185 if (dwp_file->version == 1)
12186 {
12187 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12188 dwp_file, unit_index,
12189 comp_dir, signature,
12190 is_debug_types);
12191 }
12192 else
12193 {
12194 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12195 dwp_file, unit_index,
12196 comp_dir, signature,
12197 is_debug_types);
12198 }
12199 return (struct dwo_unit *) *slot;
12200 }
12201 if (signature_in_table == 0)
12202 return NULL;
12203 hash = (hash + hash2) & mask;
12204 }
12205
12206 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12207 " [in module %s]"),
12208 dwp_file->name);
12209 }
12210
12211 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12212 Open the file specified by FILE_NAME and hand it off to BFD for
12213 preliminary analysis. Return a newly initialized bfd *, which
12214 includes a canonicalized copy of FILE_NAME.
12215 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12216 SEARCH_CWD is true if the current directory is to be searched.
12217 It will be searched before debug-file-directory.
12218 If successful, the file is added to the bfd include table of the
12219 objfile's bfd (see gdb_bfd_record_inclusion).
12220 If unable to find/open the file, return NULL.
12221 NOTE: This function is derived from symfile_bfd_open. */
12222
12223 static gdb_bfd_ref_ptr
12224 try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12225 const char *file_name, int is_dwp, int search_cwd)
12226 {
12227 int desc;
12228 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12229 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12230 to debug_file_directory. */
12231 const char *search_path;
12232 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12233
12234 gdb::unique_xmalloc_ptr<char> search_path_holder;
12235 if (search_cwd)
12236 {
12237 if (*debug_file_directory != '\0')
12238 {
12239 search_path_holder.reset (concat (".", dirname_separator_string,
12240 debug_file_directory,
12241 (char *) NULL));
12242 search_path = search_path_holder.get ();
12243 }
12244 else
12245 search_path = ".";
12246 }
12247 else
12248 search_path = debug_file_directory;
12249
12250 openp_flags flags = OPF_RETURN_REALPATH;
12251 if (is_dwp)
12252 flags |= OPF_SEARCH_IN_PATH;
12253
12254 gdb::unique_xmalloc_ptr<char> absolute_name;
12255 desc = openp (search_path, flags, file_name,
12256 O_RDONLY | O_BINARY, &absolute_name);
12257 if (desc < 0)
12258 return NULL;
12259
12260 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12261 gnutarget, desc));
12262 if (sym_bfd == NULL)
12263 return NULL;
12264 bfd_set_cacheable (sym_bfd.get (), 1);
12265
12266 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12267 return NULL;
12268
12269 /* Success. Record the bfd as having been included by the objfile's bfd.
12270 This is important because things like demangled_names_hash lives in the
12271 objfile's per_bfd space and may have references to things like symbol
12272 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12273 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
12274
12275 return sym_bfd;
12276 }
12277
12278 /* Try to open DWO file FILE_NAME.
12279 COMP_DIR is the DW_AT_comp_dir attribute.
12280 The result is the bfd handle of the file.
12281 If there is a problem finding or opening the file, return NULL.
12282 Upon success, the canonicalized path of the file is stored in the bfd,
12283 same as symfile_bfd_open. */
12284
12285 static gdb_bfd_ref_ptr
12286 open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12287 const char *file_name, const char *comp_dir)
12288 {
12289 if (IS_ABSOLUTE_PATH (file_name))
12290 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12291 0 /*is_dwp*/, 0 /*search_cwd*/);
12292
12293 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12294
12295 if (comp_dir != NULL)
12296 {
12297 gdb::unique_xmalloc_ptr<char> path_to_try
12298 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12299
12300 /* NOTE: If comp_dir is a relative path, this will also try the
12301 search path, which seems useful. */
12302 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12303 path_to_try.get (),
12304 0 /*is_dwp*/,
12305 1 /*search_cwd*/));
12306 if (abfd != NULL)
12307 return abfd;
12308 }
12309
12310 /* That didn't work, try debug-file-directory, which, despite its name,
12311 is a list of paths. */
12312
12313 if (*debug_file_directory == '\0')
12314 return NULL;
12315
12316 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12317 0 /*is_dwp*/, 1 /*search_cwd*/);
12318 }
12319
12320 /* This function is mapped across the sections and remembers the offset and
12321 size of each of the DWO debugging sections we are interested in. */
12322
12323 static void
12324 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12325 {
12326 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
12327 const struct dwop_section_names *names = &dwop_section_names;
12328
12329 if (section_is_p (sectp->name, &names->abbrev_dwo))
12330 {
12331 dwo_sections->abbrev.s.section = sectp;
12332 dwo_sections->abbrev.size = bfd_section_size (sectp);
12333 }
12334 else if (section_is_p (sectp->name, &names->info_dwo))
12335 {
12336 dwo_sections->info.s.section = sectp;
12337 dwo_sections->info.size = bfd_section_size (sectp);
12338 }
12339 else if (section_is_p (sectp->name, &names->line_dwo))
12340 {
12341 dwo_sections->line.s.section = sectp;
12342 dwo_sections->line.size = bfd_section_size (sectp);
12343 }
12344 else if (section_is_p (sectp->name, &names->loc_dwo))
12345 {
12346 dwo_sections->loc.s.section = sectp;
12347 dwo_sections->loc.size = bfd_section_size (sectp);
12348 }
12349 else if (section_is_p (sectp->name, &names->loclists_dwo))
12350 {
12351 dwo_sections->loclists.s.section = sectp;
12352 dwo_sections->loclists.size = bfd_section_size (sectp);
12353 }
12354 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12355 {
12356 dwo_sections->macinfo.s.section = sectp;
12357 dwo_sections->macinfo.size = bfd_section_size (sectp);
12358 }
12359 else if (section_is_p (sectp->name, &names->macro_dwo))
12360 {
12361 dwo_sections->macro.s.section = sectp;
12362 dwo_sections->macro.size = bfd_section_size (sectp);
12363 }
12364 else if (section_is_p (sectp->name, &names->str_dwo))
12365 {
12366 dwo_sections->str.s.section = sectp;
12367 dwo_sections->str.size = bfd_section_size (sectp);
12368 }
12369 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12370 {
12371 dwo_sections->str_offsets.s.section = sectp;
12372 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12373 }
12374 else if (section_is_p (sectp->name, &names->types_dwo))
12375 {
12376 struct dwarf2_section_info type_section;
12377
12378 memset (&type_section, 0, sizeof (type_section));
12379 type_section.s.section = sectp;
12380 type_section.size = bfd_section_size (sectp);
12381 dwo_sections->types.push_back (type_section);
12382 }
12383 }
12384
12385 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12386 by PER_CU. This is for the non-DWP case.
12387 The result is NULL if DWO_NAME can't be found. */
12388
12389 static struct dwo_file *
12390 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12391 const char *comp_dir)
12392 {
12393 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12394
12395 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
12396 if (dbfd == NULL)
12397 {
12398 if (dwarf_read_debug)
12399 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12400 return NULL;
12401 }
12402
12403 dwo_file_up dwo_file (new struct dwo_file);
12404 dwo_file->dwo_name = dwo_name;
12405 dwo_file->comp_dir = comp_dir;
12406 dwo_file->dbfd = std::move (dbfd);
12407
12408 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
12409 &dwo_file->sections);
12410
12411 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
12412 dwo_file->sections.info, dwo_file->cus);
12413
12414 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
12415 dwo_file->sections.types, dwo_file->tus);
12416
12417 if (dwarf_read_debug)
12418 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12419
12420 return dwo_file.release ();
12421 }
12422
12423 /* This function is mapped across the sections and remembers the offset and
12424 size of each of the DWP debugging sections common to version 1 and 2 that
12425 we are interested in. */
12426
12427 static void
12428 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12429 void *dwp_file_ptr)
12430 {
12431 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12432 const struct dwop_section_names *names = &dwop_section_names;
12433 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12434
12435 /* Record the ELF section number for later lookup: this is what the
12436 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12437 gdb_assert (elf_section_nr < dwp_file->num_sections);
12438 dwp_file->elf_sections[elf_section_nr] = sectp;
12439
12440 /* Look for specific sections that we need. */
12441 if (section_is_p (sectp->name, &names->str_dwo))
12442 {
12443 dwp_file->sections.str.s.section = sectp;
12444 dwp_file->sections.str.size = bfd_section_size (sectp);
12445 }
12446 else if (section_is_p (sectp->name, &names->cu_index))
12447 {
12448 dwp_file->sections.cu_index.s.section = sectp;
12449 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12450 }
12451 else if (section_is_p (sectp->name, &names->tu_index))
12452 {
12453 dwp_file->sections.tu_index.s.section = sectp;
12454 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12455 }
12456 }
12457
12458 /* This function is mapped across the sections and remembers the offset and
12459 size of each of the DWP version 2 debugging sections that we are interested
12460 in. This is split into a separate function because we don't know if we
12461 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12462
12463 static void
12464 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12465 {
12466 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12467 const struct dwop_section_names *names = &dwop_section_names;
12468 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12469
12470 /* Record the ELF section number for later lookup: this is what the
12471 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12472 gdb_assert (elf_section_nr < dwp_file->num_sections);
12473 dwp_file->elf_sections[elf_section_nr] = sectp;
12474
12475 /* Look for specific sections that we need. */
12476 if (section_is_p (sectp->name, &names->abbrev_dwo))
12477 {
12478 dwp_file->sections.abbrev.s.section = sectp;
12479 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12480 }
12481 else if (section_is_p (sectp->name, &names->info_dwo))
12482 {
12483 dwp_file->sections.info.s.section = sectp;
12484 dwp_file->sections.info.size = bfd_section_size (sectp);
12485 }
12486 else if (section_is_p (sectp->name, &names->line_dwo))
12487 {
12488 dwp_file->sections.line.s.section = sectp;
12489 dwp_file->sections.line.size = bfd_section_size (sectp);
12490 }
12491 else if (section_is_p (sectp->name, &names->loc_dwo))
12492 {
12493 dwp_file->sections.loc.s.section = sectp;
12494 dwp_file->sections.loc.size = bfd_section_size (sectp);
12495 }
12496 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12497 {
12498 dwp_file->sections.macinfo.s.section = sectp;
12499 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12500 }
12501 else if (section_is_p (sectp->name, &names->macro_dwo))
12502 {
12503 dwp_file->sections.macro.s.section = sectp;
12504 dwp_file->sections.macro.size = bfd_section_size (sectp);
12505 }
12506 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12507 {
12508 dwp_file->sections.str_offsets.s.section = sectp;
12509 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12510 }
12511 else if (section_is_p (sectp->name, &names->types_dwo))
12512 {
12513 dwp_file->sections.types.s.section = sectp;
12514 dwp_file->sections.types.size = bfd_section_size (sectp);
12515 }
12516 }
12517
12518 /* Hash function for dwp_file loaded CUs/TUs. */
12519
12520 static hashval_t
12521 hash_dwp_loaded_cutus (const void *item)
12522 {
12523 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12524
12525 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12526 return dwo_unit->signature;
12527 }
12528
12529 /* Equality function for dwp_file loaded CUs/TUs. */
12530
12531 static int
12532 eq_dwp_loaded_cutus (const void *a, const void *b)
12533 {
12534 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12535 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12536
12537 return dua->signature == dub->signature;
12538 }
12539
12540 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12541
12542 static htab_up
12543 allocate_dwp_loaded_cutus_table ()
12544 {
12545 return htab_up (htab_create_alloc (3,
12546 hash_dwp_loaded_cutus,
12547 eq_dwp_loaded_cutus,
12548 NULL, xcalloc, xfree));
12549 }
12550
12551 /* Try to open DWP file FILE_NAME.
12552 The result is the bfd handle of the file.
12553 If there is a problem finding or opening the file, return NULL.
12554 Upon success, the canonicalized path of the file is stored in the bfd,
12555 same as symfile_bfd_open. */
12556
12557 static gdb_bfd_ref_ptr
12558 open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12559 const char *file_name)
12560 {
12561 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12562 1 /*is_dwp*/,
12563 1 /*search_cwd*/));
12564 if (abfd != NULL)
12565 return abfd;
12566
12567 /* Work around upstream bug 15652.
12568 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12569 [Whether that's a "bug" is debatable, but it is getting in our way.]
12570 We have no real idea where the dwp file is, because gdb's realpath-ing
12571 of the executable's path may have discarded the needed info.
12572 [IWBN if the dwp file name was recorded in the executable, akin to
12573 .gnu_debuglink, but that doesn't exist yet.]
12574 Strip the directory from FILE_NAME and search again. */
12575 if (*debug_file_directory != '\0')
12576 {
12577 /* Don't implicitly search the current directory here.
12578 If the user wants to search "." to handle this case,
12579 it must be added to debug-file-directory. */
12580 return try_open_dwop_file (dwarf2_per_objfile,
12581 lbasename (file_name), 1 /*is_dwp*/,
12582 0 /*search_cwd*/);
12583 }
12584
12585 return NULL;
12586 }
12587
12588 /* Initialize the use of the DWP file for the current objfile.
12589 By convention the name of the DWP file is ${objfile}.dwp.
12590 The result is NULL if it can't be found. */
12591
12592 static std::unique_ptr<struct dwp_file>
12593 open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12594 {
12595 struct objfile *objfile = dwarf2_per_objfile->objfile;
12596
12597 /* Try to find first .dwp for the binary file before any symbolic links
12598 resolving. */
12599
12600 /* If the objfile is a debug file, find the name of the real binary
12601 file and get the name of dwp file from there. */
12602 std::string dwp_name;
12603 if (objfile->separate_debug_objfile_backlink != NULL)
12604 {
12605 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12606 const char *backlink_basename = lbasename (backlink->original_name);
12607
12608 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12609 }
12610 else
12611 dwp_name = objfile->original_name;
12612
12613 dwp_name += ".dwp";
12614
12615 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
12616 if (dbfd == NULL
12617 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12618 {
12619 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12620 dwp_name = objfile_name (objfile);
12621 dwp_name += ".dwp";
12622 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
12623 }
12624
12625 if (dbfd == NULL)
12626 {
12627 if (dwarf_read_debug)
12628 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
12629 return std::unique_ptr<dwp_file> ();
12630 }
12631
12632 const char *name = bfd_get_filename (dbfd.get ());
12633 std::unique_ptr<struct dwp_file> dwp_file
12634 (new struct dwp_file (name, std::move (dbfd)));
12635
12636 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12637 dwp_file->elf_sections =
12638 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
12639 dwp_file->num_sections, asection *);
12640
12641 bfd_map_over_sections (dwp_file->dbfd.get (),
12642 dwarf2_locate_common_dwp_sections,
12643 dwp_file.get ());
12644
12645 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12646 0);
12647
12648 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12649 1);
12650
12651 /* The DWP file version is stored in the hash table. Oh well. */
12652 if (dwp_file->cus && dwp_file->tus
12653 && dwp_file->cus->version != dwp_file->tus->version)
12654 {
12655 /* Technically speaking, we should try to limp along, but this is
12656 pretty bizarre. We use pulongest here because that's the established
12657 portability solution (e.g, we cannot use %u for uint32_t). */
12658 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12659 " TU version %s [in DWP file %s]"),
12660 pulongest (dwp_file->cus->version),
12661 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12662 }
12663
12664 if (dwp_file->cus)
12665 dwp_file->version = dwp_file->cus->version;
12666 else if (dwp_file->tus)
12667 dwp_file->version = dwp_file->tus->version;
12668 else
12669 dwp_file->version = 2;
12670
12671 if (dwp_file->version == 2)
12672 bfd_map_over_sections (dwp_file->dbfd.get (),
12673 dwarf2_locate_v2_dwp_sections,
12674 dwp_file.get ());
12675
12676 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12677 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12678
12679 if (dwarf_read_debug)
12680 {
12681 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12682 fprintf_unfiltered (gdb_stdlog,
12683 " %s CUs, %s TUs\n",
12684 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12685 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12686 }
12687
12688 return dwp_file;
12689 }
12690
12691 /* Wrapper around open_and_init_dwp_file, only open it once. */
12692
12693 static struct dwp_file *
12694 get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
12695 {
12696 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
12697 {
12698 dwarf2_per_objfile->per_bfd->dwp_file
12699 = open_and_init_dwp_file (dwarf2_per_objfile);
12700 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
12701 }
12702 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
12703 }
12704
12705 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12706 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12707 or in the DWP file for the objfile, referenced by THIS_UNIT.
12708 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12709 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12710
12711 This is called, for example, when wanting to read a variable with a
12712 complex location. Therefore we don't want to do file i/o for every call.
12713 Therefore we don't want to look for a DWO file on every call.
12714 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12715 then we check if we've already seen DWO_NAME, and only THEN do we check
12716 for a DWO file.
12717
12718 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12719 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12720
12721 static struct dwo_unit *
12722 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12723 ULONGEST signature, int is_debug_types)
12724 {
12725 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
12726 struct objfile *objfile = dwarf2_per_objfile->objfile;
12727 const char *kind = is_debug_types ? "TU" : "CU";
12728 void **dwo_file_slot;
12729 struct dwo_file *dwo_file;
12730 struct dwp_file *dwp_file;
12731
12732 /* First see if there's a DWP file.
12733 If we have a DWP file but didn't find the DWO inside it, don't
12734 look for the original DWO file. It makes gdb behave differently
12735 depending on whether one is debugging in the build tree. */
12736
12737 dwp_file = get_dwp_file (dwarf2_per_objfile);
12738 if (dwp_file != NULL)
12739 {
12740 const struct dwp_hash_table *dwp_htab =
12741 is_debug_types ? dwp_file->tus : dwp_file->cus;
12742
12743 if (dwp_htab != NULL)
12744 {
12745 struct dwo_unit *dwo_cutu =
12746 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
12747 signature, is_debug_types);
12748
12749 if (dwo_cutu != NULL)
12750 {
12751 if (dwarf_read_debug)
12752 {
12753 fprintf_unfiltered (gdb_stdlog,
12754 "Virtual DWO %s %s found: @%s\n",
12755 kind, hex_string (signature),
12756 host_address_to_string (dwo_cutu));
12757 }
12758 return dwo_cutu;
12759 }
12760 }
12761 }
12762 else
12763 {
12764 /* No DWP file, look for the DWO file. */
12765
12766 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12767 dwo_name, comp_dir);
12768 if (*dwo_file_slot == NULL)
12769 {
12770 /* Read in the file and build a table of the CUs/TUs it contains. */
12771 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12772 }
12773 /* NOTE: This will be NULL if unable to open the file. */
12774 dwo_file = (struct dwo_file *) *dwo_file_slot;
12775
12776 if (dwo_file != NULL)
12777 {
12778 struct dwo_unit *dwo_cutu = NULL;
12779
12780 if (is_debug_types && dwo_file->tus)
12781 {
12782 struct dwo_unit find_dwo_cutu;
12783
12784 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12785 find_dwo_cutu.signature = signature;
12786 dwo_cutu
12787 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12788 &find_dwo_cutu);
12789 }
12790 else if (!is_debug_types && dwo_file->cus)
12791 {
12792 struct dwo_unit find_dwo_cutu;
12793
12794 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12795 find_dwo_cutu.signature = signature;
12796 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
12797 &find_dwo_cutu);
12798 }
12799
12800 if (dwo_cutu != NULL)
12801 {
12802 if (dwarf_read_debug)
12803 {
12804 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12805 kind, dwo_name, hex_string (signature),
12806 host_address_to_string (dwo_cutu));
12807 }
12808 return dwo_cutu;
12809 }
12810 }
12811 }
12812
12813 /* We didn't find it. This could mean a dwo_id mismatch, or
12814 someone deleted the DWO/DWP file, or the search path isn't set up
12815 correctly to find the file. */
12816
12817 if (dwarf_read_debug)
12818 {
12819 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12820 kind, dwo_name, hex_string (signature));
12821 }
12822
12823 /* This is a warning and not a complaint because it can be caused by
12824 pilot error (e.g., user accidentally deleting the DWO). */
12825 {
12826 /* Print the name of the DWP file if we looked there, helps the user
12827 better diagnose the problem. */
12828 std::string dwp_text;
12829
12830 if (dwp_file != NULL)
12831 dwp_text = string_printf (" [in DWP file %s]",
12832 lbasename (dwp_file->name));
12833
12834 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
12835 " [in module %s]"),
12836 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12837 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
12838 }
12839 return NULL;
12840 }
12841
12842 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12843 See lookup_dwo_cutu_unit for details. */
12844
12845 static struct dwo_unit *
12846 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12847 ULONGEST signature)
12848 {
12849 gdb_assert (!cu->per_cu->is_debug_types);
12850
12851 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
12852 }
12853
12854 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12855 See lookup_dwo_cutu_unit for details. */
12856
12857 static struct dwo_unit *
12858 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
12859 {
12860 gdb_assert (cu->per_cu->is_debug_types);
12861
12862 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12863
12864 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
12865 }
12866
12867 /* Traversal function for queue_and_load_all_dwo_tus. */
12868
12869 static int
12870 queue_and_load_dwo_tu (void **slot, void *info)
12871 {
12872 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12873 dwarf2_cu *cu = (dwarf2_cu *) info;
12874 ULONGEST signature = dwo_unit->signature;
12875 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
12876
12877 if (sig_type != NULL)
12878 {
12879 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12880
12881 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12882 a real dependency of PER_CU on SIG_TYPE. That is detected later
12883 while processing PER_CU. */
12884 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
12885 load_full_type_unit (sig_cu, cu->per_objfile);
12886 cu->per_cu->imported_symtabs_push (sig_cu);
12887 }
12888
12889 return 1;
12890 }
12891
12892 /* Queue all TUs contained in the DWO of CU to be read in.
12893 The DWO may have the only definition of the type, though it may not be
12894 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12895 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12896
12897 static void
12898 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
12899 {
12900 struct dwo_unit *dwo_unit;
12901 struct dwo_file *dwo_file;
12902
12903 gdb_assert (cu != nullptr);
12904 gdb_assert (!cu->per_cu->is_debug_types);
12905 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
12906
12907 dwo_unit = cu->dwo_unit;
12908 gdb_assert (dwo_unit != NULL);
12909
12910 dwo_file = dwo_unit->dwo_file;
12911 if (dwo_file->tus != NULL)
12912 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
12913 }
12914
12915 /* Read in various DIEs. */
12916
12917 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
12918 Inherit only the children of the DW_AT_abstract_origin DIE not being
12919 already referenced by DW_AT_abstract_origin from the children of the
12920 current DIE. */
12921
12922 static void
12923 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12924 {
12925 struct die_info *child_die;
12926 sect_offset *offsetp;
12927 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12928 struct die_info *origin_die;
12929 /* Iterator of the ORIGIN_DIE children. */
12930 struct die_info *origin_child_die;
12931 struct attribute *attr;
12932 struct dwarf2_cu *origin_cu;
12933 struct pending **origin_previous_list_in_scope;
12934
12935 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12936 if (!attr)
12937 return;
12938
12939 /* Note that following die references may follow to a die in a
12940 different cu. */
12941
12942 origin_cu = cu;
12943 origin_die = follow_die_ref (die, attr, &origin_cu);
12944
12945 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12946 symbols in. */
12947 origin_previous_list_in_scope = origin_cu->list_in_scope;
12948 origin_cu->list_in_scope = cu->list_in_scope;
12949
12950 if (die->tag != origin_die->tag
12951 && !(die->tag == DW_TAG_inlined_subroutine
12952 && origin_die->tag == DW_TAG_subprogram))
12953 complaint (_("DIE %s and its abstract origin %s have different tags"),
12954 sect_offset_str (die->sect_off),
12955 sect_offset_str (origin_die->sect_off));
12956
12957 std::vector<sect_offset> offsets;
12958
12959 for (child_die = die->child;
12960 child_die && child_die->tag;
12961 child_die = child_die->sibling)
12962 {
12963 struct die_info *child_origin_die;
12964 struct dwarf2_cu *child_origin_cu;
12965
12966 /* We are trying to process concrete instance entries:
12967 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
12968 it's not relevant to our analysis here. i.e. detecting DIEs that are
12969 present in the abstract instance but not referenced in the concrete
12970 one. */
12971 if (child_die->tag == DW_TAG_call_site
12972 || child_die->tag == DW_TAG_GNU_call_site)
12973 continue;
12974
12975 /* For each CHILD_DIE, find the corresponding child of
12976 ORIGIN_DIE. If there is more than one layer of
12977 DW_AT_abstract_origin, follow them all; there shouldn't be,
12978 but GCC versions at least through 4.4 generate this (GCC PR
12979 40573). */
12980 child_origin_die = child_die;
12981 child_origin_cu = cu;
12982 while (1)
12983 {
12984 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12985 child_origin_cu);
12986 if (attr == NULL)
12987 break;
12988 child_origin_die = follow_die_ref (child_origin_die, attr,
12989 &child_origin_cu);
12990 }
12991
12992 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12993 counterpart may exist. */
12994 if (child_origin_die != child_die)
12995 {
12996 if (child_die->tag != child_origin_die->tag
12997 && !(child_die->tag == DW_TAG_inlined_subroutine
12998 && child_origin_die->tag == DW_TAG_subprogram))
12999 complaint (_("Child DIE %s and its abstract origin %s have "
13000 "different tags"),
13001 sect_offset_str (child_die->sect_off),
13002 sect_offset_str (child_origin_die->sect_off));
13003 if (child_origin_die->parent != origin_die)
13004 complaint (_("Child DIE %s and its abstract origin %s have "
13005 "different parents"),
13006 sect_offset_str (child_die->sect_off),
13007 sect_offset_str (child_origin_die->sect_off));
13008 else
13009 offsets.push_back (child_origin_die->sect_off);
13010 }
13011 }
13012 std::sort (offsets.begin (), offsets.end ());
13013 sect_offset *offsets_end = offsets.data () + offsets.size ();
13014 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13015 if (offsetp[-1] == *offsetp)
13016 complaint (_("Multiple children of DIE %s refer "
13017 "to DIE %s as their abstract origin"),
13018 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13019
13020 offsetp = offsets.data ();
13021 origin_child_die = origin_die->child;
13022 while (origin_child_die && origin_child_die->tag)
13023 {
13024 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13025 while (offsetp < offsets_end
13026 && *offsetp < origin_child_die->sect_off)
13027 offsetp++;
13028 if (offsetp >= offsets_end
13029 || *offsetp > origin_child_die->sect_off)
13030 {
13031 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13032 Check whether we're already processing ORIGIN_CHILD_DIE.
13033 This can happen with mutually referenced abstract_origins.
13034 PR 16581. */
13035 if (!origin_child_die->in_process)
13036 process_die (origin_child_die, origin_cu);
13037 }
13038 origin_child_die = origin_child_die->sibling;
13039 }
13040 origin_cu->list_in_scope = origin_previous_list_in_scope;
13041
13042 if (cu != origin_cu)
13043 compute_delayed_physnames (origin_cu);
13044 }
13045
13046 static void
13047 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13048 {
13049 struct objfile *objfile = cu->per_objfile->objfile;
13050 struct gdbarch *gdbarch = objfile->arch ();
13051 struct context_stack *newobj;
13052 CORE_ADDR lowpc;
13053 CORE_ADDR highpc;
13054 struct die_info *child_die;
13055 struct attribute *attr, *call_line, *call_file;
13056 const char *name;
13057 CORE_ADDR baseaddr;
13058 struct block *block;
13059 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13060 std::vector<struct symbol *> template_args;
13061 struct template_symbol *templ_func = NULL;
13062
13063 if (inlined_func)
13064 {
13065 /* If we do not have call site information, we can't show the
13066 caller of this inlined function. That's too confusing, so
13067 only use the scope for local variables. */
13068 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13069 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13070 if (call_line == NULL || call_file == NULL)
13071 {
13072 read_lexical_block_scope (die, cu);
13073 return;
13074 }
13075 }
13076
13077 baseaddr = objfile->text_section_offset ();
13078
13079 name = dwarf2_name (die, cu);
13080
13081 /* Ignore functions with missing or empty names. These are actually
13082 illegal according to the DWARF standard. */
13083 if (name == NULL)
13084 {
13085 complaint (_("missing name for subprogram DIE at %s"),
13086 sect_offset_str (die->sect_off));
13087 return;
13088 }
13089
13090 /* Ignore functions with missing or invalid low and high pc attributes. */
13091 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13092 <= PC_BOUNDS_INVALID)
13093 {
13094 attr = dwarf2_attr (die, DW_AT_external, cu);
13095 if (!attr || !DW_UNSND (attr))
13096 complaint (_("cannot get low and high bounds "
13097 "for subprogram DIE at %s"),
13098 sect_offset_str (die->sect_off));
13099 return;
13100 }
13101
13102 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13103 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13104
13105 /* If we have any template arguments, then we must allocate a
13106 different sort of symbol. */
13107 for (child_die = die->child; child_die; child_die = child_die->sibling)
13108 {
13109 if (child_die->tag == DW_TAG_template_type_param
13110 || child_die->tag == DW_TAG_template_value_param)
13111 {
13112 templ_func = new (&objfile->objfile_obstack) template_symbol;
13113 templ_func->subclass = SYMBOL_TEMPLATE;
13114 break;
13115 }
13116 }
13117
13118 newobj = cu->get_builder ()->push_context (0, lowpc);
13119 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13120 (struct symbol *) templ_func);
13121
13122 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13123 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13124 cu->language);
13125
13126 /* If there is a location expression for DW_AT_frame_base, record
13127 it. */
13128 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13129 if (attr != nullptr)
13130 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13131
13132 /* If there is a location for the static link, record it. */
13133 newobj->static_link = NULL;
13134 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13135 if (attr != nullptr)
13136 {
13137 newobj->static_link
13138 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13139 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13140 cu->addr_type ());
13141 }
13142
13143 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13144
13145 if (die->child != NULL)
13146 {
13147 child_die = die->child;
13148 while (child_die && child_die->tag)
13149 {
13150 if (child_die->tag == DW_TAG_template_type_param
13151 || child_die->tag == DW_TAG_template_value_param)
13152 {
13153 struct symbol *arg = new_symbol (child_die, NULL, cu);
13154
13155 if (arg != NULL)
13156 template_args.push_back (arg);
13157 }
13158 else
13159 process_die (child_die, cu);
13160 child_die = child_die->sibling;
13161 }
13162 }
13163
13164 inherit_abstract_dies (die, cu);
13165
13166 /* If we have a DW_AT_specification, we might need to import using
13167 directives from the context of the specification DIE. See the
13168 comment in determine_prefix. */
13169 if (cu->language == language_cplus
13170 && dwarf2_attr (die, DW_AT_specification, cu))
13171 {
13172 struct dwarf2_cu *spec_cu = cu;
13173 struct die_info *spec_die = die_specification (die, &spec_cu);
13174
13175 while (spec_die)
13176 {
13177 child_die = spec_die->child;
13178 while (child_die && child_die->tag)
13179 {
13180 if (child_die->tag == DW_TAG_imported_module)
13181 process_die (child_die, spec_cu);
13182 child_die = child_die->sibling;
13183 }
13184
13185 /* In some cases, GCC generates specification DIEs that
13186 themselves contain DW_AT_specification attributes. */
13187 spec_die = die_specification (spec_die, &spec_cu);
13188 }
13189 }
13190
13191 struct context_stack cstk = cu->get_builder ()->pop_context ();
13192 /* Make a block for the local symbols within. */
13193 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13194 cstk.static_link, lowpc, highpc);
13195
13196 /* For C++, set the block's scope. */
13197 if ((cu->language == language_cplus
13198 || cu->language == language_fortran
13199 || cu->language == language_d
13200 || cu->language == language_rust)
13201 && cu->processing_has_namespace_info)
13202 block_set_scope (block, determine_prefix (die, cu),
13203 &objfile->objfile_obstack);
13204
13205 /* If we have address ranges, record them. */
13206 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13207
13208 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13209
13210 /* Attach template arguments to function. */
13211 if (!template_args.empty ())
13212 {
13213 gdb_assert (templ_func != NULL);
13214
13215 templ_func->n_template_arguments = template_args.size ();
13216 templ_func->template_arguments
13217 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13218 templ_func->n_template_arguments);
13219 memcpy (templ_func->template_arguments,
13220 template_args.data (),
13221 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13222
13223 /* Make sure that the symtab is set on the new symbols. Even
13224 though they don't appear in this symtab directly, other parts
13225 of gdb assume that symbols do, and this is reasonably
13226 true. */
13227 for (symbol *sym : template_args)
13228 symbol_set_symtab (sym, symbol_symtab (templ_func));
13229 }
13230
13231 /* In C++, we can have functions nested inside functions (e.g., when
13232 a function declares a class that has methods). This means that
13233 when we finish processing a function scope, we may need to go
13234 back to building a containing block's symbol lists. */
13235 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13236 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13237
13238 /* If we've finished processing a top-level function, subsequent
13239 symbols go in the file symbol list. */
13240 if (cu->get_builder ()->outermost_context_p ())
13241 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13242 }
13243
13244 /* Process all the DIES contained within a lexical block scope. Start
13245 a new scope, process the dies, and then close the scope. */
13246
13247 static void
13248 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13249 {
13250 struct objfile *objfile = cu->per_objfile->objfile;
13251 struct gdbarch *gdbarch = objfile->arch ();
13252 CORE_ADDR lowpc, highpc;
13253 struct die_info *child_die;
13254 CORE_ADDR baseaddr;
13255
13256 baseaddr = objfile->text_section_offset ();
13257
13258 /* Ignore blocks with missing or invalid low and high pc attributes. */
13259 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13260 as multiple lexical blocks? Handling children in a sane way would
13261 be nasty. Might be easier to properly extend generic blocks to
13262 describe ranges. */
13263 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13264 {
13265 case PC_BOUNDS_NOT_PRESENT:
13266 /* DW_TAG_lexical_block has no attributes, process its children as if
13267 there was no wrapping by that DW_TAG_lexical_block.
13268 GCC does no longer produces such DWARF since GCC r224161. */
13269 for (child_die = die->child;
13270 child_die != NULL && child_die->tag;
13271 child_die = child_die->sibling)
13272 {
13273 /* We might already be processing this DIE. This can happen
13274 in an unusual circumstance -- where a subroutine A
13275 appears lexically in another subroutine B, but A actually
13276 inlines B. The recursion is broken here, rather than in
13277 inherit_abstract_dies, because it seems better to simply
13278 drop concrete children here. */
13279 if (!child_die->in_process)
13280 process_die (child_die, cu);
13281 }
13282 return;
13283 case PC_BOUNDS_INVALID:
13284 return;
13285 }
13286 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13287 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13288
13289 cu->get_builder ()->push_context (0, lowpc);
13290 if (die->child != NULL)
13291 {
13292 child_die = die->child;
13293 while (child_die && child_die->tag)
13294 {
13295 process_die (child_die, cu);
13296 child_die = child_die->sibling;
13297 }
13298 }
13299 inherit_abstract_dies (die, cu);
13300 struct context_stack cstk = cu->get_builder ()->pop_context ();
13301
13302 if (*cu->get_builder ()->get_local_symbols () != NULL
13303 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13304 {
13305 struct block *block
13306 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13307 cstk.start_addr, highpc);
13308
13309 /* Note that recording ranges after traversing children, as we
13310 do here, means that recording a parent's ranges entails
13311 walking across all its children's ranges as they appear in
13312 the address map, which is quadratic behavior.
13313
13314 It would be nicer to record the parent's ranges before
13315 traversing its children, simply overriding whatever you find
13316 there. But since we don't even decide whether to create a
13317 block until after we've traversed its children, that's hard
13318 to do. */
13319 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13320 }
13321 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13322 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13323 }
13324
13325 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13326
13327 static void
13328 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13329 {
13330 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13331 struct objfile *objfile = per_objfile->objfile;
13332 struct gdbarch *gdbarch = objfile->arch ();
13333 CORE_ADDR pc, baseaddr;
13334 struct attribute *attr;
13335 struct call_site *call_site, call_site_local;
13336 void **slot;
13337 int nparams;
13338 struct die_info *child_die;
13339
13340 baseaddr = objfile->text_section_offset ();
13341
13342 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13343 if (attr == NULL)
13344 {
13345 /* This was a pre-DWARF-5 GNU extension alias
13346 for DW_AT_call_return_pc. */
13347 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13348 }
13349 if (!attr)
13350 {
13351 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13352 "DIE %s [in module %s]"),
13353 sect_offset_str (die->sect_off), objfile_name (objfile));
13354 return;
13355 }
13356 pc = attr->value_as_address () + baseaddr;
13357 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13358
13359 if (cu->call_site_htab == NULL)
13360 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13361 NULL, &objfile->objfile_obstack,
13362 hashtab_obstack_allocate, NULL);
13363 call_site_local.pc = pc;
13364 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13365 if (*slot != NULL)
13366 {
13367 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13368 "DIE %s [in module %s]"),
13369 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13370 objfile_name (objfile));
13371 return;
13372 }
13373
13374 /* Count parameters at the caller. */
13375
13376 nparams = 0;
13377 for (child_die = die->child; child_die && child_die->tag;
13378 child_die = child_die->sibling)
13379 {
13380 if (child_die->tag != DW_TAG_call_site_parameter
13381 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13382 {
13383 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13384 "DW_TAG_call_site child DIE %s [in module %s]"),
13385 child_die->tag, sect_offset_str (child_die->sect_off),
13386 objfile_name (objfile));
13387 continue;
13388 }
13389
13390 nparams++;
13391 }
13392
13393 call_site
13394 = ((struct call_site *)
13395 obstack_alloc (&objfile->objfile_obstack,
13396 sizeof (*call_site)
13397 + (sizeof (*call_site->parameter) * (nparams - 1))));
13398 *slot = call_site;
13399 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13400 call_site->pc = pc;
13401
13402 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13403 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13404 {
13405 struct die_info *func_die;
13406
13407 /* Skip also over DW_TAG_inlined_subroutine. */
13408 for (func_die = die->parent;
13409 func_die && func_die->tag != DW_TAG_subprogram
13410 && func_die->tag != DW_TAG_subroutine_type;
13411 func_die = func_die->parent);
13412
13413 /* DW_AT_call_all_calls is a superset
13414 of DW_AT_call_all_tail_calls. */
13415 if (func_die
13416 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13417 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13418 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13419 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13420 {
13421 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13422 not complete. But keep CALL_SITE for look ups via call_site_htab,
13423 both the initial caller containing the real return address PC and
13424 the final callee containing the current PC of a chain of tail
13425 calls do not need to have the tail call list complete. But any
13426 function candidate for a virtual tail call frame searched via
13427 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13428 determined unambiguously. */
13429 }
13430 else
13431 {
13432 struct type *func_type = NULL;
13433
13434 if (func_die)
13435 func_type = get_die_type (func_die, cu);
13436 if (func_type != NULL)
13437 {
13438 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13439
13440 /* Enlist this call site to the function. */
13441 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13442 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13443 }
13444 else
13445 complaint (_("Cannot find function owning DW_TAG_call_site "
13446 "DIE %s [in module %s]"),
13447 sect_offset_str (die->sect_off), objfile_name (objfile));
13448 }
13449 }
13450
13451 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13452 if (attr == NULL)
13453 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13454 if (attr == NULL)
13455 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13456 if (attr == NULL)
13457 {
13458 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13459 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13460 }
13461 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13462 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
13463 /* Keep NULL DWARF_BLOCK. */;
13464 else if (attr->form_is_block ())
13465 {
13466 struct dwarf2_locexpr_baton *dlbaton;
13467
13468 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13469 dlbaton->data = DW_BLOCK (attr)->data;
13470 dlbaton->size = DW_BLOCK (attr)->size;
13471 dlbaton->per_objfile = per_objfile;
13472 dlbaton->per_cu = cu->per_cu;
13473
13474 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13475 }
13476 else if (attr->form_is_ref ())
13477 {
13478 struct dwarf2_cu *target_cu = cu;
13479 struct die_info *target_die;
13480
13481 target_die = follow_die_ref (die, attr, &target_cu);
13482 gdb_assert (target_cu->per_objfile->objfile == objfile);
13483 if (die_is_declaration (target_die, target_cu))
13484 {
13485 const char *target_physname;
13486
13487 /* Prefer the mangled name; otherwise compute the demangled one. */
13488 target_physname = dw2_linkage_name (target_die, target_cu);
13489 if (target_physname == NULL)
13490 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13491 if (target_physname == NULL)
13492 complaint (_("DW_AT_call_target target DIE has invalid "
13493 "physname, for referencing DIE %s [in module %s]"),
13494 sect_offset_str (die->sect_off), objfile_name (objfile));
13495 else
13496 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13497 }
13498 else
13499 {
13500 CORE_ADDR lowpc;
13501
13502 /* DW_AT_entry_pc should be preferred. */
13503 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13504 <= PC_BOUNDS_INVALID)
13505 complaint (_("DW_AT_call_target target DIE has invalid "
13506 "low pc, for referencing DIE %s [in module %s]"),
13507 sect_offset_str (die->sect_off), objfile_name (objfile));
13508 else
13509 {
13510 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13511 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13512 }
13513 }
13514 }
13515 else
13516 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13517 "block nor reference, for DIE %s [in module %s]"),
13518 sect_offset_str (die->sect_off), objfile_name (objfile));
13519
13520 call_site->per_cu = cu->per_cu;
13521 call_site->per_objfile = per_objfile;
13522
13523 for (child_die = die->child;
13524 child_die && child_die->tag;
13525 child_die = child_die->sibling)
13526 {
13527 struct call_site_parameter *parameter;
13528 struct attribute *loc, *origin;
13529
13530 if (child_die->tag != DW_TAG_call_site_parameter
13531 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13532 {
13533 /* Already printed the complaint above. */
13534 continue;
13535 }
13536
13537 gdb_assert (call_site->parameter_count < nparams);
13538 parameter = &call_site->parameter[call_site->parameter_count];
13539
13540 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13541 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13542 register is contained in DW_AT_call_value. */
13543
13544 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13545 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13546 if (origin == NULL)
13547 {
13548 /* This was a pre-DWARF-5 GNU extension alias
13549 for DW_AT_call_parameter. */
13550 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13551 }
13552 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13553 {
13554 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13555
13556 sect_offset sect_off = origin->get_ref_die_offset ();
13557 if (!cu->header.offset_in_cu_p (sect_off))
13558 {
13559 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13560 binding can be done only inside one CU. Such referenced DIE
13561 therefore cannot be even moved to DW_TAG_partial_unit. */
13562 complaint (_("DW_AT_call_parameter offset is not in CU for "
13563 "DW_TAG_call_site child DIE %s [in module %s]"),
13564 sect_offset_str (child_die->sect_off),
13565 objfile_name (objfile));
13566 continue;
13567 }
13568 parameter->u.param_cu_off
13569 = (cu_offset) (sect_off - cu->header.sect_off);
13570 }
13571 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13572 {
13573 complaint (_("No DW_FORM_block* DW_AT_location for "
13574 "DW_TAG_call_site child DIE %s [in module %s]"),
13575 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13576 continue;
13577 }
13578 else
13579 {
13580 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13581 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13582 if (parameter->u.dwarf_reg != -1)
13583 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13584 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13585 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13586 &parameter->u.fb_offset))
13587 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13588 else
13589 {
13590 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13591 "for DW_FORM_block* DW_AT_location is supported for "
13592 "DW_TAG_call_site child DIE %s "
13593 "[in module %s]"),
13594 sect_offset_str (child_die->sect_off),
13595 objfile_name (objfile));
13596 continue;
13597 }
13598 }
13599
13600 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13601 if (attr == NULL)
13602 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13603 if (attr == NULL || !attr->form_is_block ())
13604 {
13605 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13606 "DW_TAG_call_site child DIE %s [in module %s]"),
13607 sect_offset_str (child_die->sect_off),
13608 objfile_name (objfile));
13609 continue;
13610 }
13611 parameter->value = DW_BLOCK (attr)->data;
13612 parameter->value_size = DW_BLOCK (attr)->size;
13613
13614 /* Parameters are not pre-cleared by memset above. */
13615 parameter->data_value = NULL;
13616 parameter->data_value_size = 0;
13617 call_site->parameter_count++;
13618
13619 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13620 if (attr == NULL)
13621 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13622 if (attr != nullptr)
13623 {
13624 if (!attr->form_is_block ())
13625 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13626 "DW_TAG_call_site child DIE %s [in module %s]"),
13627 sect_offset_str (child_die->sect_off),
13628 objfile_name (objfile));
13629 else
13630 {
13631 parameter->data_value = DW_BLOCK (attr)->data;
13632 parameter->data_value_size = DW_BLOCK (attr)->size;
13633 }
13634 }
13635 }
13636 }
13637
13638 /* Helper function for read_variable. If DIE represents a virtual
13639 table, then return the type of the concrete object that is
13640 associated with the virtual table. Otherwise, return NULL. */
13641
13642 static struct type *
13643 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13644 {
13645 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13646 if (attr == NULL)
13647 return NULL;
13648
13649 /* Find the type DIE. */
13650 struct die_info *type_die = NULL;
13651 struct dwarf2_cu *type_cu = cu;
13652
13653 if (attr->form_is_ref ())
13654 type_die = follow_die_ref (die, attr, &type_cu);
13655 if (type_die == NULL)
13656 return NULL;
13657
13658 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13659 return NULL;
13660 return die_containing_type (type_die, type_cu);
13661 }
13662
13663 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13664
13665 static void
13666 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13667 {
13668 struct rust_vtable_symbol *storage = NULL;
13669
13670 if (cu->language == language_rust)
13671 {
13672 struct type *containing_type = rust_containing_type (die, cu);
13673
13674 if (containing_type != NULL)
13675 {
13676 struct objfile *objfile = cu->per_objfile->objfile;
13677
13678 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13679 storage->concrete_type = containing_type;
13680 storage->subclass = SYMBOL_RUST_VTABLE;
13681 }
13682 }
13683
13684 struct symbol *res = new_symbol (die, NULL, cu, storage);
13685 struct attribute *abstract_origin
13686 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13687 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13688 if (res == NULL && loc && abstract_origin)
13689 {
13690 /* We have a variable without a name, but with a location and an abstract
13691 origin. This may be a concrete instance of an abstract variable
13692 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13693 later. */
13694 struct dwarf2_cu *origin_cu = cu;
13695 struct die_info *origin_die
13696 = follow_die_ref (die, abstract_origin, &origin_cu);
13697 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13698 per_objfile->per_bfd->abstract_to_concrete
13699 [origin_die->sect_off].push_back (die->sect_off);
13700 }
13701 }
13702
13703 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13704 reading .debug_rnglists.
13705 Callback's type should be:
13706 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13707 Return true if the attributes are present and valid, otherwise,
13708 return false. */
13709
13710 template <typename Callback>
13711 static bool
13712 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13713 Callback &&callback)
13714 {
13715 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
13716 struct objfile *objfile = dwarf2_per_objfile->objfile;
13717 bfd *obfd = objfile->obfd;
13718 /* Base address selection entry. */
13719 gdb::optional<CORE_ADDR> base;
13720 const gdb_byte *buffer;
13721 CORE_ADDR baseaddr;
13722 bool overflow = false;
13723
13724 base = cu->base_address;
13725
13726 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13727 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
13728 {
13729 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13730 offset);
13731 return false;
13732 }
13733 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
13734
13735 baseaddr = objfile->text_section_offset ();
13736
13737 while (1)
13738 {
13739 /* Initialize it due to a false compiler warning. */
13740 CORE_ADDR range_beginning = 0, range_end = 0;
13741 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13742 + dwarf2_per_objfile->per_bfd->rnglists.size);
13743 unsigned int bytes_read;
13744
13745 if (buffer == buf_end)
13746 {
13747 overflow = true;
13748 break;
13749 }
13750 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13751 switch (rlet)
13752 {
13753 case DW_RLE_end_of_list:
13754 break;
13755 case DW_RLE_base_address:
13756 if (buffer + cu->header.addr_size > buf_end)
13757 {
13758 overflow = true;
13759 break;
13760 }
13761 base = cu->header.read_address (obfd, buffer, &bytes_read);
13762 buffer += bytes_read;
13763 break;
13764 case DW_RLE_start_length:
13765 if (buffer + cu->header.addr_size > buf_end)
13766 {
13767 overflow = true;
13768 break;
13769 }
13770 range_beginning = cu->header.read_address (obfd, buffer,
13771 &bytes_read);
13772 buffer += bytes_read;
13773 range_end = (range_beginning
13774 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13775 buffer += bytes_read;
13776 if (buffer > buf_end)
13777 {
13778 overflow = true;
13779 break;
13780 }
13781 break;
13782 case DW_RLE_offset_pair:
13783 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13784 buffer += bytes_read;
13785 if (buffer > buf_end)
13786 {
13787 overflow = true;
13788 break;
13789 }
13790 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13791 buffer += bytes_read;
13792 if (buffer > buf_end)
13793 {
13794 overflow = true;
13795 break;
13796 }
13797 break;
13798 case DW_RLE_start_end:
13799 if (buffer + 2 * cu->header.addr_size > buf_end)
13800 {
13801 overflow = true;
13802 break;
13803 }
13804 range_beginning = cu->header.read_address (obfd, buffer,
13805 &bytes_read);
13806 buffer += bytes_read;
13807 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
13808 buffer += bytes_read;
13809 break;
13810 default:
13811 complaint (_("Invalid .debug_rnglists data (no base address)"));
13812 return false;
13813 }
13814 if (rlet == DW_RLE_end_of_list || overflow)
13815 break;
13816 if (rlet == DW_RLE_base_address)
13817 continue;
13818
13819 if (!base.has_value ())
13820 {
13821 /* We have no valid base address for the ranges
13822 data. */
13823 complaint (_("Invalid .debug_rnglists data (no base address)"));
13824 return false;
13825 }
13826
13827 if (range_beginning > range_end)
13828 {
13829 /* Inverted range entries are invalid. */
13830 complaint (_("Invalid .debug_rnglists data (inverted range)"));
13831 return false;
13832 }
13833
13834 /* Empty range entries have no effect. */
13835 if (range_beginning == range_end)
13836 continue;
13837
13838 range_beginning += *base;
13839 range_end += *base;
13840
13841 /* A not-uncommon case of bad debug info.
13842 Don't pollute the addrmap with bad data. */
13843 if (range_beginning + baseaddr == 0
13844 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
13845 {
13846 complaint (_(".debug_rnglists entry has start address of zero"
13847 " [in module %s]"), objfile_name (objfile));
13848 continue;
13849 }
13850
13851 callback (range_beginning, range_end);
13852 }
13853
13854 if (overflow)
13855 {
13856 complaint (_("Offset %d is not terminated "
13857 "for DW_AT_ranges attribute"),
13858 offset);
13859 return false;
13860 }
13861
13862 return true;
13863 }
13864
13865 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13866 Callback's type should be:
13867 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13868 Return 1 if the attributes are present and valid, otherwise, return 0. */
13869
13870 template <typename Callback>
13871 static int
13872 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
13873 Callback &&callback)
13874 {
13875 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13876 struct objfile *objfile = per_objfile->objfile;
13877 struct comp_unit_head *cu_header = &cu->header;
13878 bfd *obfd = objfile->obfd;
13879 unsigned int addr_size = cu_header->addr_size;
13880 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13881 /* Base address selection entry. */
13882 gdb::optional<CORE_ADDR> base;
13883 unsigned int dummy;
13884 const gdb_byte *buffer;
13885 CORE_ADDR baseaddr;
13886
13887 if (cu_header->version >= 5)
13888 return dwarf2_rnglists_process (offset, cu, callback);
13889
13890 base = cu->base_address;
13891
13892 per_objfile->per_bfd->ranges.read (objfile);
13893 if (offset >= per_objfile->per_bfd->ranges.size)
13894 {
13895 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
13896 offset);
13897 return 0;
13898 }
13899 buffer = per_objfile->per_bfd->ranges.buffer + offset;
13900
13901 baseaddr = objfile->text_section_offset ();
13902
13903 while (1)
13904 {
13905 CORE_ADDR range_beginning, range_end;
13906
13907 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
13908 buffer += addr_size;
13909 range_end = cu->header.read_address (obfd, buffer, &dummy);
13910 buffer += addr_size;
13911 offset += 2 * addr_size;
13912
13913 /* An end of list marker is a pair of zero addresses. */
13914 if (range_beginning == 0 && range_end == 0)
13915 /* Found the end of list entry. */
13916 break;
13917
13918 /* Each base address selection entry is a pair of 2 values.
13919 The first is the largest possible address, the second is
13920 the base address. Check for a base address here. */
13921 if ((range_beginning & mask) == mask)
13922 {
13923 /* If we found the largest possible address, then we already
13924 have the base address in range_end. */
13925 base = range_end;
13926 continue;
13927 }
13928
13929 if (!base.has_value ())
13930 {
13931 /* We have no valid base address for the ranges
13932 data. */
13933 complaint (_("Invalid .debug_ranges data (no base address)"));
13934 return 0;
13935 }
13936
13937 if (range_beginning > range_end)
13938 {
13939 /* Inverted range entries are invalid. */
13940 complaint (_("Invalid .debug_ranges data (inverted range)"));
13941 return 0;
13942 }
13943
13944 /* Empty range entries have no effect. */
13945 if (range_beginning == range_end)
13946 continue;
13947
13948 range_beginning += *base;
13949 range_end += *base;
13950
13951 /* A not-uncommon case of bad debug info.
13952 Don't pollute the addrmap with bad data. */
13953 if (range_beginning + baseaddr == 0
13954 && !per_objfile->per_bfd->has_section_at_zero)
13955 {
13956 complaint (_(".debug_ranges entry has start address of zero"
13957 " [in module %s]"), objfile_name (objfile));
13958 continue;
13959 }
13960
13961 callback (range_beginning, range_end);
13962 }
13963
13964 return 1;
13965 }
13966
13967 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13968 Return 1 if the attributes are present and valid, otherwise, return 0.
13969 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13970
13971 static int
13972 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13973 CORE_ADDR *high_return, struct dwarf2_cu *cu,
13974 dwarf2_psymtab *ranges_pst)
13975 {
13976 struct objfile *objfile = cu->per_objfile->objfile;
13977 struct gdbarch *gdbarch = objfile->arch ();
13978 const CORE_ADDR baseaddr = objfile->text_section_offset ();
13979 int low_set = 0;
13980 CORE_ADDR low = 0;
13981 CORE_ADDR high = 0;
13982 int retval;
13983
13984 retval = dwarf2_ranges_process (offset, cu,
13985 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13986 {
13987 if (ranges_pst != NULL)
13988 {
13989 CORE_ADDR lowpc;
13990 CORE_ADDR highpc;
13991
13992 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13993 range_beginning + baseaddr)
13994 - baseaddr);
13995 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13996 range_end + baseaddr)
13997 - baseaddr);
13998 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13999 lowpc, highpc - 1, ranges_pst);
14000 }
14001
14002 /* FIXME: This is recording everything as a low-high
14003 segment of consecutive addresses. We should have a
14004 data structure for discontiguous block ranges
14005 instead. */
14006 if (! low_set)
14007 {
14008 low = range_beginning;
14009 high = range_end;
14010 low_set = 1;
14011 }
14012 else
14013 {
14014 if (range_beginning < low)
14015 low = range_beginning;
14016 if (range_end > high)
14017 high = range_end;
14018 }
14019 });
14020 if (!retval)
14021 return 0;
14022
14023 if (! low_set)
14024 /* If the first entry is an end-of-list marker, the range
14025 describes an empty scope, i.e. no instructions. */
14026 return 0;
14027
14028 if (low_return)
14029 *low_return = low;
14030 if (high_return)
14031 *high_return = high;
14032 return 1;
14033 }
14034
14035 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14036 definition for the return value. *LOWPC and *HIGHPC are set iff
14037 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14038
14039 static enum pc_bounds_kind
14040 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14041 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14042 dwarf2_psymtab *pst)
14043 {
14044 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
14045 struct attribute *attr;
14046 struct attribute *attr_high;
14047 CORE_ADDR low = 0;
14048 CORE_ADDR high = 0;
14049 enum pc_bounds_kind ret;
14050
14051 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14052 if (attr_high)
14053 {
14054 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14055 if (attr != nullptr)
14056 {
14057 low = attr->value_as_address ();
14058 high = attr_high->value_as_address ();
14059 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14060 high += low;
14061 }
14062 else
14063 /* Found high w/o low attribute. */
14064 return PC_BOUNDS_INVALID;
14065
14066 /* Found consecutive range of addresses. */
14067 ret = PC_BOUNDS_HIGH_LOW;
14068 }
14069 else
14070 {
14071 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14072 if (attr != NULL)
14073 {
14074 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14075 We take advantage of the fact that DW_AT_ranges does not appear
14076 in DW_TAG_compile_unit of DWO files. */
14077 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14078 unsigned int ranges_offset = (DW_UNSND (attr)
14079 + (need_ranges_base
14080 ? cu->ranges_base
14081 : 0));
14082
14083 /* Value of the DW_AT_ranges attribute is the offset in the
14084 .debug_ranges section. */
14085 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
14086 return PC_BOUNDS_INVALID;
14087 /* Found discontinuous range of addresses. */
14088 ret = PC_BOUNDS_RANGES;
14089 }
14090 else
14091 return PC_BOUNDS_NOT_PRESENT;
14092 }
14093
14094 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14095 if (high <= low)
14096 return PC_BOUNDS_INVALID;
14097
14098 /* When using the GNU linker, .gnu.linkonce. sections are used to
14099 eliminate duplicate copies of functions and vtables and such.
14100 The linker will arbitrarily choose one and discard the others.
14101 The AT_*_pc values for such functions refer to local labels in
14102 these sections. If the section from that file was discarded, the
14103 labels are not in the output, so the relocs get a value of 0.
14104 If this is a discarded function, mark the pc bounds as invalid,
14105 so that GDB will ignore it. */
14106 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
14107 return PC_BOUNDS_INVALID;
14108
14109 *lowpc = low;
14110 if (highpc)
14111 *highpc = high;
14112 return ret;
14113 }
14114
14115 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14116 its low and high PC addresses. Do nothing if these addresses could not
14117 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14118 and HIGHPC to the high address if greater than HIGHPC. */
14119
14120 static void
14121 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14122 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14123 struct dwarf2_cu *cu)
14124 {
14125 CORE_ADDR low, high;
14126 struct die_info *child = die->child;
14127
14128 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14129 {
14130 *lowpc = std::min (*lowpc, low);
14131 *highpc = std::max (*highpc, high);
14132 }
14133
14134 /* If the language does not allow nested subprograms (either inside
14135 subprograms or lexical blocks), we're done. */
14136 if (cu->language != language_ada)
14137 return;
14138
14139 /* Check all the children of the given DIE. If it contains nested
14140 subprograms, then check their pc bounds. Likewise, we need to
14141 check lexical blocks as well, as they may also contain subprogram
14142 definitions. */
14143 while (child && child->tag)
14144 {
14145 if (child->tag == DW_TAG_subprogram
14146 || child->tag == DW_TAG_lexical_block)
14147 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14148 child = child->sibling;
14149 }
14150 }
14151
14152 /* Get the low and high pc's represented by the scope DIE, and store
14153 them in *LOWPC and *HIGHPC. If the correct values can't be
14154 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14155
14156 static void
14157 get_scope_pc_bounds (struct die_info *die,
14158 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14159 struct dwarf2_cu *cu)
14160 {
14161 CORE_ADDR best_low = (CORE_ADDR) -1;
14162 CORE_ADDR best_high = (CORE_ADDR) 0;
14163 CORE_ADDR current_low, current_high;
14164
14165 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14166 >= PC_BOUNDS_RANGES)
14167 {
14168 best_low = current_low;
14169 best_high = current_high;
14170 }
14171 else
14172 {
14173 struct die_info *child = die->child;
14174
14175 while (child && child->tag)
14176 {
14177 switch (child->tag) {
14178 case DW_TAG_subprogram:
14179 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14180 break;
14181 case DW_TAG_namespace:
14182 case DW_TAG_module:
14183 /* FIXME: carlton/2004-01-16: Should we do this for
14184 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14185 that current GCC's always emit the DIEs corresponding
14186 to definitions of methods of classes as children of a
14187 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14188 the DIEs giving the declarations, which could be
14189 anywhere). But I don't see any reason why the
14190 standards says that they have to be there. */
14191 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14192
14193 if (current_low != ((CORE_ADDR) -1))
14194 {
14195 best_low = std::min (best_low, current_low);
14196 best_high = std::max (best_high, current_high);
14197 }
14198 break;
14199 default:
14200 /* Ignore. */
14201 break;
14202 }
14203
14204 child = child->sibling;
14205 }
14206 }
14207
14208 *lowpc = best_low;
14209 *highpc = best_high;
14210 }
14211
14212 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14213 in DIE. */
14214
14215 static void
14216 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14217 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14218 {
14219 struct objfile *objfile = cu->per_objfile->objfile;
14220 struct gdbarch *gdbarch = objfile->arch ();
14221 struct attribute *attr;
14222 struct attribute *attr_high;
14223
14224 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14225 if (attr_high)
14226 {
14227 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14228 if (attr != nullptr)
14229 {
14230 CORE_ADDR low = attr->value_as_address ();
14231 CORE_ADDR high = attr_high->value_as_address ();
14232
14233 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14234 high += low;
14235
14236 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14237 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14238 cu->get_builder ()->record_block_range (block, low, high - 1);
14239 }
14240 }
14241
14242 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14243 if (attr != nullptr)
14244 {
14245 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
14246 We take advantage of the fact that DW_AT_ranges does not appear
14247 in DW_TAG_compile_unit of DWO files. */
14248 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14249
14250 /* The value of the DW_AT_ranges attribute is the offset of the
14251 address range list in the .debug_ranges section. */
14252 unsigned long offset = (DW_UNSND (attr)
14253 + (need_ranges_base ? cu->ranges_base : 0));
14254
14255 std::vector<blockrange> blockvec;
14256 dwarf2_ranges_process (offset, cu,
14257 [&] (CORE_ADDR start, CORE_ADDR end)
14258 {
14259 start += baseaddr;
14260 end += baseaddr;
14261 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14262 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14263 cu->get_builder ()->record_block_range (block, start, end - 1);
14264 blockvec.emplace_back (start, end);
14265 });
14266
14267 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14268 }
14269 }
14270
14271 /* Check whether the producer field indicates either of GCC < 4.6, or the
14272 Intel C/C++ compiler, and cache the result in CU. */
14273
14274 static void
14275 check_producer (struct dwarf2_cu *cu)
14276 {
14277 int major, minor;
14278
14279 if (cu->producer == NULL)
14280 {
14281 /* For unknown compilers expect their behavior is DWARF version
14282 compliant.
14283
14284 GCC started to support .debug_types sections by -gdwarf-4 since
14285 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14286 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14287 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14288 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14289 }
14290 else if (producer_is_gcc (cu->producer, &major, &minor))
14291 {
14292 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14293 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14294 }
14295 else if (producer_is_icc (cu->producer, &major, &minor))
14296 {
14297 cu->producer_is_icc = true;
14298 cu->producer_is_icc_lt_14 = major < 14;
14299 }
14300 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14301 cu->producer_is_codewarrior = true;
14302 else
14303 {
14304 /* For other non-GCC compilers, expect their behavior is DWARF version
14305 compliant. */
14306 }
14307
14308 cu->checked_producer = true;
14309 }
14310
14311 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14312 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14313 during 4.6.0 experimental. */
14314
14315 static bool
14316 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14317 {
14318 if (!cu->checked_producer)
14319 check_producer (cu);
14320
14321 return cu->producer_is_gxx_lt_4_6;
14322 }
14323
14324
14325 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14326 with incorrect is_stmt attributes. */
14327
14328 static bool
14329 producer_is_codewarrior (struct dwarf2_cu *cu)
14330 {
14331 if (!cu->checked_producer)
14332 check_producer (cu);
14333
14334 return cu->producer_is_codewarrior;
14335 }
14336
14337 /* Return the default accessibility type if it is not overridden by
14338 DW_AT_accessibility. */
14339
14340 static enum dwarf_access_attribute
14341 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14342 {
14343 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14344 {
14345 /* The default DWARF 2 accessibility for members is public, the default
14346 accessibility for inheritance is private. */
14347
14348 if (die->tag != DW_TAG_inheritance)
14349 return DW_ACCESS_public;
14350 else
14351 return DW_ACCESS_private;
14352 }
14353 else
14354 {
14355 /* DWARF 3+ defines the default accessibility a different way. The same
14356 rules apply now for DW_TAG_inheritance as for the members and it only
14357 depends on the container kind. */
14358
14359 if (die->parent->tag == DW_TAG_class_type)
14360 return DW_ACCESS_private;
14361 else
14362 return DW_ACCESS_public;
14363 }
14364 }
14365
14366 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14367 offset. If the attribute was not found return 0, otherwise return
14368 1. If it was found but could not properly be handled, set *OFFSET
14369 to 0. */
14370
14371 static int
14372 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14373 LONGEST *offset)
14374 {
14375 struct attribute *attr;
14376
14377 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14378 if (attr != NULL)
14379 {
14380 *offset = 0;
14381
14382 /* Note that we do not check for a section offset first here.
14383 This is because DW_AT_data_member_location is new in DWARF 4,
14384 so if we see it, we can assume that a constant form is really
14385 a constant and not a section offset. */
14386 if (attr->form_is_constant ())
14387 *offset = attr->constant_value (0);
14388 else if (attr->form_is_section_offset ())
14389 dwarf2_complex_location_expr_complaint ();
14390 else if (attr->form_is_block ())
14391 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14392 else
14393 dwarf2_complex_location_expr_complaint ();
14394
14395 return 1;
14396 }
14397
14398 return 0;
14399 }
14400
14401 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14402
14403 static void
14404 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14405 struct field *field)
14406 {
14407 struct attribute *attr;
14408
14409 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14410 if (attr != NULL)
14411 {
14412 if (attr->form_is_constant ())
14413 {
14414 LONGEST offset = attr->constant_value (0);
14415 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14416 }
14417 else if (attr->form_is_section_offset ())
14418 dwarf2_complex_location_expr_complaint ();
14419 else if (attr->form_is_block ())
14420 {
14421 bool handled;
14422 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14423 if (handled)
14424 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14425 else
14426 {
14427 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14428 struct objfile *objfile = per_objfile->objfile;
14429 struct dwarf2_locexpr_baton *dlbaton
14430 = XOBNEW (&objfile->objfile_obstack,
14431 struct dwarf2_locexpr_baton);
14432 dlbaton->data = DW_BLOCK (attr)->data;
14433 dlbaton->size = DW_BLOCK (attr)->size;
14434 /* When using this baton, we want to compute the address
14435 of the field, not the value. This is why
14436 is_reference is set to false here. */
14437 dlbaton->is_reference = false;
14438 dlbaton->per_objfile = per_objfile;
14439 dlbaton->per_cu = cu->per_cu;
14440
14441 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14442 }
14443 }
14444 else
14445 dwarf2_complex_location_expr_complaint ();
14446 }
14447 }
14448
14449 /* Add an aggregate field to the field list. */
14450
14451 static void
14452 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14453 struct dwarf2_cu *cu)
14454 {
14455 struct objfile *objfile = cu->per_objfile->objfile;
14456 struct gdbarch *gdbarch = objfile->arch ();
14457 struct nextfield *new_field;
14458 struct attribute *attr;
14459 struct field *fp;
14460 const char *fieldname = "";
14461
14462 if (die->tag == DW_TAG_inheritance)
14463 {
14464 fip->baseclasses.emplace_back ();
14465 new_field = &fip->baseclasses.back ();
14466 }
14467 else
14468 {
14469 fip->fields.emplace_back ();
14470 new_field = &fip->fields.back ();
14471 }
14472
14473 new_field->offset = die->sect_off;
14474
14475 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14476 if (attr != nullptr)
14477 new_field->accessibility = DW_UNSND (attr);
14478 else
14479 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
14480 if (new_field->accessibility != DW_ACCESS_public)
14481 fip->non_public_fields = 1;
14482
14483 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14484 if (attr != nullptr)
14485 new_field->virtuality = DW_UNSND (attr);
14486 else
14487 new_field->virtuality = DW_VIRTUALITY_none;
14488
14489 fp = &new_field->field;
14490
14491 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14492 {
14493 /* Data member other than a C++ static data member. */
14494
14495 /* Get type of field. */
14496 fp->type = die_type (die, cu);
14497
14498 SET_FIELD_BITPOS (*fp, 0);
14499
14500 /* Get bit size of field (zero if none). */
14501 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14502 if (attr != nullptr)
14503 {
14504 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14505 }
14506 else
14507 {
14508 FIELD_BITSIZE (*fp) = 0;
14509 }
14510
14511 /* Get bit offset of field. */
14512 handle_data_member_location (die, cu, fp);
14513 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14514 if (attr != nullptr)
14515 {
14516 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14517 {
14518 /* For big endian bits, the DW_AT_bit_offset gives the
14519 additional bit offset from the MSB of the containing
14520 anonymous object to the MSB of the field. We don't
14521 have to do anything special since we don't need to
14522 know the size of the anonymous object. */
14523 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
14524 }
14525 else
14526 {
14527 /* For little endian bits, compute the bit offset to the
14528 MSB of the anonymous object, subtract off the number of
14529 bits from the MSB of the field to the MSB of the
14530 object, and then subtract off the number of bits of
14531 the field itself. The result is the bit offset of
14532 the LSB of the field. */
14533 int anonymous_size;
14534 int bit_offset = DW_UNSND (attr);
14535
14536 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14537 if (attr != nullptr)
14538 {
14539 /* The size of the anonymous object containing
14540 the bit field is explicit, so use the
14541 indicated size (in bytes). */
14542 anonymous_size = DW_UNSND (attr);
14543 }
14544 else
14545 {
14546 /* The size of the anonymous object containing
14547 the bit field must be inferred from the type
14548 attribute of the data member containing the
14549 bit field. */
14550 anonymous_size = TYPE_LENGTH (fp->type);
14551 }
14552 SET_FIELD_BITPOS (*fp,
14553 (FIELD_BITPOS (*fp)
14554 + anonymous_size * bits_per_byte
14555 - bit_offset - FIELD_BITSIZE (*fp)));
14556 }
14557 }
14558 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14559 if (attr != NULL)
14560 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14561 + attr->constant_value (0)));
14562
14563 /* Get name of field. */
14564 fieldname = dwarf2_name (die, cu);
14565 if (fieldname == NULL)
14566 fieldname = "";
14567
14568 /* The name is already allocated along with this objfile, so we don't
14569 need to duplicate it for the type. */
14570 fp->name = fieldname;
14571
14572 /* Change accessibility for artificial fields (e.g. virtual table
14573 pointer or virtual base class pointer) to private. */
14574 if (dwarf2_attr (die, DW_AT_artificial, cu))
14575 {
14576 FIELD_ARTIFICIAL (*fp) = 1;
14577 new_field->accessibility = DW_ACCESS_private;
14578 fip->non_public_fields = 1;
14579 }
14580 }
14581 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14582 {
14583 /* C++ static member. */
14584
14585 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14586 is a declaration, but all versions of G++ as of this writing
14587 (so through at least 3.2.1) incorrectly generate
14588 DW_TAG_variable tags. */
14589
14590 const char *physname;
14591
14592 /* Get name of field. */
14593 fieldname = dwarf2_name (die, cu);
14594 if (fieldname == NULL)
14595 return;
14596
14597 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14598 if (attr
14599 /* Only create a symbol if this is an external value.
14600 new_symbol checks this and puts the value in the global symbol
14601 table, which we want. If it is not external, new_symbol
14602 will try to put the value in cu->list_in_scope which is wrong. */
14603 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14604 {
14605 /* A static const member, not much different than an enum as far as
14606 we're concerned, except that we can support more types. */
14607 new_symbol (die, NULL, cu);
14608 }
14609
14610 /* Get physical name. */
14611 physname = dwarf2_physname (fieldname, die, cu);
14612
14613 /* The name is already allocated along with this objfile, so we don't
14614 need to duplicate it for the type. */
14615 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14616 FIELD_TYPE (*fp) = die_type (die, cu);
14617 FIELD_NAME (*fp) = fieldname;
14618 }
14619 else if (die->tag == DW_TAG_inheritance)
14620 {
14621 /* C++ base class field. */
14622 handle_data_member_location (die, cu, fp);
14623 FIELD_BITSIZE (*fp) = 0;
14624 FIELD_TYPE (*fp) = die_type (die, cu);
14625 FIELD_NAME (*fp) = fp->type->name ();
14626 }
14627 else
14628 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14629 }
14630
14631 /* Can the type given by DIE define another type? */
14632
14633 static bool
14634 type_can_define_types (const struct die_info *die)
14635 {
14636 switch (die->tag)
14637 {
14638 case DW_TAG_typedef:
14639 case DW_TAG_class_type:
14640 case DW_TAG_structure_type:
14641 case DW_TAG_union_type:
14642 case DW_TAG_enumeration_type:
14643 return true;
14644
14645 default:
14646 return false;
14647 }
14648 }
14649
14650 /* Add a type definition defined in the scope of the FIP's class. */
14651
14652 static void
14653 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14654 struct dwarf2_cu *cu)
14655 {
14656 struct decl_field fp;
14657 memset (&fp, 0, sizeof (fp));
14658
14659 gdb_assert (type_can_define_types (die));
14660
14661 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14662 fp.name = dwarf2_name (die, cu);
14663 fp.type = read_type_die (die, cu);
14664
14665 /* Save accessibility. */
14666 enum dwarf_access_attribute accessibility;
14667 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14668 if (attr != NULL)
14669 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14670 else
14671 accessibility = dwarf2_default_access_attribute (die, cu);
14672 switch (accessibility)
14673 {
14674 case DW_ACCESS_public:
14675 /* The assumed value if neither private nor protected. */
14676 break;
14677 case DW_ACCESS_private:
14678 fp.is_private = 1;
14679 break;
14680 case DW_ACCESS_protected:
14681 fp.is_protected = 1;
14682 break;
14683 default:
14684 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
14685 }
14686
14687 if (die->tag == DW_TAG_typedef)
14688 fip->typedef_field_list.push_back (fp);
14689 else
14690 fip->nested_types_list.push_back (fp);
14691 }
14692
14693 /* A convenience typedef that's used when finding the discriminant
14694 field for a variant part. */
14695 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14696 offset_map_type;
14697
14698 /* Compute the discriminant range for a given variant. OBSTACK is
14699 where the results will be stored. VARIANT is the variant to
14700 process. IS_UNSIGNED indicates whether the discriminant is signed
14701 or unsigned. */
14702
14703 static const gdb::array_view<discriminant_range>
14704 convert_variant_range (struct obstack *obstack, const variant_field &variant,
14705 bool is_unsigned)
14706 {
14707 std::vector<discriminant_range> ranges;
14708
14709 if (variant.default_branch)
14710 return {};
14711
14712 if (variant.discr_list_data == nullptr)
14713 {
14714 discriminant_range r
14715 = {variant.discriminant_value, variant.discriminant_value};
14716 ranges.push_back (r);
14717 }
14718 else
14719 {
14720 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14721 variant.discr_list_data->size);
14722 while (!data.empty ())
14723 {
14724 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14725 {
14726 complaint (_("invalid discriminant marker: %d"), data[0]);
14727 break;
14728 }
14729 bool is_range = data[0] == DW_DSC_range;
14730 data = data.slice (1);
14731
14732 ULONGEST low, high;
14733 unsigned int bytes_read;
14734
14735 if (data.empty ())
14736 {
14737 complaint (_("DW_AT_discr_list missing low value"));
14738 break;
14739 }
14740 if (is_unsigned)
14741 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14742 else
14743 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14744 &bytes_read);
14745 data = data.slice (bytes_read);
14746
14747 if (is_range)
14748 {
14749 if (data.empty ())
14750 {
14751 complaint (_("DW_AT_discr_list missing high value"));
14752 break;
14753 }
14754 if (is_unsigned)
14755 high = read_unsigned_leb128 (nullptr, data.data (),
14756 &bytes_read);
14757 else
14758 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14759 &bytes_read);
14760 data = data.slice (bytes_read);
14761 }
14762 else
14763 high = low;
14764
14765 ranges.push_back ({ low, high });
14766 }
14767 }
14768
14769 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14770 ranges.size ());
14771 std::copy (ranges.begin (), ranges.end (), result);
14772 return gdb::array_view<discriminant_range> (result, ranges.size ());
14773 }
14774
14775 static const gdb::array_view<variant_part> create_variant_parts
14776 (struct obstack *obstack,
14777 const offset_map_type &offset_map,
14778 struct field_info *fi,
14779 const std::vector<variant_part_builder> &variant_parts);
14780
14781 /* Fill in a "struct variant" for a given variant field. RESULT is
14782 the variant to fill in. OBSTACK is where any needed allocations
14783 will be done. OFFSET_MAP holds the mapping from section offsets to
14784 fields for the type. FI describes the fields of the type we're
14785 processing. FIELD is the variant field we're converting. */
14786
14787 static void
14788 create_one_variant (variant &result, struct obstack *obstack,
14789 const offset_map_type &offset_map,
14790 struct field_info *fi, const variant_field &field)
14791 {
14792 result.discriminants = convert_variant_range (obstack, field, false);
14793 result.first_field = field.first_field + fi->baseclasses.size ();
14794 result.last_field = field.last_field + fi->baseclasses.size ();
14795 result.parts = create_variant_parts (obstack, offset_map, fi,
14796 field.variant_parts);
14797 }
14798
14799 /* Fill in a "struct variant_part" for a given variant part. RESULT
14800 is the variant part to fill in. OBSTACK is where any needed
14801 allocations will be done. OFFSET_MAP holds the mapping from
14802 section offsets to fields for the type. FI describes the fields of
14803 the type we're processing. BUILDER is the variant part to be
14804 converted. */
14805
14806 static void
14807 create_one_variant_part (variant_part &result,
14808 struct obstack *obstack,
14809 const offset_map_type &offset_map,
14810 struct field_info *fi,
14811 const variant_part_builder &builder)
14812 {
14813 auto iter = offset_map.find (builder.discriminant_offset);
14814 if (iter == offset_map.end ())
14815 {
14816 result.discriminant_index = -1;
14817 /* Doesn't matter. */
14818 result.is_unsigned = false;
14819 }
14820 else
14821 {
14822 result.discriminant_index = iter->second;
14823 result.is_unsigned
14824 = TYPE_UNSIGNED (FIELD_TYPE
14825 (fi->fields[result.discriminant_index].field));
14826 }
14827
14828 size_t n = builder.variants.size ();
14829 variant *output = new (obstack) variant[n];
14830 for (size_t i = 0; i < n; ++i)
14831 create_one_variant (output[i], obstack, offset_map, fi,
14832 builder.variants[i]);
14833
14834 result.variants = gdb::array_view<variant> (output, n);
14835 }
14836
14837 /* Create a vector of variant parts that can be attached to a type.
14838 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14839 holds the mapping from section offsets to fields for the type. FI
14840 describes the fields of the type we're processing. VARIANT_PARTS
14841 is the vector to convert. */
14842
14843 static const gdb::array_view<variant_part>
14844 create_variant_parts (struct obstack *obstack,
14845 const offset_map_type &offset_map,
14846 struct field_info *fi,
14847 const std::vector<variant_part_builder> &variant_parts)
14848 {
14849 if (variant_parts.empty ())
14850 return {};
14851
14852 size_t n = variant_parts.size ();
14853 variant_part *result = new (obstack) variant_part[n];
14854 for (size_t i = 0; i < n; ++i)
14855 create_one_variant_part (result[i], obstack, offset_map, fi,
14856 variant_parts[i]);
14857
14858 return gdb::array_view<variant_part> (result, n);
14859 }
14860
14861 /* Compute the variant part vector for FIP, attaching it to TYPE when
14862 done. */
14863
14864 static void
14865 add_variant_property (struct field_info *fip, struct type *type,
14866 struct dwarf2_cu *cu)
14867 {
14868 /* Map section offsets of fields to their field index. Note the
14869 field index here does not take the number of baseclasses into
14870 account. */
14871 offset_map_type offset_map;
14872 for (int i = 0; i < fip->fields.size (); ++i)
14873 offset_map[fip->fields[i].offset] = i;
14874
14875 struct objfile *objfile = cu->per_objfile->objfile;
14876 gdb::array_view<variant_part> parts
14877 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14878 fip->variant_parts);
14879
14880 struct dynamic_prop prop;
14881 prop.kind = PROP_VARIANT_PARTS;
14882 prop.data.variant_parts
14883 = ((gdb::array_view<variant_part> *)
14884 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14885
14886 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
14887 }
14888
14889 /* Create the vector of fields, and attach it to the type. */
14890
14891 static void
14892 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
14893 struct dwarf2_cu *cu)
14894 {
14895 int nfields = fip->nfields ();
14896
14897 /* Record the field count, allocate space for the array of fields,
14898 and create blank accessibility bitfields if necessary. */
14899 type->set_num_fields (nfields);
14900 type->set_fields
14901 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
14902
14903 if (fip->non_public_fields && cu->language != language_ada)
14904 {
14905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14906
14907 TYPE_FIELD_PRIVATE_BITS (type) =
14908 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14909 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14910
14911 TYPE_FIELD_PROTECTED_BITS (type) =
14912 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14913 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14914
14915 TYPE_FIELD_IGNORE_BITS (type) =
14916 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14917 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
14918 }
14919
14920 /* If the type has baseclasses, allocate and clear a bit vector for
14921 TYPE_FIELD_VIRTUAL_BITS. */
14922 if (!fip->baseclasses.empty () && cu->language != language_ada)
14923 {
14924 int num_bytes = B_BYTES (fip->baseclasses.size ());
14925 unsigned char *pointer;
14926
14927 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14928 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
14929 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
14930 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14931 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
14932 }
14933
14934 if (!fip->variant_parts.empty ())
14935 add_variant_property (fip, type, cu);
14936
14937 /* Copy the saved-up fields into the field vector. */
14938 for (int i = 0; i < nfields; ++i)
14939 {
14940 struct nextfield &field
14941 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14942 : fip->fields[i - fip->baseclasses.size ()]);
14943
14944 type->field (i) = field.field;
14945 switch (field.accessibility)
14946 {
14947 case DW_ACCESS_private:
14948 if (cu->language != language_ada)
14949 SET_TYPE_FIELD_PRIVATE (type, i);
14950 break;
14951
14952 case DW_ACCESS_protected:
14953 if (cu->language != language_ada)
14954 SET_TYPE_FIELD_PROTECTED (type, i);
14955 break;
14956
14957 case DW_ACCESS_public:
14958 break;
14959
14960 default:
14961 /* Unknown accessibility. Complain and treat it as public. */
14962 {
14963 complaint (_("unsupported accessibility %d"),
14964 field.accessibility);
14965 }
14966 break;
14967 }
14968 if (i < fip->baseclasses.size ())
14969 {
14970 switch (field.virtuality)
14971 {
14972 case DW_VIRTUALITY_virtual:
14973 case DW_VIRTUALITY_pure_virtual:
14974 if (cu->language == language_ada)
14975 error (_("unexpected virtuality in component of Ada type"));
14976 SET_TYPE_FIELD_VIRTUAL (type, i);
14977 break;
14978 }
14979 }
14980 }
14981 }
14982
14983 /* Return true if this member function is a constructor, false
14984 otherwise. */
14985
14986 static int
14987 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14988 {
14989 const char *fieldname;
14990 const char *type_name;
14991 int len;
14992
14993 if (die->parent == NULL)
14994 return 0;
14995
14996 if (die->parent->tag != DW_TAG_structure_type
14997 && die->parent->tag != DW_TAG_union_type
14998 && die->parent->tag != DW_TAG_class_type)
14999 return 0;
15000
15001 fieldname = dwarf2_name (die, cu);
15002 type_name = dwarf2_name (die->parent, cu);
15003 if (fieldname == NULL || type_name == NULL)
15004 return 0;
15005
15006 len = strlen (fieldname);
15007 return (strncmp (fieldname, type_name, len) == 0
15008 && (type_name[len] == '\0' || type_name[len] == '<'));
15009 }
15010
15011 /* Check if the given VALUE is a recognized enum
15012 dwarf_defaulted_attribute constant according to DWARF5 spec,
15013 Table 7.24. */
15014
15015 static bool
15016 is_valid_DW_AT_defaulted (ULONGEST value)
15017 {
15018 switch (value)
15019 {
15020 case DW_DEFAULTED_no:
15021 case DW_DEFAULTED_in_class:
15022 case DW_DEFAULTED_out_of_class:
15023 return true;
15024 }
15025
15026 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
15027 return false;
15028 }
15029
15030 /* Add a member function to the proper fieldlist. */
15031
15032 static void
15033 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15034 struct type *type, struct dwarf2_cu *cu)
15035 {
15036 struct objfile *objfile = cu->per_objfile->objfile;
15037 struct attribute *attr;
15038 int i;
15039 struct fnfieldlist *flp = nullptr;
15040 struct fn_field *fnp;
15041 const char *fieldname;
15042 struct type *this_type;
15043 enum dwarf_access_attribute accessibility;
15044
15045 if (cu->language == language_ada)
15046 error (_("unexpected member function in Ada type"));
15047
15048 /* Get name of member function. */
15049 fieldname = dwarf2_name (die, cu);
15050 if (fieldname == NULL)
15051 return;
15052
15053 /* Look up member function name in fieldlist. */
15054 for (i = 0; i < fip->fnfieldlists.size (); i++)
15055 {
15056 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15057 {
15058 flp = &fip->fnfieldlists[i];
15059 break;
15060 }
15061 }
15062
15063 /* Create a new fnfieldlist if necessary. */
15064 if (flp == nullptr)
15065 {
15066 fip->fnfieldlists.emplace_back ();
15067 flp = &fip->fnfieldlists.back ();
15068 flp->name = fieldname;
15069 i = fip->fnfieldlists.size () - 1;
15070 }
15071
15072 /* Create a new member function field and add it to the vector of
15073 fnfieldlists. */
15074 flp->fnfields.emplace_back ();
15075 fnp = &flp->fnfields.back ();
15076
15077 /* Delay processing of the physname until later. */
15078 if (cu->language == language_cplus)
15079 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15080 die, cu);
15081 else
15082 {
15083 const char *physname = dwarf2_physname (fieldname, die, cu);
15084 fnp->physname = physname ? physname : "";
15085 }
15086
15087 fnp->type = alloc_type (objfile);
15088 this_type = read_type_die (die, cu);
15089 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15090 {
15091 int nparams = this_type->num_fields ();
15092
15093 /* TYPE is the domain of this method, and THIS_TYPE is the type
15094 of the method itself (TYPE_CODE_METHOD). */
15095 smash_to_method_type (fnp->type, type,
15096 TYPE_TARGET_TYPE (this_type),
15097 this_type->fields (),
15098 this_type->num_fields (),
15099 TYPE_VARARGS (this_type));
15100
15101 /* Handle static member functions.
15102 Dwarf2 has no clean way to discern C++ static and non-static
15103 member functions. G++ helps GDB by marking the first
15104 parameter for non-static member functions (which is the this
15105 pointer) as artificial. We obtain this information from
15106 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15107 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15108 fnp->voffset = VOFFSET_STATIC;
15109 }
15110 else
15111 complaint (_("member function type missing for '%s'"),
15112 dwarf2_full_name (fieldname, die, cu));
15113
15114 /* Get fcontext from DW_AT_containing_type if present. */
15115 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15116 fnp->fcontext = die_containing_type (die, cu);
15117
15118 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15119 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15120
15121 /* Get accessibility. */
15122 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15123 if (attr != nullptr)
15124 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15125 else
15126 accessibility = dwarf2_default_access_attribute (die, cu);
15127 switch (accessibility)
15128 {
15129 case DW_ACCESS_private:
15130 fnp->is_private = 1;
15131 break;
15132 case DW_ACCESS_protected:
15133 fnp->is_protected = 1;
15134 break;
15135 }
15136
15137 /* Check for artificial methods. */
15138 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15139 if (attr && DW_UNSND (attr) != 0)
15140 fnp->is_artificial = 1;
15141
15142 /* Check for defaulted methods. */
15143 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15144 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15145 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15146
15147 /* Check for deleted methods. */
15148 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15149 if (attr != nullptr && DW_UNSND (attr) != 0)
15150 fnp->is_deleted = 1;
15151
15152 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15153
15154 /* Get index in virtual function table if it is a virtual member
15155 function. For older versions of GCC, this is an offset in the
15156 appropriate virtual table, as specified by DW_AT_containing_type.
15157 For everyone else, it is an expression to be evaluated relative
15158 to the object address. */
15159
15160 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15161 if (attr != nullptr)
15162 {
15163 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
15164 {
15165 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15166 {
15167 /* Old-style GCC. */
15168 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15169 }
15170 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15171 || (DW_BLOCK (attr)->size > 1
15172 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15173 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15174 {
15175 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15176 if ((fnp->voffset % cu->header.addr_size) != 0)
15177 dwarf2_complex_location_expr_complaint ();
15178 else
15179 fnp->voffset /= cu->header.addr_size;
15180 fnp->voffset += 2;
15181 }
15182 else
15183 dwarf2_complex_location_expr_complaint ();
15184
15185 if (!fnp->fcontext)
15186 {
15187 /* If there is no `this' field and no DW_AT_containing_type,
15188 we cannot actually find a base class context for the
15189 vtable! */
15190 if (this_type->num_fields () == 0
15191 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15192 {
15193 complaint (_("cannot determine context for virtual member "
15194 "function \"%s\" (offset %s)"),
15195 fieldname, sect_offset_str (die->sect_off));
15196 }
15197 else
15198 {
15199 fnp->fcontext
15200 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15201 }
15202 }
15203 }
15204 else if (attr->form_is_section_offset ())
15205 {
15206 dwarf2_complex_location_expr_complaint ();
15207 }
15208 else
15209 {
15210 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15211 fieldname);
15212 }
15213 }
15214 else
15215 {
15216 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15217 if (attr && DW_UNSND (attr))
15218 {
15219 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15220 complaint (_("Member function \"%s\" (offset %s) is virtual "
15221 "but the vtable offset is not specified"),
15222 fieldname, sect_offset_str (die->sect_off));
15223 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15224 TYPE_CPLUS_DYNAMIC (type) = 1;
15225 }
15226 }
15227 }
15228
15229 /* Create the vector of member function fields, and attach it to the type. */
15230
15231 static void
15232 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15233 struct dwarf2_cu *cu)
15234 {
15235 if (cu->language == language_ada)
15236 error (_("unexpected member functions in Ada type"));
15237
15238 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15239 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15240 TYPE_ALLOC (type,
15241 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15242
15243 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15244 {
15245 struct fnfieldlist &nf = fip->fnfieldlists[i];
15246 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15247
15248 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15249 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15250 fn_flp->fn_fields = (struct fn_field *)
15251 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15252
15253 for (int k = 0; k < nf.fnfields.size (); ++k)
15254 fn_flp->fn_fields[k] = nf.fnfields[k];
15255 }
15256
15257 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15258 }
15259
15260 /* Returns non-zero if NAME is the name of a vtable member in CU's
15261 language, zero otherwise. */
15262 static int
15263 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15264 {
15265 static const char vptr[] = "_vptr";
15266
15267 /* Look for the C++ form of the vtable. */
15268 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15269 return 1;
15270
15271 return 0;
15272 }
15273
15274 /* GCC outputs unnamed structures that are really pointers to member
15275 functions, with the ABI-specified layout. If TYPE describes
15276 such a structure, smash it into a member function type.
15277
15278 GCC shouldn't do this; it should just output pointer to member DIEs.
15279 This is GCC PR debug/28767. */
15280
15281 static void
15282 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15283 {
15284 struct type *pfn_type, *self_type, *new_type;
15285
15286 /* Check for a structure with no name and two children. */
15287 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15288 return;
15289
15290 /* Check for __pfn and __delta members. */
15291 if (TYPE_FIELD_NAME (type, 0) == NULL
15292 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15293 || TYPE_FIELD_NAME (type, 1) == NULL
15294 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15295 return;
15296
15297 /* Find the type of the method. */
15298 pfn_type = TYPE_FIELD_TYPE (type, 0);
15299 if (pfn_type == NULL
15300 || pfn_type->code () != TYPE_CODE_PTR
15301 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15302 return;
15303
15304 /* Look for the "this" argument. */
15305 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15306 if (pfn_type->num_fields () == 0
15307 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
15308 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
15309 return;
15310
15311 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
15312 new_type = alloc_type (objfile);
15313 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15314 pfn_type->fields (), pfn_type->num_fields (),
15315 TYPE_VARARGS (pfn_type));
15316 smash_to_methodptr_type (type, new_type);
15317 }
15318
15319 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15320 appropriate error checking and issuing complaints if there is a
15321 problem. */
15322
15323 static ULONGEST
15324 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15325 {
15326 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15327
15328 if (attr == nullptr)
15329 return 0;
15330
15331 if (!attr->form_is_constant ())
15332 {
15333 complaint (_("DW_AT_alignment must have constant form"
15334 " - DIE at %s [in module %s]"),
15335 sect_offset_str (die->sect_off),
15336 objfile_name (cu->per_objfile->objfile));
15337 return 0;
15338 }
15339
15340 ULONGEST align;
15341 if (attr->form == DW_FORM_sdata)
15342 {
15343 LONGEST val = DW_SND (attr);
15344 if (val < 0)
15345 {
15346 complaint (_("DW_AT_alignment value must not be negative"
15347 " - DIE at %s [in module %s]"),
15348 sect_offset_str (die->sect_off),
15349 objfile_name (cu->per_objfile->objfile));
15350 return 0;
15351 }
15352 align = val;
15353 }
15354 else
15355 align = DW_UNSND (attr);
15356
15357 if (align == 0)
15358 {
15359 complaint (_("DW_AT_alignment value must not be zero"
15360 " - DIE at %s [in module %s]"),
15361 sect_offset_str (die->sect_off),
15362 objfile_name (cu->per_objfile->objfile));
15363 return 0;
15364 }
15365 if ((align & (align - 1)) != 0)
15366 {
15367 complaint (_("DW_AT_alignment value must be a power of 2"
15368 " - DIE at %s [in module %s]"),
15369 sect_offset_str (die->sect_off),
15370 objfile_name (cu->per_objfile->objfile));
15371 return 0;
15372 }
15373
15374 return align;
15375 }
15376
15377 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15378 the alignment for TYPE. */
15379
15380 static void
15381 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15382 struct type *type)
15383 {
15384 if (!set_type_align (type, get_alignment (cu, die)))
15385 complaint (_("DW_AT_alignment value too large"
15386 " - DIE at %s [in module %s]"),
15387 sect_offset_str (die->sect_off),
15388 objfile_name (cu->per_objfile->objfile));
15389 }
15390
15391 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15392 constant for a type, according to DWARF5 spec, Table 5.5. */
15393
15394 static bool
15395 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15396 {
15397 switch (value)
15398 {
15399 case DW_CC_normal:
15400 case DW_CC_pass_by_reference:
15401 case DW_CC_pass_by_value:
15402 return true;
15403
15404 default:
15405 complaint (_("unrecognized DW_AT_calling_convention value "
15406 "(%s) for a type"), pulongest (value));
15407 return false;
15408 }
15409 }
15410
15411 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15412 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15413 also according to GNU-specific values (see include/dwarf2.h). */
15414
15415 static bool
15416 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15417 {
15418 switch (value)
15419 {
15420 case DW_CC_normal:
15421 case DW_CC_program:
15422 case DW_CC_nocall:
15423 return true;
15424
15425 case DW_CC_GNU_renesas_sh:
15426 case DW_CC_GNU_borland_fastcall_i386:
15427 case DW_CC_GDB_IBM_OpenCL:
15428 return true;
15429
15430 default:
15431 complaint (_("unrecognized DW_AT_calling_convention value "
15432 "(%s) for a subroutine"), pulongest (value));
15433 return false;
15434 }
15435 }
15436
15437 /* Called when we find the DIE that starts a structure or union scope
15438 (definition) to create a type for the structure or union. Fill in
15439 the type's name and general properties; the members will not be
15440 processed until process_structure_scope. A symbol table entry for
15441 the type will also not be done until process_structure_scope (assuming
15442 the type has a name).
15443
15444 NOTE: we need to call these functions regardless of whether or not the
15445 DIE has a DW_AT_name attribute, since it might be an anonymous
15446 structure or union. This gets the type entered into our set of
15447 user defined types. */
15448
15449 static struct type *
15450 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15451 {
15452 struct objfile *objfile = cu->per_objfile->objfile;
15453 struct type *type;
15454 struct attribute *attr;
15455 const char *name;
15456
15457 /* If the definition of this type lives in .debug_types, read that type.
15458 Don't follow DW_AT_specification though, that will take us back up
15459 the chain and we want to go down. */
15460 attr = die->attr (DW_AT_signature);
15461 if (attr != nullptr)
15462 {
15463 type = get_DW_AT_signature_type (die, attr, cu);
15464
15465 /* The type's CU may not be the same as CU.
15466 Ensure TYPE is recorded with CU in die_type_hash. */
15467 return set_die_type (die, type, cu);
15468 }
15469
15470 type = alloc_type (objfile);
15471 INIT_CPLUS_SPECIFIC (type);
15472
15473 name = dwarf2_name (die, cu);
15474 if (name != NULL)
15475 {
15476 if (cu->language == language_cplus
15477 || cu->language == language_d
15478 || cu->language == language_rust)
15479 {
15480 const char *full_name = dwarf2_full_name (name, die, cu);
15481
15482 /* dwarf2_full_name might have already finished building the DIE's
15483 type. If so, there is no need to continue. */
15484 if (get_die_type (die, cu) != NULL)
15485 return get_die_type (die, cu);
15486
15487 type->set_name (full_name);
15488 }
15489 else
15490 {
15491 /* The name is already allocated along with this objfile, so
15492 we don't need to duplicate it for the type. */
15493 type->set_name (name);
15494 }
15495 }
15496
15497 if (die->tag == DW_TAG_structure_type)
15498 {
15499 type->set_code (TYPE_CODE_STRUCT);
15500 }
15501 else if (die->tag == DW_TAG_union_type)
15502 {
15503 type->set_code (TYPE_CODE_UNION);
15504 }
15505 else
15506 {
15507 type->set_code (TYPE_CODE_STRUCT);
15508 }
15509
15510 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15511 TYPE_DECLARED_CLASS (type) = 1;
15512
15513 /* Store the calling convention in the type if it's available in
15514 the die. Otherwise the calling convention remains set to
15515 the default value DW_CC_normal. */
15516 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15517 if (attr != nullptr
15518 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15519 {
15520 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15521 TYPE_CPLUS_CALLING_CONVENTION (type)
15522 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15523 }
15524
15525 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15526 if (attr != nullptr)
15527 {
15528 if (attr->form_is_constant ())
15529 TYPE_LENGTH (type) = DW_UNSND (attr);
15530 else
15531 {
15532 struct dynamic_prop prop;
15533 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15534 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15535 TYPE_LENGTH (type) = 0;
15536 }
15537 }
15538 else
15539 {
15540 TYPE_LENGTH (type) = 0;
15541 }
15542
15543 maybe_set_alignment (cu, die, type);
15544
15545 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15546 {
15547 /* ICC<14 does not output the required DW_AT_declaration on
15548 incomplete types, but gives them a size of zero. */
15549 TYPE_STUB (type) = 1;
15550 }
15551 else
15552 TYPE_STUB_SUPPORTED (type) = 1;
15553
15554 if (die_is_declaration (die, cu))
15555 TYPE_STUB (type) = 1;
15556 else if (attr == NULL && die->child == NULL
15557 && producer_is_realview (cu->producer))
15558 /* RealView does not output the required DW_AT_declaration
15559 on incomplete types. */
15560 TYPE_STUB (type) = 1;
15561
15562 /* We need to add the type field to the die immediately so we don't
15563 infinitely recurse when dealing with pointers to the structure
15564 type within the structure itself. */
15565 set_die_type (die, type, cu);
15566
15567 /* set_die_type should be already done. */
15568 set_descriptive_type (type, die, cu);
15569
15570 return type;
15571 }
15572
15573 static void handle_struct_member_die
15574 (struct die_info *child_die,
15575 struct type *type,
15576 struct field_info *fi,
15577 std::vector<struct symbol *> *template_args,
15578 struct dwarf2_cu *cu);
15579
15580 /* A helper for handle_struct_member_die that handles
15581 DW_TAG_variant_part. */
15582
15583 static void
15584 handle_variant_part (struct die_info *die, struct type *type,
15585 struct field_info *fi,
15586 std::vector<struct symbol *> *template_args,
15587 struct dwarf2_cu *cu)
15588 {
15589 variant_part_builder *new_part;
15590 if (fi->current_variant_part == nullptr)
15591 {
15592 fi->variant_parts.emplace_back ();
15593 new_part = &fi->variant_parts.back ();
15594 }
15595 else if (!fi->current_variant_part->processing_variant)
15596 {
15597 complaint (_("nested DW_TAG_variant_part seen "
15598 "- DIE at %s [in module %s]"),
15599 sect_offset_str (die->sect_off),
15600 objfile_name (cu->per_objfile->objfile));
15601 return;
15602 }
15603 else
15604 {
15605 variant_field &current = fi->current_variant_part->variants.back ();
15606 current.variant_parts.emplace_back ();
15607 new_part = &current.variant_parts.back ();
15608 }
15609
15610 /* When we recurse, we want callees to add to this new variant
15611 part. */
15612 scoped_restore save_current_variant_part
15613 = make_scoped_restore (&fi->current_variant_part, new_part);
15614
15615 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15616 if (discr == NULL)
15617 {
15618 /* It's a univariant form, an extension we support. */
15619 }
15620 else if (discr->form_is_ref ())
15621 {
15622 struct dwarf2_cu *target_cu = cu;
15623 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15624
15625 new_part->discriminant_offset = target_die->sect_off;
15626 }
15627 else
15628 {
15629 complaint (_("DW_AT_discr does not have DIE reference form"
15630 " - DIE at %s [in module %s]"),
15631 sect_offset_str (die->sect_off),
15632 objfile_name (cu->per_objfile->objfile));
15633 }
15634
15635 for (die_info *child_die = die->child;
15636 child_die != NULL;
15637 child_die = child_die->sibling)
15638 handle_struct_member_die (child_die, type, fi, template_args, cu);
15639 }
15640
15641 /* A helper for handle_struct_member_die that handles
15642 DW_TAG_variant. */
15643
15644 static void
15645 handle_variant (struct die_info *die, struct type *type,
15646 struct field_info *fi,
15647 std::vector<struct symbol *> *template_args,
15648 struct dwarf2_cu *cu)
15649 {
15650 if (fi->current_variant_part == nullptr)
15651 {
15652 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15653 "- DIE at %s [in module %s]"),
15654 sect_offset_str (die->sect_off),
15655 objfile_name (cu->per_objfile->objfile));
15656 return;
15657 }
15658 if (fi->current_variant_part->processing_variant)
15659 {
15660 complaint (_("nested DW_TAG_variant seen "
15661 "- DIE at %s [in module %s]"),
15662 sect_offset_str (die->sect_off),
15663 objfile_name (cu->per_objfile->objfile));
15664 return;
15665 }
15666
15667 scoped_restore save_processing_variant
15668 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15669 true);
15670
15671 fi->current_variant_part->variants.emplace_back ();
15672 variant_field &variant = fi->current_variant_part->variants.back ();
15673 variant.first_field = fi->fields.size ();
15674
15675 /* In a variant we want to get the discriminant and also add a
15676 field for our sole member child. */
15677 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15678 if (discr == nullptr)
15679 {
15680 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15681 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15682 variant.default_branch = true;
15683 else
15684 variant.discr_list_data = DW_BLOCK (discr);
15685 }
15686 else
15687 variant.discriminant_value = DW_UNSND (discr);
15688
15689 for (die_info *variant_child = die->child;
15690 variant_child != NULL;
15691 variant_child = variant_child->sibling)
15692 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15693
15694 variant.last_field = fi->fields.size ();
15695 }
15696
15697 /* A helper for process_structure_scope that handles a single member
15698 DIE. */
15699
15700 static void
15701 handle_struct_member_die (struct die_info *child_die, struct type *type,
15702 struct field_info *fi,
15703 std::vector<struct symbol *> *template_args,
15704 struct dwarf2_cu *cu)
15705 {
15706 if (child_die->tag == DW_TAG_member
15707 || child_die->tag == DW_TAG_variable)
15708 {
15709 /* NOTE: carlton/2002-11-05: A C++ static data member
15710 should be a DW_TAG_member that is a declaration, but
15711 all versions of G++ as of this writing (so through at
15712 least 3.2.1) incorrectly generate DW_TAG_variable
15713 tags for them instead. */
15714 dwarf2_add_field (fi, child_die, cu);
15715 }
15716 else if (child_die->tag == DW_TAG_subprogram)
15717 {
15718 /* Rust doesn't have member functions in the C++ sense.
15719 However, it does emit ordinary functions as children
15720 of a struct DIE. */
15721 if (cu->language == language_rust)
15722 read_func_scope (child_die, cu);
15723 else
15724 {
15725 /* C++ member function. */
15726 dwarf2_add_member_fn (fi, child_die, type, cu);
15727 }
15728 }
15729 else if (child_die->tag == DW_TAG_inheritance)
15730 {
15731 /* C++ base class field. */
15732 dwarf2_add_field (fi, child_die, cu);
15733 }
15734 else if (type_can_define_types (child_die))
15735 dwarf2_add_type_defn (fi, child_die, cu);
15736 else if (child_die->tag == DW_TAG_template_type_param
15737 || child_die->tag == DW_TAG_template_value_param)
15738 {
15739 struct symbol *arg = new_symbol (child_die, NULL, cu);
15740
15741 if (arg != NULL)
15742 template_args->push_back (arg);
15743 }
15744 else if (child_die->tag == DW_TAG_variant_part)
15745 handle_variant_part (child_die, type, fi, template_args, cu);
15746 else if (child_die->tag == DW_TAG_variant)
15747 handle_variant (child_die, type, fi, template_args, cu);
15748 }
15749
15750 /* Finish creating a structure or union type, including filling in
15751 its members and creating a symbol for it. */
15752
15753 static void
15754 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15755 {
15756 struct objfile *objfile = cu->per_objfile->objfile;
15757 struct die_info *child_die;
15758 struct type *type;
15759
15760 type = get_die_type (die, cu);
15761 if (type == NULL)
15762 type = read_structure_type (die, cu);
15763
15764 bool has_template_parameters = false;
15765 if (die->child != NULL && ! die_is_declaration (die, cu))
15766 {
15767 struct field_info fi;
15768 std::vector<struct symbol *> template_args;
15769
15770 child_die = die->child;
15771
15772 while (child_die && child_die->tag)
15773 {
15774 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
15775 child_die = child_die->sibling;
15776 }
15777
15778 /* Attach template arguments to type. */
15779 if (!template_args.empty ())
15780 {
15781 has_template_parameters = true;
15782 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15783 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
15784 TYPE_TEMPLATE_ARGUMENTS (type)
15785 = XOBNEWVEC (&objfile->objfile_obstack,
15786 struct symbol *,
15787 TYPE_N_TEMPLATE_ARGUMENTS (type));
15788 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
15789 template_args.data (),
15790 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15791 * sizeof (struct symbol *)));
15792 }
15793
15794 /* Attach fields and member functions to the type. */
15795 if (fi.nfields () > 0)
15796 dwarf2_attach_fields_to_type (&fi, type, cu);
15797 if (!fi.fnfieldlists.empty ())
15798 {
15799 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
15800
15801 /* Get the type which refers to the base class (possibly this
15802 class itself) which contains the vtable pointer for the current
15803 class from the DW_AT_containing_type attribute. This use of
15804 DW_AT_containing_type is a GNU extension. */
15805
15806 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15807 {
15808 struct type *t = die_containing_type (die, cu);
15809
15810 set_type_vptr_basetype (type, t);
15811 if (type == t)
15812 {
15813 int i;
15814
15815 /* Our own class provides vtbl ptr. */
15816 for (i = t->num_fields () - 1;
15817 i >= TYPE_N_BASECLASSES (t);
15818 --i)
15819 {
15820 const char *fieldname = TYPE_FIELD_NAME (t, i);
15821
15822 if (is_vtable_name (fieldname, cu))
15823 {
15824 set_type_vptr_fieldno (type, i);
15825 break;
15826 }
15827 }
15828
15829 /* Complain if virtual function table field not found. */
15830 if (i < TYPE_N_BASECLASSES (t))
15831 complaint (_("virtual function table pointer "
15832 "not found when defining class '%s'"),
15833 type->name () ? type->name () : "");
15834 }
15835 else
15836 {
15837 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
15838 }
15839 }
15840 else if (cu->producer
15841 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
15842 {
15843 /* The IBM XLC compiler does not provide direct indication
15844 of the containing type, but the vtable pointer is
15845 always named __vfp. */
15846
15847 int i;
15848
15849 for (i = type->num_fields () - 1;
15850 i >= TYPE_N_BASECLASSES (type);
15851 --i)
15852 {
15853 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15854 {
15855 set_type_vptr_fieldno (type, i);
15856 set_type_vptr_basetype (type, type);
15857 break;
15858 }
15859 }
15860 }
15861 }
15862
15863 /* Copy fi.typedef_field_list linked list elements content into the
15864 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
15865 if (!fi.typedef_field_list.empty ())
15866 {
15867 int count = fi.typedef_field_list.size ();
15868
15869 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15870 TYPE_TYPEDEF_FIELD_ARRAY (type)
15871 = ((struct decl_field *)
15872 TYPE_ALLOC (type,
15873 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15874 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
15875
15876 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15877 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
15878 }
15879
15880 /* Copy fi.nested_types_list linked list elements content into the
15881 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
15882 if (!fi.nested_types_list.empty () && cu->language != language_ada)
15883 {
15884 int count = fi.nested_types_list.size ();
15885
15886 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15887 TYPE_NESTED_TYPES_ARRAY (type)
15888 = ((struct decl_field *)
15889 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15890 TYPE_NESTED_TYPES_COUNT (type) = count;
15891
15892 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15893 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
15894 }
15895 }
15896
15897 quirk_gcc_member_function_pointer (type, objfile);
15898 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15899 cu->rust_unions.push_back (type);
15900
15901 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15902 snapshots) has been known to create a die giving a declaration
15903 for a class that has, as a child, a die giving a definition for a
15904 nested class. So we have to process our children even if the
15905 current die is a declaration. Normally, of course, a declaration
15906 won't have any children at all. */
15907
15908 child_die = die->child;
15909
15910 while (child_die != NULL && child_die->tag)
15911 {
15912 if (child_die->tag == DW_TAG_member
15913 || child_die->tag == DW_TAG_variable
15914 || child_die->tag == DW_TAG_inheritance
15915 || child_die->tag == DW_TAG_template_value_param
15916 || child_die->tag == DW_TAG_template_type_param)
15917 {
15918 /* Do nothing. */
15919 }
15920 else
15921 process_die (child_die, cu);
15922
15923 child_die = child_die->sibling;
15924 }
15925
15926 /* Do not consider external references. According to the DWARF standard,
15927 these DIEs are identified by the fact that they have no byte_size
15928 attribute, and a declaration attribute. */
15929 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15930 || !die_is_declaration (die, cu)
15931 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
15932 {
15933 struct symbol *sym = new_symbol (die, type, cu);
15934
15935 if (has_template_parameters)
15936 {
15937 struct symtab *symtab;
15938 if (sym != nullptr)
15939 symtab = symbol_symtab (sym);
15940 else if (cu->line_header != nullptr)
15941 {
15942 /* Any related symtab will do. */
15943 symtab
15944 = cu->line_header->file_names ()[0].symtab;
15945 }
15946 else
15947 {
15948 symtab = nullptr;
15949 complaint (_("could not find suitable "
15950 "symtab for template parameter"
15951 " - DIE at %s [in module %s]"),
15952 sect_offset_str (die->sect_off),
15953 objfile_name (objfile));
15954 }
15955
15956 if (symtab != nullptr)
15957 {
15958 /* Make sure that the symtab is set on the new symbols.
15959 Even though they don't appear in this symtab directly,
15960 other parts of gdb assume that symbols do, and this is
15961 reasonably true. */
15962 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15963 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15964 }
15965 }
15966 }
15967 }
15968
15969 /* Assuming DIE is an enumeration type, and TYPE is its associated
15970 type, update TYPE using some information only available in DIE's
15971 children. In particular, the fields are computed. */
15972
15973 static void
15974 update_enumeration_type_from_children (struct die_info *die,
15975 struct type *type,
15976 struct dwarf2_cu *cu)
15977 {
15978 struct die_info *child_die;
15979 int unsigned_enum = 1;
15980 int flag_enum = 1;
15981
15982 auto_obstack obstack;
15983 std::vector<struct field> fields;
15984
15985 for (child_die = die->child;
15986 child_die != NULL && child_die->tag;
15987 child_die = child_die->sibling)
15988 {
15989 struct attribute *attr;
15990 LONGEST value;
15991 const gdb_byte *bytes;
15992 struct dwarf2_locexpr_baton *baton;
15993 const char *name;
15994
15995 if (child_die->tag != DW_TAG_enumerator)
15996 continue;
15997
15998 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15999 if (attr == NULL)
16000 continue;
16001
16002 name = dwarf2_name (child_die, cu);
16003 if (name == NULL)
16004 name = "<anonymous enumerator>";
16005
16006 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16007 &value, &bytes, &baton);
16008 if (value < 0)
16009 {
16010 unsigned_enum = 0;
16011 flag_enum = 0;
16012 }
16013 else
16014 {
16015 if (count_one_bits_ll (value) >= 2)
16016 flag_enum = 0;
16017 }
16018
16019 fields.emplace_back ();
16020 struct field &field = fields.back ();
16021 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16022 SET_FIELD_ENUMVAL (field, value);
16023 }
16024
16025 if (!fields.empty ())
16026 {
16027 type->set_num_fields (fields.size ());
16028 type->set_fields
16029 ((struct field *)
16030 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16031 memcpy (type->fields (), fields.data (),
16032 sizeof (struct field) * fields.size ());
16033 }
16034
16035 if (unsigned_enum)
16036 TYPE_UNSIGNED (type) = 1;
16037 if (flag_enum)
16038 TYPE_FLAG_ENUM (type) = 1;
16039 }
16040
16041 /* Given a DW_AT_enumeration_type die, set its type. We do not
16042 complete the type's fields yet, or create any symbols. */
16043
16044 static struct type *
16045 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16046 {
16047 struct objfile *objfile = cu->per_objfile->objfile;
16048 struct type *type;
16049 struct attribute *attr;
16050 const char *name;
16051
16052 /* If the definition of this type lives in .debug_types, read that type.
16053 Don't follow DW_AT_specification though, that will take us back up
16054 the chain and we want to go down. */
16055 attr = die->attr (DW_AT_signature);
16056 if (attr != nullptr)
16057 {
16058 type = get_DW_AT_signature_type (die, attr, cu);
16059
16060 /* The type's CU may not be the same as CU.
16061 Ensure TYPE is recorded with CU in die_type_hash. */
16062 return set_die_type (die, type, cu);
16063 }
16064
16065 type = alloc_type (objfile);
16066
16067 type->set_code (TYPE_CODE_ENUM);
16068 name = dwarf2_full_name (NULL, die, cu);
16069 if (name != NULL)
16070 type->set_name (name);
16071
16072 attr = dwarf2_attr (die, DW_AT_type, cu);
16073 if (attr != NULL)
16074 {
16075 struct type *underlying_type = die_type (die, cu);
16076
16077 TYPE_TARGET_TYPE (type) = underlying_type;
16078 }
16079
16080 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16081 if (attr != nullptr)
16082 {
16083 TYPE_LENGTH (type) = DW_UNSND (attr);
16084 }
16085 else
16086 {
16087 TYPE_LENGTH (type) = 0;
16088 }
16089
16090 maybe_set_alignment (cu, die, type);
16091
16092 /* The enumeration DIE can be incomplete. In Ada, any type can be
16093 declared as private in the package spec, and then defined only
16094 inside the package body. Such types are known as Taft Amendment
16095 Types. When another package uses such a type, an incomplete DIE
16096 may be generated by the compiler. */
16097 if (die_is_declaration (die, cu))
16098 TYPE_STUB (type) = 1;
16099
16100 /* If this type has an underlying type that is not a stub, then we
16101 may use its attributes. We always use the "unsigned" attribute
16102 in this situation, because ordinarily we guess whether the type
16103 is unsigned -- but the guess can be wrong and the underlying type
16104 can tell us the reality. However, we defer to a local size
16105 attribute if one exists, because this lets the compiler override
16106 the underlying type if needed. */
16107 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16108 {
16109 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16110 underlying_type = check_typedef (underlying_type);
16111 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
16112 if (TYPE_LENGTH (type) == 0)
16113 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16114 if (TYPE_RAW_ALIGN (type) == 0
16115 && TYPE_RAW_ALIGN (underlying_type) != 0)
16116 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16117 }
16118
16119 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16120
16121 set_die_type (die, type, cu);
16122
16123 /* Finish the creation of this type by using the enum's children.
16124 Note that, as usual, this must come after set_die_type to avoid
16125 infinite recursion when trying to compute the names of the
16126 enumerators. */
16127 update_enumeration_type_from_children (die, type, cu);
16128
16129 return type;
16130 }
16131
16132 /* Given a pointer to a die which begins an enumeration, process all
16133 the dies that define the members of the enumeration, and create the
16134 symbol for the enumeration type.
16135
16136 NOTE: We reverse the order of the element list. */
16137
16138 static void
16139 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16140 {
16141 struct type *this_type;
16142
16143 this_type = get_die_type (die, cu);
16144 if (this_type == NULL)
16145 this_type = read_enumeration_type (die, cu);
16146
16147 if (die->child != NULL)
16148 {
16149 struct die_info *child_die;
16150 const char *name;
16151
16152 child_die = die->child;
16153 while (child_die && child_die->tag)
16154 {
16155 if (child_die->tag != DW_TAG_enumerator)
16156 {
16157 process_die (child_die, cu);
16158 }
16159 else
16160 {
16161 name = dwarf2_name (child_die, cu);
16162 if (name)
16163 new_symbol (child_die, this_type, cu);
16164 }
16165
16166 child_die = child_die->sibling;
16167 }
16168 }
16169
16170 /* If we are reading an enum from a .debug_types unit, and the enum
16171 is a declaration, and the enum is not the signatured type in the
16172 unit, then we do not want to add a symbol for it. Adding a
16173 symbol would in some cases obscure the true definition of the
16174 enum, giving users an incomplete type when the definition is
16175 actually available. Note that we do not want to do this for all
16176 enums which are just declarations, because C++0x allows forward
16177 enum declarations. */
16178 if (cu->per_cu->is_debug_types
16179 && die_is_declaration (die, cu))
16180 {
16181 struct signatured_type *sig_type;
16182
16183 sig_type = (struct signatured_type *) cu->per_cu;
16184 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16185 if (sig_type->type_offset_in_section != die->sect_off)
16186 return;
16187 }
16188
16189 new_symbol (die, this_type, cu);
16190 }
16191
16192 /* Extract all information from a DW_TAG_array_type DIE and put it in
16193 the DIE's type field. For now, this only handles one dimensional
16194 arrays. */
16195
16196 static struct type *
16197 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16198 {
16199 struct objfile *objfile = cu->per_objfile->objfile;
16200 struct die_info *child_die;
16201 struct type *type;
16202 struct type *element_type, *range_type, *index_type;
16203 struct attribute *attr;
16204 const char *name;
16205 struct dynamic_prop *byte_stride_prop = NULL;
16206 unsigned int bit_stride = 0;
16207
16208 element_type = die_type (die, cu);
16209
16210 /* The die_type call above may have already set the type for this DIE. */
16211 type = get_die_type (die, cu);
16212 if (type)
16213 return type;
16214
16215 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16216 if (attr != NULL)
16217 {
16218 int stride_ok;
16219 struct type *prop_type = cu->addr_sized_int_type (false);
16220
16221 byte_stride_prop
16222 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16223 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16224 prop_type);
16225 if (!stride_ok)
16226 {
16227 complaint (_("unable to read array DW_AT_byte_stride "
16228 " - DIE at %s [in module %s]"),
16229 sect_offset_str (die->sect_off),
16230 objfile_name (cu->per_objfile->objfile));
16231 /* Ignore this attribute. We will likely not be able to print
16232 arrays of this type correctly, but there is little we can do
16233 to help if we cannot read the attribute's value. */
16234 byte_stride_prop = NULL;
16235 }
16236 }
16237
16238 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16239 if (attr != NULL)
16240 bit_stride = DW_UNSND (attr);
16241
16242 /* Irix 6.2 native cc creates array types without children for
16243 arrays with unspecified length. */
16244 if (die->child == NULL)
16245 {
16246 index_type = objfile_type (objfile)->builtin_int;
16247 range_type = create_static_range_type (NULL, index_type, 0, -1);
16248 type = create_array_type_with_stride (NULL, element_type, range_type,
16249 byte_stride_prop, bit_stride);
16250 return set_die_type (die, type, cu);
16251 }
16252
16253 std::vector<struct type *> range_types;
16254 child_die = die->child;
16255 while (child_die && child_die->tag)
16256 {
16257 if (child_die->tag == DW_TAG_subrange_type)
16258 {
16259 struct type *child_type = read_type_die (child_die, cu);
16260
16261 if (child_type != NULL)
16262 {
16263 /* The range type was succesfully read. Save it for the
16264 array type creation. */
16265 range_types.push_back (child_type);
16266 }
16267 }
16268 child_die = child_die->sibling;
16269 }
16270
16271 /* Dwarf2 dimensions are output from left to right, create the
16272 necessary array types in backwards order. */
16273
16274 type = element_type;
16275
16276 if (read_array_order (die, cu) == DW_ORD_col_major)
16277 {
16278 int i = 0;
16279
16280 while (i < range_types.size ())
16281 type = create_array_type_with_stride (NULL, type, range_types[i++],
16282 byte_stride_prop, bit_stride);
16283 }
16284 else
16285 {
16286 size_t ndim = range_types.size ();
16287 while (ndim-- > 0)
16288 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16289 byte_stride_prop, bit_stride);
16290 }
16291
16292 /* Understand Dwarf2 support for vector types (like they occur on
16293 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16294 array type. This is not part of the Dwarf2/3 standard yet, but a
16295 custom vendor extension. The main difference between a regular
16296 array and the vector variant is that vectors are passed by value
16297 to functions. */
16298 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16299 if (attr != nullptr)
16300 make_vector_type (type);
16301
16302 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16303 implementation may choose to implement triple vectors using this
16304 attribute. */
16305 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16306 if (attr != nullptr)
16307 {
16308 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16309 TYPE_LENGTH (type) = DW_UNSND (attr);
16310 else
16311 complaint (_("DW_AT_byte_size for array type smaller "
16312 "than the total size of elements"));
16313 }
16314
16315 name = dwarf2_name (die, cu);
16316 if (name)
16317 type->set_name (name);
16318
16319 maybe_set_alignment (cu, die, type);
16320
16321 /* Install the type in the die. */
16322 set_die_type (die, type, cu);
16323
16324 /* set_die_type should be already done. */
16325 set_descriptive_type (type, die, cu);
16326
16327 return type;
16328 }
16329
16330 static enum dwarf_array_dim_ordering
16331 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16332 {
16333 struct attribute *attr;
16334
16335 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16336
16337 if (attr != nullptr)
16338 return (enum dwarf_array_dim_ordering) DW_SND (attr);
16339
16340 /* GNU F77 is a special case, as at 08/2004 array type info is the
16341 opposite order to the dwarf2 specification, but data is still
16342 laid out as per normal fortran.
16343
16344 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16345 version checking. */
16346
16347 if (cu->language == language_fortran
16348 && cu->producer && strstr (cu->producer, "GNU F77"))
16349 {
16350 return DW_ORD_row_major;
16351 }
16352
16353 switch (cu->language_defn->la_array_ordering)
16354 {
16355 case array_column_major:
16356 return DW_ORD_col_major;
16357 case array_row_major:
16358 default:
16359 return DW_ORD_row_major;
16360 };
16361 }
16362
16363 /* Extract all information from a DW_TAG_set_type DIE and put it in
16364 the DIE's type field. */
16365
16366 static struct type *
16367 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16368 {
16369 struct type *domain_type, *set_type;
16370 struct attribute *attr;
16371
16372 domain_type = die_type (die, cu);
16373
16374 /* The die_type call above may have already set the type for this DIE. */
16375 set_type = get_die_type (die, cu);
16376 if (set_type)
16377 return set_type;
16378
16379 set_type = create_set_type (NULL, domain_type);
16380
16381 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16382 if (attr != nullptr)
16383 TYPE_LENGTH (set_type) = DW_UNSND (attr);
16384
16385 maybe_set_alignment (cu, die, set_type);
16386
16387 return set_die_type (die, set_type, cu);
16388 }
16389
16390 /* A helper for read_common_block that creates a locexpr baton.
16391 SYM is the symbol which we are marking as computed.
16392 COMMON_DIE is the DIE for the common block.
16393 COMMON_LOC is the location expression attribute for the common
16394 block itself.
16395 MEMBER_LOC is the location expression attribute for the particular
16396 member of the common block that we are processing.
16397 CU is the CU from which the above come. */
16398
16399 static void
16400 mark_common_block_symbol_computed (struct symbol *sym,
16401 struct die_info *common_die,
16402 struct attribute *common_loc,
16403 struct attribute *member_loc,
16404 struct dwarf2_cu *cu)
16405 {
16406 dwarf2_per_objfile *per_objfile = cu->per_objfile;
16407 struct objfile *objfile = per_objfile->objfile;
16408 struct dwarf2_locexpr_baton *baton;
16409 gdb_byte *ptr;
16410 unsigned int cu_off;
16411 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
16412 LONGEST offset = 0;
16413
16414 gdb_assert (common_loc && member_loc);
16415 gdb_assert (common_loc->form_is_block ());
16416 gdb_assert (member_loc->form_is_block ()
16417 || member_loc->form_is_constant ());
16418
16419 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
16420 baton->per_objfile = per_objfile;
16421 baton->per_cu = cu->per_cu;
16422 gdb_assert (baton->per_cu);
16423
16424 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16425
16426 if (member_loc->form_is_constant ())
16427 {
16428 offset = member_loc->constant_value (0);
16429 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16430 }
16431 else
16432 baton->size += DW_BLOCK (member_loc)->size;
16433
16434 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
16435 baton->data = ptr;
16436
16437 *ptr++ = DW_OP_call4;
16438 cu_off = common_die->sect_off - cu->per_cu->sect_off;
16439 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16440 ptr += 4;
16441
16442 if (member_loc->form_is_constant ())
16443 {
16444 *ptr++ = DW_OP_addr;
16445 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16446 ptr += cu->header.addr_size;
16447 }
16448 else
16449 {
16450 /* We have to copy the data here, because DW_OP_call4 will only
16451 use a DW_AT_location attribute. */
16452 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16453 ptr += DW_BLOCK (member_loc)->size;
16454 }
16455
16456 *ptr++ = DW_OP_plus;
16457 gdb_assert (ptr - baton->data == baton->size);
16458
16459 SYMBOL_LOCATION_BATON (sym) = baton;
16460 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
16461 }
16462
16463 /* Create appropriate locally-scoped variables for all the
16464 DW_TAG_common_block entries. Also create a struct common_block
16465 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16466 is used to separate the common blocks name namespace from regular
16467 variable names. */
16468
16469 static void
16470 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
16471 {
16472 struct attribute *attr;
16473
16474 attr = dwarf2_attr (die, DW_AT_location, cu);
16475 if (attr != nullptr)
16476 {
16477 /* Support the .debug_loc offsets. */
16478 if (attr->form_is_block ())
16479 {
16480 /* Ok. */
16481 }
16482 else if (attr->form_is_section_offset ())
16483 {
16484 dwarf2_complex_location_expr_complaint ();
16485 attr = NULL;
16486 }
16487 else
16488 {
16489 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16490 "common block member");
16491 attr = NULL;
16492 }
16493 }
16494
16495 if (die->child != NULL)
16496 {
16497 struct objfile *objfile = cu->per_objfile->objfile;
16498 struct die_info *child_die;
16499 size_t n_entries = 0, size;
16500 struct common_block *common_block;
16501 struct symbol *sym;
16502
16503 for (child_die = die->child;
16504 child_die && child_die->tag;
16505 child_die = child_die->sibling)
16506 ++n_entries;
16507
16508 size = (sizeof (struct common_block)
16509 + (n_entries - 1) * sizeof (struct symbol *));
16510 common_block
16511 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16512 size);
16513 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16514 common_block->n_entries = 0;
16515
16516 for (child_die = die->child;
16517 child_die && child_die->tag;
16518 child_die = child_die->sibling)
16519 {
16520 /* Create the symbol in the DW_TAG_common_block block in the current
16521 symbol scope. */
16522 sym = new_symbol (child_die, NULL, cu);
16523 if (sym != NULL)
16524 {
16525 struct attribute *member_loc;
16526
16527 common_block->contents[common_block->n_entries++] = sym;
16528
16529 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16530 cu);
16531 if (member_loc)
16532 {
16533 /* GDB has handled this for a long time, but it is
16534 not specified by DWARF. It seems to have been
16535 emitted by gfortran at least as recently as:
16536 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
16537 complaint (_("Variable in common block has "
16538 "DW_AT_data_member_location "
16539 "- DIE at %s [in module %s]"),
16540 sect_offset_str (child_die->sect_off),
16541 objfile_name (objfile));
16542
16543 if (member_loc->form_is_section_offset ())
16544 dwarf2_complex_location_expr_complaint ();
16545 else if (member_loc->form_is_constant ()
16546 || member_loc->form_is_block ())
16547 {
16548 if (attr != nullptr)
16549 mark_common_block_symbol_computed (sym, die, attr,
16550 member_loc, cu);
16551 }
16552 else
16553 dwarf2_complex_location_expr_complaint ();
16554 }
16555 }
16556 }
16557
16558 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16559 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
16560 }
16561 }
16562
16563 /* Create a type for a C++ namespace. */
16564
16565 static struct type *
16566 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
16567 {
16568 struct objfile *objfile = cu->per_objfile->objfile;
16569 const char *previous_prefix, *name;
16570 int is_anonymous;
16571 struct type *type;
16572
16573 /* For extensions, reuse the type of the original namespace. */
16574 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16575 {
16576 struct die_info *ext_die;
16577 struct dwarf2_cu *ext_cu = cu;
16578
16579 ext_die = dwarf2_extension (die, &ext_cu);
16580 type = read_type_die (ext_die, ext_cu);
16581
16582 /* EXT_CU may not be the same as CU.
16583 Ensure TYPE is recorded with CU in die_type_hash. */
16584 return set_die_type (die, type, cu);
16585 }
16586
16587 name = namespace_name (die, &is_anonymous, cu);
16588
16589 /* Now build the name of the current namespace. */
16590
16591 previous_prefix = determine_prefix (die, cu);
16592 if (previous_prefix[0] != '\0')
16593 name = typename_concat (&objfile->objfile_obstack,
16594 previous_prefix, name, 0, cu);
16595
16596 /* Create the type. */
16597 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
16598
16599 return set_die_type (die, type, cu);
16600 }
16601
16602 /* Read a namespace scope. */
16603
16604 static void
16605 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16606 {
16607 struct objfile *objfile = cu->per_objfile->objfile;
16608 int is_anonymous;
16609
16610 /* Add a symbol associated to this if we haven't seen the namespace
16611 before. Also, add a using directive if it's an anonymous
16612 namespace. */
16613
16614 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
16615 {
16616 struct type *type;
16617
16618 type = read_type_die (die, cu);
16619 new_symbol (die, type, cu);
16620
16621 namespace_name (die, &is_anonymous, cu);
16622 if (is_anonymous)
16623 {
16624 const char *previous_prefix = determine_prefix (die, cu);
16625
16626 std::vector<const char *> excludes;
16627 add_using_directive (using_directives (cu),
16628 previous_prefix, type->name (), NULL,
16629 NULL, excludes, 0, &objfile->objfile_obstack);
16630 }
16631 }
16632
16633 if (die->child != NULL)
16634 {
16635 struct die_info *child_die = die->child;
16636
16637 while (child_die && child_die->tag)
16638 {
16639 process_die (child_die, cu);
16640 child_die = child_die->sibling;
16641 }
16642 }
16643 }
16644
16645 /* Read a Fortran module as type. This DIE can be only a declaration used for
16646 imported module. Still we need that type as local Fortran "use ... only"
16647 declaration imports depend on the created type in determine_prefix. */
16648
16649 static struct type *
16650 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16651 {
16652 struct objfile *objfile = cu->per_objfile->objfile;
16653 const char *module_name;
16654 struct type *type;
16655
16656 module_name = dwarf2_name (die, cu);
16657 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
16658
16659 return set_die_type (die, type, cu);
16660 }
16661
16662 /* Read a Fortran module. */
16663
16664 static void
16665 read_module (struct die_info *die, struct dwarf2_cu *cu)
16666 {
16667 struct die_info *child_die = die->child;
16668 struct type *type;
16669
16670 type = read_type_die (die, cu);
16671 new_symbol (die, type, cu);
16672
16673 while (child_die && child_die->tag)
16674 {
16675 process_die (child_die, cu);
16676 child_die = child_die->sibling;
16677 }
16678 }
16679
16680 /* Return the name of the namespace represented by DIE. Set
16681 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16682 namespace. */
16683
16684 static const char *
16685 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
16686 {
16687 struct die_info *current_die;
16688 const char *name = NULL;
16689
16690 /* Loop through the extensions until we find a name. */
16691
16692 for (current_die = die;
16693 current_die != NULL;
16694 current_die = dwarf2_extension (die, &cu))
16695 {
16696 /* We don't use dwarf2_name here so that we can detect the absence
16697 of a name -> anonymous namespace. */
16698 name = dwarf2_string_attr (die, DW_AT_name, cu);
16699
16700 if (name != NULL)
16701 break;
16702 }
16703
16704 /* Is it an anonymous namespace? */
16705
16706 *is_anonymous = (name == NULL);
16707 if (*is_anonymous)
16708 name = CP_ANONYMOUS_NAMESPACE_STR;
16709
16710 return name;
16711 }
16712
16713 /* Extract all information from a DW_TAG_pointer_type DIE and add to
16714 the user defined type vector. */
16715
16716 static struct type *
16717 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
16718 {
16719 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
16720 struct comp_unit_head *cu_header = &cu->header;
16721 struct type *type;
16722 struct attribute *attr_byte_size;
16723 struct attribute *attr_address_class;
16724 int byte_size, addr_class;
16725 struct type *target_type;
16726
16727 target_type = die_type (die, cu);
16728
16729 /* The die_type call above may have already set the type for this DIE. */
16730 type = get_die_type (die, cu);
16731 if (type)
16732 return type;
16733
16734 type = lookup_pointer_type (target_type);
16735
16736 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
16737 if (attr_byte_size)
16738 byte_size = DW_UNSND (attr_byte_size);
16739 else
16740 byte_size = cu_header->addr_size;
16741
16742 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
16743 if (attr_address_class)
16744 addr_class = DW_UNSND (attr_address_class);
16745 else
16746 addr_class = DW_ADDR_none;
16747
16748 ULONGEST alignment = get_alignment (cu, die);
16749
16750 /* If the pointer size, alignment, or address class is different
16751 than the default, create a type variant marked as such and set
16752 the length accordingly. */
16753 if (TYPE_LENGTH (type) != byte_size
16754 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16755 && alignment != TYPE_RAW_ALIGN (type))
16756 || addr_class != DW_ADDR_none)
16757 {
16758 if (gdbarch_address_class_type_flags_p (gdbarch))
16759 {
16760 int type_flags;
16761
16762 type_flags = gdbarch_address_class_type_flags
16763 (gdbarch, byte_size, addr_class);
16764 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16765 == 0);
16766 type = make_type_with_address_space (type, type_flags);
16767 }
16768 else if (TYPE_LENGTH (type) != byte_size)
16769 {
16770 complaint (_("invalid pointer size %d"), byte_size);
16771 }
16772 else if (TYPE_RAW_ALIGN (type) != alignment)
16773 {
16774 complaint (_("Invalid DW_AT_alignment"
16775 " - DIE at %s [in module %s]"),
16776 sect_offset_str (die->sect_off),
16777 objfile_name (cu->per_objfile->objfile));
16778 }
16779 else
16780 {
16781 /* Should we also complain about unhandled address classes? */
16782 }
16783 }
16784
16785 TYPE_LENGTH (type) = byte_size;
16786 set_type_align (type, alignment);
16787 return set_die_type (die, type, cu);
16788 }
16789
16790 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16791 the user defined type vector. */
16792
16793 static struct type *
16794 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
16795 {
16796 struct type *type;
16797 struct type *to_type;
16798 struct type *domain;
16799
16800 to_type = die_type (die, cu);
16801 domain = die_containing_type (die, cu);
16802
16803 /* The calls above may have already set the type for this DIE. */
16804 type = get_die_type (die, cu);
16805 if (type)
16806 return type;
16807
16808 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
16809 type = lookup_methodptr_type (to_type);
16810 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
16811 {
16812 struct type *new_type = alloc_type (cu->per_objfile->objfile);
16813
16814 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16815 to_type->fields (), to_type->num_fields (),
16816 TYPE_VARARGS (to_type));
16817 type = lookup_methodptr_type (new_type);
16818 }
16819 else
16820 type = lookup_memberptr_type (to_type, domain);
16821
16822 return set_die_type (die, type, cu);
16823 }
16824
16825 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
16826 the user defined type vector. */
16827
16828 static struct type *
16829 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16830 enum type_code refcode)
16831 {
16832 struct comp_unit_head *cu_header = &cu->header;
16833 struct type *type, *target_type;
16834 struct attribute *attr;
16835
16836 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16837
16838 target_type = die_type (die, cu);
16839
16840 /* The die_type call above may have already set the type for this DIE. */
16841 type = get_die_type (die, cu);
16842 if (type)
16843 return type;
16844
16845 type = lookup_reference_type (target_type, refcode);
16846 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16847 if (attr != nullptr)
16848 {
16849 TYPE_LENGTH (type) = DW_UNSND (attr);
16850 }
16851 else
16852 {
16853 TYPE_LENGTH (type) = cu_header->addr_size;
16854 }
16855 maybe_set_alignment (cu, die, type);
16856 return set_die_type (die, type, cu);
16857 }
16858
16859 /* Add the given cv-qualifiers to the element type of the array. GCC
16860 outputs DWARF type qualifiers that apply to an array, not the
16861 element type. But GDB relies on the array element type to carry
16862 the cv-qualifiers. This mimics section 6.7.3 of the C99
16863 specification. */
16864
16865 static struct type *
16866 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16867 struct type *base_type, int cnst, int voltl)
16868 {
16869 struct type *el_type, *inner_array;
16870
16871 base_type = copy_type (base_type);
16872 inner_array = base_type;
16873
16874 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
16875 {
16876 TYPE_TARGET_TYPE (inner_array) =
16877 copy_type (TYPE_TARGET_TYPE (inner_array));
16878 inner_array = TYPE_TARGET_TYPE (inner_array);
16879 }
16880
16881 el_type = TYPE_TARGET_TYPE (inner_array);
16882 cnst |= TYPE_CONST (el_type);
16883 voltl |= TYPE_VOLATILE (el_type);
16884 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16885
16886 return set_die_type (die, base_type, cu);
16887 }
16888
16889 static struct type *
16890 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
16891 {
16892 struct type *base_type, *cv_type;
16893
16894 base_type = die_type (die, cu);
16895
16896 /* The die_type call above may have already set the type for this DIE. */
16897 cv_type = get_die_type (die, cu);
16898 if (cv_type)
16899 return cv_type;
16900
16901 /* In case the const qualifier is applied to an array type, the element type
16902 is so qualified, not the array type (section 6.7.3 of C99). */
16903 if (base_type->code () == TYPE_CODE_ARRAY)
16904 return add_array_cv_type (die, cu, base_type, 1, 0);
16905
16906 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16907 return set_die_type (die, cv_type, cu);
16908 }
16909
16910 static struct type *
16911 read_tag_volatile_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 /* In case the volatile qualifier is applied to an array type, the
16923 element type is so qualified, not the array type (section 6.7.3
16924 of C99). */
16925 if (base_type->code () == TYPE_CODE_ARRAY)
16926 return add_array_cv_type (die, cu, base_type, 0, 1);
16927
16928 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16929 return set_die_type (die, cv_type, cu);
16930 }
16931
16932 /* Handle DW_TAG_restrict_type. */
16933
16934 static struct type *
16935 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16936 {
16937 struct type *base_type, *cv_type;
16938
16939 base_type = die_type (die, cu);
16940
16941 /* The die_type call above may have already set the type for this DIE. */
16942 cv_type = get_die_type (die, cu);
16943 if (cv_type)
16944 return cv_type;
16945
16946 cv_type = make_restrict_type (base_type);
16947 return set_die_type (die, cv_type, cu);
16948 }
16949
16950 /* Handle DW_TAG_atomic_type. */
16951
16952 static struct type *
16953 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16954 {
16955 struct type *base_type, *cv_type;
16956
16957 base_type = die_type (die, cu);
16958
16959 /* The die_type call above may have already set the type for this DIE. */
16960 cv_type = get_die_type (die, cu);
16961 if (cv_type)
16962 return cv_type;
16963
16964 cv_type = make_atomic_type (base_type);
16965 return set_die_type (die, cv_type, cu);
16966 }
16967
16968 /* Extract all information from a DW_TAG_string_type DIE and add to
16969 the user defined type vector. It isn't really a user defined type,
16970 but it behaves like one, with other DIE's using an AT_user_def_type
16971 attribute to reference it. */
16972
16973 static struct type *
16974 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
16975 {
16976 struct objfile *objfile = cu->per_objfile->objfile;
16977 struct gdbarch *gdbarch = objfile->arch ();
16978 struct type *type, *range_type, *index_type, *char_type;
16979 struct attribute *attr;
16980 struct dynamic_prop prop;
16981 bool length_is_constant = true;
16982 LONGEST length;
16983
16984 /* There are a couple of places where bit sizes might be made use of
16985 when parsing a DW_TAG_string_type, however, no producer that we know
16986 of make use of these. Handling bit sizes that are a multiple of the
16987 byte size is easy enough, but what about other bit sizes? Lets deal
16988 with that problem when we have to. Warn about these attributes being
16989 unsupported, then parse the type and ignore them like we always
16990 have. */
16991 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16992 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16993 {
16994 static bool warning_printed = false;
16995 if (!warning_printed)
16996 {
16997 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16998 "currently supported on DW_TAG_string_type."));
16999 warning_printed = true;
17000 }
17001 }
17002
17003 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17004 if (attr != nullptr && !attr->form_is_constant ())
17005 {
17006 /* The string length describes the location at which the length of
17007 the string can be found. The size of the length field can be
17008 specified with one of the attributes below. */
17009 struct type *prop_type;
17010 struct attribute *len
17011 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17012 if (len == nullptr)
17013 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17014 if (len != nullptr && len->form_is_constant ())
17015 {
17016 /* Pass 0 as the default as we know this attribute is constant
17017 and the default value will not be returned. */
17018 LONGEST sz = len->constant_value (0);
17019 prop_type = cu->per_objfile->int_type (sz, true);
17020 }
17021 else
17022 {
17023 /* If the size is not specified then we assume it is the size of
17024 an address on this target. */
17025 prop_type = cu->addr_sized_int_type (true);
17026 }
17027
17028 /* Convert the attribute into a dynamic property. */
17029 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17030 length = 1;
17031 else
17032 length_is_constant = false;
17033 }
17034 else if (attr != nullptr)
17035 {
17036 /* This DW_AT_string_length just contains the length with no
17037 indirection. There's no need to create a dynamic property in this
17038 case. Pass 0 for the default value as we know it will not be
17039 returned in this case. */
17040 length = attr->constant_value (0);
17041 }
17042 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17043 {
17044 /* We don't currently support non-constant byte sizes for strings. */
17045 length = attr->constant_value (1);
17046 }
17047 else
17048 {
17049 /* Use 1 as a fallback length if we have nothing else. */
17050 length = 1;
17051 }
17052
17053 index_type = objfile_type (objfile)->builtin_int;
17054 if (length_is_constant)
17055 range_type = create_static_range_type (NULL, index_type, 1, length);
17056 else
17057 {
17058 struct dynamic_prop low_bound;
17059
17060 low_bound.kind = PROP_CONST;
17061 low_bound.data.const_val = 1;
17062 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17063 }
17064 char_type = language_string_char_type (cu->language_defn, gdbarch);
17065 type = create_string_type (NULL, char_type, range_type);
17066
17067 return set_die_type (die, type, cu);
17068 }
17069
17070 /* Assuming that DIE corresponds to a function, returns nonzero
17071 if the function is prototyped. */
17072
17073 static int
17074 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17075 {
17076 struct attribute *attr;
17077
17078 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17079 if (attr && (DW_UNSND (attr) != 0))
17080 return 1;
17081
17082 /* The DWARF standard implies that the DW_AT_prototyped attribute
17083 is only meaningful for C, but the concept also extends to other
17084 languages that allow unprototyped functions (Eg: Objective C).
17085 For all other languages, assume that functions are always
17086 prototyped. */
17087 if (cu->language != language_c
17088 && cu->language != language_objc
17089 && cu->language != language_opencl)
17090 return 1;
17091
17092 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17093 prototyped and unprototyped functions; default to prototyped,
17094 since that is more common in modern code (and RealView warns
17095 about unprototyped functions). */
17096 if (producer_is_realview (cu->producer))
17097 return 1;
17098
17099 return 0;
17100 }
17101
17102 /* Handle DIES due to C code like:
17103
17104 struct foo
17105 {
17106 int (*funcp)(int a, long l);
17107 int b;
17108 };
17109
17110 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17111
17112 static struct type *
17113 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17114 {
17115 struct objfile *objfile = cu->per_objfile->objfile;
17116 struct type *type; /* Type that this function returns. */
17117 struct type *ftype; /* Function that returns above type. */
17118 struct attribute *attr;
17119
17120 type = die_type (die, cu);
17121
17122 /* The die_type call above may have already set the type for this DIE. */
17123 ftype = get_die_type (die, cu);
17124 if (ftype)
17125 return ftype;
17126
17127 ftype = lookup_function_type (type);
17128
17129 if (prototyped_function_p (die, cu))
17130 TYPE_PROTOTYPED (ftype) = 1;
17131
17132 /* Store the calling convention in the type if it's available in
17133 the subroutine die. Otherwise set the calling convention to
17134 the default value DW_CC_normal. */
17135 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17136 if (attr != nullptr
17137 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17138 TYPE_CALLING_CONVENTION (ftype)
17139 = (enum dwarf_calling_convention) (DW_UNSND (attr));
17140 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17141 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17142 else
17143 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17144
17145 /* Record whether the function returns normally to its caller or not
17146 if the DWARF producer set that information. */
17147 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17148 if (attr && (DW_UNSND (attr) != 0))
17149 TYPE_NO_RETURN (ftype) = 1;
17150
17151 /* We need to add the subroutine type to the die immediately so
17152 we don't infinitely recurse when dealing with parameters
17153 declared as the same subroutine type. */
17154 set_die_type (die, ftype, cu);
17155
17156 if (die->child != NULL)
17157 {
17158 struct type *void_type = objfile_type (objfile)->builtin_void;
17159 struct die_info *child_die;
17160 int nparams, iparams;
17161
17162 /* Count the number of parameters.
17163 FIXME: GDB currently ignores vararg functions, but knows about
17164 vararg member functions. */
17165 nparams = 0;
17166 child_die = die->child;
17167 while (child_die && child_die->tag)
17168 {
17169 if (child_die->tag == DW_TAG_formal_parameter)
17170 nparams++;
17171 else if (child_die->tag == DW_TAG_unspecified_parameters)
17172 TYPE_VARARGS (ftype) = 1;
17173 child_die = child_die->sibling;
17174 }
17175
17176 /* Allocate storage for parameters and fill them in. */
17177 ftype->set_num_fields (nparams);
17178 ftype->set_fields
17179 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17180
17181 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17182 even if we error out during the parameters reading below. */
17183 for (iparams = 0; iparams < nparams; iparams++)
17184 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17185
17186 iparams = 0;
17187 child_die = die->child;
17188 while (child_die && child_die->tag)
17189 {
17190 if (child_die->tag == DW_TAG_formal_parameter)
17191 {
17192 struct type *arg_type;
17193
17194 /* DWARF version 2 has no clean way to discern C++
17195 static and non-static member functions. G++ helps
17196 GDB by marking the first parameter for non-static
17197 member functions (which is the this pointer) as
17198 artificial. We pass this information to
17199 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17200
17201 DWARF version 3 added DW_AT_object_pointer, which GCC
17202 4.5 does not yet generate. */
17203 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17204 if (attr != nullptr)
17205 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17206 else
17207 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17208 arg_type = die_type (child_die, cu);
17209
17210 /* RealView does not mark THIS as const, which the testsuite
17211 expects. GCC marks THIS as const in method definitions,
17212 but not in the class specifications (GCC PR 43053). */
17213 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17214 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17215 {
17216 int is_this = 0;
17217 struct dwarf2_cu *arg_cu = cu;
17218 const char *name = dwarf2_name (child_die, cu);
17219
17220 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17221 if (attr != nullptr)
17222 {
17223 /* If the compiler emits this, use it. */
17224 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17225 is_this = 1;
17226 }
17227 else if (name && strcmp (name, "this") == 0)
17228 /* Function definitions will have the argument names. */
17229 is_this = 1;
17230 else if (name == NULL && iparams == 0)
17231 /* Declarations may not have the names, so like
17232 elsewhere in GDB, assume an artificial first
17233 argument is "this". */
17234 is_this = 1;
17235
17236 if (is_this)
17237 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17238 arg_type, 0);
17239 }
17240
17241 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
17242 iparams++;
17243 }
17244 child_die = child_die->sibling;
17245 }
17246 }
17247
17248 return ftype;
17249 }
17250
17251 static struct type *
17252 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17253 {
17254 struct objfile *objfile = cu->per_objfile->objfile;
17255 const char *name = NULL;
17256 struct type *this_type, *target_type;
17257
17258 name = dwarf2_full_name (NULL, die, cu);
17259 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17260 TYPE_TARGET_STUB (this_type) = 1;
17261 set_die_type (die, this_type, cu);
17262 target_type = die_type (die, cu);
17263 if (target_type != this_type)
17264 TYPE_TARGET_TYPE (this_type) = target_type;
17265 else
17266 {
17267 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17268 spec and cause infinite loops in GDB. */
17269 complaint (_("Self-referential DW_TAG_typedef "
17270 "- DIE at %s [in module %s]"),
17271 sect_offset_str (die->sect_off), objfile_name (objfile));
17272 TYPE_TARGET_TYPE (this_type) = NULL;
17273 }
17274 if (name == NULL)
17275 {
17276 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17277 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17278 Handle these by just returning the target type, rather than
17279 constructing an anonymous typedef type and trying to handle this
17280 elsewhere. */
17281 set_die_type (die, target_type, cu);
17282 return target_type;
17283 }
17284 return this_type;
17285 }
17286
17287 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17288 (which may be different from NAME) to the architecture back-end to allow
17289 it to guess the correct format if necessary. */
17290
17291 static struct type *
17292 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17293 const char *name_hint, enum bfd_endian byte_order)
17294 {
17295 struct gdbarch *gdbarch = objfile->arch ();
17296 const struct floatformat **format;
17297 struct type *type;
17298
17299 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17300 if (format)
17301 type = init_float_type (objfile, bits, name, format, byte_order);
17302 else
17303 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17304
17305 return type;
17306 }
17307
17308 /* Allocate an integer type of size BITS and name NAME. */
17309
17310 static struct type *
17311 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17312 int bits, int unsigned_p, const char *name)
17313 {
17314 struct type *type;
17315
17316 /* Versions of Intel's C Compiler generate an integer type called "void"
17317 instead of using DW_TAG_unspecified_type. This has been seen on
17318 at least versions 14, 17, and 18. */
17319 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17320 && strcmp (name, "void") == 0)
17321 type = objfile_type (objfile)->builtin_void;
17322 else
17323 type = init_integer_type (objfile, bits, unsigned_p, name);
17324
17325 return type;
17326 }
17327
17328 /* Initialise and return a floating point type of size BITS suitable for
17329 use as a component of a complex number. The NAME_HINT is passed through
17330 when initialising the floating point type and is the name of the complex
17331 type.
17332
17333 As DWARF doesn't currently provide an explicit name for the components
17334 of a complex number, but it can be helpful to have these components
17335 named, we try to select a suitable name based on the size of the
17336 component. */
17337 static struct type *
17338 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17339 struct objfile *objfile,
17340 int bits, const char *name_hint,
17341 enum bfd_endian byte_order)
17342 {
17343 gdbarch *gdbarch = objfile->arch ();
17344 struct type *tt = nullptr;
17345
17346 /* Try to find a suitable floating point builtin type of size BITS.
17347 We're going to use the name of this type as the name for the complex
17348 target type that we are about to create. */
17349 switch (cu->language)
17350 {
17351 case language_fortran:
17352 switch (bits)
17353 {
17354 case 32:
17355 tt = builtin_f_type (gdbarch)->builtin_real;
17356 break;
17357 case 64:
17358 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17359 break;
17360 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17361 case 128:
17362 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17363 break;
17364 }
17365 break;
17366 default:
17367 switch (bits)
17368 {
17369 case 32:
17370 tt = builtin_type (gdbarch)->builtin_float;
17371 break;
17372 case 64:
17373 tt = builtin_type (gdbarch)->builtin_double;
17374 break;
17375 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17376 case 128:
17377 tt = builtin_type (gdbarch)->builtin_long_double;
17378 break;
17379 }
17380 break;
17381 }
17382
17383 /* If the type we found doesn't match the size we were looking for, then
17384 pretend we didn't find a type at all, the complex target type we
17385 create will then be nameless. */
17386 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
17387 tt = nullptr;
17388
17389 const char *name = (tt == nullptr) ? nullptr : tt->name ();
17390 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
17391 }
17392
17393 /* Find a representation of a given base type and install
17394 it in the TYPE field of the die. */
17395
17396 static struct type *
17397 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
17398 {
17399 struct objfile *objfile = cu->per_objfile->objfile;
17400 struct type *type;
17401 struct attribute *attr;
17402 int encoding = 0, bits = 0;
17403 const char *name;
17404 gdbarch *arch;
17405
17406 attr = dwarf2_attr (die, DW_AT_encoding, cu);
17407 if (attr != nullptr)
17408 encoding = DW_UNSND (attr);
17409 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17410 if (attr != nullptr)
17411 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
17412 name = dwarf2_name (die, cu);
17413 if (!name)
17414 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
17415
17416 arch = objfile->arch ();
17417 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17418
17419 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17420 if (attr)
17421 {
17422 int endianity = DW_UNSND (attr);
17423
17424 switch (endianity)
17425 {
17426 case DW_END_big:
17427 byte_order = BFD_ENDIAN_BIG;
17428 break;
17429 case DW_END_little:
17430 byte_order = BFD_ENDIAN_LITTLE;
17431 break;
17432 default:
17433 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17434 break;
17435 }
17436 }
17437
17438 switch (encoding)
17439 {
17440 case DW_ATE_address:
17441 /* Turn DW_ATE_address into a void * pointer. */
17442 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
17443 type = init_pointer_type (objfile, bits, name, type);
17444 break;
17445 case DW_ATE_boolean:
17446 type = init_boolean_type (objfile, bits, 1, name);
17447 break;
17448 case DW_ATE_complex_float:
17449 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17450 byte_order);
17451 if (type->code () == TYPE_CODE_ERROR)
17452 {
17453 if (name == nullptr)
17454 {
17455 struct obstack *obstack
17456 = &cu->per_objfile->objfile->objfile_obstack;
17457 name = obconcat (obstack, "_Complex ", type->name (),
17458 nullptr);
17459 }
17460 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17461 }
17462 else
17463 type = init_complex_type (name, type);
17464 break;
17465 case DW_ATE_decimal_float:
17466 type = init_decfloat_type (objfile, bits, name);
17467 break;
17468 case DW_ATE_float:
17469 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
17470 break;
17471 case DW_ATE_signed:
17472 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17473 break;
17474 case DW_ATE_unsigned:
17475 if (cu->language == language_fortran
17476 && name
17477 && startswith (name, "character("))
17478 type = init_character_type (objfile, bits, 1, name);
17479 else
17480 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17481 break;
17482 case DW_ATE_signed_char:
17483 if (cu->language == language_ada || cu->language == language_m2
17484 || cu->language == language_pascal
17485 || cu->language == language_fortran)
17486 type = init_character_type (objfile, bits, 0, name);
17487 else
17488 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
17489 break;
17490 case DW_ATE_unsigned_char:
17491 if (cu->language == language_ada || cu->language == language_m2
17492 || cu->language == language_pascal
17493 || cu->language == language_fortran
17494 || cu->language == language_rust)
17495 type = init_character_type (objfile, bits, 1, name);
17496 else
17497 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17498 break;
17499 case DW_ATE_UTF:
17500 {
17501 if (bits == 16)
17502 type = builtin_type (arch)->builtin_char16;
17503 else if (bits == 32)
17504 type = builtin_type (arch)->builtin_char32;
17505 else
17506 {
17507 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
17508 bits);
17509 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
17510 }
17511 return set_die_type (die, type, cu);
17512 }
17513 break;
17514
17515 default:
17516 complaint (_("unsupported DW_AT_encoding: '%s'"),
17517 dwarf_type_encoding_name (encoding));
17518 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17519 break;
17520 }
17521
17522 if (name && strcmp (name, "char") == 0)
17523 TYPE_NOSIGN (type) = 1;
17524
17525 maybe_set_alignment (cu, die, type);
17526
17527 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
17528
17529 return set_die_type (die, type, cu);
17530 }
17531
17532 /* Parse dwarf attribute if it's a block, reference or constant and put the
17533 resulting value of the attribute into struct bound_prop.
17534 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17535
17536 static int
17537 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17538 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17539 struct type *default_type)
17540 {
17541 struct dwarf2_property_baton *baton;
17542 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17543 struct objfile *objfile = per_objfile->objfile;
17544 struct obstack *obstack = &objfile->objfile_obstack;
17545
17546 gdb_assert (default_type != NULL);
17547
17548 if (attr == NULL || prop == NULL)
17549 return 0;
17550
17551 if (attr->form_is_block ())
17552 {
17553 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17554 baton->property_type = default_type;
17555 baton->locexpr.per_cu = cu->per_cu;
17556 baton->locexpr.per_objfile = per_objfile;
17557 baton->locexpr.size = DW_BLOCK (attr)->size;
17558 baton->locexpr.data = DW_BLOCK (attr)->data;
17559 switch (attr->name)
17560 {
17561 case DW_AT_string_length:
17562 baton->locexpr.is_reference = true;
17563 break;
17564 default:
17565 baton->locexpr.is_reference = false;
17566 break;
17567 }
17568 prop->data.baton = baton;
17569 prop->kind = PROP_LOCEXPR;
17570 gdb_assert (prop->data.baton != NULL);
17571 }
17572 else if (attr->form_is_ref ())
17573 {
17574 struct dwarf2_cu *target_cu = cu;
17575 struct die_info *target_die;
17576 struct attribute *target_attr;
17577
17578 target_die = follow_die_ref (die, attr, &target_cu);
17579 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
17580 if (target_attr == NULL)
17581 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17582 target_cu);
17583 if (target_attr == NULL)
17584 return 0;
17585
17586 switch (target_attr->name)
17587 {
17588 case DW_AT_location:
17589 if (target_attr->form_is_section_offset ())
17590 {
17591 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17592 baton->property_type = die_type (target_die, target_cu);
17593 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17594 prop->data.baton = baton;
17595 prop->kind = PROP_LOCLIST;
17596 gdb_assert (prop->data.baton != NULL);
17597 }
17598 else if (target_attr->form_is_block ())
17599 {
17600 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17601 baton->property_type = die_type (target_die, target_cu);
17602 baton->locexpr.per_cu = cu->per_cu;
17603 baton->locexpr.per_objfile = per_objfile;
17604 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17605 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17606 baton->locexpr.is_reference = true;
17607 prop->data.baton = baton;
17608 prop->kind = PROP_LOCEXPR;
17609 gdb_assert (prop->data.baton != NULL);
17610 }
17611 else
17612 {
17613 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17614 "dynamic property");
17615 return 0;
17616 }
17617 break;
17618 case DW_AT_data_member_location:
17619 {
17620 LONGEST offset;
17621
17622 if (!handle_data_member_location (target_die, target_cu,
17623 &offset))
17624 return 0;
17625
17626 baton = XOBNEW (obstack, struct dwarf2_property_baton);
17627 baton->property_type = read_type_die (target_die->parent,
17628 target_cu);
17629 baton->offset_info.offset = offset;
17630 baton->offset_info.type = die_type (target_die, target_cu);
17631 prop->data.baton = baton;
17632 prop->kind = PROP_ADDR_OFFSET;
17633 break;
17634 }
17635 }
17636 }
17637 else if (attr->form_is_constant ())
17638 {
17639 prop->data.const_val = attr->constant_value (0);
17640 prop->kind = PROP_CONST;
17641 }
17642 else
17643 {
17644 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17645 dwarf2_name (die, cu));
17646 return 0;
17647 }
17648
17649 return 1;
17650 }
17651
17652 /* See read.h. */
17653
17654 struct type *
17655 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
17656 {
17657 struct type *int_type;
17658
17659 /* Helper macro to examine the various builtin types. */
17660 #define TRY_TYPE(F) \
17661 int_type = (unsigned_p \
17662 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17663 : objfile_type (objfile)->builtin_ ## F); \
17664 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
17665 return int_type
17666
17667 TRY_TYPE (char);
17668 TRY_TYPE (short);
17669 TRY_TYPE (int);
17670 TRY_TYPE (long);
17671 TRY_TYPE (long_long);
17672
17673 #undef TRY_TYPE
17674
17675 gdb_assert_not_reached ("unable to find suitable integer type");
17676 }
17677
17678 /* See read.h. */
17679
17680 struct type *
17681 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
17682 {
17683 int addr_size = this->per_cu->addr_size ();
17684 return this->per_objfile->int_type (addr_size, unsigned_p);
17685 }
17686
17687 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17688 present (which is valid) then compute the default type based on the
17689 compilation units address size. */
17690
17691 static struct type *
17692 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17693 {
17694 struct type *index_type = die_type (die, cu);
17695
17696 /* Dwarf-2 specifications explicitly allows to create subrange types
17697 without specifying a base type.
17698 In that case, the base type must be set to the type of
17699 the lower bound, upper bound or count, in that order, if any of these
17700 three attributes references an object that has a type.
17701 If no base type is found, the Dwarf-2 specifications say that
17702 a signed integer type of size equal to the size of an address should
17703 be used.
17704 For the following C code: `extern char gdb_int [];'
17705 GCC produces an empty range DIE.
17706 FIXME: muller/2010-05-28: Possible references to object for low bound,
17707 high bound or count are not yet handled by this code. */
17708 if (index_type->code () == TYPE_CODE_VOID)
17709 index_type = cu->addr_sized_int_type (false);
17710
17711 return index_type;
17712 }
17713
17714 /* Read the given DW_AT_subrange DIE. */
17715
17716 static struct type *
17717 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17718 {
17719 struct type *base_type, *orig_base_type;
17720 struct type *range_type;
17721 struct attribute *attr;
17722 struct dynamic_prop low, high;
17723 int low_default_is_valid;
17724 int high_bound_is_count = 0;
17725 const char *name;
17726 ULONGEST negative_mask;
17727
17728 orig_base_type = read_subrange_index_type (die, cu);
17729
17730 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17731 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17732 creating the range type, but we use the result of check_typedef
17733 when examining properties of the type. */
17734 base_type = check_typedef (orig_base_type);
17735
17736 /* The die_type call above may have already set the type for this DIE. */
17737 range_type = get_die_type (die, cu);
17738 if (range_type)
17739 return range_type;
17740
17741 low.kind = PROP_CONST;
17742 high.kind = PROP_CONST;
17743 high.data.const_val = 0;
17744
17745 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17746 omitting DW_AT_lower_bound. */
17747 switch (cu->language)
17748 {
17749 case language_c:
17750 case language_cplus:
17751 low.data.const_val = 0;
17752 low_default_is_valid = 1;
17753 break;
17754 case language_fortran:
17755 low.data.const_val = 1;
17756 low_default_is_valid = 1;
17757 break;
17758 case language_d:
17759 case language_objc:
17760 case language_rust:
17761 low.data.const_val = 0;
17762 low_default_is_valid = (cu->header.version >= 4);
17763 break;
17764 case language_ada:
17765 case language_m2:
17766 case language_pascal:
17767 low.data.const_val = 1;
17768 low_default_is_valid = (cu->header.version >= 4);
17769 break;
17770 default:
17771 low.data.const_val = 0;
17772 low_default_is_valid = 0;
17773 break;
17774 }
17775
17776 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
17777 if (attr != nullptr)
17778 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
17779 else if (!low_default_is_valid)
17780 complaint (_("Missing DW_AT_lower_bound "
17781 "- DIE at %s [in module %s]"),
17782 sect_offset_str (die->sect_off),
17783 objfile_name (cu->per_objfile->objfile));
17784
17785 struct attribute *attr_ub, *attr_count;
17786 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
17787 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17788 {
17789 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
17790 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
17791 {
17792 /* If bounds are constant do the final calculation here. */
17793 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17794 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17795 else
17796 high_bound_is_count = 1;
17797 }
17798 else
17799 {
17800 if (attr_ub != NULL)
17801 complaint (_("Unresolved DW_AT_upper_bound "
17802 "- DIE at %s [in module %s]"),
17803 sect_offset_str (die->sect_off),
17804 objfile_name (cu->per_objfile->objfile));
17805 if (attr_count != NULL)
17806 complaint (_("Unresolved DW_AT_count "
17807 "- DIE at %s [in module %s]"),
17808 sect_offset_str (die->sect_off),
17809 objfile_name (cu->per_objfile->objfile));
17810 }
17811 }
17812
17813 LONGEST bias = 0;
17814 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17815 if (bias_attr != nullptr && bias_attr->form_is_constant ())
17816 bias = bias_attr->constant_value (0);
17817
17818 /* Normally, the DWARF producers are expected to use a signed
17819 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17820 But this is unfortunately not always the case, as witnessed
17821 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17822 is used instead. To work around that ambiguity, we treat
17823 the bounds as signed, and thus sign-extend their values, when
17824 the base type is signed. */
17825 negative_mask =
17826 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
17827 if (low.kind == PROP_CONST
17828 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17829 low.data.const_val |= negative_mask;
17830 if (high.kind == PROP_CONST
17831 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17832 high.data.const_val |= negative_mask;
17833
17834 /* Check for bit and byte strides. */
17835 struct dynamic_prop byte_stride_prop;
17836 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17837 if (attr_byte_stride != nullptr)
17838 {
17839 struct type *prop_type = cu->addr_sized_int_type (false);
17840 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17841 prop_type);
17842 }
17843
17844 struct dynamic_prop bit_stride_prop;
17845 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17846 if (attr_bit_stride != nullptr)
17847 {
17848 /* It only makes sense to have either a bit or byte stride. */
17849 if (attr_byte_stride != nullptr)
17850 {
17851 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17852 "- DIE at %s [in module %s]"),
17853 sect_offset_str (die->sect_off),
17854 objfile_name (cu->per_objfile->objfile));
17855 attr_bit_stride = nullptr;
17856 }
17857 else
17858 {
17859 struct type *prop_type = cu->addr_sized_int_type (false);
17860 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17861 prop_type);
17862 }
17863 }
17864
17865 if (attr_byte_stride != nullptr
17866 || attr_bit_stride != nullptr)
17867 {
17868 bool byte_stride_p = (attr_byte_stride != nullptr);
17869 struct dynamic_prop *stride
17870 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17871
17872 range_type
17873 = create_range_type_with_stride (NULL, orig_base_type, &low,
17874 &high, bias, stride, byte_stride_p);
17875 }
17876 else
17877 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
17878
17879 if (high_bound_is_count)
17880 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17881
17882 /* Ada expects an empty array on no boundary attributes. */
17883 if (attr == NULL && cu->language != language_ada)
17884 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
17885
17886 name = dwarf2_name (die, cu);
17887 if (name)
17888 range_type->set_name (name);
17889
17890 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17891 if (attr != nullptr)
17892 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17893
17894 maybe_set_alignment (cu, die, range_type);
17895
17896 set_die_type (die, range_type, cu);
17897
17898 /* set_die_type should be already done. */
17899 set_descriptive_type (range_type, die, cu);
17900
17901 return range_type;
17902 }
17903
17904 static struct type *
17905 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17906 {
17907 struct type *type;
17908
17909 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
17910 type->set_name (dwarf2_name (die, cu));
17911
17912 /* In Ada, an unspecified type is typically used when the description
17913 of the type is deferred to a different unit. When encountering
17914 such a type, we treat it as a stub, and try to resolve it later on,
17915 when needed. */
17916 if (cu->language == language_ada)
17917 TYPE_STUB (type) = 1;
17918
17919 return set_die_type (die, type, cu);
17920 }
17921
17922 /* Read a single die and all its descendents. Set the die's sibling
17923 field to NULL; set other fields in the die correctly, and set all
17924 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17925 location of the info_ptr after reading all of those dies. PARENT
17926 is the parent of the die in question. */
17927
17928 static struct die_info *
17929 read_die_and_children (const struct die_reader_specs *reader,
17930 const gdb_byte *info_ptr,
17931 const gdb_byte **new_info_ptr,
17932 struct die_info *parent)
17933 {
17934 struct die_info *die;
17935 const gdb_byte *cur_ptr;
17936
17937 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
17938 if (die == NULL)
17939 {
17940 *new_info_ptr = cur_ptr;
17941 return NULL;
17942 }
17943 store_in_ref_table (die, reader->cu);
17944
17945 if (die->has_children)
17946 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
17947 else
17948 {
17949 die->child = NULL;
17950 *new_info_ptr = cur_ptr;
17951 }
17952
17953 die->sibling = NULL;
17954 die->parent = parent;
17955 return die;
17956 }
17957
17958 /* Read a die, all of its descendents, and all of its siblings; set
17959 all of the fields of all of the dies correctly. Arguments are as
17960 in read_die_and_children. */
17961
17962 static struct die_info *
17963 read_die_and_siblings_1 (const struct die_reader_specs *reader,
17964 const gdb_byte *info_ptr,
17965 const gdb_byte **new_info_ptr,
17966 struct die_info *parent)
17967 {
17968 struct die_info *first_die, *last_sibling;
17969 const gdb_byte *cur_ptr;
17970
17971 cur_ptr = info_ptr;
17972 first_die = last_sibling = NULL;
17973
17974 while (1)
17975 {
17976 struct die_info *die
17977 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
17978
17979 if (die == NULL)
17980 {
17981 *new_info_ptr = cur_ptr;
17982 return first_die;
17983 }
17984
17985 if (!first_die)
17986 first_die = die;
17987 else
17988 last_sibling->sibling = die;
17989
17990 last_sibling = die;
17991 }
17992 }
17993
17994 /* Read a die, all of its descendents, and all of its siblings; set
17995 all of the fields of all of the dies correctly. Arguments are as
17996 in read_die_and_children.
17997 This the main entry point for reading a DIE and all its children. */
17998
17999 static struct die_info *
18000 read_die_and_siblings (const struct die_reader_specs *reader,
18001 const gdb_byte *info_ptr,
18002 const gdb_byte **new_info_ptr,
18003 struct die_info *parent)
18004 {
18005 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18006 new_info_ptr, parent);
18007
18008 if (dwarf_die_debug)
18009 {
18010 fprintf_unfiltered (gdb_stdlog,
18011 "Read die from %s@0x%x of %s:\n",
18012 reader->die_section->get_name (),
18013 (unsigned) (info_ptr - reader->die_section->buffer),
18014 bfd_get_filename (reader->abfd));
18015 dump_die (die, dwarf_die_debug);
18016 }
18017
18018 return die;
18019 }
18020
18021 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18022 attributes.
18023 The caller is responsible for filling in the extra attributes
18024 and updating (*DIEP)->num_attrs.
18025 Set DIEP to point to a newly allocated die with its information,
18026 except for its child, sibling, and parent fields. */
18027
18028 static const gdb_byte *
18029 read_full_die_1 (const struct die_reader_specs *reader,
18030 struct die_info **diep, const gdb_byte *info_ptr,
18031 int num_extra_attrs)
18032 {
18033 unsigned int abbrev_number, bytes_read, i;
18034 struct abbrev_info *abbrev;
18035 struct die_info *die;
18036 struct dwarf2_cu *cu = reader->cu;
18037 bfd *abfd = reader->abfd;
18038
18039 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
18040 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18041 info_ptr += bytes_read;
18042 if (!abbrev_number)
18043 {
18044 *diep = NULL;
18045 return info_ptr;
18046 }
18047
18048 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
18049 if (!abbrev)
18050 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18051 abbrev_number,
18052 bfd_get_filename (abfd));
18053
18054 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
18055 die->sect_off = sect_off;
18056 die->tag = abbrev->tag;
18057 die->abbrev = abbrev_number;
18058 die->has_children = abbrev->has_children;
18059
18060 /* Make the result usable.
18061 The caller needs to update num_attrs after adding the extra
18062 attributes. */
18063 die->num_attrs = abbrev->num_attrs;
18064
18065 std::vector<int> indexes_that_need_reprocess;
18066 for (i = 0; i < abbrev->num_attrs; ++i)
18067 {
18068 bool need_reprocess;
18069 info_ptr =
18070 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18071 info_ptr, &need_reprocess);
18072 if (need_reprocess)
18073 indexes_that_need_reprocess.push_back (i);
18074 }
18075
18076 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18077 if (attr != nullptr)
18078 cu->str_offsets_base = DW_UNSND (attr);
18079
18080 attr = die->attr (DW_AT_loclists_base);
18081 if (attr != nullptr)
18082 cu->loclist_base = DW_UNSND (attr);
18083
18084 auto maybe_addr_base = die->addr_base ();
18085 if (maybe_addr_base.has_value ())
18086 cu->addr_base = *maybe_addr_base;
18087 for (int index : indexes_that_need_reprocess)
18088 read_attribute_reprocess (reader, &die->attrs[index]);
18089 *diep = die;
18090 return info_ptr;
18091 }
18092
18093 /* Read a die and all its attributes.
18094 Set DIEP to point to a newly allocated die with its information,
18095 except for its child, sibling, and parent fields. */
18096
18097 static const gdb_byte *
18098 read_full_die (const struct die_reader_specs *reader,
18099 struct die_info **diep, const gdb_byte *info_ptr)
18100 {
18101 const gdb_byte *result;
18102
18103 result = read_full_die_1 (reader, diep, info_ptr, 0);
18104
18105 if (dwarf_die_debug)
18106 {
18107 fprintf_unfiltered (gdb_stdlog,
18108 "Read die from %s@0x%x of %s:\n",
18109 reader->die_section->get_name (),
18110 (unsigned) (info_ptr - reader->die_section->buffer),
18111 bfd_get_filename (reader->abfd));
18112 dump_die (*diep, dwarf_die_debug);
18113 }
18114
18115 return result;
18116 }
18117 \f
18118
18119 /* Returns nonzero if TAG represents a type that we might generate a partial
18120 symbol for. */
18121
18122 static int
18123 is_type_tag_for_partial (int tag)
18124 {
18125 switch (tag)
18126 {
18127 #if 0
18128 /* Some types that would be reasonable to generate partial symbols for,
18129 that we don't at present. */
18130 case DW_TAG_array_type:
18131 case DW_TAG_file_type:
18132 case DW_TAG_ptr_to_member_type:
18133 case DW_TAG_set_type:
18134 case DW_TAG_string_type:
18135 case DW_TAG_subroutine_type:
18136 #endif
18137 case DW_TAG_base_type:
18138 case DW_TAG_class_type:
18139 case DW_TAG_interface_type:
18140 case DW_TAG_enumeration_type:
18141 case DW_TAG_structure_type:
18142 case DW_TAG_subrange_type:
18143 case DW_TAG_typedef:
18144 case DW_TAG_union_type:
18145 return 1;
18146 default:
18147 return 0;
18148 }
18149 }
18150
18151 /* Load all DIEs that are interesting for partial symbols into memory. */
18152
18153 static struct partial_die_info *
18154 load_partial_dies (const struct die_reader_specs *reader,
18155 const gdb_byte *info_ptr, int building_psymtab)
18156 {
18157 struct dwarf2_cu *cu = reader->cu;
18158 struct objfile *objfile = cu->per_objfile->objfile;
18159 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
18160 unsigned int bytes_read;
18161 unsigned int load_all = 0;
18162 int nesting_level = 1;
18163
18164 parent_die = NULL;
18165 last_die = NULL;
18166
18167 gdb_assert (cu->per_cu != NULL);
18168 if (cu->per_cu->load_all_dies)
18169 load_all = 1;
18170
18171 cu->partial_dies
18172 = htab_create_alloc_ex (cu->header.length / 12,
18173 partial_die_hash,
18174 partial_die_eq,
18175 NULL,
18176 &cu->comp_unit_obstack,
18177 hashtab_obstack_allocate,
18178 dummy_obstack_deallocate);
18179
18180 while (1)
18181 {
18182 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
18183
18184 /* A NULL abbrev means the end of a series of children. */
18185 if (abbrev == NULL)
18186 {
18187 if (--nesting_level == 0)
18188 return first_die;
18189
18190 info_ptr += bytes_read;
18191 last_die = parent_die;
18192 parent_die = parent_die->die_parent;
18193 continue;
18194 }
18195
18196 /* Check for template arguments. We never save these; if
18197 they're seen, we just mark the parent, and go on our way. */
18198 if (parent_die != NULL
18199 && cu->language == language_cplus
18200 && (abbrev->tag == DW_TAG_template_type_param
18201 || abbrev->tag == DW_TAG_template_value_param))
18202 {
18203 parent_die->has_template_arguments = 1;
18204
18205 if (!load_all)
18206 {
18207 /* We don't need a partial DIE for the template argument. */
18208 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18209 continue;
18210 }
18211 }
18212
18213 /* We only recurse into c++ subprograms looking for template arguments.
18214 Skip their other children. */
18215 if (!load_all
18216 && cu->language == language_cplus
18217 && parent_die != NULL
18218 && parent_die->tag == DW_TAG_subprogram)
18219 {
18220 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18221 continue;
18222 }
18223
18224 /* Check whether this DIE is interesting enough to save. Normally
18225 we would not be interested in members here, but there may be
18226 later variables referencing them via DW_AT_specification (for
18227 static members). */
18228 if (!load_all
18229 && !is_type_tag_for_partial (abbrev->tag)
18230 && abbrev->tag != DW_TAG_constant
18231 && abbrev->tag != DW_TAG_enumerator
18232 && abbrev->tag != DW_TAG_subprogram
18233 && abbrev->tag != DW_TAG_inlined_subroutine
18234 && abbrev->tag != DW_TAG_lexical_block
18235 && abbrev->tag != DW_TAG_variable
18236 && abbrev->tag != DW_TAG_namespace
18237 && abbrev->tag != DW_TAG_module
18238 && abbrev->tag != DW_TAG_member
18239 && abbrev->tag != DW_TAG_imported_unit
18240 && abbrev->tag != DW_TAG_imported_declaration)
18241 {
18242 /* Otherwise we skip to the next sibling, if any. */
18243 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
18244 continue;
18245 }
18246
18247 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18248 abbrev);
18249
18250 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
18251
18252 /* This two-pass algorithm for processing partial symbols has a
18253 high cost in cache pressure. Thus, handle some simple cases
18254 here which cover the majority of C partial symbols. DIEs
18255 which neither have specification tags in them, nor could have
18256 specification tags elsewhere pointing at them, can simply be
18257 processed and discarded.
18258
18259 This segment is also optional; scan_partial_symbols and
18260 add_partial_symbol will handle these DIEs if we chain
18261 them in normally. When compilers which do not emit large
18262 quantities of duplicate debug information are more common,
18263 this code can probably be removed. */
18264
18265 /* Any complete simple types at the top level (pretty much all
18266 of them, for a language without namespaces), can be processed
18267 directly. */
18268 if (parent_die == NULL
18269 && pdi.has_specification == 0
18270 && pdi.is_declaration == 0
18271 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18272 || pdi.tag == DW_TAG_base_type
18273 || pdi.tag == DW_TAG_subrange_type))
18274 {
18275 if (building_psymtab && pdi.name != NULL)
18276 add_psymbol_to_list (pdi.name, false,
18277 VAR_DOMAIN, LOC_TYPEDEF, -1,
18278 psymbol_placement::STATIC,
18279 0, cu->language, objfile);
18280 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18281 continue;
18282 }
18283
18284 /* The exception for DW_TAG_typedef with has_children above is
18285 a workaround of GCC PR debug/47510. In the case of this complaint
18286 type_name_or_error will error on such types later.
18287
18288 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18289 it could not find the child DIEs referenced later, this is checked
18290 above. In correct DWARF DW_TAG_typedef should have no children. */
18291
18292 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
18293 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
18294 "- DIE at %s [in module %s]"),
18295 sect_offset_str (pdi.sect_off), objfile_name (objfile));
18296
18297 /* If we're at the second level, and we're an enumerator, and
18298 our parent has no specification (meaning possibly lives in a
18299 namespace elsewhere), then we can add the partial symbol now
18300 instead of queueing it. */
18301 if (pdi.tag == DW_TAG_enumerator
18302 && parent_die != NULL
18303 && parent_die->die_parent == NULL
18304 && parent_die->tag == DW_TAG_enumeration_type
18305 && parent_die->has_specification == 0)
18306 {
18307 if (pdi.name == NULL)
18308 complaint (_("malformed enumerator DIE ignored"));
18309 else if (building_psymtab)
18310 add_psymbol_to_list (pdi.name, false,
18311 VAR_DOMAIN, LOC_CONST, -1,
18312 cu->language == language_cplus
18313 ? psymbol_placement::GLOBAL
18314 : psymbol_placement::STATIC,
18315 0, cu->language, objfile);
18316
18317 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
18318 continue;
18319 }
18320
18321 struct partial_die_info *part_die
18322 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
18323
18324 /* We'll save this DIE so link it in. */
18325 part_die->die_parent = parent_die;
18326 part_die->die_sibling = NULL;
18327 part_die->die_child = NULL;
18328
18329 if (last_die && last_die == parent_die)
18330 last_die->die_child = part_die;
18331 else if (last_die)
18332 last_die->die_sibling = part_die;
18333
18334 last_die = part_die;
18335
18336 if (first_die == NULL)
18337 first_die = part_die;
18338
18339 /* Maybe add the DIE to the hash table. Not all DIEs that we
18340 find interesting need to be in the hash table, because we
18341 also have the parent/sibling/child chains; only those that we
18342 might refer to by offset later during partial symbol reading.
18343
18344 For now this means things that might have be the target of a
18345 DW_AT_specification, DW_AT_abstract_origin, or
18346 DW_AT_extension. DW_AT_extension will refer only to
18347 namespaces; DW_AT_abstract_origin refers to functions (and
18348 many things under the function DIE, but we do not recurse
18349 into function DIEs during partial symbol reading) and
18350 possibly variables as well; DW_AT_specification refers to
18351 declarations. Declarations ought to have the DW_AT_declaration
18352 flag. It happens that GCC forgets to put it in sometimes, but
18353 only for functions, not for types.
18354
18355 Adding more things than necessary to the hash table is harmless
18356 except for the performance cost. Adding too few will result in
18357 wasted time in find_partial_die, when we reread the compilation
18358 unit with load_all_dies set. */
18359
18360 if (load_all
18361 || abbrev->tag == DW_TAG_constant
18362 || abbrev->tag == DW_TAG_subprogram
18363 || abbrev->tag == DW_TAG_variable
18364 || abbrev->tag == DW_TAG_namespace
18365 || part_die->is_declaration)
18366 {
18367 void **slot;
18368
18369 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
18370 to_underlying (part_die->sect_off),
18371 INSERT);
18372 *slot = part_die;
18373 }
18374
18375 /* For some DIEs we want to follow their children (if any). For C
18376 we have no reason to follow the children of structures; for other
18377 languages we have to, so that we can get at method physnames
18378 to infer fully qualified class names, for DW_AT_specification,
18379 and for C++ template arguments. For C++, we also look one level
18380 inside functions to find template arguments (if the name of the
18381 function does not already contain the template arguments).
18382
18383 For Ada and Fortran, we need to scan the children of subprograms
18384 and lexical blocks as well because these languages allow the
18385 definition of nested entities that could be interesting for the
18386 debugger, such as nested subprograms for instance. */
18387 if (last_die->has_children
18388 && (load_all
18389 || last_die->tag == DW_TAG_namespace
18390 || last_die->tag == DW_TAG_module
18391 || last_die->tag == DW_TAG_enumeration_type
18392 || (cu->language == language_cplus
18393 && last_die->tag == DW_TAG_subprogram
18394 && (last_die->name == NULL
18395 || strchr (last_die->name, '<') == NULL))
18396 || (cu->language != language_c
18397 && (last_die->tag == DW_TAG_class_type
18398 || last_die->tag == DW_TAG_interface_type
18399 || last_die->tag == DW_TAG_structure_type
18400 || last_die->tag == DW_TAG_union_type))
18401 || ((cu->language == language_ada
18402 || cu->language == language_fortran)
18403 && (last_die->tag == DW_TAG_subprogram
18404 || last_die->tag == DW_TAG_lexical_block))))
18405 {
18406 nesting_level++;
18407 parent_die = last_die;
18408 continue;
18409 }
18410
18411 /* Otherwise we skip to the next sibling, if any. */
18412 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
18413
18414 /* Back to the top, do it again. */
18415 }
18416 }
18417
18418 partial_die_info::partial_die_info (sect_offset sect_off_,
18419 struct abbrev_info *abbrev)
18420 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18421 {
18422 }
18423
18424 /* Read a minimal amount of information into the minimal die structure.
18425 INFO_PTR should point just after the initial uleb128 of a DIE. */
18426
18427 const gdb_byte *
18428 partial_die_info::read (const struct die_reader_specs *reader,
18429 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
18430 {
18431 struct dwarf2_cu *cu = reader->cu;
18432 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18433 unsigned int i;
18434 int has_low_pc_attr = 0;
18435 int has_high_pc_attr = 0;
18436 int high_pc_relative = 0;
18437
18438 for (i = 0; i < abbrev.num_attrs; ++i)
18439 {
18440 attribute attr;
18441 bool need_reprocess;
18442 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18443 info_ptr, &need_reprocess);
18444 /* String and address offsets that need to do the reprocessing have
18445 already been read at this point, so there is no need to wait until
18446 the loop terminates to do the reprocessing. */
18447 if (need_reprocess)
18448 read_attribute_reprocess (reader, &attr);
18449 /* Store the data if it is of an attribute we want to keep in a
18450 partial symbol table. */
18451 switch (attr.name)
18452 {
18453 case DW_AT_name:
18454 switch (tag)
18455 {
18456 case DW_TAG_compile_unit:
18457 case DW_TAG_partial_unit:
18458 case DW_TAG_type_unit:
18459 /* Compilation units have a DW_AT_name that is a filename, not
18460 a source language identifier. */
18461 case DW_TAG_enumeration_type:
18462 case DW_TAG_enumerator:
18463 /* These tags always have simple identifiers already; no need
18464 to canonicalize them. */
18465 name = DW_STRING (&attr);
18466 break;
18467 default:
18468 {
18469 struct objfile *objfile = dwarf2_per_objfile->objfile;
18470
18471 name
18472 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
18473 }
18474 break;
18475 }
18476 break;
18477 case DW_AT_linkage_name:
18478 case DW_AT_MIPS_linkage_name:
18479 /* Note that both forms of linkage name might appear. We
18480 assume they will be the same, and we only store the last
18481 one we see. */
18482 linkage_name = attr.value_as_string ();
18483 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18484 See https://github.com/rust-lang/rust/issues/32925. */
18485 if (cu->language == language_rust && linkage_name != NULL
18486 && strchr (linkage_name, '{') != NULL)
18487 linkage_name = NULL;
18488 break;
18489 case DW_AT_low_pc:
18490 has_low_pc_attr = 1;
18491 lowpc = attr.value_as_address ();
18492 break;
18493 case DW_AT_high_pc:
18494 has_high_pc_attr = 1;
18495 highpc = attr.value_as_address ();
18496 if (cu->header.version >= 4 && attr.form_is_constant ())
18497 high_pc_relative = 1;
18498 break;
18499 case DW_AT_location:
18500 /* Support the .debug_loc offsets. */
18501 if (attr.form_is_block ())
18502 {
18503 d.locdesc = DW_BLOCK (&attr);
18504 }
18505 else if (attr.form_is_section_offset ())
18506 {
18507 dwarf2_complex_location_expr_complaint ();
18508 }
18509 else
18510 {
18511 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18512 "partial symbol information");
18513 }
18514 break;
18515 case DW_AT_external:
18516 is_external = DW_UNSND (&attr);
18517 break;
18518 case DW_AT_declaration:
18519 is_declaration = DW_UNSND (&attr);
18520 break;
18521 case DW_AT_type:
18522 has_type = 1;
18523 break;
18524 case DW_AT_abstract_origin:
18525 case DW_AT_specification:
18526 case DW_AT_extension:
18527 has_specification = 1;
18528 spec_offset = attr.get_ref_die_offset ();
18529 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18530 || cu->per_cu->is_dwz);
18531 break;
18532 case DW_AT_sibling:
18533 /* Ignore absolute siblings, they might point outside of
18534 the current compile unit. */
18535 if (attr.form == DW_FORM_ref_addr)
18536 complaint (_("ignoring absolute DW_AT_sibling"));
18537 else
18538 {
18539 const gdb_byte *buffer = reader->buffer;
18540 sect_offset off = attr.get_ref_die_offset ();
18541 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
18542
18543 if (sibling_ptr < info_ptr)
18544 complaint (_("DW_AT_sibling points backwards"));
18545 else if (sibling_ptr > reader->buffer_end)
18546 reader->die_section->overflow_complaint ();
18547 else
18548 sibling = sibling_ptr;
18549 }
18550 break;
18551 case DW_AT_byte_size:
18552 has_byte_size = 1;
18553 break;
18554 case DW_AT_const_value:
18555 has_const_value = 1;
18556 break;
18557 case DW_AT_calling_convention:
18558 /* DWARF doesn't provide a way to identify a program's source-level
18559 entry point. DW_AT_calling_convention attributes are only meant
18560 to describe functions' calling conventions.
18561
18562 However, because it's a necessary piece of information in
18563 Fortran, and before DWARF 4 DW_CC_program was the only
18564 piece of debugging information whose definition refers to
18565 a 'main program' at all, several compilers marked Fortran
18566 main programs with DW_CC_program --- even when those
18567 functions use the standard calling conventions.
18568
18569 Although DWARF now specifies a way to provide this
18570 information, we support this practice for backward
18571 compatibility. */
18572 if (DW_UNSND (&attr) == DW_CC_program
18573 && cu->language == language_fortran)
18574 main_subprogram = 1;
18575 break;
18576 case DW_AT_inline:
18577 if (DW_UNSND (&attr) == DW_INL_inlined
18578 || DW_UNSND (&attr) == DW_INL_declared_inlined)
18579 may_be_inlined = 1;
18580 break;
18581
18582 case DW_AT_import:
18583 if (tag == DW_TAG_imported_unit)
18584 {
18585 d.sect_off = attr.get_ref_die_offset ();
18586 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
18587 || cu->per_cu->is_dwz);
18588 }
18589 break;
18590
18591 case DW_AT_main_subprogram:
18592 main_subprogram = DW_UNSND (&attr);
18593 break;
18594
18595 case DW_AT_ranges:
18596 {
18597 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18598 but that requires a full DIE, so instead we just
18599 reimplement it. */
18600 int need_ranges_base = tag != DW_TAG_compile_unit;
18601 unsigned int ranges_offset = (DW_UNSND (&attr)
18602 + (need_ranges_base
18603 ? cu->ranges_base
18604 : 0));
18605
18606 /* Value of the DW_AT_ranges attribute is the offset in the
18607 .debug_ranges section. */
18608 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18609 nullptr))
18610 has_pc_info = 1;
18611 }
18612 break;
18613
18614 default:
18615 break;
18616 }
18617 }
18618
18619 /* For Ada, if both the name and the linkage name appear, we prefer
18620 the latter. This lets "catch exception" work better, regardless
18621 of the order in which the name and linkage name were emitted.
18622 Really, though, this is just a workaround for the fact that gdb
18623 doesn't store both the name and the linkage name. */
18624 if (cu->language == language_ada && linkage_name != nullptr)
18625 name = linkage_name;
18626
18627 if (high_pc_relative)
18628 highpc += lowpc;
18629
18630 if (has_low_pc_attr && has_high_pc_attr)
18631 {
18632 /* When using the GNU linker, .gnu.linkonce. sections are used to
18633 eliminate duplicate copies of functions and vtables and such.
18634 The linker will arbitrarily choose one and discard the others.
18635 The AT_*_pc values for such functions refer to local labels in
18636 these sections. If the section from that file was discarded, the
18637 labels are not in the output, so the relocs get a value of 0.
18638 If this is a discarded function, mark the pc bounds as invalid,
18639 so that GDB will ignore it. */
18640 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
18641 {
18642 struct objfile *objfile = dwarf2_per_objfile->objfile;
18643 struct gdbarch *gdbarch = objfile->arch ();
18644
18645 complaint (_("DW_AT_low_pc %s is zero "
18646 "for DIE at %s [in module %s]"),
18647 paddress (gdbarch, lowpc),
18648 sect_offset_str (sect_off),
18649 objfile_name (objfile));
18650 }
18651 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
18652 else if (lowpc >= highpc)
18653 {
18654 struct objfile *objfile = dwarf2_per_objfile->objfile;
18655 struct gdbarch *gdbarch = objfile->arch ();
18656
18657 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
18658 "for DIE at %s [in module %s]"),
18659 paddress (gdbarch, lowpc),
18660 paddress (gdbarch, highpc),
18661 sect_offset_str (sect_off),
18662 objfile_name (objfile));
18663 }
18664 else
18665 has_pc_info = 1;
18666 }
18667
18668 return info_ptr;
18669 }
18670
18671 /* Find a cached partial DIE at OFFSET in CU. */
18672
18673 struct partial_die_info *
18674 dwarf2_cu::find_partial_die (sect_offset sect_off)
18675 {
18676 struct partial_die_info *lookup_die = NULL;
18677 struct partial_die_info part_die (sect_off);
18678
18679 lookup_die = ((struct partial_die_info *)
18680 htab_find_with_hash (partial_dies, &part_die,
18681 to_underlying (sect_off)));
18682
18683 return lookup_die;
18684 }
18685
18686 /* Find a partial DIE at OFFSET, which may or may not be in CU,
18687 except in the case of .debug_types DIEs which do not reference
18688 outside their CU (they do however referencing other types via
18689 DW_FORM_ref_sig8). */
18690
18691 static const struct cu_partial_die_info
18692 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
18693 {
18694 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18695 struct objfile *objfile = dwarf2_per_objfile->objfile;
18696 struct partial_die_info *pd = NULL;
18697
18698 if (offset_in_dwz == cu->per_cu->is_dwz
18699 && cu->header.offset_in_cu_p (sect_off))
18700 {
18701 pd = cu->find_partial_die (sect_off);
18702 if (pd != NULL)
18703 return { cu, pd };
18704 /* We missed recording what we needed.
18705 Load all dies and try again. */
18706 }
18707 else
18708 {
18709 /* TUs don't reference other CUs/TUs (except via type signatures). */
18710 if (cu->per_cu->is_debug_types)
18711 {
18712 error (_("Dwarf Error: Type Unit at offset %s contains"
18713 " external reference to offset %s [in module %s].\n"),
18714 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
18715 bfd_get_filename (objfile->obfd));
18716 }
18717 dwarf2_per_cu_data *per_cu
18718 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18719 dwarf2_per_objfile);
18720
18721 cu = dwarf2_per_objfile->get_cu (per_cu);
18722 if (cu == NULL || cu->partial_dies == NULL)
18723 load_partial_comp_unit (per_cu, dwarf2_per_objfile, nullptr);
18724
18725 cu = dwarf2_per_objfile->get_cu (per_cu);
18726
18727 cu->last_used = 0;
18728 pd = cu->find_partial_die (sect_off);
18729 }
18730
18731 /* If we didn't find it, and not all dies have been loaded,
18732 load them all and try again. */
18733
18734 if (pd == NULL && cu->per_cu->load_all_dies == 0)
18735 {
18736 cu->per_cu->load_all_dies = 1;
18737
18738 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18739 THIS_CU->cu may already be in use. So we can't just free it and
18740 replace its DIEs with the ones we read in. Instead, we leave those
18741 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18742 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18743 set. */
18744 load_partial_comp_unit (cu->per_cu, dwarf2_per_objfile, cu);
18745
18746 pd = cu->find_partial_die (sect_off);
18747 }
18748
18749 if (pd == NULL)
18750 internal_error (__FILE__, __LINE__,
18751 _("could not find partial DIE %s "
18752 "in cache [from module %s]\n"),
18753 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
18754 return { cu, pd };
18755 }
18756
18757 /* See if we can figure out if the class lives in a namespace. We do
18758 this by looking for a member function; its demangled name will
18759 contain namespace info, if there is any. */
18760
18761 static void
18762 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18763 struct dwarf2_cu *cu)
18764 {
18765 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18766 what template types look like, because the demangler
18767 frequently doesn't give the same name as the debug info. We
18768 could fix this by only using the demangled name to get the
18769 prefix (but see comment in read_structure_type). */
18770
18771 struct partial_die_info *real_pdi;
18772 struct partial_die_info *child_pdi;
18773
18774 /* If this DIE (this DIE's specification, if any) has a parent, then
18775 we should not do this. We'll prepend the parent's fully qualified
18776 name when we create the partial symbol. */
18777
18778 real_pdi = struct_pdi;
18779 while (real_pdi->has_specification)
18780 {
18781 auto res = find_partial_die (real_pdi->spec_offset,
18782 real_pdi->spec_is_dwz, cu);
18783 real_pdi = res.pdi;
18784 cu = res.cu;
18785 }
18786
18787 if (real_pdi->die_parent != NULL)
18788 return;
18789
18790 for (child_pdi = struct_pdi->die_child;
18791 child_pdi != NULL;
18792 child_pdi = child_pdi->die_sibling)
18793 {
18794 if (child_pdi->tag == DW_TAG_subprogram
18795 && child_pdi->linkage_name != NULL)
18796 {
18797 gdb::unique_xmalloc_ptr<char> actual_class_name
18798 (language_class_name_from_physname (cu->language_defn,
18799 child_pdi->linkage_name));
18800 if (actual_class_name != NULL)
18801 {
18802 struct objfile *objfile = cu->per_objfile->objfile;
18803 struct_pdi->name = objfile->intern (actual_class_name.get ());
18804 }
18805 break;
18806 }
18807 }
18808 }
18809
18810 /* Return true if a DIE with TAG may have the DW_AT_const_value
18811 attribute. */
18812
18813 static bool
18814 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18815 {
18816 switch (tag)
18817 {
18818 case DW_TAG_constant:
18819 case DW_TAG_enumerator:
18820 case DW_TAG_formal_parameter:
18821 case DW_TAG_template_value_param:
18822 case DW_TAG_variable:
18823 return true;
18824 }
18825
18826 return false;
18827 }
18828
18829 void
18830 partial_die_info::fixup (struct dwarf2_cu *cu)
18831 {
18832 /* Once we've fixed up a die, there's no point in doing so again.
18833 This also avoids a memory leak if we were to call
18834 guess_partial_die_structure_name multiple times. */
18835 if (fixup_called)
18836 return;
18837
18838 /* If we found a reference attribute and the DIE has no name, try
18839 to find a name in the referred to DIE. */
18840
18841 if (name == NULL && has_specification)
18842 {
18843 struct partial_die_info *spec_die;
18844
18845 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18846 spec_die = res.pdi;
18847 cu = res.cu;
18848
18849 spec_die->fixup (cu);
18850
18851 if (spec_die->name)
18852 {
18853 name = spec_die->name;
18854
18855 /* Copy DW_AT_external attribute if it is set. */
18856 if (spec_die->is_external)
18857 is_external = spec_die->is_external;
18858 }
18859 }
18860
18861 if (!has_const_value && has_specification
18862 && can_have_DW_AT_const_value_p (tag))
18863 {
18864 struct partial_die_info *spec_die;
18865
18866 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18867 spec_die = res.pdi;
18868 cu = res.cu;
18869
18870 spec_die->fixup (cu);
18871
18872 if (spec_die->has_const_value)
18873 {
18874 /* Copy DW_AT_const_value attribute if it is set. */
18875 has_const_value = spec_die->has_const_value;
18876 }
18877 }
18878
18879 /* Set default names for some unnamed DIEs. */
18880
18881 if (name == NULL && tag == DW_TAG_namespace)
18882 name = CP_ANONYMOUS_NAMESPACE_STR;
18883
18884 /* If there is no parent die to provide a namespace, and there are
18885 children, see if we can determine the namespace from their linkage
18886 name. */
18887 if (cu->language == language_cplus
18888 && !cu->per_objfile->per_bfd->types.empty ()
18889 && die_parent == NULL
18890 && has_children
18891 && (tag == DW_TAG_class_type
18892 || tag == DW_TAG_structure_type
18893 || tag == DW_TAG_union_type))
18894 guess_partial_die_structure_name (this, cu);
18895
18896 /* GCC might emit a nameless struct or union that has a linkage
18897 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
18898 if (name == NULL
18899 && (tag == DW_TAG_class_type
18900 || tag == DW_TAG_interface_type
18901 || tag == DW_TAG_structure_type
18902 || tag == DW_TAG_union_type)
18903 && linkage_name != NULL)
18904 {
18905 gdb::unique_xmalloc_ptr<char> demangled
18906 (gdb_demangle (linkage_name, DMGL_TYPES));
18907 if (demangled != nullptr)
18908 {
18909 const char *base;
18910
18911 /* Strip any leading namespaces/classes, keep only the base name.
18912 DW_AT_name for named DIEs does not contain the prefixes. */
18913 base = strrchr (demangled.get (), ':');
18914 if (base && base > demangled.get () && base[-1] == ':')
18915 base++;
18916 else
18917 base = demangled.get ();
18918
18919 struct objfile *objfile = cu->per_objfile->objfile;
18920 name = objfile->intern (base);
18921 }
18922 }
18923
18924 fixup_called = 1;
18925 }
18926
18927 /* Read the .debug_loclists header contents from the given SECTION in the
18928 HEADER. */
18929 static void
18930 read_loclist_header (struct loclist_header *header,
18931 struct dwarf2_section_info *section)
18932 {
18933 unsigned int bytes_read;
18934 bfd *abfd = section->get_bfd_owner ();
18935 const gdb_byte *info_ptr = section->buffer;
18936 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18937 info_ptr += bytes_read;
18938 header->version = read_2_bytes (abfd, info_ptr);
18939 info_ptr += 2;
18940 header->addr_size = read_1_byte (abfd, info_ptr);
18941 info_ptr += 1;
18942 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18943 info_ptr += 1;
18944 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18945 }
18946
18947 /* Return the DW_AT_loclists_base value for the CU. */
18948 static ULONGEST
18949 lookup_loclist_base (struct dwarf2_cu *cu)
18950 {
18951 /* For the .dwo unit, the loclist_base points to the first offset following
18952 the header. The header consists of the following entities-
18953 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18954 bit format)
18955 2. version (2 bytes)
18956 3. address size (1 byte)
18957 4. segment selector size (1 byte)
18958 5. offset entry count (4 bytes)
18959 These sizes are derived as per the DWARFv5 standard. */
18960 if (cu->dwo_unit != nullptr)
18961 {
18962 if (cu->header.initial_length_size == 4)
18963 return LOCLIST_HEADER_SIZE32;
18964 return LOCLIST_HEADER_SIZE64;
18965 }
18966 return cu->loclist_base;
18967 }
18968
18969 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18970 array of offsets in the .debug_loclists section. */
18971 static CORE_ADDR
18972 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18973 {
18974 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
18975 struct objfile *objfile = dwarf2_per_objfile->objfile;
18976 bfd *abfd = objfile->obfd;
18977 ULONGEST loclist_base = lookup_loclist_base (cu);
18978 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18979
18980 section->read (objfile);
18981 if (section->buffer == NULL)
18982 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18983 "section [in module %s]"), objfile_name (objfile));
18984 struct loclist_header header;
18985 read_loclist_header (&header, section);
18986 if (loclist_index >= header.offset_entry_count)
18987 complaint (_("DW_FORM_loclistx pointing outside of "
18988 ".debug_loclists offset array [in module %s]"),
18989 objfile_name (objfile));
18990 if (loclist_base + loclist_index * cu->header.offset_size
18991 >= section->size)
18992 complaint (_("DW_FORM_loclistx pointing outside of "
18993 ".debug_loclists section [in module %s]"),
18994 objfile_name (objfile));
18995 const gdb_byte *info_ptr
18996 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18997
18998 if (cu->header.offset_size == 4)
18999 return bfd_get_32 (abfd, info_ptr) + loclist_base;
19000 else
19001 return bfd_get_64 (abfd, info_ptr) + loclist_base;
19002 }
19003
19004 /* Process the attributes that had to be skipped in the first round. These
19005 attributes are the ones that need str_offsets_base or addr_base attributes.
19006 They could not have been processed in the first round, because at the time
19007 the values of str_offsets_base or addr_base may not have been known. */
19008 static void
19009 read_attribute_reprocess (const struct die_reader_specs *reader,
19010 struct attribute *attr)
19011 {
19012 struct dwarf2_cu *cu = reader->cu;
19013 switch (attr->form)
19014 {
19015 case DW_FORM_addrx:
19016 case DW_FORM_GNU_addr_index:
19017 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
19018 break;
19019 case DW_FORM_loclistx:
19020 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19021 break;
19022 case DW_FORM_strx:
19023 case DW_FORM_strx1:
19024 case DW_FORM_strx2:
19025 case DW_FORM_strx3:
19026 case DW_FORM_strx4:
19027 case DW_FORM_GNU_str_index:
19028 {
19029 unsigned int str_index = DW_UNSND (attr);
19030 if (reader->dwo_file != NULL)
19031 {
19032 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19033 DW_STRING_IS_CANONICAL (attr) = 0;
19034 }
19035 else
19036 {
19037 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19038 DW_STRING_IS_CANONICAL (attr) = 0;
19039 }
19040 break;
19041 }
19042 default:
19043 gdb_assert_not_reached (_("Unexpected DWARF form."));
19044 }
19045 }
19046
19047 /* Read an attribute value described by an attribute form. */
19048
19049 static const gdb_byte *
19050 read_attribute_value (const struct die_reader_specs *reader,
19051 struct attribute *attr, unsigned form,
19052 LONGEST implicit_const, const gdb_byte *info_ptr,
19053 bool *need_reprocess)
19054 {
19055 struct dwarf2_cu *cu = reader->cu;
19056 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19057 struct objfile *objfile = dwarf2_per_objfile->objfile;
19058 bfd *abfd = reader->abfd;
19059 struct comp_unit_head *cu_header = &cu->header;
19060 unsigned int bytes_read;
19061 struct dwarf_block *blk;
19062 *need_reprocess = false;
19063
19064 attr->form = (enum dwarf_form) form;
19065 switch (form)
19066 {
19067 case DW_FORM_ref_addr:
19068 if (cu->header.version == 2)
19069 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19070 &bytes_read);
19071 else
19072 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19073 &bytes_read);
19074 info_ptr += bytes_read;
19075 break;
19076 case DW_FORM_GNU_ref_alt:
19077 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19078 info_ptr += bytes_read;
19079 break;
19080 case DW_FORM_addr:
19081 {
19082 struct gdbarch *gdbarch = objfile->arch ();
19083 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19084 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19085 info_ptr += bytes_read;
19086 }
19087 break;
19088 case DW_FORM_block2:
19089 blk = dwarf_alloc_block (cu);
19090 blk->size = read_2_bytes (abfd, info_ptr);
19091 info_ptr += 2;
19092 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19093 info_ptr += blk->size;
19094 DW_BLOCK (attr) = blk;
19095 break;
19096 case DW_FORM_block4:
19097 blk = dwarf_alloc_block (cu);
19098 blk->size = read_4_bytes (abfd, info_ptr);
19099 info_ptr += 4;
19100 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19101 info_ptr += blk->size;
19102 DW_BLOCK (attr) = blk;
19103 break;
19104 case DW_FORM_data2:
19105 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19106 info_ptr += 2;
19107 break;
19108 case DW_FORM_data4:
19109 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19110 info_ptr += 4;
19111 break;
19112 case DW_FORM_data8:
19113 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19114 info_ptr += 8;
19115 break;
19116 case DW_FORM_data16:
19117 blk = dwarf_alloc_block (cu);
19118 blk->size = 16;
19119 blk->data = read_n_bytes (abfd, info_ptr, 16);
19120 info_ptr += 16;
19121 DW_BLOCK (attr) = blk;
19122 break;
19123 case DW_FORM_sec_offset:
19124 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
19125 info_ptr += bytes_read;
19126 break;
19127 case DW_FORM_loclistx:
19128 {
19129 *need_reprocess = true;
19130 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19131 info_ptr += bytes_read;
19132 }
19133 break;
19134 case DW_FORM_string:
19135 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
19136 DW_STRING_IS_CANONICAL (attr) = 0;
19137 info_ptr += bytes_read;
19138 break;
19139 case DW_FORM_strp:
19140 if (!cu->per_cu->is_dwz)
19141 {
19142 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19143 abfd, info_ptr, cu_header,
19144 &bytes_read);
19145 DW_STRING_IS_CANONICAL (attr) = 0;
19146 info_ptr += bytes_read;
19147 break;
19148 }
19149 /* FALLTHROUGH */
19150 case DW_FORM_line_strp:
19151 if (!cu->per_cu->is_dwz)
19152 {
19153 DW_STRING (attr)
19154 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19155 &bytes_read);
19156 DW_STRING_IS_CANONICAL (attr) = 0;
19157 info_ptr += bytes_read;
19158 break;
19159 }
19160 /* FALLTHROUGH */
19161 case DW_FORM_GNU_strp_alt:
19162 {
19163 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19164 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19165 &bytes_read);
19166
19167 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
19168 DW_STRING_IS_CANONICAL (attr) = 0;
19169 info_ptr += bytes_read;
19170 }
19171 break;
19172 case DW_FORM_exprloc:
19173 case DW_FORM_block:
19174 blk = dwarf_alloc_block (cu);
19175 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19176 info_ptr += bytes_read;
19177 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19178 info_ptr += blk->size;
19179 DW_BLOCK (attr) = blk;
19180 break;
19181 case DW_FORM_block1:
19182 blk = dwarf_alloc_block (cu);
19183 blk->size = read_1_byte (abfd, info_ptr);
19184 info_ptr += 1;
19185 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19186 info_ptr += blk->size;
19187 DW_BLOCK (attr) = blk;
19188 break;
19189 case DW_FORM_data1:
19190 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19191 info_ptr += 1;
19192 break;
19193 case DW_FORM_flag:
19194 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19195 info_ptr += 1;
19196 break;
19197 case DW_FORM_flag_present:
19198 DW_UNSND (attr) = 1;
19199 break;
19200 case DW_FORM_sdata:
19201 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19202 info_ptr += bytes_read;
19203 break;
19204 case DW_FORM_udata:
19205 case DW_FORM_rnglistx:
19206 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19207 info_ptr += bytes_read;
19208 break;
19209 case DW_FORM_ref1:
19210 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19211 + read_1_byte (abfd, info_ptr));
19212 info_ptr += 1;
19213 break;
19214 case DW_FORM_ref2:
19215 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19216 + read_2_bytes (abfd, info_ptr));
19217 info_ptr += 2;
19218 break;
19219 case DW_FORM_ref4:
19220 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19221 + read_4_bytes (abfd, info_ptr));
19222 info_ptr += 4;
19223 break;
19224 case DW_FORM_ref8:
19225 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19226 + read_8_bytes (abfd, info_ptr));
19227 info_ptr += 8;
19228 break;
19229 case DW_FORM_ref_sig8:
19230 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
19231 info_ptr += 8;
19232 break;
19233 case DW_FORM_ref_udata:
19234 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
19235 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
19236 info_ptr += bytes_read;
19237 break;
19238 case DW_FORM_indirect:
19239 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19240 info_ptr += bytes_read;
19241 if (form == DW_FORM_implicit_const)
19242 {
19243 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19244 info_ptr += bytes_read;
19245 }
19246 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19247 info_ptr, need_reprocess);
19248 break;
19249 case DW_FORM_implicit_const:
19250 DW_SND (attr) = implicit_const;
19251 break;
19252 case DW_FORM_addrx:
19253 case DW_FORM_GNU_addr_index:
19254 *need_reprocess = true;
19255 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19256 info_ptr += bytes_read;
19257 break;
19258 case DW_FORM_strx:
19259 case DW_FORM_strx1:
19260 case DW_FORM_strx2:
19261 case DW_FORM_strx3:
19262 case DW_FORM_strx4:
19263 case DW_FORM_GNU_str_index:
19264 {
19265 ULONGEST str_index;
19266 if (form == DW_FORM_strx1)
19267 {
19268 str_index = read_1_byte (abfd, info_ptr);
19269 info_ptr += 1;
19270 }
19271 else if (form == DW_FORM_strx2)
19272 {
19273 str_index = read_2_bytes (abfd, info_ptr);
19274 info_ptr += 2;
19275 }
19276 else if (form == DW_FORM_strx3)
19277 {
19278 str_index = read_3_bytes (abfd, info_ptr);
19279 info_ptr += 3;
19280 }
19281 else if (form == DW_FORM_strx4)
19282 {
19283 str_index = read_4_bytes (abfd, info_ptr);
19284 info_ptr += 4;
19285 }
19286 else
19287 {
19288 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19289 info_ptr += bytes_read;
19290 }
19291 *need_reprocess = true;
19292 DW_UNSND (attr) = str_index;
19293 }
19294 break;
19295 default:
19296 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
19297 dwarf_form_name (form),
19298 bfd_get_filename (abfd));
19299 }
19300
19301 /* Super hack. */
19302 if (cu->per_cu->is_dwz && attr->form_is_ref ())
19303 attr->form = DW_FORM_GNU_ref_alt;
19304
19305 /* We have seen instances where the compiler tried to emit a byte
19306 size attribute of -1 which ended up being encoded as an unsigned
19307 0xffffffff. Although 0xffffffff is technically a valid size value,
19308 an object of this size seems pretty unlikely so we can relatively
19309 safely treat these cases as if the size attribute was invalid and
19310 treat them as zero by default. */
19311 if (attr->name == DW_AT_byte_size
19312 && form == DW_FORM_data4
19313 && DW_UNSND (attr) >= 0xffffffff)
19314 {
19315 complaint
19316 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
19317 hex_string (DW_UNSND (attr)));
19318 DW_UNSND (attr) = 0;
19319 }
19320
19321 return info_ptr;
19322 }
19323
19324 /* Read an attribute described by an abbreviated attribute. */
19325
19326 static const gdb_byte *
19327 read_attribute (const struct die_reader_specs *reader,
19328 struct attribute *attr, struct attr_abbrev *abbrev,
19329 const gdb_byte *info_ptr, bool *need_reprocess)
19330 {
19331 attr->name = abbrev->name;
19332 return read_attribute_value (reader, attr, abbrev->form,
19333 abbrev->implicit_const, info_ptr,
19334 need_reprocess);
19335 }
19336
19337 /* Return pointer to string at .debug_str offset STR_OFFSET. */
19338
19339 static const char *
19340 read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19341 LONGEST str_offset)
19342 {
19343 return dwarf2_per_objfile->per_bfd->str.read_string
19344 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
19345 }
19346
19347 /* Return pointer to string at .debug_str offset as read from BUF.
19348 BUF is assumed to be in a compilation unit described by CU_HEADER.
19349 Return *BYTES_READ_PTR count of bytes read from BUF. */
19350
19351 static const char *
19352 read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19353 const gdb_byte *buf,
19354 const struct comp_unit_head *cu_header,
19355 unsigned int *bytes_read_ptr)
19356 {
19357 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19358
19359 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
19360 }
19361
19362 /* See read.h. */
19363
19364 const char *
19365 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
19366 const struct comp_unit_head *cu_header,
19367 unsigned int *bytes_read_ptr)
19368 {
19369 bfd *abfd = objfile->obfd;
19370 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
19371
19372 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
19373 }
19374
19375 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
19376 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
19377 ADDR_SIZE is the size of addresses from the CU header. */
19378
19379 static CORE_ADDR
19380 read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19381 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19382 int addr_size)
19383 {
19384 struct objfile *objfile = dwarf2_per_objfile->objfile;
19385 bfd *abfd = objfile->obfd;
19386 const gdb_byte *info_ptr;
19387 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
19388
19389 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19390 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
19391 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
19392 objfile_name (objfile));
19393 if (addr_base_or_zero + addr_index * addr_size
19394 >= dwarf2_per_objfile->per_bfd->addr.size)
19395 error (_("DW_FORM_addr_index pointing outside of "
19396 ".debug_addr section [in module %s]"),
19397 objfile_name (objfile));
19398 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
19399 + addr_base_or_zero + addr_index * addr_size);
19400 if (addr_size == 4)
19401 return bfd_get_32 (abfd, info_ptr);
19402 else
19403 return bfd_get_64 (abfd, info_ptr);
19404 }
19405
19406 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19407
19408 static CORE_ADDR
19409 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19410 {
19411 return read_addr_index_1 (cu->per_objfile, addr_index,
19412 cu->addr_base, cu->header.addr_size);
19413 }
19414
19415 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19416
19417 static CORE_ADDR
19418 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
19419 unsigned int *bytes_read)
19420 {
19421 bfd *abfd = cu->per_objfile->objfile->obfd;
19422 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19423
19424 return read_addr_index (cu, addr_index);
19425 }
19426
19427 /* See read.h. */
19428
19429 CORE_ADDR
19430 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19431 dwarf2_per_objfile *dwarf2_per_objfile,
19432 unsigned int addr_index)
19433 {
19434 struct dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
19435 gdb::optional<ULONGEST> addr_base;
19436 int addr_size;
19437
19438 /* We need addr_base and addr_size.
19439 If we don't have PER_CU->cu, we have to get it.
19440 Nasty, but the alternative is storing the needed info in PER_CU,
19441 which at this point doesn't seem justified: it's not clear how frequently
19442 it would get used and it would increase the size of every PER_CU.
19443 Entry points like dwarf2_per_cu_addr_size do a similar thing
19444 so we're not in uncharted territory here.
19445 Alas we need to be a bit more complicated as addr_base is contained
19446 in the DIE.
19447
19448 We don't need to read the entire CU(/TU).
19449 We just need the header and top level die.
19450
19451 IWBN to use the aging mechanism to let us lazily later discard the CU.
19452 For now we skip this optimization. */
19453
19454 if (cu != NULL)
19455 {
19456 addr_base = cu->addr_base;
19457 addr_size = cu->header.addr_size;
19458 }
19459 else
19460 {
19461 cutu_reader reader (per_cu, dwarf2_per_objfile, nullptr, nullptr, false);
19462 addr_base = reader.cu->addr_base;
19463 addr_size = reader.cu->header.addr_size;
19464 }
19465
19466 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19467 addr_size);
19468 }
19469
19470 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19471 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19472 DWO file. */
19473
19474 static const char *
19475 read_str_index (struct dwarf2_cu *cu,
19476 struct dwarf2_section_info *str_section,
19477 struct dwarf2_section_info *str_offsets_section,
19478 ULONGEST str_offsets_base, ULONGEST str_index)
19479 {
19480 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19481 struct objfile *objfile = dwarf2_per_objfile->objfile;
19482 const char *objf_name = objfile_name (objfile);
19483 bfd *abfd = objfile->obfd;
19484 const gdb_byte *info_ptr;
19485 ULONGEST str_offset;
19486 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
19487
19488 str_section->read (objfile);
19489 str_offsets_section->read (objfile);
19490 if (str_section->buffer == NULL)
19491 error (_("%s used without %s section"
19492 " in CU at offset %s [in module %s]"),
19493 form_name, str_section->get_name (),
19494 sect_offset_str (cu->header.sect_off), objf_name);
19495 if (str_offsets_section->buffer == NULL)
19496 error (_("%s used without %s section"
19497 " in CU at offset %s [in module %s]"),
19498 form_name, str_section->get_name (),
19499 sect_offset_str (cu->header.sect_off), objf_name);
19500 info_ptr = (str_offsets_section->buffer
19501 + str_offsets_base
19502 + str_index * cu->header.offset_size);
19503 if (cu->header.offset_size == 4)
19504 str_offset = bfd_get_32 (abfd, info_ptr);
19505 else
19506 str_offset = bfd_get_64 (abfd, info_ptr);
19507 if (str_offset >= str_section->size)
19508 error (_("Offset from %s pointing outside of"
19509 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19510 form_name, sect_offset_str (cu->header.sect_off), objf_name);
19511 return (const char *) (str_section->buffer + str_offset);
19512 }
19513
19514 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19515
19516 static const char *
19517 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19518 {
19519 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19520 ? reader->cu->header.addr_size : 0;
19521 return read_str_index (reader->cu,
19522 &reader->dwo_file->sections.str,
19523 &reader->dwo_file->sections.str_offsets,
19524 str_offsets_base, str_index);
19525 }
19526
19527 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19528
19529 static const char *
19530 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19531 {
19532 struct objfile *objfile = cu->per_objfile->objfile;
19533 const char *objf_name = objfile_name (objfile);
19534 static const char form_name[] = "DW_FORM_GNU_str_index";
19535 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19536
19537 if (!cu->str_offsets_base.has_value ())
19538 error (_("%s used in Fission stub without %s"
19539 " in CU at offset 0x%lx [in module %s]"),
19540 form_name, str_offsets_attr_name,
19541 (long) cu->header.offset_size, objf_name);
19542
19543 return read_str_index (cu,
19544 &cu->per_objfile->per_bfd->str,
19545 &cu->per_objfile->per_bfd->str_offsets,
19546 *cu->str_offsets_base, str_index);
19547 }
19548
19549 /* Return the length of an LEB128 number in BUF. */
19550
19551 static int
19552 leb128_size (const gdb_byte *buf)
19553 {
19554 const gdb_byte *begin = buf;
19555 gdb_byte byte;
19556
19557 while (1)
19558 {
19559 byte = *buf++;
19560 if ((byte & 128) == 0)
19561 return buf - begin;
19562 }
19563 }
19564
19565 static void
19566 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
19567 {
19568 switch (lang)
19569 {
19570 case DW_LANG_C89:
19571 case DW_LANG_C99:
19572 case DW_LANG_C11:
19573 case DW_LANG_C:
19574 case DW_LANG_UPC:
19575 cu->language = language_c;
19576 break;
19577 case DW_LANG_Java:
19578 case DW_LANG_C_plus_plus:
19579 case DW_LANG_C_plus_plus_11:
19580 case DW_LANG_C_plus_plus_14:
19581 cu->language = language_cplus;
19582 break;
19583 case DW_LANG_D:
19584 cu->language = language_d;
19585 break;
19586 case DW_LANG_Fortran77:
19587 case DW_LANG_Fortran90:
19588 case DW_LANG_Fortran95:
19589 case DW_LANG_Fortran03:
19590 case DW_LANG_Fortran08:
19591 cu->language = language_fortran;
19592 break;
19593 case DW_LANG_Go:
19594 cu->language = language_go;
19595 break;
19596 case DW_LANG_Mips_Assembler:
19597 cu->language = language_asm;
19598 break;
19599 case DW_LANG_Ada83:
19600 case DW_LANG_Ada95:
19601 cu->language = language_ada;
19602 break;
19603 case DW_LANG_Modula2:
19604 cu->language = language_m2;
19605 break;
19606 case DW_LANG_Pascal83:
19607 cu->language = language_pascal;
19608 break;
19609 case DW_LANG_ObjC:
19610 cu->language = language_objc;
19611 break;
19612 case DW_LANG_Rust:
19613 case DW_LANG_Rust_old:
19614 cu->language = language_rust;
19615 break;
19616 case DW_LANG_Cobol74:
19617 case DW_LANG_Cobol85:
19618 default:
19619 cu->language = language_minimal;
19620 break;
19621 }
19622 cu->language_defn = language_def (cu->language);
19623 }
19624
19625 /* Return the named attribute or NULL if not there. */
19626
19627 static struct attribute *
19628 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19629 {
19630 for (;;)
19631 {
19632 unsigned int i;
19633 struct attribute *spec = NULL;
19634
19635 for (i = 0; i < die->num_attrs; ++i)
19636 {
19637 if (die->attrs[i].name == name)
19638 return &die->attrs[i];
19639 if (die->attrs[i].name == DW_AT_specification
19640 || die->attrs[i].name == DW_AT_abstract_origin)
19641 spec = &die->attrs[i];
19642 }
19643
19644 if (!spec)
19645 break;
19646
19647 die = follow_die_ref (die, spec, &cu);
19648 }
19649
19650 return NULL;
19651 }
19652
19653 /* Return the string associated with a string-typed attribute, or NULL if it
19654 is either not found or is of an incorrect type. */
19655
19656 static const char *
19657 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19658 {
19659 struct attribute *attr;
19660 const char *str = NULL;
19661
19662 attr = dwarf2_attr (die, name, cu);
19663
19664 if (attr != NULL)
19665 {
19666 str = attr->value_as_string ();
19667 if (str == nullptr)
19668 complaint (_("string type expected for attribute %s for "
19669 "DIE at %s in module %s"),
19670 dwarf_attr_name (name), sect_offset_str (die->sect_off),
19671 objfile_name (cu->per_objfile->objfile));
19672 }
19673
19674 return str;
19675 }
19676
19677 /* Return the dwo name or NULL if not present. If present, it is in either
19678 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
19679 static const char *
19680 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19681 {
19682 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19683 if (dwo_name == nullptr)
19684 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19685 return dwo_name;
19686 }
19687
19688 /* Return non-zero iff the attribute NAME is defined for the given DIE,
19689 and holds a non-zero value. This function should only be used for
19690 DW_FORM_flag or DW_FORM_flag_present attributes. */
19691
19692 static int
19693 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19694 {
19695 struct attribute *attr = dwarf2_attr (die, name, cu);
19696
19697 return (attr && DW_UNSND (attr));
19698 }
19699
19700 static int
19701 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
19702 {
19703 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19704 which value is non-zero. However, we have to be careful with
19705 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19706 (via dwarf2_flag_true_p) follows this attribute. So we may
19707 end up accidently finding a declaration attribute that belongs
19708 to a different DIE referenced by the specification attribute,
19709 even though the given DIE does not have a declaration attribute. */
19710 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19711 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
19712 }
19713
19714 /* Return the die giving the specification for DIE, if there is
19715 one. *SPEC_CU is the CU containing DIE on input, and the CU
19716 containing the return value on output. If there is no
19717 specification, but there is an abstract origin, that is
19718 returned. */
19719
19720 static struct die_info *
19721 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
19722 {
19723 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19724 *spec_cu);
19725
19726 if (spec_attr == NULL)
19727 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19728
19729 if (spec_attr == NULL)
19730 return NULL;
19731 else
19732 return follow_die_ref (die, spec_attr, spec_cu);
19733 }
19734
19735 /* Stub for free_line_header to match void * callback types. */
19736
19737 static void
19738 free_line_header_voidp (void *arg)
19739 {
19740 struct line_header *lh = (struct line_header *) arg;
19741
19742 delete lh;
19743 }
19744
19745 /* A convenience function to find the proper .debug_line section for a CU. */
19746
19747 static struct dwarf2_section_info *
19748 get_debug_line_section (struct dwarf2_cu *cu)
19749 {
19750 struct dwarf2_section_info *section;
19751 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19752
19753 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19754 DWO file. */
19755 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19756 section = &cu->dwo_unit->dwo_file->sections.line;
19757 else if (cu->per_cu->is_dwz)
19758 {
19759 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
19760
19761 section = &dwz->line;
19762 }
19763 else
19764 section = &dwarf2_per_objfile->per_bfd->line;
19765
19766 return section;
19767 }
19768
19769 /* Read the statement program header starting at OFFSET in
19770 .debug_line, or .debug_line.dwo. Return a pointer
19771 to a struct line_header, allocated using xmalloc.
19772 Returns NULL if there is a problem reading the header, e.g., if it
19773 has a version we don't understand.
19774
19775 NOTE: the strings in the include directory and file name tables of
19776 the returned object point into the dwarf line section buffer,
19777 and must not be freed. */
19778
19779 static line_header_up
19780 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
19781 {
19782 struct dwarf2_section_info *section;
19783 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
19784
19785 section = get_debug_line_section (cu);
19786 section->read (dwarf2_per_objfile->objfile);
19787 if (section->buffer == NULL)
19788 {
19789 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19790 complaint (_("missing .debug_line.dwo section"));
19791 else
19792 complaint (_("missing .debug_line section"));
19793 return 0;
19794 }
19795
19796 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19797 dwarf2_per_objfile, section,
19798 &cu->header);
19799 }
19800
19801 /* Subroutine of dwarf_decode_lines to simplify it.
19802 Return the file name of the psymtab for the given file_entry.
19803 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19804 If space for the result is malloc'd, *NAME_HOLDER will be set.
19805 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
19806
19807 static const char *
19808 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
19809 const dwarf2_psymtab *pst,
19810 const char *comp_dir,
19811 gdb::unique_xmalloc_ptr<char> *name_holder)
19812 {
19813 const char *include_name = fe.name;
19814 const char *include_name_to_compare = include_name;
19815 const char *pst_filename;
19816 int file_is_pst;
19817
19818 const char *dir_name = fe.include_dir (lh);
19819
19820 gdb::unique_xmalloc_ptr<char> hold_compare;
19821 if (!IS_ABSOLUTE_PATH (include_name)
19822 && (dir_name != NULL || comp_dir != NULL))
19823 {
19824 /* Avoid creating a duplicate psymtab for PST.
19825 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19826 Before we do the comparison, however, we need to account
19827 for DIR_NAME and COMP_DIR.
19828 First prepend dir_name (if non-NULL). If we still don't
19829 have an absolute path prepend comp_dir (if non-NULL).
19830 However, the directory we record in the include-file's
19831 psymtab does not contain COMP_DIR (to match the
19832 corresponding symtab(s)).
19833
19834 Example:
19835
19836 bash$ cd /tmp
19837 bash$ gcc -g ./hello.c
19838 include_name = "hello.c"
19839 dir_name = "."
19840 DW_AT_comp_dir = comp_dir = "/tmp"
19841 DW_AT_name = "./hello.c"
19842
19843 */
19844
19845 if (dir_name != NULL)
19846 {
19847 name_holder->reset (concat (dir_name, SLASH_STRING,
19848 include_name, (char *) NULL));
19849 include_name = name_holder->get ();
19850 include_name_to_compare = include_name;
19851 }
19852 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19853 {
19854 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19855 include_name, (char *) NULL));
19856 include_name_to_compare = hold_compare.get ();
19857 }
19858 }
19859
19860 pst_filename = pst->filename;
19861 gdb::unique_xmalloc_ptr<char> copied_name;
19862 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19863 {
19864 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19865 pst_filename, (char *) NULL));
19866 pst_filename = copied_name.get ();
19867 }
19868
19869 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
19870
19871 if (file_is_pst)
19872 return NULL;
19873 return include_name;
19874 }
19875
19876 /* State machine to track the state of the line number program. */
19877
19878 class lnp_state_machine
19879 {
19880 public:
19881 /* Initialize a machine state for the start of a line number
19882 program. */
19883 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19884 bool record_lines_p);
19885
19886 file_entry *current_file ()
19887 {
19888 /* lh->file_names is 0-based, but the file name numbers in the
19889 statement program are 1-based. */
19890 return m_line_header->file_name_at (m_file);
19891 }
19892
19893 /* Record the line in the state machine. END_SEQUENCE is true if
19894 we're processing the end of a sequence. */
19895 void record_line (bool end_sequence);
19896
19897 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19898 nop-out rest of the lines in this sequence. */
19899 void check_line_address (struct dwarf2_cu *cu,
19900 const gdb_byte *line_ptr,
19901 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
19902
19903 void handle_set_discriminator (unsigned int discriminator)
19904 {
19905 m_discriminator = discriminator;
19906 m_line_has_non_zero_discriminator |= discriminator != 0;
19907 }
19908
19909 /* Handle DW_LNE_set_address. */
19910 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19911 {
19912 m_op_index = 0;
19913 address += baseaddr;
19914 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19915 }
19916
19917 /* Handle DW_LNS_advance_pc. */
19918 void handle_advance_pc (CORE_ADDR adjust);
19919
19920 /* Handle a special opcode. */
19921 void handle_special_opcode (unsigned char op_code);
19922
19923 /* Handle DW_LNS_advance_line. */
19924 void handle_advance_line (int line_delta)
19925 {
19926 advance_line (line_delta);
19927 }
19928
19929 /* Handle DW_LNS_set_file. */
19930 void handle_set_file (file_name_index file);
19931
19932 /* Handle DW_LNS_negate_stmt. */
19933 void handle_negate_stmt ()
19934 {
19935 m_is_stmt = !m_is_stmt;
19936 }
19937
19938 /* Handle DW_LNS_const_add_pc. */
19939 void handle_const_add_pc ();
19940
19941 /* Handle DW_LNS_fixed_advance_pc. */
19942 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19943 {
19944 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19945 m_op_index = 0;
19946 }
19947
19948 /* Handle DW_LNS_copy. */
19949 void handle_copy ()
19950 {
19951 record_line (false);
19952 m_discriminator = 0;
19953 }
19954
19955 /* Handle DW_LNE_end_sequence. */
19956 void handle_end_sequence ()
19957 {
19958 m_currently_recording_lines = true;
19959 }
19960
19961 private:
19962 /* Advance the line by LINE_DELTA. */
19963 void advance_line (int line_delta)
19964 {
19965 m_line += line_delta;
19966
19967 if (line_delta != 0)
19968 m_line_has_non_zero_discriminator = m_discriminator != 0;
19969 }
19970
19971 struct dwarf2_cu *m_cu;
19972
19973 gdbarch *m_gdbarch;
19974
19975 /* True if we're recording lines.
19976 Otherwise we're building partial symtabs and are just interested in
19977 finding include files mentioned by the line number program. */
19978 bool m_record_lines_p;
19979
19980 /* The line number header. */
19981 line_header *m_line_header;
19982
19983 /* These are part of the standard DWARF line number state machine,
19984 and initialized according to the DWARF spec. */
19985
19986 unsigned char m_op_index = 0;
19987 /* The line table index of the current file. */
19988 file_name_index m_file = 1;
19989 unsigned int m_line = 1;
19990
19991 /* These are initialized in the constructor. */
19992
19993 CORE_ADDR m_address;
19994 bool m_is_stmt;
19995 unsigned int m_discriminator;
19996
19997 /* Additional bits of state we need to track. */
19998
19999 /* The last file that we called dwarf2_start_subfile for.
20000 This is only used for TLLs. */
20001 unsigned int m_last_file = 0;
20002 /* The last file a line number was recorded for. */
20003 struct subfile *m_last_subfile = NULL;
20004
20005 /* When true, record the lines we decode. */
20006 bool m_currently_recording_lines = false;
20007
20008 /* The last line number that was recorded, used to coalesce
20009 consecutive entries for the same line. This can happen, for
20010 example, when discriminators are present. PR 17276. */
20011 unsigned int m_last_line = 0;
20012 bool m_line_has_non_zero_discriminator = false;
20013 };
20014
20015 void
20016 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20017 {
20018 CORE_ADDR addr_adj = (((m_op_index + adjust)
20019 / m_line_header->maximum_ops_per_instruction)
20020 * m_line_header->minimum_instruction_length);
20021 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20022 m_op_index = ((m_op_index + adjust)
20023 % m_line_header->maximum_ops_per_instruction);
20024 }
20025
20026 void
20027 lnp_state_machine::handle_special_opcode (unsigned char op_code)
20028 {
20029 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20030 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20031 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20032 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
20033 / m_line_header->maximum_ops_per_instruction)
20034 * m_line_header->minimum_instruction_length);
20035 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20036 m_op_index = ((m_op_index + adj_opcode_d)
20037 % m_line_header->maximum_ops_per_instruction);
20038
20039 int line_delta = m_line_header->line_base + adj_opcode_r;
20040 advance_line (line_delta);
20041 record_line (false);
20042 m_discriminator = 0;
20043 }
20044
20045 void
20046 lnp_state_machine::handle_set_file (file_name_index file)
20047 {
20048 m_file = file;
20049
20050 const file_entry *fe = current_file ();
20051 if (fe == NULL)
20052 dwarf2_debug_line_missing_file_complaint ();
20053 else if (m_record_lines_p)
20054 {
20055 const char *dir = fe->include_dir (m_line_header);
20056
20057 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20058 m_line_has_non_zero_discriminator = m_discriminator != 0;
20059 dwarf2_start_subfile (m_cu, fe->name, dir);
20060 }
20061 }
20062
20063 void
20064 lnp_state_machine::handle_const_add_pc ()
20065 {
20066 CORE_ADDR adjust
20067 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20068
20069 CORE_ADDR addr_adj
20070 = (((m_op_index + adjust)
20071 / m_line_header->maximum_ops_per_instruction)
20072 * m_line_header->minimum_instruction_length);
20073
20074 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20075 m_op_index = ((m_op_index + adjust)
20076 % m_line_header->maximum_ops_per_instruction);
20077 }
20078
20079 /* Return non-zero if we should add LINE to the line number table.
20080 LINE is the line to add, LAST_LINE is the last line that was added,
20081 LAST_SUBFILE is the subfile for LAST_LINE.
20082 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20083 had a non-zero discriminator.
20084
20085 We have to be careful in the presence of discriminators.
20086 E.g., for this line:
20087
20088 for (i = 0; i < 100000; i++);
20089
20090 clang can emit four line number entries for that one line,
20091 each with a different discriminator.
20092 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20093
20094 However, we want gdb to coalesce all four entries into one.
20095 Otherwise the user could stepi into the middle of the line and
20096 gdb would get confused about whether the pc really was in the
20097 middle of the line.
20098
20099 Things are further complicated by the fact that two consecutive
20100 line number entries for the same line is a heuristic used by gcc
20101 to denote the end of the prologue. So we can't just discard duplicate
20102 entries, we have to be selective about it. The heuristic we use is
20103 that we only collapse consecutive entries for the same line if at least
20104 one of those entries has a non-zero discriminator. PR 17276.
20105
20106 Note: Addresses in the line number state machine can never go backwards
20107 within one sequence, thus this coalescing is ok. */
20108
20109 static int
20110 dwarf_record_line_p (struct dwarf2_cu *cu,
20111 unsigned int line, unsigned int last_line,
20112 int line_has_non_zero_discriminator,
20113 struct subfile *last_subfile)
20114 {
20115 if (cu->get_builder ()->get_current_subfile () != last_subfile)
20116 return 1;
20117 if (line != last_line)
20118 return 1;
20119 /* Same line for the same file that we've seen already.
20120 As a last check, for pr 17276, only record the line if the line
20121 has never had a non-zero discriminator. */
20122 if (!line_has_non_zero_discriminator)
20123 return 1;
20124 return 0;
20125 }
20126
20127 /* Use the CU's builder to record line number LINE beginning at
20128 address ADDRESS in the line table of subfile SUBFILE. */
20129
20130 static void
20131 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20132 unsigned int line, CORE_ADDR address, bool is_stmt,
20133 struct dwarf2_cu *cu)
20134 {
20135 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20136
20137 if (dwarf_line_debug)
20138 {
20139 fprintf_unfiltered (gdb_stdlog,
20140 "Recording line %u, file %s, address %s\n",
20141 line, lbasename (subfile->name),
20142 paddress (gdbarch, address));
20143 }
20144
20145 if (cu != nullptr)
20146 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
20147 }
20148
20149 /* Subroutine of dwarf_decode_lines_1 to simplify it.
20150 Mark the end of a set of line number records.
20151 The arguments are the same as for dwarf_record_line_1.
20152 If SUBFILE is NULL the request is ignored. */
20153
20154 static void
20155 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20156 CORE_ADDR address, struct dwarf2_cu *cu)
20157 {
20158 if (subfile == NULL)
20159 return;
20160
20161 if (dwarf_line_debug)
20162 {
20163 fprintf_unfiltered (gdb_stdlog,
20164 "Finishing current line, file %s, address %s\n",
20165 lbasename (subfile->name),
20166 paddress (gdbarch, address));
20167 }
20168
20169 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
20170 }
20171
20172 void
20173 lnp_state_machine::record_line (bool end_sequence)
20174 {
20175 if (dwarf_line_debug)
20176 {
20177 fprintf_unfiltered (gdb_stdlog,
20178 "Processing actual line %u: file %u,"
20179 " address %s, is_stmt %u, discrim %u%s\n",
20180 m_line, m_file,
20181 paddress (m_gdbarch, m_address),
20182 m_is_stmt, m_discriminator,
20183 (end_sequence ? "\t(end sequence)" : ""));
20184 }
20185
20186 file_entry *fe = current_file ();
20187
20188 if (fe == NULL)
20189 dwarf2_debug_line_missing_file_complaint ();
20190 /* For now we ignore lines not starting on an instruction boundary.
20191 But not when processing end_sequence for compatibility with the
20192 previous version of the code. */
20193 else if (m_op_index == 0 || end_sequence)
20194 {
20195 fe->included_p = 1;
20196 if (m_record_lines_p)
20197 {
20198 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
20199 || end_sequence)
20200 {
20201 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20202 m_currently_recording_lines ? m_cu : nullptr);
20203 }
20204
20205 if (!end_sequence)
20206 {
20207 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20208
20209 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
20210 m_line_has_non_zero_discriminator,
20211 m_last_subfile))
20212 {
20213 buildsym_compunit *builder = m_cu->get_builder ();
20214 dwarf_record_line_1 (m_gdbarch,
20215 builder->get_current_subfile (),
20216 m_line, m_address, is_stmt,
20217 m_currently_recording_lines ? m_cu : nullptr);
20218 }
20219 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
20220 m_last_line = m_line;
20221 }
20222 }
20223 }
20224 }
20225
20226 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20227 line_header *lh, bool record_lines_p)
20228 {
20229 m_cu = cu;
20230 m_gdbarch = arch;
20231 m_record_lines_p = record_lines_p;
20232 m_line_header = lh;
20233
20234 m_currently_recording_lines = true;
20235
20236 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20237 was a line entry for it so that the backend has a chance to adjust it
20238 and also record it in case it needs it. This is currently used by MIPS
20239 code, cf. `mips_adjust_dwarf2_line'. */
20240 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20241 m_is_stmt = lh->default_is_stmt;
20242 m_discriminator = 0;
20243 }
20244
20245 void
20246 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20247 const gdb_byte *line_ptr,
20248 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
20249 {
20250 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20251 the pc range of the CU. However, we restrict the test to only ADDRESS
20252 values of zero to preserve GDB's previous behaviour which is to handle
20253 the specific case of a function being GC'd by the linker. */
20254
20255 if (address == 0 && address < unrelocated_lowpc)
20256 {
20257 /* This line table is for a function which has been
20258 GCd by the linker. Ignore it. PR gdb/12528 */
20259
20260 struct objfile *objfile = cu->per_objfile->objfile;
20261 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20262
20263 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
20264 line_offset, objfile_name (objfile));
20265 m_currently_recording_lines = false;
20266 /* Note: m_currently_recording_lines is left as false until we see
20267 DW_LNE_end_sequence. */
20268 }
20269 }
20270
20271 /* Subroutine of dwarf_decode_lines to simplify it.
20272 Process the line number information in LH.
20273 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20274 program in order to set included_p for every referenced header. */
20275
20276 static void
20277 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20278 const int decode_for_pst_p, CORE_ADDR lowpc)
20279 {
20280 const gdb_byte *line_ptr, *extended_end;
20281 const gdb_byte *line_end;
20282 unsigned int bytes_read, extended_len;
20283 unsigned char op_code, extended_op;
20284 CORE_ADDR baseaddr;
20285 struct objfile *objfile = cu->per_objfile->objfile;
20286 bfd *abfd = objfile->obfd;
20287 struct gdbarch *gdbarch = objfile->arch ();
20288 /* True if we're recording line info (as opposed to building partial
20289 symtabs and just interested in finding include files mentioned by
20290 the line number program). */
20291 bool record_lines_p = !decode_for_pst_p;
20292
20293 baseaddr = objfile->text_section_offset ();
20294
20295 line_ptr = lh->statement_program_start;
20296 line_end = lh->statement_program_end;
20297
20298 /* Read the statement sequences until there's nothing left. */
20299 while (line_ptr < line_end)
20300 {
20301 /* The DWARF line number program state machine. Reset the state
20302 machine at the start of each sequence. */
20303 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
20304 bool end_sequence = false;
20305
20306 if (record_lines_p)
20307 {
20308 /* Start a subfile for the current file of the state
20309 machine. */
20310 const file_entry *fe = state_machine.current_file ();
20311
20312 if (fe != NULL)
20313 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
20314 }
20315
20316 /* Decode the table. */
20317 while (line_ptr < line_end && !end_sequence)
20318 {
20319 op_code = read_1_byte (abfd, line_ptr);
20320 line_ptr += 1;
20321
20322 if (op_code >= lh->opcode_base)
20323 {
20324 /* Special opcode. */
20325 state_machine.handle_special_opcode (op_code);
20326 }
20327 else switch (op_code)
20328 {
20329 case DW_LNS_extended_op:
20330 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20331 &bytes_read);
20332 line_ptr += bytes_read;
20333 extended_end = line_ptr + extended_len;
20334 extended_op = read_1_byte (abfd, line_ptr);
20335 line_ptr += 1;
20336 switch (extended_op)
20337 {
20338 case DW_LNE_end_sequence:
20339 state_machine.handle_end_sequence ();
20340 end_sequence = true;
20341 break;
20342 case DW_LNE_set_address:
20343 {
20344 CORE_ADDR address
20345 = cu->header.read_address (abfd, line_ptr, &bytes_read);
20346 line_ptr += bytes_read;
20347
20348 state_machine.check_line_address (cu, line_ptr,
20349 lowpc - baseaddr, address);
20350 state_machine.handle_set_address (baseaddr, address);
20351 }
20352 break;
20353 case DW_LNE_define_file:
20354 {
20355 const char *cur_file;
20356 unsigned int mod_time, length;
20357 dir_index dindex;
20358
20359 cur_file = read_direct_string (abfd, line_ptr,
20360 &bytes_read);
20361 line_ptr += bytes_read;
20362 dindex = (dir_index)
20363 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20364 line_ptr += bytes_read;
20365 mod_time =
20366 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20367 line_ptr += bytes_read;
20368 length =
20369 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20370 line_ptr += bytes_read;
20371 lh->add_file_name (cur_file, dindex, mod_time, length);
20372 }
20373 break;
20374 case DW_LNE_set_discriminator:
20375 {
20376 /* The discriminator is not interesting to the
20377 debugger; just ignore it. We still need to
20378 check its value though:
20379 if there are consecutive entries for the same
20380 (non-prologue) line we want to coalesce them.
20381 PR 17276. */
20382 unsigned int discr
20383 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20384 line_ptr += bytes_read;
20385
20386 state_machine.handle_set_discriminator (discr);
20387 }
20388 break;
20389 default:
20390 complaint (_("mangled .debug_line section"));
20391 return;
20392 }
20393 /* Make sure that we parsed the extended op correctly. If e.g.
20394 we expected a different address size than the producer used,
20395 we may have read the wrong number of bytes. */
20396 if (line_ptr != extended_end)
20397 {
20398 complaint (_("mangled .debug_line section"));
20399 return;
20400 }
20401 break;
20402 case DW_LNS_copy:
20403 state_machine.handle_copy ();
20404 break;
20405 case DW_LNS_advance_pc:
20406 {
20407 CORE_ADDR adjust
20408 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20409 line_ptr += bytes_read;
20410
20411 state_machine.handle_advance_pc (adjust);
20412 }
20413 break;
20414 case DW_LNS_advance_line:
20415 {
20416 int line_delta
20417 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
20418 line_ptr += bytes_read;
20419
20420 state_machine.handle_advance_line (line_delta);
20421 }
20422 break;
20423 case DW_LNS_set_file:
20424 {
20425 file_name_index file
20426 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20427 &bytes_read);
20428 line_ptr += bytes_read;
20429
20430 state_machine.handle_set_file (file);
20431 }
20432 break;
20433 case DW_LNS_set_column:
20434 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20435 line_ptr += bytes_read;
20436 break;
20437 case DW_LNS_negate_stmt:
20438 state_machine.handle_negate_stmt ();
20439 break;
20440 case DW_LNS_set_basic_block:
20441 break;
20442 /* Add to the address register of the state machine the
20443 address increment value corresponding to special opcode
20444 255. I.e., this value is scaled by the minimum
20445 instruction length since special opcode 255 would have
20446 scaled the increment. */
20447 case DW_LNS_const_add_pc:
20448 state_machine.handle_const_add_pc ();
20449 break;
20450 case DW_LNS_fixed_advance_pc:
20451 {
20452 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
20453 line_ptr += 2;
20454
20455 state_machine.handle_fixed_advance_pc (addr_adj);
20456 }
20457 break;
20458 default:
20459 {
20460 /* Unknown standard opcode, ignore it. */
20461 int i;
20462
20463 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
20464 {
20465 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20466 line_ptr += bytes_read;
20467 }
20468 }
20469 }
20470 }
20471
20472 if (!end_sequence)
20473 dwarf2_debug_line_missing_end_sequence_complaint ();
20474
20475 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20476 in which case we still finish recording the last line). */
20477 state_machine.record_line (true);
20478 }
20479 }
20480
20481 /* Decode the Line Number Program (LNP) for the given line_header
20482 structure and CU. The actual information extracted and the type
20483 of structures created from the LNP depends on the value of PST.
20484
20485 1. If PST is NULL, then this procedure uses the data from the program
20486 to create all necessary symbol tables, and their linetables.
20487
20488 2. If PST is not NULL, this procedure reads the program to determine
20489 the list of files included by the unit represented by PST, and
20490 builds all the associated partial symbol tables.
20491
20492 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20493 It is used for relative paths in the line table.
20494 NOTE: When processing partial symtabs (pst != NULL),
20495 comp_dir == pst->dirname.
20496
20497 NOTE: It is important that psymtabs have the same file name (via strcmp)
20498 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20499 symtab we don't use it in the name of the psymtabs we create.
20500 E.g. expand_line_sal requires this when finding psymtabs to expand.
20501 A good testcase for this is mb-inline.exp.
20502
20503 LOWPC is the lowest address in CU (or 0 if not known).
20504
20505 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20506 for its PC<->lines mapping information. Otherwise only the filename
20507 table is read in. */
20508
20509 static void
20510 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
20511 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
20512 CORE_ADDR lowpc, int decode_mapping)
20513 {
20514 struct objfile *objfile = cu->per_objfile->objfile;
20515 const int decode_for_pst_p = (pst != NULL);
20516
20517 if (decode_mapping)
20518 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
20519
20520 if (decode_for_pst_p)
20521 {
20522 /* Now that we're done scanning the Line Header Program, we can
20523 create the psymtab of each included file. */
20524 for (auto &file_entry : lh->file_names ())
20525 if (file_entry.included_p == 1)
20526 {
20527 gdb::unique_xmalloc_ptr<char> name_holder;
20528 const char *include_name =
20529 psymtab_include_file_name (lh, file_entry, pst,
20530 comp_dir, &name_holder);
20531 if (include_name != NULL)
20532 dwarf2_create_include_psymtab (include_name, pst, objfile);
20533 }
20534 }
20535 else
20536 {
20537 /* Make sure a symtab is created for every file, even files
20538 which contain only variables (i.e. no code with associated
20539 line numbers). */
20540 buildsym_compunit *builder = cu->get_builder ();
20541 struct compunit_symtab *cust = builder->get_compunit_symtab ();
20542
20543 for (auto &fe : lh->file_names ())
20544 {
20545 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
20546 if (builder->get_current_subfile ()->symtab == NULL)
20547 {
20548 builder->get_current_subfile ()->symtab
20549 = allocate_symtab (cust,
20550 builder->get_current_subfile ()->name);
20551 }
20552 fe.symtab = builder->get_current_subfile ()->symtab;
20553 }
20554 }
20555 }
20556
20557 /* Start a subfile for DWARF. FILENAME is the name of the file and
20558 DIRNAME the name of the source directory which contains FILENAME
20559 or NULL if not known.
20560 This routine tries to keep line numbers from identical absolute and
20561 relative file names in a common subfile.
20562
20563 Using the `list' example from the GDB testsuite, which resides in
20564 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20565 of /srcdir/list0.c yields the following debugging information for list0.c:
20566
20567 DW_AT_name: /srcdir/list0.c
20568 DW_AT_comp_dir: /compdir
20569 files.files[0].name: list0.h
20570 files.files[0].dir: /srcdir
20571 files.files[1].name: list0.c
20572 files.files[1].dir: /srcdir
20573
20574 The line number information for list0.c has to end up in a single
20575 subfile, so that `break /srcdir/list0.c:1' works as expected.
20576 start_subfile will ensure that this happens provided that we pass the
20577 concatenation of files.files[1].dir and files.files[1].name as the
20578 subfile's name. */
20579
20580 static void
20581 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20582 const char *dirname)
20583 {
20584 gdb::unique_xmalloc_ptr<char> copy;
20585
20586 /* In order not to lose the line information directory,
20587 we concatenate it to the filename when it makes sense.
20588 Note that the Dwarf3 standard says (speaking of filenames in line
20589 information): ``The directory index is ignored for file names
20590 that represent full path names''. Thus ignoring dirname in the
20591 `else' branch below isn't an issue. */
20592
20593 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
20594 {
20595 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20596 filename = copy.get ();
20597 }
20598
20599 cu->get_builder ()->start_subfile (filename);
20600 }
20601
20602 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20603 buildsym_compunit constructor. */
20604
20605 struct compunit_symtab *
20606 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20607 CORE_ADDR low_pc)
20608 {
20609 gdb_assert (m_builder == nullptr);
20610
20611 m_builder.reset (new struct buildsym_compunit
20612 (this->per_objfile->objfile,
20613 name, comp_dir, language, low_pc));
20614
20615 list_in_scope = get_builder ()->get_file_symbols ();
20616
20617 get_builder ()->record_debugformat ("DWARF 2");
20618 get_builder ()->record_producer (producer);
20619
20620 processing_has_namespace_info = false;
20621
20622 return get_builder ()->get_compunit_symtab ();
20623 }
20624
20625 static void
20626 var_decode_location (struct attribute *attr, struct symbol *sym,
20627 struct dwarf2_cu *cu)
20628 {
20629 struct objfile *objfile = cu->per_objfile->objfile;
20630 struct comp_unit_head *cu_header = &cu->header;
20631
20632 /* NOTE drow/2003-01-30: There used to be a comment and some special
20633 code here to turn a symbol with DW_AT_external and a
20634 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20635 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20636 with some versions of binutils) where shared libraries could have
20637 relocations against symbols in their debug information - the
20638 minimal symbol would have the right address, but the debug info
20639 would not. It's no longer necessary, because we will explicitly
20640 apply relocations when we read in the debug information now. */
20641
20642 /* A DW_AT_location attribute with no contents indicates that a
20643 variable has been optimized away. */
20644 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
20645 {
20646 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20647 return;
20648 }
20649
20650 /* Handle one degenerate form of location expression specially, to
20651 preserve GDB's previous behavior when section offsets are
20652 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20653 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
20654
20655 if (attr->form_is_block ()
20656 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20657 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
20658 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20659 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
20660 && (DW_BLOCK (attr)->size
20661 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
20662 {
20663 unsigned int dummy;
20664
20665 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
20666 SET_SYMBOL_VALUE_ADDRESS
20667 (sym, cu->header.read_address (objfile->obfd,
20668 DW_BLOCK (attr)->data + 1,
20669 &dummy));
20670 else
20671 SET_SYMBOL_VALUE_ADDRESS
20672 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20673 &dummy));
20674 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
20675 fixup_symbol_section (sym, objfile);
20676 SET_SYMBOL_VALUE_ADDRESS
20677 (sym,
20678 SYMBOL_VALUE_ADDRESS (sym)
20679 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
20680 return;
20681 }
20682
20683 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20684 expression evaluator, and use LOC_COMPUTED only when necessary
20685 (i.e. when the value of a register or memory location is
20686 referenced, or a thread-local block, etc.). Then again, it might
20687 not be worthwhile. I'm assuming that it isn't unless performance
20688 or memory numbers show me otherwise. */
20689
20690 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
20691
20692 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
20693 cu->has_loclist = true;
20694 }
20695
20696 /* Given a pointer to a DWARF information entry, figure out if we need
20697 to make a symbol table entry for it, and if so, create a new entry
20698 and return a pointer to it.
20699 If TYPE is NULL, determine symbol type from the die, otherwise
20700 used the passed type.
20701 If SPACE is not NULL, use it to hold the new symbol. If it is
20702 NULL, allocate a new symbol on the objfile's obstack. */
20703
20704 static struct symbol *
20705 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20706 struct symbol *space)
20707 {
20708 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20709 struct objfile *objfile = dwarf2_per_objfile->objfile;
20710 struct gdbarch *gdbarch = objfile->arch ();
20711 struct symbol *sym = NULL;
20712 const char *name;
20713 struct attribute *attr = NULL;
20714 struct attribute *attr2 = NULL;
20715 CORE_ADDR baseaddr;
20716 struct pending **list_to_add = NULL;
20717
20718 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
20719
20720 baseaddr = objfile->text_section_offset ();
20721
20722 name = dwarf2_name (die, cu);
20723 if (name)
20724 {
20725 int suppress_add = 0;
20726
20727 if (space)
20728 sym = space;
20729 else
20730 sym = new (&objfile->objfile_obstack) symbol;
20731 OBJSTAT (objfile, n_syms++);
20732
20733 /* Cache this symbol's name and the name's demangled form (if any). */
20734 sym->set_language (cu->language, &objfile->objfile_obstack);
20735 /* Fortran does not have mangling standard and the mangling does differ
20736 between gfortran, iFort etc. */
20737 const char *physname
20738 = (cu->language == language_fortran
20739 ? dwarf2_full_name (name, die, cu)
20740 : dwarf2_physname (name, die, cu));
20741 const char *linkagename = dw2_linkage_name (die, cu);
20742
20743 if (linkagename == nullptr || cu->language == language_ada)
20744 sym->set_linkage_name (physname);
20745 else
20746 {
20747 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20748 sym->set_linkage_name (linkagename);
20749 }
20750
20751 /* Default assumptions.
20752 Use the passed type or decode it from the die. */
20753 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
20754 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
20755 if (type != NULL)
20756 SYMBOL_TYPE (sym) = type;
20757 else
20758 SYMBOL_TYPE (sym) = die_type (die, cu);
20759 attr = dwarf2_attr (die,
20760 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20761 cu);
20762 if (attr != nullptr)
20763 {
20764 SYMBOL_LINE (sym) = DW_UNSND (attr);
20765 }
20766
20767 attr = dwarf2_attr (die,
20768 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20769 cu);
20770 if (attr != nullptr)
20771 {
20772 file_name_index file_index = (file_name_index) DW_UNSND (attr);
20773 struct file_entry *fe;
20774
20775 if (cu->line_header != NULL)
20776 fe = cu->line_header->file_name_at (file_index);
20777 else
20778 fe = NULL;
20779
20780 if (fe == NULL)
20781 complaint (_("file index out of range"));
20782 else
20783 symbol_set_symtab (sym, fe->symtab);
20784 }
20785
20786 switch (die->tag)
20787 {
20788 case DW_TAG_label:
20789 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
20790 if (attr != nullptr)
20791 {
20792 CORE_ADDR addr;
20793
20794 addr = attr->value_as_address ();
20795 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
20796 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
20797 }
20798 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20799 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
20800 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
20801 add_symbol_to_list (sym, cu->list_in_scope);
20802 break;
20803 case DW_TAG_subprogram:
20804 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20805 finish_block. */
20806 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20807 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20808 if ((attr2 && (DW_UNSND (attr2) != 0))
20809 || cu->language == language_ada
20810 || cu->language == language_fortran)
20811 {
20812 /* Subprograms marked external are stored as a global symbol.
20813 Ada and Fortran subprograms, whether marked external or
20814 not, are always stored as a global symbol, because we want
20815 to be able to access them globally. For instance, we want
20816 to be able to break on a nested subprogram without having
20817 to specify the context. */
20818 list_to_add = cu->get_builder ()->get_global_symbols ();
20819 }
20820 else
20821 {
20822 list_to_add = cu->list_in_scope;
20823 }
20824 break;
20825 case DW_TAG_inlined_subroutine:
20826 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20827 finish_block. */
20828 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
20829 SYMBOL_INLINED (sym) = 1;
20830 list_to_add = cu->list_in_scope;
20831 break;
20832 case DW_TAG_template_value_param:
20833 suppress_add = 1;
20834 /* Fall through. */
20835 case DW_TAG_constant:
20836 case DW_TAG_variable:
20837 case DW_TAG_member:
20838 /* Compilation with minimal debug info may result in
20839 variables with missing type entries. Change the
20840 misleading `void' type to something sensible. */
20841 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
20842 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
20843
20844 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20845 /* In the case of DW_TAG_member, we should only be called for
20846 static const members. */
20847 if (die->tag == DW_TAG_member)
20848 {
20849 /* dwarf2_add_field uses die_is_declaration,
20850 so we do the same. */
20851 gdb_assert (die_is_declaration (die, cu));
20852 gdb_assert (attr);
20853 }
20854 if (attr != nullptr)
20855 {
20856 dwarf2_const_value (attr, sym, cu);
20857 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20858 if (!suppress_add)
20859 {
20860 if (attr2 && (DW_UNSND (attr2) != 0))
20861 list_to_add = cu->get_builder ()->get_global_symbols ();
20862 else
20863 list_to_add = cu->list_in_scope;
20864 }
20865 break;
20866 }
20867 attr = dwarf2_attr (die, DW_AT_location, cu);
20868 if (attr != nullptr)
20869 {
20870 var_decode_location (attr, sym, cu);
20871 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20872
20873 /* Fortran explicitly imports any global symbols to the local
20874 scope by DW_TAG_common_block. */
20875 if (cu->language == language_fortran && die->parent
20876 && die->parent->tag == DW_TAG_common_block)
20877 attr2 = NULL;
20878
20879 if (SYMBOL_CLASS (sym) == LOC_STATIC
20880 && SYMBOL_VALUE_ADDRESS (sym) == 0
20881 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
20882 {
20883 /* When a static variable is eliminated by the linker,
20884 the corresponding debug information is not stripped
20885 out, but the variable address is set to null;
20886 do not add such variables into symbol table. */
20887 }
20888 else if (attr2 && (DW_UNSND (attr2) != 0))
20889 {
20890 if (SYMBOL_CLASS (sym) == LOC_STATIC
20891 && (objfile->flags & OBJF_MAINLINE) == 0
20892 && dwarf2_per_objfile->per_bfd->can_copy)
20893 {
20894 /* A global static variable might be subject to
20895 copy relocation. We first check for a local
20896 minsym, though, because maybe the symbol was
20897 marked hidden, in which case this would not
20898 apply. */
20899 bound_minimal_symbol found
20900 = (lookup_minimal_symbol_linkage
20901 (sym->linkage_name (), objfile));
20902 if (found.minsym != nullptr)
20903 sym->maybe_copied = 1;
20904 }
20905
20906 /* A variable with DW_AT_external is never static,
20907 but it may be block-scoped. */
20908 list_to_add
20909 = ((cu->list_in_scope
20910 == cu->get_builder ()->get_file_symbols ())
20911 ? cu->get_builder ()->get_global_symbols ()
20912 : cu->list_in_scope);
20913 }
20914 else
20915 list_to_add = cu->list_in_scope;
20916 }
20917 else
20918 {
20919 /* We do not know the address of this symbol.
20920 If it is an external symbol and we have type information
20921 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20922 The address of the variable will then be determined from
20923 the minimal symbol table whenever the variable is
20924 referenced. */
20925 attr2 = dwarf2_attr (die, DW_AT_external, cu);
20926
20927 /* Fortran explicitly imports any global symbols to the local
20928 scope by DW_TAG_common_block. */
20929 if (cu->language == language_fortran && die->parent
20930 && die->parent->tag == DW_TAG_common_block)
20931 {
20932 /* SYMBOL_CLASS doesn't matter here because
20933 read_common_block is going to reset it. */
20934 if (!suppress_add)
20935 list_to_add = cu->list_in_scope;
20936 }
20937 else if (attr2 && (DW_UNSND (attr2) != 0)
20938 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
20939 {
20940 /* A variable with DW_AT_external is never static, but it
20941 may be block-scoped. */
20942 list_to_add
20943 = ((cu->list_in_scope
20944 == cu->get_builder ()->get_file_symbols ())
20945 ? cu->get_builder ()->get_global_symbols ()
20946 : cu->list_in_scope);
20947
20948 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
20949 }
20950 else if (!die_is_declaration (die, cu))
20951 {
20952 /* Use the default LOC_OPTIMIZED_OUT class. */
20953 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
20954 if (!suppress_add)
20955 list_to_add = cu->list_in_scope;
20956 }
20957 }
20958 break;
20959 case DW_TAG_formal_parameter:
20960 {
20961 /* If we are inside a function, mark this as an argument. If
20962 not, we might be looking at an argument to an inlined function
20963 when we do not have enough information to show inlined frames;
20964 pretend it's a local variable in that case so that the user can
20965 still see it. */
20966 struct context_stack *curr
20967 = cu->get_builder ()->get_current_context_stack ();
20968 if (curr != nullptr && curr->name != nullptr)
20969 SYMBOL_IS_ARGUMENT (sym) = 1;
20970 attr = dwarf2_attr (die, DW_AT_location, cu);
20971 if (attr != nullptr)
20972 {
20973 var_decode_location (attr, sym, cu);
20974 }
20975 attr = dwarf2_attr (die, DW_AT_const_value, cu);
20976 if (attr != nullptr)
20977 {
20978 dwarf2_const_value (attr, sym, cu);
20979 }
20980
20981 list_to_add = cu->list_in_scope;
20982 }
20983 break;
20984 case DW_TAG_unspecified_parameters:
20985 /* From varargs functions; gdb doesn't seem to have any
20986 interest in this information, so just ignore it for now.
20987 (FIXME?) */
20988 break;
20989 case DW_TAG_template_type_param:
20990 suppress_add = 1;
20991 /* Fall through. */
20992 case DW_TAG_class_type:
20993 case DW_TAG_interface_type:
20994 case DW_TAG_structure_type:
20995 case DW_TAG_union_type:
20996 case DW_TAG_set_type:
20997 case DW_TAG_enumeration_type:
20998 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20999 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
21000
21001 {
21002 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
21003 really ever be static objects: otherwise, if you try
21004 to, say, break of a class's method and you're in a file
21005 which doesn't mention that class, it won't work unless
21006 the check for all static symbols in lookup_symbol_aux
21007 saves you. See the OtherFileClass tests in
21008 gdb.c++/namespace.exp. */
21009
21010 if (!suppress_add)
21011 {
21012 buildsym_compunit *builder = cu->get_builder ();
21013 list_to_add
21014 = (cu->list_in_scope == builder->get_file_symbols ()
21015 && cu->language == language_cplus
21016 ? builder->get_global_symbols ()
21017 : cu->list_in_scope);
21018
21019 /* The semantics of C++ state that "struct foo {
21020 ... }" also defines a typedef for "foo". */
21021 if (cu->language == language_cplus
21022 || cu->language == language_ada
21023 || cu->language == language_d
21024 || cu->language == language_rust)
21025 {
21026 /* The symbol's name is already allocated along
21027 with this objfile, so we don't need to
21028 duplicate it for the type. */
21029 if (SYMBOL_TYPE (sym)->name () == 0)
21030 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
21031 }
21032 }
21033 }
21034 break;
21035 case DW_TAG_typedef:
21036 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21037 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21038 list_to_add = cu->list_in_scope;
21039 break;
21040 case DW_TAG_base_type:
21041 case DW_TAG_subrange_type:
21042 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21043 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21044 list_to_add = cu->list_in_scope;
21045 break;
21046 case DW_TAG_enumerator:
21047 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21048 if (attr != nullptr)
21049 {
21050 dwarf2_const_value (attr, sym, cu);
21051 }
21052 {
21053 /* NOTE: carlton/2003-11-10: See comment above in the
21054 DW_TAG_class_type, etc. block. */
21055
21056 list_to_add
21057 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
21058 && cu->language == language_cplus
21059 ? cu->get_builder ()->get_global_symbols ()
21060 : cu->list_in_scope);
21061 }
21062 break;
21063 case DW_TAG_imported_declaration:
21064 case DW_TAG_namespace:
21065 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21066 list_to_add = cu->get_builder ()->get_global_symbols ();
21067 break;
21068 case DW_TAG_module:
21069 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21070 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21071 list_to_add = cu->get_builder ()->get_global_symbols ();
21072 break;
21073 case DW_TAG_common_block:
21074 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
21075 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21076 add_symbol_to_list (sym, cu->list_in_scope);
21077 break;
21078 default:
21079 /* Not a tag we recognize. Hopefully we aren't processing
21080 trash data, but since we must specifically ignore things
21081 we don't recognize, there is nothing else we should do at
21082 this point. */
21083 complaint (_("unsupported tag: '%s'"),
21084 dwarf_tag_name (die->tag));
21085 break;
21086 }
21087
21088 if (suppress_add)
21089 {
21090 sym->hash_next = objfile->template_symbols;
21091 objfile->template_symbols = sym;
21092 list_to_add = NULL;
21093 }
21094
21095 if (list_to_add != NULL)
21096 add_symbol_to_list (sym, list_to_add);
21097
21098 /* For the benefit of old versions of GCC, check for anonymous
21099 namespaces based on the demangled name. */
21100 if (!cu->processing_has_namespace_info
21101 && cu->language == language_cplus)
21102 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
21103 }
21104 return (sym);
21105 }
21106
21107 /* Given an attr with a DW_FORM_dataN value in host byte order,
21108 zero-extend it as appropriate for the symbol's type. The DWARF
21109 standard (v4) is not entirely clear about the meaning of using
21110 DW_FORM_dataN for a constant with a signed type, where the type is
21111 wider than the data. The conclusion of a discussion on the DWARF
21112 list was that this is unspecified. We choose to always zero-extend
21113 because that is the interpretation long in use by GCC. */
21114
21115 static gdb_byte *
21116 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
21117 struct dwarf2_cu *cu, LONGEST *value, int bits)
21118 {
21119 struct objfile *objfile = cu->per_objfile->objfile;
21120 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21121 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
21122 LONGEST l = DW_UNSND (attr);
21123
21124 if (bits < sizeof (*value) * 8)
21125 {
21126 l &= ((LONGEST) 1 << bits) - 1;
21127 *value = l;
21128 }
21129 else if (bits == sizeof (*value) * 8)
21130 *value = l;
21131 else
21132 {
21133 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
21134 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21135 return bytes;
21136 }
21137
21138 return NULL;
21139 }
21140
21141 /* Read a constant value from an attribute. Either set *VALUE, or if
21142 the value does not fit in *VALUE, set *BYTES - either already
21143 allocated on the objfile obstack, or newly allocated on OBSTACK,
21144 or, set *BATON, if we translated the constant to a location
21145 expression. */
21146
21147 static void
21148 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
21149 const char *name, struct obstack *obstack,
21150 struct dwarf2_cu *cu,
21151 LONGEST *value, const gdb_byte **bytes,
21152 struct dwarf2_locexpr_baton **baton)
21153 {
21154 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21155 struct objfile *objfile = per_objfile->objfile;
21156 struct comp_unit_head *cu_header = &cu->header;
21157 struct dwarf_block *blk;
21158 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21159 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21160
21161 *value = 0;
21162 *bytes = NULL;
21163 *baton = NULL;
21164
21165 switch (attr->form)
21166 {
21167 case DW_FORM_addr:
21168 case DW_FORM_addrx:
21169 case DW_FORM_GNU_addr_index:
21170 {
21171 gdb_byte *data;
21172
21173 if (TYPE_LENGTH (type) != cu_header->addr_size)
21174 dwarf2_const_value_length_mismatch_complaint (name,
21175 cu_header->addr_size,
21176 TYPE_LENGTH (type));
21177 /* Symbols of this form are reasonably rare, so we just
21178 piggyback on the existing location code rather than writing
21179 a new implementation of symbol_computed_ops. */
21180 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
21181 (*baton)->per_objfile = per_objfile;
21182 (*baton)->per_cu = cu->per_cu;
21183 gdb_assert ((*baton)->per_cu);
21184
21185 (*baton)->size = 2 + cu_header->addr_size;
21186 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
21187 (*baton)->data = data;
21188
21189 data[0] = DW_OP_addr;
21190 store_unsigned_integer (&data[1], cu_header->addr_size,
21191 byte_order, DW_ADDR (attr));
21192 data[cu_header->addr_size + 1] = DW_OP_stack_value;
21193 }
21194 break;
21195 case DW_FORM_string:
21196 case DW_FORM_strp:
21197 case DW_FORM_strx:
21198 case DW_FORM_GNU_str_index:
21199 case DW_FORM_GNU_strp_alt:
21200 /* DW_STRING is already allocated on the objfile obstack, point
21201 directly to it. */
21202 *bytes = (const gdb_byte *) DW_STRING (attr);
21203 break;
21204 case DW_FORM_block1:
21205 case DW_FORM_block2:
21206 case DW_FORM_block4:
21207 case DW_FORM_block:
21208 case DW_FORM_exprloc:
21209 case DW_FORM_data16:
21210 blk = DW_BLOCK (attr);
21211 if (TYPE_LENGTH (type) != blk->size)
21212 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21213 TYPE_LENGTH (type));
21214 *bytes = blk->data;
21215 break;
21216
21217 /* The DW_AT_const_value attributes are supposed to carry the
21218 symbol's value "represented as it would be on the target
21219 architecture." By the time we get here, it's already been
21220 converted to host endianness, so we just need to sign- or
21221 zero-extend it as appropriate. */
21222 case DW_FORM_data1:
21223 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
21224 break;
21225 case DW_FORM_data2:
21226 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
21227 break;
21228 case DW_FORM_data4:
21229 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
21230 break;
21231 case DW_FORM_data8:
21232 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
21233 break;
21234
21235 case DW_FORM_sdata:
21236 case DW_FORM_implicit_const:
21237 *value = DW_SND (attr);
21238 break;
21239
21240 case DW_FORM_udata:
21241 *value = DW_UNSND (attr);
21242 break;
21243
21244 default:
21245 complaint (_("unsupported const value attribute form: '%s'"),
21246 dwarf_form_name (attr->form));
21247 *value = 0;
21248 break;
21249 }
21250 }
21251
21252
21253 /* Copy constant value from an attribute to a symbol. */
21254
21255 static void
21256 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
21257 struct dwarf2_cu *cu)
21258 {
21259 struct objfile *objfile = cu->per_objfile->objfile;
21260 LONGEST value;
21261 const gdb_byte *bytes;
21262 struct dwarf2_locexpr_baton *baton;
21263
21264 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21265 sym->print_name (),
21266 &objfile->objfile_obstack, cu,
21267 &value, &bytes, &baton);
21268
21269 if (baton != NULL)
21270 {
21271 SYMBOL_LOCATION_BATON (sym) = baton;
21272 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
21273 }
21274 else if (bytes != NULL)
21275 {
21276 SYMBOL_VALUE_BYTES (sym) = bytes;
21277 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
21278 }
21279 else
21280 {
21281 SYMBOL_VALUE (sym) = value;
21282 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
21283 }
21284 }
21285
21286 /* Return the type of the die in question using its DW_AT_type attribute. */
21287
21288 static struct type *
21289 die_type (struct die_info *die, struct dwarf2_cu *cu)
21290 {
21291 struct attribute *type_attr;
21292
21293 type_attr = dwarf2_attr (die, DW_AT_type, cu);
21294 if (!type_attr)
21295 {
21296 struct objfile *objfile = cu->per_objfile->objfile;
21297 /* A missing DW_AT_type represents a void type. */
21298 return objfile_type (objfile)->builtin_void;
21299 }
21300
21301 return lookup_die_type (die, type_attr, cu);
21302 }
21303
21304 /* True iff CU's producer generates GNAT Ada auxiliary information
21305 that allows to find parallel types through that information instead
21306 of having to do expensive parallel lookups by type name. */
21307
21308 static int
21309 need_gnat_info (struct dwarf2_cu *cu)
21310 {
21311 /* Assume that the Ada compiler was GNAT, which always produces
21312 the auxiliary information. */
21313 return (cu->language == language_ada);
21314 }
21315
21316 /* Return the auxiliary type of the die in question using its
21317 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21318 attribute is not present. */
21319
21320 static struct type *
21321 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21322 {
21323 struct attribute *type_attr;
21324
21325 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21326 if (!type_attr)
21327 return NULL;
21328
21329 return lookup_die_type (die, type_attr, cu);
21330 }
21331
21332 /* If DIE has a descriptive_type attribute, then set the TYPE's
21333 descriptive type accordingly. */
21334
21335 static void
21336 set_descriptive_type (struct type *type, struct die_info *die,
21337 struct dwarf2_cu *cu)
21338 {
21339 struct type *descriptive_type = die_descriptive_type (die, cu);
21340
21341 if (descriptive_type)
21342 {
21343 ALLOCATE_GNAT_AUX_TYPE (type);
21344 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21345 }
21346 }
21347
21348 /* Return the containing type of the die in question using its
21349 DW_AT_containing_type attribute. */
21350
21351 static struct type *
21352 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
21353 {
21354 struct attribute *type_attr;
21355 struct objfile *objfile = cu->per_objfile->objfile;
21356
21357 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
21358 if (!type_attr)
21359 error (_("Dwarf Error: Problem turning containing type into gdb type "
21360 "[in module %s]"), objfile_name (objfile));
21361
21362 return lookup_die_type (die, type_attr, cu);
21363 }
21364
21365 /* Return an error marker type to use for the ill formed type in DIE/CU. */
21366
21367 static struct type *
21368 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21369 {
21370 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21371 struct objfile *objfile = dwarf2_per_objfile->objfile;
21372 char *saved;
21373
21374 std::string message
21375 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21376 objfile_name (objfile),
21377 sect_offset_str (cu->header.sect_off),
21378 sect_offset_str (die->sect_off));
21379 saved = obstack_strdup (&objfile->objfile_obstack, message);
21380
21381 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
21382 }
21383
21384 /* Look up the type of DIE in CU using its type attribute ATTR.
21385 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21386 DW_AT_containing_type.
21387 If there is no type substitute an error marker. */
21388
21389 static struct type *
21390 lookup_die_type (struct die_info *die, const struct attribute *attr,
21391 struct dwarf2_cu *cu)
21392 {
21393 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21394 struct objfile *objfile = dwarf2_per_objfile->objfile;
21395 struct type *this_type;
21396
21397 gdb_assert (attr->name == DW_AT_type
21398 || attr->name == DW_AT_GNAT_descriptive_type
21399 || attr->name == DW_AT_containing_type);
21400
21401 /* First see if we have it cached. */
21402
21403 if (attr->form == DW_FORM_GNU_ref_alt)
21404 {
21405 struct dwarf2_per_cu_data *per_cu;
21406 sect_offset sect_off = attr->get_ref_die_offset ();
21407
21408 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21409 dwarf2_per_objfile);
21410 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
21411 }
21412 else if (attr->form_is_ref ())
21413 {
21414 sect_offset sect_off = attr->get_ref_die_offset ();
21415
21416 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21417 dwarf2_per_objfile);
21418 }
21419 else if (attr->form == DW_FORM_ref_sig8)
21420 {
21421 ULONGEST signature = DW_SIGNATURE (attr);
21422
21423 return get_signatured_type (die, signature, cu);
21424 }
21425 else
21426 {
21427 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
21428 " at %s [in module %s]"),
21429 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
21430 objfile_name (objfile));
21431 return build_error_marker_type (cu, die);
21432 }
21433
21434 /* If not cached we need to read it in. */
21435
21436 if (this_type == NULL)
21437 {
21438 struct die_info *type_die = NULL;
21439 struct dwarf2_cu *type_cu = cu;
21440
21441 if (attr->form_is_ref ())
21442 type_die = follow_die_ref (die, attr, &type_cu);
21443 if (type_die == NULL)
21444 return build_error_marker_type (cu, die);
21445 /* If we find the type now, it's probably because the type came
21446 from an inter-CU reference and the type's CU got expanded before
21447 ours. */
21448 this_type = read_type_die (type_die, type_cu);
21449 }
21450
21451 /* If we still don't have a type use an error marker. */
21452
21453 if (this_type == NULL)
21454 return build_error_marker_type (cu, die);
21455
21456 return this_type;
21457 }
21458
21459 /* Return the type in DIE, CU.
21460 Returns NULL for invalid types.
21461
21462 This first does a lookup in die_type_hash,
21463 and only reads the die in if necessary.
21464
21465 NOTE: This can be called when reading in partial or full symbols. */
21466
21467 static struct type *
21468 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
21469 {
21470 struct type *this_type;
21471
21472 this_type = get_die_type (die, cu);
21473 if (this_type)
21474 return this_type;
21475
21476 return read_type_die_1 (die, cu);
21477 }
21478
21479 /* Read the type in DIE, CU.
21480 Returns NULL for invalid types. */
21481
21482 static struct type *
21483 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21484 {
21485 struct type *this_type = NULL;
21486
21487 switch (die->tag)
21488 {
21489 case DW_TAG_class_type:
21490 case DW_TAG_interface_type:
21491 case DW_TAG_structure_type:
21492 case DW_TAG_union_type:
21493 this_type = read_structure_type (die, cu);
21494 break;
21495 case DW_TAG_enumeration_type:
21496 this_type = read_enumeration_type (die, cu);
21497 break;
21498 case DW_TAG_subprogram:
21499 case DW_TAG_subroutine_type:
21500 case DW_TAG_inlined_subroutine:
21501 this_type = read_subroutine_type (die, cu);
21502 break;
21503 case DW_TAG_array_type:
21504 this_type = read_array_type (die, cu);
21505 break;
21506 case DW_TAG_set_type:
21507 this_type = read_set_type (die, cu);
21508 break;
21509 case DW_TAG_pointer_type:
21510 this_type = read_tag_pointer_type (die, cu);
21511 break;
21512 case DW_TAG_ptr_to_member_type:
21513 this_type = read_tag_ptr_to_member_type (die, cu);
21514 break;
21515 case DW_TAG_reference_type:
21516 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21517 break;
21518 case DW_TAG_rvalue_reference_type:
21519 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
21520 break;
21521 case DW_TAG_const_type:
21522 this_type = read_tag_const_type (die, cu);
21523 break;
21524 case DW_TAG_volatile_type:
21525 this_type = read_tag_volatile_type (die, cu);
21526 break;
21527 case DW_TAG_restrict_type:
21528 this_type = read_tag_restrict_type (die, cu);
21529 break;
21530 case DW_TAG_string_type:
21531 this_type = read_tag_string_type (die, cu);
21532 break;
21533 case DW_TAG_typedef:
21534 this_type = read_typedef (die, cu);
21535 break;
21536 case DW_TAG_subrange_type:
21537 this_type = read_subrange_type (die, cu);
21538 break;
21539 case DW_TAG_base_type:
21540 this_type = read_base_type (die, cu);
21541 break;
21542 case DW_TAG_unspecified_type:
21543 this_type = read_unspecified_type (die, cu);
21544 break;
21545 case DW_TAG_namespace:
21546 this_type = read_namespace_type (die, cu);
21547 break;
21548 case DW_TAG_module:
21549 this_type = read_module_type (die, cu);
21550 break;
21551 case DW_TAG_atomic_type:
21552 this_type = read_tag_atomic_type (die, cu);
21553 break;
21554 default:
21555 complaint (_("unexpected tag in read_type_die: '%s'"),
21556 dwarf_tag_name (die->tag));
21557 break;
21558 }
21559
21560 return this_type;
21561 }
21562
21563 /* See if we can figure out if the class lives in a namespace. We do
21564 this by looking for a member function; its demangled name will
21565 contain namespace info, if there is any.
21566 Return the computed name or NULL.
21567 Space for the result is allocated on the objfile's obstack.
21568 This is the full-die version of guess_partial_die_structure_name.
21569 In this case we know DIE has no useful parent. */
21570
21571 static const char *
21572 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21573 {
21574 struct die_info *spec_die;
21575 struct dwarf2_cu *spec_cu;
21576 struct die_info *child;
21577 struct objfile *objfile = cu->per_objfile->objfile;
21578
21579 spec_cu = cu;
21580 spec_die = die_specification (die, &spec_cu);
21581 if (spec_die != NULL)
21582 {
21583 die = spec_die;
21584 cu = spec_cu;
21585 }
21586
21587 for (child = die->child;
21588 child != NULL;
21589 child = child->sibling)
21590 {
21591 if (child->tag == DW_TAG_subprogram)
21592 {
21593 const char *linkage_name = dw2_linkage_name (child, cu);
21594
21595 if (linkage_name != NULL)
21596 {
21597 gdb::unique_xmalloc_ptr<char> actual_name
21598 (language_class_name_from_physname (cu->language_defn,
21599 linkage_name));
21600 const char *name = NULL;
21601
21602 if (actual_name != NULL)
21603 {
21604 const char *die_name = dwarf2_name (die, cu);
21605
21606 if (die_name != NULL
21607 && strcmp (die_name, actual_name.get ()) != 0)
21608 {
21609 /* Strip off the class name from the full name.
21610 We want the prefix. */
21611 int die_name_len = strlen (die_name);
21612 int actual_name_len = strlen (actual_name.get ());
21613 const char *ptr = actual_name.get ();
21614
21615 /* Test for '::' as a sanity check. */
21616 if (actual_name_len > die_name_len + 2
21617 && ptr[actual_name_len - die_name_len - 1] == ':')
21618 name = obstack_strndup (
21619 &objfile->per_bfd->storage_obstack,
21620 ptr, actual_name_len - die_name_len - 2);
21621 }
21622 }
21623 return name;
21624 }
21625 }
21626 }
21627
21628 return NULL;
21629 }
21630
21631 /* GCC might emit a nameless typedef that has a linkage name. Determine the
21632 prefix part in such case. See
21633 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21634
21635 static const char *
21636 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21637 {
21638 struct attribute *attr;
21639 const char *base;
21640
21641 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21642 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21643 return NULL;
21644
21645 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
21646 return NULL;
21647
21648 attr = dw2_linkage_name_attr (die, cu);
21649 if (attr == NULL || DW_STRING (attr) == NULL)
21650 return NULL;
21651
21652 /* dwarf2_name had to be already called. */
21653 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21654
21655 /* Strip the base name, keep any leading namespaces/classes. */
21656 base = strrchr (DW_STRING (attr), ':');
21657 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21658 return "";
21659
21660 struct objfile *objfile = cu->per_objfile->objfile;
21661 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21662 DW_STRING (attr),
21663 &base[-1] - DW_STRING (attr));
21664 }
21665
21666 /* Return the name of the namespace/class that DIE is defined within,
21667 or "" if we can't tell. The caller should not xfree the result.
21668
21669 For example, if we're within the method foo() in the following
21670 code:
21671
21672 namespace N {
21673 class C {
21674 void foo () {
21675 }
21676 };
21677 }
21678
21679 then determine_prefix on foo's die will return "N::C". */
21680
21681 static const char *
21682 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
21683 {
21684 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
21685 struct die_info *parent, *spec_die;
21686 struct dwarf2_cu *spec_cu;
21687 struct type *parent_type;
21688 const char *retval;
21689
21690 if (cu->language != language_cplus
21691 && cu->language != language_fortran && cu->language != language_d
21692 && cu->language != language_rust)
21693 return "";
21694
21695 retval = anonymous_struct_prefix (die, cu);
21696 if (retval)
21697 return retval;
21698
21699 /* We have to be careful in the presence of DW_AT_specification.
21700 For example, with GCC 3.4, given the code
21701
21702 namespace N {
21703 void foo() {
21704 // Definition of N::foo.
21705 }
21706 }
21707
21708 then we'll have a tree of DIEs like this:
21709
21710 1: DW_TAG_compile_unit
21711 2: DW_TAG_namespace // N
21712 3: DW_TAG_subprogram // declaration of N::foo
21713 4: DW_TAG_subprogram // definition of N::foo
21714 DW_AT_specification // refers to die #3
21715
21716 Thus, when processing die #4, we have to pretend that we're in
21717 the context of its DW_AT_specification, namely the contex of die
21718 #3. */
21719 spec_cu = cu;
21720 spec_die = die_specification (die, &spec_cu);
21721 if (spec_die == NULL)
21722 parent = die->parent;
21723 else
21724 {
21725 parent = spec_die->parent;
21726 cu = spec_cu;
21727 }
21728
21729 if (parent == NULL)
21730 return "";
21731 else if (parent->building_fullname)
21732 {
21733 const char *name;
21734 const char *parent_name;
21735
21736 /* It has been seen on RealView 2.2 built binaries,
21737 DW_TAG_template_type_param types actually _defined_ as
21738 children of the parent class:
21739
21740 enum E {};
21741 template class <class Enum> Class{};
21742 Class<enum E> class_e;
21743
21744 1: DW_TAG_class_type (Class)
21745 2: DW_TAG_enumeration_type (E)
21746 3: DW_TAG_enumerator (enum1:0)
21747 3: DW_TAG_enumerator (enum2:1)
21748 ...
21749 2: DW_TAG_template_type_param
21750 DW_AT_type DW_FORM_ref_udata (E)
21751
21752 Besides being broken debug info, it can put GDB into an
21753 infinite loop. Consider:
21754
21755 When we're building the full name for Class<E>, we'll start
21756 at Class, and go look over its template type parameters,
21757 finding E. We'll then try to build the full name of E, and
21758 reach here. We're now trying to build the full name of E,
21759 and look over the parent DIE for containing scope. In the
21760 broken case, if we followed the parent DIE of E, we'd again
21761 find Class, and once again go look at its template type
21762 arguments, etc., etc. Simply don't consider such parent die
21763 as source-level parent of this die (it can't be, the language
21764 doesn't allow it), and break the loop here. */
21765 name = dwarf2_name (die, cu);
21766 parent_name = dwarf2_name (parent, cu);
21767 complaint (_("template param type '%s' defined within parent '%s'"),
21768 name ? name : "<unknown>",
21769 parent_name ? parent_name : "<unknown>");
21770 return "";
21771 }
21772 else
21773 switch (parent->tag)
21774 {
21775 case DW_TAG_namespace:
21776 parent_type = read_type_die (parent, cu);
21777 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21778 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21779 Work around this problem here. */
21780 if (cu->language == language_cplus
21781 && strcmp (parent_type->name (), "::") == 0)
21782 return "";
21783 /* We give a name to even anonymous namespaces. */
21784 return parent_type->name ();
21785 case DW_TAG_class_type:
21786 case DW_TAG_interface_type:
21787 case DW_TAG_structure_type:
21788 case DW_TAG_union_type:
21789 case DW_TAG_module:
21790 parent_type = read_type_die (parent, cu);
21791 if (parent_type->name () != NULL)
21792 return parent_type->name ();
21793 else
21794 /* An anonymous structure is only allowed non-static data
21795 members; no typedefs, no member functions, et cetera.
21796 So it does not need a prefix. */
21797 return "";
21798 case DW_TAG_compile_unit:
21799 case DW_TAG_partial_unit:
21800 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21801 if (cu->language == language_cplus
21802 && !dwarf2_per_objfile->per_bfd->types.empty ()
21803 && die->child != NULL
21804 && (die->tag == DW_TAG_class_type
21805 || die->tag == DW_TAG_structure_type
21806 || die->tag == DW_TAG_union_type))
21807 {
21808 const char *name = guess_full_die_structure_name (die, cu);
21809 if (name != NULL)
21810 return name;
21811 }
21812 return "";
21813 case DW_TAG_subprogram:
21814 /* Nested subroutines in Fortran get a prefix with the name
21815 of the parent's subroutine. */
21816 if (cu->language == language_fortran)
21817 {
21818 if ((die->tag == DW_TAG_subprogram)
21819 && (dwarf2_name (parent, cu) != NULL))
21820 return dwarf2_name (parent, cu);
21821 }
21822 return determine_prefix (parent, cu);
21823 case DW_TAG_enumeration_type:
21824 parent_type = read_type_die (parent, cu);
21825 if (TYPE_DECLARED_CLASS (parent_type))
21826 {
21827 if (parent_type->name () != NULL)
21828 return parent_type->name ();
21829 return "";
21830 }
21831 /* Fall through. */
21832 default:
21833 return determine_prefix (parent, cu);
21834 }
21835 }
21836
21837 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21838 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21839 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21840 an obconcat, otherwise allocate storage for the result. The CU argument is
21841 used to determine the language and hence, the appropriate separator. */
21842
21843 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
21844
21845 static char *
21846 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21847 int physname, struct dwarf2_cu *cu)
21848 {
21849 const char *lead = "";
21850 const char *sep;
21851
21852 if (suffix == NULL || suffix[0] == '\0'
21853 || prefix == NULL || prefix[0] == '\0')
21854 sep = "";
21855 else if (cu->language == language_d)
21856 {
21857 /* For D, the 'main' function could be defined in any module, but it
21858 should never be prefixed. */
21859 if (strcmp (suffix, "D main") == 0)
21860 {
21861 prefix = "";
21862 sep = "";
21863 }
21864 else
21865 sep = ".";
21866 }
21867 else if (cu->language == language_fortran && physname)
21868 {
21869 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21870 DW_AT_MIPS_linkage_name is preferred and used instead. */
21871
21872 lead = "__";
21873 sep = "_MOD_";
21874 }
21875 else
21876 sep = "::";
21877
21878 if (prefix == NULL)
21879 prefix = "";
21880 if (suffix == NULL)
21881 suffix = "";
21882
21883 if (obs == NULL)
21884 {
21885 char *retval
21886 = ((char *)
21887 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
21888
21889 strcpy (retval, lead);
21890 strcat (retval, prefix);
21891 strcat (retval, sep);
21892 strcat (retval, suffix);
21893 return retval;
21894 }
21895 else
21896 {
21897 /* We have an obstack. */
21898 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
21899 }
21900 }
21901
21902 /* Get name of a die, return NULL if not found. */
21903
21904 static const char *
21905 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
21906 struct objfile *objfile)
21907 {
21908 if (name && cu->language == language_cplus)
21909 {
21910 gdb::unique_xmalloc_ptr<char> canon_name
21911 = cp_canonicalize_string (name);
21912
21913 if (canon_name != nullptr)
21914 name = objfile->intern (canon_name.get ());
21915 }
21916
21917 return name;
21918 }
21919
21920 /* Get name of a die, return NULL if not found.
21921 Anonymous namespaces are converted to their magic string. */
21922
21923 static const char *
21924 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
21925 {
21926 struct attribute *attr;
21927 struct objfile *objfile = cu->per_objfile->objfile;
21928
21929 attr = dwarf2_attr (die, DW_AT_name, cu);
21930 if ((!attr || !DW_STRING (attr))
21931 && die->tag != DW_TAG_namespace
21932 && die->tag != DW_TAG_class_type
21933 && die->tag != DW_TAG_interface_type
21934 && die->tag != DW_TAG_structure_type
21935 && die->tag != DW_TAG_union_type)
21936 return NULL;
21937
21938 switch (die->tag)
21939 {
21940 case DW_TAG_compile_unit:
21941 case DW_TAG_partial_unit:
21942 /* Compilation units have a DW_AT_name that is a filename, not
21943 a source language identifier. */
21944 case DW_TAG_enumeration_type:
21945 case DW_TAG_enumerator:
21946 /* These tags always have simple identifiers already; no need
21947 to canonicalize them. */
21948 return DW_STRING (attr);
21949
21950 case DW_TAG_namespace:
21951 if (attr != NULL && DW_STRING (attr) != NULL)
21952 return DW_STRING (attr);
21953 return CP_ANONYMOUS_NAMESPACE_STR;
21954
21955 case DW_TAG_class_type:
21956 case DW_TAG_interface_type:
21957 case DW_TAG_structure_type:
21958 case DW_TAG_union_type:
21959 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21960 structures or unions. These were of the form "._%d" in GCC 4.1,
21961 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21962 and GCC 4.4. We work around this problem by ignoring these. */
21963 if (attr && DW_STRING (attr)
21964 && (startswith (DW_STRING (attr), "._")
21965 || startswith (DW_STRING (attr), "<anonymous")))
21966 return NULL;
21967
21968 /* GCC might emit a nameless typedef that has a linkage name. See
21969 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21970 if (!attr || DW_STRING (attr) == NULL)
21971 {
21972 attr = dw2_linkage_name_attr (die, cu);
21973 if (attr == NULL || DW_STRING (attr) == NULL)
21974 return NULL;
21975
21976 /* Avoid demangling DW_STRING (attr) the second time on a second
21977 call for the same DIE. */
21978 if (!DW_STRING_IS_CANONICAL (attr))
21979 {
21980 gdb::unique_xmalloc_ptr<char> demangled
21981 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
21982 if (demangled == nullptr)
21983 return nullptr;
21984
21985 DW_STRING (attr) = objfile->intern (demangled.get ());
21986 DW_STRING_IS_CANONICAL (attr) = 1;
21987 }
21988
21989 /* Strip any leading namespaces/classes, keep only the base name.
21990 DW_AT_name for named DIEs does not contain the prefixes. */
21991 const char *base = strrchr (DW_STRING (attr), ':');
21992 if (base && base > DW_STRING (attr) && base[-1] == ':')
21993 return &base[1];
21994 else
21995 return DW_STRING (attr);
21996 }
21997 break;
21998
21999 default:
22000 break;
22001 }
22002
22003 if (!DW_STRING_IS_CANONICAL (attr))
22004 {
22005 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
22006 objfile);
22007 DW_STRING_IS_CANONICAL (attr) = 1;
22008 }
22009 return DW_STRING (attr);
22010 }
22011
22012 /* Return the die that this die in an extension of, or NULL if there
22013 is none. *EXT_CU is the CU containing DIE on input, and the CU
22014 containing the return value on output. */
22015
22016 static struct die_info *
22017 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
22018 {
22019 struct attribute *attr;
22020
22021 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
22022 if (attr == NULL)
22023 return NULL;
22024
22025 return follow_die_ref (die, attr, ext_cu);
22026 }
22027
22028 static void
22029 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
22030 {
22031 unsigned int i;
22032
22033 print_spaces (indent, f);
22034 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
22035 dwarf_tag_name (die->tag), die->abbrev,
22036 sect_offset_str (die->sect_off));
22037
22038 if (die->parent != NULL)
22039 {
22040 print_spaces (indent, f);
22041 fprintf_unfiltered (f, " parent at offset: %s\n",
22042 sect_offset_str (die->parent->sect_off));
22043 }
22044
22045 print_spaces (indent, f);
22046 fprintf_unfiltered (f, " has children: %s\n",
22047 dwarf_bool_name (die->child != NULL));
22048
22049 print_spaces (indent, f);
22050 fprintf_unfiltered (f, " attributes:\n");
22051
22052 for (i = 0; i < die->num_attrs; ++i)
22053 {
22054 print_spaces (indent, f);
22055 fprintf_unfiltered (f, " %s (%s) ",
22056 dwarf_attr_name (die->attrs[i].name),
22057 dwarf_form_name (die->attrs[i].form));
22058
22059 switch (die->attrs[i].form)
22060 {
22061 case DW_FORM_addr:
22062 case DW_FORM_addrx:
22063 case DW_FORM_GNU_addr_index:
22064 fprintf_unfiltered (f, "address: ");
22065 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
22066 break;
22067 case DW_FORM_block2:
22068 case DW_FORM_block4:
22069 case DW_FORM_block:
22070 case DW_FORM_block1:
22071 fprintf_unfiltered (f, "block: size %s",
22072 pulongest (DW_BLOCK (&die->attrs[i])->size));
22073 break;
22074 case DW_FORM_exprloc:
22075 fprintf_unfiltered (f, "expression: size %s",
22076 pulongest (DW_BLOCK (&die->attrs[i])->size));
22077 break;
22078 case DW_FORM_data16:
22079 fprintf_unfiltered (f, "constant of 16 bytes");
22080 break;
22081 case DW_FORM_ref_addr:
22082 fprintf_unfiltered (f, "ref address: ");
22083 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22084 break;
22085 case DW_FORM_GNU_ref_alt:
22086 fprintf_unfiltered (f, "alt ref address: ");
22087 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22088 break;
22089 case DW_FORM_ref1:
22090 case DW_FORM_ref2:
22091 case DW_FORM_ref4:
22092 case DW_FORM_ref8:
22093 case DW_FORM_ref_udata:
22094 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
22095 (long) (DW_UNSND (&die->attrs[i])));
22096 break;
22097 case DW_FORM_data1:
22098 case DW_FORM_data2:
22099 case DW_FORM_data4:
22100 case DW_FORM_data8:
22101 case DW_FORM_udata:
22102 case DW_FORM_sdata:
22103 fprintf_unfiltered (f, "constant: %s",
22104 pulongest (DW_UNSND (&die->attrs[i])));
22105 break;
22106 case DW_FORM_sec_offset:
22107 fprintf_unfiltered (f, "section offset: %s",
22108 pulongest (DW_UNSND (&die->attrs[i])));
22109 break;
22110 case DW_FORM_ref_sig8:
22111 fprintf_unfiltered (f, "signature: %s",
22112 hex_string (DW_SIGNATURE (&die->attrs[i])));
22113 break;
22114 case DW_FORM_string:
22115 case DW_FORM_strp:
22116 case DW_FORM_line_strp:
22117 case DW_FORM_strx:
22118 case DW_FORM_GNU_str_index:
22119 case DW_FORM_GNU_strp_alt:
22120 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
22121 DW_STRING (&die->attrs[i])
22122 ? DW_STRING (&die->attrs[i]) : "",
22123 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
22124 break;
22125 case DW_FORM_flag:
22126 if (DW_UNSND (&die->attrs[i]))
22127 fprintf_unfiltered (f, "flag: TRUE");
22128 else
22129 fprintf_unfiltered (f, "flag: FALSE");
22130 break;
22131 case DW_FORM_flag_present:
22132 fprintf_unfiltered (f, "flag: TRUE");
22133 break;
22134 case DW_FORM_indirect:
22135 /* The reader will have reduced the indirect form to
22136 the "base form" so this form should not occur. */
22137 fprintf_unfiltered (f,
22138 "unexpected attribute form: DW_FORM_indirect");
22139 break;
22140 case DW_FORM_implicit_const:
22141 fprintf_unfiltered (f, "constant: %s",
22142 plongest (DW_SND (&die->attrs[i])));
22143 break;
22144 default:
22145 fprintf_unfiltered (f, "unsupported attribute form: %d.",
22146 die->attrs[i].form);
22147 break;
22148 }
22149 fprintf_unfiltered (f, "\n");
22150 }
22151 }
22152
22153 static void
22154 dump_die_for_error (struct die_info *die)
22155 {
22156 dump_die_shallow (gdb_stderr, 0, die);
22157 }
22158
22159 static void
22160 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22161 {
22162 int indent = level * 4;
22163
22164 gdb_assert (die != NULL);
22165
22166 if (level >= max_level)
22167 return;
22168
22169 dump_die_shallow (f, indent, die);
22170
22171 if (die->child != NULL)
22172 {
22173 print_spaces (indent, f);
22174 fprintf_unfiltered (f, " Children:");
22175 if (level + 1 < max_level)
22176 {
22177 fprintf_unfiltered (f, "\n");
22178 dump_die_1 (f, level + 1, max_level, die->child);
22179 }
22180 else
22181 {
22182 fprintf_unfiltered (f,
22183 " [not printed, max nesting level reached]\n");
22184 }
22185 }
22186
22187 if (die->sibling != NULL && level > 0)
22188 {
22189 dump_die_1 (f, level, max_level, die->sibling);
22190 }
22191 }
22192
22193 /* This is called from the pdie macro in gdbinit.in.
22194 It's not static so gcc will keep a copy callable from gdb. */
22195
22196 void
22197 dump_die (struct die_info *die, int max_level)
22198 {
22199 dump_die_1 (gdb_stdlog, 0, max_level, die);
22200 }
22201
22202 static void
22203 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
22204 {
22205 void **slot;
22206
22207 slot = htab_find_slot_with_hash (cu->die_hash, die,
22208 to_underlying (die->sect_off),
22209 INSERT);
22210
22211 *slot = die;
22212 }
22213
22214 /* Follow reference or signature attribute ATTR of SRC_DIE.
22215 On entry *REF_CU is the CU of SRC_DIE.
22216 On exit *REF_CU is the CU of the result. */
22217
22218 static struct die_info *
22219 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
22220 struct dwarf2_cu **ref_cu)
22221 {
22222 struct die_info *die;
22223
22224 if (attr->form_is_ref ())
22225 die = follow_die_ref (src_die, attr, ref_cu);
22226 else if (attr->form == DW_FORM_ref_sig8)
22227 die = follow_die_sig (src_die, attr, ref_cu);
22228 else
22229 {
22230 dump_die_for_error (src_die);
22231 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
22232 objfile_name ((*ref_cu)->per_objfile->objfile));
22233 }
22234
22235 return die;
22236 }
22237
22238 /* Follow reference OFFSET.
22239 On entry *REF_CU is the CU of the source die referencing OFFSET.
22240 On exit *REF_CU is the CU of the result.
22241 Returns NULL if OFFSET is invalid. */
22242
22243 static struct die_info *
22244 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
22245 struct dwarf2_cu **ref_cu)
22246 {
22247 struct die_info temp_die;
22248 struct dwarf2_cu *target_cu, *cu = *ref_cu;
22249 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22250
22251 gdb_assert (cu->per_cu != NULL);
22252
22253 target_cu = cu;
22254
22255 if (cu->per_cu->is_debug_types)
22256 {
22257 /* .debug_types CUs cannot reference anything outside their CU.
22258 If they need to, they have to reference a signatured type via
22259 DW_FORM_ref_sig8. */
22260 if (!cu->header.offset_in_cu_p (sect_off))
22261 return NULL;
22262 }
22263 else if (offset_in_dwz != cu->per_cu->is_dwz
22264 || !cu->header.offset_in_cu_p (sect_off))
22265 {
22266 struct dwarf2_per_cu_data *per_cu;
22267
22268 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22269 dwarf2_per_objfile);
22270
22271 /* If necessary, add it to the queue and load its DIEs. */
22272 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
22273 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
22274
22275 target_cu = dwarf2_per_objfile->get_cu (per_cu);
22276 }
22277 else if (cu->dies == NULL)
22278 {
22279 /* We're loading full DIEs during partial symbol reading. */
22280 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
22281 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22282 language_minimal);
22283 }
22284
22285 *ref_cu = target_cu;
22286 temp_die.sect_off = sect_off;
22287
22288 if (target_cu != cu)
22289 target_cu->ancestor = cu;
22290
22291 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
22292 &temp_die,
22293 to_underlying (sect_off));
22294 }
22295
22296 /* Follow reference attribute ATTR of SRC_DIE.
22297 On entry *REF_CU is the CU of SRC_DIE.
22298 On exit *REF_CU is the CU of the result. */
22299
22300 static struct die_info *
22301 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
22302 struct dwarf2_cu **ref_cu)
22303 {
22304 sect_offset sect_off = attr->get_ref_die_offset ();
22305 struct dwarf2_cu *cu = *ref_cu;
22306 struct die_info *die;
22307
22308 die = follow_die_offset (sect_off,
22309 (attr->form == DW_FORM_GNU_ref_alt
22310 || cu->per_cu->is_dwz),
22311 ref_cu);
22312 if (!die)
22313 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22314 "at %s [in module %s]"),
22315 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
22316 objfile_name (cu->per_objfile->objfile));
22317
22318 return die;
22319 }
22320
22321 /* See read.h. */
22322
22323 struct dwarf2_locexpr_baton
22324 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
22325 dwarf2_per_cu_data *per_cu,
22326 dwarf2_per_objfile *dwarf2_per_objfile,
22327 CORE_ADDR (*get_frame_pc) (void *baton),
22328 void *baton, bool resolve_abstract_p)
22329 {
22330 struct die_info *die;
22331 struct attribute *attr;
22332 struct dwarf2_locexpr_baton retval;
22333 struct objfile *objfile = dwarf2_per_objfile->objfile;
22334
22335 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
22336 if (cu == nullptr)
22337 cu = load_cu (per_cu, dwarf2_per_objfile, false);
22338
22339 if (cu == nullptr)
22340 {
22341 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22342 Instead just throw an error, not much else we can do. */
22343 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22344 sect_offset_str (sect_off), objfile_name (objfile));
22345 }
22346
22347 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22348 if (!die)
22349 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22350 sect_offset_str (sect_off), objfile_name (objfile));
22351
22352 attr = dwarf2_attr (die, DW_AT_location, cu);
22353 if (!attr && resolve_abstract_p
22354 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22355 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
22356 {
22357 CORE_ADDR pc = (*get_frame_pc) (baton);
22358 CORE_ADDR baseaddr = objfile->text_section_offset ();
22359 struct gdbarch *gdbarch = objfile->arch ();
22360
22361 for (const auto &cand_off
22362 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
22363 {
22364 struct dwarf2_cu *cand_cu = cu;
22365 struct die_info *cand
22366 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22367 if (!cand
22368 || !cand->parent
22369 || cand->parent->tag != DW_TAG_subprogram)
22370 continue;
22371
22372 CORE_ADDR pc_low, pc_high;
22373 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
22374 if (pc_low == ((CORE_ADDR) -1))
22375 continue;
22376 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22377 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22378 if (!(pc_low <= pc && pc < pc_high))
22379 continue;
22380
22381 die = cand;
22382 attr = dwarf2_attr (die, DW_AT_location, cu);
22383 break;
22384 }
22385 }
22386
22387 if (!attr)
22388 {
22389 /* DWARF: "If there is no such attribute, then there is no effect.".
22390 DATA is ignored if SIZE is 0. */
22391
22392 retval.data = NULL;
22393 retval.size = 0;
22394 }
22395 else if (attr->form_is_section_offset ())
22396 {
22397 struct dwarf2_loclist_baton loclist_baton;
22398 CORE_ADDR pc = (*get_frame_pc) (baton);
22399 size_t size;
22400
22401 fill_in_loclist_baton (cu, &loclist_baton, attr);
22402
22403 retval.data = dwarf2_find_location_expression (&loclist_baton,
22404 &size, pc);
22405 retval.size = size;
22406 }
22407 else
22408 {
22409 if (!attr->form_is_block ())
22410 error (_("Dwarf Error: DIE at %s referenced in module %s "
22411 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
22412 sect_offset_str (sect_off), objfile_name (objfile));
22413
22414 retval.data = DW_BLOCK (attr)->data;
22415 retval.size = DW_BLOCK (attr)->size;
22416 }
22417 retval.per_objfile = dwarf2_per_objfile;
22418 retval.per_cu = cu->per_cu;
22419
22420 dwarf2_per_objfile->age_comp_units ();
22421
22422 return retval;
22423 }
22424
22425 /* See read.h. */
22426
22427 struct dwarf2_locexpr_baton
22428 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22429 dwarf2_per_cu_data *per_cu,
22430 dwarf2_per_objfile *per_objfile,
22431 CORE_ADDR (*get_frame_pc) (void *baton),
22432 void *baton)
22433 {
22434 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
22435
22436 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22437 get_frame_pc, baton);
22438 }
22439
22440 /* Write a constant of a given type as target-ordered bytes into
22441 OBSTACK. */
22442
22443 static const gdb_byte *
22444 write_constant_as_bytes (struct obstack *obstack,
22445 enum bfd_endian byte_order,
22446 struct type *type,
22447 ULONGEST value,
22448 LONGEST *len)
22449 {
22450 gdb_byte *result;
22451
22452 *len = TYPE_LENGTH (type);
22453 result = (gdb_byte *) obstack_alloc (obstack, *len);
22454 store_unsigned_integer (result, *len, byte_order, value);
22455
22456 return result;
22457 }
22458
22459 /* See read.h. */
22460
22461 const gdb_byte *
22462 dwarf2_fetch_constant_bytes (sect_offset sect_off,
22463 dwarf2_per_cu_data *per_cu,
22464 dwarf2_per_objfile *per_objfile,
22465 obstack *obstack,
22466 LONGEST *len)
22467 {
22468 struct die_info *die;
22469 struct attribute *attr;
22470 const gdb_byte *result = NULL;
22471 struct type *type;
22472 LONGEST value;
22473 enum bfd_endian byte_order;
22474 struct objfile *objfile = per_objfile->objfile;
22475
22476 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22477 if (cu == nullptr)
22478 cu = load_cu (per_cu, per_objfile, false);
22479
22480 if (cu == nullptr)
22481 {
22482 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22483 Instead just throw an error, not much else we can do. */
22484 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22485 sect_offset_str (sect_off), objfile_name (objfile));
22486 }
22487
22488 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22489 if (!die)
22490 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22491 sect_offset_str (sect_off), objfile_name (objfile));
22492
22493 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22494 if (attr == NULL)
22495 return NULL;
22496
22497 byte_order = (bfd_big_endian (objfile->obfd)
22498 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22499
22500 switch (attr->form)
22501 {
22502 case DW_FORM_addr:
22503 case DW_FORM_addrx:
22504 case DW_FORM_GNU_addr_index:
22505 {
22506 gdb_byte *tem;
22507
22508 *len = cu->header.addr_size;
22509 tem = (gdb_byte *) obstack_alloc (obstack, *len);
22510 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22511 result = tem;
22512 }
22513 break;
22514 case DW_FORM_string:
22515 case DW_FORM_strp:
22516 case DW_FORM_strx:
22517 case DW_FORM_GNU_str_index:
22518 case DW_FORM_GNU_strp_alt:
22519 /* DW_STRING is already allocated on the objfile obstack, point
22520 directly to it. */
22521 result = (const gdb_byte *) DW_STRING (attr);
22522 *len = strlen (DW_STRING (attr));
22523 break;
22524 case DW_FORM_block1:
22525 case DW_FORM_block2:
22526 case DW_FORM_block4:
22527 case DW_FORM_block:
22528 case DW_FORM_exprloc:
22529 case DW_FORM_data16:
22530 result = DW_BLOCK (attr)->data;
22531 *len = DW_BLOCK (attr)->size;
22532 break;
22533
22534 /* The DW_AT_const_value attributes are supposed to carry the
22535 symbol's value "represented as it would be on the target
22536 architecture." By the time we get here, it's already been
22537 converted to host endianness, so we just need to sign- or
22538 zero-extend it as appropriate. */
22539 case DW_FORM_data1:
22540 type = die_type (die, cu);
22541 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22542 if (result == NULL)
22543 result = write_constant_as_bytes (obstack, byte_order,
22544 type, value, len);
22545 break;
22546 case DW_FORM_data2:
22547 type = die_type (die, cu);
22548 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22549 if (result == NULL)
22550 result = write_constant_as_bytes (obstack, byte_order,
22551 type, value, len);
22552 break;
22553 case DW_FORM_data4:
22554 type = die_type (die, cu);
22555 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22556 if (result == NULL)
22557 result = write_constant_as_bytes (obstack, byte_order,
22558 type, value, len);
22559 break;
22560 case DW_FORM_data8:
22561 type = die_type (die, cu);
22562 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22563 if (result == NULL)
22564 result = write_constant_as_bytes (obstack, byte_order,
22565 type, value, len);
22566 break;
22567
22568 case DW_FORM_sdata:
22569 case DW_FORM_implicit_const:
22570 type = die_type (die, cu);
22571 result = write_constant_as_bytes (obstack, byte_order,
22572 type, DW_SND (attr), len);
22573 break;
22574
22575 case DW_FORM_udata:
22576 type = die_type (die, cu);
22577 result = write_constant_as_bytes (obstack, byte_order,
22578 type, DW_UNSND (attr), len);
22579 break;
22580
22581 default:
22582 complaint (_("unsupported const value attribute form: '%s'"),
22583 dwarf_form_name (attr->form));
22584 break;
22585 }
22586
22587 return result;
22588 }
22589
22590 /* See read.h. */
22591
22592 struct type *
22593 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
22594 dwarf2_per_cu_data *per_cu,
22595 dwarf2_per_objfile *per_objfile)
22596 {
22597 struct die_info *die;
22598
22599 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
22600 if (cu == nullptr)
22601 cu = load_cu (per_cu, per_objfile, false);
22602
22603 if (cu == nullptr)
22604 return nullptr;
22605
22606 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
22607 if (!die)
22608 return NULL;
22609
22610 return die_type (die, cu);
22611 }
22612
22613 /* See read.h. */
22614
22615 struct type *
22616 dwarf2_get_die_type (cu_offset die_offset,
22617 dwarf2_per_cu_data *per_cu,
22618 dwarf2_per_objfile *per_objfile)
22619 {
22620 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
22621 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
22622 }
22623
22624 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
22625 On entry *REF_CU is the CU of SRC_DIE.
22626 On exit *REF_CU is the CU of the result.
22627 Returns NULL if the referenced DIE isn't found. */
22628
22629 static struct die_info *
22630 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22631 struct dwarf2_cu **ref_cu)
22632 {
22633 struct die_info temp_die;
22634 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
22635 struct die_info *die;
22636 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22637
22638
22639 /* While it might be nice to assert sig_type->type == NULL here,
22640 we can get here for DW_AT_imported_declaration where we need
22641 the DIE not the type. */
22642
22643 /* If necessary, add it to the queue and load its DIEs. */
22644
22645 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22646 language_minimal))
22647 read_signatured_type (sig_type, dwarf2_per_objfile);
22648
22649 sig_cu = dwarf2_per_objfile->get_cu (&sig_type->per_cu);
22650 gdb_assert (sig_cu != NULL);
22651 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22652 temp_die.sect_off = sig_type->type_offset_in_section;
22653 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
22654 to_underlying (temp_die.sect_off));
22655 if (die)
22656 {
22657 /* For .gdb_index version 7 keep track of included TUs.
22658 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22659 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22660 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
22661 {
22662 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
22663 }
22664
22665 *ref_cu = sig_cu;
22666 if (sig_cu != cu)
22667 sig_cu->ancestor = cu;
22668
22669 return die;
22670 }
22671
22672 return NULL;
22673 }
22674
22675 /* Follow signatured type referenced by ATTR in SRC_DIE.
22676 On entry *REF_CU is the CU of SRC_DIE.
22677 On exit *REF_CU is the CU of the result.
22678 The result is the DIE of the type.
22679 If the referenced type cannot be found an error is thrown. */
22680
22681 static struct die_info *
22682 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
22683 struct dwarf2_cu **ref_cu)
22684 {
22685 ULONGEST signature = DW_SIGNATURE (attr);
22686 struct signatured_type *sig_type;
22687 struct die_info *die;
22688
22689 gdb_assert (attr->form == DW_FORM_ref_sig8);
22690
22691 sig_type = lookup_signatured_type (*ref_cu, signature);
22692 /* sig_type will be NULL if the signatured type is missing from
22693 the debug info. */
22694 if (sig_type == NULL)
22695 {
22696 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22697 " from DIE at %s [in module %s]"),
22698 hex_string (signature), sect_offset_str (src_die->sect_off),
22699 objfile_name ((*ref_cu)->per_objfile->objfile));
22700 }
22701
22702 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22703 if (die == NULL)
22704 {
22705 dump_die_for_error (src_die);
22706 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22707 " from DIE at %s [in module %s]"),
22708 hex_string (signature), sect_offset_str (src_die->sect_off),
22709 objfile_name ((*ref_cu)->per_objfile->objfile));
22710 }
22711
22712 return die;
22713 }
22714
22715 /* Get the type specified by SIGNATURE referenced in DIE/CU,
22716 reading in and processing the type unit if necessary. */
22717
22718 static struct type *
22719 get_signatured_type (struct die_info *die, ULONGEST signature,
22720 struct dwarf2_cu *cu)
22721 {
22722 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22723 struct signatured_type *sig_type;
22724 struct dwarf2_cu *type_cu;
22725 struct die_info *type_die;
22726 struct type *type;
22727
22728 sig_type = lookup_signatured_type (cu, signature);
22729 /* sig_type will be NULL if the signatured type is missing from
22730 the debug info. */
22731 if (sig_type == NULL)
22732 {
22733 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
22734 " from DIE at %s [in module %s]"),
22735 hex_string (signature), sect_offset_str (die->sect_off),
22736 objfile_name (dwarf2_per_objfile->objfile));
22737 return build_error_marker_type (cu, die);
22738 }
22739
22740 /* If we already know the type we're done. */
22741 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22742 if (type != nullptr)
22743 return type;
22744
22745 type_cu = cu;
22746 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22747 if (type_die != NULL)
22748 {
22749 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22750 is created. This is important, for example, because for c++ classes
22751 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22752 type = read_type_die (type_die, type_cu);
22753 if (type == NULL)
22754 {
22755 complaint (_("Dwarf Error: Cannot build signatured type %s"
22756 " referenced from DIE at %s [in module %s]"),
22757 hex_string (signature), sect_offset_str (die->sect_off),
22758 objfile_name (dwarf2_per_objfile->objfile));
22759 type = build_error_marker_type (cu, die);
22760 }
22761 }
22762 else
22763 {
22764 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
22765 " from DIE at %s [in module %s]"),
22766 hex_string (signature), sect_offset_str (die->sect_off),
22767 objfile_name (dwarf2_per_objfile->objfile));
22768 type = build_error_marker_type (cu, die);
22769 }
22770
22771 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
22772
22773 return type;
22774 }
22775
22776 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22777 reading in and processing the type unit if necessary. */
22778
22779 static struct type *
22780 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
22781 struct dwarf2_cu *cu) /* ARI: editCase function */
22782 {
22783 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
22784 if (attr->form_is_ref ())
22785 {
22786 struct dwarf2_cu *type_cu = cu;
22787 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22788
22789 return read_type_die (type_die, type_cu);
22790 }
22791 else if (attr->form == DW_FORM_ref_sig8)
22792 {
22793 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22794 }
22795 else
22796 {
22797 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
22798
22799 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
22800 " at %s [in module %s]"),
22801 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
22802 objfile_name (dwarf2_per_objfile->objfile));
22803 return build_error_marker_type (cu, die);
22804 }
22805 }
22806
22807 /* Load the DIEs associated with type unit PER_CU into memory. */
22808
22809 static void
22810 load_full_type_unit (dwarf2_per_cu_data *per_cu,
22811 dwarf2_per_objfile *per_objfile)
22812 {
22813 struct signatured_type *sig_type;
22814
22815 /* Caller is responsible for ensuring type_unit_groups don't get here. */
22816 gdb_assert (! per_cu->type_unit_group_p ());
22817
22818 /* We have the per_cu, but we need the signatured_type.
22819 Fortunately this is an easy translation. */
22820 gdb_assert (per_cu->is_debug_types);
22821 sig_type = (struct signatured_type *) per_cu;
22822
22823 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22824
22825 read_signatured_type (sig_type, per_objfile);
22826
22827 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
22828 }
22829
22830 /* Read in a signatured type and build its CU and DIEs.
22831 If the type is a stub for the real type in a DWO file,
22832 read in the real type from the DWO file as well. */
22833
22834 static void
22835 read_signatured_type (signatured_type *sig_type,
22836 dwarf2_per_objfile *per_objfile)
22837 {
22838 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
22839
22840 gdb_assert (per_cu->is_debug_types);
22841 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
22842
22843 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
22844
22845 if (!reader.dummy_p)
22846 {
22847 struct dwarf2_cu *cu = reader.cu;
22848 const gdb_byte *info_ptr = reader.info_ptr;
22849
22850 gdb_assert (cu->die_hash == NULL);
22851 cu->die_hash =
22852 htab_create_alloc_ex (cu->header.length / 12,
22853 die_hash,
22854 die_eq,
22855 NULL,
22856 &cu->comp_unit_obstack,
22857 hashtab_obstack_allocate,
22858 dummy_obstack_deallocate);
22859
22860 if (reader.comp_unit_die->has_children)
22861 reader.comp_unit_die->child
22862 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22863 reader.comp_unit_die);
22864 cu->dies = reader.comp_unit_die;
22865 /* comp_unit_die is not stored in die_hash, no need. */
22866
22867 /* We try not to read any attributes in this function, because
22868 not all CUs needed for references have been loaded yet, and
22869 symbol table processing isn't initialized. But we have to
22870 set the CU language, or we won't be able to build types
22871 correctly. Similarly, if we do not read the producer, we can
22872 not apply producer-specific interpretation. */
22873 prepare_one_comp_unit (cu, cu->dies, language_minimal);
22874
22875 reader.keep ();
22876 }
22877
22878 sig_type->per_cu.tu_read = 1;
22879 }
22880
22881 /* Decode simple location descriptions.
22882 Given a pointer to a dwarf block that defines a location, compute
22883 the location and return the value. If COMPUTED is non-null, it is
22884 set to true to indicate that decoding was successful, and false
22885 otherwise. If COMPUTED is null, then this function may emit a
22886 complaint. */
22887
22888 static CORE_ADDR
22889 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
22890 {
22891 struct objfile *objfile = cu->per_objfile->objfile;
22892 size_t i;
22893 size_t size = blk->size;
22894 const gdb_byte *data = blk->data;
22895 CORE_ADDR stack[64];
22896 int stacki;
22897 unsigned int bytes_read, unsnd;
22898 gdb_byte op;
22899
22900 if (computed != nullptr)
22901 *computed = false;
22902
22903 i = 0;
22904 stacki = 0;
22905 stack[stacki] = 0;
22906 stack[++stacki] = 0;
22907
22908 while (i < size)
22909 {
22910 op = data[i++];
22911 switch (op)
22912 {
22913 case DW_OP_lit0:
22914 case DW_OP_lit1:
22915 case DW_OP_lit2:
22916 case DW_OP_lit3:
22917 case DW_OP_lit4:
22918 case DW_OP_lit5:
22919 case DW_OP_lit6:
22920 case DW_OP_lit7:
22921 case DW_OP_lit8:
22922 case DW_OP_lit9:
22923 case DW_OP_lit10:
22924 case DW_OP_lit11:
22925 case DW_OP_lit12:
22926 case DW_OP_lit13:
22927 case DW_OP_lit14:
22928 case DW_OP_lit15:
22929 case DW_OP_lit16:
22930 case DW_OP_lit17:
22931 case DW_OP_lit18:
22932 case DW_OP_lit19:
22933 case DW_OP_lit20:
22934 case DW_OP_lit21:
22935 case DW_OP_lit22:
22936 case DW_OP_lit23:
22937 case DW_OP_lit24:
22938 case DW_OP_lit25:
22939 case DW_OP_lit26:
22940 case DW_OP_lit27:
22941 case DW_OP_lit28:
22942 case DW_OP_lit29:
22943 case DW_OP_lit30:
22944 case DW_OP_lit31:
22945 stack[++stacki] = op - DW_OP_lit0;
22946 break;
22947
22948 case DW_OP_reg0:
22949 case DW_OP_reg1:
22950 case DW_OP_reg2:
22951 case DW_OP_reg3:
22952 case DW_OP_reg4:
22953 case DW_OP_reg5:
22954 case DW_OP_reg6:
22955 case DW_OP_reg7:
22956 case DW_OP_reg8:
22957 case DW_OP_reg9:
22958 case DW_OP_reg10:
22959 case DW_OP_reg11:
22960 case DW_OP_reg12:
22961 case DW_OP_reg13:
22962 case DW_OP_reg14:
22963 case DW_OP_reg15:
22964 case DW_OP_reg16:
22965 case DW_OP_reg17:
22966 case DW_OP_reg18:
22967 case DW_OP_reg19:
22968 case DW_OP_reg20:
22969 case DW_OP_reg21:
22970 case DW_OP_reg22:
22971 case DW_OP_reg23:
22972 case DW_OP_reg24:
22973 case DW_OP_reg25:
22974 case DW_OP_reg26:
22975 case DW_OP_reg27:
22976 case DW_OP_reg28:
22977 case DW_OP_reg29:
22978 case DW_OP_reg30:
22979 case DW_OP_reg31:
22980 stack[++stacki] = op - DW_OP_reg0;
22981 if (i < size)
22982 {
22983 if (computed == nullptr)
22984 dwarf2_complex_location_expr_complaint ();
22985 else
22986 return 0;
22987 }
22988 break;
22989
22990 case DW_OP_regx:
22991 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22992 i += bytes_read;
22993 stack[++stacki] = unsnd;
22994 if (i < size)
22995 {
22996 if (computed == nullptr)
22997 dwarf2_complex_location_expr_complaint ();
22998 else
22999 return 0;
23000 }
23001 break;
23002
23003 case DW_OP_addr:
23004 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
23005 &bytes_read);
23006 i += bytes_read;
23007 break;
23008
23009 case DW_OP_const1u:
23010 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23011 i += 1;
23012 break;
23013
23014 case DW_OP_const1s:
23015 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23016 i += 1;
23017 break;
23018
23019 case DW_OP_const2u:
23020 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23021 i += 2;
23022 break;
23023
23024 case DW_OP_const2s:
23025 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23026 i += 2;
23027 break;
23028
23029 case DW_OP_const4u:
23030 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23031 i += 4;
23032 break;
23033
23034 case DW_OP_const4s:
23035 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23036 i += 4;
23037 break;
23038
23039 case DW_OP_const8u:
23040 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23041 i += 8;
23042 break;
23043
23044 case DW_OP_constu:
23045 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23046 &bytes_read);
23047 i += bytes_read;
23048 break;
23049
23050 case DW_OP_consts:
23051 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23052 i += bytes_read;
23053 break;
23054
23055 case DW_OP_dup:
23056 stack[stacki + 1] = stack[stacki];
23057 stacki++;
23058 break;
23059
23060 case DW_OP_plus:
23061 stack[stacki - 1] += stack[stacki];
23062 stacki--;
23063 break;
23064
23065 case DW_OP_plus_uconst:
23066 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23067 &bytes_read);
23068 i += bytes_read;
23069 break;
23070
23071 case DW_OP_minus:
23072 stack[stacki - 1] -= stack[stacki];
23073 stacki--;
23074 break;
23075
23076 case DW_OP_deref:
23077 /* If we're not the last op, then we definitely can't encode
23078 this using GDB's address_class enum. This is valid for partial
23079 global symbols, although the variable's address will be bogus
23080 in the psymtab. */
23081 if (i < size)
23082 {
23083 if (computed == nullptr)
23084 dwarf2_complex_location_expr_complaint ();
23085 else
23086 return 0;
23087 }
23088 break;
23089
23090 case DW_OP_GNU_push_tls_address:
23091 case DW_OP_form_tls_address:
23092 /* The top of the stack has the offset from the beginning
23093 of the thread control block at which the variable is located. */
23094 /* Nothing should follow this operator, so the top of stack would
23095 be returned. */
23096 /* This is valid for partial global symbols, but the variable's
23097 address will be bogus in the psymtab. Make it always at least
23098 non-zero to not look as a variable garbage collected by linker
23099 which have DW_OP_addr 0. */
23100 if (i < size)
23101 {
23102 if (computed == nullptr)
23103 dwarf2_complex_location_expr_complaint ();
23104 else
23105 return 0;
23106 }
23107 stack[stacki]++;
23108 break;
23109
23110 case DW_OP_GNU_uninit:
23111 if (computed != nullptr)
23112 return 0;
23113 break;
23114
23115 case DW_OP_addrx:
23116 case DW_OP_GNU_addr_index:
23117 case DW_OP_GNU_const_index:
23118 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23119 &bytes_read);
23120 i += bytes_read;
23121 break;
23122
23123 default:
23124 if (computed == nullptr)
23125 {
23126 const char *name = get_DW_OP_name (op);
23127
23128 if (name)
23129 complaint (_("unsupported stack op: '%s'"),
23130 name);
23131 else
23132 complaint (_("unsupported stack op: '%02x'"),
23133 op);
23134 }
23135
23136 return (stack[stacki]);
23137 }
23138
23139 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23140 outside of the allocated space. Also enforce minimum>0. */
23141 if (stacki >= ARRAY_SIZE (stack) - 1)
23142 {
23143 if (computed == nullptr)
23144 complaint (_("location description stack overflow"));
23145 return 0;
23146 }
23147
23148 if (stacki <= 0)
23149 {
23150 if (computed == nullptr)
23151 complaint (_("location description stack underflow"));
23152 return 0;
23153 }
23154 }
23155
23156 if (computed != nullptr)
23157 *computed = true;
23158 return (stack[stacki]);
23159 }
23160
23161 /* memory allocation interface */
23162
23163 static struct dwarf_block *
23164 dwarf_alloc_block (struct dwarf2_cu *cu)
23165 {
23166 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
23167 }
23168
23169 static struct die_info *
23170 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
23171 {
23172 struct die_info *die;
23173 size_t size = sizeof (struct die_info);
23174
23175 if (num_attrs > 1)
23176 size += (num_attrs - 1) * sizeof (struct attribute);
23177
23178 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
23179 memset (die, 0, sizeof (struct die_info));
23180 return (die);
23181 }
23182
23183 \f
23184
23185 /* Macro support. */
23186
23187 /* An overload of dwarf_decode_macros that finds the correct section
23188 and ensures it is read in before calling the other overload. */
23189
23190 static void
23191 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23192 int section_is_gnu)
23193 {
23194 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23195 struct objfile *objfile = dwarf2_per_objfile->objfile;
23196 const struct line_header *lh = cu->line_header;
23197 unsigned int offset_size = cu->header.offset_size;
23198 struct dwarf2_section_info *section;
23199 const char *section_name;
23200
23201 if (cu->dwo_unit != nullptr)
23202 {
23203 if (section_is_gnu)
23204 {
23205 section = &cu->dwo_unit->dwo_file->sections.macro;
23206 section_name = ".debug_macro.dwo";
23207 }
23208 else
23209 {
23210 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23211 section_name = ".debug_macinfo.dwo";
23212 }
23213 }
23214 else
23215 {
23216 if (section_is_gnu)
23217 {
23218 section = &dwarf2_per_objfile->per_bfd->macro;
23219 section_name = ".debug_macro";
23220 }
23221 else
23222 {
23223 section = &dwarf2_per_objfile->per_bfd->macinfo;
23224 section_name = ".debug_macinfo";
23225 }
23226 }
23227
23228 section->read (objfile);
23229 if (section->buffer == nullptr)
23230 {
23231 complaint (_("missing %s section"), section_name);
23232 return;
23233 }
23234
23235 buildsym_compunit *builder = cu->get_builder ();
23236
23237 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23238 offset_size, offset, section_is_gnu);
23239 }
23240
23241 /* Return the .debug_loc section to use for CU.
23242 For DWO files use .debug_loc.dwo. */
23243
23244 static struct dwarf2_section_info *
23245 cu_debug_loc_section (struct dwarf2_cu *cu)
23246 {
23247 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23248
23249 if (cu->dwo_unit)
23250 {
23251 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
23252
23253 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23254 }
23255 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23256 : &dwarf2_per_objfile->per_bfd->loc);
23257 }
23258
23259 /* A helper function that fills in a dwarf2_loclist_baton. */
23260
23261 static void
23262 fill_in_loclist_baton (struct dwarf2_cu *cu,
23263 struct dwarf2_loclist_baton *baton,
23264 const struct attribute *attr)
23265 {
23266 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23267 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23268
23269 section->read (dwarf2_per_objfile->objfile);
23270
23271 baton->per_objfile = dwarf2_per_objfile;
23272 baton->per_cu = cu->per_cu;
23273 gdb_assert (baton->per_cu);
23274 /* We don't know how long the location list is, but make sure we
23275 don't run off the edge of the section. */
23276 baton->size = section->size - DW_UNSND (attr);
23277 baton->data = section->buffer + DW_UNSND (attr);
23278 if (cu->base_address.has_value ())
23279 baton->base_address = *cu->base_address;
23280 else
23281 baton->base_address = 0;
23282 baton->from_dwo = cu->dwo_unit != NULL;
23283 }
23284
23285 static void
23286 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
23287 struct dwarf2_cu *cu, int is_block)
23288 {
23289 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23290 struct objfile *objfile = dwarf2_per_objfile->objfile;
23291 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23292
23293 if (attr->form_is_section_offset ()
23294 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
23295 the section. If so, fall through to the complaint in the
23296 other branch. */
23297 && DW_UNSND (attr) < section->get_size (objfile))
23298 {
23299 struct dwarf2_loclist_baton *baton;
23300
23301 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
23302
23303 fill_in_loclist_baton (cu, baton, attr);
23304
23305 if (!cu->base_address.has_value ())
23306 complaint (_("Location list used without "
23307 "specifying the CU base address."));
23308
23309 SYMBOL_ACLASS_INDEX (sym) = (is_block
23310 ? dwarf2_loclist_block_index
23311 : dwarf2_loclist_index);
23312 SYMBOL_LOCATION_BATON (sym) = baton;
23313 }
23314 else
23315 {
23316 struct dwarf2_locexpr_baton *baton;
23317
23318 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
23319 baton->per_objfile = dwarf2_per_objfile;
23320 baton->per_cu = cu->per_cu;
23321 gdb_assert (baton->per_cu);
23322
23323 if (attr->form_is_block ())
23324 {
23325 /* Note that we're just copying the block's data pointer
23326 here, not the actual data. We're still pointing into the
23327 info_buffer for SYM's objfile; right now we never release
23328 that buffer, but when we do clean up properly this may
23329 need to change. */
23330 baton->size = DW_BLOCK (attr)->size;
23331 baton->data = DW_BLOCK (attr)->data;
23332 }
23333 else
23334 {
23335 dwarf2_invalid_attrib_class_complaint ("location description",
23336 sym->natural_name ());
23337 baton->size = 0;
23338 }
23339
23340 SYMBOL_ACLASS_INDEX (sym) = (is_block
23341 ? dwarf2_locexpr_block_index
23342 : dwarf2_locexpr_index);
23343 SYMBOL_LOCATION_BATON (sym) = baton;
23344 }
23345 }
23346
23347 /* See read.h. */
23348
23349 const comp_unit_head *
23350 dwarf2_per_cu_data::get_header () const
23351 {
23352 if (!m_header_read_in)
23353 {
23354 const gdb_byte *info_ptr
23355 = this->section->buffer + to_underlying (this->sect_off);
23356
23357 memset (&m_header, 0, sizeof (m_header));
23358
23359 read_comp_unit_head (&m_header, info_ptr, this->section,
23360 rcuh_kind::COMPILE);
23361 }
23362
23363 return &m_header;
23364 }
23365
23366 /* See read.h. */
23367
23368 int
23369 dwarf2_per_cu_data::addr_size () const
23370 {
23371 return this->get_header ()->addr_size;
23372 }
23373
23374 /* See read.h. */
23375
23376 int
23377 dwarf2_per_cu_data::offset_size () const
23378 {
23379 return this->get_header ()->offset_size;
23380 }
23381
23382 /* See read.h. */
23383
23384 int
23385 dwarf2_per_cu_data::ref_addr_size () const
23386 {
23387 const comp_unit_head *header = this->get_header ();
23388
23389 if (header->version == 2)
23390 return header->addr_size;
23391 else
23392 return header->offset_size;
23393 }
23394
23395 /* See read.h. */
23396
23397 struct type *
23398 dwarf2_cu::addr_type () const
23399 {
23400 struct objfile *objfile = this->per_objfile->objfile;
23401 struct type *void_type = objfile_type (objfile)->builtin_void;
23402 struct type *addr_type = lookup_pointer_type (void_type);
23403 int addr_size = this->per_cu->addr_size ();
23404
23405 if (TYPE_LENGTH (addr_type) == addr_size)
23406 return addr_type;
23407
23408 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
23409 return addr_type;
23410 }
23411
23412 /* A helper function for dwarf2_find_containing_comp_unit that returns
23413 the index of the result, and that searches a vector. It will
23414 return a result even if the offset in question does not actually
23415 occur in any CU. This is separate so that it can be unit
23416 tested. */
23417
23418 static int
23419 dwarf2_find_containing_comp_unit
23420 (sect_offset sect_off,
23421 unsigned int offset_in_dwz,
23422 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
23423 {
23424 int low, high;
23425
23426 low = 0;
23427 high = all_comp_units.size () - 1;
23428 while (high > low)
23429 {
23430 struct dwarf2_per_cu_data *mid_cu;
23431 int mid = low + (high - low) / 2;
23432
23433 mid_cu = all_comp_units[mid];
23434 if (mid_cu->is_dwz > offset_in_dwz
23435 || (mid_cu->is_dwz == offset_in_dwz
23436 && mid_cu->sect_off + mid_cu->length > sect_off))
23437 high = mid;
23438 else
23439 low = mid + 1;
23440 }
23441 gdb_assert (low == high);
23442 return low;
23443 }
23444
23445 /* Locate the .debug_info compilation unit from CU's objfile which contains
23446 the DIE at OFFSET. Raises an error on failure. */
23447
23448 static struct dwarf2_per_cu_data *
23449 dwarf2_find_containing_comp_unit (sect_offset sect_off,
23450 unsigned int offset_in_dwz,
23451 struct dwarf2_per_objfile *dwarf2_per_objfile)
23452 {
23453 int low
23454 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23455 dwarf2_per_objfile->per_bfd->all_comp_units);
23456 struct dwarf2_per_cu_data *this_cu
23457 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
23458
23459 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
23460 {
23461 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
23462 error (_("Dwarf Error: could not find partial DIE containing "
23463 "offset %s [in module %s]"),
23464 sect_offset_str (sect_off),
23465 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
23466
23467 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
23468 <= sect_off);
23469 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
23470 }
23471 else
23472 {
23473 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
23474 && sect_off >= this_cu->sect_off + this_cu->length)
23475 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
23476 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
23477 return this_cu;
23478 }
23479 }
23480
23481 #if GDB_SELF_TEST
23482
23483 namespace selftests {
23484 namespace find_containing_comp_unit {
23485
23486 static void
23487 run_test ()
23488 {
23489 struct dwarf2_per_cu_data one {};
23490 struct dwarf2_per_cu_data two {};
23491 struct dwarf2_per_cu_data three {};
23492 struct dwarf2_per_cu_data four {};
23493
23494 one.length = 5;
23495 two.sect_off = sect_offset (one.length);
23496 two.length = 7;
23497
23498 three.length = 5;
23499 three.is_dwz = 1;
23500 four.sect_off = sect_offset (three.length);
23501 four.length = 7;
23502 four.is_dwz = 1;
23503
23504 std::vector<dwarf2_per_cu_data *> units;
23505 units.push_back (&one);
23506 units.push_back (&two);
23507 units.push_back (&three);
23508 units.push_back (&four);
23509
23510 int result;
23511
23512 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23513 SELF_CHECK (units[result] == &one);
23514 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23515 SELF_CHECK (units[result] == &one);
23516 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23517 SELF_CHECK (units[result] == &two);
23518
23519 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23520 SELF_CHECK (units[result] == &three);
23521 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23522 SELF_CHECK (units[result] == &three);
23523 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23524 SELF_CHECK (units[result] == &four);
23525 }
23526
23527 }
23528 }
23529
23530 #endif /* GDB_SELF_TEST */
23531
23532 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
23533
23534 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23535 dwarf2_per_objfile *per_objfile)
23536 : per_cu (per_cu),
23537 per_objfile (per_objfile),
23538 mark (false),
23539 has_loclist (false),
23540 checked_producer (false),
23541 producer_is_gxx_lt_4_6 (false),
23542 producer_is_gcc_lt_4_3 (false),
23543 producer_is_icc (false),
23544 producer_is_icc_lt_14 (false),
23545 producer_is_codewarrior (false),
23546 processing_has_namespace_info (false)
23547 {
23548 }
23549
23550 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23551
23552 static void
23553 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23554 enum language pretend_language)
23555 {
23556 struct attribute *attr;
23557
23558 /* Set the language we're debugging. */
23559 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
23560 if (attr != nullptr)
23561 set_cu_language (DW_UNSND (attr), cu);
23562 else
23563 {
23564 cu->language = pretend_language;
23565 cu->language_defn = language_def (cu->language);
23566 }
23567
23568 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
23569 }
23570
23571 /* See read.h. */
23572
23573 dwarf2_cu *
23574 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
23575 {
23576 auto it = m_dwarf2_cus.find (per_cu);
23577 if (it == m_dwarf2_cus.end ())
23578 return nullptr;
23579
23580 return it->second;
23581 }
23582
23583 /* See read.h. */
23584
23585 void
23586 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23587 {
23588 gdb_assert (this->get_cu (per_cu) == nullptr);
23589
23590 m_dwarf2_cus[per_cu] = cu;
23591 }
23592
23593 /* See read.h. */
23594
23595 void
23596 dwarf2_per_objfile::age_comp_units ()
23597 {
23598 /* Start by clearing all marks. */
23599 for (auto pair : m_dwarf2_cus)
23600 pair.second->mark = false;
23601
23602 /* Traverse all CUs, mark them and their dependencies if used recently
23603 enough. */
23604 for (auto pair : m_dwarf2_cus)
23605 {
23606 dwarf2_cu *cu = pair.second;
23607
23608 cu->last_used++;
23609 if (cu->last_used <= dwarf_max_cache_age)
23610 dwarf2_mark (cu);
23611 }
23612
23613 /* Delete all CUs still not marked. */
23614 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
23615 {
23616 dwarf2_cu *cu = it->second;
23617
23618 if (!cu->mark)
23619 {
23620 delete cu;
23621 it = m_dwarf2_cus.erase (it);
23622 }
23623 else
23624 it++;
23625 }
23626 }
23627
23628 /* See read.h. */
23629
23630 void
23631 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
23632 {
23633 auto it = m_dwarf2_cus.find (per_cu);
23634 if (it == m_dwarf2_cus.end ())
23635 return;
23636
23637 delete it->second;
23638
23639 m_dwarf2_cus.erase (it);
23640 }
23641
23642 dwarf2_per_objfile::~dwarf2_per_objfile ()
23643 {
23644 remove_all_cus ();
23645 }
23646
23647 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23648 We store these in a hash table separate from the DIEs, and preserve them
23649 when the DIEs are flushed out of cache.
23650
23651 The CU "per_cu" pointer is needed because offset alone is not enough to
23652 uniquely identify the type. A file may have multiple .debug_types sections,
23653 or the type may come from a DWO file. Furthermore, while it's more logical
23654 to use per_cu->section+offset, with Fission the section with the data is in
23655 the DWO file but we don't know that section at the point we need it.
23656 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23657 because we can enter the lookup routine, get_die_type_at_offset, from
23658 outside this file, and thus won't necessarily have PER_CU->cu.
23659 Fortunately, PER_CU is stable for the life of the objfile. */
23660
23661 struct dwarf2_per_cu_offset_and_type
23662 {
23663 const struct dwarf2_per_cu_data *per_cu;
23664 sect_offset sect_off;
23665 struct type *type;
23666 };
23667
23668 /* Hash function for a dwarf2_per_cu_offset_and_type. */
23669
23670 static hashval_t
23671 per_cu_offset_and_type_hash (const void *item)
23672 {
23673 const struct dwarf2_per_cu_offset_and_type *ofs
23674 = (const struct dwarf2_per_cu_offset_and_type *) item;
23675
23676 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
23677 }
23678
23679 /* Equality function for a dwarf2_per_cu_offset_and_type. */
23680
23681 static int
23682 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
23683 {
23684 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23685 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23686 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23687 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
23688
23689 return (ofs_lhs->per_cu == ofs_rhs->per_cu
23690 && ofs_lhs->sect_off == ofs_rhs->sect_off);
23691 }
23692
23693 /* Set the type associated with DIE to TYPE. Save it in CU's hash
23694 table if necessary. For convenience, return TYPE.
23695
23696 The DIEs reading must have careful ordering to:
23697 * Not cause infinite loops trying to read in DIEs as a prerequisite for
23698 reading current DIE.
23699 * Not trying to dereference contents of still incompletely read in types
23700 while reading in other DIEs.
23701 * Enable referencing still incompletely read in types just by a pointer to
23702 the type without accessing its fields.
23703
23704 Therefore caller should follow these rules:
23705 * Try to fetch any prerequisite types we may need to build this DIE type
23706 before building the type and calling set_die_type.
23707 * After building type call set_die_type for current DIE as soon as
23708 possible before fetching more types to complete the current type.
23709 * Make the type as complete as possible before fetching more types. */
23710
23711 static struct type *
23712 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23713 {
23714 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
23715 struct dwarf2_per_cu_offset_and_type **slot, ofs;
23716 struct objfile *objfile = dwarf2_per_objfile->objfile;
23717 struct attribute *attr;
23718 struct dynamic_prop prop;
23719
23720 /* For Ada types, make sure that the gnat-specific data is always
23721 initialized (if not already set). There are a few types where
23722 we should not be doing so, because the type-specific area is
23723 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23724 where the type-specific area is used to store the floatformat).
23725 But this is not a problem, because the gnat-specific information
23726 is actually not needed for these types. */
23727 if (need_gnat_info (cu)
23728 && type->code () != TYPE_CODE_FUNC
23729 && type->code () != TYPE_CODE_FLT
23730 && type->code () != TYPE_CODE_METHODPTR
23731 && type->code () != TYPE_CODE_MEMBERPTR
23732 && type->code () != TYPE_CODE_METHOD
23733 && !HAVE_GNAT_AUX_INFO (type))
23734 INIT_GNAT_SPECIFIC (type);
23735
23736 /* Read DW_AT_allocated and set in type. */
23737 attr = dwarf2_attr (die, DW_AT_allocated, cu);
23738 if (attr != NULL && attr->form_is_block ())
23739 {
23740 struct type *prop_type = cu->addr_sized_int_type (false);
23741 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23742 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
23743 }
23744 else if (attr != NULL)
23745 {
23746 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
23747 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23748 sect_offset_str (die->sect_off));
23749 }
23750
23751 /* Read DW_AT_associated and set in type. */
23752 attr = dwarf2_attr (die, DW_AT_associated, cu);
23753 if (attr != NULL && attr->form_is_block ())
23754 {
23755 struct type *prop_type = cu->addr_sized_int_type (false);
23756 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
23757 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
23758 }
23759 else if (attr != NULL)
23760 {
23761 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
23762 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
23763 sect_offset_str (die->sect_off));
23764 }
23765
23766 /* Read DW_AT_data_location and set in type. */
23767 attr = dwarf2_attr (die, DW_AT_data_location, cu);
23768 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
23769 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
23770
23771 if (dwarf2_per_objfile->die_type_hash == NULL)
23772 dwarf2_per_objfile->die_type_hash
23773 = htab_up (htab_create_alloc (127,
23774 per_cu_offset_and_type_hash,
23775 per_cu_offset_and_type_eq,
23776 NULL, xcalloc, xfree));
23777
23778 ofs.per_cu = cu->per_cu;
23779 ofs.sect_off = die->sect_off;
23780 ofs.type = type;
23781 slot = (struct dwarf2_per_cu_offset_and_type **)
23782 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
23783 if (*slot)
23784 complaint (_("A problem internal to GDB: DIE %s has type already set"),
23785 sect_offset_str (die->sect_off));
23786 *slot = XOBNEW (&objfile->objfile_obstack,
23787 struct dwarf2_per_cu_offset_and_type);
23788 **slot = ofs;
23789 return type;
23790 }
23791
23792 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
23793 or return NULL if the die does not have a saved type. */
23794
23795 static struct type *
23796 get_die_type_at_offset (sect_offset sect_off,
23797 dwarf2_per_cu_data *per_cu,
23798 dwarf2_per_objfile *dwarf2_per_objfile)
23799 {
23800 struct dwarf2_per_cu_offset_and_type *slot, ofs;
23801
23802 if (dwarf2_per_objfile->die_type_hash == NULL)
23803 return NULL;
23804
23805 ofs.per_cu = per_cu;
23806 ofs.sect_off = sect_off;
23807 slot = ((struct dwarf2_per_cu_offset_and_type *)
23808 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
23809 if (slot)
23810 return slot->type;
23811 else
23812 return NULL;
23813 }
23814
23815 /* Look up the type for DIE in CU in die_type_hash,
23816 or return NULL if DIE does not have a saved type. */
23817
23818 static struct type *
23819 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23820 {
23821 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
23822 }
23823
23824 /* Add a dependence relationship from CU to REF_PER_CU. */
23825
23826 static void
23827 dwarf2_add_dependence (struct dwarf2_cu *cu,
23828 struct dwarf2_per_cu_data *ref_per_cu)
23829 {
23830 void **slot;
23831
23832 if (cu->dependencies == NULL)
23833 cu->dependencies
23834 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23835 NULL, &cu->comp_unit_obstack,
23836 hashtab_obstack_allocate,
23837 dummy_obstack_deallocate);
23838
23839 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23840 if (*slot == NULL)
23841 *slot = ref_per_cu;
23842 }
23843
23844 /* Subroutine of dwarf2_mark to pass to htab_traverse.
23845 Set the mark field in every compilation unit in the
23846 cache that we must keep because we are keeping CU.
23847
23848 DATA is the dwarf2_per_objfile object in which to look up CUs. */
23849
23850 static int
23851 dwarf2_mark_helper (void **slot, void *data)
23852 {
23853 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23854 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23855 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23856
23857 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23858 reading of the chain. As such dependencies remain valid it is not much
23859 useful to track and undo them during QUIT cleanups. */
23860 if (cu == nullptr)
23861 return 1;
23862
23863 if (cu->mark)
23864 return 1;
23865
23866 cu->mark = true;
23867
23868 if (cu->dependencies != nullptr)
23869 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
23870
23871 return 1;
23872 }
23873
23874 /* Set the mark field in CU and in every other compilation unit in the
23875 cache that we must keep because we are keeping CU. */
23876
23877 static void
23878 dwarf2_mark (struct dwarf2_cu *cu)
23879 {
23880 if (cu->mark)
23881 return;
23882
23883 cu->mark = true;
23884
23885 if (cu->dependencies != nullptr)
23886 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
23887 }
23888
23889 /* Trivial hash function for partial_die_info: the hash value of a DIE
23890 is its offset in .debug_info for this objfile. */
23891
23892 static hashval_t
23893 partial_die_hash (const void *item)
23894 {
23895 const struct partial_die_info *part_die
23896 = (const struct partial_die_info *) item;
23897
23898 return to_underlying (part_die->sect_off);
23899 }
23900
23901 /* Trivial comparison function for partial_die_info structures: two DIEs
23902 are equal if they have the same offset. */
23903
23904 static int
23905 partial_die_eq (const void *item_lhs, const void *item_rhs)
23906 {
23907 const struct partial_die_info *part_die_lhs
23908 = (const struct partial_die_info *) item_lhs;
23909 const struct partial_die_info *part_die_rhs
23910 = (const struct partial_die_info *) item_rhs;
23911
23912 return part_die_lhs->sect_off == part_die_rhs->sect_off;
23913 }
23914
23915 struct cmd_list_element *set_dwarf_cmdlist;
23916 struct cmd_list_element *show_dwarf_cmdlist;
23917
23918 static void
23919 show_check_physname (struct ui_file *file, int from_tty,
23920 struct cmd_list_element *c, const char *value)
23921 {
23922 fprintf_filtered (file,
23923 _("Whether to check \"physname\" is %s.\n"),
23924 value);
23925 }
23926
23927 void _initialize_dwarf2_read ();
23928 void
23929 _initialize_dwarf2_read ()
23930 {
23931 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
23932 Set DWARF specific variables.\n\
23933 Configure DWARF variables such as the cache size."),
23934 &set_dwarf_cmdlist, "maintenance set dwarf ",
23935 0/*allow-unknown*/, &maintenance_set_cmdlist);
23936
23937 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
23938 Show DWARF specific variables.\n\
23939 Show DWARF variables such as the cache size."),
23940 &show_dwarf_cmdlist, "maintenance show dwarf ",
23941 0/*allow-unknown*/, &maintenance_show_cmdlist);
23942
23943 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23944 &dwarf_max_cache_age, _("\
23945 Set the upper bound on the age of cached DWARF compilation units."), _("\
23946 Show the upper bound on the age of cached DWARF compilation units."), _("\
23947 A higher limit means that cached compilation units will be stored\n\
23948 in memory longer, and more total memory will be used. Zero disables\n\
23949 caching, which can slow down startup."),
23950 NULL,
23951 show_dwarf_max_cache_age,
23952 &set_dwarf_cmdlist,
23953 &show_dwarf_cmdlist);
23954
23955 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23956 Set debugging of the DWARF reader."), _("\
23957 Show debugging of the DWARF reader."), _("\
23958 When enabled (non-zero), debugging messages are printed during DWARF\n\
23959 reading and symtab expansion. A value of 1 (one) provides basic\n\
23960 information. A value greater than 1 provides more verbose information."),
23961 NULL,
23962 NULL,
23963 &setdebuglist, &showdebuglist);
23964
23965 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23966 Set debugging of the DWARF DIE reader."), _("\
23967 Show debugging of the DWARF DIE reader."), _("\
23968 When enabled (non-zero), DIEs are dumped after they are read in.\n\
23969 The value is the maximum depth to print."),
23970 NULL,
23971 NULL,
23972 &setdebuglist, &showdebuglist);
23973
23974 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23975 Set debugging of the dwarf line reader."), _("\
23976 Show debugging of the dwarf line reader."), _("\
23977 When enabled (non-zero), line number entries are dumped as they are read in.\n\
23978 A value of 1 (one) provides basic information.\n\
23979 A value greater than 1 provides more verbose information."),
23980 NULL,
23981 NULL,
23982 &setdebuglist, &showdebuglist);
23983
23984 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23985 Set cross-checking of \"physname\" code against demangler."), _("\
23986 Show cross-checking of \"physname\" code against demangler."), _("\
23987 When enabled, GDB's internal \"physname\" code is checked against\n\
23988 the demangler."),
23989 NULL, show_check_physname,
23990 &setdebuglist, &showdebuglist);
23991
23992 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23993 no_class, &use_deprecated_index_sections, _("\
23994 Set whether to use deprecated gdb_index sections."), _("\
23995 Show whether to use deprecated gdb_index sections."), _("\
23996 When enabled, deprecated .gdb_index sections are used anyway.\n\
23997 Normally they are ignored either because of a missing feature or\n\
23998 performance issue.\n\
23999 Warning: This option must be enabled before gdb reads the file."),
24000 NULL,
24001 NULL,
24002 &setlist, &showlist);
24003
24004 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
24005 &dwarf2_locexpr_funcs);
24006 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
24007 &dwarf2_loclist_funcs);
24008
24009 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
24010 &dwarf2_block_frame_base_locexpr_funcs);
24011 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
24012 &dwarf2_block_frame_base_loclist_funcs);
24013
24014 #if GDB_SELF_TEST
24015 selftests::register_test ("dw2_expand_symtabs_matching",
24016 selftests::dw2_expand_symtabs_matching::run_test);
24017 selftests::register_test ("dwarf2_find_containing_comp_unit",
24018 selftests::find_containing_comp_unit::run_test);
24019 #endif
24020 }
This page took 0.582392 seconds and 4 git commands to generate.