b36694e54fe1bcff3b7a1f73c6a8c6091ae33e11
[binutils-gdb.git] / gdb / dwarf2 / read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2021 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 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
97
98 #define dwarf_read_debug_printf(fmt, ...) \
99 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
100 ##__VA_ARGS__)
101
102 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
103
104 #define dwarf_read_debug_printf_v(fmt, ...) \
105 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
107
108 /* When non-zero, dump DIEs after they are read in. */
109 static unsigned int dwarf_die_debug = 0;
110
111 /* When non-zero, dump line number entries as they are read in. */
112 unsigned int dwarf_line_debug = 0;
113
114 /* When true, cross-check physname against demangler. */
115 static bool check_physname = false;
116
117 /* When true, do not reject deprecated .gdb_index sections. */
118 static bool use_deprecated_index_sections = false;
119
120 /* This is used to store the data that is always per objfile. */
121 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
122
123 /* These are used to store the dwarf2_per_bfd objects.
124
125 objfiles having the same BFD, which doesn't require relocations, are going to
126 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
127
128 Other objfiles are not going to share a dwarf2_per_bfd with any other
129 objfiles, so they'll have their own version kept in the _objfile_data_key
130 version. */
131 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
132 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
133
134 /* The "aclass" indices for various kinds of computed DWARF symbols. */
135
136 static int dwarf2_locexpr_index;
137 static int dwarf2_loclist_index;
138 static int dwarf2_locexpr_block_index;
139 static int dwarf2_loclist_block_index;
140
141 /* Size of .debug_loclists section header for 32-bit DWARF format. */
142 #define LOCLIST_HEADER_SIZE32 12
143
144 /* Size of .debug_loclists section header for 64-bit DWARF format. */
145 #define LOCLIST_HEADER_SIZE64 20
146
147 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
148 #define RNGLIST_HEADER_SIZE32 12
149
150 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
151 #define RNGLIST_HEADER_SIZE64 20
152
153 /* An index into a (C++) symbol name component in a symbol name as
154 recorded in the mapped_index's symbol table. For each C++ symbol
155 in the symbol table, we record one entry for the start of each
156 component in the symbol in a table of name components, and then
157 sort the table, in order to be able to binary search symbol names,
158 ignoring leading namespaces, both completion and regular look up.
159 For example, for symbol "A::B::C", we'll have an entry that points
160 to "A::B::C", another that points to "B::C", and another for "C".
161 Note that function symbols in GDB index have no parameter
162 information, just the function/method names. You can convert a
163 name_component to a "const char *" using the
164 'mapped_index::symbol_name_at(offset_type)' method. */
165
166 struct name_component
167 {
168 /* Offset in the symbol name where the component starts. Stored as
169 a (32-bit) offset instead of a pointer to save memory and improve
170 locality on 64-bit architectures. */
171 offset_type name_offset;
172
173 /* The symbol's index in the symbol and constant pool tables of a
174 mapped_index. */
175 offset_type idx;
176 };
177
178 /* Base class containing bits shared by both .gdb_index and
179 .debug_name indexes. */
180
181 struct mapped_index_base
182 {
183 mapped_index_base () = default;
184 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
185
186 /* The name_component table (a sorted vector). See name_component's
187 description above. */
188 std::vector<name_component> name_components;
189
190 /* How NAME_COMPONENTS is sorted. */
191 enum case_sensitivity name_components_casing;
192
193 /* Return the number of names in the symbol table. */
194 virtual size_t symbol_name_count () const = 0;
195
196 /* Get the name of the symbol at IDX in the symbol table. */
197 virtual const char *symbol_name_at
198 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
199
200 /* Return whether the name at IDX in the symbol table should be
201 ignored. */
202 virtual bool symbol_name_slot_invalid (offset_type idx) const
203 {
204 return false;
205 }
206
207 /* Build the symbol name component sorted vector, if we haven't
208 yet. */
209 void build_name_components (dwarf2_per_objfile *per_objfile);
210
211 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
212 possible matches for LN_NO_PARAMS in the name component
213 vector. */
214 std::pair<std::vector<name_component>::const_iterator,
215 std::vector<name_component>::const_iterator>
216 find_name_components_bounds (const lookup_name_info &ln_no_params,
217 enum language lang,
218 dwarf2_per_objfile *per_objfile) const;
219
220 /* Prevent deleting/destroying via a base class pointer. */
221 protected:
222 ~mapped_index_base() = default;
223 };
224
225 /* A description of the mapped index. The file format is described in
226 a comment by the code that writes the index. */
227 struct mapped_index final : public mapped_index_base
228 {
229 /* A slot/bucket in the symbol table hash. */
230 struct symbol_table_slot
231 {
232 const offset_type name;
233 const offset_type vec;
234 };
235
236 /* Index data format version. */
237 int version = 0;
238
239 /* The address table data. */
240 gdb::array_view<const gdb_byte> address_table;
241
242 /* The symbol table, implemented as a hash table. */
243 gdb::array_view<symbol_table_slot> symbol_table;
244
245 /* A pointer to the constant pool. */
246 const char *constant_pool = nullptr;
247
248 bool symbol_name_slot_invalid (offset_type idx) const override
249 {
250 const auto &bucket = this->symbol_table[idx];
251 return bucket.name == 0 && bucket.vec == 0;
252 }
253
254 /* Convenience method to get at the name of the symbol at IDX in the
255 symbol table. */
256 const char *symbol_name_at
257 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
258 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
259
260 size_t symbol_name_count () const override
261 { return this->symbol_table.size (); }
262 };
263
264 /* A description of the mapped .debug_names.
265 Uninitialized map has CU_COUNT 0. */
266 struct mapped_debug_names final : public mapped_index_base
267 {
268 bfd_endian dwarf5_byte_order;
269 bool dwarf5_is_dwarf64;
270 bool augmentation_is_gdb;
271 uint8_t offset_size;
272 uint32_t cu_count = 0;
273 uint32_t tu_count, bucket_count, name_count;
274 const gdb_byte *cu_table_reordered, *tu_table_reordered;
275 const uint32_t *bucket_table_reordered, *hash_table_reordered;
276 const gdb_byte *name_table_string_offs_reordered;
277 const gdb_byte *name_table_entry_offs_reordered;
278 const gdb_byte *entry_pool;
279
280 struct index_val
281 {
282 ULONGEST dwarf_tag;
283 struct attr
284 {
285 /* Attribute name DW_IDX_*. */
286 ULONGEST dw_idx;
287
288 /* Attribute form DW_FORM_*. */
289 ULONGEST form;
290
291 /* Value if FORM is DW_FORM_implicit_const. */
292 LONGEST implicit_const;
293 };
294 std::vector<attr> attr_vec;
295 };
296
297 std::unordered_map<ULONGEST, index_val> abbrev_map;
298
299 const char *namei_to_name
300 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
301
302 /* Implementation of the mapped_index_base virtual interface, for
303 the name_components cache. */
304
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 { return namei_to_name (idx, per_objfile); }
308
309 size_t symbol_name_count () const override
310 { return this->name_count; }
311 };
312
313 /* See dwarf2read.h. */
314
315 dwarf2_per_objfile *
316 get_dwarf2_per_objfile (struct objfile *objfile)
317 {
318 return dwarf2_objfile_data_key.get (objfile);
319 }
320
321 /* Default names of the debugging sections. */
322
323 /* Note that if the debugging section has been compressed, it might
324 have a name like .zdebug_info. */
325
326 static const struct dwarf2_debug_sections dwarf2_elf_names =
327 {
328 { ".debug_info", ".zdebug_info" },
329 { ".debug_abbrev", ".zdebug_abbrev" },
330 { ".debug_line", ".zdebug_line" },
331 { ".debug_loc", ".zdebug_loc" },
332 { ".debug_loclists", ".zdebug_loclists" },
333 { ".debug_macinfo", ".zdebug_macinfo" },
334 { ".debug_macro", ".zdebug_macro" },
335 { ".debug_str", ".zdebug_str" },
336 { ".debug_str_offsets", ".zdebug_str_offsets" },
337 { ".debug_line_str", ".zdebug_line_str" },
338 { ".debug_ranges", ".zdebug_ranges" },
339 { ".debug_rnglists", ".zdebug_rnglists" },
340 { ".debug_types", ".zdebug_types" },
341 { ".debug_addr", ".zdebug_addr" },
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
344 { ".gdb_index", ".zgdb_index" },
345 { ".debug_names", ".zdebug_names" },
346 { ".debug_aranges", ".zdebug_aranges" },
347 23
348 };
349
350 /* List of DWO/DWP sections. */
351
352 static const struct dwop_section_names
353 {
354 struct dwarf2_section_names abbrev_dwo;
355 struct dwarf2_section_names info_dwo;
356 struct dwarf2_section_names line_dwo;
357 struct dwarf2_section_names loc_dwo;
358 struct dwarf2_section_names loclists_dwo;
359 struct dwarf2_section_names macinfo_dwo;
360 struct dwarf2_section_names macro_dwo;
361 struct dwarf2_section_names rnglists_dwo;
362 struct dwarf2_section_names str_dwo;
363 struct dwarf2_section_names str_offsets_dwo;
364 struct dwarf2_section_names types_dwo;
365 struct dwarf2_section_names cu_index;
366 struct dwarf2_section_names tu_index;
367 }
368 dwop_section_names =
369 {
370 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
371 { ".debug_info.dwo", ".zdebug_info.dwo" },
372 { ".debug_line.dwo", ".zdebug_line.dwo" },
373 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
374 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
375 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
376 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
377 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
378 { ".debug_str.dwo", ".zdebug_str.dwo" },
379 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
380 { ".debug_types.dwo", ".zdebug_types.dwo" },
381 { ".debug_cu_index", ".zdebug_cu_index" },
382 { ".debug_tu_index", ".zdebug_tu_index" },
383 };
384
385 /* local data types */
386
387 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
388 begin with a header, which contains the following information. */
389 struct loclists_rnglists_header
390 {
391 /* A 4-byte or 12-byte length containing the length of the
392 set of entries for this compilation unit, not including the
393 length field itself. */
394 unsigned int length;
395
396 /* A 2-byte version identifier. */
397 short version;
398
399 /* A 1-byte unsigned integer containing the size in bytes of an address on
400 the target system. */
401 unsigned char addr_size;
402
403 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
404 on the target system. */
405 unsigned char segment_collector_size;
406
407 /* A 4-byte count of the number of offsets that follow the header. */
408 unsigned int offset_entry_count;
409 };
410
411 /* Type used for delaying computation of method physnames.
412 See comments for compute_delayed_physnames. */
413 struct delayed_method_info
414 {
415 /* The type to which the method is attached, i.e., its parent class. */
416 struct type *type;
417
418 /* The index of the method in the type's function fieldlists. */
419 int fnfield_index;
420
421 /* The index of the method in the fieldlist. */
422 int index;
423
424 /* The name of the DIE. */
425 const char *name;
426
427 /* The DIE associated with this method. */
428 struct die_info *die;
429 };
430
431 /* Internal state when decoding a particular compilation unit. */
432 struct dwarf2_cu
433 {
434 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
435 dwarf2_per_objfile *per_objfile);
436
437 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
438
439 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
440 Create the set of symtabs used by this TU, or if this TU is sharing
441 symtabs with another TU and the symtabs have already been created
442 then restore those symtabs in the line header.
443 We don't need the pc/line-number mapping for type units. */
444 void setup_type_unit_groups (struct die_info *die);
445
446 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
447 buildsym_compunit constructor. */
448 struct compunit_symtab *start_symtab (const char *name,
449 const char *comp_dir,
450 CORE_ADDR low_pc);
451
452 /* Reset the builder. */
453 void reset_builder () { m_builder.reset (); }
454
455 /* Return a type that is a generic pointer type, the size of which
456 matches the address size given in the compilation unit header for
457 this CU. */
458 struct type *addr_type () const;
459
460 /* Find an integer type the same size as the address size given in
461 the compilation unit header for this CU. UNSIGNED_P controls if
462 the integer is unsigned or not. */
463 struct type *addr_sized_int_type (bool unsigned_p) const;
464
465 /* The header of the compilation unit. */
466 struct comp_unit_head header {};
467
468 /* Base address of this compilation unit. */
469 gdb::optional<CORE_ADDR> base_address;
470
471 /* The language we are debugging. */
472 enum language language = language_unknown;
473 const struct language_defn *language_defn = nullptr;
474
475 const char *producer = nullptr;
476
477 private:
478 /* The symtab builder for this CU. This is only non-NULL when full
479 symbols are being read. */
480 std::unique_ptr<buildsym_compunit> m_builder;
481
482 public:
483 /* The generic symbol table building routines have separate lists for
484 file scope symbols and all all other scopes (local scopes). So
485 we need to select the right one to pass to add_symbol_to_list().
486 We do it by keeping a pointer to the correct list in list_in_scope.
487
488 FIXME: The original dwarf code just treated the file scope as the
489 first local scope, and all other local scopes as nested local
490 scopes, and worked fine. Check to see if we really need to
491 distinguish these in buildsym.c. */
492 struct pending **list_in_scope = nullptr;
493
494 /* Hash table holding all the loaded partial DIEs
495 with partial_die->offset.SECT_OFF as hash. */
496 htab_t partial_dies = nullptr;
497
498 /* Storage for things with the same lifetime as this read-in compilation
499 unit, including partial DIEs. */
500 auto_obstack comp_unit_obstack;
501
502 /* Backlink to our per_cu entry. */
503 struct dwarf2_per_cu_data *per_cu;
504
505 /* The dwarf2_per_objfile that owns this. */
506 dwarf2_per_objfile *per_objfile;
507
508 /* How many compilation units ago was this CU last referenced? */
509 int last_used = 0;
510
511 /* A hash table of DIE cu_offset for following references with
512 die_info->offset.sect_off as hash. */
513 htab_t die_hash = nullptr;
514
515 /* Full DIEs if read in. */
516 struct die_info *dies = nullptr;
517
518 /* A set of pointers to dwarf2_per_cu_data objects for compilation
519 units referenced by this one. Only set during full symbol processing;
520 partial symbol tables do not have dependencies. */
521 htab_t dependencies = nullptr;
522
523 /* Header data from the line table, during full symbol processing. */
524 struct line_header *line_header = nullptr;
525 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
526 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
527 this is the DW_TAG_compile_unit die for this CU. We'll hold on
528 to the line header as long as this DIE is being processed. See
529 process_die_scope. */
530 die_info *line_header_die_owner = nullptr;
531
532 /* A list of methods which need to have physnames computed
533 after all type information has been read. */
534 std::vector<delayed_method_info> method_list;
535
536 /* To be copied to symtab->call_site_htab. */
537 htab_t call_site_htab = nullptr;
538
539 /* Non-NULL if this CU came from a DWO file.
540 There is an invariant here that is important to remember:
541 Except for attributes copied from the top level DIE in the "main"
542 (or "stub") file in preparation for reading the DWO file
543 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
544 Either there isn't a DWO file (in which case this is NULL and the point
545 is moot), or there is and either we're not going to read it (in which
546 case this is NULL) or there is and we are reading it (in which case this
547 is non-NULL). */
548 struct dwo_unit *dwo_unit = nullptr;
549
550 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
551 Note this value comes from the Fission stub CU/TU's DIE. */
552 gdb::optional<ULONGEST> addr_base;
553
554 /* The DW_AT_GNU_ranges_base attribute, if present.
555
556 This is only relevant in the context of pre-DWARF 5 split units. In this
557 context, there is a .debug_ranges section in the linked executable,
558 containing all the ranges data for all the compilation units. Each
559 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
560 indicates the base of its contribution to that section. The DW_AT_ranges
561 attributes in the split-unit are of the form DW_FORM_sec_offset and point
562 into the .debug_ranges section of the linked file. However, they are not
563 "true" DW_FORM_sec_offset, because they are relative to the base of their
564 compilation unit's contribution, rather than relative to the beginning of
565 the section. The DW_AT_GNU_ranges_base value must be added to it to make
566 it relative to the beginning of the section.
567
568 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
569 case, so this value can be added without needing to know whether we are in
570 this case or not.
571
572 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
573 skeleton/stub, it must not have the base added, as it already points to the
574 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
575 have a DW_AT_ranges attribute, we can use the
576
577 die->tag != DW_AT_compile_unit
578
579 to determine whether the base should be added or not. */
580 ULONGEST gnu_ranges_base = 0;
581
582 /* The DW_AT_rnglists_base attribute, if present.
583
584 This is used when processing attributes of form DW_FORM_rnglistx in
585 non-split units. Attributes of this form found in a split unit don't
586 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
587 section. */
588 ULONGEST rnglists_base = 0;
589
590 /* The DW_AT_loclists_base attribute if present. */
591 ULONGEST loclist_base = 0;
592
593 /* When reading debug info generated by older versions of rustc, we
594 have to rewrite some union types to be struct types with a
595 variant part. This rewriting must be done after the CU is fully
596 read in, because otherwise at the point of rewriting some struct
597 type might not have been fully processed. So, we keep a list of
598 all such types here and process them after expansion. */
599 std::vector<struct type *> rust_unions;
600
601 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
602 files, the value is implicitly zero. For DWARF 5 version DWO files, the
603 value is often implicit and is the size of the header of
604 .debug_str_offsets section (8 or 4, depending on the address size). */
605 gdb::optional<ULONGEST> str_offsets_base;
606
607 /* Mark used when releasing cached dies. */
608 bool mark : 1;
609
610 /* This CU references .debug_loc. See the symtab->locations_valid field.
611 This test is imperfect as there may exist optimized debug code not using
612 any location list and still facing inlining issues if handled as
613 unoptimized code. For a future better test see GCC PR other/32998. */
614 bool has_loclist : 1;
615
616 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
617 if all the producer_is_* fields are valid. This information is cached
618 because profiling CU expansion showed excessive time spent in
619 producer_is_gxx_lt_4_6. */
620 bool checked_producer : 1;
621 bool producer_is_gxx_lt_4_6 : 1;
622 bool producer_is_gcc_lt_4_3 : 1;
623 bool producer_is_icc : 1;
624 bool producer_is_icc_lt_14 : 1;
625 bool producer_is_codewarrior : 1;
626
627 /* When true, the file that we're processing is known to have
628 debugging info for C++ namespaces. GCC 3.3.x did not produce
629 this information, but later versions do. */
630
631 bool processing_has_namespace_info : 1;
632
633 struct partial_die_info *find_partial_die (sect_offset sect_off);
634
635 /* If this CU was inherited by another CU (via specification,
636 abstract_origin, etc), this is the ancestor CU. */
637 dwarf2_cu *ancestor;
638
639 /* Get the buildsym_compunit for this CU. */
640 buildsym_compunit *get_builder ()
641 {
642 /* If this CU has a builder associated with it, use that. */
643 if (m_builder != nullptr)
644 return m_builder.get ();
645
646 /* Otherwise, search ancestors for a valid builder. */
647 if (ancestor != nullptr)
648 return ancestor->get_builder ();
649
650 return nullptr;
651 }
652 };
653
654 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
655 This includes type_unit_group and quick_file_names. */
656
657 struct stmt_list_hash
658 {
659 /* The DWO unit this table is from or NULL if there is none. */
660 struct dwo_unit *dwo_unit;
661
662 /* Offset in .debug_line or .debug_line.dwo. */
663 sect_offset line_sect_off;
664 };
665
666 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
667 an object of this type. This contains elements of type unit groups
668 that can be shared across objfiles. The non-shareable parts are in
669 type_unit_group_unshareable. */
670
671 struct type_unit_group
672 {
673 /* dwarf2read.c's main "handle" on a TU symtab.
674 To simplify things we create an artificial CU that "includes" all the
675 type units using this stmt_list so that the rest of the code still has
676 a "per_cu" handle on the symtab. */
677 struct dwarf2_per_cu_data per_cu;
678
679 /* The TUs that share this DW_AT_stmt_list entry.
680 This is added to while parsing type units to build partial symtabs,
681 and is deleted afterwards and not used again. */
682 std::vector<signatured_type *> *tus;
683
684 /* The data used to construct the hash key. */
685 struct stmt_list_hash hash;
686 };
687
688 /* These sections are what may appear in a (real or virtual) DWO file. */
689
690 struct dwo_sections
691 {
692 struct dwarf2_section_info abbrev;
693 struct dwarf2_section_info line;
694 struct dwarf2_section_info loc;
695 struct dwarf2_section_info loclists;
696 struct dwarf2_section_info macinfo;
697 struct dwarf2_section_info macro;
698 struct dwarf2_section_info rnglists;
699 struct dwarf2_section_info str;
700 struct dwarf2_section_info str_offsets;
701 /* In the case of a virtual DWO file, these two are unused. */
702 struct dwarf2_section_info info;
703 std::vector<dwarf2_section_info> types;
704 };
705
706 /* CUs/TUs in DWP/DWO files. */
707
708 struct dwo_unit
709 {
710 /* Backlink to the containing struct dwo_file. */
711 struct dwo_file *dwo_file;
712
713 /* The "id" that distinguishes this CU/TU.
714 .debug_info calls this "dwo_id", .debug_types calls this "signature".
715 Since signatures came first, we stick with it for consistency. */
716 ULONGEST signature;
717
718 /* The section this CU/TU lives in, in the DWO file. */
719 struct dwarf2_section_info *section;
720
721 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
722 sect_offset sect_off;
723 unsigned int length;
724
725 /* For types, offset in the type's DIE of the type defined by this TU. */
726 cu_offset type_offset_in_tu;
727 };
728
729 /* include/dwarf2.h defines the DWP section codes.
730 It defines a max value but it doesn't define a min value, which we
731 use for error checking, so provide one. */
732
733 enum dwp_v2_section_ids
734 {
735 DW_SECT_MIN = 1
736 };
737
738 /* Data for one DWO file.
739
740 This includes virtual DWO files (a virtual DWO file is a DWO file as it
741 appears in a DWP file). DWP files don't really have DWO files per se -
742 comdat folding of types "loses" the DWO file they came from, and from
743 a high level view DWP files appear to contain a mass of random types.
744 However, to maintain consistency with the non-DWP case we pretend DWP
745 files contain virtual DWO files, and we assign each TU with one virtual
746 DWO file (generally based on the line and abbrev section offsets -
747 a heuristic that seems to work in practice). */
748
749 struct dwo_file
750 {
751 dwo_file () = default;
752 DISABLE_COPY_AND_ASSIGN (dwo_file);
753
754 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
755 For virtual DWO files the name is constructed from the section offsets
756 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
757 from related CU+TUs. */
758 const char *dwo_name = nullptr;
759
760 /* The DW_AT_comp_dir attribute. */
761 const char *comp_dir = nullptr;
762
763 /* The bfd, when the file is open. Otherwise this is NULL.
764 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
765 gdb_bfd_ref_ptr dbfd;
766
767 /* The sections that make up this DWO file.
768 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
769 sections (for lack of a better name). */
770 struct dwo_sections sections {};
771
772 /* The CUs in the file.
773 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
774 an extension to handle LLVM's Link Time Optimization output (where
775 multiple source files may be compiled into a single object/dwo pair). */
776 htab_up cus;
777
778 /* Table of TUs in the file.
779 Each element is a struct dwo_unit. */
780 htab_up tus;
781 };
782
783 /* These sections are what may appear in a DWP file. */
784
785 struct dwp_sections
786 {
787 /* These are used by all DWP versions (1, 2 and 5). */
788 struct dwarf2_section_info str;
789 struct dwarf2_section_info cu_index;
790 struct dwarf2_section_info tu_index;
791
792 /* These are only used by DWP version 2 and version 5 files.
793 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
794 sections are referenced by section number, and are not recorded here.
795 In DWP version 2 or 5 there is at most one copy of all these sections,
796 each section being (effectively) comprised of the concatenation of all of
797 the individual sections that exist in the version 1 format.
798 To keep the code simple we treat each of these concatenated pieces as a
799 section itself (a virtual section?). */
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info info;
802 struct dwarf2_section_info line;
803 struct dwarf2_section_info loc;
804 struct dwarf2_section_info loclists;
805 struct dwarf2_section_info macinfo;
806 struct dwarf2_section_info macro;
807 struct dwarf2_section_info rnglists;
808 struct dwarf2_section_info str_offsets;
809 struct dwarf2_section_info types;
810 };
811
812 /* These sections are what may appear in a virtual DWO file in DWP version 1.
813 A virtual DWO file is a DWO file as it appears in a DWP file. */
814
815 struct virtual_v1_dwo_sections
816 {
817 struct dwarf2_section_info abbrev;
818 struct dwarf2_section_info line;
819 struct dwarf2_section_info loc;
820 struct dwarf2_section_info macinfo;
821 struct dwarf2_section_info macro;
822 struct dwarf2_section_info str_offsets;
823 /* Each DWP hash table entry records one CU or one TU.
824 That is recorded here, and copied to dwo_unit.section. */
825 struct dwarf2_section_info info_or_types;
826 };
827
828 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
829 In version 2, the sections of the DWO files are concatenated together
830 and stored in one section of that name. Thus each ELF section contains
831 several "virtual" sections. */
832
833 struct virtual_v2_or_v5_dwo_sections
834 {
835 bfd_size_type abbrev_offset;
836 bfd_size_type abbrev_size;
837
838 bfd_size_type line_offset;
839 bfd_size_type line_size;
840
841 bfd_size_type loc_offset;
842 bfd_size_type loc_size;
843
844 bfd_size_type loclists_offset;
845 bfd_size_type loclists_size;
846
847 bfd_size_type macinfo_offset;
848 bfd_size_type macinfo_size;
849
850 bfd_size_type macro_offset;
851 bfd_size_type macro_size;
852
853 bfd_size_type rnglists_offset;
854 bfd_size_type rnglists_size;
855
856 bfd_size_type str_offsets_offset;
857 bfd_size_type str_offsets_size;
858
859 /* Each DWP hash table entry records one CU or one TU.
860 That is recorded here, and copied to dwo_unit.section. */
861 bfd_size_type info_or_types_offset;
862 bfd_size_type info_or_types_size;
863 };
864
865 /* Contents of DWP hash tables. */
866
867 struct dwp_hash_table
868 {
869 uint32_t version, nr_columns;
870 uint32_t nr_units, nr_slots;
871 const gdb_byte *hash_table, *unit_table;
872 union
873 {
874 struct
875 {
876 const gdb_byte *indices;
877 } v1;
878 struct
879 {
880 /* This is indexed by column number and gives the id of the section
881 in that column. */
882 #define MAX_NR_V2_DWO_SECTIONS \
883 (1 /* .debug_info or .debug_types */ \
884 + 1 /* .debug_abbrev */ \
885 + 1 /* .debug_line */ \
886 + 1 /* .debug_loc */ \
887 + 1 /* .debug_str_offsets */ \
888 + 1 /* .debug_macro or .debug_macinfo */)
889 int section_ids[MAX_NR_V2_DWO_SECTIONS];
890 const gdb_byte *offsets;
891 const gdb_byte *sizes;
892 } v2;
893 struct
894 {
895 /* This is indexed by column number and gives the id of the section
896 in that column. */
897 #define MAX_NR_V5_DWO_SECTIONS \
898 (1 /* .debug_info */ \
899 + 1 /* .debug_abbrev */ \
900 + 1 /* .debug_line */ \
901 + 1 /* .debug_loclists */ \
902 + 1 /* .debug_str_offsets */ \
903 + 1 /* .debug_macro */ \
904 + 1 /* .debug_rnglists */)
905 int section_ids[MAX_NR_V5_DWO_SECTIONS];
906 const gdb_byte *offsets;
907 const gdb_byte *sizes;
908 } v5;
909 } section_pool;
910 };
911
912 /* Data for one DWP file. */
913
914 struct dwp_file
915 {
916 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
917 : name (name_),
918 dbfd (std::move (abfd))
919 {
920 }
921
922 /* Name of the file. */
923 const char *name;
924
925 /* File format version. */
926 int version = 0;
927
928 /* The bfd. */
929 gdb_bfd_ref_ptr dbfd;
930
931 /* Section info for this file. */
932 struct dwp_sections sections {};
933
934 /* Table of CUs in the file. */
935 const struct dwp_hash_table *cus = nullptr;
936
937 /* Table of TUs in the file. */
938 const struct dwp_hash_table *tus = nullptr;
939
940 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
941 htab_up loaded_cus;
942 htab_up loaded_tus;
943
944 /* Table to map ELF section numbers to their sections.
945 This is only needed for the DWP V1 file format. */
946 unsigned int num_sections = 0;
947 asection **elf_sections = nullptr;
948 };
949
950 /* Struct used to pass misc. parameters to read_die_and_children, et
951 al. which are used for both .debug_info and .debug_types dies.
952 All parameters here are unchanging for the life of the call. This
953 struct exists to abstract away the constant parameters of die reading. */
954
955 struct die_reader_specs
956 {
957 /* The bfd of die_section. */
958 bfd* abfd;
959
960 /* The CU of the DIE we are parsing. */
961 struct dwarf2_cu *cu;
962
963 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
964 struct dwo_file *dwo_file;
965
966 /* The section the die comes from.
967 This is either .debug_info or .debug_types, or the .dwo variants. */
968 struct dwarf2_section_info *die_section;
969
970 /* die_section->buffer. */
971 const gdb_byte *buffer;
972
973 /* The end of the buffer. */
974 const gdb_byte *buffer_end;
975
976 /* The abbreviation table to use when reading the DIEs. */
977 struct abbrev_table *abbrev_table;
978 };
979
980 /* A subclass of die_reader_specs that holds storage and has complex
981 constructor and destructor behavior. */
982
983 class cutu_reader : public die_reader_specs
984 {
985 public:
986
987 cutu_reader (dwarf2_per_cu_data *this_cu,
988 dwarf2_per_objfile *per_objfile,
989 struct abbrev_table *abbrev_table,
990 dwarf2_cu *existing_cu,
991 bool skip_partial);
992
993 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
994 dwarf2_per_objfile *per_objfile,
995 struct dwarf2_cu *parent_cu = nullptr,
996 struct dwo_file *dwo_file = nullptr);
997
998 DISABLE_COPY_AND_ASSIGN (cutu_reader);
999
1000 const gdb_byte *info_ptr = nullptr;
1001 struct die_info *comp_unit_die = nullptr;
1002 bool dummy_p = false;
1003
1004 /* Release the new CU, putting it on the chain. This cannot be done
1005 for dummy CUs. */
1006 void keep ();
1007
1008 private:
1009 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1010 dwarf2_per_objfile *per_objfile,
1011 dwarf2_cu *existing_cu);
1012
1013 struct dwarf2_per_cu_data *m_this_cu;
1014 std::unique_ptr<dwarf2_cu> m_new_cu;
1015
1016 /* The ordinary abbreviation table. */
1017 abbrev_table_up m_abbrev_table_holder;
1018
1019 /* The DWO abbreviation table. */
1020 abbrev_table_up m_dwo_abbrev_table;
1021 };
1022
1023 /* When we construct a partial symbol table entry we only
1024 need this much information. */
1025 struct partial_die_info : public allocate_on_obstack
1026 {
1027 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1028
1029 /* Disable assign but still keep copy ctor, which is needed
1030 load_partial_dies. */
1031 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1032
1033 /* Adjust the partial die before generating a symbol for it. This
1034 function may set the is_external flag or change the DIE's
1035 name. */
1036 void fixup (struct dwarf2_cu *cu);
1037
1038 /* Read a minimal amount of information into the minimal die
1039 structure. */
1040 const gdb_byte *read (const struct die_reader_specs *reader,
1041 const struct abbrev_info &abbrev,
1042 const gdb_byte *info_ptr);
1043
1044 /* Compute the name of this partial DIE. This memoizes the
1045 result, so it is safe to call multiple times. */
1046 const char *name (dwarf2_cu *cu);
1047
1048 /* Offset of this DIE. */
1049 const sect_offset sect_off;
1050
1051 /* DWARF-2 tag for this DIE. */
1052 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1053
1054 /* Assorted flags describing the data found in this DIE. */
1055 const unsigned int has_children : 1;
1056
1057 unsigned int is_external : 1;
1058 unsigned int is_declaration : 1;
1059 unsigned int has_type : 1;
1060 unsigned int has_specification : 1;
1061 unsigned int has_pc_info : 1;
1062 unsigned int may_be_inlined : 1;
1063
1064 /* This DIE has been marked DW_AT_main_subprogram. */
1065 unsigned int main_subprogram : 1;
1066
1067 /* Flag set if the SCOPE field of this structure has been
1068 computed. */
1069 unsigned int scope_set : 1;
1070
1071 /* Flag set if the DIE has a byte_size attribute. */
1072 unsigned int has_byte_size : 1;
1073
1074 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1075 unsigned int has_const_value : 1;
1076
1077 /* Flag set if any of the DIE's children are template arguments. */
1078 unsigned int has_template_arguments : 1;
1079
1080 /* Flag set if fixup has been called on this die. */
1081 unsigned int fixup_called : 1;
1082
1083 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1084 unsigned int is_dwz : 1;
1085
1086 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1087 unsigned int spec_is_dwz : 1;
1088
1089 unsigned int canonical_name : 1;
1090
1091 /* The name of this DIE. Normally the value of DW_AT_name, but
1092 sometimes a default name for unnamed DIEs. */
1093 const char *raw_name = nullptr;
1094
1095 /* The linkage name, if present. */
1096 const char *linkage_name = nullptr;
1097
1098 /* The scope to prepend to our children. This is generally
1099 allocated on the comp_unit_obstack, so will disappear
1100 when this compilation unit leaves the cache. */
1101 const char *scope = nullptr;
1102
1103 /* Some data associated with the partial DIE. The tag determines
1104 which field is live. */
1105 union
1106 {
1107 /* The location description associated with this DIE, if any. */
1108 struct dwarf_block *locdesc;
1109 /* The offset of an import, for DW_TAG_imported_unit. */
1110 sect_offset sect_off;
1111 } d {};
1112
1113 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1114 CORE_ADDR lowpc = 0;
1115 CORE_ADDR highpc = 0;
1116
1117 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1118 DW_AT_sibling, if any. */
1119 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1120 could return DW_AT_sibling values to its caller load_partial_dies. */
1121 const gdb_byte *sibling = nullptr;
1122
1123 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1124 DW_AT_specification (or DW_AT_abstract_origin or
1125 DW_AT_extension). */
1126 sect_offset spec_offset {};
1127
1128 /* Pointers to this DIE's parent, first child, and next sibling,
1129 if any. */
1130 struct partial_die_info *die_parent = nullptr;
1131 struct partial_die_info *die_child = nullptr;
1132 struct partial_die_info *die_sibling = nullptr;
1133
1134 friend struct partial_die_info *
1135 dwarf2_cu::find_partial_die (sect_offset sect_off);
1136
1137 private:
1138 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1139 partial_die_info (sect_offset sect_off)
1140 : partial_die_info (sect_off, DW_TAG_padding, 0)
1141 {
1142 }
1143
1144 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1145 int has_children_)
1146 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1147 {
1148 is_external = 0;
1149 is_declaration = 0;
1150 has_type = 0;
1151 has_specification = 0;
1152 has_pc_info = 0;
1153 may_be_inlined = 0;
1154 main_subprogram = 0;
1155 scope_set = 0;
1156 has_byte_size = 0;
1157 has_const_value = 0;
1158 has_template_arguments = 0;
1159 fixup_called = 0;
1160 is_dwz = 0;
1161 spec_is_dwz = 0;
1162 canonical_name = 0;
1163 }
1164 };
1165
1166 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1167 but this would require a corresponding change in unpack_field_as_long
1168 and friends. */
1169 static int bits_per_byte = 8;
1170
1171 struct variant_part_builder;
1172
1173 /* When reading a variant, we track a bit more information about the
1174 field, and store it in an object of this type. */
1175
1176 struct variant_field
1177 {
1178 int first_field = -1;
1179 int last_field = -1;
1180
1181 /* A variant can contain other variant parts. */
1182 std::vector<variant_part_builder> variant_parts;
1183
1184 /* If we see a DW_TAG_variant, then this will be set if this is the
1185 default branch. */
1186 bool default_branch = false;
1187 /* If we see a DW_AT_discr_value, then this will be the discriminant
1188 value. */
1189 ULONGEST discriminant_value = 0;
1190 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1191 data. */
1192 struct dwarf_block *discr_list_data = nullptr;
1193 };
1194
1195 /* This represents a DW_TAG_variant_part. */
1196
1197 struct variant_part_builder
1198 {
1199 /* The offset of the discriminant field. */
1200 sect_offset discriminant_offset {};
1201
1202 /* Variants that are direct children of this variant part. */
1203 std::vector<variant_field> variants;
1204
1205 /* True if we're currently reading a variant. */
1206 bool processing_variant = false;
1207 };
1208
1209 struct nextfield
1210 {
1211 int accessibility = 0;
1212 int virtuality = 0;
1213 /* Variant parts need to find the discriminant, which is a DIE
1214 reference. We track the section offset of each field to make
1215 this link. */
1216 sect_offset offset;
1217 struct field field {};
1218 };
1219
1220 struct fnfieldlist
1221 {
1222 const char *name = nullptr;
1223 std::vector<struct fn_field> fnfields;
1224 };
1225
1226 /* The routines that read and process dies for a C struct or C++ class
1227 pass lists of data member fields and lists of member function fields
1228 in an instance of a field_info structure, as defined below. */
1229 struct field_info
1230 {
1231 /* List of data member and baseclasses fields. */
1232 std::vector<struct nextfield> fields;
1233 std::vector<struct nextfield> baseclasses;
1234
1235 /* Set if the accessibility of one of the fields is not public. */
1236 bool non_public_fields = false;
1237
1238 /* Member function fieldlist array, contains name of possibly overloaded
1239 member function, number of overloaded member functions and a pointer
1240 to the head of the member function field chain. */
1241 std::vector<struct fnfieldlist> fnfieldlists;
1242
1243 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1244 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1245 std::vector<struct decl_field> typedef_field_list;
1246
1247 /* Nested types defined by this class and the number of elements in this
1248 list. */
1249 std::vector<struct decl_field> nested_types_list;
1250
1251 /* If non-null, this is the variant part we are currently
1252 reading. */
1253 variant_part_builder *current_variant_part = nullptr;
1254 /* This holds all the top-level variant parts attached to the type
1255 we're reading. */
1256 std::vector<variant_part_builder> variant_parts;
1257
1258 /* Return the total number of fields (including baseclasses). */
1259 int nfields () const
1260 {
1261 return fields.size () + baseclasses.size ();
1262 }
1263 };
1264
1265 /* Loaded secondary compilation units are kept in memory until they
1266 have not been referenced for the processing of this many
1267 compilation units. Set this to zero to disable caching. Cache
1268 sizes of up to at least twenty will improve startup time for
1269 typical inter-CU-reference binaries, at an obvious memory cost. */
1270 static int dwarf_max_cache_age = 5;
1271 static void
1272 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1273 struct cmd_list_element *c, const char *value)
1274 {
1275 fprintf_filtered (file, _("The upper bound on the age of cached "
1276 "DWARF compilation units is %s.\n"),
1277 value);
1278 }
1279 \f
1280 /* local function prototypes */
1281
1282 static void dwarf2_find_base_address (struct die_info *die,
1283 struct dwarf2_cu *cu);
1284
1285 static dwarf2_psymtab *create_partial_symtab
1286 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1287 const char *name);
1288
1289 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1290 const gdb_byte *info_ptr,
1291 struct die_info *type_unit_die);
1292
1293 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1294
1295 static void scan_partial_symbols (struct partial_die_info *,
1296 CORE_ADDR *, CORE_ADDR *,
1297 int, struct dwarf2_cu *);
1298
1299 static void add_partial_symbol (struct partial_die_info *,
1300 struct dwarf2_cu *);
1301
1302 static void add_partial_namespace (struct partial_die_info *pdi,
1303 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1304 int set_addrmap, struct dwarf2_cu *cu);
1305
1306 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1307 CORE_ADDR *highpc, int set_addrmap,
1308 struct dwarf2_cu *cu);
1309
1310 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1311 struct dwarf2_cu *cu);
1312
1313 static void add_partial_subprogram (struct partial_die_info *pdi,
1314 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1315 int need_pc, struct dwarf2_cu *cu);
1316
1317 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1318
1319 static struct partial_die_info *load_partial_dies
1320 (const struct die_reader_specs *, const gdb_byte *, int);
1321
1322 /* A pair of partial_die_info and compilation unit. */
1323 struct cu_partial_die_info
1324 {
1325 /* The compilation unit of the partial_die_info. */
1326 struct dwarf2_cu *cu;
1327 /* A partial_die_info. */
1328 struct partial_die_info *pdi;
1329
1330 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1331 : cu (cu),
1332 pdi (pdi)
1333 { /* Nothing. */ }
1334
1335 private:
1336 cu_partial_die_info () = delete;
1337 };
1338
1339 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1340 struct dwarf2_cu *);
1341
1342 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1343 struct attribute *, struct attr_abbrev *,
1344 const gdb_byte *);
1345
1346 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1347 struct attribute *attr, dwarf_tag tag);
1348
1349 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1350
1351 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1352 dwarf2_section_info *, sect_offset);
1353
1354 static const char *read_indirect_string
1355 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1356 const struct comp_unit_head *, unsigned int *);
1357
1358 static const char *read_indirect_string_at_offset
1359 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1360
1361 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1362 const gdb_byte *,
1363 unsigned int *);
1364
1365 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1366 ULONGEST str_index);
1367
1368 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1369 ULONGEST str_index);
1370
1371 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1372
1373 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1374 struct dwarf2_cu *);
1375
1376 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1377 struct dwarf2_cu *cu);
1378
1379 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1380
1381 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1382 struct dwarf2_cu *cu);
1383
1384 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1385
1386 static struct die_info *die_specification (struct die_info *die,
1387 struct dwarf2_cu **);
1388
1389 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1390 struct dwarf2_cu *cu);
1391
1392 static void dwarf_decode_lines (struct line_header *, const char *,
1393 struct dwarf2_cu *, dwarf2_psymtab *,
1394 CORE_ADDR, int decode_mapping);
1395
1396 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1397 const char *);
1398
1399 static struct symbol *new_symbol (struct die_info *, struct type *,
1400 struct dwarf2_cu *, struct symbol * = NULL);
1401
1402 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1403 struct dwarf2_cu *);
1404
1405 static void dwarf2_const_value_attr (const struct attribute *attr,
1406 struct type *type,
1407 const char *name,
1408 struct obstack *obstack,
1409 struct dwarf2_cu *cu, LONGEST *value,
1410 const gdb_byte **bytes,
1411 struct dwarf2_locexpr_baton **baton);
1412
1413 static struct type *read_subrange_index_type (struct die_info *die,
1414 struct dwarf2_cu *cu);
1415
1416 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1417
1418 static int need_gnat_info (struct dwarf2_cu *);
1419
1420 static struct type *die_descriptive_type (struct die_info *,
1421 struct dwarf2_cu *);
1422
1423 static void set_descriptive_type (struct type *, struct die_info *,
1424 struct dwarf2_cu *);
1425
1426 static struct type *die_containing_type (struct die_info *,
1427 struct dwarf2_cu *);
1428
1429 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1430 struct dwarf2_cu *);
1431
1432 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1433
1434 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1435
1436 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1437
1438 static char *typename_concat (struct obstack *obs, const char *prefix,
1439 const char *suffix, int physname,
1440 struct dwarf2_cu *cu);
1441
1442 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1443
1444 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1451
1452 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 /* Return the .debug_loclists section to use for cu. */
1455 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1456
1457 /* Return the .debug_rnglists section to use for cu. */
1458 static struct dwarf2_section_info *cu_debug_rnglists_section
1459 (struct dwarf2_cu *cu, dwarf_tag tag);
1460
1461 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1462 values. Keep the items ordered with increasing constraints compliance. */
1463 enum pc_bounds_kind
1464 {
1465 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1466 PC_BOUNDS_NOT_PRESENT,
1467
1468 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1469 were present but they do not form a valid range of PC addresses. */
1470 PC_BOUNDS_INVALID,
1471
1472 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1473 PC_BOUNDS_RANGES,
1474
1475 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1476 PC_BOUNDS_HIGH_LOW,
1477 };
1478
1479 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1480 CORE_ADDR *, CORE_ADDR *,
1481 struct dwarf2_cu *,
1482 dwarf2_psymtab *);
1483
1484 static void get_scope_pc_bounds (struct die_info *,
1485 CORE_ADDR *, CORE_ADDR *,
1486 struct dwarf2_cu *);
1487
1488 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1489 CORE_ADDR, struct dwarf2_cu *);
1490
1491 static void dwarf2_add_field (struct field_info *, struct die_info *,
1492 struct dwarf2_cu *);
1493
1494 static void dwarf2_attach_fields_to_type (struct field_info *,
1495 struct type *, struct dwarf2_cu *);
1496
1497 static void dwarf2_add_member_fn (struct field_info *,
1498 struct die_info *, struct type *,
1499 struct dwarf2_cu *);
1500
1501 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1502 struct type *,
1503 struct dwarf2_cu *);
1504
1505 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1506
1507 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1510
1511 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1512
1513 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1514
1515 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1516
1517 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1518
1519 static struct type *read_module_type (struct die_info *die,
1520 struct dwarf2_cu *cu);
1521
1522 static const char *namespace_name (struct die_info *die,
1523 int *is_anonymous, struct dwarf2_cu *);
1524
1525 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1526
1527 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1528 bool * = nullptr);
1529
1530 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1531 struct dwarf2_cu *);
1532
1533 static struct die_info *read_die_and_siblings_1
1534 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1535 struct die_info *);
1536
1537 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1538 const gdb_byte *info_ptr,
1539 const gdb_byte **new_info_ptr,
1540 struct die_info *parent);
1541
1542 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1543 struct die_info **, const gdb_byte *,
1544 int);
1545
1546 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1547 struct die_info **, const gdb_byte *);
1548
1549 static void process_die (struct die_info *, struct dwarf2_cu *);
1550
1551 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1552 struct objfile *);
1553
1554 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1555
1556 static const char *dwarf2_full_name (const char *name,
1557 struct die_info *die,
1558 struct dwarf2_cu *cu);
1559
1560 static const char *dwarf2_physname (const char *name, struct die_info *die,
1561 struct dwarf2_cu *cu);
1562
1563 static struct die_info *dwarf2_extension (struct die_info *die,
1564 struct dwarf2_cu **);
1565
1566 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1567
1568 static void dump_die_for_error (struct die_info *);
1569
1570 static void dump_die_1 (struct ui_file *, int level, int max_level,
1571 struct die_info *);
1572
1573 /*static*/ void dump_die (struct die_info *, int max_level);
1574
1575 static void store_in_ref_table (struct die_info *,
1576 struct dwarf2_cu *);
1577
1578 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1579 const struct attribute *,
1580 struct dwarf2_cu **);
1581
1582 static struct die_info *follow_die_ref (struct die_info *,
1583 const struct attribute *,
1584 struct dwarf2_cu **);
1585
1586 static struct die_info *follow_die_sig (struct die_info *,
1587 const struct attribute *,
1588 struct dwarf2_cu **);
1589
1590 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1591 struct dwarf2_cu *);
1592
1593 static struct type *get_DW_AT_signature_type (struct die_info *,
1594 const struct attribute *,
1595 struct dwarf2_cu *);
1596
1597 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1598 dwarf2_per_objfile *per_objfile);
1599
1600 static void read_signatured_type (signatured_type *sig_type,
1601 dwarf2_per_objfile *per_objfile);
1602
1603 static int attr_to_dynamic_prop (const struct attribute *attr,
1604 struct die_info *die, struct dwarf2_cu *cu,
1605 struct dynamic_prop *prop, struct type *type);
1606
1607 /* memory allocation interface */
1608
1609 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1610
1611 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1612
1613 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1614
1615 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1616 struct dwarf2_loclist_baton *baton,
1617 const struct attribute *attr);
1618
1619 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1620 struct symbol *sym,
1621 struct dwarf2_cu *cu,
1622 int is_block);
1623
1624 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1625 const gdb_byte *info_ptr,
1626 struct abbrev_info *abbrev);
1627
1628 static hashval_t partial_die_hash (const void *item);
1629
1630 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1631
1632 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1633 (sect_offset sect_off, unsigned int offset_in_dwz,
1634 dwarf2_per_objfile *per_objfile);
1635
1636 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1637 struct die_info *comp_unit_die,
1638 enum language pretend_language);
1639
1640 static struct type *set_die_type (struct die_info *, struct type *,
1641 struct dwarf2_cu *, bool = false);
1642
1643 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1644
1645 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1646
1647 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1648 dwarf2_per_objfile *per_objfile,
1649 dwarf2_cu *existing_cu,
1650 bool skip_partial,
1651 enum language pretend_language);
1652
1653 static void process_full_comp_unit (dwarf2_cu *cu,
1654 enum language pretend_language);
1655
1656 static void process_full_type_unit (dwarf2_cu *cu,
1657 enum language pretend_language);
1658
1659 static void dwarf2_add_dependence (struct dwarf2_cu *,
1660 struct dwarf2_per_cu_data *);
1661
1662 static void dwarf2_mark (struct dwarf2_cu *);
1663
1664 static struct type *get_die_type_at_offset (sect_offset,
1665 dwarf2_per_cu_data *per_cu,
1666 dwarf2_per_objfile *per_objfile);
1667
1668 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1669
1670 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1671 dwarf2_per_objfile *per_objfile,
1672 enum language pretend_language);
1673
1674 static void process_queue (dwarf2_per_objfile *per_objfile);
1675
1676 /* Class, the destructor of which frees all allocated queue entries. This
1677 will only have work to do if an error was thrown while processing the
1678 dwarf. If no error was thrown then the queue entries should have all
1679 been processed, and freed, as we went along. */
1680
1681 class dwarf2_queue_guard
1682 {
1683 public:
1684 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1685 : m_per_objfile (per_objfile)
1686 {
1687 }
1688
1689 /* Free any entries remaining on the queue. There should only be
1690 entries left if we hit an error while processing the dwarf. */
1691 ~dwarf2_queue_guard ()
1692 {
1693 /* Ensure that no memory is allocated by the queue. */
1694 std::queue<dwarf2_queue_item> empty;
1695 std::swap (m_per_objfile->per_bfd->queue, empty);
1696 }
1697
1698 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1699
1700 private:
1701 dwarf2_per_objfile *m_per_objfile;
1702 };
1703
1704 dwarf2_queue_item::~dwarf2_queue_item ()
1705 {
1706 /* Anything still marked queued is likely to be in an
1707 inconsistent state, so discard it. */
1708 if (per_cu->queued)
1709 {
1710 per_objfile->remove_cu (per_cu);
1711 per_cu->queued = 0;
1712 }
1713 }
1714
1715 /* The return type of find_file_and_directory. Note, the enclosed
1716 string pointers are only valid while this object is valid. */
1717
1718 struct file_and_directory
1719 {
1720 /* The filename. This is never NULL. */
1721 const char *name;
1722
1723 /* The compilation directory. NULL if not known. If we needed to
1724 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1725 points directly to the DW_AT_comp_dir string attribute owned by
1726 the obstack that owns the DIE. */
1727 const char *comp_dir;
1728
1729 /* If we needed to build a new string for comp_dir, this is what
1730 owns the storage. */
1731 std::string comp_dir_storage;
1732 };
1733
1734 static file_and_directory find_file_and_directory (struct die_info *die,
1735 struct dwarf2_cu *cu);
1736
1737 static htab_up allocate_signatured_type_table ();
1738
1739 static htab_up allocate_dwo_unit_table ();
1740
1741 static struct dwo_unit *lookup_dwo_unit_in_dwp
1742 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1743 const char *comp_dir, ULONGEST signature, int is_debug_types);
1744
1745 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1746
1747 static struct dwo_unit *lookup_dwo_comp_unit
1748 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1749 ULONGEST signature);
1750
1751 static struct dwo_unit *lookup_dwo_type_unit
1752 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1753
1754 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1755
1756 /* A unique pointer to a dwo_file. */
1757
1758 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1759
1760 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1761
1762 static void check_producer (struct dwarf2_cu *cu);
1763
1764 static void free_line_header_voidp (void *arg);
1765 \f
1766 /* Various complaints about symbol reading that don't abort the process. */
1767
1768 static void
1769 dwarf2_debug_line_missing_file_complaint (void)
1770 {
1771 complaint (_(".debug_line section has line data without a file"));
1772 }
1773
1774 static void
1775 dwarf2_debug_line_missing_end_sequence_complaint (void)
1776 {
1777 complaint (_(".debug_line section has line "
1778 "program sequence without an end"));
1779 }
1780
1781 static void
1782 dwarf2_complex_location_expr_complaint (void)
1783 {
1784 complaint (_("location expression too complex"));
1785 }
1786
1787 static void
1788 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1789 int arg3)
1790 {
1791 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1792 arg1, arg2, arg3);
1793 }
1794
1795 static void
1796 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1797 {
1798 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1799 arg1, arg2);
1800 }
1801
1802 /* Hash function for line_header_hash. */
1803
1804 static hashval_t
1805 line_header_hash (const struct line_header *ofs)
1806 {
1807 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1808 }
1809
1810 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1811
1812 static hashval_t
1813 line_header_hash_voidp (const void *item)
1814 {
1815 const struct line_header *ofs = (const struct line_header *) item;
1816
1817 return line_header_hash (ofs);
1818 }
1819
1820 /* Equality function for line_header_hash. */
1821
1822 static int
1823 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1824 {
1825 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1826 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1827
1828 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1829 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1830 }
1831
1832 \f
1833
1834 /* See declaration. */
1835
1836 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1837 bool can_copy_)
1838 : obfd (obfd),
1839 can_copy (can_copy_)
1840 {
1841 if (names == NULL)
1842 names = &dwarf2_elf_names;
1843
1844 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1845 locate_sections (obfd, sec, *names);
1846 }
1847
1848 dwarf2_per_bfd::~dwarf2_per_bfd ()
1849 {
1850 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1851 per_cu->imported_symtabs_free ();
1852
1853 for (signatured_type *sig_type : all_type_units)
1854 sig_type->per_cu.imported_symtabs_free ();
1855
1856 /* Everything else should be on this->obstack. */
1857 }
1858
1859 /* See read.h. */
1860
1861 void
1862 dwarf2_per_objfile::remove_all_cus ()
1863 {
1864 for (auto pair : m_dwarf2_cus)
1865 delete pair.second;
1866
1867 m_dwarf2_cus.clear ();
1868 }
1869
1870 /* A helper class that calls free_cached_comp_units on
1871 destruction. */
1872
1873 class free_cached_comp_units
1874 {
1875 public:
1876
1877 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1878 : m_per_objfile (per_objfile)
1879 {
1880 }
1881
1882 ~free_cached_comp_units ()
1883 {
1884 m_per_objfile->remove_all_cus ();
1885 }
1886
1887 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1888
1889 private:
1890
1891 dwarf2_per_objfile *m_per_objfile;
1892 };
1893
1894 /* See read.h. */
1895
1896 bool
1897 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1898 {
1899 gdb_assert (per_cu->index < this->m_symtabs.size ());
1900
1901 return this->m_symtabs[per_cu->index] != nullptr;
1902 }
1903
1904 /* See read.h. */
1905
1906 compunit_symtab *
1907 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1908 {
1909 gdb_assert (per_cu->index < this->m_symtabs.size ());
1910
1911 return this->m_symtabs[per_cu->index];
1912 }
1913
1914 /* See read.h. */
1915
1916 void
1917 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1918 compunit_symtab *symtab)
1919 {
1920 gdb_assert (per_cu->index < this->m_symtabs.size ());
1921 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1922
1923 this->m_symtabs[per_cu->index] = symtab;
1924 }
1925
1926 /* Try to locate the sections we need for DWARF 2 debugging
1927 information and return true if we have enough to do something.
1928 NAMES points to the dwarf2 section names, or is NULL if the standard
1929 ELF names are used. CAN_COPY is true for formats where symbol
1930 interposition is possible and so symbol values must follow copy
1931 relocation rules. */
1932
1933 int
1934 dwarf2_has_info (struct objfile *objfile,
1935 const struct dwarf2_debug_sections *names,
1936 bool can_copy)
1937 {
1938 if (objfile->flags & OBJF_READNEVER)
1939 return 0;
1940
1941 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1942
1943 if (per_objfile == NULL)
1944 {
1945 dwarf2_per_bfd *per_bfd;
1946
1947 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
1948 doesn't require relocations and if there aren't partial symbols
1949 from some other reader. */
1950 if (!objfile_has_partial_symbols (objfile)
1951 && !gdb_bfd_requires_relocations (objfile->obfd))
1952 {
1953 /* See if one has been created for this BFD yet. */
1954 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1955
1956 if (per_bfd == nullptr)
1957 {
1958 /* No, create it now. */
1959 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1960 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1961 }
1962 }
1963 else
1964 {
1965 /* No sharing possible, create one specifically for this objfile. */
1966 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1967 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1968 }
1969
1970 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1971 }
1972
1973 return (!per_objfile->per_bfd->info.is_virtual
1974 && per_objfile->per_bfd->info.s.section != NULL
1975 && !per_objfile->per_bfd->abbrev.is_virtual
1976 && per_objfile->per_bfd->abbrev.s.section != NULL);
1977 }
1978
1979 /* When loading sections, we look either for uncompressed section or for
1980 compressed section names. */
1981
1982 static int
1983 section_is_p (const char *section_name,
1984 const struct dwarf2_section_names *names)
1985 {
1986 if (names->normal != NULL
1987 && strcmp (section_name, names->normal) == 0)
1988 return 1;
1989 if (names->compressed != NULL
1990 && strcmp (section_name, names->compressed) == 0)
1991 return 1;
1992 return 0;
1993 }
1994
1995 /* See declaration. */
1996
1997 void
1998 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1999 const dwarf2_debug_sections &names)
2000 {
2001 flagword aflag = bfd_section_flags (sectp);
2002
2003 if ((aflag & SEC_HAS_CONTENTS) == 0)
2004 {
2005 }
2006 else if (elf_section_data (sectp)->this_hdr.sh_size
2007 > bfd_get_file_size (abfd))
2008 {
2009 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2010 warning (_("Discarding section %s which has a section size (%s"
2011 ") larger than the file size [in module %s]"),
2012 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2013 bfd_get_filename (abfd));
2014 }
2015 else if (section_is_p (sectp->name, &names.info))
2016 {
2017 this->info.s.section = sectp;
2018 this->info.size = bfd_section_size (sectp);
2019 }
2020 else if (section_is_p (sectp->name, &names.abbrev))
2021 {
2022 this->abbrev.s.section = sectp;
2023 this->abbrev.size = bfd_section_size (sectp);
2024 }
2025 else if (section_is_p (sectp->name, &names.line))
2026 {
2027 this->line.s.section = sectp;
2028 this->line.size = bfd_section_size (sectp);
2029 }
2030 else if (section_is_p (sectp->name, &names.loc))
2031 {
2032 this->loc.s.section = sectp;
2033 this->loc.size = bfd_section_size (sectp);
2034 }
2035 else if (section_is_p (sectp->name, &names.loclists))
2036 {
2037 this->loclists.s.section = sectp;
2038 this->loclists.size = bfd_section_size (sectp);
2039 }
2040 else if (section_is_p (sectp->name, &names.macinfo))
2041 {
2042 this->macinfo.s.section = sectp;
2043 this->macinfo.size = bfd_section_size (sectp);
2044 }
2045 else if (section_is_p (sectp->name, &names.macro))
2046 {
2047 this->macro.s.section = sectp;
2048 this->macro.size = bfd_section_size (sectp);
2049 }
2050 else if (section_is_p (sectp->name, &names.str))
2051 {
2052 this->str.s.section = sectp;
2053 this->str.size = bfd_section_size (sectp);
2054 }
2055 else if (section_is_p (sectp->name, &names.str_offsets))
2056 {
2057 this->str_offsets.s.section = sectp;
2058 this->str_offsets.size = bfd_section_size (sectp);
2059 }
2060 else if (section_is_p (sectp->name, &names.line_str))
2061 {
2062 this->line_str.s.section = sectp;
2063 this->line_str.size = bfd_section_size (sectp);
2064 }
2065 else if (section_is_p (sectp->name, &names.addr))
2066 {
2067 this->addr.s.section = sectp;
2068 this->addr.size = bfd_section_size (sectp);
2069 }
2070 else if (section_is_p (sectp->name, &names.frame))
2071 {
2072 this->frame.s.section = sectp;
2073 this->frame.size = bfd_section_size (sectp);
2074 }
2075 else if (section_is_p (sectp->name, &names.eh_frame))
2076 {
2077 this->eh_frame.s.section = sectp;
2078 this->eh_frame.size = bfd_section_size (sectp);
2079 }
2080 else if (section_is_p (sectp->name, &names.ranges))
2081 {
2082 this->ranges.s.section = sectp;
2083 this->ranges.size = bfd_section_size (sectp);
2084 }
2085 else if (section_is_p (sectp->name, &names.rnglists))
2086 {
2087 this->rnglists.s.section = sectp;
2088 this->rnglists.size = bfd_section_size (sectp);
2089 }
2090 else if (section_is_p (sectp->name, &names.types))
2091 {
2092 struct dwarf2_section_info type_section;
2093
2094 memset (&type_section, 0, sizeof (type_section));
2095 type_section.s.section = sectp;
2096 type_section.size = bfd_section_size (sectp);
2097
2098 this->types.push_back (type_section);
2099 }
2100 else if (section_is_p (sectp->name, &names.gdb_index))
2101 {
2102 this->gdb_index.s.section = sectp;
2103 this->gdb_index.size = bfd_section_size (sectp);
2104 }
2105 else if (section_is_p (sectp->name, &names.debug_names))
2106 {
2107 this->debug_names.s.section = sectp;
2108 this->debug_names.size = bfd_section_size (sectp);
2109 }
2110 else if (section_is_p (sectp->name, &names.debug_aranges))
2111 {
2112 this->debug_aranges.s.section = sectp;
2113 this->debug_aranges.size = bfd_section_size (sectp);
2114 }
2115
2116 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2117 && bfd_section_vma (sectp) == 0)
2118 this->has_section_at_zero = true;
2119 }
2120
2121 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2122 SECTION_NAME. */
2123
2124 void
2125 dwarf2_get_section_info (struct objfile *objfile,
2126 enum dwarf2_section_enum sect,
2127 asection **sectp, const gdb_byte **bufp,
2128 bfd_size_type *sizep)
2129 {
2130 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2131 struct dwarf2_section_info *info;
2132
2133 /* We may see an objfile without any DWARF, in which case we just
2134 return nothing. */
2135 if (per_objfile == NULL)
2136 {
2137 *sectp = NULL;
2138 *bufp = NULL;
2139 *sizep = 0;
2140 return;
2141 }
2142 switch (sect)
2143 {
2144 case DWARF2_DEBUG_FRAME:
2145 info = &per_objfile->per_bfd->frame;
2146 break;
2147 case DWARF2_EH_FRAME:
2148 info = &per_objfile->per_bfd->eh_frame;
2149 break;
2150 default:
2151 gdb_assert_not_reached ("unexpected section");
2152 }
2153
2154 info->read (objfile);
2155
2156 *sectp = info->get_bfd_section ();
2157 *bufp = info->buffer;
2158 *sizep = info->size;
2159 }
2160
2161 /* A helper function to find the sections for a .dwz file. */
2162
2163 static void
2164 locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
2165 {
2166 /* Note that we only support the standard ELF names, because .dwz
2167 is ELF-only (at the time of writing). */
2168 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2169 {
2170 dwz_file->abbrev.s.section = sectp;
2171 dwz_file->abbrev.size = bfd_section_size (sectp);
2172 }
2173 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2174 {
2175 dwz_file->info.s.section = sectp;
2176 dwz_file->info.size = bfd_section_size (sectp);
2177 }
2178 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2179 {
2180 dwz_file->str.s.section = sectp;
2181 dwz_file->str.size = bfd_section_size (sectp);
2182 }
2183 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2184 {
2185 dwz_file->line.s.section = sectp;
2186 dwz_file->line.size = bfd_section_size (sectp);
2187 }
2188 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2189 {
2190 dwz_file->macro.s.section = sectp;
2191 dwz_file->macro.size = bfd_section_size (sectp);
2192 }
2193 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2194 {
2195 dwz_file->gdb_index.s.section = sectp;
2196 dwz_file->gdb_index.size = bfd_section_size (sectp);
2197 }
2198 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2199 {
2200 dwz_file->debug_names.s.section = sectp;
2201 dwz_file->debug_names.size = bfd_section_size (sectp);
2202 }
2203 }
2204
2205 /* Attempt to find a .dwz file (whose full path is represented by
2206 FILENAME) in all of the specified debug file directories provided.
2207
2208 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2209 nullptr if it could not find anything. */
2210
2211 static gdb_bfd_ref_ptr
2212 dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2213 size_t buildid_len)
2214 {
2215 /* Let's assume that the path represented by FILENAME has the
2216 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2217 distributions do, anyway. */
2218 size_t dwz_pos = filename.find ("/.dwz/");
2219
2220 if (dwz_pos == std::string::npos)
2221 return nullptr;
2222
2223 /* This is an obvious assertion, but it's here more to educate
2224 future readers of this code that FILENAME at DWZ_POS *must*
2225 contain a directory separator. */
2226 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2227
2228 gdb_bfd_ref_ptr dwz_bfd;
2229 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2230 = dirnames_to_char_ptr_vec (debug_file_directory);
2231
2232 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2233 {
2234 /* The idea is to iterate over the
2235 debug file directories provided by the user and
2236 replace the hard-coded path in the "filename" by each
2237 debug-file-directory.
2238
2239 For example, suppose that filename is:
2240
2241 /usr/lib/debug/.dwz/foo.dwz
2242
2243 And suppose that we have "$HOME/bar" as the
2244 debug-file-directory. We would then adjust filename
2245 to look like:
2246
2247 $HOME/bar/.dwz/foo.dwz
2248
2249 which would hopefully allow us to find the alt debug
2250 file. */
2251 std::string ddir = debugdir.get ();
2252
2253 if (ddir.empty ())
2254 continue;
2255
2256 /* Make sure the current debug-file-directory ends with a
2257 directory separator. This is needed because, if FILENAME
2258 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2259 DDIR is "/usr/lib/abc", then could wrongfully skip it
2260 below. */
2261 if (!IS_DIR_SEPARATOR (ddir.back ()))
2262 ddir += SLASH_STRING;
2263
2264 /* Check whether the beginning of FILENAME is DDIR. If it is,
2265 then we are dealing with a file which we already attempted to
2266 open before, so we just skip it and continue processing the
2267 remaining debug file directories. */
2268 if (filename.size () > ddir.size ()
2269 && filename.compare (0, ddir.size (), ddir) == 0)
2270 continue;
2271
2272 /* Replace FILENAME's default debug-file-directory with
2273 DDIR. */
2274 std::string new_filename = ddir + &filename[dwz_pos + 1];
2275
2276 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2277
2278 if (dwz_bfd == nullptr)
2279 continue;
2280
2281 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2282 {
2283 dwz_bfd.reset (nullptr);
2284 continue;
2285 }
2286
2287 /* Found it. */
2288 break;
2289 }
2290
2291 return dwz_bfd;
2292 }
2293
2294 /* See dwarf2read.h. */
2295
2296 struct dwz_file *
2297 dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
2298 {
2299 bfd_size_type buildid_len_arg;
2300 size_t buildid_len;
2301 bfd_byte *buildid;
2302
2303 if (per_bfd->dwz_file != NULL)
2304 return per_bfd->dwz_file.get ();
2305
2306 bfd_set_error (bfd_error_no_error);
2307 gdb::unique_xmalloc_ptr<char> data
2308 (bfd_get_alt_debug_link_info (per_bfd->obfd,
2309 &buildid_len_arg, &buildid));
2310 if (data == NULL)
2311 {
2312 if (bfd_get_error () == bfd_error_no_error)
2313 return NULL;
2314 error (_("could not read '.gnu_debugaltlink' section: %s"),
2315 bfd_errmsg (bfd_get_error ()));
2316 }
2317
2318 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
2319
2320 buildid_len = (size_t) buildid_len_arg;
2321
2322 std::string filename = data.get ();
2323
2324 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
2325 {
2326 gdb::unique_xmalloc_ptr<char> abs
2327 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
2328
2329 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
2330 }
2331
2332 /* First try the file name given in the section. If that doesn't
2333 work, try to use the build-id instead. */
2334 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
2335 if (dwz_bfd != NULL)
2336 {
2337 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2338 dwz_bfd.reset (nullptr);
2339 }
2340
2341 if (dwz_bfd == NULL)
2342 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2343
2344 if (dwz_bfd == nullptr)
2345 {
2346 /* If the user has provided us with different
2347 debug file directories, we can try them in order. */
2348 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2349 }
2350
2351 if (dwz_bfd == nullptr)
2352 {
2353 gdb::unique_xmalloc_ptr<char> alt_filename;
2354 const char *origname = bfd_get_filename (per_bfd->obfd);
2355
2356 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2357 buildid_len,
2358 origname,
2359 &alt_filename));
2360
2361 if (fd.get () >= 0)
2362 {
2363 /* File successfully retrieved from server. */
2364 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
2365
2366 if (dwz_bfd == nullptr)
2367 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2368 alt_filename.get ());
2369 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2370 dwz_bfd.reset (nullptr);
2371 }
2372 }
2373
2374 if (dwz_bfd == NULL)
2375 error (_("could not find '.gnu_debugaltlink' file for %s"),
2376 bfd_get_filename (per_bfd->obfd));
2377
2378 std::unique_ptr<struct dwz_file> result
2379 (new struct dwz_file (std::move (dwz_bfd)));
2380
2381 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2382 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
2383
2384 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2385 per_bfd->dwz_file = std::move (result);
2386 return per_bfd->dwz_file.get ();
2387 }
2388 \f
2389 /* DWARF quick_symbols_functions support. */
2390
2391 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2392 unique line tables, so we maintain a separate table of all .debug_line
2393 derived entries to support the sharing.
2394 All the quick functions need is the list of file names. We discard the
2395 line_header when we're done and don't need to record it here. */
2396 struct quick_file_names
2397 {
2398 /* The data used to construct the hash key. */
2399 struct stmt_list_hash hash;
2400
2401 /* The number of entries in file_names, real_names. */
2402 unsigned int num_file_names;
2403
2404 /* The file names from the line table, after being run through
2405 file_full_name. */
2406 const char **file_names;
2407
2408 /* The file names from the line table after being run through
2409 gdb_realpath. These are computed lazily. */
2410 const char **real_names;
2411 };
2412
2413 /* When using the index (and thus not using psymtabs), each CU has an
2414 object of this type. This is used to hold information needed by
2415 the various "quick" methods. */
2416 struct dwarf2_per_cu_quick_data
2417 {
2418 /* The file table. This can be NULL if there was no file table
2419 or it's currently not read in.
2420 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2421 struct quick_file_names *file_names;
2422
2423 /* A temporary mark bit used when iterating over all CUs in
2424 expand_symtabs_matching. */
2425 unsigned int mark : 1;
2426
2427 /* True if we've tried to read the file table and found there isn't one.
2428 There will be no point in trying to read it again next time. */
2429 unsigned int no_file_data : 1;
2430 };
2431
2432 /* Utility hash function for a stmt_list_hash. */
2433
2434 static hashval_t
2435 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2436 {
2437 hashval_t v = 0;
2438
2439 if (stmt_list_hash->dwo_unit != NULL)
2440 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2441 v += to_underlying (stmt_list_hash->line_sect_off);
2442 return v;
2443 }
2444
2445 /* Utility equality function for a stmt_list_hash. */
2446
2447 static int
2448 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2449 const struct stmt_list_hash *rhs)
2450 {
2451 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2452 return 0;
2453 if (lhs->dwo_unit != NULL
2454 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2455 return 0;
2456
2457 return lhs->line_sect_off == rhs->line_sect_off;
2458 }
2459
2460 /* Hash function for a quick_file_names. */
2461
2462 static hashval_t
2463 hash_file_name_entry (const void *e)
2464 {
2465 const struct quick_file_names *file_data
2466 = (const struct quick_file_names *) e;
2467
2468 return hash_stmt_list_entry (&file_data->hash);
2469 }
2470
2471 /* Equality function for a quick_file_names. */
2472
2473 static int
2474 eq_file_name_entry (const void *a, const void *b)
2475 {
2476 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2477 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2478
2479 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2480 }
2481
2482 /* Delete function for a quick_file_names. */
2483
2484 static void
2485 delete_file_name_entry (void *e)
2486 {
2487 struct quick_file_names *file_data = (struct quick_file_names *) e;
2488 int i;
2489
2490 for (i = 0; i < file_data->num_file_names; ++i)
2491 {
2492 xfree ((void*) file_data->file_names[i]);
2493 if (file_data->real_names)
2494 xfree ((void*) file_data->real_names[i]);
2495 }
2496
2497 /* The space for the struct itself lives on the obstack, so we don't
2498 free it here. */
2499 }
2500
2501 /* Create a quick_file_names hash table. */
2502
2503 static htab_up
2504 create_quick_file_names_table (unsigned int nr_initial_entries)
2505 {
2506 return htab_up (htab_create_alloc (nr_initial_entries,
2507 hash_file_name_entry, eq_file_name_entry,
2508 delete_file_name_entry, xcalloc, xfree));
2509 }
2510
2511 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2512 function is unrelated to symtabs, symtab would have to be created afterwards.
2513 You should call age_cached_comp_units after processing the CU. */
2514
2515 static dwarf2_cu *
2516 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2517 bool skip_partial)
2518 {
2519 if (per_cu->is_debug_types)
2520 load_full_type_unit (per_cu, per_objfile);
2521 else
2522 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2523 skip_partial, language_minimal);
2524
2525 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2526 if (cu == nullptr)
2527 return nullptr; /* Dummy CU. */
2528
2529 dwarf2_find_base_address (cu->dies, cu);
2530
2531 return cu;
2532 }
2533
2534 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2535
2536 static void
2537 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2538 dwarf2_per_objfile *per_objfile, bool skip_partial)
2539 {
2540 /* Skip type_unit_groups, reading the type units they contain
2541 is handled elsewhere. */
2542 if (per_cu->type_unit_group_p ())
2543 return;
2544
2545 /* The destructor of dwarf2_queue_guard frees any entries left on
2546 the queue. After this point we're guaranteed to leave this function
2547 with the dwarf queue empty. */
2548 dwarf2_queue_guard q_guard (per_objfile);
2549
2550 if (!per_objfile->symtab_set_p (per_cu))
2551 {
2552 queue_comp_unit (per_cu, per_objfile, language_minimal);
2553 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2554
2555 /* If we just loaded a CU from a DWO, and we're working with an index
2556 that may badly handle TUs, load all the TUs in that DWO as well.
2557 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2558 if (!per_cu->is_debug_types
2559 && cu != NULL
2560 && cu->dwo_unit != NULL
2561 && per_objfile->per_bfd->index_table != NULL
2562 && per_objfile->per_bfd->index_table->version <= 7
2563 /* DWP files aren't supported yet. */
2564 && get_dwp_file (per_objfile) == NULL)
2565 queue_and_load_all_dwo_tus (cu);
2566 }
2567
2568 process_queue (per_objfile);
2569
2570 /* Age the cache, releasing compilation units that have not
2571 been used recently. */
2572 per_objfile->age_comp_units ();
2573 }
2574
2575 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2576 the per-objfile for which this symtab is instantiated.
2577
2578 Returns the resulting symbol table. */
2579
2580 static struct compunit_symtab *
2581 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2582 dwarf2_per_objfile *per_objfile,
2583 bool skip_partial)
2584 {
2585 gdb_assert (per_objfile->per_bfd->using_index);
2586
2587 if (!per_objfile->symtab_set_p (per_cu))
2588 {
2589 free_cached_comp_units freer (per_objfile);
2590 scoped_restore decrementer = increment_reading_symtab ();
2591 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2592 process_cu_includes (per_objfile);
2593 }
2594
2595 return per_objfile->get_symtab (per_cu);
2596 }
2597
2598 /* See declaration. */
2599
2600 dwarf2_per_cu_data *
2601 dwarf2_per_bfd::get_cutu (int index)
2602 {
2603 if (index >= this->all_comp_units.size ())
2604 {
2605 index -= this->all_comp_units.size ();
2606 gdb_assert (index < this->all_type_units.size ());
2607 return &this->all_type_units[index]->per_cu;
2608 }
2609
2610 return this->all_comp_units[index];
2611 }
2612
2613 /* See declaration. */
2614
2615 dwarf2_per_cu_data *
2616 dwarf2_per_bfd::get_cu (int index)
2617 {
2618 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2619
2620 return this->all_comp_units[index];
2621 }
2622
2623 /* See declaration. */
2624
2625 signatured_type *
2626 dwarf2_per_bfd::get_tu (int index)
2627 {
2628 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2629
2630 return this->all_type_units[index];
2631 }
2632
2633 /* See read.h. */
2634
2635 dwarf2_per_cu_data *
2636 dwarf2_per_bfd::allocate_per_cu ()
2637 {
2638 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2639 result->per_bfd = this;
2640 result->index = m_num_psymtabs++;
2641 return result;
2642 }
2643
2644 /* See read.h. */
2645
2646 signatured_type *
2647 dwarf2_per_bfd::allocate_signatured_type ()
2648 {
2649 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2650 result->per_cu.per_bfd = this;
2651 result->per_cu.index = m_num_psymtabs++;
2652 return result;
2653 }
2654
2655 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2656 obstack, and constructed with the specified field values. */
2657
2658 static dwarf2_per_cu_data *
2659 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2660 struct dwarf2_section_info *section,
2661 int is_dwz,
2662 sect_offset sect_off, ULONGEST length)
2663 {
2664 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2665 the_cu->sect_off = sect_off;
2666 the_cu->length = length;
2667 the_cu->section = section;
2668 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2669 struct dwarf2_per_cu_quick_data);
2670 the_cu->is_dwz = is_dwz;
2671 return the_cu;
2672 }
2673
2674 /* A helper for create_cus_from_index that handles a given list of
2675 CUs. */
2676
2677 static void
2678 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2679 const gdb_byte *cu_list, offset_type n_elements,
2680 struct dwarf2_section_info *section,
2681 int is_dwz)
2682 {
2683 for (offset_type i = 0; i < n_elements; i += 2)
2684 {
2685 gdb_static_assert (sizeof (ULONGEST) >= 8);
2686
2687 sect_offset sect_off
2688 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2689 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2690 cu_list += 2 * 8;
2691
2692 dwarf2_per_cu_data *per_cu
2693 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2694 length);
2695 per_bfd->all_comp_units.push_back (per_cu);
2696 }
2697 }
2698
2699 /* Read the CU list from the mapped index, and use it to create all
2700 the CU objects for PER_BFD. */
2701
2702 static void
2703 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2704 const gdb_byte *cu_list, offset_type cu_list_elements,
2705 const gdb_byte *dwz_list, offset_type dwz_elements)
2706 {
2707 gdb_assert (per_bfd->all_comp_units.empty ());
2708 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2709
2710 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2711 &per_bfd->info, 0);
2712
2713 if (dwz_elements == 0)
2714 return;
2715
2716 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2717 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2718 &dwz->info, 1);
2719 }
2720
2721 /* Create the signatured type hash table from the index. */
2722
2723 static void
2724 create_signatured_type_table_from_index
2725 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2726 const gdb_byte *bytes, offset_type elements)
2727 {
2728 gdb_assert (per_bfd->all_type_units.empty ());
2729 per_bfd->all_type_units.reserve (elements / 3);
2730
2731 htab_up sig_types_hash = allocate_signatured_type_table ();
2732
2733 for (offset_type i = 0; i < elements; i += 3)
2734 {
2735 struct signatured_type *sig_type;
2736 ULONGEST signature;
2737 void **slot;
2738 cu_offset type_offset_in_tu;
2739
2740 gdb_static_assert (sizeof (ULONGEST) >= 8);
2741 sect_offset sect_off
2742 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2743 type_offset_in_tu
2744 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2745 BFD_ENDIAN_LITTLE);
2746 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2747 bytes += 3 * 8;
2748
2749 sig_type = per_bfd->allocate_signatured_type ();
2750 sig_type->signature = signature;
2751 sig_type->type_offset_in_tu = type_offset_in_tu;
2752 sig_type->per_cu.is_debug_types = 1;
2753 sig_type->per_cu.section = section;
2754 sig_type->per_cu.sect_off = sect_off;
2755 sig_type->per_cu.v.quick
2756 = OBSTACK_ZALLOC (&per_bfd->obstack,
2757 struct dwarf2_per_cu_quick_data);
2758
2759 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2760 *slot = sig_type;
2761
2762 per_bfd->all_type_units.push_back (sig_type);
2763 }
2764
2765 per_bfd->signatured_types = std::move (sig_types_hash);
2766 }
2767
2768 /* Create the signatured type hash table from .debug_names. */
2769
2770 static void
2771 create_signatured_type_table_from_debug_names
2772 (dwarf2_per_objfile *per_objfile,
2773 const mapped_debug_names &map,
2774 struct dwarf2_section_info *section,
2775 struct dwarf2_section_info *abbrev_section)
2776 {
2777 struct objfile *objfile = per_objfile->objfile;
2778
2779 section->read (objfile);
2780 abbrev_section->read (objfile);
2781
2782 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2783 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2784
2785 htab_up sig_types_hash = allocate_signatured_type_table ();
2786
2787 for (uint32_t i = 0; i < map.tu_count; ++i)
2788 {
2789 struct signatured_type *sig_type;
2790 void **slot;
2791
2792 sect_offset sect_off
2793 = (sect_offset) (extract_unsigned_integer
2794 (map.tu_table_reordered + i * map.offset_size,
2795 map.offset_size,
2796 map.dwarf5_byte_order));
2797
2798 comp_unit_head cu_header;
2799 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2800 abbrev_section,
2801 section->buffer + to_underlying (sect_off),
2802 rcuh_kind::TYPE);
2803
2804 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2805 sig_type->signature = cu_header.signature;
2806 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2807 sig_type->per_cu.is_debug_types = 1;
2808 sig_type->per_cu.section = section;
2809 sig_type->per_cu.sect_off = sect_off;
2810 sig_type->per_cu.v.quick
2811 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2812 struct dwarf2_per_cu_quick_data);
2813
2814 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2815 *slot = sig_type;
2816
2817 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2818 }
2819
2820 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2821 }
2822
2823 /* Read the address map data from the mapped index, and use it to
2824 populate the objfile's psymtabs_addrmap. */
2825
2826 static void
2827 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2828 struct mapped_index *index)
2829 {
2830 struct objfile *objfile = per_objfile->objfile;
2831 struct gdbarch *gdbarch = objfile->arch ();
2832 const gdb_byte *iter, *end;
2833 struct addrmap *mutable_map;
2834 CORE_ADDR baseaddr;
2835
2836 auto_obstack temp_obstack;
2837
2838 mutable_map = addrmap_create_mutable (&temp_obstack);
2839
2840 iter = index->address_table.data ();
2841 end = iter + index->address_table.size ();
2842
2843 baseaddr = objfile->text_section_offset ();
2844
2845 while (iter < end)
2846 {
2847 ULONGEST hi, lo, cu_index;
2848 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2849 iter += 8;
2850 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2851 iter += 8;
2852 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2853 iter += 4;
2854
2855 if (lo > hi)
2856 {
2857 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2858 hex_string (lo), hex_string (hi));
2859 continue;
2860 }
2861
2862 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
2863 {
2864 complaint (_(".gdb_index address table has invalid CU number %u"),
2865 (unsigned) cu_index);
2866 continue;
2867 }
2868
2869 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2870 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2871 addrmap_set_empty (mutable_map, lo, hi - 1,
2872 per_objfile->per_bfd->get_cu (cu_index));
2873 }
2874
2875 objfile->partial_symtabs->psymtabs_addrmap
2876 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
2877 }
2878
2879 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2880 populate the objfile's psymtabs_addrmap. */
2881
2882 static void
2883 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2884 struct dwarf2_section_info *section)
2885 {
2886 struct objfile *objfile = per_objfile->objfile;
2887 bfd *abfd = objfile->obfd;
2888 struct gdbarch *gdbarch = objfile->arch ();
2889 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2890
2891 auto_obstack temp_obstack;
2892 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2893
2894 std::unordered_map<sect_offset,
2895 dwarf2_per_cu_data *,
2896 gdb::hash_enum<sect_offset>>
2897 debug_info_offset_to_per_cu;
2898 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
2899 {
2900 const auto insertpair
2901 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2902 if (!insertpair.second)
2903 {
2904 warning (_("Section .debug_aranges in %s has duplicate "
2905 "debug_info_offset %s, ignoring .debug_aranges."),
2906 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2907 return;
2908 }
2909 }
2910
2911 section->read (objfile);
2912
2913 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2914
2915 const gdb_byte *addr = section->buffer;
2916
2917 while (addr < section->buffer + section->size)
2918 {
2919 const gdb_byte *const entry_addr = addr;
2920 unsigned int bytes_read;
2921
2922 const LONGEST entry_length = read_initial_length (abfd, addr,
2923 &bytes_read);
2924 addr += bytes_read;
2925
2926 const gdb_byte *const entry_end = addr + entry_length;
2927 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2928 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2929 if (addr + entry_length > section->buffer + section->size)
2930 {
2931 warning (_("Section .debug_aranges in %s entry at offset %s "
2932 "length %s exceeds section length %s, "
2933 "ignoring .debug_aranges."),
2934 objfile_name (objfile),
2935 plongest (entry_addr - section->buffer),
2936 plongest (bytes_read + entry_length),
2937 pulongest (section->size));
2938 return;
2939 }
2940
2941 /* The version number. */
2942 const uint16_t version = read_2_bytes (abfd, addr);
2943 addr += 2;
2944 if (version != 2)
2945 {
2946 warning (_("Section .debug_aranges in %s entry at offset %s "
2947 "has unsupported version %d, ignoring .debug_aranges."),
2948 objfile_name (objfile),
2949 plongest (entry_addr - section->buffer), version);
2950 return;
2951 }
2952
2953 const uint64_t debug_info_offset
2954 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2955 addr += offset_size;
2956 const auto per_cu_it
2957 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2958 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2959 {
2960 warning (_("Section .debug_aranges in %s entry at offset %s "
2961 "debug_info_offset %s does not exists, "
2962 "ignoring .debug_aranges."),
2963 objfile_name (objfile),
2964 plongest (entry_addr - section->buffer),
2965 pulongest (debug_info_offset));
2966 return;
2967 }
2968 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2969
2970 const uint8_t address_size = *addr++;
2971 if (address_size < 1 || address_size > 8)
2972 {
2973 warning (_("Section .debug_aranges in %s entry at offset %s "
2974 "address_size %u is invalid, ignoring .debug_aranges."),
2975 objfile_name (objfile),
2976 plongest (entry_addr - section->buffer), address_size);
2977 return;
2978 }
2979
2980 const uint8_t segment_selector_size = *addr++;
2981 if (segment_selector_size != 0)
2982 {
2983 warning (_("Section .debug_aranges in %s entry at offset %s "
2984 "segment_selector_size %u is not supported, "
2985 "ignoring .debug_aranges."),
2986 objfile_name (objfile),
2987 plongest (entry_addr - section->buffer),
2988 segment_selector_size);
2989 return;
2990 }
2991
2992 /* Must pad to an alignment boundary that is twice the address
2993 size. It is undocumented by the DWARF standard but GCC does
2994 use it. */
2995 for (size_t padding = ((-(addr - section->buffer))
2996 & (2 * address_size - 1));
2997 padding > 0; padding--)
2998 if (*addr++ != 0)
2999 {
3000 warning (_("Section .debug_aranges in %s entry at offset %s "
3001 "padding is not zero, ignoring .debug_aranges."),
3002 objfile_name (objfile),
3003 plongest (entry_addr - section->buffer));
3004 return;
3005 }
3006
3007 for (;;)
3008 {
3009 if (addr + 2 * address_size > entry_end)
3010 {
3011 warning (_("Section .debug_aranges in %s entry at offset %s "
3012 "address list is not properly terminated, "
3013 "ignoring .debug_aranges."),
3014 objfile_name (objfile),
3015 plongest (entry_addr - section->buffer));
3016 return;
3017 }
3018 ULONGEST start = extract_unsigned_integer (addr, address_size,
3019 dwarf5_byte_order);
3020 addr += address_size;
3021 ULONGEST length = extract_unsigned_integer (addr, address_size,
3022 dwarf5_byte_order);
3023 addr += address_size;
3024 if (start == 0 && length == 0)
3025 break;
3026 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
3027 {
3028 /* Symbol was eliminated due to a COMDAT group. */
3029 continue;
3030 }
3031 ULONGEST end = start + length;
3032 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3033 - baseaddr);
3034 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3035 - baseaddr);
3036 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3037 }
3038 }
3039
3040 objfile->partial_symtabs->psymtabs_addrmap
3041 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
3042 }
3043
3044 /* Find a slot in the mapped index INDEX for the object named NAME.
3045 If NAME is found, set *VEC_OUT to point to the CU vector in the
3046 constant pool and return true. If NAME cannot be found, return
3047 false. */
3048
3049 static bool
3050 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3051 offset_type **vec_out)
3052 {
3053 offset_type hash;
3054 offset_type slot, step;
3055 int (*cmp) (const char *, const char *);
3056
3057 gdb::unique_xmalloc_ptr<char> without_params;
3058 if (current_language->la_language == language_cplus
3059 || current_language->la_language == language_fortran
3060 || current_language->la_language == language_d)
3061 {
3062 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3063 not contain any. */
3064
3065 if (strchr (name, '(') != NULL)
3066 {
3067 without_params = cp_remove_params (name);
3068
3069 if (without_params != NULL)
3070 name = without_params.get ();
3071 }
3072 }
3073
3074 /* Index version 4 did not support case insensitive searches. But the
3075 indices for case insensitive languages are built in lowercase, therefore
3076 simulate our NAME being searched is also lowercased. */
3077 hash = mapped_index_string_hash ((index->version == 4
3078 && case_sensitivity == case_sensitive_off
3079 ? 5 : index->version),
3080 name);
3081
3082 slot = hash & (index->symbol_table.size () - 1);
3083 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
3084 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
3085
3086 for (;;)
3087 {
3088 const char *str;
3089
3090 const auto &bucket = index->symbol_table[slot];
3091 if (bucket.name == 0 && bucket.vec == 0)
3092 return false;
3093
3094 str = index->constant_pool + MAYBE_SWAP (bucket.name);
3095 if (!cmp (name, str))
3096 {
3097 *vec_out = (offset_type *) (index->constant_pool
3098 + MAYBE_SWAP (bucket.vec));
3099 return true;
3100 }
3101
3102 slot = (slot + step) & (index->symbol_table.size () - 1);
3103 }
3104 }
3105
3106 /* A helper function that reads the .gdb_index from BUFFER and fills
3107 in MAP. FILENAME is the name of the file containing the data;
3108 it is used for error reporting. DEPRECATED_OK is true if it is
3109 ok to use deprecated sections.
3110
3111 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3112 out parameters that are filled in with information about the CU and
3113 TU lists in the section.
3114
3115 Returns true if all went well, false otherwise. */
3116
3117 static bool
3118 read_gdb_index_from_buffer (const char *filename,
3119 bool deprecated_ok,
3120 gdb::array_view<const gdb_byte> buffer,
3121 struct mapped_index *map,
3122 const gdb_byte **cu_list,
3123 offset_type *cu_list_elements,
3124 const gdb_byte **types_list,
3125 offset_type *types_list_elements)
3126 {
3127 const gdb_byte *addr = &buffer[0];
3128
3129 /* Version check. */
3130 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
3131 /* Versions earlier than 3 emitted every copy of a psymbol. This
3132 causes the index to behave very poorly for certain requests. Version 3
3133 contained incomplete addrmap. So, it seems better to just ignore such
3134 indices. */
3135 if (version < 4)
3136 {
3137 static int warning_printed = 0;
3138 if (!warning_printed)
3139 {
3140 warning (_("Skipping obsolete .gdb_index section in %s."),
3141 filename);
3142 warning_printed = 1;
3143 }
3144 return 0;
3145 }
3146 /* Index version 4 uses a different hash function than index version
3147 5 and later.
3148
3149 Versions earlier than 6 did not emit psymbols for inlined
3150 functions. Using these files will cause GDB not to be able to
3151 set breakpoints on inlined functions by name, so we ignore these
3152 indices unless the user has done
3153 "set use-deprecated-index-sections on". */
3154 if (version < 6 && !deprecated_ok)
3155 {
3156 static int warning_printed = 0;
3157 if (!warning_printed)
3158 {
3159 warning (_("\
3160 Skipping deprecated .gdb_index section in %s.\n\
3161 Do \"set use-deprecated-index-sections on\" before the file is read\n\
3162 to use the section anyway."),
3163 filename);
3164 warning_printed = 1;
3165 }
3166 return 0;
3167 }
3168 /* Version 7 indices generated by gold refer to the CU for a symbol instead
3169 of the TU (for symbols coming from TUs),
3170 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3171 Plus gold-generated indices can have duplicate entries for global symbols,
3172 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3173 These are just performance bugs, and we can't distinguish gdb-generated
3174 indices from gold-generated ones, so issue no warning here. */
3175
3176 /* Indexes with higher version than the one supported by GDB may be no
3177 longer backward compatible. */
3178 if (version > 8)
3179 return 0;
3180
3181 map->version = version;
3182
3183 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3184
3185 int i = 0;
3186 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3187 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3188 / 8);
3189 ++i;
3190
3191 *types_list = addr + MAYBE_SWAP (metadata[i]);
3192 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3193 - MAYBE_SWAP (metadata[i]))
3194 / 8);
3195 ++i;
3196
3197 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3198 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3199 map->address_table
3200 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3201 ++i;
3202
3203 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3204 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3205 map->symbol_table
3206 = gdb::array_view<mapped_index::symbol_table_slot>
3207 ((mapped_index::symbol_table_slot *) symbol_table,
3208 (mapped_index::symbol_table_slot *) symbol_table_end);
3209
3210 ++i;
3211 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3212
3213 return 1;
3214 }
3215
3216 /* Callback types for dwarf2_read_gdb_index. */
3217
3218 typedef gdb::function_view
3219 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3220 get_gdb_index_contents_ftype;
3221 typedef gdb::function_view
3222 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3223 get_gdb_index_contents_dwz_ftype;
3224
3225 /* Read .gdb_index. If everything went ok, initialize the "quick"
3226 elements of all the CUs and return 1. Otherwise, return 0. */
3227
3228 static int
3229 dwarf2_read_gdb_index
3230 (dwarf2_per_objfile *per_objfile,
3231 get_gdb_index_contents_ftype get_gdb_index_contents,
3232 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3233 {
3234 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3235 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3236 struct dwz_file *dwz;
3237 struct objfile *objfile = per_objfile->objfile;
3238 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3239
3240 gdb::array_view<const gdb_byte> main_index_contents
3241 = get_gdb_index_contents (objfile, per_bfd);
3242
3243 if (main_index_contents.empty ())
3244 return 0;
3245
3246 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3247 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3248 use_deprecated_index_sections,
3249 main_index_contents, map.get (), &cu_list,
3250 &cu_list_elements, &types_list,
3251 &types_list_elements))
3252 return 0;
3253
3254 /* Don't use the index if it's empty. */
3255 if (map->symbol_table.empty ())
3256 return 0;
3257
3258 /* If there is a .dwz file, read it so we can get its CU list as
3259 well. */
3260 dwz = dwarf2_get_dwz_file (per_bfd);
3261 if (dwz != NULL)
3262 {
3263 struct mapped_index dwz_map;
3264 const gdb_byte *dwz_types_ignore;
3265 offset_type dwz_types_elements_ignore;
3266
3267 gdb::array_view<const gdb_byte> dwz_index_content
3268 = get_gdb_index_contents_dwz (objfile, dwz);
3269
3270 if (dwz_index_content.empty ())
3271 return 0;
3272
3273 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3274 1, dwz_index_content, &dwz_map,
3275 &dwz_list, &dwz_list_elements,
3276 &dwz_types_ignore,
3277 &dwz_types_elements_ignore))
3278 {
3279 warning (_("could not read '.gdb_index' section from %s; skipping"),
3280 bfd_get_filename (dwz->dwz_bfd.get ()));
3281 return 0;
3282 }
3283 }
3284
3285 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3286 dwz_list_elements);
3287
3288 if (types_list_elements)
3289 {
3290 /* We can only handle a single .debug_types when we have an
3291 index. */
3292 if (per_bfd->types.size () != 1)
3293 return 0;
3294
3295 dwarf2_section_info *section = &per_bfd->types[0];
3296
3297 create_signatured_type_table_from_index (per_bfd, section, types_list,
3298 types_list_elements);
3299 }
3300
3301 create_addrmap_from_index (per_objfile, map.get ());
3302
3303 per_bfd->index_table = std::move (map);
3304 per_bfd->using_index = 1;
3305 per_bfd->quick_file_names_table =
3306 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3307
3308 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3309 objfiles using the same BFD. */
3310 gdb_assert (per_bfd->partial_symtabs == nullptr);
3311 per_bfd->partial_symtabs = objfile->partial_symtabs;
3312
3313 return 1;
3314 }
3315
3316 /* die_reader_func for dw2_get_file_names. */
3317
3318 static void
3319 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3320 const gdb_byte *info_ptr,
3321 struct die_info *comp_unit_die)
3322 {
3323 struct dwarf2_cu *cu = reader->cu;
3324 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3325 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3326 struct dwarf2_per_cu_data *lh_cu;
3327 struct attribute *attr;
3328 void **slot;
3329 struct quick_file_names *qfn;
3330
3331 gdb_assert (! this_cu->is_debug_types);
3332
3333 /* Our callers never want to match partial units -- instead they
3334 will match the enclosing full CU. */
3335 if (comp_unit_die->tag == DW_TAG_partial_unit)
3336 {
3337 this_cu->v.quick->no_file_data = 1;
3338 return;
3339 }
3340
3341 lh_cu = this_cu;
3342 slot = NULL;
3343
3344 line_header_up lh;
3345 sect_offset line_offset {};
3346
3347 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3348 if (attr != nullptr && attr->form_is_unsigned ())
3349 {
3350 struct quick_file_names find_entry;
3351
3352 line_offset = (sect_offset) attr->as_unsigned ();
3353
3354 /* We may have already read in this line header (TU line header sharing).
3355 If we have we're done. */
3356 find_entry.hash.dwo_unit = cu->dwo_unit;
3357 find_entry.hash.line_sect_off = line_offset;
3358 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3359 &find_entry, INSERT);
3360 if (*slot != NULL)
3361 {
3362 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3363 return;
3364 }
3365
3366 lh = dwarf_decode_line_header (line_offset, cu);
3367 }
3368 if (lh == NULL)
3369 {
3370 lh_cu->v.quick->no_file_data = 1;
3371 return;
3372 }
3373
3374 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3375 qfn->hash.dwo_unit = cu->dwo_unit;
3376 qfn->hash.line_sect_off = line_offset;
3377 gdb_assert (slot != NULL);
3378 *slot = qfn;
3379
3380 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3381
3382 int offset = 0;
3383 if (strcmp (fnd.name, "<unknown>") != 0)
3384 ++offset;
3385
3386 qfn->num_file_names = offset + lh->file_names_size ();
3387 qfn->file_names =
3388 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3389 qfn->num_file_names);
3390 if (offset != 0)
3391 qfn->file_names[0] = xstrdup (fnd.name);
3392 for (int i = 0; i < lh->file_names_size (); ++i)
3393 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3394 fnd.comp_dir).release ();
3395 qfn->real_names = NULL;
3396
3397 lh_cu->v.quick->file_names = qfn;
3398 }
3399
3400 /* A helper for the "quick" functions which attempts to read the line
3401 table for THIS_CU. */
3402
3403 static struct quick_file_names *
3404 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3405 dwarf2_per_objfile *per_objfile)
3406 {
3407 /* This should never be called for TUs. */
3408 gdb_assert (! this_cu->is_debug_types);
3409 /* Nor type unit groups. */
3410 gdb_assert (! this_cu->type_unit_group_p ());
3411
3412 if (this_cu->v.quick->file_names != NULL)
3413 return this_cu->v.quick->file_names;
3414 /* If we know there is no line data, no point in looking again. */
3415 if (this_cu->v.quick->no_file_data)
3416 return NULL;
3417
3418 cutu_reader reader (this_cu, per_objfile);
3419 if (!reader.dummy_p)
3420 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
3421
3422 if (this_cu->v.quick->no_file_data)
3423 return NULL;
3424 return this_cu->v.quick->file_names;
3425 }
3426
3427 /* A helper for the "quick" functions which computes and caches the
3428 real path for a given file name from the line table. */
3429
3430 static const char *
3431 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3432 struct quick_file_names *qfn, int index)
3433 {
3434 if (qfn->real_names == NULL)
3435 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3436 qfn->num_file_names, const char *);
3437
3438 if (qfn->real_names[index] == NULL)
3439 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3440
3441 return qfn->real_names[index];
3442 }
3443
3444 static struct symtab *
3445 dw2_find_last_source_symtab (struct objfile *objfile)
3446 {
3447 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3448 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3449 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3450
3451 if (cust == NULL)
3452 return NULL;
3453
3454 return compunit_primary_filetab (cust);
3455 }
3456
3457 /* Traversal function for dw2_forget_cached_source_info. */
3458
3459 static int
3460 dw2_free_cached_file_names (void **slot, void *info)
3461 {
3462 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3463
3464 if (file_data->real_names)
3465 {
3466 int i;
3467
3468 for (i = 0; i < file_data->num_file_names; ++i)
3469 {
3470 xfree ((void*) file_data->real_names[i]);
3471 file_data->real_names[i] = NULL;
3472 }
3473 }
3474
3475 return 1;
3476 }
3477
3478 static void
3479 dw2_forget_cached_source_info (struct objfile *objfile)
3480 {
3481 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3482
3483 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3484 dw2_free_cached_file_names, NULL);
3485 }
3486
3487 /* Helper function for dw2_map_symtabs_matching_filename that expands
3488 the symtabs and calls the iterator. */
3489
3490 static int
3491 dw2_map_expand_apply (struct objfile *objfile,
3492 struct dwarf2_per_cu_data *per_cu,
3493 const char *name, const char *real_path,
3494 gdb::function_view<bool (symtab *)> callback)
3495 {
3496 struct compunit_symtab *last_made = objfile->compunit_symtabs;
3497
3498 /* Don't visit already-expanded CUs. */
3499 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3500 if (per_objfile->symtab_set_p (per_cu))
3501 return 0;
3502
3503 /* This may expand more than one symtab, and we want to iterate over
3504 all of them. */
3505 dw2_instantiate_symtab (per_cu, per_objfile, false);
3506
3507 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3508 last_made, callback);
3509 }
3510
3511 /* Implementation of the map_symtabs_matching_filename method. */
3512
3513 static bool
3514 dw2_map_symtabs_matching_filename
3515 (struct objfile *objfile, const char *name, const char *real_path,
3516 gdb::function_view<bool (symtab *)> callback)
3517 {
3518 const char *name_basename = lbasename (name);
3519 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3520
3521 /* The rule is CUs specify all the files, including those used by
3522 any TU, so there's no need to scan TUs here. */
3523
3524 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3525 {
3526 /* We only need to look at symtabs not already expanded. */
3527 if (per_objfile->symtab_set_p (per_cu))
3528 continue;
3529
3530 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3531 if (file_data == NULL)
3532 continue;
3533
3534 for (int j = 0; j < file_data->num_file_names; ++j)
3535 {
3536 const char *this_name = file_data->file_names[j];
3537 const char *this_real_name;
3538
3539 if (compare_filenames_for_search (this_name, name))
3540 {
3541 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3542 callback))
3543 return true;
3544 continue;
3545 }
3546
3547 /* Before we invoke realpath, which can get expensive when many
3548 files are involved, do a quick comparison of the basenames. */
3549 if (! basenames_may_differ
3550 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3551 continue;
3552
3553 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
3554 if (compare_filenames_for_search (this_real_name, name))
3555 {
3556 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3557 callback))
3558 return true;
3559 continue;
3560 }
3561
3562 if (real_path != NULL)
3563 {
3564 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3565 gdb_assert (IS_ABSOLUTE_PATH (name));
3566 if (this_real_name != NULL
3567 && FILENAME_CMP (real_path, this_real_name) == 0)
3568 {
3569 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
3570 callback))
3571 return true;
3572 continue;
3573 }
3574 }
3575 }
3576 }
3577
3578 return false;
3579 }
3580
3581 /* Struct used to manage iterating over all CUs looking for a symbol. */
3582
3583 struct dw2_symtab_iterator
3584 {
3585 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3586 dwarf2_per_objfile *per_objfile;
3587 /* If set, only look for symbols that match that block. Valid values are
3588 GLOBAL_BLOCK and STATIC_BLOCK. */
3589 gdb::optional<block_enum> block_index;
3590 /* The kind of symbol we're looking for. */
3591 domain_enum domain;
3592 /* The list of CUs from the index entry of the symbol,
3593 or NULL if not found. */
3594 offset_type *vec;
3595 /* The next element in VEC to look at. */
3596 int next;
3597 /* The number of elements in VEC, or zero if there is no match. */
3598 int length;
3599 /* Have we seen a global version of the symbol?
3600 If so we can ignore all further global instances.
3601 This is to work around gold/15646, inefficient gold-generated
3602 indices. */
3603 int global_seen;
3604 };
3605
3606 /* Initialize the index symtab iterator ITER, common part. */
3607
3608 static void
3609 dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3610 dwarf2_per_objfile *per_objfile,
3611 gdb::optional<block_enum> block_index,
3612 domain_enum domain)
3613 {
3614 iter->per_objfile = per_objfile;
3615 iter->block_index = block_index;
3616 iter->domain = domain;
3617 iter->next = 0;
3618 iter->global_seen = 0;
3619 iter->vec = NULL;
3620 iter->length = 0;
3621 }
3622
3623 /* Initialize the index symtab iterator ITER, const char *NAME variant. */
3624
3625 static void
3626 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3627 dwarf2_per_objfile *per_objfile,
3628 gdb::optional<block_enum> block_index,
3629 domain_enum domain,
3630 const char *name)
3631 {
3632 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3633
3634 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3635 /* index is NULL if OBJF_READNOW. */
3636 if (index == NULL)
3637 return;
3638
3639 if (find_slot_in_mapped_hash (index, name, &iter->vec))
3640 iter->length = MAYBE_SWAP (*iter->vec);
3641 }
3642
3643 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3644
3645 static void
3646 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3647 dwarf2_per_objfile *per_objfile,
3648 gdb::optional<block_enum> block_index,
3649 domain_enum domain, offset_type namei)
3650 {
3651 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3652
3653 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3654 /* index is NULL if OBJF_READNOW. */
3655 if (index == NULL)
3656 return;
3657
3658 gdb_assert (!index->symbol_name_slot_invalid (namei));
3659 const auto &bucket = index->symbol_table[namei];
3660
3661 iter->vec = (offset_type *) (index->constant_pool
3662 + MAYBE_SWAP (bucket.vec));
3663 iter->length = MAYBE_SWAP (*iter->vec);
3664 }
3665
3666 /* Return the next matching CU or NULL if there are no more. */
3667
3668 static struct dwarf2_per_cu_data *
3669 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3670 {
3671 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3672
3673 for ( ; iter->next < iter->length; ++iter->next)
3674 {
3675 offset_type cu_index_and_attrs =
3676 MAYBE_SWAP (iter->vec[iter->next + 1]);
3677 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3678 gdb_index_symbol_kind symbol_kind =
3679 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3680 /* Only check the symbol attributes if they're present.
3681 Indices prior to version 7 don't record them,
3682 and indices >= 7 may elide them for certain symbols
3683 (gold does this). */
3684 int attrs_valid =
3685 (per_objfile->per_bfd->index_table->version >= 7
3686 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3687
3688 /* Don't crash on bad data. */
3689 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3690 + per_objfile->per_bfd->all_type_units.size ()))
3691 {
3692 complaint (_(".gdb_index entry has bad CU index"
3693 " [in module %s]"), objfile_name (per_objfile->objfile));
3694 continue;
3695 }
3696
3697 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3698
3699 /* Skip if already read in. */
3700 if (per_objfile->symtab_set_p (per_cu))
3701 continue;
3702
3703 /* Check static vs global. */
3704 if (attrs_valid)
3705 {
3706 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3707
3708 if (iter->block_index.has_value ())
3709 {
3710 bool want_static = *iter->block_index == STATIC_BLOCK;
3711
3712 if (is_static != want_static)
3713 continue;
3714 }
3715
3716 /* Work around gold/15646. */
3717 if (!is_static
3718 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3719 {
3720 if (iter->global_seen)
3721 continue;
3722
3723 iter->global_seen = 1;
3724 }
3725 }
3726
3727 /* Only check the symbol's kind if it has one. */
3728 if (attrs_valid)
3729 {
3730 switch (iter->domain)
3731 {
3732 case VAR_DOMAIN:
3733 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3734 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3735 /* Some types are also in VAR_DOMAIN. */
3736 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3737 continue;
3738 break;
3739 case STRUCT_DOMAIN:
3740 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3741 continue;
3742 break;
3743 case LABEL_DOMAIN:
3744 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3745 continue;
3746 break;
3747 case MODULE_DOMAIN:
3748 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3749 continue;
3750 break;
3751 default:
3752 break;
3753 }
3754 }
3755
3756 ++iter->next;
3757 return per_cu;
3758 }
3759
3760 return NULL;
3761 }
3762
3763 static struct compunit_symtab *
3764 dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
3765 const char *name, domain_enum domain)
3766 {
3767 struct compunit_symtab *stab_best = NULL;
3768 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3769
3770 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3771
3772 struct dw2_symtab_iterator iter;
3773 struct dwarf2_per_cu_data *per_cu;
3774
3775 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
3776
3777 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3778 {
3779 struct symbol *sym, *with_opaque = NULL;
3780 struct compunit_symtab *stab
3781 = dw2_instantiate_symtab (per_cu, per_objfile, false);
3782 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
3783 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
3784
3785 sym = block_find_symbol (block, name, domain,
3786 block_find_non_opaque_type_preferred,
3787 &with_opaque);
3788
3789 /* Some caution must be observed with overloaded functions
3790 and methods, since the index will not contain any overload
3791 information (but NAME might contain it). */
3792
3793 if (sym != NULL
3794 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3795 return stab;
3796 if (with_opaque != NULL
3797 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3798 stab_best = stab;
3799
3800 /* Keep looking through other CUs. */
3801 }
3802
3803 return stab_best;
3804 }
3805
3806 static void
3807 dw2_print_stats (struct objfile *objfile)
3808 {
3809 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3810 int total = (per_objfile->per_bfd->all_comp_units.size ()
3811 + per_objfile->per_bfd->all_type_units.size ());
3812 int count = 0;
3813
3814 for (int i = 0; i < total; ++i)
3815 {
3816 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3817
3818 if (!per_objfile->symtab_set_p (per_cu))
3819 ++count;
3820 }
3821 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3822 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3823 }
3824
3825 /* This dumps minimal information about the index.
3826 It is called via "mt print objfiles".
3827 One use is to verify .gdb_index has been loaded by the
3828 gdb.dwarf2/gdb-index.exp testcase. */
3829
3830 static void
3831 dw2_dump (struct objfile *objfile)
3832 {
3833 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3834
3835 gdb_assert (per_objfile->per_bfd->using_index);
3836 printf_filtered (".gdb_index:");
3837 if (per_objfile->per_bfd->index_table != NULL)
3838 {
3839 printf_filtered (" version %d\n",
3840 per_objfile->per_bfd->index_table->version);
3841 }
3842 else
3843 printf_filtered (" faked for \"readnow\"\n");
3844 printf_filtered ("\n");
3845 }
3846
3847 static void
3848 dw2_expand_symtabs_for_function (struct objfile *objfile,
3849 const char *func_name)
3850 {
3851 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3852
3853 struct dw2_symtab_iterator iter;
3854 struct dwarf2_per_cu_data *per_cu;
3855
3856 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
3857
3858 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3859 dw2_instantiate_symtab (per_cu, per_objfile, false);
3860
3861 }
3862
3863 static void
3864 dw2_expand_all_symtabs (struct objfile *objfile)
3865 {
3866 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3867 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3868 + per_objfile->per_bfd->all_type_units.size ());
3869
3870 for (int i = 0; i < total_units; ++i)
3871 {
3872 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3873
3874 /* We don't want to directly expand a partial CU, because if we
3875 read it with the wrong language, then assertion failures can
3876 be triggered later on. See PR symtab/23010. So, tell
3877 dw2_instantiate_symtab to skip partial CUs -- any important
3878 partial CU will be read via DW_TAG_imported_unit anyway. */
3879 dw2_instantiate_symtab (per_cu, per_objfile, true);
3880 }
3881 }
3882
3883 static void
3884 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3885 const char *fullname)
3886 {
3887 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3888
3889 /* We don't need to consider type units here.
3890 This is only called for examining code, e.g. expand_line_sal.
3891 There can be an order of magnitude (or more) more type units
3892 than comp units, and we avoid them if we can. */
3893
3894 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3895 {
3896 /* We only need to look at symtabs not already expanded. */
3897 if (per_objfile->symtab_set_p (per_cu))
3898 continue;
3899
3900 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
3901 if (file_data == NULL)
3902 continue;
3903
3904 for (int j = 0; j < file_data->num_file_names; ++j)
3905 {
3906 const char *this_fullname = file_data->file_names[j];
3907
3908 if (filename_cmp (this_fullname, fullname) == 0)
3909 {
3910 dw2_instantiate_symtab (per_cu, per_objfile, false);
3911 break;
3912 }
3913 }
3914 }
3915 }
3916
3917 static void
3918 dw2_expand_symtabs_matching_symbol
3919 (mapped_index_base &index,
3920 const lookup_name_info &lookup_name_in,
3921 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3922 enum search_domain kind,
3923 gdb::function_view<bool (offset_type)> match_callback,
3924 dwarf2_per_objfile *per_objfile);
3925
3926 static void
3927 dw2_expand_symtabs_matching_one
3928 (dwarf2_per_cu_data *per_cu,
3929 dwarf2_per_objfile *per_objfile,
3930 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3931 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3932
3933 static void
3934 dw2_map_matching_symbols
3935 (struct objfile *objfile,
3936 const lookup_name_info &name, domain_enum domain,
3937 int global,
3938 gdb::function_view<symbol_found_callback_ftype> callback,
3939 symbol_compare_ftype *ordered_compare)
3940 {
3941 /* Used for Ada. */
3942 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3943
3944 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3945
3946 if (per_objfile->per_bfd->index_table != nullptr)
3947 {
3948 mapped_index &index = *per_objfile->per_bfd->index_table;
3949
3950 const char *match_name = name.ada ().lookup_name ().c_str ();
3951 auto matcher = [&] (const char *symname)
3952 {
3953 if (ordered_compare == nullptr)
3954 return true;
3955 return ordered_compare (symname, match_name) == 0;
3956 };
3957
3958 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3959 [&] (offset_type namei)
3960 {
3961 struct dw2_symtab_iterator iter;
3962 struct dwarf2_per_cu_data *per_cu;
3963
3964 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3965 namei);
3966 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3967 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3968 nullptr);
3969 return true;
3970 }, per_objfile);
3971 }
3972 else
3973 {
3974 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3975 proceed assuming all symtabs have been read in. */
3976 }
3977
3978 for (compunit_symtab *cust : objfile->compunits ())
3979 {
3980 const struct block *block;
3981
3982 if (cust == NULL)
3983 continue;
3984 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3985 if (!iterate_over_symbols_terminated (block, name,
3986 domain, callback))
3987 return;
3988 }
3989 }
3990
3991 /* Starting from a search name, return the string that finds the upper
3992 bound of all strings that start with SEARCH_NAME in a sorted name
3993 list. Returns the empty string to indicate that the upper bound is
3994 the end of the list. */
3995
3996 static std::string
3997 make_sort_after_prefix_name (const char *search_name)
3998 {
3999 /* When looking to complete "func", we find the upper bound of all
4000 symbols that start with "func" by looking for where we'd insert
4001 the closest string that would follow "func" in lexicographical
4002 order. Usually, that's "func"-with-last-character-incremented,
4003 i.e. "fund". Mind non-ASCII characters, though. Usually those
4004 will be UTF-8 multi-byte sequences, but we can't be certain.
4005 Especially mind the 0xff character, which is a valid character in
4006 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4007 rule out compilers allowing it in identifiers. Note that
4008 conveniently, strcmp/strcasecmp are specified to compare
4009 characters interpreted as unsigned char. So what we do is treat
4010 the whole string as a base 256 number composed of a sequence of
4011 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4012 to 0, and carries 1 to the following more-significant position.
4013 If the very first character in SEARCH_NAME ends up incremented
4014 and carries/overflows, then the upper bound is the end of the
4015 list. The string after the empty string is also the empty
4016 string.
4017
4018 Some examples of this operation:
4019
4020 SEARCH_NAME => "+1" RESULT
4021
4022 "abc" => "abd"
4023 "ab\xff" => "ac"
4024 "\xff" "a" "\xff" => "\xff" "b"
4025 "\xff" => ""
4026 "\xff\xff" => ""
4027 "" => ""
4028
4029 Then, with these symbols for example:
4030
4031 func
4032 func1
4033 fund
4034
4035 completing "func" looks for symbols between "func" and
4036 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4037 which finds "func" and "func1", but not "fund".
4038
4039 And with:
4040
4041 funcÿ (Latin1 'ÿ' [0xff])
4042 funcÿ1
4043 fund
4044
4045 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4046 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4047
4048 And with:
4049
4050 ÿÿ (Latin1 'ÿ' [0xff])
4051 ÿÿ1
4052
4053 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4054 the end of the list.
4055 */
4056 std::string after = search_name;
4057 while (!after.empty () && (unsigned char) after.back () == 0xff)
4058 after.pop_back ();
4059 if (!after.empty ())
4060 after.back () = (unsigned char) after.back () + 1;
4061 return after;
4062 }
4063
4064 /* See declaration. */
4065
4066 std::pair<std::vector<name_component>::const_iterator,
4067 std::vector<name_component>::const_iterator>
4068 mapped_index_base::find_name_components_bounds
4069 (const lookup_name_info &lookup_name_without_params, language lang,
4070 dwarf2_per_objfile *per_objfile) const
4071 {
4072 auto *name_cmp
4073 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4074
4075 const char *lang_name
4076 = lookup_name_without_params.language_lookup_name (lang);
4077
4078 /* Comparison function object for lower_bound that matches against a
4079 given symbol name. */
4080 auto lookup_compare_lower = [&] (const name_component &elem,
4081 const char *name)
4082 {
4083 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4084 const char *elem_name = elem_qualified + elem.name_offset;
4085 return name_cmp (elem_name, name) < 0;
4086 };
4087
4088 /* Comparison function object for upper_bound that matches against a
4089 given symbol name. */
4090 auto lookup_compare_upper = [&] (const char *name,
4091 const name_component &elem)
4092 {
4093 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
4094 const char *elem_name = elem_qualified + elem.name_offset;
4095 return name_cmp (name, elem_name) < 0;
4096 };
4097
4098 auto begin = this->name_components.begin ();
4099 auto end = this->name_components.end ();
4100
4101 /* Find the lower bound. */
4102 auto lower = [&] ()
4103 {
4104 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
4105 return begin;
4106 else
4107 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
4108 } ();
4109
4110 /* Find the upper bound. */
4111 auto upper = [&] ()
4112 {
4113 if (lookup_name_without_params.completion_mode ())
4114 {
4115 /* In completion mode, we want UPPER to point past all
4116 symbols names that have the same prefix. I.e., with
4117 these symbols, and completing "func":
4118
4119 function << lower bound
4120 function1
4121 other_function << upper bound
4122
4123 We find the upper bound by looking for the insertion
4124 point of "func"-with-last-character-incremented,
4125 i.e. "fund". */
4126 std::string after = make_sort_after_prefix_name (lang_name);
4127 if (after.empty ())
4128 return end;
4129 return std::lower_bound (lower, end, after.c_str (),
4130 lookup_compare_lower);
4131 }
4132 else
4133 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
4134 } ();
4135
4136 return {lower, upper};
4137 }
4138
4139 /* See declaration. */
4140
4141 void
4142 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
4143 {
4144 if (!this->name_components.empty ())
4145 return;
4146
4147 this->name_components_casing = case_sensitivity;
4148 auto *name_cmp
4149 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4150
4151 /* The code below only knows how to break apart components of C++
4152 symbol names (and other languages that use '::' as
4153 namespace/module separator) and Ada symbol names. */
4154 auto count = this->symbol_name_count ();
4155 for (offset_type idx = 0; idx < count; idx++)
4156 {
4157 if (this->symbol_name_slot_invalid (idx))
4158 continue;
4159
4160 const char *name = this->symbol_name_at (idx, per_objfile);
4161
4162 /* Add each name component to the name component table. */
4163 unsigned int previous_len = 0;
4164
4165 if (strstr (name, "::") != nullptr)
4166 {
4167 for (unsigned int current_len = cp_find_first_component (name);
4168 name[current_len] != '\0';
4169 current_len += cp_find_first_component (name + current_len))
4170 {
4171 gdb_assert (name[current_len] == ':');
4172 this->name_components.push_back ({previous_len, idx});
4173 /* Skip the '::'. */
4174 current_len += 2;
4175 previous_len = current_len;
4176 }
4177 }
4178 else
4179 {
4180 /* Handle the Ada encoded (aka mangled) form here. */
4181 for (const char *iter = strstr (name, "__");
4182 iter != nullptr;
4183 iter = strstr (iter, "__"))
4184 {
4185 this->name_components.push_back ({previous_len, idx});
4186 iter += 2;
4187 previous_len = iter - name;
4188 }
4189 }
4190
4191 this->name_components.push_back ({previous_len, idx});
4192 }
4193
4194 /* Sort name_components elements by name. */
4195 auto name_comp_compare = [&] (const name_component &left,
4196 const name_component &right)
4197 {
4198 const char *left_qualified
4199 = this->symbol_name_at (left.idx, per_objfile);
4200 const char *right_qualified
4201 = this->symbol_name_at (right.idx, per_objfile);
4202
4203 const char *left_name = left_qualified + left.name_offset;
4204 const char *right_name = right_qualified + right.name_offset;
4205
4206 return name_cmp (left_name, right_name) < 0;
4207 };
4208
4209 std::sort (this->name_components.begin (),
4210 this->name_components.end (),
4211 name_comp_compare);
4212 }
4213
4214 /* Helper for dw2_expand_symtabs_matching that works with a
4215 mapped_index_base instead of the containing objfile. This is split
4216 to a separate function in order to be able to unit test the
4217 name_components matching using a mock mapped_index_base. For each
4218 symbol name that matches, calls MATCH_CALLBACK, passing it the
4219 symbol's index in the mapped_index_base symbol table. */
4220
4221 static void
4222 dw2_expand_symtabs_matching_symbol
4223 (mapped_index_base &index,
4224 const lookup_name_info &lookup_name_in,
4225 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4226 enum search_domain kind,
4227 gdb::function_view<bool (offset_type)> match_callback,
4228 dwarf2_per_objfile *per_objfile)
4229 {
4230 lookup_name_info lookup_name_without_params
4231 = lookup_name_in.make_ignore_params ();
4232
4233 /* Build the symbol name component sorted vector, if we haven't
4234 yet. */
4235 index.build_name_components (per_objfile);
4236
4237 /* The same symbol may appear more than once in the range though.
4238 E.g., if we're looking for symbols that complete "w", and we have
4239 a symbol named "w1::w2", we'll find the two name components for
4240 that same symbol in the range. To be sure we only call the
4241 callback once per symbol, we first collect the symbol name
4242 indexes that matched in a temporary vector and ignore
4243 duplicates. */
4244 std::vector<offset_type> matches;
4245
4246 struct name_and_matcher
4247 {
4248 symbol_name_matcher_ftype *matcher;
4249 const char *name;
4250
4251 bool operator== (const name_and_matcher &other) const
4252 {
4253 return matcher == other.matcher && strcmp (name, other.name) == 0;
4254 }
4255 };
4256
4257 /* A vector holding all the different symbol name matchers, for all
4258 languages. */
4259 std::vector<name_and_matcher> matchers;
4260
4261 for (int i = 0; i < nr_languages; i++)
4262 {
4263 enum language lang_e = (enum language) i;
4264
4265 const language_defn *lang = language_def (lang_e);
4266 symbol_name_matcher_ftype *name_matcher
4267 = lang->get_symbol_name_matcher (lookup_name_without_params);
4268
4269 name_and_matcher key {
4270 name_matcher,
4271 lookup_name_without_params.language_lookup_name (lang_e)
4272 };
4273
4274 /* Don't insert the same comparison routine more than once.
4275 Note that we do this linear walk. This is not a problem in
4276 practice because the number of supported languages is
4277 low. */
4278 if (std::find (matchers.begin (), matchers.end (), key)
4279 != matchers.end ())
4280 continue;
4281 matchers.push_back (std::move (key));
4282
4283 auto bounds
4284 = index.find_name_components_bounds (lookup_name_without_params,
4285 lang_e, per_objfile);
4286
4287 /* Now for each symbol name in range, check to see if we have a name
4288 match, and if so, call the MATCH_CALLBACK callback. */
4289
4290 for (; bounds.first != bounds.second; ++bounds.first)
4291 {
4292 const char *qualified
4293 = index.symbol_name_at (bounds.first->idx, per_objfile);
4294
4295 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4296 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4297 continue;
4298
4299 matches.push_back (bounds.first->idx);
4300 }
4301 }
4302
4303 std::sort (matches.begin (), matches.end ());
4304
4305 /* Finally call the callback, once per match. */
4306 ULONGEST prev = -1;
4307 for (offset_type idx : matches)
4308 {
4309 if (prev != idx)
4310 {
4311 if (!match_callback (idx))
4312 break;
4313 prev = idx;
4314 }
4315 }
4316
4317 /* Above we use a type wider than idx's for 'prev', since 0 and
4318 (offset_type)-1 are both possible values. */
4319 static_assert (sizeof (prev) > sizeof (offset_type), "");
4320 }
4321
4322 #if GDB_SELF_TEST
4323
4324 namespace selftests { namespace dw2_expand_symtabs_matching {
4325
4326 /* A mock .gdb_index/.debug_names-like name index table, enough to
4327 exercise dw2_expand_symtabs_matching_symbol, which works with the
4328 mapped_index_base interface. Builds an index from the symbol list
4329 passed as parameter to the constructor. */
4330 class mock_mapped_index : public mapped_index_base
4331 {
4332 public:
4333 mock_mapped_index (gdb::array_view<const char *> symbols)
4334 : m_symbol_table (symbols)
4335 {}
4336
4337 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
4338
4339 /* Return the number of names in the symbol table. */
4340 size_t symbol_name_count () const override
4341 {
4342 return m_symbol_table.size ();
4343 }
4344
4345 /* Get the name of the symbol at IDX in the symbol table. */
4346 const char *symbol_name_at
4347 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
4348 {
4349 return m_symbol_table[idx];
4350 }
4351
4352 private:
4353 gdb::array_view<const char *> m_symbol_table;
4354 };
4355
4356 /* Convenience function that converts a NULL pointer to a "<null>"
4357 string, to pass to print routines. */
4358
4359 static const char *
4360 string_or_null (const char *str)
4361 {
4362 return str != NULL ? str : "<null>";
4363 }
4364
4365 /* Check if a lookup_name_info built from
4366 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4367 index. EXPECTED_LIST is the list of expected matches, in expected
4368 matching order. If no match expected, then an empty list is
4369 specified. Returns true on success. On failure prints a warning
4370 indicating the file:line that failed, and returns false. */
4371
4372 static bool
4373 check_match (const char *file, int line,
4374 mock_mapped_index &mock_index,
4375 const char *name, symbol_name_match_type match_type,
4376 bool completion_mode,
4377 std::initializer_list<const char *> expected_list,
4378 dwarf2_per_objfile *per_objfile)
4379 {
4380 lookup_name_info lookup_name (name, match_type, completion_mode);
4381
4382 bool matched = true;
4383
4384 auto mismatch = [&] (const char *expected_str,
4385 const char *got)
4386 {
4387 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4388 "expected=\"%s\", got=\"%s\"\n"),
4389 file, line,
4390 (match_type == symbol_name_match_type::FULL
4391 ? "FULL" : "WILD"),
4392 name, string_or_null (expected_str), string_or_null (got));
4393 matched = false;
4394 };
4395
4396 auto expected_it = expected_list.begin ();
4397 auto expected_end = expected_list.end ();
4398
4399 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4400 NULL, ALL_DOMAIN,
4401 [&] (offset_type idx)
4402 {
4403 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4404 const char *expected_str
4405 = expected_it == expected_end ? NULL : *expected_it++;
4406
4407 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4408 mismatch (expected_str, matched_name);
4409 return true;
4410 }, per_objfile);
4411
4412 const char *expected_str
4413 = expected_it == expected_end ? NULL : *expected_it++;
4414 if (expected_str != NULL)
4415 mismatch (expected_str, NULL);
4416
4417 return matched;
4418 }
4419
4420 /* The symbols added to the mock mapped_index for testing (in
4421 canonical form). */
4422 static const char *test_symbols[] = {
4423 "function",
4424 "std::bar",
4425 "std::zfunction",
4426 "std::zfunction2",
4427 "w1::w2",
4428 "ns::foo<char*>",
4429 "ns::foo<int>",
4430 "ns::foo<long>",
4431 "ns2::tmpl<int>::foo2",
4432 "(anonymous namespace)::A::B::C",
4433
4434 /* These are used to check that the increment-last-char in the
4435 matching algorithm for completion doesn't match "t1_fund" when
4436 completing "t1_func". */
4437 "t1_func",
4438 "t1_func1",
4439 "t1_fund",
4440 "t1_fund1",
4441
4442 /* A UTF-8 name with multi-byte sequences to make sure that
4443 cp-name-parser understands this as a single identifier ("função"
4444 is "function" in PT). */
4445 u8"u8função",
4446
4447 /* \377 (0xff) is Latin1 'ÿ'. */
4448 "yfunc\377",
4449
4450 /* \377 (0xff) is Latin1 'ÿ'. */
4451 "\377",
4452 "\377\377123",
4453
4454 /* A name with all sorts of complications. Starts with "z" to make
4455 it easier for the completion tests below. */
4456 #define Z_SYM_NAME \
4457 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4458 "::tuple<(anonymous namespace)::ui*, " \
4459 "std::default_delete<(anonymous namespace)::ui>, void>"
4460
4461 Z_SYM_NAME
4462 };
4463
4464 /* Returns true if the mapped_index_base::find_name_component_bounds
4465 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4466 in completion mode. */
4467
4468 static bool
4469 check_find_bounds_finds (mapped_index_base &index,
4470 const char *search_name,
4471 gdb::array_view<const char *> expected_syms,
4472 dwarf2_per_objfile *per_objfile)
4473 {
4474 lookup_name_info lookup_name (search_name,
4475 symbol_name_match_type::FULL, true);
4476
4477 auto bounds = index.find_name_components_bounds (lookup_name,
4478 language_cplus,
4479 per_objfile);
4480
4481 size_t distance = std::distance (bounds.first, bounds.second);
4482 if (distance != expected_syms.size ())
4483 return false;
4484
4485 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4486 {
4487 auto nc_elem = bounds.first + exp_elem;
4488 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4489 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4490 return false;
4491 }
4492
4493 return true;
4494 }
4495
4496 /* Test the lower-level mapped_index::find_name_component_bounds
4497 method. */
4498
4499 static void
4500 test_mapped_index_find_name_component_bounds ()
4501 {
4502 mock_mapped_index mock_index (test_symbols);
4503
4504 mock_index.build_name_components (NULL /* per_objfile */);
4505
4506 /* Test the lower-level mapped_index::find_name_component_bounds
4507 method in completion mode. */
4508 {
4509 static const char *expected_syms[] = {
4510 "t1_func",
4511 "t1_func1",
4512 };
4513
4514 SELF_CHECK (check_find_bounds_finds
4515 (mock_index, "t1_func", expected_syms,
4516 NULL /* per_objfile */));
4517 }
4518
4519 /* Check that the increment-last-char in the name matching algorithm
4520 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4521 {
4522 static const char *expected_syms1[] = {
4523 "\377",
4524 "\377\377123",
4525 };
4526 SELF_CHECK (check_find_bounds_finds
4527 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4528
4529 static const char *expected_syms2[] = {
4530 "\377\377123",
4531 };
4532 SELF_CHECK (check_find_bounds_finds
4533 (mock_index, "\377\377", expected_syms2,
4534 NULL /* per_objfile */));
4535 }
4536 }
4537
4538 /* Test dw2_expand_symtabs_matching_symbol. */
4539
4540 static void
4541 test_dw2_expand_symtabs_matching_symbol ()
4542 {
4543 mock_mapped_index mock_index (test_symbols);
4544
4545 /* We let all tests run until the end even if some fails, for debug
4546 convenience. */
4547 bool any_mismatch = false;
4548
4549 /* Create the expected symbols list (an initializer_list). Needed
4550 because lists have commas, and we need to pass them to CHECK,
4551 which is a macro. */
4552 #define EXPECT(...) { __VA_ARGS__ }
4553
4554 /* Wrapper for check_match that passes down the current
4555 __FILE__/__LINE__. */
4556 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4557 any_mismatch |= !check_match (__FILE__, __LINE__, \
4558 mock_index, \
4559 NAME, MATCH_TYPE, COMPLETION_MODE, \
4560 EXPECTED_LIST, NULL)
4561
4562 /* Identity checks. */
4563 for (const char *sym : test_symbols)
4564 {
4565 /* Should be able to match all existing symbols. */
4566 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4567 EXPECT (sym));
4568
4569 /* Should be able to match all existing symbols with
4570 parameters. */
4571 std::string with_params = std::string (sym) + "(int)";
4572 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4573 EXPECT (sym));
4574
4575 /* Should be able to match all existing symbols with
4576 parameters and qualifiers. */
4577 with_params = std::string (sym) + " ( int ) const";
4578 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4579 EXPECT (sym));
4580
4581 /* This should really find sym, but cp-name-parser.y doesn't
4582 know about lvalue/rvalue qualifiers yet. */
4583 with_params = std::string (sym) + " ( int ) &&";
4584 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4585 {});
4586 }
4587
4588 /* Check that the name matching algorithm for completion doesn't get
4589 confused with Latin1 'ÿ' / 0xff. */
4590 {
4591 static const char str[] = "\377";
4592 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4593 EXPECT ("\377", "\377\377123"));
4594 }
4595
4596 /* Check that the increment-last-char in the matching algorithm for
4597 completion doesn't match "t1_fund" when completing "t1_func". */
4598 {
4599 static const char str[] = "t1_func";
4600 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4601 EXPECT ("t1_func", "t1_func1"));
4602 }
4603
4604 /* Check that completion mode works at each prefix of the expected
4605 symbol name. */
4606 {
4607 static const char str[] = "function(int)";
4608 size_t len = strlen (str);
4609 std::string lookup;
4610
4611 for (size_t i = 1; i < len; i++)
4612 {
4613 lookup.assign (str, i);
4614 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4615 EXPECT ("function"));
4616 }
4617 }
4618
4619 /* While "w" is a prefix of both components, the match function
4620 should still only be called once. */
4621 {
4622 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4623 EXPECT ("w1::w2"));
4624 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4625 EXPECT ("w1::w2"));
4626 }
4627
4628 /* Same, with a "complicated" symbol. */
4629 {
4630 static const char str[] = Z_SYM_NAME;
4631 size_t len = strlen (str);
4632 std::string lookup;
4633
4634 for (size_t i = 1; i < len; i++)
4635 {
4636 lookup.assign (str, i);
4637 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4638 EXPECT (Z_SYM_NAME));
4639 }
4640 }
4641
4642 /* In FULL mode, an incomplete symbol doesn't match. */
4643 {
4644 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4645 {});
4646 }
4647
4648 /* A complete symbol with parameters matches any overload, since the
4649 index has no overload info. */
4650 {
4651 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4652 EXPECT ("std::zfunction", "std::zfunction2"));
4653 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4654 EXPECT ("std::zfunction", "std::zfunction2"));
4655 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4656 EXPECT ("std::zfunction", "std::zfunction2"));
4657 }
4658
4659 /* Check that whitespace is ignored appropriately. A symbol with a
4660 template argument list. */
4661 {
4662 static const char expected[] = "ns::foo<int>";
4663 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4664 EXPECT (expected));
4665 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4666 EXPECT (expected));
4667 }
4668
4669 /* Check that whitespace is ignored appropriately. A symbol with a
4670 template argument list that includes a pointer. */
4671 {
4672 static const char expected[] = "ns::foo<char*>";
4673 /* Try both completion and non-completion modes. */
4674 static const bool completion_mode[2] = {false, true};
4675 for (size_t i = 0; i < 2; i++)
4676 {
4677 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4678 completion_mode[i], EXPECT (expected));
4679 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4680 completion_mode[i], EXPECT (expected));
4681
4682 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4683 completion_mode[i], EXPECT (expected));
4684 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4685 completion_mode[i], EXPECT (expected));
4686 }
4687 }
4688
4689 {
4690 /* Check method qualifiers are ignored. */
4691 static const char expected[] = "ns::foo<char*>";
4692 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4693 symbol_name_match_type::FULL, true, EXPECT (expected));
4694 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4695 symbol_name_match_type::FULL, true, EXPECT (expected));
4696 CHECK_MATCH ("foo < char * > ( int ) const",
4697 symbol_name_match_type::WILD, true, EXPECT (expected));
4698 CHECK_MATCH ("foo < char * > ( int ) &&",
4699 symbol_name_match_type::WILD, true, EXPECT (expected));
4700 }
4701
4702 /* Test lookup names that don't match anything. */
4703 {
4704 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4705 {});
4706
4707 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4708 {});
4709 }
4710
4711 /* Some wild matching tests, exercising "(anonymous namespace)",
4712 which should not be confused with a parameter list. */
4713 {
4714 static const char *syms[] = {
4715 "A::B::C",
4716 "B::C",
4717 "C",
4718 "A :: B :: C ( int )",
4719 "B :: C ( int )",
4720 "C ( int )",
4721 };
4722
4723 for (const char *s : syms)
4724 {
4725 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4726 EXPECT ("(anonymous namespace)::A::B::C"));
4727 }
4728 }
4729
4730 {
4731 static const char expected[] = "ns2::tmpl<int>::foo2";
4732 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4733 EXPECT (expected));
4734 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4735 EXPECT (expected));
4736 }
4737
4738 SELF_CHECK (!any_mismatch);
4739
4740 #undef EXPECT
4741 #undef CHECK_MATCH
4742 }
4743
4744 static void
4745 run_test ()
4746 {
4747 test_mapped_index_find_name_component_bounds ();
4748 test_dw2_expand_symtabs_matching_symbol ();
4749 }
4750
4751 }} // namespace selftests::dw2_expand_symtabs_matching
4752
4753 #endif /* GDB_SELF_TEST */
4754
4755 /* If FILE_MATCHER is NULL or if PER_CU has
4756 dwarf2_per_cu_quick_data::MARK set (see
4757 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4758 EXPANSION_NOTIFY on it. */
4759
4760 static void
4761 dw2_expand_symtabs_matching_one
4762 (dwarf2_per_cu_data *per_cu,
4763 dwarf2_per_objfile *per_objfile,
4764 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4765 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4766 {
4767 if (file_matcher == NULL || per_cu->v.quick->mark)
4768 {
4769 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4770
4771 compunit_symtab *symtab
4772 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4773 gdb_assert (symtab != nullptr);
4774
4775 if (expansion_notify != NULL && symtab_was_null)
4776 expansion_notify (symtab);
4777 }
4778 }
4779
4780 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4781 matched, to expand corresponding CUs that were marked. IDX is the
4782 index of the symbol name that matched. */
4783
4784 static void
4785 dw2_expand_marked_cus
4786 (dwarf2_per_objfile *per_objfile, offset_type idx,
4787 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4788 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4789 search_domain kind)
4790 {
4791 offset_type *vec, vec_len, vec_idx;
4792 bool global_seen = false;
4793 mapped_index &index = *per_objfile->per_bfd->index_table;
4794
4795 vec = (offset_type *) (index.constant_pool
4796 + MAYBE_SWAP (index.symbol_table[idx].vec));
4797 vec_len = MAYBE_SWAP (vec[0]);
4798 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4799 {
4800 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4801 /* This value is only valid for index versions >= 7. */
4802 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4803 gdb_index_symbol_kind symbol_kind =
4804 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4805 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4806 /* Only check the symbol attributes if they're present.
4807 Indices prior to version 7 don't record them,
4808 and indices >= 7 may elide them for certain symbols
4809 (gold does this). */
4810 int attrs_valid =
4811 (index.version >= 7
4812 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4813
4814 /* Work around gold/15646. */
4815 if (attrs_valid
4816 && !is_static
4817 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4818 {
4819 if (global_seen)
4820 continue;
4821
4822 global_seen = true;
4823 }
4824
4825 /* Only check the symbol's kind if it has one. */
4826 if (attrs_valid)
4827 {
4828 switch (kind)
4829 {
4830 case VARIABLES_DOMAIN:
4831 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4832 continue;
4833 break;
4834 case FUNCTIONS_DOMAIN:
4835 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4836 continue;
4837 break;
4838 case TYPES_DOMAIN:
4839 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4840 continue;
4841 break;
4842 case MODULES_DOMAIN:
4843 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4844 continue;
4845 break;
4846 default:
4847 break;
4848 }
4849 }
4850
4851 /* Don't crash on bad data. */
4852 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4853 + per_objfile->per_bfd->all_type_units.size ()))
4854 {
4855 complaint (_(".gdb_index entry has bad CU index"
4856 " [in module %s]"), objfile_name (per_objfile->objfile));
4857 continue;
4858 }
4859
4860 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4861 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4862 expansion_notify);
4863 }
4864 }
4865
4866 /* If FILE_MATCHER is non-NULL, set all the
4867 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4868 that match FILE_MATCHER. */
4869
4870 static void
4871 dw_expand_symtabs_matching_file_matcher
4872 (dwarf2_per_objfile *per_objfile,
4873 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4874 {
4875 if (file_matcher == NULL)
4876 return;
4877
4878 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4879 htab_eq_pointer,
4880 NULL, xcalloc, xfree));
4881 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4882 htab_eq_pointer,
4883 NULL, xcalloc, xfree));
4884
4885 /* The rule is CUs specify all the files, including those used by
4886 any TU, so there's no need to scan TUs here. */
4887
4888 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4889 {
4890 QUIT;
4891
4892 per_cu->v.quick->mark = 0;
4893
4894 /* We only need to look at symtabs not already expanded. */
4895 if (per_objfile->symtab_set_p (per_cu))
4896 continue;
4897
4898 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4899 if (file_data == NULL)
4900 continue;
4901
4902 if (htab_find (visited_not_found.get (), file_data) != NULL)
4903 continue;
4904 else if (htab_find (visited_found.get (), file_data) != NULL)
4905 {
4906 per_cu->v.quick->mark = 1;
4907 continue;
4908 }
4909
4910 for (int j = 0; j < file_data->num_file_names; ++j)
4911 {
4912 const char *this_real_name;
4913
4914 if (file_matcher (file_data->file_names[j], false))
4915 {
4916 per_cu->v.quick->mark = 1;
4917 break;
4918 }
4919
4920 /* Before we invoke realpath, which can get expensive when many
4921 files are involved, do a quick comparison of the basenames. */
4922 if (!basenames_may_differ
4923 && !file_matcher (lbasename (file_data->file_names[j]),
4924 true))
4925 continue;
4926
4927 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4928 if (file_matcher (this_real_name, false))
4929 {
4930 per_cu->v.quick->mark = 1;
4931 break;
4932 }
4933 }
4934
4935 void **slot = htab_find_slot (per_cu->v.quick->mark
4936 ? visited_found.get ()
4937 : visited_not_found.get (),
4938 file_data, INSERT);
4939 *slot = file_data;
4940 }
4941 }
4942
4943 static void
4944 dw2_expand_symtabs_matching
4945 (struct objfile *objfile,
4946 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4947 const lookup_name_info *lookup_name,
4948 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4949 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4950 enum search_domain kind)
4951 {
4952 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4953
4954 /* index_table is NULL if OBJF_READNOW. */
4955 if (!per_objfile->per_bfd->index_table)
4956 return;
4957
4958 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4959
4960 if (symbol_matcher == NULL && lookup_name == NULL)
4961 {
4962 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4963 {
4964 QUIT;
4965
4966 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4967 file_matcher, expansion_notify);
4968 }
4969 return;
4970 }
4971
4972 mapped_index &index = *per_objfile->per_bfd->index_table;
4973
4974 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4975 symbol_matcher,
4976 kind, [&] (offset_type idx)
4977 {
4978 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4979 kind);
4980 return true;
4981 }, per_objfile);
4982 }
4983
4984 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4985 symtab. */
4986
4987 static struct compunit_symtab *
4988 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4989 CORE_ADDR pc)
4990 {
4991 int i;
4992
4993 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4994 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4995 return cust;
4996
4997 if (cust->includes == NULL)
4998 return NULL;
4999
5000 for (i = 0; cust->includes[i]; ++i)
5001 {
5002 struct compunit_symtab *s = cust->includes[i];
5003
5004 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5005 if (s != NULL)
5006 return s;
5007 }
5008
5009 return NULL;
5010 }
5011
5012 static struct compunit_symtab *
5013 dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5014 struct bound_minimal_symbol msymbol,
5015 CORE_ADDR pc,
5016 struct obj_section *section,
5017 int warn_if_readin)
5018 {
5019 struct dwarf2_per_cu_data *data;
5020 struct compunit_symtab *result;
5021
5022 if (!objfile->partial_symtabs->psymtabs_addrmap)
5023 return NULL;
5024
5025 CORE_ADDR baseaddr = objfile->text_section_offset ();
5026 data = (struct dwarf2_per_cu_data *) addrmap_find
5027 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
5028 if (!data)
5029 return NULL;
5030
5031 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5032 if (warn_if_readin && per_objfile->symtab_set_p (data))
5033 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5034 paddress (objfile->arch (), pc));
5035
5036 result = recursively_find_pc_sect_compunit_symtab
5037 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5038
5039 gdb_assert (result != NULL);
5040 return result;
5041 }
5042
5043 static void
5044 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5045 void *data, int need_fullname)
5046 {
5047 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5048
5049 if (!per_objfile->per_bfd->filenames_cache)
5050 {
5051 per_objfile->per_bfd->filenames_cache.emplace ();
5052
5053 htab_up visited (htab_create_alloc (10,
5054 htab_hash_pointer, htab_eq_pointer,
5055 NULL, xcalloc, xfree));
5056
5057 /* The rule is CUs specify all the files, including those used
5058 by any TU, so there's no need to scan TUs here. We can
5059 ignore file names coming from already-expanded CUs. */
5060
5061 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5062 {
5063 if (per_objfile->symtab_set_p (per_cu))
5064 {
5065 void **slot = htab_find_slot (visited.get (),
5066 per_cu->v.quick->file_names,
5067 INSERT);
5068
5069 *slot = per_cu->v.quick->file_names;
5070 }
5071 }
5072
5073 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5074 {
5075 /* We only need to look at symtabs not already expanded. */
5076 if (per_objfile->symtab_set_p (per_cu))
5077 continue;
5078
5079 quick_file_names *file_data
5080 = dw2_get_file_names (per_cu, per_objfile);
5081 if (file_data == NULL)
5082 continue;
5083
5084 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
5085 if (*slot)
5086 {
5087 /* Already visited. */
5088 continue;
5089 }
5090 *slot = file_data;
5091
5092 for (int j = 0; j < file_data->num_file_names; ++j)
5093 {
5094 const char *filename = file_data->file_names[j];
5095 per_objfile->per_bfd->filenames_cache->seen (filename);
5096 }
5097 }
5098 }
5099
5100 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
5101 {
5102 gdb::unique_xmalloc_ptr<char> this_real_name;
5103
5104 if (need_fullname)
5105 this_real_name = gdb_realpath (filename);
5106 (*fun) (filename, this_real_name.get (), data);
5107 });
5108 }
5109
5110 static int
5111 dw2_has_symbols (struct objfile *objfile)
5112 {
5113 return 1;
5114 }
5115
5116 const struct quick_symbol_functions dwarf2_gdb_index_functions =
5117 {
5118 dw2_has_symbols,
5119 dw2_find_last_source_symtab,
5120 dw2_forget_cached_source_info,
5121 dw2_map_symtabs_matching_filename,
5122 dw2_lookup_symbol,
5123 NULL,
5124 dw2_print_stats,
5125 dw2_dump,
5126 dw2_expand_symtabs_for_function,
5127 dw2_expand_all_symtabs,
5128 dw2_expand_symtabs_with_fullname,
5129 dw2_map_matching_symbols,
5130 dw2_expand_symtabs_matching,
5131 dw2_find_pc_sect_compunit_symtab,
5132 NULL,
5133 dw2_map_symbol_filenames
5134 };
5135
5136 /* DWARF-5 debug_names reader. */
5137
5138 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5139 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5140
5141 /* A helper function that reads the .debug_names section in SECTION
5142 and fills in MAP. FILENAME is the name of the file containing the
5143 section; it is used for error reporting.
5144
5145 Returns true if all went well, false otherwise. */
5146
5147 static bool
5148 read_debug_names_from_section (struct objfile *objfile,
5149 const char *filename,
5150 struct dwarf2_section_info *section,
5151 mapped_debug_names &map)
5152 {
5153 if (section->empty ())
5154 return false;
5155
5156 /* Older elfutils strip versions could keep the section in the main
5157 executable while splitting it for the separate debug info file. */
5158 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5159 return false;
5160
5161 section->read (objfile);
5162
5163 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
5164
5165 const gdb_byte *addr = section->buffer;
5166
5167 bfd *const abfd = section->get_bfd_owner ();
5168
5169 unsigned int bytes_read;
5170 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5171 addr += bytes_read;
5172
5173 map.dwarf5_is_dwarf64 = bytes_read != 4;
5174 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5175 if (bytes_read + length != section->size)
5176 {
5177 /* There may be multiple per-CU indices. */
5178 warning (_("Section .debug_names in %s length %s does not match "
5179 "section length %s, ignoring .debug_names."),
5180 filename, plongest (bytes_read + length),
5181 pulongest (section->size));
5182 return false;
5183 }
5184
5185 /* The version number. */
5186 uint16_t version = read_2_bytes (abfd, addr);
5187 addr += 2;
5188 if (version != 5)
5189 {
5190 warning (_("Section .debug_names in %s has unsupported version %d, "
5191 "ignoring .debug_names."),
5192 filename, version);
5193 return false;
5194 }
5195
5196 /* Padding. */
5197 uint16_t padding = read_2_bytes (abfd, addr);
5198 addr += 2;
5199 if (padding != 0)
5200 {
5201 warning (_("Section .debug_names in %s has unsupported padding %d, "
5202 "ignoring .debug_names."),
5203 filename, padding);
5204 return false;
5205 }
5206
5207 /* comp_unit_count - The number of CUs in the CU list. */
5208 map.cu_count = read_4_bytes (abfd, addr);
5209 addr += 4;
5210
5211 /* local_type_unit_count - The number of TUs in the local TU
5212 list. */
5213 map.tu_count = read_4_bytes (abfd, addr);
5214 addr += 4;
5215
5216 /* foreign_type_unit_count - The number of TUs in the foreign TU
5217 list. */
5218 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5219 addr += 4;
5220 if (foreign_tu_count != 0)
5221 {
5222 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5223 "ignoring .debug_names."),
5224 filename, static_cast<unsigned long> (foreign_tu_count));
5225 return false;
5226 }
5227
5228 /* bucket_count - The number of hash buckets in the hash lookup
5229 table. */
5230 map.bucket_count = read_4_bytes (abfd, addr);
5231 addr += 4;
5232
5233 /* name_count - The number of unique names in the index. */
5234 map.name_count = read_4_bytes (abfd, addr);
5235 addr += 4;
5236
5237 /* abbrev_table_size - The size in bytes of the abbreviations
5238 table. */
5239 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5240 addr += 4;
5241
5242 /* augmentation_string_size - The size in bytes of the augmentation
5243 string. This value is rounded up to a multiple of 4. */
5244 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5245 addr += 4;
5246 map.augmentation_is_gdb = ((augmentation_string_size
5247 == sizeof (dwarf5_augmentation))
5248 && memcmp (addr, dwarf5_augmentation,
5249 sizeof (dwarf5_augmentation)) == 0);
5250 augmentation_string_size += (-augmentation_string_size) & 3;
5251 addr += augmentation_string_size;
5252
5253 /* List of CUs */
5254 map.cu_table_reordered = addr;
5255 addr += map.cu_count * map.offset_size;
5256
5257 /* List of Local TUs */
5258 map.tu_table_reordered = addr;
5259 addr += map.tu_count * map.offset_size;
5260
5261 /* Hash Lookup Table */
5262 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5263 addr += map.bucket_count * 4;
5264 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5265 addr += map.name_count * 4;
5266
5267 /* Name Table */
5268 map.name_table_string_offs_reordered = addr;
5269 addr += map.name_count * map.offset_size;
5270 map.name_table_entry_offs_reordered = addr;
5271 addr += map.name_count * map.offset_size;
5272
5273 const gdb_byte *abbrev_table_start = addr;
5274 for (;;)
5275 {
5276 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5277 addr += bytes_read;
5278 if (index_num == 0)
5279 break;
5280
5281 const auto insertpair
5282 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5283 if (!insertpair.second)
5284 {
5285 warning (_("Section .debug_names in %s has duplicate index %s, "
5286 "ignoring .debug_names."),
5287 filename, pulongest (index_num));
5288 return false;
5289 }
5290 mapped_debug_names::index_val &indexval = insertpair.first->second;
5291 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5292 addr += bytes_read;
5293
5294 for (;;)
5295 {
5296 mapped_debug_names::index_val::attr attr;
5297 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5298 addr += bytes_read;
5299 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5300 addr += bytes_read;
5301 if (attr.form == DW_FORM_implicit_const)
5302 {
5303 attr.implicit_const = read_signed_leb128 (abfd, addr,
5304 &bytes_read);
5305 addr += bytes_read;
5306 }
5307 if (attr.dw_idx == 0 && attr.form == 0)
5308 break;
5309 indexval.attr_vec.push_back (std::move (attr));
5310 }
5311 }
5312 if (addr != abbrev_table_start + abbrev_table_size)
5313 {
5314 warning (_("Section .debug_names in %s has abbreviation_table "
5315 "of size %s vs. written as %u, ignoring .debug_names."),
5316 filename, plongest (addr - abbrev_table_start),
5317 abbrev_table_size);
5318 return false;
5319 }
5320 map.entry_pool = addr;
5321
5322 return true;
5323 }
5324
5325 /* A helper for create_cus_from_debug_names that handles the MAP's CU
5326 list. */
5327
5328 static void
5329 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
5330 const mapped_debug_names &map,
5331 dwarf2_section_info &section,
5332 bool is_dwz)
5333 {
5334 if (!map.augmentation_is_gdb)
5335 {
5336 for (uint32_t i = 0; i < map.cu_count; ++i)
5337 {
5338 sect_offset sect_off
5339 = (sect_offset) (extract_unsigned_integer
5340 (map.cu_table_reordered + i * map.offset_size,
5341 map.offset_size,
5342 map.dwarf5_byte_order));
5343 /* We don't know the length of the CU, because the CU list in a
5344 .debug_names index can be incomplete, so we can't use the start
5345 of the next CU as end of this CU. We create the CUs here with
5346 length 0, and in cutu_reader::cutu_reader we'll fill in the
5347 actual length. */
5348 dwarf2_per_cu_data *per_cu
5349 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5350 sect_off, 0);
5351 per_bfd->all_comp_units.push_back (per_cu);
5352 }
5353 return;
5354 }
5355
5356 sect_offset sect_off_prev;
5357 for (uint32_t i = 0; i <= map.cu_count; ++i)
5358 {
5359 sect_offset sect_off_next;
5360 if (i < map.cu_count)
5361 {
5362 sect_off_next
5363 = (sect_offset) (extract_unsigned_integer
5364 (map.cu_table_reordered + i * map.offset_size,
5365 map.offset_size,
5366 map.dwarf5_byte_order));
5367 }
5368 else
5369 sect_off_next = (sect_offset) section.size;
5370 if (i >= 1)
5371 {
5372 const ULONGEST length = sect_off_next - sect_off_prev;
5373 dwarf2_per_cu_data *per_cu
5374 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5375 sect_off_prev, length);
5376 per_bfd->all_comp_units.push_back (per_cu);
5377 }
5378 sect_off_prev = sect_off_next;
5379 }
5380 }
5381
5382 /* Read the CU list from the mapped index, and use it to create all
5383 the CU objects for this dwarf2_per_objfile. */
5384
5385 static void
5386 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5387 const mapped_debug_names &map,
5388 const mapped_debug_names &dwz_map)
5389 {
5390 gdb_assert (per_bfd->all_comp_units.empty ());
5391 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5392
5393 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5394 false /* is_dwz */);
5395
5396 if (dwz_map.cu_count == 0)
5397 return;
5398
5399 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5400 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5401 true /* is_dwz */);
5402 }
5403
5404 /* Read .debug_names. If everything went ok, initialize the "quick"
5405 elements of all the CUs and return true. Otherwise, return false. */
5406
5407 static bool
5408 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5409 {
5410 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5411 mapped_debug_names dwz_map;
5412 struct objfile *objfile = per_objfile->objfile;
5413 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5414
5415 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5416 &per_objfile->per_bfd->debug_names, *map))
5417 return false;
5418
5419 /* Don't use the index if it's empty. */
5420 if (map->name_count == 0)
5421 return false;
5422
5423 /* If there is a .dwz file, read it so we can get its CU list as
5424 well. */
5425 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5426 if (dwz != NULL)
5427 {
5428 if (!read_debug_names_from_section (objfile,
5429 bfd_get_filename (dwz->dwz_bfd.get ()),
5430 &dwz->debug_names, dwz_map))
5431 {
5432 warning (_("could not read '.debug_names' section from %s; skipping"),
5433 bfd_get_filename (dwz->dwz_bfd.get ()));
5434 return false;
5435 }
5436 }
5437
5438 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5439
5440 if (map->tu_count != 0)
5441 {
5442 /* We can only handle a single .debug_types when we have an
5443 index. */
5444 if (per_bfd->types.size () != 1)
5445 return false;
5446
5447 dwarf2_section_info *section = &per_bfd->types[0];
5448
5449 create_signatured_type_table_from_debug_names
5450 (per_objfile, *map, section, &per_bfd->abbrev);
5451 }
5452
5453 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5454
5455 per_bfd->debug_names_table = std::move (map);
5456 per_bfd->using_index = 1;
5457 per_bfd->quick_file_names_table =
5458 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5459
5460 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5461 objfiles using the same BFD. */
5462 gdb_assert (per_bfd->partial_symtabs == nullptr);
5463 per_bfd->partial_symtabs = objfile->partial_symtabs;
5464
5465 return true;
5466 }
5467
5468 /* Type used to manage iterating over all CUs looking for a symbol for
5469 .debug_names. */
5470
5471 class dw2_debug_names_iterator
5472 {
5473 public:
5474 dw2_debug_names_iterator (const mapped_debug_names &map,
5475 gdb::optional<block_enum> block_index,
5476 domain_enum domain,
5477 const char *name, dwarf2_per_objfile *per_objfile)
5478 : m_map (map), m_block_index (block_index), m_domain (domain),
5479 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5480 m_per_objfile (per_objfile)
5481 {}
5482
5483 dw2_debug_names_iterator (const mapped_debug_names &map,
5484 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5485 : m_map (map),
5486 m_search (search),
5487 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5488 m_per_objfile (per_objfile)
5489 {}
5490
5491 dw2_debug_names_iterator (const mapped_debug_names &map,
5492 block_enum block_index, domain_enum domain,
5493 uint32_t namei, dwarf2_per_objfile *per_objfile)
5494 : m_map (map), m_block_index (block_index), m_domain (domain),
5495 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5496 m_per_objfile (per_objfile)
5497 {}
5498
5499 /* Return the next matching CU or NULL if there are no more. */
5500 dwarf2_per_cu_data *next ();
5501
5502 private:
5503 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5504 const char *name,
5505 dwarf2_per_objfile *per_objfile);
5506 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5507 uint32_t namei,
5508 dwarf2_per_objfile *per_objfile);
5509
5510 /* The internalized form of .debug_names. */
5511 const mapped_debug_names &m_map;
5512
5513 /* If set, only look for symbols that match that block. Valid values are
5514 GLOBAL_BLOCK and STATIC_BLOCK. */
5515 const gdb::optional<block_enum> m_block_index;
5516
5517 /* The kind of symbol we're looking for. */
5518 const domain_enum m_domain = UNDEF_DOMAIN;
5519 const search_domain m_search = ALL_DOMAIN;
5520
5521 /* The list of CUs from the index entry of the symbol, or NULL if
5522 not found. */
5523 const gdb_byte *m_addr;
5524
5525 dwarf2_per_objfile *m_per_objfile;
5526 };
5527
5528 const char *
5529 mapped_debug_names::namei_to_name
5530 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5531 {
5532 const ULONGEST namei_string_offs
5533 = extract_unsigned_integer ((name_table_string_offs_reordered
5534 + namei * offset_size),
5535 offset_size,
5536 dwarf5_byte_order);
5537 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5538 }
5539
5540 /* Find a slot in .debug_names for the object named NAME. If NAME is
5541 found, return pointer to its pool data. If NAME cannot be found,
5542 return NULL. */
5543
5544 const gdb_byte *
5545 dw2_debug_names_iterator::find_vec_in_debug_names
5546 (const mapped_debug_names &map, const char *name,
5547 dwarf2_per_objfile *per_objfile)
5548 {
5549 int (*cmp) (const char *, const char *);
5550
5551 gdb::unique_xmalloc_ptr<char> without_params;
5552 if (current_language->la_language == language_cplus
5553 || current_language->la_language == language_fortran
5554 || current_language->la_language == language_d)
5555 {
5556 /* NAME is already canonical. Drop any qualifiers as
5557 .debug_names does not contain any. */
5558
5559 if (strchr (name, '(') != NULL)
5560 {
5561 without_params = cp_remove_params (name);
5562 if (without_params != NULL)
5563 name = without_params.get ();
5564 }
5565 }
5566
5567 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5568
5569 const uint32_t full_hash = dwarf5_djb_hash (name);
5570 uint32_t namei
5571 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5572 (map.bucket_table_reordered
5573 + (full_hash % map.bucket_count)), 4,
5574 map.dwarf5_byte_order);
5575 if (namei == 0)
5576 return NULL;
5577 --namei;
5578 if (namei >= map.name_count)
5579 {
5580 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5581 "[in module %s]"),
5582 namei, map.name_count,
5583 objfile_name (per_objfile->objfile));
5584 return NULL;
5585 }
5586
5587 for (;;)
5588 {
5589 const uint32_t namei_full_hash
5590 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5591 (map.hash_table_reordered + namei), 4,
5592 map.dwarf5_byte_order);
5593 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5594 return NULL;
5595
5596 if (full_hash == namei_full_hash)
5597 {
5598 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5599
5600 #if 0 /* An expensive sanity check. */
5601 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5602 {
5603 complaint (_("Wrong .debug_names hash for string at index %u "
5604 "[in module %s]"),
5605 namei, objfile_name (dwarf2_per_objfile->objfile));
5606 return NULL;
5607 }
5608 #endif
5609
5610 if (cmp (namei_string, name) == 0)
5611 {
5612 const ULONGEST namei_entry_offs
5613 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5614 + namei * map.offset_size),
5615 map.offset_size, map.dwarf5_byte_order);
5616 return map.entry_pool + namei_entry_offs;
5617 }
5618 }
5619
5620 ++namei;
5621 if (namei >= map.name_count)
5622 return NULL;
5623 }
5624 }
5625
5626 const gdb_byte *
5627 dw2_debug_names_iterator::find_vec_in_debug_names
5628 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5629 {
5630 if (namei >= map.name_count)
5631 {
5632 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5633 "[in module %s]"),
5634 namei, map.name_count,
5635 objfile_name (per_objfile->objfile));
5636 return NULL;
5637 }
5638
5639 const ULONGEST namei_entry_offs
5640 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5641 + namei * map.offset_size),
5642 map.offset_size, map.dwarf5_byte_order);
5643 return map.entry_pool + namei_entry_offs;
5644 }
5645
5646 /* See dw2_debug_names_iterator. */
5647
5648 dwarf2_per_cu_data *
5649 dw2_debug_names_iterator::next ()
5650 {
5651 if (m_addr == NULL)
5652 return NULL;
5653
5654 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5655 struct objfile *objfile = m_per_objfile->objfile;
5656 bfd *const abfd = objfile->obfd;
5657
5658 again:
5659
5660 unsigned int bytes_read;
5661 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5662 m_addr += bytes_read;
5663 if (abbrev == 0)
5664 return NULL;
5665
5666 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5667 if (indexval_it == m_map.abbrev_map.cend ())
5668 {
5669 complaint (_("Wrong .debug_names undefined abbrev code %s "
5670 "[in module %s]"),
5671 pulongest (abbrev), objfile_name (objfile));
5672 return NULL;
5673 }
5674 const mapped_debug_names::index_val &indexval = indexval_it->second;
5675 enum class symbol_linkage {
5676 unknown,
5677 static_,
5678 extern_,
5679 } symbol_linkage_ = symbol_linkage::unknown;
5680 dwarf2_per_cu_data *per_cu = NULL;
5681 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5682 {
5683 ULONGEST ull;
5684 switch (attr.form)
5685 {
5686 case DW_FORM_implicit_const:
5687 ull = attr.implicit_const;
5688 break;
5689 case DW_FORM_flag_present:
5690 ull = 1;
5691 break;
5692 case DW_FORM_udata:
5693 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5694 m_addr += bytes_read;
5695 break;
5696 case DW_FORM_ref4:
5697 ull = read_4_bytes (abfd, m_addr);
5698 m_addr += 4;
5699 break;
5700 case DW_FORM_ref8:
5701 ull = read_8_bytes (abfd, m_addr);
5702 m_addr += 8;
5703 break;
5704 case DW_FORM_ref_sig8:
5705 ull = read_8_bytes (abfd, m_addr);
5706 m_addr += 8;
5707 break;
5708 default:
5709 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5710 dwarf_form_name (attr.form),
5711 objfile_name (objfile));
5712 return NULL;
5713 }
5714 switch (attr.dw_idx)
5715 {
5716 case DW_IDX_compile_unit:
5717 /* Don't crash on bad data. */
5718 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5719 {
5720 complaint (_(".debug_names entry has bad CU index %s"
5721 " [in module %s]"),
5722 pulongest (ull),
5723 objfile_name (objfile));
5724 continue;
5725 }
5726 per_cu = per_bfd->get_cutu (ull);
5727 break;
5728 case DW_IDX_type_unit:
5729 /* Don't crash on bad data. */
5730 if (ull >= per_bfd->all_type_units.size ())
5731 {
5732 complaint (_(".debug_names entry has bad TU index %s"
5733 " [in module %s]"),
5734 pulongest (ull),
5735 objfile_name (objfile));
5736 continue;
5737 }
5738 per_cu = &per_bfd->get_tu (ull)->per_cu;
5739 break;
5740 case DW_IDX_die_offset:
5741 /* In a per-CU index (as opposed to a per-module index), index
5742 entries without CU attribute implicitly refer to the single CU. */
5743 if (per_cu == NULL)
5744 per_cu = per_bfd->get_cu (0);
5745 break;
5746 case DW_IDX_GNU_internal:
5747 if (!m_map.augmentation_is_gdb)
5748 break;
5749 symbol_linkage_ = symbol_linkage::static_;
5750 break;
5751 case DW_IDX_GNU_external:
5752 if (!m_map.augmentation_is_gdb)
5753 break;
5754 symbol_linkage_ = symbol_linkage::extern_;
5755 break;
5756 }
5757 }
5758
5759 /* Skip if already read in. */
5760 if (m_per_objfile->symtab_set_p (per_cu))
5761 goto again;
5762
5763 /* Check static vs global. */
5764 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5765 {
5766 const bool want_static = *m_block_index == STATIC_BLOCK;
5767 const bool symbol_is_static =
5768 symbol_linkage_ == symbol_linkage::static_;
5769 if (want_static != symbol_is_static)
5770 goto again;
5771 }
5772
5773 /* Match dw2_symtab_iter_next, symbol_kind
5774 and debug_names::psymbol_tag. */
5775 switch (m_domain)
5776 {
5777 case VAR_DOMAIN:
5778 switch (indexval.dwarf_tag)
5779 {
5780 case DW_TAG_variable:
5781 case DW_TAG_subprogram:
5782 /* Some types are also in VAR_DOMAIN. */
5783 case DW_TAG_typedef:
5784 case DW_TAG_structure_type:
5785 break;
5786 default:
5787 goto again;
5788 }
5789 break;
5790 case STRUCT_DOMAIN:
5791 switch (indexval.dwarf_tag)
5792 {
5793 case DW_TAG_typedef:
5794 case DW_TAG_structure_type:
5795 break;
5796 default:
5797 goto again;
5798 }
5799 break;
5800 case LABEL_DOMAIN:
5801 switch (indexval.dwarf_tag)
5802 {
5803 case 0:
5804 case DW_TAG_variable:
5805 break;
5806 default:
5807 goto again;
5808 }
5809 break;
5810 case MODULE_DOMAIN:
5811 switch (indexval.dwarf_tag)
5812 {
5813 case DW_TAG_module:
5814 break;
5815 default:
5816 goto again;
5817 }
5818 break;
5819 default:
5820 break;
5821 }
5822
5823 /* Match dw2_expand_symtabs_matching, symbol_kind and
5824 debug_names::psymbol_tag. */
5825 switch (m_search)
5826 {
5827 case VARIABLES_DOMAIN:
5828 switch (indexval.dwarf_tag)
5829 {
5830 case DW_TAG_variable:
5831 break;
5832 default:
5833 goto again;
5834 }
5835 break;
5836 case FUNCTIONS_DOMAIN:
5837 switch (indexval.dwarf_tag)
5838 {
5839 case DW_TAG_subprogram:
5840 break;
5841 default:
5842 goto again;
5843 }
5844 break;
5845 case TYPES_DOMAIN:
5846 switch (indexval.dwarf_tag)
5847 {
5848 case DW_TAG_typedef:
5849 case DW_TAG_structure_type:
5850 break;
5851 default:
5852 goto again;
5853 }
5854 break;
5855 case MODULES_DOMAIN:
5856 switch (indexval.dwarf_tag)
5857 {
5858 case DW_TAG_module:
5859 break;
5860 default:
5861 goto again;
5862 }
5863 default:
5864 break;
5865 }
5866
5867 return per_cu;
5868 }
5869
5870 static struct compunit_symtab *
5871 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5872 const char *name, domain_enum domain)
5873 {
5874 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5875
5876 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5877 if (!mapp)
5878 {
5879 /* index is NULL if OBJF_READNOW. */
5880 return NULL;
5881 }
5882 const auto &map = *mapp;
5883
5884 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5885
5886 struct compunit_symtab *stab_best = NULL;
5887 struct dwarf2_per_cu_data *per_cu;
5888 while ((per_cu = iter.next ()) != NULL)
5889 {
5890 struct symbol *sym, *with_opaque = NULL;
5891 compunit_symtab *stab
5892 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5893 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5894 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5895
5896 sym = block_find_symbol (block, name, domain,
5897 block_find_non_opaque_type_preferred,
5898 &with_opaque);
5899
5900 /* Some caution must be observed with overloaded functions and
5901 methods, since the index will not contain any overload
5902 information (but NAME might contain it). */
5903
5904 if (sym != NULL
5905 && strcmp_iw (sym->search_name (), name) == 0)
5906 return stab;
5907 if (with_opaque != NULL
5908 && strcmp_iw (with_opaque->search_name (), name) == 0)
5909 stab_best = stab;
5910
5911 /* Keep looking through other CUs. */
5912 }
5913
5914 return stab_best;
5915 }
5916
5917 /* This dumps minimal information about .debug_names. It is called
5918 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5919 uses this to verify that .debug_names has been loaded. */
5920
5921 static void
5922 dw2_debug_names_dump (struct objfile *objfile)
5923 {
5924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5925
5926 gdb_assert (per_objfile->per_bfd->using_index);
5927 printf_filtered (".debug_names:");
5928 if (per_objfile->per_bfd->debug_names_table)
5929 printf_filtered (" exists\n");
5930 else
5931 printf_filtered (" faked for \"readnow\"\n");
5932 printf_filtered ("\n");
5933 }
5934
5935 static void
5936 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5937 const char *func_name)
5938 {
5939 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5940
5941 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5942 if (per_objfile->per_bfd->debug_names_table)
5943 {
5944 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5945
5946 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5947 per_objfile);
5948
5949 struct dwarf2_per_cu_data *per_cu;
5950 while ((per_cu = iter.next ()) != NULL)
5951 dw2_instantiate_symtab (per_cu, per_objfile, false);
5952 }
5953 }
5954
5955 static void
5956 dw2_debug_names_map_matching_symbols
5957 (struct objfile *objfile,
5958 const lookup_name_info &name, domain_enum domain,
5959 int global,
5960 gdb::function_view<symbol_found_callback_ftype> callback,
5961 symbol_compare_ftype *ordered_compare)
5962 {
5963 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5964
5965 /* debug_names_table is NULL if OBJF_READNOW. */
5966 if (!per_objfile->per_bfd->debug_names_table)
5967 return;
5968
5969 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5970 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5971
5972 const char *match_name = name.ada ().lookup_name ().c_str ();
5973 auto matcher = [&] (const char *symname)
5974 {
5975 if (ordered_compare == nullptr)
5976 return true;
5977 return ordered_compare (symname, match_name) == 0;
5978 };
5979
5980 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5981 [&] (offset_type namei)
5982 {
5983 /* The name was matched, now expand corresponding CUs that were
5984 marked. */
5985 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5986 per_objfile);
5987
5988 struct dwarf2_per_cu_data *per_cu;
5989 while ((per_cu = iter.next ()) != NULL)
5990 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5991 nullptr);
5992 return true;
5993 }, per_objfile);
5994
5995 /* It's a shame we couldn't do this inside the
5996 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5997 that have already been expanded. Instead, this loop matches what
5998 the psymtab code does. */
5999 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
6000 {
6001 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
6002 if (symtab != nullptr)
6003 {
6004 const struct block *block
6005 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
6006 if (!iterate_over_symbols_terminated (block, name,
6007 domain, callback))
6008 break;
6009 }
6010 }
6011 }
6012
6013 static void
6014 dw2_debug_names_expand_symtabs_matching
6015 (struct objfile *objfile,
6016 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6017 const lookup_name_info *lookup_name,
6018 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6019 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6020 enum search_domain kind)
6021 {
6022 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6023
6024 /* debug_names_table is NULL if OBJF_READNOW. */
6025 if (!per_objfile->per_bfd->debug_names_table)
6026 return;
6027
6028 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
6029
6030 if (symbol_matcher == NULL && lookup_name == NULL)
6031 {
6032 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
6033 {
6034 QUIT;
6035
6036 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6037 expansion_notify);
6038 }
6039 return;
6040 }
6041
6042 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
6043
6044 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
6045 symbol_matcher,
6046 kind, [&] (offset_type namei)
6047 {
6048 /* The name was matched, now expand corresponding CUs that were
6049 marked. */
6050 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
6051
6052 struct dwarf2_per_cu_data *per_cu;
6053 while ((per_cu = iter.next ()) != NULL)
6054 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6055 expansion_notify);
6056 return true;
6057 }, per_objfile);
6058 }
6059
6060 const struct quick_symbol_functions dwarf2_debug_names_functions =
6061 {
6062 dw2_has_symbols,
6063 dw2_find_last_source_symtab,
6064 dw2_forget_cached_source_info,
6065 dw2_map_symtabs_matching_filename,
6066 dw2_debug_names_lookup_symbol,
6067 NULL,
6068 dw2_print_stats,
6069 dw2_debug_names_dump,
6070 dw2_debug_names_expand_symtabs_for_function,
6071 dw2_expand_all_symtabs,
6072 dw2_expand_symtabs_with_fullname,
6073 dw2_debug_names_map_matching_symbols,
6074 dw2_debug_names_expand_symtabs_matching,
6075 dw2_find_pc_sect_compunit_symtab,
6076 NULL,
6077 dw2_map_symbol_filenames
6078 };
6079
6080 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6081 to either a dwarf2_per_bfd or dwz_file object. */
6082
6083 template <typename T>
6084 static gdb::array_view<const gdb_byte>
6085 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6086 {
6087 dwarf2_section_info *section = &section_owner->gdb_index;
6088
6089 if (section->empty ())
6090 return {};
6091
6092 /* Older elfutils strip versions could keep the section in the main
6093 executable while splitting it for the separate debug info file. */
6094 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6095 return {};
6096
6097 section->read (obj);
6098
6099 /* dwarf2_section_info::size is a bfd_size_type, while
6100 gdb::array_view works with size_t. On 32-bit hosts, with
6101 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6102 is 32-bit. So we need an explicit narrowing conversion here.
6103 This is fine, because it's impossible to allocate or mmap an
6104 array/buffer larger than what size_t can represent. */
6105 return gdb::make_array_view (section->buffer, section->size);
6106 }
6107
6108 /* Lookup the index cache for the contents of the index associated to
6109 DWARF2_OBJ. */
6110
6111 static gdb::array_view<const gdb_byte>
6112 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6113 {
6114 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6115 if (build_id == nullptr)
6116 return {};
6117
6118 return global_index_cache.lookup_gdb_index (build_id,
6119 &dwarf2_per_bfd->index_cache_res);
6120 }
6121
6122 /* Same as the above, but for DWZ. */
6123
6124 static gdb::array_view<const gdb_byte>
6125 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6126 {
6127 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6128 if (build_id == nullptr)
6129 return {};
6130
6131 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6132 }
6133
6134 /* See symfile.h. */
6135
6136 bool
6137 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6138 {
6139 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6140 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6141
6142 /* If we're about to read full symbols, don't bother with the
6143 indices. In this case we also don't care if some other debug
6144 format is making psymtabs, because they are all about to be
6145 expanded anyway. */
6146 if ((objfile->flags & OBJF_READNOW))
6147 {
6148 /* When using READNOW, the using_index flag (set below) indicates that
6149 PER_BFD was already initialized, when we loaded some other objfile. */
6150 if (per_bfd->using_index)
6151 {
6152 *index_kind = dw_index_kind::GDB_INDEX;
6153 per_objfile->resize_symtabs ();
6154 return true;
6155 }
6156
6157 per_bfd->using_index = 1;
6158 create_all_comp_units (per_objfile);
6159 create_all_type_units (per_objfile);
6160 per_bfd->quick_file_names_table
6161 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6162 per_objfile->resize_symtabs ();
6163
6164 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6165 + per_bfd->all_type_units.size ()); ++i)
6166 {
6167 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6168
6169 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6170 struct dwarf2_per_cu_quick_data);
6171 }
6172
6173 /* Return 1 so that gdb sees the "quick" functions. However,
6174 these functions will be no-ops because we will have expanded
6175 all symtabs. */
6176 *index_kind = dw_index_kind::GDB_INDEX;
6177 return true;
6178 }
6179
6180 /* Was a debug names index already read when we processed an objfile sharing
6181 PER_BFD? */
6182 if (per_bfd->debug_names_table != nullptr)
6183 {
6184 *index_kind = dw_index_kind::DEBUG_NAMES;
6185 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6186 per_objfile->resize_symtabs ();
6187 return true;
6188 }
6189
6190 /* Was a GDB index already read when we processed an objfile sharing
6191 PER_BFD? */
6192 if (per_bfd->index_table != nullptr)
6193 {
6194 *index_kind = dw_index_kind::GDB_INDEX;
6195 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6196 per_objfile->resize_symtabs ();
6197 return true;
6198 }
6199
6200 /* There might already be partial symtabs built for this BFD. This happens
6201 when loading the same binary twice with the index-cache enabled. If so,
6202 don't try to read an index. The objfile / per_objfile initialization will
6203 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6204 code path. */
6205 if (per_bfd->partial_symtabs != nullptr)
6206 return false;
6207
6208 if (dwarf2_read_debug_names (per_objfile))
6209 {
6210 *index_kind = dw_index_kind::DEBUG_NAMES;
6211 per_objfile->resize_symtabs ();
6212 return true;
6213 }
6214
6215 if (dwarf2_read_gdb_index (per_objfile,
6216 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6217 get_gdb_index_contents_from_section<dwz_file>))
6218 {
6219 *index_kind = dw_index_kind::GDB_INDEX;
6220 per_objfile->resize_symtabs ();
6221 return true;
6222 }
6223
6224 /* ... otherwise, try to find the index in the index cache. */
6225 if (dwarf2_read_gdb_index (per_objfile,
6226 get_gdb_index_contents_from_cache,
6227 get_gdb_index_contents_from_cache_dwz))
6228 {
6229 global_index_cache.hit ();
6230 *index_kind = dw_index_kind::GDB_INDEX;
6231 per_objfile->resize_symtabs ();
6232 return true;
6233 }
6234
6235 global_index_cache.miss ();
6236 return false;
6237 }
6238
6239 \f
6240
6241 /* Build a partial symbol table. */
6242
6243 void
6244 dwarf2_build_psymtabs (struct objfile *objfile)
6245 {
6246 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6247 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6248
6249 if (per_bfd->partial_symtabs != nullptr)
6250 {
6251 /* Partial symbols were already read, so now we can simply
6252 attach them. */
6253 objfile->partial_symtabs = per_bfd->partial_symtabs;
6254 per_objfile->resize_symtabs ();
6255 return;
6256 }
6257
6258 try
6259 {
6260 /* This isn't really ideal: all the data we allocate on the
6261 objfile's obstack is still uselessly kept around. However,
6262 freeing it seems unsafe. */
6263 psymtab_discarder psymtabs (objfile);
6264 dwarf2_build_psymtabs_hard (per_objfile);
6265 psymtabs.keep ();
6266
6267 per_objfile->resize_symtabs ();
6268
6269 /* (maybe) store an index in the cache. */
6270 global_index_cache.store (per_objfile);
6271 }
6272 catch (const gdb_exception_error &except)
6273 {
6274 exception_print (gdb_stderr, except);
6275 }
6276
6277 /* Finish by setting the local reference to partial symtabs, so that
6278 we don't try to read them again if reading another objfile with the same
6279 BFD. If we can't in fact share, this won't make a difference anyway as
6280 the dwarf2_per_bfd object won't be shared. */
6281 per_bfd->partial_symtabs = objfile->partial_symtabs;
6282 }
6283
6284 /* Find the base address of the compilation unit for range lists and
6285 location lists. It will normally be specified by DW_AT_low_pc.
6286 In DWARF-3 draft 4, the base address could be overridden by
6287 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6288 compilation units with discontinuous ranges. */
6289
6290 static void
6291 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6292 {
6293 struct attribute *attr;
6294
6295 cu->base_address.reset ();
6296
6297 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6298 if (attr != nullptr)
6299 cu->base_address = attr->as_address ();
6300 else
6301 {
6302 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6303 if (attr != nullptr)
6304 cu->base_address = attr->as_address ();
6305 }
6306 }
6307
6308 /* Helper function that returns the proper abbrev section for
6309 THIS_CU. */
6310
6311 static struct dwarf2_section_info *
6312 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6313 {
6314 struct dwarf2_section_info *abbrev;
6315 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6316
6317 if (this_cu->is_dwz)
6318 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6319 else
6320 abbrev = &per_bfd->abbrev;
6321
6322 return abbrev;
6323 }
6324
6325 /* Fetch the abbreviation table offset from a comp or type unit header. */
6326
6327 static sect_offset
6328 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6329 struct dwarf2_section_info *section,
6330 sect_offset sect_off)
6331 {
6332 bfd *abfd = section->get_bfd_owner ();
6333 const gdb_byte *info_ptr;
6334 unsigned int initial_length_size, offset_size;
6335 uint16_t version;
6336
6337 section->read (per_objfile->objfile);
6338 info_ptr = section->buffer + to_underlying (sect_off);
6339 read_initial_length (abfd, info_ptr, &initial_length_size);
6340 offset_size = initial_length_size == 4 ? 4 : 8;
6341 info_ptr += initial_length_size;
6342
6343 version = read_2_bytes (abfd, info_ptr);
6344 info_ptr += 2;
6345 if (version >= 5)
6346 {
6347 /* Skip unit type and address size. */
6348 info_ptr += 2;
6349 }
6350
6351 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6352 }
6353
6354 /* A partial symtab that is used only for include files. */
6355 struct dwarf2_include_psymtab : public partial_symtab
6356 {
6357 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6358 : partial_symtab (filename, objfile)
6359 {
6360 }
6361
6362 void read_symtab (struct objfile *objfile) override
6363 {
6364 /* It's an include file, no symbols to read for it.
6365 Everything is in the includer symtab. */
6366
6367 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6368 expansion of the includer psymtab. We use the dependencies[0] field to
6369 model the includer. But if we go the regular route of calling
6370 expand_psymtab here, and having expand_psymtab call expand_dependencies
6371 to expand the includer, we'll only use expand_psymtab on the includer
6372 (making it a non-toplevel psymtab), while if we expand the includer via
6373 another path, we'll use read_symtab (making it a toplevel psymtab).
6374 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6375 psymtab, and trigger read_symtab on the includer here directly. */
6376 includer ()->read_symtab (objfile);
6377 }
6378
6379 void expand_psymtab (struct objfile *objfile) override
6380 {
6381 /* This is not called by read_symtab, and should not be called by any
6382 expand_dependencies. */
6383 gdb_assert (false);
6384 }
6385
6386 bool readin_p (struct objfile *objfile) const override
6387 {
6388 return includer ()->readin_p (objfile);
6389 }
6390
6391 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6392 {
6393 return nullptr;
6394 }
6395
6396 private:
6397 partial_symtab *includer () const
6398 {
6399 /* An include psymtab has exactly one dependency: the psymtab that
6400 includes it. */
6401 gdb_assert (this->number_of_dependencies == 1);
6402 return this->dependencies[0];
6403 }
6404 };
6405
6406 /* Allocate a new partial symtab for file named NAME and mark this new
6407 partial symtab as being an include of PST. */
6408
6409 static void
6410 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6411 struct objfile *objfile)
6412 {
6413 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6414
6415 if (!IS_ABSOLUTE_PATH (subpst->filename))
6416 subpst->dirname = pst->dirname;
6417
6418 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6419 subpst->dependencies[0] = pst;
6420 subpst->number_of_dependencies = 1;
6421 }
6422
6423 /* Read the Line Number Program data and extract the list of files
6424 included by the source file represented by PST. Build an include
6425 partial symtab for each of these included files. */
6426
6427 static void
6428 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6429 struct die_info *die,
6430 dwarf2_psymtab *pst)
6431 {
6432 line_header_up lh;
6433 struct attribute *attr;
6434
6435 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6436 if (attr != nullptr && attr->form_is_unsigned ())
6437 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6438 if (lh == NULL)
6439 return; /* No linetable, so no includes. */
6440
6441 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6442 that we pass in the raw text_low here; that is ok because we're
6443 only decoding the line table to make include partial symtabs, and
6444 so the addresses aren't really used. */
6445 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6446 pst->raw_text_low (), 1);
6447 }
6448
6449 static hashval_t
6450 hash_signatured_type (const void *item)
6451 {
6452 const struct signatured_type *sig_type
6453 = (const struct signatured_type *) item;
6454
6455 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6456 return sig_type->signature;
6457 }
6458
6459 static int
6460 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6461 {
6462 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6463 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6464
6465 return lhs->signature == rhs->signature;
6466 }
6467
6468 /* Allocate a hash table for signatured types. */
6469
6470 static htab_up
6471 allocate_signatured_type_table ()
6472 {
6473 return htab_up (htab_create_alloc (41,
6474 hash_signatured_type,
6475 eq_signatured_type,
6476 NULL, xcalloc, xfree));
6477 }
6478
6479 /* A helper function to add a signatured type CU to a table. */
6480
6481 static int
6482 add_signatured_type_cu_to_table (void **slot, void *datum)
6483 {
6484 struct signatured_type *sigt = (struct signatured_type *) *slot;
6485 std::vector<signatured_type *> *all_type_units
6486 = (std::vector<signatured_type *> *) datum;
6487
6488 all_type_units->push_back (sigt);
6489
6490 return 1;
6491 }
6492
6493 /* A helper for create_debug_types_hash_table. Read types from SECTION
6494 and fill them into TYPES_HTAB. It will process only type units,
6495 therefore DW_UT_type. */
6496
6497 static void
6498 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6499 struct dwo_file *dwo_file,
6500 dwarf2_section_info *section, htab_up &types_htab,
6501 rcuh_kind section_kind)
6502 {
6503 struct objfile *objfile = per_objfile->objfile;
6504 struct dwarf2_section_info *abbrev_section;
6505 bfd *abfd;
6506 const gdb_byte *info_ptr, *end_ptr;
6507
6508 abbrev_section = (dwo_file != NULL
6509 ? &dwo_file->sections.abbrev
6510 : &per_objfile->per_bfd->abbrev);
6511
6512 dwarf_read_debug_printf ("Reading %s for %s:",
6513 section->get_name (),
6514 abbrev_section->get_file_name ());
6515
6516 section->read (objfile);
6517 info_ptr = section->buffer;
6518
6519 if (info_ptr == NULL)
6520 return;
6521
6522 /* We can't set abfd until now because the section may be empty or
6523 not present, in which case the bfd is unknown. */
6524 abfd = section->get_bfd_owner ();
6525
6526 /* We don't use cutu_reader here because we don't need to read
6527 any dies: the signature is in the header. */
6528
6529 end_ptr = info_ptr + section->size;
6530 while (info_ptr < end_ptr)
6531 {
6532 struct signatured_type *sig_type;
6533 struct dwo_unit *dwo_tu;
6534 void **slot;
6535 const gdb_byte *ptr = info_ptr;
6536 struct comp_unit_head header;
6537 unsigned int length;
6538
6539 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6540
6541 /* Initialize it due to a false compiler warning. */
6542 header.signature = -1;
6543 header.type_cu_offset_in_tu = (cu_offset) -1;
6544
6545 /* We need to read the type's signature in order to build the hash
6546 table, but we don't need anything else just yet. */
6547
6548 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6549 abbrev_section, ptr, section_kind);
6550
6551 length = header.get_length ();
6552
6553 /* Skip dummy type units. */
6554 if (ptr >= info_ptr + length
6555 || peek_abbrev_code (abfd, ptr) == 0
6556 || (header.unit_type != DW_UT_type
6557 && header.unit_type != DW_UT_split_type))
6558 {
6559 info_ptr += length;
6560 continue;
6561 }
6562
6563 if (types_htab == NULL)
6564 {
6565 if (dwo_file)
6566 types_htab = allocate_dwo_unit_table ();
6567 else
6568 types_htab = allocate_signatured_type_table ();
6569 }
6570
6571 if (dwo_file)
6572 {
6573 sig_type = NULL;
6574 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6575 dwo_tu->dwo_file = dwo_file;
6576 dwo_tu->signature = header.signature;
6577 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6578 dwo_tu->section = section;
6579 dwo_tu->sect_off = sect_off;
6580 dwo_tu->length = length;
6581 }
6582 else
6583 {
6584 /* N.B.: type_offset is not usable if this type uses a DWO file.
6585 The real type_offset is in the DWO file. */
6586 dwo_tu = NULL;
6587 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6588 sig_type->signature = header.signature;
6589 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6590 sig_type->per_cu.is_debug_types = 1;
6591 sig_type->per_cu.section = section;
6592 sig_type->per_cu.sect_off = sect_off;
6593 sig_type->per_cu.length = length;
6594 }
6595
6596 slot = htab_find_slot (types_htab.get (),
6597 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6598 INSERT);
6599 gdb_assert (slot != NULL);
6600 if (*slot != NULL)
6601 {
6602 sect_offset dup_sect_off;
6603
6604 if (dwo_file)
6605 {
6606 const struct dwo_unit *dup_tu
6607 = (const struct dwo_unit *) *slot;
6608
6609 dup_sect_off = dup_tu->sect_off;
6610 }
6611 else
6612 {
6613 const struct signatured_type *dup_tu
6614 = (const struct signatured_type *) *slot;
6615
6616 dup_sect_off = dup_tu->per_cu.sect_off;
6617 }
6618
6619 complaint (_("debug type entry at offset %s is duplicate to"
6620 " the entry at offset %s, signature %s"),
6621 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6622 hex_string (header.signature));
6623 }
6624 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6625
6626 dwarf_read_debug_printf_v (" offset %s, signature %s",
6627 sect_offset_str (sect_off),
6628 hex_string (header.signature));
6629
6630 info_ptr += length;
6631 }
6632 }
6633
6634 /* Create the hash table of all entries in the .debug_types
6635 (or .debug_types.dwo) section(s).
6636 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6637 otherwise it is NULL.
6638
6639 The result is a pointer to the hash table or NULL if there are no types.
6640
6641 Note: This function processes DWO files only, not DWP files. */
6642
6643 static void
6644 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6645 struct dwo_file *dwo_file,
6646 gdb::array_view<dwarf2_section_info> type_sections,
6647 htab_up &types_htab)
6648 {
6649 for (dwarf2_section_info &section : type_sections)
6650 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6651 rcuh_kind::TYPE);
6652 }
6653
6654 /* Create the hash table of all entries in the .debug_types section,
6655 and initialize all_type_units.
6656 The result is zero if there is an error (e.g. missing .debug_types section),
6657 otherwise non-zero. */
6658
6659 static int
6660 create_all_type_units (dwarf2_per_objfile *per_objfile)
6661 {
6662 htab_up types_htab;
6663
6664 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6665 types_htab, rcuh_kind::COMPILE);
6666 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6667 types_htab);
6668 if (types_htab == NULL)
6669 {
6670 per_objfile->per_bfd->signatured_types = NULL;
6671 return 0;
6672 }
6673
6674 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6675
6676 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6677 per_objfile->per_bfd->all_type_units.reserve
6678 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6679
6680 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6681 add_signatured_type_cu_to_table,
6682 &per_objfile->per_bfd->all_type_units);
6683
6684 return 1;
6685 }
6686
6687 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6688 If SLOT is non-NULL, it is the entry to use in the hash table.
6689 Otherwise we find one. */
6690
6691 static struct signatured_type *
6692 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6693 {
6694 if (per_objfile->per_bfd->all_type_units.size ()
6695 == per_objfile->per_bfd->all_type_units.capacity ())
6696 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6697
6698 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6699
6700 per_objfile->resize_symtabs ();
6701
6702 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6703 sig_type->signature = sig;
6704 sig_type->per_cu.is_debug_types = 1;
6705 if (per_objfile->per_bfd->using_index)
6706 {
6707 sig_type->per_cu.v.quick =
6708 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6709 struct dwarf2_per_cu_quick_data);
6710 }
6711
6712 if (slot == NULL)
6713 {
6714 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6715 sig_type, INSERT);
6716 }
6717 gdb_assert (*slot == NULL);
6718 *slot = sig_type;
6719 /* The rest of sig_type must be filled in by the caller. */
6720 return sig_type;
6721 }
6722
6723 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6724 Fill in SIG_ENTRY with DWO_ENTRY. */
6725
6726 static void
6727 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6728 struct signatured_type *sig_entry,
6729 struct dwo_unit *dwo_entry)
6730 {
6731 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6732
6733 /* Make sure we're not clobbering something we don't expect to. */
6734 gdb_assert (! sig_entry->per_cu.queued);
6735 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6736 if (per_bfd->using_index)
6737 {
6738 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6739 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6740 }
6741 else
6742 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6743 gdb_assert (sig_entry->signature == dwo_entry->signature);
6744 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6745 gdb_assert (sig_entry->type_unit_group == NULL);
6746 gdb_assert (sig_entry->dwo_unit == NULL);
6747
6748 sig_entry->per_cu.section = dwo_entry->section;
6749 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6750 sig_entry->per_cu.length = dwo_entry->length;
6751 sig_entry->per_cu.reading_dwo_directly = 1;
6752 sig_entry->per_cu.per_bfd = per_bfd;
6753 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6754 sig_entry->dwo_unit = dwo_entry;
6755 }
6756
6757 /* Subroutine of lookup_signatured_type.
6758 If we haven't read the TU yet, create the signatured_type data structure
6759 for a TU to be read in directly from a DWO file, bypassing the stub.
6760 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6761 using .gdb_index, then when reading a CU we want to stay in the DWO file
6762 containing that CU. Otherwise we could end up reading several other DWO
6763 files (due to comdat folding) to process the transitive closure of all the
6764 mentioned TUs, and that can be slow. The current DWO file will have every
6765 type signature that it needs.
6766 We only do this for .gdb_index because in the psymtab case we already have
6767 to read all the DWOs to build the type unit groups. */
6768
6769 static struct signatured_type *
6770 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6771 {
6772 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6773 struct dwo_file *dwo_file;
6774 struct dwo_unit find_dwo_entry, *dwo_entry;
6775 struct signatured_type find_sig_entry, *sig_entry;
6776 void **slot;
6777
6778 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6779
6780 /* If TU skeletons have been removed then we may not have read in any
6781 TUs yet. */
6782 if (per_objfile->per_bfd->signatured_types == NULL)
6783 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6784
6785 /* We only ever need to read in one copy of a signatured type.
6786 Use the global signatured_types array to do our own comdat-folding
6787 of types. If this is the first time we're reading this TU, and
6788 the TU has an entry in .gdb_index, replace the recorded data from
6789 .gdb_index with this TU. */
6790
6791 find_sig_entry.signature = sig;
6792 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6793 &find_sig_entry, INSERT);
6794 sig_entry = (struct signatured_type *) *slot;
6795
6796 /* We can get here with the TU already read, *or* in the process of being
6797 read. Don't reassign the global entry to point to this DWO if that's
6798 the case. Also note that if the TU is already being read, it may not
6799 have come from a DWO, the program may be a mix of Fission-compiled
6800 code and non-Fission-compiled code. */
6801
6802 /* Have we already tried to read this TU?
6803 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6804 needn't exist in the global table yet). */
6805 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6806 return sig_entry;
6807
6808 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6809 dwo_unit of the TU itself. */
6810 dwo_file = cu->dwo_unit->dwo_file;
6811
6812 /* Ok, this is the first time we're reading this TU. */
6813 if (dwo_file->tus == NULL)
6814 return NULL;
6815 find_dwo_entry.signature = sig;
6816 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6817 &find_dwo_entry);
6818 if (dwo_entry == NULL)
6819 return NULL;
6820
6821 /* If the global table doesn't have an entry for this TU, add one. */
6822 if (sig_entry == NULL)
6823 sig_entry = add_type_unit (per_objfile, sig, slot);
6824
6825 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6826 sig_entry->per_cu.tu_read = 1;
6827 return sig_entry;
6828 }
6829
6830 /* Subroutine of lookup_signatured_type.
6831 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6832 then try the DWP file. If the TU stub (skeleton) has been removed then
6833 it won't be in .gdb_index. */
6834
6835 static struct signatured_type *
6836 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6837 {
6838 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6839 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6840 struct dwo_unit *dwo_entry;
6841 struct signatured_type find_sig_entry, *sig_entry;
6842 void **slot;
6843
6844 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6845 gdb_assert (dwp_file != NULL);
6846
6847 /* If TU skeletons have been removed then we may not have read in any
6848 TUs yet. */
6849 if (per_objfile->per_bfd->signatured_types == NULL)
6850 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6851
6852 find_sig_entry.signature = sig;
6853 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6854 &find_sig_entry, INSERT);
6855 sig_entry = (struct signatured_type *) *slot;
6856
6857 /* Have we already tried to read this TU?
6858 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6859 needn't exist in the global table yet). */
6860 if (sig_entry != NULL)
6861 return sig_entry;
6862
6863 if (dwp_file->tus == NULL)
6864 return NULL;
6865 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6866 1 /* is_debug_types */);
6867 if (dwo_entry == NULL)
6868 return NULL;
6869
6870 sig_entry = add_type_unit (per_objfile, sig, slot);
6871 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6872
6873 return sig_entry;
6874 }
6875
6876 /* Lookup a signature based type for DW_FORM_ref_sig8.
6877 Returns NULL if signature SIG is not present in the table.
6878 It is up to the caller to complain about this. */
6879
6880 static struct signatured_type *
6881 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6882 {
6883 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6884
6885 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6886 {
6887 /* We're in a DWO/DWP file, and we're using .gdb_index.
6888 These cases require special processing. */
6889 if (get_dwp_file (per_objfile) == NULL)
6890 return lookup_dwo_signatured_type (cu, sig);
6891 else
6892 return lookup_dwp_signatured_type (cu, sig);
6893 }
6894 else
6895 {
6896 struct signatured_type find_entry, *entry;
6897
6898 if (per_objfile->per_bfd->signatured_types == NULL)
6899 return NULL;
6900 find_entry.signature = sig;
6901 entry = ((struct signatured_type *)
6902 htab_find (per_objfile->per_bfd->signatured_types.get (),
6903 &find_entry));
6904 return entry;
6905 }
6906 }
6907
6908 /* Low level DIE reading support. */
6909
6910 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6911
6912 static void
6913 init_cu_die_reader (struct die_reader_specs *reader,
6914 struct dwarf2_cu *cu,
6915 struct dwarf2_section_info *section,
6916 struct dwo_file *dwo_file,
6917 struct abbrev_table *abbrev_table)
6918 {
6919 gdb_assert (section->readin && section->buffer != NULL);
6920 reader->abfd = section->get_bfd_owner ();
6921 reader->cu = cu;
6922 reader->dwo_file = dwo_file;
6923 reader->die_section = section;
6924 reader->buffer = section->buffer;
6925 reader->buffer_end = section->buffer + section->size;
6926 reader->abbrev_table = abbrev_table;
6927 }
6928
6929 /* Subroutine of cutu_reader to simplify it.
6930 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6931 There's just a lot of work to do, and cutu_reader is big enough
6932 already.
6933
6934 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6935 from it to the DIE in the DWO. If NULL we are skipping the stub.
6936 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6937 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6938 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6939 STUB_COMP_DIR may be non-NULL.
6940 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6941 are filled in with the info of the DIE from the DWO file.
6942 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6943 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6944 kept around for at least as long as *RESULT_READER.
6945
6946 The result is non-zero if a valid (non-dummy) DIE was found. */
6947
6948 static int
6949 read_cutu_die_from_dwo (dwarf2_cu *cu,
6950 struct dwo_unit *dwo_unit,
6951 struct die_info *stub_comp_unit_die,
6952 const char *stub_comp_dir,
6953 struct die_reader_specs *result_reader,
6954 const gdb_byte **result_info_ptr,
6955 struct die_info **result_comp_unit_die,
6956 abbrev_table_up *result_dwo_abbrev_table)
6957 {
6958 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6959 dwarf2_per_cu_data *per_cu = cu->per_cu;
6960 struct objfile *objfile = per_objfile->objfile;
6961 bfd *abfd;
6962 const gdb_byte *begin_info_ptr, *info_ptr;
6963 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6964 int i,num_extra_attrs;
6965 struct dwarf2_section_info *dwo_abbrev_section;
6966 struct die_info *comp_unit_die;
6967
6968 /* At most one of these may be provided. */
6969 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6970
6971 /* These attributes aren't processed until later:
6972 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6973 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6974 referenced later. However, these attributes are found in the stub
6975 which we won't have later. In order to not impose this complication
6976 on the rest of the code, we read them here and copy them to the
6977 DWO CU/TU die. */
6978
6979 stmt_list = NULL;
6980 low_pc = NULL;
6981 high_pc = NULL;
6982 ranges = NULL;
6983 comp_dir = NULL;
6984
6985 if (stub_comp_unit_die != NULL)
6986 {
6987 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6988 DWO file. */
6989 if (!per_cu->is_debug_types)
6990 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6991 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6992 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6993 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6994 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6995
6996 cu->addr_base = stub_comp_unit_die->addr_base ();
6997
6998 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6999 We need the value before we can process DW_AT_ranges values from the
7000 DWO. */
7001 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
7002
7003 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
7004 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
7005 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
7006 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
7007 section. */
7008 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
7009 }
7010 else if (stub_comp_dir != NULL)
7011 {
7012 /* Reconstruct the comp_dir attribute to simplify the code below. */
7013 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
7014 comp_dir->name = DW_AT_comp_dir;
7015 comp_dir->form = DW_FORM_string;
7016 comp_dir->set_string_noncanonical (stub_comp_dir);
7017 }
7018
7019 /* Set up for reading the DWO CU/TU. */
7020 cu->dwo_unit = dwo_unit;
7021 dwarf2_section_info *section = dwo_unit->section;
7022 section->read (objfile);
7023 abfd = section->get_bfd_owner ();
7024 begin_info_ptr = info_ptr = (section->buffer
7025 + to_underlying (dwo_unit->sect_off));
7026 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7027
7028 if (per_cu->is_debug_types)
7029 {
7030 signatured_type *sig_type = (struct signatured_type *) per_cu;
7031
7032 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7033 section, dwo_abbrev_section,
7034 info_ptr, rcuh_kind::TYPE);
7035 /* This is not an assert because it can be caused by bad debug info. */
7036 if (sig_type->signature != cu->header.signature)
7037 {
7038 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7039 " TU at offset %s [in module %s]"),
7040 hex_string (sig_type->signature),
7041 hex_string (cu->header.signature),
7042 sect_offset_str (dwo_unit->sect_off),
7043 bfd_get_filename (abfd));
7044 }
7045 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7046 /* For DWOs coming from DWP files, we don't know the CU length
7047 nor the type's offset in the TU until now. */
7048 dwo_unit->length = cu->header.get_length ();
7049 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7050
7051 /* Establish the type offset that can be used to lookup the type.
7052 For DWO files, we don't know it until now. */
7053 sig_type->type_offset_in_section
7054 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7055 }
7056 else
7057 {
7058 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7059 section, dwo_abbrev_section,
7060 info_ptr, rcuh_kind::COMPILE);
7061 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7062 /* For DWOs coming from DWP files, we don't know the CU length
7063 until now. */
7064 dwo_unit->length = cu->header.get_length ();
7065 }
7066
7067 dwo_abbrev_section->read (objfile);
7068 *result_dwo_abbrev_table
7069 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
7070 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7071 result_dwo_abbrev_table->get ());
7072
7073 /* Read in the die, but leave space to copy over the attributes
7074 from the stub. This has the benefit of simplifying the rest of
7075 the code - all the work to maintain the illusion of a single
7076 DW_TAG_{compile,type}_unit DIE is done here. */
7077 num_extra_attrs = ((stmt_list != NULL)
7078 + (low_pc != NULL)
7079 + (high_pc != NULL)
7080 + (ranges != NULL)
7081 + (comp_dir != NULL));
7082 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7083 num_extra_attrs);
7084
7085 /* Copy over the attributes from the stub to the DIE we just read in. */
7086 comp_unit_die = *result_comp_unit_die;
7087 i = comp_unit_die->num_attrs;
7088 if (stmt_list != NULL)
7089 comp_unit_die->attrs[i++] = *stmt_list;
7090 if (low_pc != NULL)
7091 comp_unit_die->attrs[i++] = *low_pc;
7092 if (high_pc != NULL)
7093 comp_unit_die->attrs[i++] = *high_pc;
7094 if (ranges != NULL)
7095 comp_unit_die->attrs[i++] = *ranges;
7096 if (comp_dir != NULL)
7097 comp_unit_die->attrs[i++] = *comp_dir;
7098 comp_unit_die->num_attrs += num_extra_attrs;
7099
7100 if (dwarf_die_debug)
7101 {
7102 fprintf_unfiltered (gdb_stdlog,
7103 "Read die from %s@0x%x of %s:\n",
7104 section->get_name (),
7105 (unsigned) (begin_info_ptr - section->buffer),
7106 bfd_get_filename (abfd));
7107 dump_die (comp_unit_die, dwarf_die_debug);
7108 }
7109
7110 /* Skip dummy compilation units. */
7111 if (info_ptr >= begin_info_ptr + dwo_unit->length
7112 || peek_abbrev_code (abfd, info_ptr) == 0)
7113 return 0;
7114
7115 *result_info_ptr = info_ptr;
7116 return 1;
7117 }
7118
7119 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7120 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7121 signature is part of the header. */
7122 static gdb::optional<ULONGEST>
7123 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7124 {
7125 if (cu->header.version >= 5)
7126 return cu->header.signature;
7127 struct attribute *attr;
7128 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7129 if (attr == nullptr || !attr->form_is_unsigned ())
7130 return gdb::optional<ULONGEST> ();
7131 return attr->as_unsigned ();
7132 }
7133
7134 /* Subroutine of cutu_reader to simplify it.
7135 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7136 Returns NULL if the specified DWO unit cannot be found. */
7137
7138 static struct dwo_unit *
7139 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7140 {
7141 dwarf2_per_cu_data *per_cu = cu->per_cu;
7142 struct dwo_unit *dwo_unit;
7143 const char *comp_dir;
7144
7145 gdb_assert (cu != NULL);
7146
7147 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7148 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7149 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7150
7151 if (per_cu->is_debug_types)
7152 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7153 else
7154 {
7155 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7156
7157 if (!signature.has_value ())
7158 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7159 " [in module %s]"),
7160 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7161
7162 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7163 }
7164
7165 return dwo_unit;
7166 }
7167
7168 /* Subroutine of cutu_reader to simplify it.
7169 See it for a description of the parameters.
7170 Read a TU directly from a DWO file, bypassing the stub. */
7171
7172 void
7173 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7174 dwarf2_per_objfile *per_objfile,
7175 dwarf2_cu *existing_cu)
7176 {
7177 struct signatured_type *sig_type;
7178
7179 /* Verify we can do the following downcast, and that we have the
7180 data we need. */
7181 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7182 sig_type = (struct signatured_type *) this_cu;
7183 gdb_assert (sig_type->dwo_unit != NULL);
7184
7185 dwarf2_cu *cu;
7186
7187 if (existing_cu != nullptr)
7188 {
7189 cu = existing_cu;
7190 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7191 /* There's no need to do the rereading_dwo_cu handling that
7192 cutu_reader does since we don't read the stub. */
7193 }
7194 else
7195 {
7196 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7197 in per_objfile yet. */
7198 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7199 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7200 cu = m_new_cu.get ();
7201 }
7202
7203 /* A future optimization, if needed, would be to use an existing
7204 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7205 could share abbrev tables. */
7206
7207 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7208 NULL /* stub_comp_unit_die */,
7209 sig_type->dwo_unit->dwo_file->comp_dir,
7210 this, &info_ptr,
7211 &comp_unit_die,
7212 &m_dwo_abbrev_table) == 0)
7213 {
7214 /* Dummy die. */
7215 dummy_p = true;
7216 }
7217 }
7218
7219 /* Initialize a CU (or TU) and read its DIEs.
7220 If the CU defers to a DWO file, read the DWO file as well.
7221
7222 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7223 Otherwise the table specified in the comp unit header is read in and used.
7224 This is an optimization for when we already have the abbrev table.
7225
7226 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7227 allocated. */
7228
7229 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7230 dwarf2_per_objfile *per_objfile,
7231 struct abbrev_table *abbrev_table,
7232 dwarf2_cu *existing_cu,
7233 bool skip_partial)
7234 : die_reader_specs {},
7235 m_this_cu (this_cu)
7236 {
7237 struct objfile *objfile = per_objfile->objfile;
7238 struct dwarf2_section_info *section = this_cu->section;
7239 bfd *abfd = section->get_bfd_owner ();
7240 const gdb_byte *begin_info_ptr;
7241 struct signatured_type *sig_type = NULL;
7242 struct dwarf2_section_info *abbrev_section;
7243 /* Non-zero if CU currently points to a DWO file and we need to
7244 reread it. When this happens we need to reread the skeleton die
7245 before we can reread the DWO file (this only applies to CUs, not TUs). */
7246 int rereading_dwo_cu = 0;
7247
7248 if (dwarf_die_debug)
7249 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7250 this_cu->is_debug_types ? "type" : "comp",
7251 sect_offset_str (this_cu->sect_off));
7252
7253 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7254 file (instead of going through the stub), short-circuit all of this. */
7255 if (this_cu->reading_dwo_directly)
7256 {
7257 /* Narrow down the scope of possibilities to have to understand. */
7258 gdb_assert (this_cu->is_debug_types);
7259 gdb_assert (abbrev_table == NULL);
7260 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7261 return;
7262 }
7263
7264 /* This is cheap if the section is already read in. */
7265 section->read (objfile);
7266
7267 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7268
7269 abbrev_section = get_abbrev_section_for_cu (this_cu);
7270
7271 dwarf2_cu *cu;
7272
7273 if (existing_cu != nullptr)
7274 {
7275 cu = existing_cu;
7276 /* If this CU is from a DWO file we need to start over, we need to
7277 refetch the attributes from the skeleton CU.
7278 This could be optimized by retrieving those attributes from when we
7279 were here the first time: the previous comp_unit_die was stored in
7280 comp_unit_obstack. But there's no data yet that we need this
7281 optimization. */
7282 if (cu->dwo_unit != NULL)
7283 rereading_dwo_cu = 1;
7284 }
7285 else
7286 {
7287 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7288 in per_objfile yet. */
7289 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7290 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7291 cu = m_new_cu.get ();
7292 }
7293
7294 /* Get the header. */
7295 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7296 {
7297 /* We already have the header, there's no need to read it in again. */
7298 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7299 }
7300 else
7301 {
7302 if (this_cu->is_debug_types)
7303 {
7304 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7305 section, abbrev_section,
7306 info_ptr, rcuh_kind::TYPE);
7307
7308 /* Since per_cu is the first member of struct signatured_type,
7309 we can go from a pointer to one to a pointer to the other. */
7310 sig_type = (struct signatured_type *) this_cu;
7311 gdb_assert (sig_type->signature == cu->header.signature);
7312 gdb_assert (sig_type->type_offset_in_tu
7313 == cu->header.type_cu_offset_in_tu);
7314 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7315
7316 /* LENGTH has not been set yet for type units if we're
7317 using .gdb_index. */
7318 this_cu->length = cu->header.get_length ();
7319
7320 /* Establish the type offset that can be used to lookup the type. */
7321 sig_type->type_offset_in_section =
7322 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7323
7324 this_cu->dwarf_version = cu->header.version;
7325 }
7326 else
7327 {
7328 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7329 section, abbrev_section,
7330 info_ptr,
7331 rcuh_kind::COMPILE);
7332
7333 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7334 if (this_cu->length == 0)
7335 this_cu->length = cu->header.get_length ();
7336 else
7337 gdb_assert (this_cu->length == cu->header.get_length ());
7338 this_cu->dwarf_version = cu->header.version;
7339 }
7340 }
7341
7342 /* Skip dummy compilation units. */
7343 if (info_ptr >= begin_info_ptr + this_cu->length
7344 || peek_abbrev_code (abfd, info_ptr) == 0)
7345 {
7346 dummy_p = true;
7347 return;
7348 }
7349
7350 /* If we don't have them yet, read the abbrevs for this compilation unit.
7351 And if we need to read them now, make sure they're freed when we're
7352 done. */
7353 if (abbrev_table != NULL)
7354 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7355 else
7356 {
7357 abbrev_section->read (objfile);
7358 m_abbrev_table_holder
7359 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7360 abbrev_table = m_abbrev_table_holder.get ();
7361 }
7362
7363 /* Read the top level CU/TU die. */
7364 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7365 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7366
7367 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7368 {
7369 dummy_p = true;
7370 return;
7371 }
7372
7373 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7374 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7375 table from the DWO file and pass the ownership over to us. It will be
7376 referenced from READER, so we must make sure to free it after we're done
7377 with READER.
7378
7379 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7380 DWO CU, that this test will fail (the attribute will not be present). */
7381 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7382 if (dwo_name != nullptr)
7383 {
7384 struct dwo_unit *dwo_unit;
7385 struct die_info *dwo_comp_unit_die;
7386
7387 if (comp_unit_die->has_children)
7388 {
7389 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7390 " has children (offset %s) [in module %s]"),
7391 sect_offset_str (this_cu->sect_off),
7392 bfd_get_filename (abfd));
7393 }
7394 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7395 if (dwo_unit != NULL)
7396 {
7397 if (read_cutu_die_from_dwo (cu, dwo_unit,
7398 comp_unit_die, NULL,
7399 this, &info_ptr,
7400 &dwo_comp_unit_die,
7401 &m_dwo_abbrev_table) == 0)
7402 {
7403 /* Dummy die. */
7404 dummy_p = true;
7405 return;
7406 }
7407 comp_unit_die = dwo_comp_unit_die;
7408 }
7409 else
7410 {
7411 /* Yikes, we couldn't find the rest of the DIE, we only have
7412 the stub. A complaint has already been logged. There's
7413 not much more we can do except pass on the stub DIE to
7414 die_reader_func. We don't want to throw an error on bad
7415 debug info. */
7416 }
7417 }
7418 }
7419
7420 void
7421 cutu_reader::keep ()
7422 {
7423 /* Done, clean up. */
7424 gdb_assert (!dummy_p);
7425 if (m_new_cu != NULL)
7426 {
7427 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7428 now. */
7429 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7430 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7431 }
7432 }
7433
7434 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7435 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7436 assumed to have already done the lookup to find the DWO file).
7437
7438 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7439 THIS_CU->is_debug_types, but nothing else.
7440
7441 We fill in THIS_CU->length.
7442
7443 THIS_CU->cu is always freed when done.
7444 This is done in order to not leave THIS_CU->cu in a state where we have
7445 to care whether it refers to the "main" CU or the DWO CU.
7446
7447 When parent_cu is passed, it is used to provide a default value for
7448 str_offsets_base and addr_base from the parent. */
7449
7450 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7451 dwarf2_per_objfile *per_objfile,
7452 struct dwarf2_cu *parent_cu,
7453 struct dwo_file *dwo_file)
7454 : die_reader_specs {},
7455 m_this_cu (this_cu)
7456 {
7457 struct objfile *objfile = per_objfile->objfile;
7458 struct dwarf2_section_info *section = this_cu->section;
7459 bfd *abfd = section->get_bfd_owner ();
7460 struct dwarf2_section_info *abbrev_section;
7461 const gdb_byte *begin_info_ptr, *info_ptr;
7462
7463 if (dwarf_die_debug)
7464 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7465 this_cu->is_debug_types ? "type" : "comp",
7466 sect_offset_str (this_cu->sect_off));
7467
7468 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7469
7470 abbrev_section = (dwo_file != NULL
7471 ? &dwo_file->sections.abbrev
7472 : get_abbrev_section_for_cu (this_cu));
7473
7474 /* This is cheap if the section is already read in. */
7475 section->read (objfile);
7476
7477 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7478
7479 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7480 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7481 section, abbrev_section, info_ptr,
7482 (this_cu->is_debug_types
7483 ? rcuh_kind::TYPE
7484 : rcuh_kind::COMPILE));
7485
7486 if (parent_cu != nullptr)
7487 {
7488 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7489 m_new_cu->addr_base = parent_cu->addr_base;
7490 }
7491 this_cu->length = m_new_cu->header.get_length ();
7492
7493 /* Skip dummy compilation units. */
7494 if (info_ptr >= begin_info_ptr + this_cu->length
7495 || peek_abbrev_code (abfd, info_ptr) == 0)
7496 {
7497 dummy_p = true;
7498 return;
7499 }
7500
7501 abbrev_section->read (objfile);
7502 m_abbrev_table_holder
7503 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7504
7505 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7506 m_abbrev_table_holder.get ());
7507 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7508 }
7509
7510 \f
7511 /* Type Unit Groups.
7512
7513 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7514 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7515 so that all types coming from the same compilation (.o file) are grouped
7516 together. A future step could be to put the types in the same symtab as
7517 the CU the types ultimately came from. */
7518
7519 static hashval_t
7520 hash_type_unit_group (const void *item)
7521 {
7522 const struct type_unit_group *tu_group
7523 = (const struct type_unit_group *) item;
7524
7525 return hash_stmt_list_entry (&tu_group->hash);
7526 }
7527
7528 static int
7529 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7530 {
7531 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7532 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7533
7534 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7535 }
7536
7537 /* Allocate a hash table for type unit groups. */
7538
7539 static htab_up
7540 allocate_type_unit_groups_table ()
7541 {
7542 return htab_up (htab_create_alloc (3,
7543 hash_type_unit_group,
7544 eq_type_unit_group,
7545 NULL, xcalloc, xfree));
7546 }
7547
7548 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7549 partial symtabs. We combine several TUs per psymtab to not let the size
7550 of any one psymtab grow too big. */
7551 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7552 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7553
7554 /* Helper routine for get_type_unit_group.
7555 Create the type_unit_group object used to hold one or more TUs. */
7556
7557 static struct type_unit_group *
7558 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7559 {
7560 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7561 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7562 struct dwarf2_per_cu_data *per_cu;
7563 struct type_unit_group *tu_group;
7564
7565 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7566 per_cu = &tu_group->per_cu;
7567 per_cu->per_bfd = per_bfd;
7568
7569 if (per_bfd->using_index)
7570 {
7571 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7572 struct dwarf2_per_cu_quick_data);
7573 }
7574 else
7575 {
7576 unsigned int line_offset = to_underlying (line_offset_struct);
7577 dwarf2_psymtab *pst;
7578 std::string name;
7579
7580 /* Give the symtab a useful name for debug purposes. */
7581 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7582 name = string_printf ("<type_units_%d>",
7583 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7584 else
7585 name = string_printf ("<type_units_at_0x%x>", line_offset);
7586
7587 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7588 pst->anonymous = true;
7589 }
7590
7591 tu_group->hash.dwo_unit = cu->dwo_unit;
7592 tu_group->hash.line_sect_off = line_offset_struct;
7593
7594 return tu_group;
7595 }
7596
7597 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7598 STMT_LIST is a DW_AT_stmt_list attribute. */
7599
7600 static struct type_unit_group *
7601 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7602 {
7603 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7604 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7605 struct type_unit_group *tu_group;
7606 void **slot;
7607 unsigned int line_offset;
7608 struct type_unit_group type_unit_group_for_lookup;
7609
7610 if (per_objfile->per_bfd->type_unit_groups == NULL)
7611 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7612
7613 /* Do we need to create a new group, or can we use an existing one? */
7614
7615 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7616 {
7617 line_offset = stmt_list->as_unsigned ();
7618 ++tu_stats->nr_symtab_sharers;
7619 }
7620 else
7621 {
7622 /* Ugh, no stmt_list. Rare, but we have to handle it.
7623 We can do various things here like create one group per TU or
7624 spread them over multiple groups to split up the expansion work.
7625 To avoid worst case scenarios (too many groups or too large groups)
7626 we, umm, group them in bunches. */
7627 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7628 | (tu_stats->nr_stmt_less_type_units
7629 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7630 ++tu_stats->nr_stmt_less_type_units;
7631 }
7632
7633 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7634 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7635 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7636 &type_unit_group_for_lookup, INSERT);
7637 if (*slot != NULL)
7638 {
7639 tu_group = (struct type_unit_group *) *slot;
7640 gdb_assert (tu_group != NULL);
7641 }
7642 else
7643 {
7644 sect_offset line_offset_struct = (sect_offset) line_offset;
7645 tu_group = create_type_unit_group (cu, line_offset_struct);
7646 *slot = tu_group;
7647 ++tu_stats->nr_symtabs;
7648 }
7649
7650 return tu_group;
7651 }
7652 \f
7653 /* Partial symbol tables. */
7654
7655 /* Create a psymtab named NAME and assign it to PER_CU.
7656
7657 The caller must fill in the following details:
7658 dirname, textlow, texthigh. */
7659
7660 static dwarf2_psymtab *
7661 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7662 dwarf2_per_objfile *per_objfile,
7663 const char *name)
7664 {
7665 struct objfile *objfile = per_objfile->objfile;
7666 dwarf2_psymtab *pst;
7667
7668 pst = new dwarf2_psymtab (name, objfile, per_cu);
7669
7670 pst->psymtabs_addrmap_supported = true;
7671
7672 /* This is the glue that links PST into GDB's symbol API. */
7673 per_cu->v.psymtab = pst;
7674
7675 return pst;
7676 }
7677
7678 /* DIE reader function for process_psymtab_comp_unit. */
7679
7680 static void
7681 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7682 const gdb_byte *info_ptr,
7683 struct die_info *comp_unit_die,
7684 enum language pretend_language)
7685 {
7686 struct dwarf2_cu *cu = reader->cu;
7687 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7688 struct objfile *objfile = per_objfile->objfile;
7689 struct gdbarch *gdbarch = objfile->arch ();
7690 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7691 CORE_ADDR baseaddr;
7692 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7693 dwarf2_psymtab *pst;
7694 enum pc_bounds_kind cu_bounds_kind;
7695 const char *filename;
7696
7697 gdb_assert (! per_cu->is_debug_types);
7698
7699 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7700
7701 /* Allocate a new partial symbol table structure. */
7702 gdb::unique_xmalloc_ptr<char> debug_filename;
7703 static const char artificial[] = "<artificial>";
7704 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7705 if (filename == NULL)
7706 filename = "";
7707 else if (strcmp (filename, artificial) == 0)
7708 {
7709 debug_filename.reset (concat (artificial, "@",
7710 sect_offset_str (per_cu->sect_off),
7711 (char *) NULL));
7712 filename = debug_filename.get ();
7713 }
7714
7715 pst = create_partial_symtab (per_cu, per_objfile, filename);
7716
7717 /* This must be done before calling dwarf2_build_include_psymtabs. */
7718 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7719
7720 baseaddr = objfile->text_section_offset ();
7721
7722 dwarf2_find_base_address (comp_unit_die, cu);
7723
7724 /* Possibly set the default values of LOWPC and HIGHPC from
7725 `DW_AT_ranges'. */
7726 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7727 &best_highpc, cu, pst);
7728 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7729 {
7730 CORE_ADDR low
7731 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7732 - baseaddr);
7733 CORE_ADDR high
7734 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7735 - baseaddr - 1);
7736 /* Store the contiguous range if it is not empty; it can be
7737 empty for CUs with no code. */
7738 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7739 low, high, pst);
7740 }
7741
7742 /* Check if comp unit has_children.
7743 If so, read the rest of the partial symbols from this comp unit.
7744 If not, there's no more debug_info for this comp unit. */
7745 if (comp_unit_die->has_children)
7746 {
7747 struct partial_die_info *first_die;
7748 CORE_ADDR lowpc, highpc;
7749
7750 lowpc = ((CORE_ADDR) -1);
7751 highpc = ((CORE_ADDR) 0);
7752
7753 first_die = load_partial_dies (reader, info_ptr, 1);
7754
7755 scan_partial_symbols (first_die, &lowpc, &highpc,
7756 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7757
7758 /* If we didn't find a lowpc, set it to highpc to avoid
7759 complaints from `maint check'. */
7760 if (lowpc == ((CORE_ADDR) -1))
7761 lowpc = highpc;
7762
7763 /* If the compilation unit didn't have an explicit address range,
7764 then use the information extracted from its child dies. */
7765 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7766 {
7767 best_lowpc = lowpc;
7768 best_highpc = highpc;
7769 }
7770 }
7771 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7772 best_lowpc + baseaddr)
7773 - baseaddr);
7774 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7775 best_highpc + baseaddr)
7776 - baseaddr);
7777
7778 pst->end ();
7779
7780 if (!cu->per_cu->imported_symtabs_empty ())
7781 {
7782 int i;
7783 int len = cu->per_cu->imported_symtabs_size ();
7784
7785 /* Fill in 'dependencies' here; we fill in 'users' in a
7786 post-pass. */
7787 pst->number_of_dependencies = len;
7788 pst->dependencies
7789 = objfile->partial_symtabs->allocate_dependencies (len);
7790 for (i = 0; i < len; ++i)
7791 {
7792 pst->dependencies[i]
7793 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7794 }
7795
7796 cu->per_cu->imported_symtabs_free ();
7797 }
7798
7799 /* Get the list of files included in the current compilation unit,
7800 and build a psymtab for each of them. */
7801 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7802
7803 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7804 ", %d global, %d static syms",
7805 per_cu->is_debug_types ? "type" : "comp",
7806 sect_offset_str (per_cu->sect_off),
7807 paddress (gdbarch, pst->text_low (objfile)),
7808 paddress (gdbarch, pst->text_high (objfile)),
7809 (int) pst->global_psymbols.size (),
7810 (int) pst->static_psymbols.size ());
7811 }
7812
7813 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7814 Process compilation unit THIS_CU for a psymtab. */
7815
7816 static void
7817 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7818 dwarf2_per_objfile *per_objfile,
7819 bool want_partial_unit,
7820 enum language pretend_language)
7821 {
7822 /* If this compilation unit was already read in, free the
7823 cached copy in order to read it in again. This is
7824 necessary because we skipped some symbols when we first
7825 read in the compilation unit (see load_partial_dies).
7826 This problem could be avoided, but the benefit is unclear. */
7827 per_objfile->remove_cu (this_cu);
7828
7829 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7830
7831 switch (reader.comp_unit_die->tag)
7832 {
7833 case DW_TAG_compile_unit:
7834 this_cu->unit_type = DW_UT_compile;
7835 break;
7836 case DW_TAG_partial_unit:
7837 this_cu->unit_type = DW_UT_partial;
7838 break;
7839 case DW_TAG_type_unit:
7840 this_cu->unit_type = DW_UT_type;
7841 break;
7842 default:
7843 abort ();
7844 }
7845
7846 if (reader.dummy_p)
7847 {
7848 /* Nothing. */
7849 }
7850 else if (this_cu->is_debug_types)
7851 build_type_psymtabs_reader (&reader, reader.info_ptr,
7852 reader.comp_unit_die);
7853 else if (want_partial_unit
7854 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7855 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7856 reader.comp_unit_die,
7857 pretend_language);
7858
7859 this_cu->lang = reader.cu->language;
7860
7861 /* Age out any secondary CUs. */
7862 per_objfile->age_comp_units ();
7863 }
7864
7865 /* Reader function for build_type_psymtabs. */
7866
7867 static void
7868 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7869 const gdb_byte *info_ptr,
7870 struct die_info *type_unit_die)
7871 {
7872 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7873 struct dwarf2_cu *cu = reader->cu;
7874 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7875 struct signatured_type *sig_type;
7876 struct type_unit_group *tu_group;
7877 struct attribute *attr;
7878 struct partial_die_info *first_die;
7879 CORE_ADDR lowpc, highpc;
7880 dwarf2_psymtab *pst;
7881
7882 gdb_assert (per_cu->is_debug_types);
7883 sig_type = (struct signatured_type *) per_cu;
7884
7885 if (! type_unit_die->has_children)
7886 return;
7887
7888 attr = type_unit_die->attr (DW_AT_stmt_list);
7889 tu_group = get_type_unit_group (cu, attr);
7890
7891 if (tu_group->tus == nullptr)
7892 tu_group->tus = new std::vector<signatured_type *>;
7893 tu_group->tus->push_back (sig_type);
7894
7895 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7896 pst = create_partial_symtab (per_cu, per_objfile, "");
7897 pst->anonymous = true;
7898
7899 first_die = load_partial_dies (reader, info_ptr, 1);
7900
7901 lowpc = (CORE_ADDR) -1;
7902 highpc = (CORE_ADDR) 0;
7903 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7904
7905 pst->end ();
7906 }
7907
7908 /* Struct used to sort TUs by their abbreviation table offset. */
7909
7910 struct tu_abbrev_offset
7911 {
7912 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7913 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7914 {}
7915
7916 signatured_type *sig_type;
7917 sect_offset abbrev_offset;
7918 };
7919
7920 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7921
7922 static bool
7923 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7924 const struct tu_abbrev_offset &b)
7925 {
7926 return a.abbrev_offset < b.abbrev_offset;
7927 }
7928
7929 /* Efficiently read all the type units.
7930 This does the bulk of the work for build_type_psymtabs.
7931
7932 The efficiency is because we sort TUs by the abbrev table they use and
7933 only read each abbrev table once. In one program there are 200K TUs
7934 sharing 8K abbrev tables.
7935
7936 The main purpose of this function is to support building the
7937 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7938 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7939 can collapse the search space by grouping them by stmt_list.
7940 The savings can be significant, in the same program from above the 200K TUs
7941 share 8K stmt_list tables.
7942
7943 FUNC is expected to call get_type_unit_group, which will create the
7944 struct type_unit_group if necessary and add it to
7945 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7946
7947 static void
7948 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7949 {
7950 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7951 abbrev_table_up abbrev_table;
7952 sect_offset abbrev_offset;
7953
7954 /* It's up to the caller to not call us multiple times. */
7955 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7956
7957 if (per_objfile->per_bfd->all_type_units.empty ())
7958 return;
7959
7960 /* TUs typically share abbrev tables, and there can be way more TUs than
7961 abbrev tables. Sort by abbrev table to reduce the number of times we
7962 read each abbrev table in.
7963 Alternatives are to punt or to maintain a cache of abbrev tables.
7964 This is simpler and efficient enough for now.
7965
7966 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7967 symtab to use). Typically TUs with the same abbrev offset have the same
7968 stmt_list value too so in practice this should work well.
7969
7970 The basic algorithm here is:
7971
7972 sort TUs by abbrev table
7973 for each TU with same abbrev table:
7974 read abbrev table if first user
7975 read TU top level DIE
7976 [IWBN if DWO skeletons had DW_AT_stmt_list]
7977 call FUNC */
7978
7979 dwarf_read_debug_printf ("Building type unit groups ...");
7980
7981 /* Sort in a separate table to maintain the order of all_type_units
7982 for .gdb_index: TU indices directly index all_type_units. */
7983 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7984 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7985
7986 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7987 sorted_by_abbrev.emplace_back
7988 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7989 sig_type->per_cu.sect_off));
7990
7991 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7992 sort_tu_by_abbrev_offset);
7993
7994 abbrev_offset = (sect_offset) ~(unsigned) 0;
7995
7996 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7997 {
7998 /* Switch to the next abbrev table if necessary. */
7999 if (abbrev_table == NULL
8000 || tu.abbrev_offset != abbrev_offset)
8001 {
8002 abbrev_offset = tu.abbrev_offset;
8003 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
8004 abbrev_table =
8005 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
8006 ++tu_stats->nr_uniq_abbrev_tables;
8007 }
8008
8009 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
8010 abbrev_table.get (), nullptr, false);
8011 if (!reader.dummy_p)
8012 build_type_psymtabs_reader (&reader, reader.info_ptr,
8013 reader.comp_unit_die);
8014 }
8015 }
8016
8017 /* Print collected type unit statistics. */
8018
8019 static void
8020 print_tu_stats (dwarf2_per_objfile *per_objfile)
8021 {
8022 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
8023
8024 dwarf_read_debug_printf ("Type unit statistics:");
8025 dwarf_read_debug_printf (" %zu TUs",
8026 per_objfile->per_bfd->all_type_units.size ());
8027 dwarf_read_debug_printf (" %d uniq abbrev tables",
8028 tu_stats->nr_uniq_abbrev_tables);
8029 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
8030 tu_stats->nr_symtabs);
8031 dwarf_read_debug_printf (" %d symtab sharers",
8032 tu_stats->nr_symtab_sharers);
8033 dwarf_read_debug_printf (" %d type units without a stmt_list",
8034 tu_stats->nr_stmt_less_type_units);
8035 dwarf_read_debug_printf (" %d all_type_units reallocs",
8036 tu_stats->nr_all_type_units_reallocs);
8037 }
8038
8039 /* Traversal function for build_type_psymtabs. */
8040
8041 static int
8042 build_type_psymtab_dependencies (void **slot, void *info)
8043 {
8044 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8045 struct objfile *objfile = per_objfile->objfile;
8046 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8047 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8048 dwarf2_psymtab *pst = per_cu->v.psymtab;
8049 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8050 int i;
8051
8052 gdb_assert (len > 0);
8053 gdb_assert (per_cu->type_unit_group_p ());
8054
8055 pst->number_of_dependencies = len;
8056 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8057 for (i = 0; i < len; ++i)
8058 {
8059 struct signatured_type *iter = tu_group->tus->at (i);
8060 gdb_assert (iter->per_cu.is_debug_types);
8061 pst->dependencies[i] = iter->per_cu.v.psymtab;
8062 iter->type_unit_group = tu_group;
8063 }
8064
8065 delete tu_group->tus;
8066 tu_group->tus = nullptr;
8067
8068 return 1;
8069 }
8070
8071 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8072 Build partial symbol tables for the .debug_types comp-units. */
8073
8074 static void
8075 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8076 {
8077 if (! create_all_type_units (per_objfile))
8078 return;
8079
8080 build_type_psymtabs_1 (per_objfile);
8081 }
8082
8083 /* Traversal function for process_skeletonless_type_unit.
8084 Read a TU in a DWO file and build partial symbols for it. */
8085
8086 static int
8087 process_skeletonless_type_unit (void **slot, void *info)
8088 {
8089 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8090 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8091 struct signatured_type find_entry, *entry;
8092
8093 /* If this TU doesn't exist in the global table, add it and read it in. */
8094
8095 if (per_objfile->per_bfd->signatured_types == NULL)
8096 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8097
8098 find_entry.signature = dwo_unit->signature;
8099 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8100 &find_entry, INSERT);
8101 /* If we've already seen this type there's nothing to do. What's happening
8102 is we're doing our own version of comdat-folding here. */
8103 if (*slot != NULL)
8104 return 1;
8105
8106 /* This does the job that create_all_type_units would have done for
8107 this TU. */
8108 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8109 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8110 *slot = entry;
8111
8112 /* This does the job that build_type_psymtabs_1 would have done. */
8113 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8114 if (!reader.dummy_p)
8115 build_type_psymtabs_reader (&reader, reader.info_ptr,
8116 reader.comp_unit_die);
8117
8118 return 1;
8119 }
8120
8121 /* Traversal function for process_skeletonless_type_units. */
8122
8123 static int
8124 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8125 {
8126 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8127
8128 if (dwo_file->tus != NULL)
8129 htab_traverse_noresize (dwo_file->tus.get (),
8130 process_skeletonless_type_unit, info);
8131
8132 return 1;
8133 }
8134
8135 /* Scan all TUs of DWO files, verifying we've processed them.
8136 This is needed in case a TU was emitted without its skeleton.
8137 Note: This can't be done until we know what all the DWO files are. */
8138
8139 static void
8140 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8141 {
8142 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8143 if (get_dwp_file (per_objfile) == NULL
8144 && per_objfile->per_bfd->dwo_files != NULL)
8145 {
8146 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8147 process_dwo_file_for_skeletonless_type_units,
8148 per_objfile);
8149 }
8150 }
8151
8152 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8153
8154 static void
8155 set_partial_user (dwarf2_per_objfile *per_objfile)
8156 {
8157 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8158 {
8159 dwarf2_psymtab *pst = per_cu->v.psymtab;
8160
8161 if (pst == NULL)
8162 continue;
8163
8164 for (int j = 0; j < pst->number_of_dependencies; ++j)
8165 {
8166 /* Set the 'user' field only if it is not already set. */
8167 if (pst->dependencies[j]->user == NULL)
8168 pst->dependencies[j]->user = pst;
8169 }
8170 }
8171 }
8172
8173 /* Build the partial symbol table by doing a quick pass through the
8174 .debug_info and .debug_abbrev sections. */
8175
8176 static void
8177 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8178 {
8179 struct objfile *objfile = per_objfile->objfile;
8180
8181 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8182 objfile_name (objfile));
8183
8184 scoped_restore restore_reading_psyms
8185 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8186 true);
8187
8188 per_objfile->per_bfd->info.read (objfile);
8189
8190 /* Any cached compilation units will be linked by the per-objfile
8191 read_in_chain. Make sure to free them when we're done. */
8192 free_cached_comp_units freer (per_objfile);
8193
8194 build_type_psymtabs (per_objfile);
8195
8196 create_all_comp_units (per_objfile);
8197
8198 /* Create a temporary address map on a temporary obstack. We later
8199 copy this to the final obstack. */
8200 auto_obstack temp_obstack;
8201
8202 scoped_restore save_psymtabs_addrmap
8203 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8204 addrmap_create_mutable (&temp_obstack));
8205
8206 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8207 {
8208 if (per_cu->v.psymtab != NULL)
8209 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8210 continue;
8211 process_psymtab_comp_unit (per_cu, per_objfile, false,
8212 language_minimal);
8213 }
8214
8215 /* This has to wait until we read the CUs, we need the list of DWOs. */
8216 process_skeletonless_type_units (per_objfile);
8217
8218 /* Now that all TUs have been processed we can fill in the dependencies. */
8219 if (per_objfile->per_bfd->type_unit_groups != NULL)
8220 {
8221 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8222 build_type_psymtab_dependencies, per_objfile);
8223 }
8224
8225 if (dwarf_read_debug > 0)
8226 print_tu_stats (per_objfile);
8227
8228 set_partial_user (per_objfile);
8229
8230 objfile->partial_symtabs->psymtabs_addrmap
8231 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8232 objfile->partial_symtabs->obstack ());
8233 /* At this point we want to keep the address map. */
8234 save_psymtabs_addrmap.release ();
8235
8236 dwarf_read_debug_printf ("Done building psymtabs of %s",
8237 objfile_name (objfile));
8238 }
8239
8240 /* Load the partial DIEs for a secondary CU into memory.
8241 This is also used when rereading a primary CU with load_all_dies. */
8242
8243 static void
8244 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8245 dwarf2_per_objfile *per_objfile,
8246 dwarf2_cu *existing_cu)
8247 {
8248 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8249
8250 if (!reader.dummy_p)
8251 {
8252 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8253 language_minimal);
8254
8255 /* Check if comp unit has_children.
8256 If so, read the rest of the partial symbols from this comp unit.
8257 If not, there's no more debug_info for this comp unit. */
8258 if (reader.comp_unit_die->has_children)
8259 load_partial_dies (&reader, reader.info_ptr, 0);
8260
8261 reader.keep ();
8262 }
8263 }
8264
8265 static void
8266 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8267 struct dwarf2_section_info *section,
8268 struct dwarf2_section_info *abbrev_section,
8269 unsigned int is_dwz)
8270 {
8271 const gdb_byte *info_ptr;
8272 struct objfile *objfile = per_objfile->objfile;
8273
8274 dwarf_read_debug_printf ("Reading %s for %s",
8275 section->get_name (),
8276 section->get_file_name ());
8277
8278 section->read (objfile);
8279
8280 info_ptr = section->buffer;
8281
8282 while (info_ptr < section->buffer + section->size)
8283 {
8284 struct dwarf2_per_cu_data *this_cu;
8285
8286 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8287
8288 comp_unit_head cu_header;
8289 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8290 abbrev_section, info_ptr,
8291 rcuh_kind::COMPILE);
8292
8293 /* Save the compilation unit for later lookup. */
8294 if (cu_header.unit_type != DW_UT_type)
8295 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8296 else
8297 {
8298 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8299 sig_type->signature = cu_header.signature;
8300 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8301 this_cu = &sig_type->per_cu;
8302 }
8303 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8304 this_cu->sect_off = sect_off;
8305 this_cu->length = cu_header.length + cu_header.initial_length_size;
8306 this_cu->is_dwz = is_dwz;
8307 this_cu->section = section;
8308
8309 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8310
8311 info_ptr = info_ptr + this_cu->length;
8312 }
8313 }
8314
8315 /* Create a list of all compilation units in OBJFILE.
8316 This is only done for -readnow and building partial symtabs. */
8317
8318 static void
8319 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8320 {
8321 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8322 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8323 &per_objfile->per_bfd->abbrev, 0);
8324
8325 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8326 if (dwz != NULL)
8327 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8328 }
8329
8330 /* Process all loaded DIEs for compilation unit CU, starting at
8331 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8332 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8333 DW_AT_ranges). See the comments of add_partial_subprogram on how
8334 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8335
8336 static void
8337 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8338 CORE_ADDR *highpc, int set_addrmap,
8339 struct dwarf2_cu *cu)
8340 {
8341 struct partial_die_info *pdi;
8342
8343 /* Now, march along the PDI's, descending into ones which have
8344 interesting children but skipping the children of the other ones,
8345 until we reach the end of the compilation unit. */
8346
8347 pdi = first_die;
8348
8349 while (pdi != NULL)
8350 {
8351 pdi->fixup (cu);
8352
8353 /* Anonymous namespaces or modules have no name but have interesting
8354 children, so we need to look at them. Ditto for anonymous
8355 enums. */
8356
8357 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8358 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8359 || pdi->tag == DW_TAG_imported_unit
8360 || pdi->tag == DW_TAG_inlined_subroutine)
8361 {
8362 switch (pdi->tag)
8363 {
8364 case DW_TAG_subprogram:
8365 case DW_TAG_inlined_subroutine:
8366 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8367 if (cu->language == language_cplus)
8368 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8369 set_addrmap, cu);
8370 break;
8371 case DW_TAG_constant:
8372 case DW_TAG_variable:
8373 case DW_TAG_typedef:
8374 case DW_TAG_union_type:
8375 if (!pdi->is_declaration
8376 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8377 {
8378 add_partial_symbol (pdi, cu);
8379 }
8380 break;
8381 case DW_TAG_class_type:
8382 case DW_TAG_interface_type:
8383 case DW_TAG_structure_type:
8384 if (!pdi->is_declaration)
8385 {
8386 add_partial_symbol (pdi, cu);
8387 }
8388 if ((cu->language == language_rust
8389 || cu->language == language_cplus) && pdi->has_children)
8390 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8391 set_addrmap, cu);
8392 break;
8393 case DW_TAG_enumeration_type:
8394 if (!pdi->is_declaration)
8395 add_partial_enumeration (pdi, cu);
8396 break;
8397 case DW_TAG_base_type:
8398 case DW_TAG_subrange_type:
8399 /* File scope base type definitions are added to the partial
8400 symbol table. */
8401 add_partial_symbol (pdi, cu);
8402 break;
8403 case DW_TAG_namespace:
8404 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8405 break;
8406 case DW_TAG_module:
8407 if (!pdi->is_declaration)
8408 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8409 break;
8410 case DW_TAG_imported_unit:
8411 {
8412 struct dwarf2_per_cu_data *per_cu;
8413
8414 /* For now we don't handle imported units in type units. */
8415 if (cu->per_cu->is_debug_types)
8416 {
8417 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8418 " supported in type units [in module %s]"),
8419 objfile_name (cu->per_objfile->objfile));
8420 }
8421
8422 per_cu = dwarf2_find_containing_comp_unit
8423 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8424
8425 /* Go read the partial unit, if needed. */
8426 if (per_cu->v.psymtab == NULL)
8427 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8428 cu->language);
8429
8430 cu->per_cu->imported_symtabs_push (per_cu);
8431 }
8432 break;
8433 case DW_TAG_imported_declaration:
8434 add_partial_symbol (pdi, cu);
8435 break;
8436 default:
8437 break;
8438 }
8439 }
8440
8441 /* If the die has a sibling, skip to the sibling. */
8442
8443 pdi = pdi->die_sibling;
8444 }
8445 }
8446
8447 /* Functions used to compute the fully scoped name of a partial DIE.
8448
8449 Normally, this is simple. For C++, the parent DIE's fully scoped
8450 name is concatenated with "::" and the partial DIE's name.
8451 Enumerators are an exception; they use the scope of their parent
8452 enumeration type, i.e. the name of the enumeration type is not
8453 prepended to the enumerator.
8454
8455 There are two complexities. One is DW_AT_specification; in this
8456 case "parent" means the parent of the target of the specification,
8457 instead of the direct parent of the DIE. The other is compilers
8458 which do not emit DW_TAG_namespace; in this case we try to guess
8459 the fully qualified name of structure types from their members'
8460 linkage names. This must be done using the DIE's children rather
8461 than the children of any DW_AT_specification target. We only need
8462 to do this for structures at the top level, i.e. if the target of
8463 any DW_AT_specification (if any; otherwise the DIE itself) does not
8464 have a parent. */
8465
8466 /* Compute the scope prefix associated with PDI's parent, in
8467 compilation unit CU. The result will be allocated on CU's
8468 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8469 field. NULL is returned if no prefix is necessary. */
8470 static const char *
8471 partial_die_parent_scope (struct partial_die_info *pdi,
8472 struct dwarf2_cu *cu)
8473 {
8474 const char *grandparent_scope;
8475 struct partial_die_info *parent, *real_pdi;
8476
8477 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8478 then this means the parent of the specification DIE. */
8479
8480 real_pdi = pdi;
8481 while (real_pdi->has_specification)
8482 {
8483 auto res = find_partial_die (real_pdi->spec_offset,
8484 real_pdi->spec_is_dwz, cu);
8485 real_pdi = res.pdi;
8486 cu = res.cu;
8487 }
8488
8489 parent = real_pdi->die_parent;
8490 if (parent == NULL)
8491 return NULL;
8492
8493 if (parent->scope_set)
8494 return parent->scope;
8495
8496 parent->fixup (cu);
8497
8498 grandparent_scope = partial_die_parent_scope (parent, cu);
8499
8500 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8501 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8502 Work around this problem here. */
8503 if (cu->language == language_cplus
8504 && parent->tag == DW_TAG_namespace
8505 && strcmp (parent->name (cu), "::") == 0
8506 && grandparent_scope == NULL)
8507 {
8508 parent->scope = NULL;
8509 parent->scope_set = 1;
8510 return NULL;
8511 }
8512
8513 /* Nested subroutines in Fortran get a prefix. */
8514 if (pdi->tag == DW_TAG_enumerator)
8515 /* Enumerators should not get the name of the enumeration as a prefix. */
8516 parent->scope = grandparent_scope;
8517 else if (parent->tag == DW_TAG_namespace
8518 || parent->tag == DW_TAG_module
8519 || parent->tag == DW_TAG_structure_type
8520 || parent->tag == DW_TAG_class_type
8521 || parent->tag == DW_TAG_interface_type
8522 || parent->tag == DW_TAG_union_type
8523 || parent->tag == DW_TAG_enumeration_type
8524 || (cu->language == language_fortran
8525 && parent->tag == DW_TAG_subprogram
8526 && pdi->tag == DW_TAG_subprogram))
8527 {
8528 if (grandparent_scope == NULL)
8529 parent->scope = parent->name (cu);
8530 else
8531 parent->scope = typename_concat (&cu->comp_unit_obstack,
8532 grandparent_scope,
8533 parent->name (cu), 0, cu);
8534 }
8535 else
8536 {
8537 /* FIXME drow/2004-04-01: What should we be doing with
8538 function-local names? For partial symbols, we should probably be
8539 ignoring them. */
8540 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8541 dwarf_tag_name (parent->tag),
8542 sect_offset_str (pdi->sect_off));
8543 parent->scope = grandparent_scope;
8544 }
8545
8546 parent->scope_set = 1;
8547 return parent->scope;
8548 }
8549
8550 /* Return the fully scoped name associated with PDI, from compilation unit
8551 CU. The result will be allocated with malloc. */
8552
8553 static gdb::unique_xmalloc_ptr<char>
8554 partial_die_full_name (struct partial_die_info *pdi,
8555 struct dwarf2_cu *cu)
8556 {
8557 const char *parent_scope;
8558
8559 /* If this is a template instantiation, we can not work out the
8560 template arguments from partial DIEs. So, unfortunately, we have
8561 to go through the full DIEs. At least any work we do building
8562 types here will be reused if full symbols are loaded later. */
8563 if (pdi->has_template_arguments)
8564 {
8565 pdi->fixup (cu);
8566
8567 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8568 {
8569 struct die_info *die;
8570 struct attribute attr;
8571 struct dwarf2_cu *ref_cu = cu;
8572
8573 /* DW_FORM_ref_addr is using section offset. */
8574 attr.name = (enum dwarf_attribute) 0;
8575 attr.form = DW_FORM_ref_addr;
8576 attr.u.unsnd = to_underlying (pdi->sect_off);
8577 die = follow_die_ref (NULL, &attr, &ref_cu);
8578
8579 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8580 }
8581 }
8582
8583 parent_scope = partial_die_parent_scope (pdi, cu);
8584 if (parent_scope == NULL)
8585 return NULL;
8586 else
8587 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8588 pdi->name (cu),
8589 0, cu));
8590 }
8591
8592 static void
8593 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8594 {
8595 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8596 struct objfile *objfile = per_objfile->objfile;
8597 struct gdbarch *gdbarch = objfile->arch ();
8598 CORE_ADDR addr = 0;
8599 const char *actual_name = NULL;
8600 CORE_ADDR baseaddr;
8601
8602 baseaddr = objfile->text_section_offset ();
8603
8604 gdb::unique_xmalloc_ptr<char> built_actual_name
8605 = partial_die_full_name (pdi, cu);
8606 if (built_actual_name != NULL)
8607 actual_name = built_actual_name.get ();
8608
8609 if (actual_name == NULL)
8610 actual_name = pdi->name (cu);
8611
8612 partial_symbol psymbol;
8613 memset (&psymbol, 0, sizeof (psymbol));
8614 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8615 psymbol.ginfo.set_section_index (-1);
8616
8617 /* The code below indicates that the psymbol should be installed by
8618 setting this. */
8619 gdb::optional<psymbol_placement> where;
8620
8621 switch (pdi->tag)
8622 {
8623 case DW_TAG_inlined_subroutine:
8624 case DW_TAG_subprogram:
8625 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8626 - baseaddr);
8627 if (pdi->is_external
8628 || cu->language == language_ada
8629 || (cu->language == language_fortran
8630 && pdi->die_parent != NULL
8631 && pdi->die_parent->tag == DW_TAG_subprogram))
8632 {
8633 /* Normally, only "external" DIEs are part of the global scope.
8634 But in Ada and Fortran, we want to be able to access nested
8635 procedures globally. So all Ada and Fortran subprograms are
8636 stored in the global scope. */
8637 where = psymbol_placement::GLOBAL;
8638 }
8639 else
8640 where = psymbol_placement::STATIC;
8641
8642 psymbol.domain = VAR_DOMAIN;
8643 psymbol.aclass = LOC_BLOCK;
8644 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8645 psymbol.ginfo.value.address = addr;
8646
8647 if (pdi->main_subprogram && actual_name != NULL)
8648 set_objfile_main_name (objfile, actual_name, cu->language);
8649 break;
8650 case DW_TAG_constant:
8651 psymbol.domain = VAR_DOMAIN;
8652 psymbol.aclass = LOC_STATIC;
8653 where = (pdi->is_external
8654 ? psymbol_placement::GLOBAL
8655 : psymbol_placement::STATIC);
8656 break;
8657 case DW_TAG_variable:
8658 if (pdi->d.locdesc)
8659 addr = decode_locdesc (pdi->d.locdesc, cu);
8660
8661 if (pdi->d.locdesc
8662 && addr == 0
8663 && !per_objfile->per_bfd->has_section_at_zero)
8664 {
8665 /* A global or static variable may also have been stripped
8666 out by the linker if unused, in which case its address
8667 will be nullified; do not add such variables into partial
8668 symbol table then. */
8669 }
8670 else if (pdi->is_external)
8671 {
8672 /* Global Variable.
8673 Don't enter into the minimal symbol tables as there is
8674 a minimal symbol table entry from the ELF symbols already.
8675 Enter into partial symbol table if it has a location
8676 descriptor or a type.
8677 If the location descriptor is missing, new_symbol will create
8678 a LOC_UNRESOLVED symbol, the address of the variable will then
8679 be determined from the minimal symbol table whenever the variable
8680 is referenced.
8681 The address for the partial symbol table entry is not
8682 used by GDB, but it comes in handy for debugging partial symbol
8683 table building. */
8684
8685 if (pdi->d.locdesc || pdi->has_type)
8686 {
8687 psymbol.domain = VAR_DOMAIN;
8688 psymbol.aclass = LOC_STATIC;
8689 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8690 psymbol.ginfo.value.address = addr;
8691 where = psymbol_placement::GLOBAL;
8692 }
8693 }
8694 else
8695 {
8696 int has_loc = pdi->d.locdesc != NULL;
8697
8698 /* Static Variable. Skip symbols whose value we cannot know (those
8699 without location descriptors or constant values). */
8700 if (!has_loc && !pdi->has_const_value)
8701 return;
8702
8703 psymbol.domain = VAR_DOMAIN;
8704 psymbol.aclass = LOC_STATIC;
8705 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8706 if (has_loc)
8707 psymbol.ginfo.value.address = addr;
8708 where = psymbol_placement::STATIC;
8709 }
8710 break;
8711 case DW_TAG_array_type:
8712 case DW_TAG_typedef:
8713 case DW_TAG_base_type:
8714 case DW_TAG_subrange_type:
8715 psymbol.domain = VAR_DOMAIN;
8716 psymbol.aclass = LOC_TYPEDEF;
8717 where = psymbol_placement::STATIC;
8718 break;
8719 case DW_TAG_imported_declaration:
8720 case DW_TAG_namespace:
8721 psymbol.domain = VAR_DOMAIN;
8722 psymbol.aclass = LOC_TYPEDEF;
8723 where = psymbol_placement::GLOBAL;
8724 break;
8725 case DW_TAG_module:
8726 /* With Fortran 77 there might be a "BLOCK DATA" module
8727 available without any name. If so, we skip the module as it
8728 doesn't bring any value. */
8729 if (actual_name != nullptr)
8730 {
8731 psymbol.domain = MODULE_DOMAIN;
8732 psymbol.aclass = LOC_TYPEDEF;
8733 where = psymbol_placement::GLOBAL;
8734 }
8735 break;
8736 case DW_TAG_class_type:
8737 case DW_TAG_interface_type:
8738 case DW_TAG_structure_type:
8739 case DW_TAG_union_type:
8740 case DW_TAG_enumeration_type:
8741 /* Skip external references. The DWARF standard says in the section
8742 about "Structure, Union, and Class Type Entries": "An incomplete
8743 structure, union or class type is represented by a structure,
8744 union or class entry that does not have a byte size attribute
8745 and that has a DW_AT_declaration attribute." */
8746 if (!pdi->has_byte_size && pdi->is_declaration)
8747 return;
8748
8749 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8750 static vs. global. */
8751 psymbol.domain = STRUCT_DOMAIN;
8752 psymbol.aclass = LOC_TYPEDEF;
8753 where = (cu->language == language_cplus
8754 ? psymbol_placement::GLOBAL
8755 : psymbol_placement::STATIC);
8756 break;
8757 case DW_TAG_enumerator:
8758 psymbol.domain = VAR_DOMAIN;
8759 psymbol.aclass = LOC_CONST;
8760 where = (cu->language == language_cplus
8761 ? psymbol_placement::GLOBAL
8762 : psymbol_placement::STATIC);
8763 break;
8764 default:
8765 break;
8766 }
8767
8768 if (where.has_value ())
8769 {
8770 if (built_actual_name != nullptr)
8771 actual_name = objfile->intern (actual_name);
8772 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8773 psymbol.ginfo.set_linkage_name (actual_name);
8774 else
8775 {
8776 psymbol.ginfo.set_demangled_name (actual_name,
8777 &objfile->objfile_obstack);
8778 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8779 }
8780 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8781 }
8782 }
8783
8784 /* Read a partial die corresponding to a namespace; also, add a symbol
8785 corresponding to that namespace to the symbol table. NAMESPACE is
8786 the name of the enclosing namespace. */
8787
8788 static void
8789 add_partial_namespace (struct partial_die_info *pdi,
8790 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8791 int set_addrmap, struct dwarf2_cu *cu)
8792 {
8793 /* Add a symbol for the namespace. */
8794
8795 add_partial_symbol (pdi, cu);
8796
8797 /* Now scan partial symbols in that namespace. */
8798
8799 if (pdi->has_children)
8800 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8801 }
8802
8803 /* Read a partial die corresponding to a Fortran module. */
8804
8805 static void
8806 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8807 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8808 {
8809 /* Add a symbol for the namespace. */
8810
8811 add_partial_symbol (pdi, cu);
8812
8813 /* Now scan partial symbols in that module. */
8814
8815 if (pdi->has_children)
8816 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8817 }
8818
8819 /* Read a partial die corresponding to a subprogram or an inlined
8820 subprogram and create a partial symbol for that subprogram.
8821 When the CU language allows it, this routine also defines a partial
8822 symbol for each nested subprogram that this subprogram contains.
8823 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8824 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8825
8826 PDI may also be a lexical block, in which case we simply search
8827 recursively for subprograms defined inside that lexical block.
8828 Again, this is only performed when the CU language allows this
8829 type of definitions. */
8830
8831 static void
8832 add_partial_subprogram (struct partial_die_info *pdi,
8833 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8834 int set_addrmap, struct dwarf2_cu *cu)
8835 {
8836 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8837 {
8838 if (pdi->has_pc_info)
8839 {
8840 if (pdi->lowpc < *lowpc)
8841 *lowpc = pdi->lowpc;
8842 if (pdi->highpc > *highpc)
8843 *highpc = pdi->highpc;
8844 if (set_addrmap)
8845 {
8846 struct objfile *objfile = cu->per_objfile->objfile;
8847 struct gdbarch *gdbarch = objfile->arch ();
8848 CORE_ADDR baseaddr;
8849 CORE_ADDR this_highpc;
8850 CORE_ADDR this_lowpc;
8851
8852 baseaddr = objfile->text_section_offset ();
8853 this_lowpc
8854 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8855 pdi->lowpc + baseaddr)
8856 - baseaddr);
8857 this_highpc
8858 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8859 pdi->highpc + baseaddr)
8860 - baseaddr);
8861 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8862 this_lowpc, this_highpc - 1,
8863 cu->per_cu->v.psymtab);
8864 }
8865 }
8866
8867 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8868 {
8869 if (!pdi->is_declaration)
8870 /* Ignore subprogram DIEs that do not have a name, they are
8871 illegal. Do not emit a complaint at this point, we will
8872 do so when we convert this psymtab into a symtab. */
8873 if (pdi->name (cu))
8874 add_partial_symbol (pdi, cu);
8875 }
8876 }
8877
8878 if (! pdi->has_children)
8879 return;
8880
8881 if (cu->language == language_ada || cu->language == language_fortran)
8882 {
8883 pdi = pdi->die_child;
8884 while (pdi != NULL)
8885 {
8886 pdi->fixup (cu);
8887 if (pdi->tag == DW_TAG_subprogram
8888 || pdi->tag == DW_TAG_inlined_subroutine
8889 || pdi->tag == DW_TAG_lexical_block)
8890 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8891 pdi = pdi->die_sibling;
8892 }
8893 }
8894 }
8895
8896 /* Read a partial die corresponding to an enumeration type. */
8897
8898 static void
8899 add_partial_enumeration (struct partial_die_info *enum_pdi,
8900 struct dwarf2_cu *cu)
8901 {
8902 struct partial_die_info *pdi;
8903
8904 if (enum_pdi->name (cu) != NULL)
8905 add_partial_symbol (enum_pdi, cu);
8906
8907 pdi = enum_pdi->die_child;
8908 while (pdi)
8909 {
8910 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8911 complaint (_("malformed enumerator DIE ignored"));
8912 else
8913 add_partial_symbol (pdi, cu);
8914 pdi = pdi->die_sibling;
8915 }
8916 }
8917
8918 /* Return the initial uleb128 in the die at INFO_PTR. */
8919
8920 static unsigned int
8921 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8922 {
8923 unsigned int bytes_read;
8924
8925 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8926 }
8927
8928 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8929 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8930
8931 Return the corresponding abbrev, or NULL if the number is zero (indicating
8932 an empty DIE). In either case *BYTES_READ will be set to the length of
8933 the initial number. */
8934
8935 static struct abbrev_info *
8936 peek_die_abbrev (const die_reader_specs &reader,
8937 const gdb_byte *info_ptr, unsigned int *bytes_read)
8938 {
8939 dwarf2_cu *cu = reader.cu;
8940 bfd *abfd = cu->per_objfile->objfile->obfd;
8941 unsigned int abbrev_number
8942 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8943
8944 if (abbrev_number == 0)
8945 return NULL;
8946
8947 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8948 if (!abbrev)
8949 {
8950 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8951 " at offset %s [in module %s]"),
8952 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8953 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8954 }
8955
8956 return abbrev;
8957 }
8958
8959 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8960 Returns a pointer to the end of a series of DIEs, terminated by an empty
8961 DIE. Any children of the skipped DIEs will also be skipped. */
8962
8963 static const gdb_byte *
8964 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8965 {
8966 while (1)
8967 {
8968 unsigned int bytes_read;
8969 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8970
8971 if (abbrev == NULL)
8972 return info_ptr + bytes_read;
8973 else
8974 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8975 }
8976 }
8977
8978 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8979 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8980 abbrev corresponding to that skipped uleb128 should be passed in
8981 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8982 children. */
8983
8984 static const gdb_byte *
8985 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8986 struct abbrev_info *abbrev)
8987 {
8988 unsigned int bytes_read;
8989 struct attribute attr;
8990 bfd *abfd = reader->abfd;
8991 struct dwarf2_cu *cu = reader->cu;
8992 const gdb_byte *buffer = reader->buffer;
8993 const gdb_byte *buffer_end = reader->buffer_end;
8994 unsigned int form, i;
8995
8996 for (i = 0; i < abbrev->num_attrs; i++)
8997 {
8998 /* The only abbrev we care about is DW_AT_sibling. */
8999 if (abbrev->attrs[i].name == DW_AT_sibling)
9000 {
9001 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
9002 if (attr.form == DW_FORM_ref_addr)
9003 complaint (_("ignoring absolute DW_AT_sibling"));
9004 else
9005 {
9006 sect_offset off = attr.get_ref_die_offset ();
9007 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9008
9009 if (sibling_ptr < info_ptr)
9010 complaint (_("DW_AT_sibling points backwards"));
9011 else if (sibling_ptr > reader->buffer_end)
9012 reader->die_section->overflow_complaint ();
9013 else
9014 return sibling_ptr;
9015 }
9016 }
9017
9018 /* If it isn't DW_AT_sibling, skip this attribute. */
9019 form = abbrev->attrs[i].form;
9020 skip_attribute:
9021 switch (form)
9022 {
9023 case DW_FORM_ref_addr:
9024 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9025 and later it is offset sized. */
9026 if (cu->header.version == 2)
9027 info_ptr += cu->header.addr_size;
9028 else
9029 info_ptr += cu->header.offset_size;
9030 break;
9031 case DW_FORM_GNU_ref_alt:
9032 info_ptr += cu->header.offset_size;
9033 break;
9034 case DW_FORM_addr:
9035 info_ptr += cu->header.addr_size;
9036 break;
9037 case DW_FORM_data1:
9038 case DW_FORM_ref1:
9039 case DW_FORM_flag:
9040 case DW_FORM_strx1:
9041 info_ptr += 1;
9042 break;
9043 case DW_FORM_flag_present:
9044 case DW_FORM_implicit_const:
9045 break;
9046 case DW_FORM_data2:
9047 case DW_FORM_ref2:
9048 case DW_FORM_strx2:
9049 info_ptr += 2;
9050 break;
9051 case DW_FORM_strx3:
9052 info_ptr += 3;
9053 break;
9054 case DW_FORM_data4:
9055 case DW_FORM_ref4:
9056 case DW_FORM_strx4:
9057 info_ptr += 4;
9058 break;
9059 case DW_FORM_data8:
9060 case DW_FORM_ref8:
9061 case DW_FORM_ref_sig8:
9062 info_ptr += 8;
9063 break;
9064 case DW_FORM_data16:
9065 info_ptr += 16;
9066 break;
9067 case DW_FORM_string:
9068 read_direct_string (abfd, info_ptr, &bytes_read);
9069 info_ptr += bytes_read;
9070 break;
9071 case DW_FORM_sec_offset:
9072 case DW_FORM_strp:
9073 case DW_FORM_GNU_strp_alt:
9074 info_ptr += cu->header.offset_size;
9075 break;
9076 case DW_FORM_exprloc:
9077 case DW_FORM_block:
9078 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9079 info_ptr += bytes_read;
9080 break;
9081 case DW_FORM_block1:
9082 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9083 break;
9084 case DW_FORM_block2:
9085 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9086 break;
9087 case DW_FORM_block4:
9088 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9089 break;
9090 case DW_FORM_addrx:
9091 case DW_FORM_strx:
9092 case DW_FORM_sdata:
9093 case DW_FORM_udata:
9094 case DW_FORM_ref_udata:
9095 case DW_FORM_GNU_addr_index:
9096 case DW_FORM_GNU_str_index:
9097 case DW_FORM_rnglistx:
9098 case DW_FORM_loclistx:
9099 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9100 break;
9101 case DW_FORM_indirect:
9102 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9103 info_ptr += bytes_read;
9104 /* We need to continue parsing from here, so just go back to
9105 the top. */
9106 goto skip_attribute;
9107
9108 default:
9109 error (_("Dwarf Error: Cannot handle %s "
9110 "in DWARF reader [in module %s]"),
9111 dwarf_form_name (form),
9112 bfd_get_filename (abfd));
9113 }
9114 }
9115
9116 if (abbrev->has_children)
9117 return skip_children (reader, info_ptr);
9118 else
9119 return info_ptr;
9120 }
9121
9122 /* Locate ORIG_PDI's sibling.
9123 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9124
9125 static const gdb_byte *
9126 locate_pdi_sibling (const struct die_reader_specs *reader,
9127 struct partial_die_info *orig_pdi,
9128 const gdb_byte *info_ptr)
9129 {
9130 /* Do we know the sibling already? */
9131
9132 if (orig_pdi->sibling)
9133 return orig_pdi->sibling;
9134
9135 /* Are there any children to deal with? */
9136
9137 if (!orig_pdi->has_children)
9138 return info_ptr;
9139
9140 /* Skip the children the long way. */
9141
9142 return skip_children (reader, info_ptr);
9143 }
9144
9145 /* Expand this partial symbol table into a full symbol table. SELF is
9146 not NULL. */
9147
9148 void
9149 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9150 {
9151 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9152
9153 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9154
9155 /* If this psymtab is constructed from a debug-only objfile, the
9156 has_section_at_zero flag will not necessarily be correct. We
9157 can get the correct value for this flag by looking at the data
9158 associated with the (presumably stripped) associated objfile. */
9159 if (objfile->separate_debug_objfile_backlink)
9160 {
9161 dwarf2_per_objfile *per_objfile_backlink
9162 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9163
9164 per_objfile->per_bfd->has_section_at_zero
9165 = per_objfile_backlink->per_bfd->has_section_at_zero;
9166 }
9167
9168 expand_psymtab (objfile);
9169
9170 process_cu_includes (per_objfile);
9171 }
9172 \f
9173 /* Reading in full CUs. */
9174
9175 /* Add PER_CU to the queue. */
9176
9177 static void
9178 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9179 dwarf2_per_objfile *per_objfile,
9180 enum language pretend_language)
9181 {
9182 per_cu->queued = 1;
9183 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9184 }
9185
9186 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
9187
9188 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9189 dependency.
9190
9191 Return true if maybe_queue_comp_unit requires the caller to load the CU's
9192 DIEs, false otherwise.
9193
9194 Explanation: there is an invariant that if a CU is queued for expansion
9195 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
9196 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
9197 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
9198 are not yet loaded, the the caller must load the CU's DIEs to ensure the
9199 invariant is respected.
9200
9201 The caller is therefore not required to load the CU's DIEs (we return false)
9202 if:
9203
9204 - the CU is already expanded, and therefore does not get enqueued
9205 - the CU gets enqueued for expansion, but its DIEs are already loaded
9206
9207 Note that the caller should not use this function's return value as an
9208 indicator of whether the CU's DIEs are loaded right now, it should check
9209 that by calling `dwarf2_per_objfile::get_cu` instead. */
9210
9211 static int
9212 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9213 dwarf2_per_cu_data *per_cu,
9214 dwarf2_per_objfile *per_objfile,
9215 enum language pretend_language)
9216 {
9217 /* We may arrive here during partial symbol reading, if we need full
9218 DIEs to process an unusual case (e.g. template arguments). Do
9219 not queue PER_CU, just tell our caller to load its DIEs. */
9220 if (per_cu->per_bfd->reading_partial_symbols)
9221 {
9222 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9223
9224 if (cu == NULL || cu->dies == NULL)
9225 return 1;
9226 return 0;
9227 }
9228
9229 /* Mark the dependence relation so that we don't flush PER_CU
9230 too early. */
9231 if (dependent_cu != NULL)
9232 dwarf2_add_dependence (dependent_cu, per_cu);
9233
9234 /* If it's already on the queue, we have nothing to do. */
9235 if (per_cu->queued)
9236 {
9237 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9238 loaded. */
9239 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9240
9241 /* If the CU is queued for expansion, it should not already be
9242 expanded. */
9243 gdb_assert (!per_objfile->symtab_set_p (per_cu));
9244
9245 /* The DIEs are already loaded, the caller doesn't need to do it. */
9246 return 0;
9247 }
9248
9249 bool queued = false;
9250 if (!per_objfile->symtab_set_p (per_cu))
9251 {
9252 /* Add it to the queue. */
9253 queue_comp_unit (per_cu, per_objfile, pretend_language);
9254 queued = true;
9255 }
9256
9257 /* If the compilation unit is already loaded, just mark it as
9258 used. */
9259 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9260 if (cu != nullptr)
9261 cu->last_used = 0;
9262
9263 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
9264 and the DIEs are not already loaded. */
9265 return queued && cu == nullptr;
9266 }
9267
9268 /* Process the queue. */
9269
9270 static void
9271 process_queue (dwarf2_per_objfile *per_objfile)
9272 {
9273 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9274 objfile_name (per_objfile->objfile));
9275
9276 /* The queue starts out with one item, but following a DIE reference
9277 may load a new CU, adding it to the end of the queue. */
9278 while (!per_objfile->per_bfd->queue.empty ())
9279 {
9280 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9281 dwarf2_per_cu_data *per_cu = item.per_cu;
9282
9283 if (!per_objfile->symtab_set_p (per_cu))
9284 {
9285 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9286
9287 /* Skip dummy CUs. */
9288 if (cu != nullptr)
9289 {
9290 unsigned int debug_print_threshold;
9291 char buf[100];
9292
9293 if (per_cu->is_debug_types)
9294 {
9295 struct signatured_type *sig_type =
9296 (struct signatured_type *) per_cu;
9297
9298 sprintf (buf, "TU %s at offset %s",
9299 hex_string (sig_type->signature),
9300 sect_offset_str (per_cu->sect_off));
9301 /* There can be 100s of TUs.
9302 Only print them in verbose mode. */
9303 debug_print_threshold = 2;
9304 }
9305 else
9306 {
9307 sprintf (buf, "CU at offset %s",
9308 sect_offset_str (per_cu->sect_off));
9309 debug_print_threshold = 1;
9310 }
9311
9312 if (dwarf_read_debug >= debug_print_threshold)
9313 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9314
9315 if (per_cu->is_debug_types)
9316 process_full_type_unit (cu, item.pretend_language);
9317 else
9318 process_full_comp_unit (cu, item.pretend_language);
9319
9320 if (dwarf_read_debug >= debug_print_threshold)
9321 dwarf_read_debug_printf ("Done expanding %s", buf);
9322 }
9323 }
9324
9325 per_cu->queued = 0;
9326 per_objfile->per_bfd->queue.pop ();
9327 }
9328
9329 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9330 objfile_name (per_objfile->objfile));
9331 }
9332
9333 /* Read in full symbols for PST, and anything it depends on. */
9334
9335 void
9336 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9337 {
9338 gdb_assert (!readin_p (objfile));
9339
9340 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9341 free_cached_comp_units freer (per_objfile);
9342 expand_dependencies (objfile);
9343
9344 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9345 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9346 }
9347
9348 /* See psympriv.h. */
9349
9350 bool
9351 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9352 {
9353 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9354 return per_objfile->symtab_set_p (per_cu_data);
9355 }
9356
9357 /* See psympriv.h. */
9358
9359 compunit_symtab *
9360 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9361 {
9362 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9363 return per_objfile->get_symtab (per_cu_data);
9364 }
9365
9366 /* Trivial hash function for die_info: the hash value of a DIE
9367 is its offset in .debug_info for this objfile. */
9368
9369 static hashval_t
9370 die_hash (const void *item)
9371 {
9372 const struct die_info *die = (const struct die_info *) item;
9373
9374 return to_underlying (die->sect_off);
9375 }
9376
9377 /* Trivial comparison function for die_info structures: two DIEs
9378 are equal if they have the same offset. */
9379
9380 static int
9381 die_eq (const void *item_lhs, const void *item_rhs)
9382 {
9383 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9384 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9385
9386 return die_lhs->sect_off == die_rhs->sect_off;
9387 }
9388
9389 /* Load the DIEs associated with PER_CU into memory.
9390
9391 In some cases, the caller, while reading partial symbols, will need to load
9392 the full symbols for the CU for some reason. It will already have a
9393 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9394 rather than creating a new one. */
9395
9396 static void
9397 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9398 dwarf2_per_objfile *per_objfile,
9399 dwarf2_cu *existing_cu,
9400 bool skip_partial,
9401 enum language pretend_language)
9402 {
9403 gdb_assert (! this_cu->is_debug_types);
9404
9405 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9406 if (reader.dummy_p)
9407 return;
9408
9409 struct dwarf2_cu *cu = reader.cu;
9410 const gdb_byte *info_ptr = reader.info_ptr;
9411
9412 gdb_assert (cu->die_hash == NULL);
9413 cu->die_hash =
9414 htab_create_alloc_ex (cu->header.length / 12,
9415 die_hash,
9416 die_eq,
9417 NULL,
9418 &cu->comp_unit_obstack,
9419 hashtab_obstack_allocate,
9420 dummy_obstack_deallocate);
9421
9422 if (reader.comp_unit_die->has_children)
9423 reader.comp_unit_die->child
9424 = read_die_and_siblings (&reader, reader.info_ptr,
9425 &info_ptr, reader.comp_unit_die);
9426 cu->dies = reader.comp_unit_die;
9427 /* comp_unit_die is not stored in die_hash, no need. */
9428
9429 /* We try not to read any attributes in this function, because not
9430 all CUs needed for references have been loaded yet, and symbol
9431 table processing isn't initialized. But we have to set the CU language,
9432 or we won't be able to build types correctly.
9433 Similarly, if we do not read the producer, we can not apply
9434 producer-specific interpretation. */
9435 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9436
9437 reader.keep ();
9438 }
9439
9440 /* Add a DIE to the delayed physname list. */
9441
9442 static void
9443 add_to_method_list (struct type *type, int fnfield_index, int index,
9444 const char *name, struct die_info *die,
9445 struct dwarf2_cu *cu)
9446 {
9447 struct delayed_method_info mi;
9448 mi.type = type;
9449 mi.fnfield_index = fnfield_index;
9450 mi.index = index;
9451 mi.name = name;
9452 mi.die = die;
9453 cu->method_list.push_back (mi);
9454 }
9455
9456 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9457 "const" / "volatile". If so, decrements LEN by the length of the
9458 modifier and return true. Otherwise return false. */
9459
9460 template<size_t N>
9461 static bool
9462 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9463 {
9464 size_t mod_len = sizeof (mod) - 1;
9465 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9466 {
9467 len -= mod_len;
9468 return true;
9469 }
9470 return false;
9471 }
9472
9473 /* Compute the physnames of any methods on the CU's method list.
9474
9475 The computation of method physnames is delayed in order to avoid the
9476 (bad) condition that one of the method's formal parameters is of an as yet
9477 incomplete type. */
9478
9479 static void
9480 compute_delayed_physnames (struct dwarf2_cu *cu)
9481 {
9482 /* Only C++ delays computing physnames. */
9483 if (cu->method_list.empty ())
9484 return;
9485 gdb_assert (cu->language == language_cplus);
9486
9487 for (const delayed_method_info &mi : cu->method_list)
9488 {
9489 const char *physname;
9490 struct fn_fieldlist *fn_flp
9491 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9492 physname = dwarf2_physname (mi.name, mi.die, cu);
9493 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9494 = physname ? physname : "";
9495
9496 /* Since there's no tag to indicate whether a method is a
9497 const/volatile overload, extract that information out of the
9498 demangled name. */
9499 if (physname != NULL)
9500 {
9501 size_t len = strlen (physname);
9502
9503 while (1)
9504 {
9505 if (physname[len] == ')') /* shortcut */
9506 break;
9507 else if (check_modifier (physname, len, " const"))
9508 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9509 else if (check_modifier (physname, len, " volatile"))
9510 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9511 else
9512 break;
9513 }
9514 }
9515 }
9516
9517 /* The list is no longer needed. */
9518 cu->method_list.clear ();
9519 }
9520
9521 /* Go objects should be embedded in a DW_TAG_module DIE,
9522 and it's not clear if/how imported objects will appear.
9523 To keep Go support simple until that's worked out,
9524 go back through what we've read and create something usable.
9525 We could do this while processing each DIE, and feels kinda cleaner,
9526 but that way is more invasive.
9527 This is to, for example, allow the user to type "p var" or "b main"
9528 without having to specify the package name, and allow lookups
9529 of module.object to work in contexts that use the expression
9530 parser. */
9531
9532 static void
9533 fixup_go_packaging (struct dwarf2_cu *cu)
9534 {
9535 gdb::unique_xmalloc_ptr<char> package_name;
9536 struct pending *list;
9537 int i;
9538
9539 for (list = *cu->get_builder ()->get_global_symbols ();
9540 list != NULL;
9541 list = list->next)
9542 {
9543 for (i = 0; i < list->nsyms; ++i)
9544 {
9545 struct symbol *sym = list->symbol[i];
9546
9547 if (sym->language () == language_go
9548 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9549 {
9550 gdb::unique_xmalloc_ptr<char> this_package_name
9551 (go_symbol_package_name (sym));
9552
9553 if (this_package_name == NULL)
9554 continue;
9555 if (package_name == NULL)
9556 package_name = std::move (this_package_name);
9557 else
9558 {
9559 struct objfile *objfile = cu->per_objfile->objfile;
9560 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9561 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9562 (symbol_symtab (sym) != NULL
9563 ? symtab_to_filename_for_display
9564 (symbol_symtab (sym))
9565 : objfile_name (objfile)),
9566 this_package_name.get (), package_name.get ());
9567 }
9568 }
9569 }
9570 }
9571
9572 if (package_name != NULL)
9573 {
9574 struct objfile *objfile = cu->per_objfile->objfile;
9575 const char *saved_package_name = objfile->intern (package_name.get ());
9576 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9577 saved_package_name);
9578 struct symbol *sym;
9579
9580 sym = new (&objfile->objfile_obstack) symbol;
9581 sym->set_language (language_go, &objfile->objfile_obstack);
9582 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9583 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9584 e.g., "main" finds the "main" module and not C's main(). */
9585 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9586 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9587 SYMBOL_TYPE (sym) = type;
9588
9589 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9590 }
9591 }
9592
9593 /* Allocate a fully-qualified name consisting of the two parts on the
9594 obstack. */
9595
9596 static const char *
9597 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9598 {
9599 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9600 }
9601
9602 /* A helper that allocates a variant part to attach to a Rust enum
9603 type. OBSTACK is where the results should be allocated. TYPE is
9604 the type we're processing. DISCRIMINANT_INDEX is the index of the
9605 discriminant. It must be the index of one of the fields of TYPE,
9606 or -1 to mean there is no discriminant (univariant enum).
9607 DEFAULT_INDEX is the index of the default field; or -1 if there is
9608 no default. RANGES is indexed by "effective" field number (the
9609 field index, but omitting the discriminant and default fields) and
9610 must hold the discriminant values used by the variants. Note that
9611 RANGES must have a lifetime at least as long as OBSTACK -- either
9612 already allocated on it, or static. */
9613
9614 static void
9615 alloc_rust_variant (struct obstack *obstack, struct type *type,
9616 int discriminant_index, int default_index,
9617 gdb::array_view<discriminant_range> ranges)
9618 {
9619 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9620 gdb_assert (discriminant_index == -1
9621 || (discriminant_index >= 0
9622 && discriminant_index < type->num_fields ()));
9623 gdb_assert (default_index == -1
9624 || (default_index >= 0 && default_index < type->num_fields ()));
9625
9626 /* We have one variant for each non-discriminant field. */
9627 int n_variants = type->num_fields ();
9628 if (discriminant_index != -1)
9629 --n_variants;
9630
9631 variant *variants = new (obstack) variant[n_variants];
9632 int var_idx = 0;
9633 int range_idx = 0;
9634 for (int i = 0; i < type->num_fields (); ++i)
9635 {
9636 if (i == discriminant_index)
9637 continue;
9638
9639 variants[var_idx].first_field = i;
9640 variants[var_idx].last_field = i + 1;
9641
9642 /* The default field does not need a range, but other fields do.
9643 We skipped the discriminant above. */
9644 if (i != default_index)
9645 {
9646 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9647 ++range_idx;
9648 }
9649
9650 ++var_idx;
9651 }
9652
9653 gdb_assert (range_idx == ranges.size ());
9654 gdb_assert (var_idx == n_variants);
9655
9656 variant_part *part = new (obstack) variant_part;
9657 part->discriminant_index = discriminant_index;
9658 /* If there is no discriminant, then whether it is signed is of no
9659 consequence. */
9660 part->is_unsigned
9661 = (discriminant_index == -1
9662 ? false
9663 : type->field (discriminant_index).type ()->is_unsigned ());
9664 part->variants = gdb::array_view<variant> (variants, n_variants);
9665
9666 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9667 gdb::array_view<variant_part> *prop_value
9668 = new (storage) gdb::array_view<variant_part> (part, 1);
9669
9670 struct dynamic_prop prop;
9671 prop.set_variant_parts (prop_value);
9672
9673 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9674 }
9675
9676 /* Some versions of rustc emitted enums in an unusual way.
9677
9678 Ordinary enums were emitted as unions. The first element of each
9679 structure in the union was named "RUST$ENUM$DISR". This element
9680 held the discriminant.
9681
9682 These versions of Rust also implemented the "non-zero"
9683 optimization. When the enum had two values, and one is empty and
9684 the other holds a pointer that cannot be zero, the pointer is used
9685 as the discriminant, with a zero value meaning the empty variant.
9686 Here, the union's first member is of the form
9687 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9688 where the fieldnos are the indices of the fields that should be
9689 traversed in order to find the field (which may be several fields deep)
9690 and the variantname is the name of the variant of the case when the
9691 field is zero.
9692
9693 This function recognizes whether TYPE is of one of these forms,
9694 and, if so, smashes it to be a variant type. */
9695
9696 static void
9697 quirk_rust_enum (struct type *type, struct objfile *objfile)
9698 {
9699 gdb_assert (type->code () == TYPE_CODE_UNION);
9700
9701 /* We don't need to deal with empty enums. */
9702 if (type->num_fields () == 0)
9703 return;
9704
9705 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9706 if (type->num_fields () == 1
9707 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9708 {
9709 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9710
9711 /* Decode the field name to find the offset of the
9712 discriminant. */
9713 ULONGEST bit_offset = 0;
9714 struct type *field_type = type->field (0).type ();
9715 while (name[0] >= '0' && name[0] <= '9')
9716 {
9717 char *tail;
9718 unsigned long index = strtoul (name, &tail, 10);
9719 name = tail;
9720 if (*name != '$'
9721 || index >= field_type->num_fields ()
9722 || (TYPE_FIELD_LOC_KIND (field_type, index)
9723 != FIELD_LOC_KIND_BITPOS))
9724 {
9725 complaint (_("Could not parse Rust enum encoding string \"%s\""
9726 "[in module %s]"),
9727 TYPE_FIELD_NAME (type, 0),
9728 objfile_name (objfile));
9729 return;
9730 }
9731 ++name;
9732
9733 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9734 field_type = field_type->field (index).type ();
9735 }
9736
9737 /* Smash this type to be a structure type. We have to do this
9738 because the type has already been recorded. */
9739 type->set_code (TYPE_CODE_STRUCT);
9740 type->set_num_fields (3);
9741 /* Save the field we care about. */
9742 struct field saved_field = type->field (0);
9743 type->set_fields
9744 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9745
9746 /* Put the discriminant at index 0. */
9747 type->field (0).set_type (field_type);
9748 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9749 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9750 SET_FIELD_BITPOS (type->field (0), bit_offset);
9751
9752 /* The order of fields doesn't really matter, so put the real
9753 field at index 1 and the data-less field at index 2. */
9754 type->field (1) = saved_field;
9755 TYPE_FIELD_NAME (type, 1)
9756 = rust_last_path_segment (type->field (1).type ()->name ());
9757 type->field (1).type ()->set_name
9758 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9759 TYPE_FIELD_NAME (type, 1)));
9760
9761 const char *dataless_name
9762 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9763 name);
9764 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9765 dataless_name);
9766 type->field (2).set_type (dataless_type);
9767 /* NAME points into the original discriminant name, which
9768 already has the correct lifetime. */
9769 TYPE_FIELD_NAME (type, 2) = name;
9770 SET_FIELD_BITPOS (type->field (2), 0);
9771
9772 /* Indicate that this is a variant type. */
9773 static discriminant_range ranges[1] = { { 0, 0 } };
9774 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9775 }
9776 /* A union with a single anonymous field is probably an old-style
9777 univariant enum. */
9778 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9779 {
9780 /* Smash this type to be a structure type. We have to do this
9781 because the type has already been recorded. */
9782 type->set_code (TYPE_CODE_STRUCT);
9783
9784 struct type *field_type = type->field (0).type ();
9785 const char *variant_name
9786 = rust_last_path_segment (field_type->name ());
9787 TYPE_FIELD_NAME (type, 0) = variant_name;
9788 field_type->set_name
9789 (rust_fully_qualify (&objfile->objfile_obstack,
9790 type->name (), variant_name));
9791
9792 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9793 }
9794 else
9795 {
9796 struct type *disr_type = nullptr;
9797 for (int i = 0; i < type->num_fields (); ++i)
9798 {
9799 disr_type = type->field (i).type ();
9800
9801 if (disr_type->code () != TYPE_CODE_STRUCT)
9802 {
9803 /* All fields of a true enum will be structs. */
9804 return;
9805 }
9806 else if (disr_type->num_fields () == 0)
9807 {
9808 /* Could be data-less variant, so keep going. */
9809 disr_type = nullptr;
9810 }
9811 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9812 "RUST$ENUM$DISR") != 0)
9813 {
9814 /* Not a Rust enum. */
9815 return;
9816 }
9817 else
9818 {
9819 /* Found one. */
9820 break;
9821 }
9822 }
9823
9824 /* If we got here without a discriminant, then it's probably
9825 just a union. */
9826 if (disr_type == nullptr)
9827 return;
9828
9829 /* Smash this type to be a structure type. We have to do this
9830 because the type has already been recorded. */
9831 type->set_code (TYPE_CODE_STRUCT);
9832
9833 /* Make space for the discriminant field. */
9834 struct field *disr_field = &disr_type->field (0);
9835 field *new_fields
9836 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9837 * sizeof (struct field)));
9838 memcpy (new_fields + 1, type->fields (),
9839 type->num_fields () * sizeof (struct field));
9840 type->set_fields (new_fields);
9841 type->set_num_fields (type->num_fields () + 1);
9842
9843 /* Install the discriminant at index 0 in the union. */
9844 type->field (0) = *disr_field;
9845 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9846 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9847
9848 /* We need a way to find the correct discriminant given a
9849 variant name. For convenience we build a map here. */
9850 struct type *enum_type = disr_field->type ();
9851 std::unordered_map<std::string, ULONGEST> discriminant_map;
9852 for (int i = 0; i < enum_type->num_fields (); ++i)
9853 {
9854 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9855 {
9856 const char *name
9857 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9858 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9859 }
9860 }
9861
9862 int n_fields = type->num_fields ();
9863 /* We don't need a range entry for the discriminant, but we do
9864 need one for every other field, as there is no default
9865 variant. */
9866 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9867 discriminant_range,
9868 n_fields - 1);
9869 /* Skip the discriminant here. */
9870 for (int i = 1; i < n_fields; ++i)
9871 {
9872 /* Find the final word in the name of this variant's type.
9873 That name can be used to look up the correct
9874 discriminant. */
9875 const char *variant_name
9876 = rust_last_path_segment (type->field (i).type ()->name ());
9877
9878 auto iter = discriminant_map.find (variant_name);
9879 if (iter != discriminant_map.end ())
9880 {
9881 ranges[i - 1].low = iter->second;
9882 ranges[i - 1].high = iter->second;
9883 }
9884
9885 /* In Rust, each element should have the size of the
9886 enclosing enum. */
9887 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9888
9889 /* Remove the discriminant field, if it exists. */
9890 struct type *sub_type = type->field (i).type ();
9891 if (sub_type->num_fields () > 0)
9892 {
9893 sub_type->set_num_fields (sub_type->num_fields () - 1);
9894 sub_type->set_fields (sub_type->fields () + 1);
9895 }
9896 TYPE_FIELD_NAME (type, i) = variant_name;
9897 sub_type->set_name
9898 (rust_fully_qualify (&objfile->objfile_obstack,
9899 type->name (), variant_name));
9900 }
9901
9902 /* Indicate that this is a variant type. */
9903 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9904 gdb::array_view<discriminant_range> (ranges,
9905 n_fields - 1));
9906 }
9907 }
9908
9909 /* Rewrite some Rust unions to be structures with variants parts. */
9910
9911 static void
9912 rust_union_quirks (struct dwarf2_cu *cu)
9913 {
9914 gdb_assert (cu->language == language_rust);
9915 for (type *type_ : cu->rust_unions)
9916 quirk_rust_enum (type_, cu->per_objfile->objfile);
9917 /* We don't need this any more. */
9918 cu->rust_unions.clear ();
9919 }
9920
9921 /* See read.h. */
9922
9923 type_unit_group_unshareable *
9924 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9925 {
9926 auto iter = this->m_type_units.find (tu_group);
9927 if (iter != this->m_type_units.end ())
9928 return iter->second.get ();
9929
9930 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9931 type_unit_group_unshareable *result = uniq.get ();
9932 this->m_type_units[tu_group] = std::move (uniq);
9933 return result;
9934 }
9935
9936 struct type *
9937 dwarf2_per_objfile::get_type_for_signatured_type
9938 (signatured_type *sig_type) const
9939 {
9940 auto iter = this->m_type_map.find (sig_type);
9941 if (iter == this->m_type_map.end ())
9942 return nullptr;
9943
9944 return iter->second;
9945 }
9946
9947 void dwarf2_per_objfile::set_type_for_signatured_type
9948 (signatured_type *sig_type, struct type *type)
9949 {
9950 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9951
9952 this->m_type_map[sig_type] = type;
9953 }
9954
9955 /* A helper function for computing the list of all symbol tables
9956 included by PER_CU. */
9957
9958 static void
9959 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9960 htab_t all_children, htab_t all_type_symtabs,
9961 dwarf2_per_cu_data *per_cu,
9962 dwarf2_per_objfile *per_objfile,
9963 struct compunit_symtab *immediate_parent)
9964 {
9965 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9966 if (*slot != NULL)
9967 {
9968 /* This inclusion and its children have been processed. */
9969 return;
9970 }
9971
9972 *slot = per_cu;
9973
9974 /* Only add a CU if it has a symbol table. */
9975 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9976 if (cust != NULL)
9977 {
9978 /* If this is a type unit only add its symbol table if we haven't
9979 seen it yet (type unit per_cu's can share symtabs). */
9980 if (per_cu->is_debug_types)
9981 {
9982 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9983 if (*slot == NULL)
9984 {
9985 *slot = cust;
9986 result->push_back (cust);
9987 if (cust->user == NULL)
9988 cust->user = immediate_parent;
9989 }
9990 }
9991 else
9992 {
9993 result->push_back (cust);
9994 if (cust->user == NULL)
9995 cust->user = immediate_parent;
9996 }
9997 }
9998
9999 if (!per_cu->imported_symtabs_empty ())
10000 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10001 {
10002 recursively_compute_inclusions (result, all_children,
10003 all_type_symtabs, ptr, per_objfile,
10004 cust);
10005 }
10006 }
10007
10008 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
10009 PER_CU. */
10010
10011 static void
10012 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
10013 dwarf2_per_objfile *per_objfile)
10014 {
10015 gdb_assert (! per_cu->is_debug_types);
10016
10017 if (!per_cu->imported_symtabs_empty ())
10018 {
10019 int len;
10020 std::vector<compunit_symtab *> result_symtabs;
10021 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
10022
10023 /* If we don't have a symtab, we can just skip this case. */
10024 if (cust == NULL)
10025 return;
10026
10027 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
10028 htab_eq_pointer,
10029 NULL, xcalloc, xfree));
10030 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
10031 htab_eq_pointer,
10032 NULL, xcalloc, xfree));
10033
10034 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10035 {
10036 recursively_compute_inclusions (&result_symtabs, all_children.get (),
10037 all_type_symtabs.get (), ptr,
10038 per_objfile, cust);
10039 }
10040
10041 /* Now we have a transitive closure of all the included symtabs. */
10042 len = result_symtabs.size ();
10043 cust->includes
10044 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
10045 struct compunit_symtab *, len + 1);
10046 memcpy (cust->includes, result_symtabs.data (),
10047 len * sizeof (compunit_symtab *));
10048 cust->includes[len] = NULL;
10049 }
10050 }
10051
10052 /* Compute the 'includes' field for the symtabs of all the CUs we just
10053 read. */
10054
10055 static void
10056 process_cu_includes (dwarf2_per_objfile *per_objfile)
10057 {
10058 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
10059 {
10060 if (! iter->is_debug_types)
10061 compute_compunit_symtab_includes (iter, per_objfile);
10062 }
10063
10064 per_objfile->per_bfd->just_read_cus.clear ();
10065 }
10066
10067 /* Generate full symbol information for CU, whose DIEs have
10068 already been loaded into memory. */
10069
10070 static void
10071 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10072 {
10073 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10074 struct objfile *objfile = per_objfile->objfile;
10075 struct gdbarch *gdbarch = objfile->arch ();
10076 CORE_ADDR lowpc, highpc;
10077 struct compunit_symtab *cust;
10078 CORE_ADDR baseaddr;
10079 struct block *static_block;
10080 CORE_ADDR addr;
10081
10082 baseaddr = objfile->text_section_offset ();
10083
10084 /* Clear the list here in case something was left over. */
10085 cu->method_list.clear ();
10086
10087 cu->language = pretend_language;
10088 cu->language_defn = language_def (cu->language);
10089
10090 dwarf2_find_base_address (cu->dies, cu);
10091
10092 /* Do line number decoding in read_file_scope () */
10093 process_die (cu->dies, cu);
10094
10095 /* For now fudge the Go package. */
10096 if (cu->language == language_go)
10097 fixup_go_packaging (cu);
10098
10099 /* Now that we have processed all the DIEs in the CU, all the types
10100 should be complete, and it should now be safe to compute all of the
10101 physnames. */
10102 compute_delayed_physnames (cu);
10103
10104 if (cu->language == language_rust)
10105 rust_union_quirks (cu);
10106
10107 /* Some compilers don't define a DW_AT_high_pc attribute for the
10108 compilation unit. If the DW_AT_high_pc is missing, synthesize
10109 it, by scanning the DIE's below the compilation unit. */
10110 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10111
10112 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10113 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10114
10115 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10116 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10117 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10118 addrmap to help ensure it has an accurate map of pc values belonging to
10119 this comp unit. */
10120 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10121
10122 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10123 SECT_OFF_TEXT (objfile),
10124 0);
10125
10126 if (cust != NULL)
10127 {
10128 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10129
10130 /* Set symtab language to language from DW_AT_language. If the
10131 compilation is from a C file generated by language preprocessors, do
10132 not set the language if it was already deduced by start_subfile. */
10133 if (!(cu->language == language_c
10134 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10135 COMPUNIT_FILETABS (cust)->language = cu->language;
10136
10137 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10138 produce DW_AT_location with location lists but it can be possibly
10139 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10140 there were bugs in prologue debug info, fixed later in GCC-4.5
10141 by "unwind info for epilogues" patch (which is not directly related).
10142
10143 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10144 needed, it would be wrong due to missing DW_AT_producer there.
10145
10146 Still one can confuse GDB by using non-standard GCC compilation
10147 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10148 */
10149 if (cu->has_loclist && gcc_4_minor >= 5)
10150 cust->locations_valid = 1;
10151
10152 if (gcc_4_minor >= 5)
10153 cust->epilogue_unwind_valid = 1;
10154
10155 cust->call_site_htab = cu->call_site_htab;
10156 }
10157
10158 per_objfile->set_symtab (cu->per_cu, cust);
10159
10160 /* Push it for inclusion processing later. */
10161 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10162
10163 /* Not needed any more. */
10164 cu->reset_builder ();
10165 }
10166
10167 /* Generate full symbol information for type unit CU, whose DIEs have
10168 already been loaded into memory. */
10169
10170 static void
10171 process_full_type_unit (dwarf2_cu *cu,
10172 enum language pretend_language)
10173 {
10174 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10175 struct objfile *objfile = per_objfile->objfile;
10176 struct compunit_symtab *cust;
10177 struct signatured_type *sig_type;
10178
10179 gdb_assert (cu->per_cu->is_debug_types);
10180 sig_type = (struct signatured_type *) cu->per_cu;
10181
10182 /* Clear the list here in case something was left over. */
10183 cu->method_list.clear ();
10184
10185 cu->language = pretend_language;
10186 cu->language_defn = language_def (cu->language);
10187
10188 /* The symbol tables are set up in read_type_unit_scope. */
10189 process_die (cu->dies, cu);
10190
10191 /* For now fudge the Go package. */
10192 if (cu->language == language_go)
10193 fixup_go_packaging (cu);
10194
10195 /* Now that we have processed all the DIEs in the CU, all the types
10196 should be complete, and it should now be safe to compute all of the
10197 physnames. */
10198 compute_delayed_physnames (cu);
10199
10200 if (cu->language == language_rust)
10201 rust_union_quirks (cu);
10202
10203 /* TUs share symbol tables.
10204 If this is the first TU to use this symtab, complete the construction
10205 of it with end_expandable_symtab. Otherwise, complete the addition of
10206 this TU's symbols to the existing symtab. */
10207 type_unit_group_unshareable *tug_unshare =
10208 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10209 if (tug_unshare->compunit_symtab == NULL)
10210 {
10211 buildsym_compunit *builder = cu->get_builder ();
10212 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10213 tug_unshare->compunit_symtab = cust;
10214
10215 if (cust != NULL)
10216 {
10217 /* Set symtab language to language from DW_AT_language. If the
10218 compilation is from a C file generated by language preprocessors,
10219 do not set the language if it was already deduced by
10220 start_subfile. */
10221 if (!(cu->language == language_c
10222 && COMPUNIT_FILETABS (cust)->language != language_c))
10223 COMPUNIT_FILETABS (cust)->language = cu->language;
10224 }
10225 }
10226 else
10227 {
10228 cu->get_builder ()->augment_type_symtab ();
10229 cust = tug_unshare->compunit_symtab;
10230 }
10231
10232 per_objfile->set_symtab (cu->per_cu, cust);
10233
10234 /* Not needed any more. */
10235 cu->reset_builder ();
10236 }
10237
10238 /* Process an imported unit DIE. */
10239
10240 static void
10241 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10242 {
10243 struct attribute *attr;
10244
10245 /* For now we don't handle imported units in type units. */
10246 if (cu->per_cu->is_debug_types)
10247 {
10248 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10249 " supported in type units [in module %s]"),
10250 objfile_name (cu->per_objfile->objfile));
10251 }
10252
10253 attr = dwarf2_attr (die, DW_AT_import, cu);
10254 if (attr != NULL)
10255 {
10256 sect_offset sect_off = attr->get_ref_die_offset ();
10257 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10258 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10259 dwarf2_per_cu_data *per_cu
10260 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10261
10262 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10263 into another compilation unit, at root level. Regard this as a hint,
10264 and ignore it. */
10265 if (die->parent && die->parent->parent == NULL
10266 && per_cu->unit_type == DW_UT_compile
10267 && per_cu->lang == language_cplus)
10268 return;
10269
10270 /* If necessary, add it to the queue and load its DIEs. */
10271 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10272 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10273 false, cu->language);
10274
10275 cu->per_cu->imported_symtabs_push (per_cu);
10276 }
10277 }
10278
10279 /* RAII object that represents a process_die scope: i.e.,
10280 starts/finishes processing a DIE. */
10281 class process_die_scope
10282 {
10283 public:
10284 process_die_scope (die_info *die, dwarf2_cu *cu)
10285 : m_die (die), m_cu (cu)
10286 {
10287 /* We should only be processing DIEs not already in process. */
10288 gdb_assert (!m_die->in_process);
10289 m_die->in_process = true;
10290 }
10291
10292 ~process_die_scope ()
10293 {
10294 m_die->in_process = false;
10295
10296 /* If we're done processing the DIE for the CU that owns the line
10297 header, we don't need the line header anymore. */
10298 if (m_cu->line_header_die_owner == m_die)
10299 {
10300 delete m_cu->line_header;
10301 m_cu->line_header = NULL;
10302 m_cu->line_header_die_owner = NULL;
10303 }
10304 }
10305
10306 private:
10307 die_info *m_die;
10308 dwarf2_cu *m_cu;
10309 };
10310
10311 /* Process a die and its children. */
10312
10313 static void
10314 process_die (struct die_info *die, struct dwarf2_cu *cu)
10315 {
10316 process_die_scope scope (die, cu);
10317
10318 switch (die->tag)
10319 {
10320 case DW_TAG_padding:
10321 break;
10322 case DW_TAG_compile_unit:
10323 case DW_TAG_partial_unit:
10324 read_file_scope (die, cu);
10325 break;
10326 case DW_TAG_type_unit:
10327 read_type_unit_scope (die, cu);
10328 break;
10329 case DW_TAG_subprogram:
10330 /* Nested subprograms in Fortran get a prefix. */
10331 if (cu->language == language_fortran
10332 && die->parent != NULL
10333 && die->parent->tag == DW_TAG_subprogram)
10334 cu->processing_has_namespace_info = true;
10335 /* Fall through. */
10336 case DW_TAG_inlined_subroutine:
10337 read_func_scope (die, cu);
10338 break;
10339 case DW_TAG_lexical_block:
10340 case DW_TAG_try_block:
10341 case DW_TAG_catch_block:
10342 read_lexical_block_scope (die, cu);
10343 break;
10344 case DW_TAG_call_site:
10345 case DW_TAG_GNU_call_site:
10346 read_call_site_scope (die, cu);
10347 break;
10348 case DW_TAG_class_type:
10349 case DW_TAG_interface_type:
10350 case DW_TAG_structure_type:
10351 case DW_TAG_union_type:
10352 process_structure_scope (die, cu);
10353 break;
10354 case DW_TAG_enumeration_type:
10355 process_enumeration_scope (die, cu);
10356 break;
10357
10358 /* These dies have a type, but processing them does not create
10359 a symbol or recurse to process the children. Therefore we can
10360 read them on-demand through read_type_die. */
10361 case DW_TAG_subroutine_type:
10362 case DW_TAG_set_type:
10363 case DW_TAG_pointer_type:
10364 case DW_TAG_ptr_to_member_type:
10365 case DW_TAG_reference_type:
10366 case DW_TAG_rvalue_reference_type:
10367 case DW_TAG_string_type:
10368 break;
10369
10370 case DW_TAG_array_type:
10371 /* We only need to handle this case for Ada -- in other
10372 languages, it's normal for the compiler to emit a typedef
10373 instead. */
10374 if (cu->language != language_ada)
10375 break;
10376 /* FALLTHROUGH */
10377 case DW_TAG_base_type:
10378 case DW_TAG_subrange_type:
10379 case DW_TAG_typedef:
10380 /* Add a typedef symbol for the type definition, if it has a
10381 DW_AT_name. */
10382 new_symbol (die, read_type_die (die, cu), cu);
10383 break;
10384 case DW_TAG_common_block:
10385 read_common_block (die, cu);
10386 break;
10387 case DW_TAG_common_inclusion:
10388 break;
10389 case DW_TAG_namespace:
10390 cu->processing_has_namespace_info = true;
10391 read_namespace (die, cu);
10392 break;
10393 case DW_TAG_module:
10394 cu->processing_has_namespace_info = true;
10395 read_module (die, cu);
10396 break;
10397 case DW_TAG_imported_declaration:
10398 cu->processing_has_namespace_info = true;
10399 if (read_namespace_alias (die, cu))
10400 break;
10401 /* The declaration is not a global namespace alias. */
10402 /* Fall through. */
10403 case DW_TAG_imported_module:
10404 cu->processing_has_namespace_info = true;
10405 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10406 || cu->language != language_fortran))
10407 complaint (_("Tag '%s' has unexpected children"),
10408 dwarf_tag_name (die->tag));
10409 read_import_statement (die, cu);
10410 break;
10411
10412 case DW_TAG_imported_unit:
10413 process_imported_unit_die (die, cu);
10414 break;
10415
10416 case DW_TAG_variable:
10417 read_variable (die, cu);
10418 break;
10419
10420 default:
10421 new_symbol (die, NULL, cu);
10422 break;
10423 }
10424 }
10425 \f
10426 /* DWARF name computation. */
10427
10428 /* A helper function for dwarf2_compute_name which determines whether DIE
10429 needs to have the name of the scope prepended to the name listed in the
10430 die. */
10431
10432 static int
10433 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10434 {
10435 struct attribute *attr;
10436
10437 switch (die->tag)
10438 {
10439 case DW_TAG_namespace:
10440 case DW_TAG_typedef:
10441 case DW_TAG_class_type:
10442 case DW_TAG_interface_type:
10443 case DW_TAG_structure_type:
10444 case DW_TAG_union_type:
10445 case DW_TAG_enumeration_type:
10446 case DW_TAG_enumerator:
10447 case DW_TAG_subprogram:
10448 case DW_TAG_inlined_subroutine:
10449 case DW_TAG_member:
10450 case DW_TAG_imported_declaration:
10451 return 1;
10452
10453 case DW_TAG_variable:
10454 case DW_TAG_constant:
10455 /* We only need to prefix "globally" visible variables. These include
10456 any variable marked with DW_AT_external or any variable that
10457 lives in a namespace. [Variables in anonymous namespaces
10458 require prefixing, but they are not DW_AT_external.] */
10459
10460 if (dwarf2_attr (die, DW_AT_specification, cu))
10461 {
10462 struct dwarf2_cu *spec_cu = cu;
10463
10464 return die_needs_namespace (die_specification (die, &spec_cu),
10465 spec_cu);
10466 }
10467
10468 attr = dwarf2_attr (die, DW_AT_external, cu);
10469 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10470 && die->parent->tag != DW_TAG_module)
10471 return 0;
10472 /* A variable in a lexical block of some kind does not need a
10473 namespace, even though in C++ such variables may be external
10474 and have a mangled name. */
10475 if (die->parent->tag == DW_TAG_lexical_block
10476 || die->parent->tag == DW_TAG_try_block
10477 || die->parent->tag == DW_TAG_catch_block
10478 || die->parent->tag == DW_TAG_subprogram)
10479 return 0;
10480 return 1;
10481
10482 default:
10483 return 0;
10484 }
10485 }
10486
10487 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10488 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10489 defined for the given DIE. */
10490
10491 static struct attribute *
10492 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10493 {
10494 struct attribute *attr;
10495
10496 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10497 if (attr == NULL)
10498 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10499
10500 return attr;
10501 }
10502
10503 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10504 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10505 defined for the given DIE. */
10506
10507 static const char *
10508 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10509 {
10510 const char *linkage_name;
10511
10512 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10513 if (linkage_name == NULL)
10514 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10515
10516 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10517 See https://github.com/rust-lang/rust/issues/32925. */
10518 if (cu->language == language_rust && linkage_name != NULL
10519 && strchr (linkage_name, '{') != NULL)
10520 linkage_name = NULL;
10521
10522 return linkage_name;
10523 }
10524
10525 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10526 compute the physname for the object, which include a method's:
10527 - formal parameters (C++),
10528 - receiver type (Go),
10529
10530 The term "physname" is a bit confusing.
10531 For C++, for example, it is the demangled name.
10532 For Go, for example, it's the mangled name.
10533
10534 For Ada, return the DIE's linkage name rather than the fully qualified
10535 name. PHYSNAME is ignored..
10536
10537 The result is allocated on the objfile->per_bfd's obstack and
10538 canonicalized. */
10539
10540 static const char *
10541 dwarf2_compute_name (const char *name,
10542 struct die_info *die, struct dwarf2_cu *cu,
10543 int physname)
10544 {
10545 struct objfile *objfile = cu->per_objfile->objfile;
10546
10547 if (name == NULL)
10548 name = dwarf2_name (die, cu);
10549
10550 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10551 but otherwise compute it by typename_concat inside GDB.
10552 FIXME: Actually this is not really true, or at least not always true.
10553 It's all very confusing. compute_and_set_names doesn't try to demangle
10554 Fortran names because there is no mangling standard. So new_symbol
10555 will set the demangled name to the result of dwarf2_full_name, and it is
10556 the demangled name that GDB uses if it exists. */
10557 if (cu->language == language_ada
10558 || (cu->language == language_fortran && physname))
10559 {
10560 /* For Ada unit, we prefer the linkage name over the name, as
10561 the former contains the exported name, which the user expects
10562 to be able to reference. Ideally, we want the user to be able
10563 to reference this entity using either natural or linkage name,
10564 but we haven't started looking at this enhancement yet. */
10565 const char *linkage_name = dw2_linkage_name (die, cu);
10566
10567 if (linkage_name != NULL)
10568 return linkage_name;
10569 }
10570
10571 /* These are the only languages we know how to qualify names in. */
10572 if (name != NULL
10573 && (cu->language == language_cplus
10574 || cu->language == language_fortran || cu->language == language_d
10575 || cu->language == language_rust))
10576 {
10577 if (die_needs_namespace (die, cu))
10578 {
10579 const char *prefix;
10580 const char *canonical_name = NULL;
10581
10582 string_file buf;
10583
10584 prefix = determine_prefix (die, cu);
10585 if (*prefix != '\0')
10586 {
10587 gdb::unique_xmalloc_ptr<char> prefixed_name
10588 (typename_concat (NULL, prefix, name, physname, cu));
10589
10590 buf.puts (prefixed_name.get ());
10591 }
10592 else
10593 buf.puts (name);
10594
10595 /* Template parameters may be specified in the DIE's DW_AT_name, or
10596 as children with DW_TAG_template_type_param or
10597 DW_TAG_value_type_param. If the latter, add them to the name
10598 here. If the name already has template parameters, then
10599 skip this step; some versions of GCC emit both, and
10600 it is more efficient to use the pre-computed name.
10601
10602 Something to keep in mind about this process: it is very
10603 unlikely, or in some cases downright impossible, to produce
10604 something that will match the mangled name of a function.
10605 If the definition of the function has the same debug info,
10606 we should be able to match up with it anyway. But fallbacks
10607 using the minimal symbol, for instance to find a method
10608 implemented in a stripped copy of libstdc++, will not work.
10609 If we do not have debug info for the definition, we will have to
10610 match them up some other way.
10611
10612 When we do name matching there is a related problem with function
10613 templates; two instantiated function templates are allowed to
10614 differ only by their return types, which we do not add here. */
10615
10616 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10617 {
10618 struct attribute *attr;
10619 struct die_info *child;
10620 int first = 1;
10621 const language_defn *cplus_lang = language_def (cu->language);
10622
10623 die->building_fullname = 1;
10624
10625 for (child = die->child; child != NULL; child = child->sibling)
10626 {
10627 struct type *type;
10628 LONGEST value;
10629 const gdb_byte *bytes;
10630 struct dwarf2_locexpr_baton *baton;
10631 struct value *v;
10632
10633 if (child->tag != DW_TAG_template_type_param
10634 && child->tag != DW_TAG_template_value_param)
10635 continue;
10636
10637 if (first)
10638 {
10639 buf.puts ("<");
10640 first = 0;
10641 }
10642 else
10643 buf.puts (", ");
10644
10645 attr = dwarf2_attr (child, DW_AT_type, cu);
10646 if (attr == NULL)
10647 {
10648 complaint (_("template parameter missing DW_AT_type"));
10649 buf.puts ("UNKNOWN_TYPE");
10650 continue;
10651 }
10652 type = die_type (child, cu);
10653
10654 if (child->tag == DW_TAG_template_type_param)
10655 {
10656 cplus_lang->print_type (type, "", &buf, -1, 0,
10657 &type_print_raw_options);
10658 continue;
10659 }
10660
10661 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10662 if (attr == NULL)
10663 {
10664 complaint (_("template parameter missing "
10665 "DW_AT_const_value"));
10666 buf.puts ("UNKNOWN_VALUE");
10667 continue;
10668 }
10669
10670 dwarf2_const_value_attr (attr, type, name,
10671 &cu->comp_unit_obstack, cu,
10672 &value, &bytes, &baton);
10673
10674 if (type->has_no_signedness ())
10675 /* GDB prints characters as NUMBER 'CHAR'. If that's
10676 changed, this can use value_print instead. */
10677 cplus_lang->printchar (value, type, &buf);
10678 else
10679 {
10680 struct value_print_options opts;
10681
10682 if (baton != NULL)
10683 v = dwarf2_evaluate_loc_desc (type, NULL,
10684 baton->data,
10685 baton->size,
10686 baton->per_cu,
10687 baton->per_objfile);
10688 else if (bytes != NULL)
10689 {
10690 v = allocate_value (type);
10691 memcpy (value_contents_writeable (v), bytes,
10692 TYPE_LENGTH (type));
10693 }
10694 else
10695 v = value_from_longest (type, value);
10696
10697 /* Specify decimal so that we do not depend on
10698 the radix. */
10699 get_formatted_print_options (&opts, 'd');
10700 opts.raw = 1;
10701 value_print (v, &buf, &opts);
10702 release_value (v);
10703 }
10704 }
10705
10706 die->building_fullname = 0;
10707
10708 if (!first)
10709 {
10710 /* Close the argument list, with a space if necessary
10711 (nested templates). */
10712 if (!buf.empty () && buf.string ().back () == '>')
10713 buf.puts (" >");
10714 else
10715 buf.puts (">");
10716 }
10717 }
10718
10719 /* For C++ methods, append formal parameter type
10720 information, if PHYSNAME. */
10721
10722 if (physname && die->tag == DW_TAG_subprogram
10723 && cu->language == language_cplus)
10724 {
10725 struct type *type = read_type_die (die, cu);
10726
10727 c_type_print_args (type, &buf, 1, cu->language,
10728 &type_print_raw_options);
10729
10730 if (cu->language == language_cplus)
10731 {
10732 /* Assume that an artificial first parameter is
10733 "this", but do not crash if it is not. RealView
10734 marks unnamed (and thus unused) parameters as
10735 artificial; there is no way to differentiate
10736 the two cases. */
10737 if (type->num_fields () > 0
10738 && TYPE_FIELD_ARTIFICIAL (type, 0)
10739 && type->field (0).type ()->code () == TYPE_CODE_PTR
10740 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10741 buf.puts (" const");
10742 }
10743 }
10744
10745 const std::string &intermediate_name = buf.string ();
10746
10747 if (cu->language == language_cplus)
10748 canonical_name
10749 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10750 objfile);
10751
10752 /* If we only computed INTERMEDIATE_NAME, or if
10753 INTERMEDIATE_NAME is already canonical, then we need to
10754 intern it. */
10755 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10756 name = objfile->intern (intermediate_name);
10757 else
10758 name = canonical_name;
10759 }
10760 }
10761
10762 return name;
10763 }
10764
10765 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10766 If scope qualifiers are appropriate they will be added. The result
10767 will be allocated on the storage_obstack, or NULL if the DIE does
10768 not have a name. NAME may either be from a previous call to
10769 dwarf2_name or NULL.
10770
10771 The output string will be canonicalized (if C++). */
10772
10773 static const char *
10774 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10775 {
10776 return dwarf2_compute_name (name, die, cu, 0);
10777 }
10778
10779 /* Construct a physname for the given DIE in CU. NAME may either be
10780 from a previous call to dwarf2_name or NULL. The result will be
10781 allocated on the objfile_objstack or NULL if the DIE does not have a
10782 name.
10783
10784 The output string will be canonicalized (if C++). */
10785
10786 static const char *
10787 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10788 {
10789 struct objfile *objfile = cu->per_objfile->objfile;
10790 const char *retval, *mangled = NULL, *canon = NULL;
10791 int need_copy = 1;
10792
10793 /* In this case dwarf2_compute_name is just a shortcut not building anything
10794 on its own. */
10795 if (!die_needs_namespace (die, cu))
10796 return dwarf2_compute_name (name, die, cu, 1);
10797
10798 if (cu->language != language_rust)
10799 mangled = dw2_linkage_name (die, cu);
10800
10801 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10802 has computed. */
10803 gdb::unique_xmalloc_ptr<char> demangled;
10804 if (mangled != NULL)
10805 {
10806
10807 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10808 {
10809 /* Do nothing (do not demangle the symbol name). */
10810 }
10811 else
10812 {
10813 /* Use DMGL_RET_DROP for C++ template functions to suppress
10814 their return type. It is easier for GDB users to search
10815 for such functions as `name(params)' than `long name(params)'.
10816 In such case the minimal symbol names do not match the full
10817 symbol names but for template functions there is never a need
10818 to look up their definition from their declaration so
10819 the only disadvantage remains the minimal symbol variant
10820 `long name(params)' does not have the proper inferior type. */
10821 demangled.reset (gdb_demangle (mangled,
10822 (DMGL_PARAMS | DMGL_ANSI
10823 | DMGL_RET_DROP)));
10824 }
10825 if (demangled)
10826 canon = demangled.get ();
10827 else
10828 {
10829 canon = mangled;
10830 need_copy = 0;
10831 }
10832 }
10833
10834 if (canon == NULL || check_physname)
10835 {
10836 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10837
10838 if (canon != NULL && strcmp (physname, canon) != 0)
10839 {
10840 /* It may not mean a bug in GDB. The compiler could also
10841 compute DW_AT_linkage_name incorrectly. But in such case
10842 GDB would need to be bug-to-bug compatible. */
10843
10844 complaint (_("Computed physname <%s> does not match demangled <%s> "
10845 "(from linkage <%s>) - DIE at %s [in module %s]"),
10846 physname, canon, mangled, sect_offset_str (die->sect_off),
10847 objfile_name (objfile));
10848
10849 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10850 is available here - over computed PHYSNAME. It is safer
10851 against both buggy GDB and buggy compilers. */
10852
10853 retval = canon;
10854 }
10855 else
10856 {
10857 retval = physname;
10858 need_copy = 0;
10859 }
10860 }
10861 else
10862 retval = canon;
10863
10864 if (need_copy)
10865 retval = objfile->intern (retval);
10866
10867 return retval;
10868 }
10869
10870 /* Inspect DIE in CU for a namespace alias. If one exists, record
10871 a new symbol for it.
10872
10873 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10874
10875 static int
10876 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10877 {
10878 struct attribute *attr;
10879
10880 /* If the die does not have a name, this is not a namespace
10881 alias. */
10882 attr = dwarf2_attr (die, DW_AT_name, cu);
10883 if (attr != NULL)
10884 {
10885 int num;
10886 struct die_info *d = die;
10887 struct dwarf2_cu *imported_cu = cu;
10888
10889 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10890 keep inspecting DIEs until we hit the underlying import. */
10891 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10892 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10893 {
10894 attr = dwarf2_attr (d, DW_AT_import, cu);
10895 if (attr == NULL)
10896 break;
10897
10898 d = follow_die_ref (d, attr, &imported_cu);
10899 if (d->tag != DW_TAG_imported_declaration)
10900 break;
10901 }
10902
10903 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10904 {
10905 complaint (_("DIE at %s has too many recursively imported "
10906 "declarations"), sect_offset_str (d->sect_off));
10907 return 0;
10908 }
10909
10910 if (attr != NULL)
10911 {
10912 struct type *type;
10913 sect_offset sect_off = attr->get_ref_die_offset ();
10914
10915 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10916 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10917 {
10918 /* This declaration is a global namespace alias. Add
10919 a symbol for it whose type is the aliased namespace. */
10920 new_symbol (die, type, cu);
10921 return 1;
10922 }
10923 }
10924 }
10925
10926 return 0;
10927 }
10928
10929 /* Return the using directives repository (global or local?) to use in the
10930 current context for CU.
10931
10932 For Ada, imported declarations can materialize renamings, which *may* be
10933 global. However it is impossible (for now?) in DWARF to distinguish
10934 "external" imported declarations and "static" ones. As all imported
10935 declarations seem to be static in all other languages, make them all CU-wide
10936 global only in Ada. */
10937
10938 static struct using_direct **
10939 using_directives (struct dwarf2_cu *cu)
10940 {
10941 if (cu->language == language_ada
10942 && cu->get_builder ()->outermost_context_p ())
10943 return cu->get_builder ()->get_global_using_directives ();
10944 else
10945 return cu->get_builder ()->get_local_using_directives ();
10946 }
10947
10948 /* Read the import statement specified by the given die and record it. */
10949
10950 static void
10951 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10952 {
10953 struct objfile *objfile = cu->per_objfile->objfile;
10954 struct attribute *import_attr;
10955 struct die_info *imported_die, *child_die;
10956 struct dwarf2_cu *imported_cu;
10957 const char *imported_name;
10958 const char *imported_name_prefix;
10959 const char *canonical_name;
10960 const char *import_alias;
10961 const char *imported_declaration = NULL;
10962 const char *import_prefix;
10963 std::vector<const char *> excludes;
10964
10965 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10966 if (import_attr == NULL)
10967 {
10968 complaint (_("Tag '%s' has no DW_AT_import"),
10969 dwarf_tag_name (die->tag));
10970 return;
10971 }
10972
10973 imported_cu = cu;
10974 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10975 imported_name = dwarf2_name (imported_die, imported_cu);
10976 if (imported_name == NULL)
10977 {
10978 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10979
10980 The import in the following code:
10981 namespace A
10982 {
10983 typedef int B;
10984 }
10985
10986 int main ()
10987 {
10988 using A::B;
10989 B b;
10990 return b;
10991 }
10992
10993 ...
10994 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10995 <52> DW_AT_decl_file : 1
10996 <53> DW_AT_decl_line : 6
10997 <54> DW_AT_import : <0x75>
10998 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10999 <59> DW_AT_name : B
11000 <5b> DW_AT_decl_file : 1
11001 <5c> DW_AT_decl_line : 2
11002 <5d> DW_AT_type : <0x6e>
11003 ...
11004 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11005 <76> DW_AT_byte_size : 4
11006 <77> DW_AT_encoding : 5 (signed)
11007
11008 imports the wrong die ( 0x75 instead of 0x58 ).
11009 This case will be ignored until the gcc bug is fixed. */
11010 return;
11011 }
11012
11013 /* Figure out the local name after import. */
11014 import_alias = dwarf2_name (die, cu);
11015
11016 /* Figure out where the statement is being imported to. */
11017 import_prefix = determine_prefix (die, cu);
11018
11019 /* Figure out what the scope of the imported die is and prepend it
11020 to the name of the imported die. */
11021 imported_name_prefix = determine_prefix (imported_die, imported_cu);
11022
11023 if (imported_die->tag != DW_TAG_namespace
11024 && imported_die->tag != DW_TAG_module)
11025 {
11026 imported_declaration = imported_name;
11027 canonical_name = imported_name_prefix;
11028 }
11029 else if (strlen (imported_name_prefix) > 0)
11030 canonical_name = obconcat (&objfile->objfile_obstack,
11031 imported_name_prefix,
11032 (cu->language == language_d ? "." : "::"),
11033 imported_name, (char *) NULL);
11034 else
11035 canonical_name = imported_name;
11036
11037 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11038 for (child_die = die->child; child_die && child_die->tag;
11039 child_die = child_die->sibling)
11040 {
11041 /* DWARF-4: A Fortran use statement with a “rename list” may be
11042 represented by an imported module entry with an import attribute
11043 referring to the module and owned entries corresponding to those
11044 entities that are renamed as part of being imported. */
11045
11046 if (child_die->tag != DW_TAG_imported_declaration)
11047 {
11048 complaint (_("child DW_TAG_imported_declaration expected "
11049 "- DIE at %s [in module %s]"),
11050 sect_offset_str (child_die->sect_off),
11051 objfile_name (objfile));
11052 continue;
11053 }
11054
11055 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11056 if (import_attr == NULL)
11057 {
11058 complaint (_("Tag '%s' has no DW_AT_import"),
11059 dwarf_tag_name (child_die->tag));
11060 continue;
11061 }
11062
11063 imported_cu = cu;
11064 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11065 &imported_cu);
11066 imported_name = dwarf2_name (imported_die, imported_cu);
11067 if (imported_name == NULL)
11068 {
11069 complaint (_("child DW_TAG_imported_declaration has unknown "
11070 "imported name - DIE at %s [in module %s]"),
11071 sect_offset_str (child_die->sect_off),
11072 objfile_name (objfile));
11073 continue;
11074 }
11075
11076 excludes.push_back (imported_name);
11077
11078 process_die (child_die, cu);
11079 }
11080
11081 add_using_directive (using_directives (cu),
11082 import_prefix,
11083 canonical_name,
11084 import_alias,
11085 imported_declaration,
11086 excludes,
11087 0,
11088 &objfile->objfile_obstack);
11089 }
11090
11091 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11092 types, but gives them a size of zero. Starting with version 14,
11093 ICC is compatible with GCC. */
11094
11095 static bool
11096 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11097 {
11098 if (!cu->checked_producer)
11099 check_producer (cu);
11100
11101 return cu->producer_is_icc_lt_14;
11102 }
11103
11104 /* ICC generates a DW_AT_type for C void functions. This was observed on
11105 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11106 which says that void functions should not have a DW_AT_type. */
11107
11108 static bool
11109 producer_is_icc (struct dwarf2_cu *cu)
11110 {
11111 if (!cu->checked_producer)
11112 check_producer (cu);
11113
11114 return cu->producer_is_icc;
11115 }
11116
11117 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11118 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11119 this, it was first present in GCC release 4.3.0. */
11120
11121 static bool
11122 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11123 {
11124 if (!cu->checked_producer)
11125 check_producer (cu);
11126
11127 return cu->producer_is_gcc_lt_4_3;
11128 }
11129
11130 static file_and_directory
11131 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11132 {
11133 file_and_directory res;
11134
11135 /* Find the filename. Do not use dwarf2_name here, since the filename
11136 is not a source language identifier. */
11137 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11138 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11139
11140 if (res.comp_dir == NULL
11141 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11142 && IS_ABSOLUTE_PATH (res.name))
11143 {
11144 res.comp_dir_storage = ldirname (res.name);
11145 if (!res.comp_dir_storage.empty ())
11146 res.comp_dir = res.comp_dir_storage.c_str ();
11147 }
11148 if (res.comp_dir != NULL)
11149 {
11150 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11151 directory, get rid of it. */
11152 const char *cp = strchr (res.comp_dir, ':');
11153
11154 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11155 res.comp_dir = cp + 1;
11156 }
11157
11158 if (res.name == NULL)
11159 res.name = "<unknown>";
11160
11161 return res;
11162 }
11163
11164 /* Handle DW_AT_stmt_list for a compilation unit.
11165 DIE is the DW_TAG_compile_unit die for CU.
11166 COMP_DIR is the compilation directory. LOWPC is passed to
11167 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11168
11169 static void
11170 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11171 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11172 {
11173 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11174 struct attribute *attr;
11175 struct line_header line_header_local;
11176 hashval_t line_header_local_hash;
11177 void **slot;
11178 int decode_mapping;
11179
11180 gdb_assert (! cu->per_cu->is_debug_types);
11181
11182 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11183 if (attr == NULL || !attr->form_is_unsigned ())
11184 return;
11185
11186 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11187
11188 /* The line header hash table is only created if needed (it exists to
11189 prevent redundant reading of the line table for partial_units).
11190 If we're given a partial_unit, we'll need it. If we're given a
11191 compile_unit, then use the line header hash table if it's already
11192 created, but don't create one just yet. */
11193
11194 if (per_objfile->line_header_hash == NULL
11195 && die->tag == DW_TAG_partial_unit)
11196 {
11197 per_objfile->line_header_hash
11198 .reset (htab_create_alloc (127, line_header_hash_voidp,
11199 line_header_eq_voidp,
11200 free_line_header_voidp,
11201 xcalloc, xfree));
11202 }
11203
11204 line_header_local.sect_off = line_offset;
11205 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11206 line_header_local_hash = line_header_hash (&line_header_local);
11207 if (per_objfile->line_header_hash != NULL)
11208 {
11209 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11210 &line_header_local,
11211 line_header_local_hash, NO_INSERT);
11212
11213 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11214 is not present in *SLOT (since if there is something in *SLOT then
11215 it will be for a partial_unit). */
11216 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11217 {
11218 gdb_assert (*slot != NULL);
11219 cu->line_header = (struct line_header *) *slot;
11220 return;
11221 }
11222 }
11223
11224 /* dwarf_decode_line_header does not yet provide sufficient information.
11225 We always have to call also dwarf_decode_lines for it. */
11226 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11227 if (lh == NULL)
11228 return;
11229
11230 cu->line_header = lh.release ();
11231 cu->line_header_die_owner = die;
11232
11233 if (per_objfile->line_header_hash == NULL)
11234 slot = NULL;
11235 else
11236 {
11237 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11238 &line_header_local,
11239 line_header_local_hash, INSERT);
11240 gdb_assert (slot != NULL);
11241 }
11242 if (slot != NULL && *slot == NULL)
11243 {
11244 /* This newly decoded line number information unit will be owned
11245 by line_header_hash hash table. */
11246 *slot = cu->line_header;
11247 cu->line_header_die_owner = NULL;
11248 }
11249 else
11250 {
11251 /* We cannot free any current entry in (*slot) as that struct line_header
11252 may be already used by multiple CUs. Create only temporary decoded
11253 line_header for this CU - it may happen at most once for each line
11254 number information unit. And if we're not using line_header_hash
11255 then this is what we want as well. */
11256 gdb_assert (die->tag != DW_TAG_partial_unit);
11257 }
11258 decode_mapping = (die->tag != DW_TAG_partial_unit);
11259 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11260 decode_mapping);
11261
11262 }
11263
11264 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11265
11266 static void
11267 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11268 {
11269 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11270 struct objfile *objfile = per_objfile->objfile;
11271 struct gdbarch *gdbarch = objfile->arch ();
11272 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11273 CORE_ADDR highpc = ((CORE_ADDR) 0);
11274 struct attribute *attr;
11275 struct die_info *child_die;
11276 CORE_ADDR baseaddr;
11277
11278 prepare_one_comp_unit (cu, die, cu->language);
11279 baseaddr = objfile->text_section_offset ();
11280
11281 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11282
11283 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11284 from finish_block. */
11285 if (lowpc == ((CORE_ADDR) -1))
11286 lowpc = highpc;
11287 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11288
11289 file_and_directory fnd = find_file_and_directory (die, cu);
11290
11291 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11292 standardised yet. As a workaround for the language detection we fall
11293 back to the DW_AT_producer string. */
11294 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11295 cu->language = language_opencl;
11296
11297 /* Similar hack for Go. */
11298 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11299 set_cu_language (DW_LANG_Go, cu);
11300
11301 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11302
11303 /* Decode line number information if present. We do this before
11304 processing child DIEs, so that the line header table is available
11305 for DW_AT_decl_file. */
11306 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11307
11308 /* Process all dies in compilation unit. */
11309 if (die->child != NULL)
11310 {
11311 child_die = die->child;
11312 while (child_die && child_die->tag)
11313 {
11314 process_die (child_die, cu);
11315 child_die = child_die->sibling;
11316 }
11317 }
11318
11319 /* Decode macro information, if present. Dwarf 2 macro information
11320 refers to information in the line number info statement program
11321 header, so we can only read it if we've read the header
11322 successfully. */
11323 attr = dwarf2_attr (die, DW_AT_macros, cu);
11324 if (attr == NULL)
11325 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11326 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11327 {
11328 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11329 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11330
11331 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11332 }
11333 else
11334 {
11335 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11336 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11337 {
11338 unsigned int macro_offset = attr->as_unsigned ();
11339
11340 dwarf_decode_macros (cu, macro_offset, 0);
11341 }
11342 }
11343 }
11344
11345 void
11346 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11347 {
11348 struct type_unit_group *tu_group;
11349 int first_time;
11350 struct attribute *attr;
11351 unsigned int i;
11352 struct signatured_type *sig_type;
11353
11354 gdb_assert (per_cu->is_debug_types);
11355 sig_type = (struct signatured_type *) per_cu;
11356
11357 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11358
11359 /* If we're using .gdb_index (includes -readnow) then
11360 per_cu->type_unit_group may not have been set up yet. */
11361 if (sig_type->type_unit_group == NULL)
11362 sig_type->type_unit_group = get_type_unit_group (this, attr);
11363 tu_group = sig_type->type_unit_group;
11364
11365 /* If we've already processed this stmt_list there's no real need to
11366 do it again, we could fake it and just recreate the part we need
11367 (file name,index -> symtab mapping). If data shows this optimization
11368 is useful we can do it then. */
11369 type_unit_group_unshareable *tug_unshare
11370 = per_objfile->get_type_unit_group_unshareable (tu_group);
11371 first_time = tug_unshare->compunit_symtab == NULL;
11372
11373 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11374 debug info. */
11375 line_header_up lh;
11376 if (attr != NULL && attr->form_is_unsigned ())
11377 {
11378 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11379 lh = dwarf_decode_line_header (line_offset, this);
11380 }
11381 if (lh == NULL)
11382 {
11383 if (first_time)
11384 start_symtab ("", NULL, 0);
11385 else
11386 {
11387 gdb_assert (tug_unshare->symtabs == NULL);
11388 gdb_assert (m_builder == nullptr);
11389 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11390 m_builder.reset (new struct buildsym_compunit
11391 (COMPUNIT_OBJFILE (cust), "",
11392 COMPUNIT_DIRNAME (cust),
11393 compunit_language (cust),
11394 0, cust));
11395 list_in_scope = get_builder ()->get_file_symbols ();
11396 }
11397 return;
11398 }
11399
11400 line_header = lh.release ();
11401 line_header_die_owner = die;
11402
11403 if (first_time)
11404 {
11405 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11406
11407 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11408 still initializing it, and our caller (a few levels up)
11409 process_full_type_unit still needs to know if this is the first
11410 time. */
11411
11412 tug_unshare->symtabs
11413 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11414 struct symtab *, line_header->file_names_size ());
11415
11416 auto &file_names = line_header->file_names ();
11417 for (i = 0; i < file_names.size (); ++i)
11418 {
11419 file_entry &fe = file_names[i];
11420 dwarf2_start_subfile (this, fe.name,
11421 fe.include_dir (line_header));
11422 buildsym_compunit *b = get_builder ();
11423 if (b->get_current_subfile ()->symtab == NULL)
11424 {
11425 /* NOTE: start_subfile will recognize when it's been
11426 passed a file it has already seen. So we can't
11427 assume there's a simple mapping from
11428 cu->line_header->file_names to subfiles, plus
11429 cu->line_header->file_names may contain dups. */
11430 b->get_current_subfile ()->symtab
11431 = allocate_symtab (cust, b->get_current_subfile ()->name);
11432 }
11433
11434 fe.symtab = b->get_current_subfile ()->symtab;
11435 tug_unshare->symtabs[i] = fe.symtab;
11436 }
11437 }
11438 else
11439 {
11440 gdb_assert (m_builder == nullptr);
11441 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11442 m_builder.reset (new struct buildsym_compunit
11443 (COMPUNIT_OBJFILE (cust), "",
11444 COMPUNIT_DIRNAME (cust),
11445 compunit_language (cust),
11446 0, cust));
11447 list_in_scope = get_builder ()->get_file_symbols ();
11448
11449 auto &file_names = line_header->file_names ();
11450 for (i = 0; i < file_names.size (); ++i)
11451 {
11452 file_entry &fe = file_names[i];
11453 fe.symtab = tug_unshare->symtabs[i];
11454 }
11455 }
11456
11457 /* The main symtab is allocated last. Type units don't have DW_AT_name
11458 so they don't have a "real" (so to speak) symtab anyway.
11459 There is later code that will assign the main symtab to all symbols
11460 that don't have one. We need to handle the case of a symbol with a
11461 missing symtab (DW_AT_decl_file) anyway. */
11462 }
11463
11464 /* Process DW_TAG_type_unit.
11465 For TUs we want to skip the first top level sibling if it's not the
11466 actual type being defined by this TU. In this case the first top
11467 level sibling is there to provide context only. */
11468
11469 static void
11470 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11471 {
11472 struct die_info *child_die;
11473
11474 prepare_one_comp_unit (cu, die, language_minimal);
11475
11476 /* Initialize (or reinitialize) the machinery for building symtabs.
11477 We do this before processing child DIEs, so that the line header table
11478 is available for DW_AT_decl_file. */
11479 cu->setup_type_unit_groups (die);
11480
11481 if (die->child != NULL)
11482 {
11483 child_die = die->child;
11484 while (child_die && child_die->tag)
11485 {
11486 process_die (child_die, cu);
11487 child_die = child_die->sibling;
11488 }
11489 }
11490 }
11491 \f
11492 /* DWO/DWP files.
11493
11494 http://gcc.gnu.org/wiki/DebugFission
11495 http://gcc.gnu.org/wiki/DebugFissionDWP
11496
11497 To simplify handling of both DWO files ("object" files with the DWARF info)
11498 and DWP files (a file with the DWOs packaged up into one file), we treat
11499 DWP files as having a collection of virtual DWO files. */
11500
11501 static hashval_t
11502 hash_dwo_file (const void *item)
11503 {
11504 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11505 hashval_t hash;
11506
11507 hash = htab_hash_string (dwo_file->dwo_name);
11508 if (dwo_file->comp_dir != NULL)
11509 hash += htab_hash_string (dwo_file->comp_dir);
11510 return hash;
11511 }
11512
11513 static int
11514 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11515 {
11516 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11517 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11518
11519 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11520 return 0;
11521 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11522 return lhs->comp_dir == rhs->comp_dir;
11523 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11524 }
11525
11526 /* Allocate a hash table for DWO files. */
11527
11528 static htab_up
11529 allocate_dwo_file_hash_table ()
11530 {
11531 auto delete_dwo_file = [] (void *item)
11532 {
11533 struct dwo_file *dwo_file = (struct dwo_file *) item;
11534
11535 delete dwo_file;
11536 };
11537
11538 return htab_up (htab_create_alloc (41,
11539 hash_dwo_file,
11540 eq_dwo_file,
11541 delete_dwo_file,
11542 xcalloc, xfree));
11543 }
11544
11545 /* Lookup DWO file DWO_NAME. */
11546
11547 static void **
11548 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11549 const char *dwo_name,
11550 const char *comp_dir)
11551 {
11552 struct dwo_file find_entry;
11553 void **slot;
11554
11555 if (per_objfile->per_bfd->dwo_files == NULL)
11556 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11557
11558 find_entry.dwo_name = dwo_name;
11559 find_entry.comp_dir = comp_dir;
11560 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11561 INSERT);
11562
11563 return slot;
11564 }
11565
11566 static hashval_t
11567 hash_dwo_unit (const void *item)
11568 {
11569 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11570
11571 /* This drops the top 32 bits of the id, but is ok for a hash. */
11572 return dwo_unit->signature;
11573 }
11574
11575 static int
11576 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11577 {
11578 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11579 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11580
11581 /* The signature is assumed to be unique within the DWO file.
11582 So while object file CU dwo_id's always have the value zero,
11583 that's OK, assuming each object file DWO file has only one CU,
11584 and that's the rule for now. */
11585 return lhs->signature == rhs->signature;
11586 }
11587
11588 /* Allocate a hash table for DWO CUs,TUs.
11589 There is one of these tables for each of CUs,TUs for each DWO file. */
11590
11591 static htab_up
11592 allocate_dwo_unit_table ()
11593 {
11594 /* Start out with a pretty small number.
11595 Generally DWO files contain only one CU and maybe some TUs. */
11596 return htab_up (htab_create_alloc (3,
11597 hash_dwo_unit,
11598 eq_dwo_unit,
11599 NULL, xcalloc, xfree));
11600 }
11601
11602 /* die_reader_func for create_dwo_cu. */
11603
11604 static void
11605 create_dwo_cu_reader (const struct die_reader_specs *reader,
11606 const gdb_byte *info_ptr,
11607 struct die_info *comp_unit_die,
11608 struct dwo_file *dwo_file,
11609 struct dwo_unit *dwo_unit)
11610 {
11611 struct dwarf2_cu *cu = reader->cu;
11612 sect_offset sect_off = cu->per_cu->sect_off;
11613 struct dwarf2_section_info *section = cu->per_cu->section;
11614
11615 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11616 if (!signature.has_value ())
11617 {
11618 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11619 " its dwo_id [in module %s]"),
11620 sect_offset_str (sect_off), dwo_file->dwo_name);
11621 return;
11622 }
11623
11624 dwo_unit->dwo_file = dwo_file;
11625 dwo_unit->signature = *signature;
11626 dwo_unit->section = section;
11627 dwo_unit->sect_off = sect_off;
11628 dwo_unit->length = cu->per_cu->length;
11629
11630 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11631 sect_offset_str (sect_off),
11632 hex_string (dwo_unit->signature));
11633 }
11634
11635 /* Create the dwo_units for the CUs in a DWO_FILE.
11636 Note: This function processes DWO files only, not DWP files. */
11637
11638 static void
11639 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11640 dwarf2_cu *cu, struct dwo_file &dwo_file,
11641 dwarf2_section_info &section, htab_up &cus_htab)
11642 {
11643 struct objfile *objfile = per_objfile->objfile;
11644 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11645 const gdb_byte *info_ptr, *end_ptr;
11646
11647 section.read (objfile);
11648 info_ptr = section.buffer;
11649
11650 if (info_ptr == NULL)
11651 return;
11652
11653 dwarf_read_debug_printf ("Reading %s for %s:",
11654 section.get_name (),
11655 section.get_file_name ());
11656
11657 end_ptr = info_ptr + section.size;
11658 while (info_ptr < end_ptr)
11659 {
11660 struct dwarf2_per_cu_data per_cu;
11661 struct dwo_unit read_unit {};
11662 struct dwo_unit *dwo_unit;
11663 void **slot;
11664 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11665
11666 memset (&per_cu, 0, sizeof (per_cu));
11667 per_cu.per_bfd = per_bfd;
11668 per_cu.is_debug_types = 0;
11669 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11670 per_cu.section = &section;
11671
11672 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11673 if (!reader.dummy_p)
11674 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11675 &dwo_file, &read_unit);
11676 info_ptr += per_cu.length;
11677
11678 // If the unit could not be parsed, skip it.
11679 if (read_unit.dwo_file == NULL)
11680 continue;
11681
11682 if (cus_htab == NULL)
11683 cus_htab = allocate_dwo_unit_table ();
11684
11685 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11686 struct dwo_unit);
11687 *dwo_unit = read_unit;
11688 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11689 gdb_assert (slot != NULL);
11690 if (*slot != NULL)
11691 {
11692 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11693 sect_offset dup_sect_off = dup_cu->sect_off;
11694
11695 complaint (_("debug cu entry at offset %s is duplicate to"
11696 " the entry at offset %s, signature %s"),
11697 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11698 hex_string (dwo_unit->signature));
11699 }
11700 *slot = (void *)dwo_unit;
11701 }
11702 }
11703
11704 /* DWP file .debug_{cu,tu}_index section format:
11705 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11706 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11707
11708 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11709 officially standard DWP format was published with DWARF v5 and is called
11710 Version 5. There are no versions 3 or 4.
11711
11712 DWP Version 1:
11713
11714 Both index sections have the same format, and serve to map a 64-bit
11715 signature to a set of section numbers. Each section begins with a header,
11716 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11717 indexes, and a pool of 32-bit section numbers. The index sections will be
11718 aligned at 8-byte boundaries in the file.
11719
11720 The index section header consists of:
11721
11722 V, 32 bit version number
11723 -, 32 bits unused
11724 N, 32 bit number of compilation units or type units in the index
11725 M, 32 bit number of slots in the hash table
11726
11727 Numbers are recorded using the byte order of the application binary.
11728
11729 The hash table begins at offset 16 in the section, and consists of an array
11730 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11731 order of the application binary). Unused slots in the hash table are 0.
11732 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11733
11734 The parallel table begins immediately after the hash table
11735 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11736 array of 32-bit indexes (using the byte order of the application binary),
11737 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11738 table contains a 32-bit index into the pool of section numbers. For unused
11739 hash table slots, the corresponding entry in the parallel table will be 0.
11740
11741 The pool of section numbers begins immediately following the hash table
11742 (at offset 16 + 12 * M from the beginning of the section). The pool of
11743 section numbers consists of an array of 32-bit words (using the byte order
11744 of the application binary). Each item in the array is indexed starting
11745 from 0. The hash table entry provides the index of the first section
11746 number in the set. Additional section numbers in the set follow, and the
11747 set is terminated by a 0 entry (section number 0 is not used in ELF).
11748
11749 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11750 section must be the first entry in the set, and the .debug_abbrev.dwo must
11751 be the second entry. Other members of the set may follow in any order.
11752
11753 ---
11754
11755 DWP Versions 2 and 5:
11756
11757 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11758 and the entries in the index tables are now offsets into these sections.
11759 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11760 section.
11761
11762 Index Section Contents:
11763 Header
11764 Hash Table of Signatures dwp_hash_table.hash_table
11765 Parallel Table of Indices dwp_hash_table.unit_table
11766 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11767 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11768
11769 The index section header consists of:
11770
11771 V, 32 bit version number
11772 L, 32 bit number of columns in the table of section offsets
11773 N, 32 bit number of compilation units or type units in the index
11774 M, 32 bit number of slots in the hash table
11775
11776 Numbers are recorded using the byte order of the application binary.
11777
11778 The hash table has the same format as version 1.
11779 The parallel table of indices has the same format as version 1,
11780 except that the entries are origin-1 indices into the table of sections
11781 offsets and the table of section sizes.
11782
11783 The table of offsets begins immediately following the parallel table
11784 (at offset 16 + 12 * M from the beginning of the section). The table is
11785 a two-dimensional array of 32-bit words (using the byte order of the
11786 application binary), with L columns and N+1 rows, in row-major order.
11787 Each row in the array is indexed starting from 0. The first row provides
11788 a key to the remaining rows: each column in this row provides an identifier
11789 for a debug section, and the offsets in the same column of subsequent rows
11790 refer to that section. The section identifiers for Version 2 are:
11791
11792 DW_SECT_INFO 1 .debug_info.dwo
11793 DW_SECT_TYPES 2 .debug_types.dwo
11794 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11795 DW_SECT_LINE 4 .debug_line.dwo
11796 DW_SECT_LOC 5 .debug_loc.dwo
11797 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11798 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11799 DW_SECT_MACRO 8 .debug_macro.dwo
11800
11801 The section identifiers for Version 5 are:
11802
11803 DW_SECT_INFO_V5 1 .debug_info.dwo
11804 DW_SECT_RESERVED_V5 2 --
11805 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11806 DW_SECT_LINE_V5 4 .debug_line.dwo
11807 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11808 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11809 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11810 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11811
11812 The offsets provided by the CU and TU index sections are the base offsets
11813 for the contributions made by each CU or TU to the corresponding section
11814 in the package file. Each CU and TU header contains an abbrev_offset
11815 field, used to find the abbreviations table for that CU or TU within the
11816 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11817 be interpreted as relative to the base offset given in the index section.
11818 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11819 should be interpreted as relative to the base offset for .debug_line.dwo,
11820 and offsets into other debug sections obtained from DWARF attributes should
11821 also be interpreted as relative to the corresponding base offset.
11822
11823 The table of sizes begins immediately following the table of offsets.
11824 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11825 with L columns and N rows, in row-major order. Each row in the array is
11826 indexed starting from 1 (row 0 is shared by the two tables).
11827
11828 ---
11829
11830 Hash table lookup is handled the same in version 1 and 2:
11831
11832 We assume that N and M will not exceed 2^32 - 1.
11833 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11834
11835 Given a 64-bit compilation unit signature or a type signature S, an entry
11836 in the hash table is located as follows:
11837
11838 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11839 the low-order k bits all set to 1.
11840
11841 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11842
11843 3) If the hash table entry at index H matches the signature, use that
11844 entry. If the hash table entry at index H is unused (all zeroes),
11845 terminate the search: the signature is not present in the table.
11846
11847 4) Let H = (H + H') modulo M. Repeat at Step 3.
11848
11849 Because M > N and H' and M are relatively prime, the search is guaranteed
11850 to stop at an unused slot or find the match. */
11851
11852 /* Create a hash table to map DWO IDs to their CU/TU entry in
11853 .debug_{info,types}.dwo in DWP_FILE.
11854 Returns NULL if there isn't one.
11855 Note: This function processes DWP files only, not DWO files. */
11856
11857 static struct dwp_hash_table *
11858 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11859 struct dwp_file *dwp_file, int is_debug_types)
11860 {
11861 struct objfile *objfile = per_objfile->objfile;
11862 bfd *dbfd = dwp_file->dbfd.get ();
11863 const gdb_byte *index_ptr, *index_end;
11864 struct dwarf2_section_info *index;
11865 uint32_t version, nr_columns, nr_units, nr_slots;
11866 struct dwp_hash_table *htab;
11867
11868 if (is_debug_types)
11869 index = &dwp_file->sections.tu_index;
11870 else
11871 index = &dwp_file->sections.cu_index;
11872
11873 if (index->empty ())
11874 return NULL;
11875 index->read (objfile);
11876
11877 index_ptr = index->buffer;
11878 index_end = index_ptr + index->size;
11879
11880 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11881 For now it's safe to just read 4 bytes (particularly as it's difficult to
11882 tell if you're dealing with Version 5 before you've read the version). */
11883 version = read_4_bytes (dbfd, index_ptr);
11884 index_ptr += 4;
11885 if (version == 2 || version == 5)
11886 nr_columns = read_4_bytes (dbfd, index_ptr);
11887 else
11888 nr_columns = 0;
11889 index_ptr += 4;
11890 nr_units = read_4_bytes (dbfd, index_ptr);
11891 index_ptr += 4;
11892 nr_slots = read_4_bytes (dbfd, index_ptr);
11893 index_ptr += 4;
11894
11895 if (version != 1 && version != 2 && version != 5)
11896 {
11897 error (_("Dwarf Error: unsupported DWP file version (%s)"
11898 " [in module %s]"),
11899 pulongest (version), dwp_file->name);
11900 }
11901 if (nr_slots != (nr_slots & -nr_slots))
11902 {
11903 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11904 " is not power of 2 [in module %s]"),
11905 pulongest (nr_slots), dwp_file->name);
11906 }
11907
11908 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11909 htab->version = version;
11910 htab->nr_columns = nr_columns;
11911 htab->nr_units = nr_units;
11912 htab->nr_slots = nr_slots;
11913 htab->hash_table = index_ptr;
11914 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11915
11916 /* Exit early if the table is empty. */
11917 if (nr_slots == 0 || nr_units == 0
11918 || (version == 2 && nr_columns == 0)
11919 || (version == 5 && nr_columns == 0))
11920 {
11921 /* All must be zero. */
11922 if (nr_slots != 0 || nr_units != 0
11923 || (version == 2 && nr_columns != 0)
11924 || (version == 5 && nr_columns != 0))
11925 {
11926 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11927 " all zero [in modules %s]"),
11928 dwp_file->name);
11929 }
11930 return htab;
11931 }
11932
11933 if (version == 1)
11934 {
11935 htab->section_pool.v1.indices =
11936 htab->unit_table + sizeof (uint32_t) * nr_slots;
11937 /* It's harder to decide whether the section is too small in v1.
11938 V1 is deprecated anyway so we punt. */
11939 }
11940 else if (version == 2)
11941 {
11942 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11943 int *ids = htab->section_pool.v2.section_ids;
11944 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11945 /* Reverse map for error checking. */
11946 int ids_seen[DW_SECT_MAX + 1];
11947 int i;
11948
11949 if (nr_columns < 2)
11950 {
11951 error (_("Dwarf Error: bad DWP hash table, too few columns"
11952 " in section table [in module %s]"),
11953 dwp_file->name);
11954 }
11955 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11956 {
11957 error (_("Dwarf Error: bad DWP hash table, too many columns"
11958 " in section table [in module %s]"),
11959 dwp_file->name);
11960 }
11961 memset (ids, 255, sizeof_ids);
11962 memset (ids_seen, 255, sizeof (ids_seen));
11963 for (i = 0; i < nr_columns; ++i)
11964 {
11965 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11966
11967 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11968 {
11969 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11970 " in section table [in module %s]"),
11971 id, dwp_file->name);
11972 }
11973 if (ids_seen[id] != -1)
11974 {
11975 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11976 " id %d in section table [in module %s]"),
11977 id, dwp_file->name);
11978 }
11979 ids_seen[id] = i;
11980 ids[i] = id;
11981 }
11982 /* Must have exactly one info or types section. */
11983 if (((ids_seen[DW_SECT_INFO] != -1)
11984 + (ids_seen[DW_SECT_TYPES] != -1))
11985 != 1)
11986 {
11987 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11988 " DWO info/types section [in module %s]"),
11989 dwp_file->name);
11990 }
11991 /* Must have an abbrev section. */
11992 if (ids_seen[DW_SECT_ABBREV] == -1)
11993 {
11994 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11995 " section [in module %s]"),
11996 dwp_file->name);
11997 }
11998 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11999 htab->section_pool.v2.sizes =
12000 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12001 * nr_units * nr_columns);
12002 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12003 * nr_units * nr_columns))
12004 > index_end)
12005 {
12006 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12007 " [in module %s]"),
12008 dwp_file->name);
12009 }
12010 }
12011 else /* version == 5 */
12012 {
12013 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12014 int *ids = htab->section_pool.v5.section_ids;
12015 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
12016 /* Reverse map for error checking. */
12017 int ids_seen[DW_SECT_MAX_V5 + 1];
12018
12019 if (nr_columns < 2)
12020 {
12021 error (_("Dwarf Error: bad DWP hash table, too few columns"
12022 " in section table [in module %s]"),
12023 dwp_file->name);
12024 }
12025 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
12026 {
12027 error (_("Dwarf Error: bad DWP hash table, too many columns"
12028 " in section table [in module %s]"),
12029 dwp_file->name);
12030 }
12031 memset (ids, 255, sizeof_ids);
12032 memset (ids_seen, 255, sizeof (ids_seen));
12033 for (int i = 0; i < nr_columns; ++i)
12034 {
12035 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12036
12037 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12038 {
12039 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12040 " in section table [in module %s]"),
12041 id, dwp_file->name);
12042 }
12043 if (ids_seen[id] != -1)
12044 {
12045 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12046 " id %d in section table [in module %s]"),
12047 id, dwp_file->name);
12048 }
12049 ids_seen[id] = i;
12050 ids[i] = id;
12051 }
12052 /* Must have seen an info section. */
12053 if (ids_seen[DW_SECT_INFO_V5] == -1)
12054 {
12055 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12056 " DWO info/types section [in module %s]"),
12057 dwp_file->name);
12058 }
12059 /* Must have an abbrev section. */
12060 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12061 {
12062 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12063 " section [in module %s]"),
12064 dwp_file->name);
12065 }
12066 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12067 htab->section_pool.v5.sizes
12068 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12069 * nr_units * nr_columns);
12070 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12071 * nr_units * nr_columns))
12072 > index_end)
12073 {
12074 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12075 " [in module %s]"),
12076 dwp_file->name);
12077 }
12078 }
12079
12080 return htab;
12081 }
12082
12083 /* Update SECTIONS with the data from SECTP.
12084
12085 This function is like the other "locate" section routines, but in
12086 this context the sections to read comes from the DWP V1 hash table,
12087 not the full ELF section table.
12088
12089 The result is non-zero for success, or zero if an error was found. */
12090
12091 static int
12092 locate_v1_virtual_dwo_sections (asection *sectp,
12093 struct virtual_v1_dwo_sections *sections)
12094 {
12095 const struct dwop_section_names *names = &dwop_section_names;
12096
12097 if (section_is_p (sectp->name, &names->abbrev_dwo))
12098 {
12099 /* There can be only one. */
12100 if (sections->abbrev.s.section != NULL)
12101 return 0;
12102 sections->abbrev.s.section = sectp;
12103 sections->abbrev.size = bfd_section_size (sectp);
12104 }
12105 else if (section_is_p (sectp->name, &names->info_dwo)
12106 || section_is_p (sectp->name, &names->types_dwo))
12107 {
12108 /* There can be only one. */
12109 if (sections->info_or_types.s.section != NULL)
12110 return 0;
12111 sections->info_or_types.s.section = sectp;
12112 sections->info_or_types.size = bfd_section_size (sectp);
12113 }
12114 else if (section_is_p (sectp->name, &names->line_dwo))
12115 {
12116 /* There can be only one. */
12117 if (sections->line.s.section != NULL)
12118 return 0;
12119 sections->line.s.section = sectp;
12120 sections->line.size = bfd_section_size (sectp);
12121 }
12122 else if (section_is_p (sectp->name, &names->loc_dwo))
12123 {
12124 /* There can be only one. */
12125 if (sections->loc.s.section != NULL)
12126 return 0;
12127 sections->loc.s.section = sectp;
12128 sections->loc.size = bfd_section_size (sectp);
12129 }
12130 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12131 {
12132 /* There can be only one. */
12133 if (sections->macinfo.s.section != NULL)
12134 return 0;
12135 sections->macinfo.s.section = sectp;
12136 sections->macinfo.size = bfd_section_size (sectp);
12137 }
12138 else if (section_is_p (sectp->name, &names->macro_dwo))
12139 {
12140 /* There can be only one. */
12141 if (sections->macro.s.section != NULL)
12142 return 0;
12143 sections->macro.s.section = sectp;
12144 sections->macro.size = bfd_section_size (sectp);
12145 }
12146 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12147 {
12148 /* There can be only one. */
12149 if (sections->str_offsets.s.section != NULL)
12150 return 0;
12151 sections->str_offsets.s.section = sectp;
12152 sections->str_offsets.size = bfd_section_size (sectp);
12153 }
12154 else
12155 {
12156 /* No other kind of section is valid. */
12157 return 0;
12158 }
12159
12160 return 1;
12161 }
12162
12163 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12164 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12165 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12166 This is for DWP version 1 files. */
12167
12168 static struct dwo_unit *
12169 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12170 struct dwp_file *dwp_file,
12171 uint32_t unit_index,
12172 const char *comp_dir,
12173 ULONGEST signature, int is_debug_types)
12174 {
12175 const struct dwp_hash_table *dwp_htab =
12176 is_debug_types ? dwp_file->tus : dwp_file->cus;
12177 bfd *dbfd = dwp_file->dbfd.get ();
12178 const char *kind = is_debug_types ? "TU" : "CU";
12179 struct dwo_file *dwo_file;
12180 struct dwo_unit *dwo_unit;
12181 struct virtual_v1_dwo_sections sections;
12182 void **dwo_file_slot;
12183 int i;
12184
12185 gdb_assert (dwp_file->version == 1);
12186
12187 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12188 kind, pulongest (unit_index), hex_string (signature),
12189 dwp_file->name);
12190
12191 /* Fetch the sections of this DWO unit.
12192 Put a limit on the number of sections we look for so that bad data
12193 doesn't cause us to loop forever. */
12194
12195 #define MAX_NR_V1_DWO_SECTIONS \
12196 (1 /* .debug_info or .debug_types */ \
12197 + 1 /* .debug_abbrev */ \
12198 + 1 /* .debug_line */ \
12199 + 1 /* .debug_loc */ \
12200 + 1 /* .debug_str_offsets */ \
12201 + 1 /* .debug_macro or .debug_macinfo */ \
12202 + 1 /* trailing zero */)
12203
12204 memset (&sections, 0, sizeof (sections));
12205
12206 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12207 {
12208 asection *sectp;
12209 uint32_t section_nr =
12210 read_4_bytes (dbfd,
12211 dwp_htab->section_pool.v1.indices
12212 + (unit_index + i) * sizeof (uint32_t));
12213
12214 if (section_nr == 0)
12215 break;
12216 if (section_nr >= dwp_file->num_sections)
12217 {
12218 error (_("Dwarf Error: bad DWP hash table, section number too large"
12219 " [in module %s]"),
12220 dwp_file->name);
12221 }
12222
12223 sectp = dwp_file->elf_sections[section_nr];
12224 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12225 {
12226 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12227 " [in module %s]"),
12228 dwp_file->name);
12229 }
12230 }
12231
12232 if (i < 2
12233 || sections.info_or_types.empty ()
12234 || sections.abbrev.empty ())
12235 {
12236 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12237 " [in module %s]"),
12238 dwp_file->name);
12239 }
12240 if (i == MAX_NR_V1_DWO_SECTIONS)
12241 {
12242 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12243 " [in module %s]"),
12244 dwp_file->name);
12245 }
12246
12247 /* It's easier for the rest of the code if we fake a struct dwo_file and
12248 have dwo_unit "live" in that. At least for now.
12249
12250 The DWP file can be made up of a random collection of CUs and TUs.
12251 However, for each CU + set of TUs that came from the same original DWO
12252 file, we can combine them back into a virtual DWO file to save space
12253 (fewer struct dwo_file objects to allocate). Remember that for really
12254 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12255
12256 std::string virtual_dwo_name =
12257 string_printf ("virtual-dwo/%d-%d-%d-%d",
12258 sections.abbrev.get_id (),
12259 sections.line.get_id (),
12260 sections.loc.get_id (),
12261 sections.str_offsets.get_id ());
12262 /* Can we use an existing virtual DWO file? */
12263 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12264 comp_dir);
12265 /* Create one if necessary. */
12266 if (*dwo_file_slot == NULL)
12267 {
12268 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12269 virtual_dwo_name.c_str ());
12270
12271 dwo_file = new struct dwo_file;
12272 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12273 dwo_file->comp_dir = comp_dir;
12274 dwo_file->sections.abbrev = sections.abbrev;
12275 dwo_file->sections.line = sections.line;
12276 dwo_file->sections.loc = sections.loc;
12277 dwo_file->sections.macinfo = sections.macinfo;
12278 dwo_file->sections.macro = sections.macro;
12279 dwo_file->sections.str_offsets = sections.str_offsets;
12280 /* The "str" section is global to the entire DWP file. */
12281 dwo_file->sections.str = dwp_file->sections.str;
12282 /* The info or types section is assigned below to dwo_unit,
12283 there's no need to record it in dwo_file.
12284 Also, we can't simply record type sections in dwo_file because
12285 we record a pointer into the vector in dwo_unit. As we collect more
12286 types we'll grow the vector and eventually have to reallocate space
12287 for it, invalidating all copies of pointers into the previous
12288 contents. */
12289 *dwo_file_slot = dwo_file;
12290 }
12291 else
12292 {
12293 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12294 virtual_dwo_name.c_str ());
12295
12296 dwo_file = (struct dwo_file *) *dwo_file_slot;
12297 }
12298
12299 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12300 dwo_unit->dwo_file = dwo_file;
12301 dwo_unit->signature = signature;
12302 dwo_unit->section =
12303 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12304 *dwo_unit->section = sections.info_or_types;
12305 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12306
12307 return dwo_unit;
12308 }
12309
12310 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12311 simplify them. Given a pointer to the containing section SECTION, and
12312 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12313 virtual section of just that piece. */
12314
12315 static struct dwarf2_section_info
12316 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12317 struct dwarf2_section_info *section,
12318 bfd_size_type offset, bfd_size_type size)
12319 {
12320 struct dwarf2_section_info result;
12321 asection *sectp;
12322
12323 gdb_assert (section != NULL);
12324 gdb_assert (!section->is_virtual);
12325
12326 memset (&result, 0, sizeof (result));
12327 result.s.containing_section = section;
12328 result.is_virtual = true;
12329
12330 if (size == 0)
12331 return result;
12332
12333 sectp = section->get_bfd_section ();
12334
12335 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12336 bounds of the real section. This is a pretty-rare event, so just
12337 flag an error (easier) instead of a warning and trying to cope. */
12338 if (sectp == NULL
12339 || offset + size > bfd_section_size (sectp))
12340 {
12341 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12342 " in section %s [in module %s]"),
12343 sectp ? bfd_section_name (sectp) : "<unknown>",
12344 objfile_name (per_objfile->objfile));
12345 }
12346
12347 result.virtual_offset = offset;
12348 result.size = size;
12349 return result;
12350 }
12351
12352 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12353 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12354 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12355 This is for DWP version 2 files. */
12356
12357 static struct dwo_unit *
12358 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12359 struct dwp_file *dwp_file,
12360 uint32_t unit_index,
12361 const char *comp_dir,
12362 ULONGEST signature, int is_debug_types)
12363 {
12364 const struct dwp_hash_table *dwp_htab =
12365 is_debug_types ? dwp_file->tus : dwp_file->cus;
12366 bfd *dbfd = dwp_file->dbfd.get ();
12367 const char *kind = is_debug_types ? "TU" : "CU";
12368 struct dwo_file *dwo_file;
12369 struct dwo_unit *dwo_unit;
12370 struct virtual_v2_or_v5_dwo_sections sections;
12371 void **dwo_file_slot;
12372 int i;
12373
12374 gdb_assert (dwp_file->version == 2);
12375
12376 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12377 kind, pulongest (unit_index), hex_string (signature),
12378 dwp_file->name);
12379
12380 /* Fetch the section offsets of this DWO unit. */
12381
12382 memset (&sections, 0, sizeof (sections));
12383
12384 for (i = 0; i < dwp_htab->nr_columns; ++i)
12385 {
12386 uint32_t offset = read_4_bytes (dbfd,
12387 dwp_htab->section_pool.v2.offsets
12388 + (((unit_index - 1) * dwp_htab->nr_columns
12389 + i)
12390 * sizeof (uint32_t)));
12391 uint32_t size = read_4_bytes (dbfd,
12392 dwp_htab->section_pool.v2.sizes
12393 + (((unit_index - 1) * dwp_htab->nr_columns
12394 + i)
12395 * sizeof (uint32_t)));
12396
12397 switch (dwp_htab->section_pool.v2.section_ids[i])
12398 {
12399 case DW_SECT_INFO:
12400 case DW_SECT_TYPES:
12401 sections.info_or_types_offset = offset;
12402 sections.info_or_types_size = size;
12403 break;
12404 case DW_SECT_ABBREV:
12405 sections.abbrev_offset = offset;
12406 sections.abbrev_size = size;
12407 break;
12408 case DW_SECT_LINE:
12409 sections.line_offset = offset;
12410 sections.line_size = size;
12411 break;
12412 case DW_SECT_LOC:
12413 sections.loc_offset = offset;
12414 sections.loc_size = size;
12415 break;
12416 case DW_SECT_STR_OFFSETS:
12417 sections.str_offsets_offset = offset;
12418 sections.str_offsets_size = size;
12419 break;
12420 case DW_SECT_MACINFO:
12421 sections.macinfo_offset = offset;
12422 sections.macinfo_size = size;
12423 break;
12424 case DW_SECT_MACRO:
12425 sections.macro_offset = offset;
12426 sections.macro_size = size;
12427 break;
12428 }
12429 }
12430
12431 /* It's easier for the rest of the code if we fake a struct dwo_file and
12432 have dwo_unit "live" in that. At least for now.
12433
12434 The DWP file can be made up of a random collection of CUs and TUs.
12435 However, for each CU + set of TUs that came from the same original DWO
12436 file, we can combine them back into a virtual DWO file to save space
12437 (fewer struct dwo_file objects to allocate). Remember that for really
12438 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12439
12440 std::string virtual_dwo_name =
12441 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12442 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12443 (long) (sections.line_size ? sections.line_offset : 0),
12444 (long) (sections.loc_size ? sections.loc_offset : 0),
12445 (long) (sections.str_offsets_size
12446 ? sections.str_offsets_offset : 0));
12447 /* Can we use an existing virtual DWO file? */
12448 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12449 comp_dir);
12450 /* Create one if necessary. */
12451 if (*dwo_file_slot == NULL)
12452 {
12453 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12454 virtual_dwo_name.c_str ());
12455
12456 dwo_file = new struct dwo_file;
12457 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12458 dwo_file->comp_dir = comp_dir;
12459 dwo_file->sections.abbrev =
12460 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12461 sections.abbrev_offset,
12462 sections.abbrev_size);
12463 dwo_file->sections.line =
12464 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12465 sections.line_offset,
12466 sections.line_size);
12467 dwo_file->sections.loc =
12468 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12469 sections.loc_offset, sections.loc_size);
12470 dwo_file->sections.macinfo =
12471 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12472 sections.macinfo_offset,
12473 sections.macinfo_size);
12474 dwo_file->sections.macro =
12475 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12476 sections.macro_offset,
12477 sections.macro_size);
12478 dwo_file->sections.str_offsets =
12479 create_dwp_v2_or_v5_section (per_objfile,
12480 &dwp_file->sections.str_offsets,
12481 sections.str_offsets_offset,
12482 sections.str_offsets_size);
12483 /* The "str" section is global to the entire DWP file. */
12484 dwo_file->sections.str = dwp_file->sections.str;
12485 /* The info or types section is assigned below to dwo_unit,
12486 there's no need to record it in dwo_file.
12487 Also, we can't simply record type sections in dwo_file because
12488 we record a pointer into the vector in dwo_unit. As we collect more
12489 types we'll grow the vector and eventually have to reallocate space
12490 for it, invalidating all copies of pointers into the previous
12491 contents. */
12492 *dwo_file_slot = dwo_file;
12493 }
12494 else
12495 {
12496 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12497 virtual_dwo_name.c_str ());
12498
12499 dwo_file = (struct dwo_file *) *dwo_file_slot;
12500 }
12501
12502 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12503 dwo_unit->dwo_file = dwo_file;
12504 dwo_unit->signature = signature;
12505 dwo_unit->section =
12506 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12507 *dwo_unit->section = create_dwp_v2_or_v5_section
12508 (per_objfile,
12509 is_debug_types
12510 ? &dwp_file->sections.types
12511 : &dwp_file->sections.info,
12512 sections.info_or_types_offset,
12513 sections.info_or_types_size);
12514 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12515
12516 return dwo_unit;
12517 }
12518
12519 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12520 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12521 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12522 This is for DWP version 5 files. */
12523
12524 static struct dwo_unit *
12525 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12526 struct dwp_file *dwp_file,
12527 uint32_t unit_index,
12528 const char *comp_dir,
12529 ULONGEST signature, int is_debug_types)
12530 {
12531 const struct dwp_hash_table *dwp_htab
12532 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12533 bfd *dbfd = dwp_file->dbfd.get ();
12534 const char *kind = is_debug_types ? "TU" : "CU";
12535 struct dwo_file *dwo_file;
12536 struct dwo_unit *dwo_unit;
12537 struct virtual_v2_or_v5_dwo_sections sections {};
12538 void **dwo_file_slot;
12539
12540 gdb_assert (dwp_file->version == 5);
12541
12542 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12543 kind, pulongest (unit_index), hex_string (signature),
12544 dwp_file->name);
12545
12546 /* Fetch the section offsets of this DWO unit. */
12547
12548 /* memset (&sections, 0, sizeof (sections)); */
12549
12550 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12551 {
12552 uint32_t offset = read_4_bytes (dbfd,
12553 dwp_htab->section_pool.v5.offsets
12554 + (((unit_index - 1)
12555 * dwp_htab->nr_columns
12556 + i)
12557 * sizeof (uint32_t)));
12558 uint32_t size = read_4_bytes (dbfd,
12559 dwp_htab->section_pool.v5.sizes
12560 + (((unit_index - 1) * dwp_htab->nr_columns
12561 + i)
12562 * sizeof (uint32_t)));
12563
12564 switch (dwp_htab->section_pool.v5.section_ids[i])
12565 {
12566 case DW_SECT_ABBREV_V5:
12567 sections.abbrev_offset = offset;
12568 sections.abbrev_size = size;
12569 break;
12570 case DW_SECT_INFO_V5:
12571 sections.info_or_types_offset = offset;
12572 sections.info_or_types_size = size;
12573 break;
12574 case DW_SECT_LINE_V5:
12575 sections.line_offset = offset;
12576 sections.line_size = size;
12577 break;
12578 case DW_SECT_LOCLISTS_V5:
12579 sections.loclists_offset = offset;
12580 sections.loclists_size = size;
12581 break;
12582 case DW_SECT_MACRO_V5:
12583 sections.macro_offset = offset;
12584 sections.macro_size = size;
12585 break;
12586 case DW_SECT_RNGLISTS_V5:
12587 sections.rnglists_offset = offset;
12588 sections.rnglists_size = size;
12589 break;
12590 case DW_SECT_STR_OFFSETS_V5:
12591 sections.str_offsets_offset = offset;
12592 sections.str_offsets_size = size;
12593 break;
12594 case DW_SECT_RESERVED_V5:
12595 default:
12596 break;
12597 }
12598 }
12599
12600 /* It's easier for the rest of the code if we fake a struct dwo_file and
12601 have dwo_unit "live" in that. At least for now.
12602
12603 The DWP file can be made up of a random collection of CUs and TUs.
12604 However, for each CU + set of TUs that came from the same original DWO
12605 file, we can combine them back into a virtual DWO file to save space
12606 (fewer struct dwo_file objects to allocate). Remember that for really
12607 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12608
12609 std::string virtual_dwo_name =
12610 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12611 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12612 (long) (sections.line_size ? sections.line_offset : 0),
12613 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12614 (long) (sections.str_offsets_size
12615 ? sections.str_offsets_offset : 0),
12616 (long) (sections.macro_size ? sections.macro_offset : 0),
12617 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12618 /* Can we use an existing virtual DWO file? */
12619 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12620 virtual_dwo_name.c_str (),
12621 comp_dir);
12622 /* Create one if necessary. */
12623 if (*dwo_file_slot == NULL)
12624 {
12625 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12626 virtual_dwo_name.c_str ());
12627
12628 dwo_file = new struct dwo_file;
12629 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12630 dwo_file->comp_dir = comp_dir;
12631 dwo_file->sections.abbrev =
12632 create_dwp_v2_or_v5_section (per_objfile,
12633 &dwp_file->sections.abbrev,
12634 sections.abbrev_offset,
12635 sections.abbrev_size);
12636 dwo_file->sections.line =
12637 create_dwp_v2_or_v5_section (per_objfile,
12638 &dwp_file->sections.line,
12639 sections.line_offset, sections.line_size);
12640 dwo_file->sections.macro =
12641 create_dwp_v2_or_v5_section (per_objfile,
12642 &dwp_file->sections.macro,
12643 sections.macro_offset,
12644 sections.macro_size);
12645 dwo_file->sections.loclists =
12646 create_dwp_v2_or_v5_section (per_objfile,
12647 &dwp_file->sections.loclists,
12648 sections.loclists_offset,
12649 sections.loclists_size);
12650 dwo_file->sections.rnglists =
12651 create_dwp_v2_or_v5_section (per_objfile,
12652 &dwp_file->sections.rnglists,
12653 sections.rnglists_offset,
12654 sections.rnglists_size);
12655 dwo_file->sections.str_offsets =
12656 create_dwp_v2_or_v5_section (per_objfile,
12657 &dwp_file->sections.str_offsets,
12658 sections.str_offsets_offset,
12659 sections.str_offsets_size);
12660 /* The "str" section is global to the entire DWP file. */
12661 dwo_file->sections.str = dwp_file->sections.str;
12662 /* The info or types section is assigned below to dwo_unit,
12663 there's no need to record it in dwo_file.
12664 Also, we can't simply record type sections in dwo_file because
12665 we record a pointer into the vector in dwo_unit. As we collect more
12666 types we'll grow the vector and eventually have to reallocate space
12667 for it, invalidating all copies of pointers into the previous
12668 contents. */
12669 *dwo_file_slot = dwo_file;
12670 }
12671 else
12672 {
12673 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12674 virtual_dwo_name.c_str ());
12675
12676 dwo_file = (struct dwo_file *) *dwo_file_slot;
12677 }
12678
12679 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12680 dwo_unit->dwo_file = dwo_file;
12681 dwo_unit->signature = signature;
12682 dwo_unit->section
12683 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12684 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12685 &dwp_file->sections.info,
12686 sections.info_or_types_offset,
12687 sections.info_or_types_size);
12688 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12689
12690 return dwo_unit;
12691 }
12692
12693 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12694 Returns NULL if the signature isn't found. */
12695
12696 static struct dwo_unit *
12697 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12698 struct dwp_file *dwp_file, const char *comp_dir,
12699 ULONGEST signature, int is_debug_types)
12700 {
12701 const struct dwp_hash_table *dwp_htab =
12702 is_debug_types ? dwp_file->tus : dwp_file->cus;
12703 bfd *dbfd = dwp_file->dbfd.get ();
12704 uint32_t mask = dwp_htab->nr_slots - 1;
12705 uint32_t hash = signature & mask;
12706 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12707 unsigned int i;
12708 void **slot;
12709 struct dwo_unit find_dwo_cu;
12710
12711 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12712 find_dwo_cu.signature = signature;
12713 slot = htab_find_slot (is_debug_types
12714 ? dwp_file->loaded_tus.get ()
12715 : dwp_file->loaded_cus.get (),
12716 &find_dwo_cu, INSERT);
12717
12718 if (*slot != NULL)
12719 return (struct dwo_unit *) *slot;
12720
12721 /* Use a for loop so that we don't loop forever on bad debug info. */
12722 for (i = 0; i < dwp_htab->nr_slots; ++i)
12723 {
12724 ULONGEST signature_in_table;
12725
12726 signature_in_table =
12727 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12728 if (signature_in_table == signature)
12729 {
12730 uint32_t unit_index =
12731 read_4_bytes (dbfd,
12732 dwp_htab->unit_table + hash * sizeof (uint32_t));
12733
12734 if (dwp_file->version == 1)
12735 {
12736 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12737 unit_index, comp_dir,
12738 signature, is_debug_types);
12739 }
12740 else if (dwp_file->version == 2)
12741 {
12742 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12743 unit_index, comp_dir,
12744 signature, is_debug_types);
12745 }
12746 else /* version == 5 */
12747 {
12748 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12749 unit_index, comp_dir,
12750 signature, is_debug_types);
12751 }
12752 return (struct dwo_unit *) *slot;
12753 }
12754 if (signature_in_table == 0)
12755 return NULL;
12756 hash = (hash + hash2) & mask;
12757 }
12758
12759 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12760 " [in module %s]"),
12761 dwp_file->name);
12762 }
12763
12764 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12765 Open the file specified by FILE_NAME and hand it off to BFD for
12766 preliminary analysis. Return a newly initialized bfd *, which
12767 includes a canonicalized copy of FILE_NAME.
12768 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12769 SEARCH_CWD is true if the current directory is to be searched.
12770 It will be searched before debug-file-directory.
12771 If successful, the file is added to the bfd include table of the
12772 objfile's bfd (see gdb_bfd_record_inclusion).
12773 If unable to find/open the file, return NULL.
12774 NOTE: This function is derived from symfile_bfd_open. */
12775
12776 static gdb_bfd_ref_ptr
12777 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12778 const char *file_name, int is_dwp, int search_cwd)
12779 {
12780 int desc;
12781 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12782 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12783 to debug_file_directory. */
12784 const char *search_path;
12785 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12786
12787 gdb::unique_xmalloc_ptr<char> search_path_holder;
12788 if (search_cwd)
12789 {
12790 if (*debug_file_directory != '\0')
12791 {
12792 search_path_holder.reset (concat (".", dirname_separator_string,
12793 debug_file_directory,
12794 (char *) NULL));
12795 search_path = search_path_holder.get ();
12796 }
12797 else
12798 search_path = ".";
12799 }
12800 else
12801 search_path = debug_file_directory;
12802
12803 openp_flags flags = OPF_RETURN_REALPATH;
12804 if (is_dwp)
12805 flags |= OPF_SEARCH_IN_PATH;
12806
12807 gdb::unique_xmalloc_ptr<char> absolute_name;
12808 desc = openp (search_path, flags, file_name,
12809 O_RDONLY | O_BINARY, &absolute_name);
12810 if (desc < 0)
12811 return NULL;
12812
12813 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12814 gnutarget, desc));
12815 if (sym_bfd == NULL)
12816 return NULL;
12817 bfd_set_cacheable (sym_bfd.get (), 1);
12818
12819 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12820 return NULL;
12821
12822 /* Success. Record the bfd as having been included by the objfile's bfd.
12823 This is important because things like demangled_names_hash lives in the
12824 objfile's per_bfd space and may have references to things like symbol
12825 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12826 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12827
12828 return sym_bfd;
12829 }
12830
12831 /* Try to open DWO file FILE_NAME.
12832 COMP_DIR is the DW_AT_comp_dir attribute.
12833 The result is the bfd handle of the file.
12834 If there is a problem finding or opening the file, return NULL.
12835 Upon success, the canonicalized path of the file is stored in the bfd,
12836 same as symfile_bfd_open. */
12837
12838 static gdb_bfd_ref_ptr
12839 open_dwo_file (dwarf2_per_objfile *per_objfile,
12840 const char *file_name, const char *comp_dir)
12841 {
12842 if (IS_ABSOLUTE_PATH (file_name))
12843 return try_open_dwop_file (per_objfile, file_name,
12844 0 /*is_dwp*/, 0 /*search_cwd*/);
12845
12846 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12847
12848 if (comp_dir != NULL)
12849 {
12850 gdb::unique_xmalloc_ptr<char> path_to_try
12851 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12852
12853 /* NOTE: If comp_dir is a relative path, this will also try the
12854 search path, which seems useful. */
12855 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12856 0 /*is_dwp*/,
12857 1 /*search_cwd*/));
12858 if (abfd != NULL)
12859 return abfd;
12860 }
12861
12862 /* That didn't work, try debug-file-directory, which, despite its name,
12863 is a list of paths. */
12864
12865 if (*debug_file_directory == '\0')
12866 return NULL;
12867
12868 return try_open_dwop_file (per_objfile, file_name,
12869 0 /*is_dwp*/, 1 /*search_cwd*/);
12870 }
12871
12872 /* This function is mapped across the sections and remembers the offset and
12873 size of each of the DWO debugging sections we are interested in. */
12874
12875 static void
12876 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12877 dwo_sections *dwo_sections)
12878 {
12879 const struct dwop_section_names *names = &dwop_section_names;
12880
12881 if (section_is_p (sectp->name, &names->abbrev_dwo))
12882 {
12883 dwo_sections->abbrev.s.section = sectp;
12884 dwo_sections->abbrev.size = bfd_section_size (sectp);
12885 }
12886 else if (section_is_p (sectp->name, &names->info_dwo))
12887 {
12888 dwo_sections->info.s.section = sectp;
12889 dwo_sections->info.size = bfd_section_size (sectp);
12890 }
12891 else if (section_is_p (sectp->name, &names->line_dwo))
12892 {
12893 dwo_sections->line.s.section = sectp;
12894 dwo_sections->line.size = bfd_section_size (sectp);
12895 }
12896 else if (section_is_p (sectp->name, &names->loc_dwo))
12897 {
12898 dwo_sections->loc.s.section = sectp;
12899 dwo_sections->loc.size = bfd_section_size (sectp);
12900 }
12901 else if (section_is_p (sectp->name, &names->loclists_dwo))
12902 {
12903 dwo_sections->loclists.s.section = sectp;
12904 dwo_sections->loclists.size = bfd_section_size (sectp);
12905 }
12906 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12907 {
12908 dwo_sections->macinfo.s.section = sectp;
12909 dwo_sections->macinfo.size = bfd_section_size (sectp);
12910 }
12911 else if (section_is_p (sectp->name, &names->macro_dwo))
12912 {
12913 dwo_sections->macro.s.section = sectp;
12914 dwo_sections->macro.size = bfd_section_size (sectp);
12915 }
12916 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12917 {
12918 dwo_sections->rnglists.s.section = sectp;
12919 dwo_sections->rnglists.size = bfd_section_size (sectp);
12920 }
12921 else if (section_is_p (sectp->name, &names->str_dwo))
12922 {
12923 dwo_sections->str.s.section = sectp;
12924 dwo_sections->str.size = bfd_section_size (sectp);
12925 }
12926 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12927 {
12928 dwo_sections->str_offsets.s.section = sectp;
12929 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12930 }
12931 else if (section_is_p (sectp->name, &names->types_dwo))
12932 {
12933 struct dwarf2_section_info type_section;
12934
12935 memset (&type_section, 0, sizeof (type_section));
12936 type_section.s.section = sectp;
12937 type_section.size = bfd_section_size (sectp);
12938 dwo_sections->types.push_back (type_section);
12939 }
12940 }
12941
12942 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12943 by PER_CU. This is for the non-DWP case.
12944 The result is NULL if DWO_NAME can't be found. */
12945
12946 static struct dwo_file *
12947 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12948 const char *comp_dir)
12949 {
12950 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12951
12952 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12953 if (dbfd == NULL)
12954 {
12955 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12956
12957 return NULL;
12958 }
12959
12960 dwo_file_up dwo_file (new struct dwo_file);
12961 dwo_file->dwo_name = dwo_name;
12962 dwo_file->comp_dir = comp_dir;
12963 dwo_file->dbfd = std::move (dbfd);
12964
12965 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12966 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12967 &dwo_file->sections);
12968
12969 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12970 dwo_file->cus);
12971
12972 if (cu->per_cu->dwarf_version < 5)
12973 {
12974 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12975 dwo_file->sections.types, dwo_file->tus);
12976 }
12977 else
12978 {
12979 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12980 &dwo_file->sections.info, dwo_file->tus,
12981 rcuh_kind::TYPE);
12982 }
12983
12984 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12985
12986 return dwo_file.release ();
12987 }
12988
12989 /* This function is mapped across the sections and remembers the offset and
12990 size of each of the DWP debugging sections common to version 1 and 2 that
12991 we are interested in. */
12992
12993 static void
12994 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12995 dwp_file *dwp_file)
12996 {
12997 const struct dwop_section_names *names = &dwop_section_names;
12998 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12999
13000 /* Record the ELF section number for later lookup: this is what the
13001 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13002 gdb_assert (elf_section_nr < dwp_file->num_sections);
13003 dwp_file->elf_sections[elf_section_nr] = sectp;
13004
13005 /* Look for specific sections that we need. */
13006 if (section_is_p (sectp->name, &names->str_dwo))
13007 {
13008 dwp_file->sections.str.s.section = sectp;
13009 dwp_file->sections.str.size = bfd_section_size (sectp);
13010 }
13011 else if (section_is_p (sectp->name, &names->cu_index))
13012 {
13013 dwp_file->sections.cu_index.s.section = sectp;
13014 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
13015 }
13016 else if (section_is_p (sectp->name, &names->tu_index))
13017 {
13018 dwp_file->sections.tu_index.s.section = sectp;
13019 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
13020 }
13021 }
13022
13023 /* This function is mapped across the sections and remembers the offset and
13024 size of each of the DWP version 2 debugging sections that we are interested
13025 in. This is split into a separate function because we don't know if we
13026 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13027
13028 static void
13029 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13030 {
13031 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13032 const struct dwop_section_names *names = &dwop_section_names;
13033 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13034
13035 /* Record the ELF section number for later lookup: this is what the
13036 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13037 gdb_assert (elf_section_nr < dwp_file->num_sections);
13038 dwp_file->elf_sections[elf_section_nr] = sectp;
13039
13040 /* Look for specific sections that we need. */
13041 if (section_is_p (sectp->name, &names->abbrev_dwo))
13042 {
13043 dwp_file->sections.abbrev.s.section = sectp;
13044 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13045 }
13046 else if (section_is_p (sectp->name, &names->info_dwo))
13047 {
13048 dwp_file->sections.info.s.section = sectp;
13049 dwp_file->sections.info.size = bfd_section_size (sectp);
13050 }
13051 else if (section_is_p (sectp->name, &names->line_dwo))
13052 {
13053 dwp_file->sections.line.s.section = sectp;
13054 dwp_file->sections.line.size = bfd_section_size (sectp);
13055 }
13056 else if (section_is_p (sectp->name, &names->loc_dwo))
13057 {
13058 dwp_file->sections.loc.s.section = sectp;
13059 dwp_file->sections.loc.size = bfd_section_size (sectp);
13060 }
13061 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13062 {
13063 dwp_file->sections.macinfo.s.section = sectp;
13064 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13065 }
13066 else if (section_is_p (sectp->name, &names->macro_dwo))
13067 {
13068 dwp_file->sections.macro.s.section = sectp;
13069 dwp_file->sections.macro.size = bfd_section_size (sectp);
13070 }
13071 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13072 {
13073 dwp_file->sections.str_offsets.s.section = sectp;
13074 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13075 }
13076 else if (section_is_p (sectp->name, &names->types_dwo))
13077 {
13078 dwp_file->sections.types.s.section = sectp;
13079 dwp_file->sections.types.size = bfd_section_size (sectp);
13080 }
13081 }
13082
13083 /* This function is mapped across the sections and remembers the offset and
13084 size of each of the DWP version 5 debugging sections that we are interested
13085 in. This is split into a separate function because we don't know if we
13086 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13087
13088 static void
13089 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13090 {
13091 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13092 const struct dwop_section_names *names = &dwop_section_names;
13093 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13094
13095 /* Record the ELF section number for later lookup: this is what the
13096 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13097 gdb_assert (elf_section_nr < dwp_file->num_sections);
13098 dwp_file->elf_sections[elf_section_nr] = sectp;
13099
13100 /* Look for specific sections that we need. */
13101 if (section_is_p (sectp->name, &names->abbrev_dwo))
13102 {
13103 dwp_file->sections.abbrev.s.section = sectp;
13104 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13105 }
13106 else if (section_is_p (sectp->name, &names->info_dwo))
13107 {
13108 dwp_file->sections.info.s.section = sectp;
13109 dwp_file->sections.info.size = bfd_section_size (sectp);
13110 }
13111 else if (section_is_p (sectp->name, &names->line_dwo))
13112 {
13113 dwp_file->sections.line.s.section = sectp;
13114 dwp_file->sections.line.size = bfd_section_size (sectp);
13115 }
13116 else if (section_is_p (sectp->name, &names->loclists_dwo))
13117 {
13118 dwp_file->sections.loclists.s.section = sectp;
13119 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13120 }
13121 else if (section_is_p (sectp->name, &names->macro_dwo))
13122 {
13123 dwp_file->sections.macro.s.section = sectp;
13124 dwp_file->sections.macro.size = bfd_section_size (sectp);
13125 }
13126 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13127 {
13128 dwp_file->sections.rnglists.s.section = sectp;
13129 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13130 }
13131 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13132 {
13133 dwp_file->sections.str_offsets.s.section = sectp;
13134 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13135 }
13136 }
13137
13138 /* Hash function for dwp_file loaded CUs/TUs. */
13139
13140 static hashval_t
13141 hash_dwp_loaded_cutus (const void *item)
13142 {
13143 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13144
13145 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13146 return dwo_unit->signature;
13147 }
13148
13149 /* Equality function for dwp_file loaded CUs/TUs. */
13150
13151 static int
13152 eq_dwp_loaded_cutus (const void *a, const void *b)
13153 {
13154 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13155 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13156
13157 return dua->signature == dub->signature;
13158 }
13159
13160 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13161
13162 static htab_up
13163 allocate_dwp_loaded_cutus_table ()
13164 {
13165 return htab_up (htab_create_alloc (3,
13166 hash_dwp_loaded_cutus,
13167 eq_dwp_loaded_cutus,
13168 NULL, xcalloc, xfree));
13169 }
13170
13171 /* Try to open DWP file FILE_NAME.
13172 The result is the bfd handle of the file.
13173 If there is a problem finding or opening the file, return NULL.
13174 Upon success, the canonicalized path of the file is stored in the bfd,
13175 same as symfile_bfd_open. */
13176
13177 static gdb_bfd_ref_ptr
13178 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13179 {
13180 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13181 1 /*is_dwp*/,
13182 1 /*search_cwd*/));
13183 if (abfd != NULL)
13184 return abfd;
13185
13186 /* Work around upstream bug 15652.
13187 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13188 [Whether that's a "bug" is debatable, but it is getting in our way.]
13189 We have no real idea where the dwp file is, because gdb's realpath-ing
13190 of the executable's path may have discarded the needed info.
13191 [IWBN if the dwp file name was recorded in the executable, akin to
13192 .gnu_debuglink, but that doesn't exist yet.]
13193 Strip the directory from FILE_NAME and search again. */
13194 if (*debug_file_directory != '\0')
13195 {
13196 /* Don't implicitly search the current directory here.
13197 If the user wants to search "." to handle this case,
13198 it must be added to debug-file-directory. */
13199 return try_open_dwop_file (per_objfile, lbasename (file_name),
13200 1 /*is_dwp*/,
13201 0 /*search_cwd*/);
13202 }
13203
13204 return NULL;
13205 }
13206
13207 /* Initialize the use of the DWP file for the current objfile.
13208 By convention the name of the DWP file is ${objfile}.dwp.
13209 The result is NULL if it can't be found. */
13210
13211 static std::unique_ptr<struct dwp_file>
13212 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13213 {
13214 struct objfile *objfile = per_objfile->objfile;
13215
13216 /* Try to find first .dwp for the binary file before any symbolic links
13217 resolving. */
13218
13219 /* If the objfile is a debug file, find the name of the real binary
13220 file and get the name of dwp file from there. */
13221 std::string dwp_name;
13222 if (objfile->separate_debug_objfile_backlink != NULL)
13223 {
13224 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13225 const char *backlink_basename = lbasename (backlink->original_name);
13226
13227 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13228 }
13229 else
13230 dwp_name = objfile->original_name;
13231
13232 dwp_name += ".dwp";
13233
13234 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13235 if (dbfd == NULL
13236 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13237 {
13238 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13239 dwp_name = objfile_name (objfile);
13240 dwp_name += ".dwp";
13241 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13242 }
13243
13244 if (dbfd == NULL)
13245 {
13246 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13247
13248 return std::unique_ptr<dwp_file> ();
13249 }
13250
13251 const char *name = bfd_get_filename (dbfd.get ());
13252 std::unique_ptr<struct dwp_file> dwp_file
13253 (new struct dwp_file (name, std::move (dbfd)));
13254
13255 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13256 dwp_file->elf_sections =
13257 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13258 dwp_file->num_sections, asection *);
13259
13260 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13261 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13262 dwp_file.get ());
13263
13264 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13265
13266 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13267
13268 /* The DWP file version is stored in the hash table. Oh well. */
13269 if (dwp_file->cus && dwp_file->tus
13270 && dwp_file->cus->version != dwp_file->tus->version)
13271 {
13272 /* Technically speaking, we should try to limp along, but this is
13273 pretty bizarre. We use pulongest here because that's the established
13274 portability solution (e.g, we cannot use %u for uint32_t). */
13275 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13276 " TU version %s [in DWP file %s]"),
13277 pulongest (dwp_file->cus->version),
13278 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13279 }
13280
13281 if (dwp_file->cus)
13282 dwp_file->version = dwp_file->cus->version;
13283 else if (dwp_file->tus)
13284 dwp_file->version = dwp_file->tus->version;
13285 else
13286 dwp_file->version = 2;
13287
13288 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13289 {
13290 if (dwp_file->version == 2)
13291 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13292 dwp_file.get ());
13293 else
13294 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13295 dwp_file.get ());
13296 }
13297
13298 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13299 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13300
13301 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13302 dwarf_read_debug_printf (" %s CUs, %s TUs",
13303 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13304 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13305
13306 return dwp_file;
13307 }
13308
13309 /* Wrapper around open_and_init_dwp_file, only open it once. */
13310
13311 static struct dwp_file *
13312 get_dwp_file (dwarf2_per_objfile *per_objfile)
13313 {
13314 if (!per_objfile->per_bfd->dwp_checked)
13315 {
13316 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13317 per_objfile->per_bfd->dwp_checked = 1;
13318 }
13319 return per_objfile->per_bfd->dwp_file.get ();
13320 }
13321
13322 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13323 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13324 or in the DWP file for the objfile, referenced by THIS_UNIT.
13325 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13326 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13327
13328 This is called, for example, when wanting to read a variable with a
13329 complex location. Therefore we don't want to do file i/o for every call.
13330 Therefore we don't want to look for a DWO file on every call.
13331 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13332 then we check if we've already seen DWO_NAME, and only THEN do we check
13333 for a DWO file.
13334
13335 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13336 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13337
13338 static struct dwo_unit *
13339 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13340 ULONGEST signature, int is_debug_types)
13341 {
13342 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13343 struct objfile *objfile = per_objfile->objfile;
13344 const char *kind = is_debug_types ? "TU" : "CU";
13345 void **dwo_file_slot;
13346 struct dwo_file *dwo_file;
13347 struct dwp_file *dwp_file;
13348
13349 /* First see if there's a DWP file.
13350 If we have a DWP file but didn't find the DWO inside it, don't
13351 look for the original DWO file. It makes gdb behave differently
13352 depending on whether one is debugging in the build tree. */
13353
13354 dwp_file = get_dwp_file (per_objfile);
13355 if (dwp_file != NULL)
13356 {
13357 const struct dwp_hash_table *dwp_htab =
13358 is_debug_types ? dwp_file->tus : dwp_file->cus;
13359
13360 if (dwp_htab != NULL)
13361 {
13362 struct dwo_unit *dwo_cutu =
13363 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13364 is_debug_types);
13365
13366 if (dwo_cutu != NULL)
13367 {
13368 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13369 kind, hex_string (signature),
13370 host_address_to_string (dwo_cutu));
13371
13372 return dwo_cutu;
13373 }
13374 }
13375 }
13376 else
13377 {
13378 /* No DWP file, look for the DWO file. */
13379
13380 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13381 if (*dwo_file_slot == NULL)
13382 {
13383 /* Read in the file and build a table of the CUs/TUs it contains. */
13384 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13385 }
13386 /* NOTE: This will be NULL if unable to open the file. */
13387 dwo_file = (struct dwo_file *) *dwo_file_slot;
13388
13389 if (dwo_file != NULL)
13390 {
13391 struct dwo_unit *dwo_cutu = NULL;
13392
13393 if (is_debug_types && dwo_file->tus)
13394 {
13395 struct dwo_unit find_dwo_cutu;
13396
13397 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13398 find_dwo_cutu.signature = signature;
13399 dwo_cutu
13400 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13401 &find_dwo_cutu);
13402 }
13403 else if (!is_debug_types && dwo_file->cus)
13404 {
13405 struct dwo_unit find_dwo_cutu;
13406
13407 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13408 find_dwo_cutu.signature = signature;
13409 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13410 &find_dwo_cutu);
13411 }
13412
13413 if (dwo_cutu != NULL)
13414 {
13415 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13416 kind, dwo_name, hex_string (signature),
13417 host_address_to_string (dwo_cutu));
13418
13419 return dwo_cutu;
13420 }
13421 }
13422 }
13423
13424 /* We didn't find it. This could mean a dwo_id mismatch, or
13425 someone deleted the DWO/DWP file, or the search path isn't set up
13426 correctly to find the file. */
13427
13428 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13429 kind, dwo_name, hex_string (signature));
13430
13431 /* This is a warning and not a complaint because it can be caused by
13432 pilot error (e.g., user accidentally deleting the DWO). */
13433 {
13434 /* Print the name of the DWP file if we looked there, helps the user
13435 better diagnose the problem. */
13436 std::string dwp_text;
13437
13438 if (dwp_file != NULL)
13439 dwp_text = string_printf (" [in DWP file %s]",
13440 lbasename (dwp_file->name));
13441
13442 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13443 " [in module %s]"),
13444 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13445 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13446 }
13447 return NULL;
13448 }
13449
13450 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13451 See lookup_dwo_cutu_unit for details. */
13452
13453 static struct dwo_unit *
13454 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13455 ULONGEST signature)
13456 {
13457 gdb_assert (!cu->per_cu->is_debug_types);
13458
13459 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13460 }
13461
13462 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13463 See lookup_dwo_cutu_unit for details. */
13464
13465 static struct dwo_unit *
13466 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13467 {
13468 gdb_assert (cu->per_cu->is_debug_types);
13469
13470 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13471
13472 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13473 }
13474
13475 /* Traversal function for queue_and_load_all_dwo_tus. */
13476
13477 static int
13478 queue_and_load_dwo_tu (void **slot, void *info)
13479 {
13480 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13481 dwarf2_cu *cu = (dwarf2_cu *) info;
13482 ULONGEST signature = dwo_unit->signature;
13483 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13484
13485 if (sig_type != NULL)
13486 {
13487 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13488
13489 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13490 a real dependency of PER_CU on SIG_TYPE. That is detected later
13491 while processing PER_CU. */
13492 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13493 load_full_type_unit (sig_cu, cu->per_objfile);
13494 cu->per_cu->imported_symtabs_push (sig_cu);
13495 }
13496
13497 return 1;
13498 }
13499
13500 /* Queue all TUs contained in the DWO of CU to be read in.
13501 The DWO may have the only definition of the type, though it may not be
13502 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13503 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13504
13505 static void
13506 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13507 {
13508 struct dwo_unit *dwo_unit;
13509 struct dwo_file *dwo_file;
13510
13511 gdb_assert (cu != nullptr);
13512 gdb_assert (!cu->per_cu->is_debug_types);
13513 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13514
13515 dwo_unit = cu->dwo_unit;
13516 gdb_assert (dwo_unit != NULL);
13517
13518 dwo_file = dwo_unit->dwo_file;
13519 if (dwo_file->tus != NULL)
13520 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13521 }
13522
13523 /* Read in various DIEs. */
13524
13525 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13526 Inherit only the children of the DW_AT_abstract_origin DIE not being
13527 already referenced by DW_AT_abstract_origin from the children of the
13528 current DIE. */
13529
13530 static void
13531 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13532 {
13533 struct die_info *child_die;
13534 sect_offset *offsetp;
13535 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13536 struct die_info *origin_die;
13537 /* Iterator of the ORIGIN_DIE children. */
13538 struct die_info *origin_child_die;
13539 struct attribute *attr;
13540 struct dwarf2_cu *origin_cu;
13541 struct pending **origin_previous_list_in_scope;
13542
13543 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13544 if (!attr)
13545 return;
13546
13547 /* Note that following die references may follow to a die in a
13548 different cu. */
13549
13550 origin_cu = cu;
13551 origin_die = follow_die_ref (die, attr, &origin_cu);
13552
13553 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13554 symbols in. */
13555 origin_previous_list_in_scope = origin_cu->list_in_scope;
13556 origin_cu->list_in_scope = cu->list_in_scope;
13557
13558 if (die->tag != origin_die->tag
13559 && !(die->tag == DW_TAG_inlined_subroutine
13560 && origin_die->tag == DW_TAG_subprogram))
13561 complaint (_("DIE %s and its abstract origin %s have different tags"),
13562 sect_offset_str (die->sect_off),
13563 sect_offset_str (origin_die->sect_off));
13564
13565 std::vector<sect_offset> offsets;
13566
13567 for (child_die = die->child;
13568 child_die && child_die->tag;
13569 child_die = child_die->sibling)
13570 {
13571 struct die_info *child_origin_die;
13572 struct dwarf2_cu *child_origin_cu;
13573
13574 /* We are trying to process concrete instance entries:
13575 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13576 it's not relevant to our analysis here. i.e. detecting DIEs that are
13577 present in the abstract instance but not referenced in the concrete
13578 one. */
13579 if (child_die->tag == DW_TAG_call_site
13580 || child_die->tag == DW_TAG_GNU_call_site)
13581 continue;
13582
13583 /* For each CHILD_DIE, find the corresponding child of
13584 ORIGIN_DIE. If there is more than one layer of
13585 DW_AT_abstract_origin, follow them all; there shouldn't be,
13586 but GCC versions at least through 4.4 generate this (GCC PR
13587 40573). */
13588 child_origin_die = child_die;
13589 child_origin_cu = cu;
13590 while (1)
13591 {
13592 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13593 child_origin_cu);
13594 if (attr == NULL)
13595 break;
13596 child_origin_die = follow_die_ref (child_origin_die, attr,
13597 &child_origin_cu);
13598 }
13599
13600 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13601 counterpart may exist. */
13602 if (child_origin_die != child_die)
13603 {
13604 if (child_die->tag != child_origin_die->tag
13605 && !(child_die->tag == DW_TAG_inlined_subroutine
13606 && child_origin_die->tag == DW_TAG_subprogram))
13607 complaint (_("Child DIE %s and its abstract origin %s have "
13608 "different tags"),
13609 sect_offset_str (child_die->sect_off),
13610 sect_offset_str (child_origin_die->sect_off));
13611 if (child_origin_die->parent != origin_die)
13612 complaint (_("Child DIE %s and its abstract origin %s have "
13613 "different parents"),
13614 sect_offset_str (child_die->sect_off),
13615 sect_offset_str (child_origin_die->sect_off));
13616 else
13617 offsets.push_back (child_origin_die->sect_off);
13618 }
13619 }
13620 std::sort (offsets.begin (), offsets.end ());
13621 sect_offset *offsets_end = offsets.data () + offsets.size ();
13622 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13623 if (offsetp[-1] == *offsetp)
13624 complaint (_("Multiple children of DIE %s refer "
13625 "to DIE %s as their abstract origin"),
13626 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13627
13628 offsetp = offsets.data ();
13629 origin_child_die = origin_die->child;
13630 while (origin_child_die && origin_child_die->tag)
13631 {
13632 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13633 while (offsetp < offsets_end
13634 && *offsetp < origin_child_die->sect_off)
13635 offsetp++;
13636 if (offsetp >= offsets_end
13637 || *offsetp > origin_child_die->sect_off)
13638 {
13639 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13640 Check whether we're already processing ORIGIN_CHILD_DIE.
13641 This can happen with mutually referenced abstract_origins.
13642 PR 16581. */
13643 if (!origin_child_die->in_process)
13644 process_die (origin_child_die, origin_cu);
13645 }
13646 origin_child_die = origin_child_die->sibling;
13647 }
13648 origin_cu->list_in_scope = origin_previous_list_in_scope;
13649
13650 if (cu != origin_cu)
13651 compute_delayed_physnames (origin_cu);
13652 }
13653
13654 static void
13655 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13656 {
13657 struct objfile *objfile = cu->per_objfile->objfile;
13658 struct gdbarch *gdbarch = objfile->arch ();
13659 struct context_stack *newobj;
13660 CORE_ADDR lowpc;
13661 CORE_ADDR highpc;
13662 struct die_info *child_die;
13663 struct attribute *attr, *call_line, *call_file;
13664 const char *name;
13665 CORE_ADDR baseaddr;
13666 struct block *block;
13667 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13668 std::vector<struct symbol *> template_args;
13669 struct template_symbol *templ_func = NULL;
13670
13671 if (inlined_func)
13672 {
13673 /* If we do not have call site information, we can't show the
13674 caller of this inlined function. That's too confusing, so
13675 only use the scope for local variables. */
13676 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13677 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13678 if (call_line == NULL || call_file == NULL)
13679 {
13680 read_lexical_block_scope (die, cu);
13681 return;
13682 }
13683 }
13684
13685 baseaddr = objfile->text_section_offset ();
13686
13687 name = dwarf2_name (die, cu);
13688
13689 /* Ignore functions with missing or empty names. These are actually
13690 illegal according to the DWARF standard. */
13691 if (name == NULL)
13692 {
13693 complaint (_("missing name for subprogram DIE at %s"),
13694 sect_offset_str (die->sect_off));
13695 return;
13696 }
13697
13698 /* Ignore functions with missing or invalid low and high pc attributes. */
13699 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13700 <= PC_BOUNDS_INVALID)
13701 {
13702 attr = dwarf2_attr (die, DW_AT_external, cu);
13703 if (attr == nullptr || !attr->as_boolean ())
13704 complaint (_("cannot get low and high bounds "
13705 "for subprogram DIE at %s"),
13706 sect_offset_str (die->sect_off));
13707 return;
13708 }
13709
13710 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13711 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13712
13713 /* If we have any template arguments, then we must allocate a
13714 different sort of symbol. */
13715 for (child_die = die->child; child_die; child_die = child_die->sibling)
13716 {
13717 if (child_die->tag == DW_TAG_template_type_param
13718 || child_die->tag == DW_TAG_template_value_param)
13719 {
13720 templ_func = new (&objfile->objfile_obstack) template_symbol;
13721 templ_func->subclass = SYMBOL_TEMPLATE;
13722 break;
13723 }
13724 }
13725
13726 newobj = cu->get_builder ()->push_context (0, lowpc);
13727 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13728 (struct symbol *) templ_func);
13729
13730 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13731 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13732 cu->language);
13733
13734 /* If there is a location expression for DW_AT_frame_base, record
13735 it. */
13736 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13737 if (attr != nullptr)
13738 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13739
13740 /* If there is a location for the static link, record it. */
13741 newobj->static_link = NULL;
13742 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13743 if (attr != nullptr)
13744 {
13745 newobj->static_link
13746 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13747 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13748 cu->addr_type ());
13749 }
13750
13751 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13752
13753 if (die->child != NULL)
13754 {
13755 child_die = die->child;
13756 while (child_die && child_die->tag)
13757 {
13758 if (child_die->tag == DW_TAG_template_type_param
13759 || child_die->tag == DW_TAG_template_value_param)
13760 {
13761 struct symbol *arg = new_symbol (child_die, NULL, cu);
13762
13763 if (arg != NULL)
13764 template_args.push_back (arg);
13765 }
13766 else
13767 process_die (child_die, cu);
13768 child_die = child_die->sibling;
13769 }
13770 }
13771
13772 inherit_abstract_dies (die, cu);
13773
13774 /* If we have a DW_AT_specification, we might need to import using
13775 directives from the context of the specification DIE. See the
13776 comment in determine_prefix. */
13777 if (cu->language == language_cplus
13778 && dwarf2_attr (die, DW_AT_specification, cu))
13779 {
13780 struct dwarf2_cu *spec_cu = cu;
13781 struct die_info *spec_die = die_specification (die, &spec_cu);
13782
13783 while (spec_die)
13784 {
13785 child_die = spec_die->child;
13786 while (child_die && child_die->tag)
13787 {
13788 if (child_die->tag == DW_TAG_imported_module)
13789 process_die (child_die, spec_cu);
13790 child_die = child_die->sibling;
13791 }
13792
13793 /* In some cases, GCC generates specification DIEs that
13794 themselves contain DW_AT_specification attributes. */
13795 spec_die = die_specification (spec_die, &spec_cu);
13796 }
13797 }
13798
13799 struct context_stack cstk = cu->get_builder ()->pop_context ();
13800 /* Make a block for the local symbols within. */
13801 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13802 cstk.static_link, lowpc, highpc);
13803
13804 /* For C++, set the block's scope. */
13805 if ((cu->language == language_cplus
13806 || cu->language == language_fortran
13807 || cu->language == language_d
13808 || cu->language == language_rust)
13809 && cu->processing_has_namespace_info)
13810 block_set_scope (block, determine_prefix (die, cu),
13811 &objfile->objfile_obstack);
13812
13813 /* If we have address ranges, record them. */
13814 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13815
13816 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13817
13818 /* Attach template arguments to function. */
13819 if (!template_args.empty ())
13820 {
13821 gdb_assert (templ_func != NULL);
13822
13823 templ_func->n_template_arguments = template_args.size ();
13824 templ_func->template_arguments
13825 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13826 templ_func->n_template_arguments);
13827 memcpy (templ_func->template_arguments,
13828 template_args.data (),
13829 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13830
13831 /* Make sure that the symtab is set on the new symbols. Even
13832 though they don't appear in this symtab directly, other parts
13833 of gdb assume that symbols do, and this is reasonably
13834 true. */
13835 for (symbol *sym : template_args)
13836 symbol_set_symtab (sym, symbol_symtab (templ_func));
13837 }
13838
13839 /* In C++, we can have functions nested inside functions (e.g., when
13840 a function declares a class that has methods). This means that
13841 when we finish processing a function scope, we may need to go
13842 back to building a containing block's symbol lists. */
13843 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13844 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13845
13846 /* If we've finished processing a top-level function, subsequent
13847 symbols go in the file symbol list. */
13848 if (cu->get_builder ()->outermost_context_p ())
13849 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13850 }
13851
13852 /* Process all the DIES contained within a lexical block scope. Start
13853 a new scope, process the dies, and then close the scope. */
13854
13855 static void
13856 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13857 {
13858 struct objfile *objfile = cu->per_objfile->objfile;
13859 struct gdbarch *gdbarch = objfile->arch ();
13860 CORE_ADDR lowpc, highpc;
13861 struct die_info *child_die;
13862 CORE_ADDR baseaddr;
13863
13864 baseaddr = objfile->text_section_offset ();
13865
13866 /* Ignore blocks with missing or invalid low and high pc attributes. */
13867 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13868 as multiple lexical blocks? Handling children in a sane way would
13869 be nasty. Might be easier to properly extend generic blocks to
13870 describe ranges. */
13871 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13872 {
13873 case PC_BOUNDS_NOT_PRESENT:
13874 /* DW_TAG_lexical_block has no attributes, process its children as if
13875 there was no wrapping by that DW_TAG_lexical_block.
13876 GCC does no longer produces such DWARF since GCC r224161. */
13877 for (child_die = die->child;
13878 child_die != NULL && child_die->tag;
13879 child_die = child_die->sibling)
13880 {
13881 /* We might already be processing this DIE. This can happen
13882 in an unusual circumstance -- where a subroutine A
13883 appears lexically in another subroutine B, but A actually
13884 inlines B. The recursion is broken here, rather than in
13885 inherit_abstract_dies, because it seems better to simply
13886 drop concrete children here. */
13887 if (!child_die->in_process)
13888 process_die (child_die, cu);
13889 }
13890 return;
13891 case PC_BOUNDS_INVALID:
13892 return;
13893 }
13894 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13895 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13896
13897 cu->get_builder ()->push_context (0, lowpc);
13898 if (die->child != NULL)
13899 {
13900 child_die = die->child;
13901 while (child_die && child_die->tag)
13902 {
13903 process_die (child_die, cu);
13904 child_die = child_die->sibling;
13905 }
13906 }
13907 inherit_abstract_dies (die, cu);
13908 struct context_stack cstk = cu->get_builder ()->pop_context ();
13909
13910 if (*cu->get_builder ()->get_local_symbols () != NULL
13911 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13912 {
13913 struct block *block
13914 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13915 cstk.start_addr, highpc);
13916
13917 /* Note that recording ranges after traversing children, as we
13918 do here, means that recording a parent's ranges entails
13919 walking across all its children's ranges as they appear in
13920 the address map, which is quadratic behavior.
13921
13922 It would be nicer to record the parent's ranges before
13923 traversing its children, simply overriding whatever you find
13924 there. But since we don't even decide whether to create a
13925 block until after we've traversed its children, that's hard
13926 to do. */
13927 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13928 }
13929 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13930 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13931 }
13932
13933 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13934
13935 static void
13936 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13937 {
13938 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13939 struct objfile *objfile = per_objfile->objfile;
13940 struct gdbarch *gdbarch = objfile->arch ();
13941 CORE_ADDR pc, baseaddr;
13942 struct attribute *attr;
13943 struct call_site *call_site, call_site_local;
13944 void **slot;
13945 int nparams;
13946 struct die_info *child_die;
13947
13948 baseaddr = objfile->text_section_offset ();
13949
13950 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13951 if (attr == NULL)
13952 {
13953 /* This was a pre-DWARF-5 GNU extension alias
13954 for DW_AT_call_return_pc. */
13955 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13956 }
13957 if (!attr)
13958 {
13959 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13960 "DIE %s [in module %s]"),
13961 sect_offset_str (die->sect_off), objfile_name (objfile));
13962 return;
13963 }
13964 pc = attr->as_address () + baseaddr;
13965 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13966
13967 if (cu->call_site_htab == NULL)
13968 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13969 NULL, &objfile->objfile_obstack,
13970 hashtab_obstack_allocate, NULL);
13971 call_site_local.pc = pc;
13972 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13973 if (*slot != NULL)
13974 {
13975 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13976 "DIE %s [in module %s]"),
13977 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13978 objfile_name (objfile));
13979 return;
13980 }
13981
13982 /* Count parameters at the caller. */
13983
13984 nparams = 0;
13985 for (child_die = die->child; child_die && child_die->tag;
13986 child_die = child_die->sibling)
13987 {
13988 if (child_die->tag != DW_TAG_call_site_parameter
13989 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13990 {
13991 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13992 "DW_TAG_call_site child DIE %s [in module %s]"),
13993 child_die->tag, sect_offset_str (child_die->sect_off),
13994 objfile_name (objfile));
13995 continue;
13996 }
13997
13998 nparams++;
13999 }
14000
14001 call_site
14002 = ((struct call_site *)
14003 obstack_alloc (&objfile->objfile_obstack,
14004 sizeof (*call_site)
14005 + (sizeof (*call_site->parameter) * (nparams - 1))));
14006 *slot = call_site;
14007 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14008 call_site->pc = pc;
14009
14010 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14011 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
14012 {
14013 struct die_info *func_die;
14014
14015 /* Skip also over DW_TAG_inlined_subroutine. */
14016 for (func_die = die->parent;
14017 func_die && func_die->tag != DW_TAG_subprogram
14018 && func_die->tag != DW_TAG_subroutine_type;
14019 func_die = func_die->parent);
14020
14021 /* DW_AT_call_all_calls is a superset
14022 of DW_AT_call_all_tail_calls. */
14023 if (func_die
14024 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
14025 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
14026 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
14027 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14028 {
14029 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14030 not complete. But keep CALL_SITE for look ups via call_site_htab,
14031 both the initial caller containing the real return address PC and
14032 the final callee containing the current PC of a chain of tail
14033 calls do not need to have the tail call list complete. But any
14034 function candidate for a virtual tail call frame searched via
14035 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14036 determined unambiguously. */
14037 }
14038 else
14039 {
14040 struct type *func_type = NULL;
14041
14042 if (func_die)
14043 func_type = get_die_type (func_die, cu);
14044 if (func_type != NULL)
14045 {
14046 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
14047
14048 /* Enlist this call site to the function. */
14049 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14050 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14051 }
14052 else
14053 complaint (_("Cannot find function owning DW_TAG_call_site "
14054 "DIE %s [in module %s]"),
14055 sect_offset_str (die->sect_off), objfile_name (objfile));
14056 }
14057 }
14058
14059 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14060 if (attr == NULL)
14061 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14062 if (attr == NULL)
14063 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14064 if (attr == NULL)
14065 {
14066 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14067 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14068 }
14069 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14070 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14071 /* Keep NULL DWARF_BLOCK. */;
14072 else if (attr->form_is_block ())
14073 {
14074 struct dwarf2_locexpr_baton *dlbaton;
14075 struct dwarf_block *block = attr->as_block ();
14076
14077 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14078 dlbaton->data = block->data;
14079 dlbaton->size = block->size;
14080 dlbaton->per_objfile = per_objfile;
14081 dlbaton->per_cu = cu->per_cu;
14082
14083 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14084 }
14085 else if (attr->form_is_ref ())
14086 {
14087 struct dwarf2_cu *target_cu = cu;
14088 struct die_info *target_die;
14089
14090 target_die = follow_die_ref (die, attr, &target_cu);
14091 gdb_assert (target_cu->per_objfile->objfile == objfile);
14092 if (die_is_declaration (target_die, target_cu))
14093 {
14094 const char *target_physname;
14095
14096 /* Prefer the mangled name; otherwise compute the demangled one. */
14097 target_physname = dw2_linkage_name (target_die, target_cu);
14098 if (target_physname == NULL)
14099 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14100 if (target_physname == NULL)
14101 complaint (_("DW_AT_call_target target DIE has invalid "
14102 "physname, for referencing DIE %s [in module %s]"),
14103 sect_offset_str (die->sect_off), objfile_name (objfile));
14104 else
14105 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14106 }
14107 else
14108 {
14109 CORE_ADDR lowpc;
14110
14111 /* DW_AT_entry_pc should be preferred. */
14112 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14113 <= PC_BOUNDS_INVALID)
14114 complaint (_("DW_AT_call_target target DIE has invalid "
14115 "low pc, for referencing DIE %s [in module %s]"),
14116 sect_offset_str (die->sect_off), objfile_name (objfile));
14117 else
14118 {
14119 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14120 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14121 }
14122 }
14123 }
14124 else
14125 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14126 "block nor reference, for DIE %s [in module %s]"),
14127 sect_offset_str (die->sect_off), objfile_name (objfile));
14128
14129 call_site->per_cu = cu->per_cu;
14130 call_site->per_objfile = per_objfile;
14131
14132 for (child_die = die->child;
14133 child_die && child_die->tag;
14134 child_die = child_die->sibling)
14135 {
14136 struct call_site_parameter *parameter;
14137 struct attribute *loc, *origin;
14138
14139 if (child_die->tag != DW_TAG_call_site_parameter
14140 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14141 {
14142 /* Already printed the complaint above. */
14143 continue;
14144 }
14145
14146 gdb_assert (call_site->parameter_count < nparams);
14147 parameter = &call_site->parameter[call_site->parameter_count];
14148
14149 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14150 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14151 register is contained in DW_AT_call_value. */
14152
14153 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14154 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14155 if (origin == NULL)
14156 {
14157 /* This was a pre-DWARF-5 GNU extension alias
14158 for DW_AT_call_parameter. */
14159 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14160 }
14161 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14162 {
14163 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14164
14165 sect_offset sect_off = origin->get_ref_die_offset ();
14166 if (!cu->header.offset_in_cu_p (sect_off))
14167 {
14168 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14169 binding can be done only inside one CU. Such referenced DIE
14170 therefore cannot be even moved to DW_TAG_partial_unit. */
14171 complaint (_("DW_AT_call_parameter offset is not in CU for "
14172 "DW_TAG_call_site child DIE %s [in module %s]"),
14173 sect_offset_str (child_die->sect_off),
14174 objfile_name (objfile));
14175 continue;
14176 }
14177 parameter->u.param_cu_off
14178 = (cu_offset) (sect_off - cu->header.sect_off);
14179 }
14180 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14181 {
14182 complaint (_("No DW_FORM_block* DW_AT_location for "
14183 "DW_TAG_call_site child DIE %s [in module %s]"),
14184 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14185 continue;
14186 }
14187 else
14188 {
14189 struct dwarf_block *block = loc->as_block ();
14190
14191 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14192 (block->data, &block->data[block->size]);
14193 if (parameter->u.dwarf_reg != -1)
14194 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14195 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14196 &block->data[block->size],
14197 &parameter->u.fb_offset))
14198 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14199 else
14200 {
14201 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14202 "for DW_FORM_block* DW_AT_location is supported for "
14203 "DW_TAG_call_site child DIE %s "
14204 "[in module %s]"),
14205 sect_offset_str (child_die->sect_off),
14206 objfile_name (objfile));
14207 continue;
14208 }
14209 }
14210
14211 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14212 if (attr == NULL)
14213 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14214 if (attr == NULL || !attr->form_is_block ())
14215 {
14216 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14217 "DW_TAG_call_site child DIE %s [in module %s]"),
14218 sect_offset_str (child_die->sect_off),
14219 objfile_name (objfile));
14220 continue;
14221 }
14222
14223 struct dwarf_block *block = attr->as_block ();
14224 parameter->value = block->data;
14225 parameter->value_size = block->size;
14226
14227 /* Parameters are not pre-cleared by memset above. */
14228 parameter->data_value = NULL;
14229 parameter->data_value_size = 0;
14230 call_site->parameter_count++;
14231
14232 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14233 if (attr == NULL)
14234 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14235 if (attr != nullptr)
14236 {
14237 if (!attr->form_is_block ())
14238 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14239 "DW_TAG_call_site child DIE %s [in module %s]"),
14240 sect_offset_str (child_die->sect_off),
14241 objfile_name (objfile));
14242 else
14243 {
14244 block = attr->as_block ();
14245 parameter->data_value = block->data;
14246 parameter->data_value_size = block->size;
14247 }
14248 }
14249 }
14250 }
14251
14252 /* Helper function for read_variable. If DIE represents a virtual
14253 table, then return the type of the concrete object that is
14254 associated with the virtual table. Otherwise, return NULL. */
14255
14256 static struct type *
14257 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14258 {
14259 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14260 if (attr == NULL)
14261 return NULL;
14262
14263 /* Find the type DIE. */
14264 struct die_info *type_die = NULL;
14265 struct dwarf2_cu *type_cu = cu;
14266
14267 if (attr->form_is_ref ())
14268 type_die = follow_die_ref (die, attr, &type_cu);
14269 if (type_die == NULL)
14270 return NULL;
14271
14272 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14273 return NULL;
14274 return die_containing_type (type_die, type_cu);
14275 }
14276
14277 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14278
14279 static void
14280 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14281 {
14282 struct rust_vtable_symbol *storage = NULL;
14283
14284 if (cu->language == language_rust)
14285 {
14286 struct type *containing_type = rust_containing_type (die, cu);
14287
14288 if (containing_type != NULL)
14289 {
14290 struct objfile *objfile = cu->per_objfile->objfile;
14291
14292 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14293 storage->concrete_type = containing_type;
14294 storage->subclass = SYMBOL_RUST_VTABLE;
14295 }
14296 }
14297
14298 struct symbol *res = new_symbol (die, NULL, cu, storage);
14299 struct attribute *abstract_origin
14300 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14301 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14302 if (res == NULL && loc && abstract_origin)
14303 {
14304 /* We have a variable without a name, but with a location and an abstract
14305 origin. This may be a concrete instance of an abstract variable
14306 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14307 later. */
14308 struct dwarf2_cu *origin_cu = cu;
14309 struct die_info *origin_die
14310 = follow_die_ref (die, abstract_origin, &origin_cu);
14311 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14312 per_objfile->per_bfd->abstract_to_concrete
14313 [origin_die->sect_off].push_back (die->sect_off);
14314 }
14315 }
14316
14317 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14318 reading .debug_rnglists.
14319 Callback's type should be:
14320 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14321 Return true if the attributes are present and valid, otherwise,
14322 return false. */
14323
14324 template <typename Callback>
14325 static bool
14326 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14327 dwarf_tag tag, Callback &&callback)
14328 {
14329 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14330 struct objfile *objfile = per_objfile->objfile;
14331 bfd *obfd = objfile->obfd;
14332 /* Base address selection entry. */
14333 gdb::optional<CORE_ADDR> base;
14334 const gdb_byte *buffer;
14335 CORE_ADDR baseaddr;
14336 bool overflow = false;
14337 ULONGEST addr_index;
14338 struct dwarf2_section_info *rnglists_section;
14339
14340 base = cu->base_address;
14341 rnglists_section = cu_debug_rnglists_section (cu, tag);
14342 rnglists_section->read (objfile);
14343
14344 if (offset >= rnglists_section->size)
14345 {
14346 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14347 offset);
14348 return false;
14349 }
14350 buffer = rnglists_section->buffer + offset;
14351
14352 baseaddr = objfile->text_section_offset ();
14353
14354 while (1)
14355 {
14356 /* Initialize it due to a false compiler warning. */
14357 CORE_ADDR range_beginning = 0, range_end = 0;
14358 const gdb_byte *buf_end = (rnglists_section->buffer
14359 + rnglists_section->size);
14360 unsigned int bytes_read;
14361
14362 if (buffer == buf_end)
14363 {
14364 overflow = true;
14365 break;
14366 }
14367 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14368 switch (rlet)
14369 {
14370 case DW_RLE_end_of_list:
14371 break;
14372 case DW_RLE_base_address:
14373 if (buffer + cu->header.addr_size > buf_end)
14374 {
14375 overflow = true;
14376 break;
14377 }
14378 base = cu->header.read_address (obfd, buffer, &bytes_read);
14379 buffer += bytes_read;
14380 break;
14381 case DW_RLE_base_addressx:
14382 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14383 buffer += bytes_read;
14384 base = read_addr_index (cu, addr_index);
14385 break;
14386 case DW_RLE_start_length:
14387 if (buffer + cu->header.addr_size > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 range_beginning = cu->header.read_address (obfd, buffer,
14393 &bytes_read);
14394 buffer += bytes_read;
14395 range_end = (range_beginning
14396 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14397 buffer += bytes_read;
14398 if (buffer > buf_end)
14399 {
14400 overflow = true;
14401 break;
14402 }
14403 break;
14404 case DW_RLE_startx_length:
14405 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14406 buffer += bytes_read;
14407 range_beginning = read_addr_index (cu, addr_index);
14408 if (buffer > buf_end)
14409 {
14410 overflow = true;
14411 break;
14412 }
14413 range_end = (range_beginning
14414 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14415 buffer += bytes_read;
14416 break;
14417 case DW_RLE_offset_pair:
14418 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14419 buffer += bytes_read;
14420 if (buffer > buf_end)
14421 {
14422 overflow = true;
14423 break;
14424 }
14425 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14426 buffer += bytes_read;
14427 if (buffer > buf_end)
14428 {
14429 overflow = true;
14430 break;
14431 }
14432 break;
14433 case DW_RLE_start_end:
14434 if (buffer + 2 * cu->header.addr_size > buf_end)
14435 {
14436 overflow = true;
14437 break;
14438 }
14439 range_beginning = cu->header.read_address (obfd, buffer,
14440 &bytes_read);
14441 buffer += bytes_read;
14442 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14443 buffer += bytes_read;
14444 break;
14445 case DW_RLE_startx_endx:
14446 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14447 buffer += bytes_read;
14448 range_beginning = read_addr_index (cu, addr_index);
14449 if (buffer > buf_end)
14450 {
14451 overflow = true;
14452 break;
14453 }
14454 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14455 buffer += bytes_read;
14456 range_end = read_addr_index (cu, addr_index);
14457 break;
14458 default:
14459 complaint (_("Invalid .debug_rnglists data (no base address)"));
14460 return false;
14461 }
14462 if (rlet == DW_RLE_end_of_list || overflow)
14463 break;
14464 if (rlet == DW_RLE_base_address)
14465 continue;
14466
14467 if (range_beginning > range_end)
14468 {
14469 /* Inverted range entries are invalid. */
14470 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14471 return false;
14472 }
14473
14474 /* Empty range entries have no effect. */
14475 if (range_beginning == range_end)
14476 continue;
14477
14478 /* Only DW_RLE_offset_pair needs the base address added. */
14479 if (rlet == DW_RLE_offset_pair)
14480 {
14481 if (!base.has_value ())
14482 {
14483 /* We have no valid base address for the DW_RLE_offset_pair. */
14484 complaint (_("Invalid .debug_rnglists data (no base address for "
14485 "DW_RLE_offset_pair)"));
14486 return false;
14487 }
14488
14489 range_beginning += *base;
14490 range_end += *base;
14491 }
14492
14493 /* A not-uncommon case of bad debug info.
14494 Don't pollute the addrmap with bad data. */
14495 if (range_beginning + baseaddr == 0
14496 && !per_objfile->per_bfd->has_section_at_zero)
14497 {
14498 complaint (_(".debug_rnglists entry has start address of zero"
14499 " [in module %s]"), objfile_name (objfile));
14500 continue;
14501 }
14502
14503 callback (range_beginning, range_end);
14504 }
14505
14506 if (overflow)
14507 {
14508 complaint (_("Offset %d is not terminated "
14509 "for DW_AT_ranges attribute"),
14510 offset);
14511 return false;
14512 }
14513
14514 return true;
14515 }
14516
14517 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14518 Callback's type should be:
14519 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14520 Return 1 if the attributes are present and valid, otherwise, return 0. */
14521
14522 template <typename Callback>
14523 static int
14524 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14525 Callback &&callback)
14526 {
14527 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14528 struct objfile *objfile = per_objfile->objfile;
14529 struct comp_unit_head *cu_header = &cu->header;
14530 bfd *obfd = objfile->obfd;
14531 unsigned int addr_size = cu_header->addr_size;
14532 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14533 /* Base address selection entry. */
14534 gdb::optional<CORE_ADDR> base;
14535 unsigned int dummy;
14536 const gdb_byte *buffer;
14537 CORE_ADDR baseaddr;
14538
14539 if (cu_header->version >= 5)
14540 return dwarf2_rnglists_process (offset, cu, tag, callback);
14541
14542 base = cu->base_address;
14543
14544 per_objfile->per_bfd->ranges.read (objfile);
14545 if (offset >= per_objfile->per_bfd->ranges.size)
14546 {
14547 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14548 offset);
14549 return 0;
14550 }
14551 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14552
14553 baseaddr = objfile->text_section_offset ();
14554
14555 while (1)
14556 {
14557 CORE_ADDR range_beginning, range_end;
14558
14559 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14560 buffer += addr_size;
14561 range_end = cu->header.read_address (obfd, buffer, &dummy);
14562 buffer += addr_size;
14563 offset += 2 * addr_size;
14564
14565 /* An end of list marker is a pair of zero addresses. */
14566 if (range_beginning == 0 && range_end == 0)
14567 /* Found the end of list entry. */
14568 break;
14569
14570 /* Each base address selection entry is a pair of 2 values.
14571 The first is the largest possible address, the second is
14572 the base address. Check for a base address here. */
14573 if ((range_beginning & mask) == mask)
14574 {
14575 /* If we found the largest possible address, then we already
14576 have the base address in range_end. */
14577 base = range_end;
14578 continue;
14579 }
14580
14581 if (!base.has_value ())
14582 {
14583 /* We have no valid base address for the ranges
14584 data. */
14585 complaint (_("Invalid .debug_ranges data (no base address)"));
14586 return 0;
14587 }
14588
14589 if (range_beginning > range_end)
14590 {
14591 /* Inverted range entries are invalid. */
14592 complaint (_("Invalid .debug_ranges data (inverted range)"));
14593 return 0;
14594 }
14595
14596 /* Empty range entries have no effect. */
14597 if (range_beginning == range_end)
14598 continue;
14599
14600 range_beginning += *base;
14601 range_end += *base;
14602
14603 /* A not-uncommon case of bad debug info.
14604 Don't pollute the addrmap with bad data. */
14605 if (range_beginning + baseaddr == 0
14606 && !per_objfile->per_bfd->has_section_at_zero)
14607 {
14608 complaint (_(".debug_ranges entry has start address of zero"
14609 " [in module %s]"), objfile_name (objfile));
14610 continue;
14611 }
14612
14613 callback (range_beginning, range_end);
14614 }
14615
14616 return 1;
14617 }
14618
14619 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14620 Return 1 if the attributes are present and valid, otherwise, return 0.
14621 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14622
14623 static int
14624 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14625 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14626 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14627 {
14628 struct objfile *objfile = cu->per_objfile->objfile;
14629 struct gdbarch *gdbarch = objfile->arch ();
14630 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14631 int low_set = 0;
14632 CORE_ADDR low = 0;
14633 CORE_ADDR high = 0;
14634 int retval;
14635
14636 retval = dwarf2_ranges_process (offset, cu, tag,
14637 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14638 {
14639 if (ranges_pst != NULL)
14640 {
14641 CORE_ADDR lowpc;
14642 CORE_ADDR highpc;
14643
14644 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14645 range_beginning + baseaddr)
14646 - baseaddr);
14647 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14648 range_end + baseaddr)
14649 - baseaddr);
14650 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14651 lowpc, highpc - 1, ranges_pst);
14652 }
14653
14654 /* FIXME: This is recording everything as a low-high
14655 segment of consecutive addresses. We should have a
14656 data structure for discontiguous block ranges
14657 instead. */
14658 if (! low_set)
14659 {
14660 low = range_beginning;
14661 high = range_end;
14662 low_set = 1;
14663 }
14664 else
14665 {
14666 if (range_beginning < low)
14667 low = range_beginning;
14668 if (range_end > high)
14669 high = range_end;
14670 }
14671 });
14672 if (!retval)
14673 return 0;
14674
14675 if (! low_set)
14676 /* If the first entry is an end-of-list marker, the range
14677 describes an empty scope, i.e. no instructions. */
14678 return 0;
14679
14680 if (low_return)
14681 *low_return = low;
14682 if (high_return)
14683 *high_return = high;
14684 return 1;
14685 }
14686
14687 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14688 definition for the return value. *LOWPC and *HIGHPC are set iff
14689 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14690
14691 static enum pc_bounds_kind
14692 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14693 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14694 dwarf2_psymtab *pst)
14695 {
14696 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14697 struct attribute *attr;
14698 struct attribute *attr_high;
14699 CORE_ADDR low = 0;
14700 CORE_ADDR high = 0;
14701 enum pc_bounds_kind ret;
14702
14703 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14704 if (attr_high)
14705 {
14706 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14707 if (attr != nullptr)
14708 {
14709 low = attr->as_address ();
14710 high = attr_high->as_address ();
14711 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14712 high += low;
14713 }
14714 else
14715 /* Found high w/o low attribute. */
14716 return PC_BOUNDS_INVALID;
14717
14718 /* Found consecutive range of addresses. */
14719 ret = PC_BOUNDS_HIGH_LOW;
14720 }
14721 else
14722 {
14723 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14724 if (attr != nullptr && attr->form_is_unsigned ())
14725 {
14726 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14727 on DWARF version). */
14728 ULONGEST ranges_offset = attr->as_unsigned ();
14729
14730 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14731 this value. */
14732 if (die->tag != DW_TAG_compile_unit)
14733 ranges_offset += cu->gnu_ranges_base;
14734
14735 /* Value of the DW_AT_ranges attribute is the offset in the
14736 .debug_ranges section. */
14737 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14738 die->tag))
14739 return PC_BOUNDS_INVALID;
14740 /* Found discontinuous range of addresses. */
14741 ret = PC_BOUNDS_RANGES;
14742 }
14743 else
14744 return PC_BOUNDS_NOT_PRESENT;
14745 }
14746
14747 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14748 if (high <= low)
14749 return PC_BOUNDS_INVALID;
14750
14751 /* When using the GNU linker, .gnu.linkonce. sections are used to
14752 eliminate duplicate copies of functions and vtables and such.
14753 The linker will arbitrarily choose one and discard the others.
14754 The AT_*_pc values for such functions refer to local labels in
14755 these sections. If the section from that file was discarded, the
14756 labels are not in the output, so the relocs get a value of 0.
14757 If this is a discarded function, mark the pc bounds as invalid,
14758 so that GDB will ignore it. */
14759 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14760 return PC_BOUNDS_INVALID;
14761
14762 *lowpc = low;
14763 if (highpc)
14764 *highpc = high;
14765 return ret;
14766 }
14767
14768 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14769 its low and high PC addresses. Do nothing if these addresses could not
14770 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14771 and HIGHPC to the high address if greater than HIGHPC. */
14772
14773 static void
14774 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14775 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14776 struct dwarf2_cu *cu)
14777 {
14778 CORE_ADDR low, high;
14779 struct die_info *child = die->child;
14780
14781 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14782 {
14783 *lowpc = std::min (*lowpc, low);
14784 *highpc = std::max (*highpc, high);
14785 }
14786
14787 /* If the language does not allow nested subprograms (either inside
14788 subprograms or lexical blocks), we're done. */
14789 if (cu->language != language_ada)
14790 return;
14791
14792 /* Check all the children of the given DIE. If it contains nested
14793 subprograms, then check their pc bounds. Likewise, we need to
14794 check lexical blocks as well, as they may also contain subprogram
14795 definitions. */
14796 while (child && child->tag)
14797 {
14798 if (child->tag == DW_TAG_subprogram
14799 || child->tag == DW_TAG_lexical_block)
14800 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14801 child = child->sibling;
14802 }
14803 }
14804
14805 /* Get the low and high pc's represented by the scope DIE, and store
14806 them in *LOWPC and *HIGHPC. If the correct values can't be
14807 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14808
14809 static void
14810 get_scope_pc_bounds (struct die_info *die,
14811 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14812 struct dwarf2_cu *cu)
14813 {
14814 CORE_ADDR best_low = (CORE_ADDR) -1;
14815 CORE_ADDR best_high = (CORE_ADDR) 0;
14816 CORE_ADDR current_low, current_high;
14817
14818 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14819 >= PC_BOUNDS_RANGES)
14820 {
14821 best_low = current_low;
14822 best_high = current_high;
14823 }
14824 else
14825 {
14826 struct die_info *child = die->child;
14827
14828 while (child && child->tag)
14829 {
14830 switch (child->tag) {
14831 case DW_TAG_subprogram:
14832 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14833 break;
14834 case DW_TAG_namespace:
14835 case DW_TAG_module:
14836 /* FIXME: carlton/2004-01-16: Should we do this for
14837 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14838 that current GCC's always emit the DIEs corresponding
14839 to definitions of methods of classes as children of a
14840 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14841 the DIEs giving the declarations, which could be
14842 anywhere). But I don't see any reason why the
14843 standards says that they have to be there. */
14844 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14845
14846 if (current_low != ((CORE_ADDR) -1))
14847 {
14848 best_low = std::min (best_low, current_low);
14849 best_high = std::max (best_high, current_high);
14850 }
14851 break;
14852 default:
14853 /* Ignore. */
14854 break;
14855 }
14856
14857 child = child->sibling;
14858 }
14859 }
14860
14861 *lowpc = best_low;
14862 *highpc = best_high;
14863 }
14864
14865 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14866 in DIE. */
14867
14868 static void
14869 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14870 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14871 {
14872 struct objfile *objfile = cu->per_objfile->objfile;
14873 struct gdbarch *gdbarch = objfile->arch ();
14874 struct attribute *attr;
14875 struct attribute *attr_high;
14876
14877 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14878 if (attr_high)
14879 {
14880 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14881 if (attr != nullptr)
14882 {
14883 CORE_ADDR low = attr->as_address ();
14884 CORE_ADDR high = attr_high->as_address ();
14885
14886 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14887 high += low;
14888
14889 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14890 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14891 cu->get_builder ()->record_block_range (block, low, high - 1);
14892 }
14893 }
14894
14895 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14896 if (attr != nullptr && attr->form_is_unsigned ())
14897 {
14898 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14899 on DWARF version). */
14900 ULONGEST ranges_offset = attr->as_unsigned ();
14901
14902 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14903 this value. */
14904 if (die->tag != DW_TAG_compile_unit)
14905 ranges_offset += cu->gnu_ranges_base;
14906
14907 std::vector<blockrange> blockvec;
14908 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14909 [&] (CORE_ADDR start, CORE_ADDR end)
14910 {
14911 start += baseaddr;
14912 end += baseaddr;
14913 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14914 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14915 cu->get_builder ()->record_block_range (block, start, end - 1);
14916 blockvec.emplace_back (start, end);
14917 });
14918
14919 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14920 }
14921 }
14922
14923 /* Check whether the producer field indicates either of GCC < 4.6, or the
14924 Intel C/C++ compiler, and cache the result in CU. */
14925
14926 static void
14927 check_producer (struct dwarf2_cu *cu)
14928 {
14929 int major, minor;
14930
14931 if (cu->producer == NULL)
14932 {
14933 /* For unknown compilers expect their behavior is DWARF version
14934 compliant.
14935
14936 GCC started to support .debug_types sections by -gdwarf-4 since
14937 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14938 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14939 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14940 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14941 }
14942 else if (producer_is_gcc (cu->producer, &major, &minor))
14943 {
14944 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14945 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14946 }
14947 else if (producer_is_icc (cu->producer, &major, &minor))
14948 {
14949 cu->producer_is_icc = true;
14950 cu->producer_is_icc_lt_14 = major < 14;
14951 }
14952 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14953 cu->producer_is_codewarrior = true;
14954 else
14955 {
14956 /* For other non-GCC compilers, expect their behavior is DWARF version
14957 compliant. */
14958 }
14959
14960 cu->checked_producer = true;
14961 }
14962
14963 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14964 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14965 during 4.6.0 experimental. */
14966
14967 static bool
14968 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14969 {
14970 if (!cu->checked_producer)
14971 check_producer (cu);
14972
14973 return cu->producer_is_gxx_lt_4_6;
14974 }
14975
14976
14977 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14978 with incorrect is_stmt attributes. */
14979
14980 static bool
14981 producer_is_codewarrior (struct dwarf2_cu *cu)
14982 {
14983 if (!cu->checked_producer)
14984 check_producer (cu);
14985
14986 return cu->producer_is_codewarrior;
14987 }
14988
14989 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14990 If that attribute is not available, return the appropriate
14991 default. */
14992
14993 static enum dwarf_access_attribute
14994 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14995 {
14996 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14997 if (attr != nullptr)
14998 {
14999 LONGEST value = attr->constant_value (-1);
15000 if (value == DW_ACCESS_public
15001 || value == DW_ACCESS_protected
15002 || value == DW_ACCESS_private)
15003 return (dwarf_access_attribute) value;
15004 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
15005 plongest (value));
15006 }
15007
15008 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
15009 {
15010 /* The default DWARF 2 accessibility for members is public, the default
15011 accessibility for inheritance is private. */
15012
15013 if (die->tag != DW_TAG_inheritance)
15014 return DW_ACCESS_public;
15015 else
15016 return DW_ACCESS_private;
15017 }
15018 else
15019 {
15020 /* DWARF 3+ defines the default accessibility a different way. The same
15021 rules apply now for DW_TAG_inheritance as for the members and it only
15022 depends on the container kind. */
15023
15024 if (die->parent->tag == DW_TAG_class_type)
15025 return DW_ACCESS_private;
15026 else
15027 return DW_ACCESS_public;
15028 }
15029 }
15030
15031 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15032 offset. If the attribute was not found return 0, otherwise return
15033 1. If it was found but could not properly be handled, set *OFFSET
15034 to 0. */
15035
15036 static int
15037 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15038 LONGEST *offset)
15039 {
15040 struct attribute *attr;
15041
15042 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15043 if (attr != NULL)
15044 {
15045 *offset = 0;
15046
15047 /* Note that we do not check for a section offset first here.
15048 This is because DW_AT_data_member_location is new in DWARF 4,
15049 so if we see it, we can assume that a constant form is really
15050 a constant and not a section offset. */
15051 if (attr->form_is_constant ())
15052 *offset = attr->constant_value (0);
15053 else if (attr->form_is_section_offset ())
15054 dwarf2_complex_location_expr_complaint ();
15055 else if (attr->form_is_block ())
15056 *offset = decode_locdesc (attr->as_block (), cu);
15057 else
15058 dwarf2_complex_location_expr_complaint ();
15059
15060 return 1;
15061 }
15062
15063 return 0;
15064 }
15065
15066 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15067
15068 static void
15069 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15070 struct field *field)
15071 {
15072 struct attribute *attr;
15073
15074 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15075 if (attr != NULL)
15076 {
15077 if (attr->form_is_constant ())
15078 {
15079 LONGEST offset = attr->constant_value (0);
15080 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15081 }
15082 else if (attr->form_is_section_offset ())
15083 dwarf2_complex_location_expr_complaint ();
15084 else if (attr->form_is_block ())
15085 {
15086 bool handled;
15087 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15088 if (handled)
15089 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15090 else
15091 {
15092 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15093 struct objfile *objfile = per_objfile->objfile;
15094 struct dwarf2_locexpr_baton *dlbaton
15095 = XOBNEW (&objfile->objfile_obstack,
15096 struct dwarf2_locexpr_baton);
15097 dlbaton->data = attr->as_block ()->data;
15098 dlbaton->size = attr->as_block ()->size;
15099 /* When using this baton, we want to compute the address
15100 of the field, not the value. This is why
15101 is_reference is set to false here. */
15102 dlbaton->is_reference = false;
15103 dlbaton->per_objfile = per_objfile;
15104 dlbaton->per_cu = cu->per_cu;
15105
15106 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15107 }
15108 }
15109 else
15110 dwarf2_complex_location_expr_complaint ();
15111 }
15112 }
15113
15114 /* Add an aggregate field to the field list. */
15115
15116 static void
15117 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15118 struct dwarf2_cu *cu)
15119 {
15120 struct objfile *objfile = cu->per_objfile->objfile;
15121 struct gdbarch *gdbarch = objfile->arch ();
15122 struct nextfield *new_field;
15123 struct attribute *attr;
15124 struct field *fp;
15125 const char *fieldname = "";
15126
15127 if (die->tag == DW_TAG_inheritance)
15128 {
15129 fip->baseclasses.emplace_back ();
15130 new_field = &fip->baseclasses.back ();
15131 }
15132 else
15133 {
15134 fip->fields.emplace_back ();
15135 new_field = &fip->fields.back ();
15136 }
15137
15138 new_field->offset = die->sect_off;
15139
15140 new_field->accessibility = dwarf2_access_attribute (die, cu);
15141 if (new_field->accessibility != DW_ACCESS_public)
15142 fip->non_public_fields = true;
15143
15144 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15145 if (attr != nullptr)
15146 new_field->virtuality = attr->as_virtuality ();
15147 else
15148 new_field->virtuality = DW_VIRTUALITY_none;
15149
15150 fp = &new_field->field;
15151
15152 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15153 {
15154 /* Data member other than a C++ static data member. */
15155
15156 /* Get type of field. */
15157 fp->set_type (die_type (die, cu));
15158
15159 SET_FIELD_BITPOS (*fp, 0);
15160
15161 /* Get bit size of field (zero if none). */
15162 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15163 if (attr != nullptr)
15164 {
15165 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15166 }
15167 else
15168 {
15169 FIELD_BITSIZE (*fp) = 0;
15170 }
15171
15172 /* Get bit offset of field. */
15173 handle_data_member_location (die, cu, fp);
15174 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15175 if (attr != nullptr && attr->form_is_constant ())
15176 {
15177 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15178 {
15179 /* For big endian bits, the DW_AT_bit_offset gives the
15180 additional bit offset from the MSB of the containing
15181 anonymous object to the MSB of the field. We don't
15182 have to do anything special since we don't need to
15183 know the size of the anonymous object. */
15184 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15185 + attr->constant_value (0)));
15186 }
15187 else
15188 {
15189 /* For little endian bits, compute the bit offset to the
15190 MSB of the anonymous object, subtract off the number of
15191 bits from the MSB of the field to the MSB of the
15192 object, and then subtract off the number of bits of
15193 the field itself. The result is the bit offset of
15194 the LSB of the field. */
15195 int anonymous_size;
15196 int bit_offset = attr->constant_value (0);
15197
15198 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15199 if (attr != nullptr && attr->form_is_constant ())
15200 {
15201 /* The size of the anonymous object containing
15202 the bit field is explicit, so use the
15203 indicated size (in bytes). */
15204 anonymous_size = attr->constant_value (0);
15205 }
15206 else
15207 {
15208 /* The size of the anonymous object containing
15209 the bit field must be inferred from the type
15210 attribute of the data member containing the
15211 bit field. */
15212 anonymous_size = TYPE_LENGTH (fp->type ());
15213 }
15214 SET_FIELD_BITPOS (*fp,
15215 (FIELD_BITPOS (*fp)
15216 + anonymous_size * bits_per_byte
15217 - bit_offset - FIELD_BITSIZE (*fp)));
15218 }
15219 }
15220 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15221 if (attr != NULL)
15222 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15223 + attr->constant_value (0)));
15224
15225 /* Get name of field. */
15226 fieldname = dwarf2_name (die, cu);
15227 if (fieldname == NULL)
15228 fieldname = "";
15229
15230 /* The name is already allocated along with this objfile, so we don't
15231 need to duplicate it for the type. */
15232 fp->name = fieldname;
15233
15234 /* Change accessibility for artificial fields (e.g. virtual table
15235 pointer or virtual base class pointer) to private. */
15236 if (dwarf2_attr (die, DW_AT_artificial, cu))
15237 {
15238 FIELD_ARTIFICIAL (*fp) = 1;
15239 new_field->accessibility = DW_ACCESS_private;
15240 fip->non_public_fields = true;
15241 }
15242 }
15243 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15244 {
15245 /* C++ static member. */
15246
15247 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15248 is a declaration, but all versions of G++ as of this writing
15249 (so through at least 3.2.1) incorrectly generate
15250 DW_TAG_variable tags. */
15251
15252 const char *physname;
15253
15254 /* Get name of field. */
15255 fieldname = dwarf2_name (die, cu);
15256 if (fieldname == NULL)
15257 return;
15258
15259 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15260 if (attr
15261 /* Only create a symbol if this is an external value.
15262 new_symbol checks this and puts the value in the global symbol
15263 table, which we want. If it is not external, new_symbol
15264 will try to put the value in cu->list_in_scope which is wrong. */
15265 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15266 {
15267 /* A static const member, not much different than an enum as far as
15268 we're concerned, except that we can support more types. */
15269 new_symbol (die, NULL, cu);
15270 }
15271
15272 /* Get physical name. */
15273 physname = dwarf2_physname (fieldname, die, cu);
15274
15275 /* The name is already allocated along with this objfile, so we don't
15276 need to duplicate it for the type. */
15277 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15278 fp->set_type (die_type (die, cu));
15279 FIELD_NAME (*fp) = fieldname;
15280 }
15281 else if (die->tag == DW_TAG_inheritance)
15282 {
15283 /* C++ base class field. */
15284 handle_data_member_location (die, cu, fp);
15285 FIELD_BITSIZE (*fp) = 0;
15286 fp->set_type (die_type (die, cu));
15287 FIELD_NAME (*fp) = fp->type ()->name ();
15288 }
15289 else
15290 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15291 }
15292
15293 /* Can the type given by DIE define another type? */
15294
15295 static bool
15296 type_can_define_types (const struct die_info *die)
15297 {
15298 switch (die->tag)
15299 {
15300 case DW_TAG_typedef:
15301 case DW_TAG_class_type:
15302 case DW_TAG_structure_type:
15303 case DW_TAG_union_type:
15304 case DW_TAG_enumeration_type:
15305 return true;
15306
15307 default:
15308 return false;
15309 }
15310 }
15311
15312 /* Add a type definition defined in the scope of the FIP's class. */
15313
15314 static void
15315 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15316 struct dwarf2_cu *cu)
15317 {
15318 struct decl_field fp;
15319 memset (&fp, 0, sizeof (fp));
15320
15321 gdb_assert (type_can_define_types (die));
15322
15323 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15324 fp.name = dwarf2_name (die, cu);
15325 fp.type = read_type_die (die, cu);
15326
15327 /* Save accessibility. */
15328 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15329 switch (accessibility)
15330 {
15331 case DW_ACCESS_public:
15332 /* The assumed value if neither private nor protected. */
15333 break;
15334 case DW_ACCESS_private:
15335 fp.is_private = 1;
15336 break;
15337 case DW_ACCESS_protected:
15338 fp.is_protected = 1;
15339 break;
15340 }
15341
15342 if (die->tag == DW_TAG_typedef)
15343 fip->typedef_field_list.push_back (fp);
15344 else
15345 fip->nested_types_list.push_back (fp);
15346 }
15347
15348 /* A convenience typedef that's used when finding the discriminant
15349 field for a variant part. */
15350 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15351 offset_map_type;
15352
15353 /* Compute the discriminant range for a given variant. OBSTACK is
15354 where the results will be stored. VARIANT is the variant to
15355 process. IS_UNSIGNED indicates whether the discriminant is signed
15356 or unsigned. */
15357
15358 static const gdb::array_view<discriminant_range>
15359 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15360 bool is_unsigned)
15361 {
15362 std::vector<discriminant_range> ranges;
15363
15364 if (variant.default_branch)
15365 return {};
15366
15367 if (variant.discr_list_data == nullptr)
15368 {
15369 discriminant_range r
15370 = {variant.discriminant_value, variant.discriminant_value};
15371 ranges.push_back (r);
15372 }
15373 else
15374 {
15375 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15376 variant.discr_list_data->size);
15377 while (!data.empty ())
15378 {
15379 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15380 {
15381 complaint (_("invalid discriminant marker: %d"), data[0]);
15382 break;
15383 }
15384 bool is_range = data[0] == DW_DSC_range;
15385 data = data.slice (1);
15386
15387 ULONGEST low, high;
15388 unsigned int bytes_read;
15389
15390 if (data.empty ())
15391 {
15392 complaint (_("DW_AT_discr_list missing low value"));
15393 break;
15394 }
15395 if (is_unsigned)
15396 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15397 else
15398 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15399 &bytes_read);
15400 data = data.slice (bytes_read);
15401
15402 if (is_range)
15403 {
15404 if (data.empty ())
15405 {
15406 complaint (_("DW_AT_discr_list missing high value"));
15407 break;
15408 }
15409 if (is_unsigned)
15410 high = read_unsigned_leb128 (nullptr, data.data (),
15411 &bytes_read);
15412 else
15413 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15414 &bytes_read);
15415 data = data.slice (bytes_read);
15416 }
15417 else
15418 high = low;
15419
15420 ranges.push_back ({ low, high });
15421 }
15422 }
15423
15424 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15425 ranges.size ());
15426 std::copy (ranges.begin (), ranges.end (), result);
15427 return gdb::array_view<discriminant_range> (result, ranges.size ());
15428 }
15429
15430 static const gdb::array_view<variant_part> create_variant_parts
15431 (struct obstack *obstack,
15432 const offset_map_type &offset_map,
15433 struct field_info *fi,
15434 const std::vector<variant_part_builder> &variant_parts);
15435
15436 /* Fill in a "struct variant" for a given variant field. RESULT is
15437 the variant to fill in. OBSTACK is where any needed allocations
15438 will be done. OFFSET_MAP holds the mapping from section offsets to
15439 fields for the type. FI describes the fields of the type we're
15440 processing. FIELD is the variant field we're converting. */
15441
15442 static void
15443 create_one_variant (variant &result, struct obstack *obstack,
15444 const offset_map_type &offset_map,
15445 struct field_info *fi, const variant_field &field)
15446 {
15447 result.discriminants = convert_variant_range (obstack, field, false);
15448 result.first_field = field.first_field + fi->baseclasses.size ();
15449 result.last_field = field.last_field + fi->baseclasses.size ();
15450 result.parts = create_variant_parts (obstack, offset_map, fi,
15451 field.variant_parts);
15452 }
15453
15454 /* Fill in a "struct variant_part" for a given variant part. RESULT
15455 is the variant part to fill in. OBSTACK is where any needed
15456 allocations will be done. OFFSET_MAP holds the mapping from
15457 section offsets to fields for the type. FI describes the fields of
15458 the type we're processing. BUILDER is the variant part to be
15459 converted. */
15460
15461 static void
15462 create_one_variant_part (variant_part &result,
15463 struct obstack *obstack,
15464 const offset_map_type &offset_map,
15465 struct field_info *fi,
15466 const variant_part_builder &builder)
15467 {
15468 auto iter = offset_map.find (builder.discriminant_offset);
15469 if (iter == offset_map.end ())
15470 {
15471 result.discriminant_index = -1;
15472 /* Doesn't matter. */
15473 result.is_unsigned = false;
15474 }
15475 else
15476 {
15477 result.discriminant_index = iter->second;
15478 result.is_unsigned
15479 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15480 }
15481
15482 size_t n = builder.variants.size ();
15483 variant *output = new (obstack) variant[n];
15484 for (size_t i = 0; i < n; ++i)
15485 create_one_variant (output[i], obstack, offset_map, fi,
15486 builder.variants[i]);
15487
15488 result.variants = gdb::array_view<variant> (output, n);
15489 }
15490
15491 /* Create a vector of variant parts that can be attached to a type.
15492 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15493 holds the mapping from section offsets to fields for the type. FI
15494 describes the fields of the type we're processing. VARIANT_PARTS
15495 is the vector to convert. */
15496
15497 static const gdb::array_view<variant_part>
15498 create_variant_parts (struct obstack *obstack,
15499 const offset_map_type &offset_map,
15500 struct field_info *fi,
15501 const std::vector<variant_part_builder> &variant_parts)
15502 {
15503 if (variant_parts.empty ())
15504 return {};
15505
15506 size_t n = variant_parts.size ();
15507 variant_part *result = new (obstack) variant_part[n];
15508 for (size_t i = 0; i < n; ++i)
15509 create_one_variant_part (result[i], obstack, offset_map, fi,
15510 variant_parts[i]);
15511
15512 return gdb::array_view<variant_part> (result, n);
15513 }
15514
15515 /* Compute the variant part vector for FIP, attaching it to TYPE when
15516 done. */
15517
15518 static void
15519 add_variant_property (struct field_info *fip, struct type *type,
15520 struct dwarf2_cu *cu)
15521 {
15522 /* Map section offsets of fields to their field index. Note the
15523 field index here does not take the number of baseclasses into
15524 account. */
15525 offset_map_type offset_map;
15526 for (int i = 0; i < fip->fields.size (); ++i)
15527 offset_map[fip->fields[i].offset] = i;
15528
15529 struct objfile *objfile = cu->per_objfile->objfile;
15530 gdb::array_view<variant_part> parts
15531 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15532 fip->variant_parts);
15533
15534 struct dynamic_prop prop;
15535 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15536 obstack_copy (&objfile->objfile_obstack, &parts,
15537 sizeof (parts)));
15538
15539 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15540 }
15541
15542 /* Create the vector of fields, and attach it to the type. */
15543
15544 static void
15545 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15546 struct dwarf2_cu *cu)
15547 {
15548 int nfields = fip->nfields ();
15549
15550 /* Record the field count, allocate space for the array of fields,
15551 and create blank accessibility bitfields if necessary. */
15552 type->set_num_fields (nfields);
15553 type->set_fields
15554 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15555
15556 if (fip->non_public_fields && cu->language != language_ada)
15557 {
15558 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15559
15560 TYPE_FIELD_PRIVATE_BITS (type) =
15561 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15562 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15563
15564 TYPE_FIELD_PROTECTED_BITS (type) =
15565 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15566 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15567
15568 TYPE_FIELD_IGNORE_BITS (type) =
15569 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15570 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15571 }
15572
15573 /* If the type has baseclasses, allocate and clear a bit vector for
15574 TYPE_FIELD_VIRTUAL_BITS. */
15575 if (!fip->baseclasses.empty () && cu->language != language_ada)
15576 {
15577 int num_bytes = B_BYTES (fip->baseclasses.size ());
15578 unsigned char *pointer;
15579
15580 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15581 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15582 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15583 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15584 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15585 }
15586
15587 if (!fip->variant_parts.empty ())
15588 add_variant_property (fip, type, cu);
15589
15590 /* Copy the saved-up fields into the field vector. */
15591 for (int i = 0; i < nfields; ++i)
15592 {
15593 struct nextfield &field
15594 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15595 : fip->fields[i - fip->baseclasses.size ()]);
15596
15597 type->field (i) = field.field;
15598 switch (field.accessibility)
15599 {
15600 case DW_ACCESS_private:
15601 if (cu->language != language_ada)
15602 SET_TYPE_FIELD_PRIVATE (type, i);
15603 break;
15604
15605 case DW_ACCESS_protected:
15606 if (cu->language != language_ada)
15607 SET_TYPE_FIELD_PROTECTED (type, i);
15608 break;
15609
15610 case DW_ACCESS_public:
15611 break;
15612
15613 default:
15614 /* Unknown accessibility. Complain and treat it as public. */
15615 {
15616 complaint (_("unsupported accessibility %d"),
15617 field.accessibility);
15618 }
15619 break;
15620 }
15621 if (i < fip->baseclasses.size ())
15622 {
15623 switch (field.virtuality)
15624 {
15625 case DW_VIRTUALITY_virtual:
15626 case DW_VIRTUALITY_pure_virtual:
15627 if (cu->language == language_ada)
15628 error (_("unexpected virtuality in component of Ada type"));
15629 SET_TYPE_FIELD_VIRTUAL (type, i);
15630 break;
15631 }
15632 }
15633 }
15634 }
15635
15636 /* Return true if this member function is a constructor, false
15637 otherwise. */
15638
15639 static int
15640 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15641 {
15642 const char *fieldname;
15643 const char *type_name;
15644 int len;
15645
15646 if (die->parent == NULL)
15647 return 0;
15648
15649 if (die->parent->tag != DW_TAG_structure_type
15650 && die->parent->tag != DW_TAG_union_type
15651 && die->parent->tag != DW_TAG_class_type)
15652 return 0;
15653
15654 fieldname = dwarf2_name (die, cu);
15655 type_name = dwarf2_name (die->parent, cu);
15656 if (fieldname == NULL || type_name == NULL)
15657 return 0;
15658
15659 len = strlen (fieldname);
15660 return (strncmp (fieldname, type_name, len) == 0
15661 && (type_name[len] == '\0' || type_name[len] == '<'));
15662 }
15663
15664 /* Add a member function to the proper fieldlist. */
15665
15666 static void
15667 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15668 struct type *type, struct dwarf2_cu *cu)
15669 {
15670 struct objfile *objfile = cu->per_objfile->objfile;
15671 struct attribute *attr;
15672 int i;
15673 struct fnfieldlist *flp = nullptr;
15674 struct fn_field *fnp;
15675 const char *fieldname;
15676 struct type *this_type;
15677
15678 if (cu->language == language_ada)
15679 error (_("unexpected member function in Ada type"));
15680
15681 /* Get name of member function. */
15682 fieldname = dwarf2_name (die, cu);
15683 if (fieldname == NULL)
15684 return;
15685
15686 /* Look up member function name in fieldlist. */
15687 for (i = 0; i < fip->fnfieldlists.size (); i++)
15688 {
15689 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15690 {
15691 flp = &fip->fnfieldlists[i];
15692 break;
15693 }
15694 }
15695
15696 /* Create a new fnfieldlist if necessary. */
15697 if (flp == nullptr)
15698 {
15699 fip->fnfieldlists.emplace_back ();
15700 flp = &fip->fnfieldlists.back ();
15701 flp->name = fieldname;
15702 i = fip->fnfieldlists.size () - 1;
15703 }
15704
15705 /* Create a new member function field and add it to the vector of
15706 fnfieldlists. */
15707 flp->fnfields.emplace_back ();
15708 fnp = &flp->fnfields.back ();
15709
15710 /* Delay processing of the physname until later. */
15711 if (cu->language == language_cplus)
15712 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15713 die, cu);
15714 else
15715 {
15716 const char *physname = dwarf2_physname (fieldname, die, cu);
15717 fnp->physname = physname ? physname : "";
15718 }
15719
15720 fnp->type = alloc_type (objfile);
15721 this_type = read_type_die (die, cu);
15722 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15723 {
15724 int nparams = this_type->num_fields ();
15725
15726 /* TYPE is the domain of this method, and THIS_TYPE is the type
15727 of the method itself (TYPE_CODE_METHOD). */
15728 smash_to_method_type (fnp->type, type,
15729 TYPE_TARGET_TYPE (this_type),
15730 this_type->fields (),
15731 this_type->num_fields (),
15732 this_type->has_varargs ());
15733
15734 /* Handle static member functions.
15735 Dwarf2 has no clean way to discern C++ static and non-static
15736 member functions. G++ helps GDB by marking the first
15737 parameter for non-static member functions (which is the this
15738 pointer) as artificial. We obtain this information from
15739 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15740 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15741 fnp->voffset = VOFFSET_STATIC;
15742 }
15743 else
15744 complaint (_("member function type missing for '%s'"),
15745 dwarf2_full_name (fieldname, die, cu));
15746
15747 /* Get fcontext from DW_AT_containing_type if present. */
15748 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15749 fnp->fcontext = die_containing_type (die, cu);
15750
15751 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15752 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15753
15754 /* Get accessibility. */
15755 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15756 switch (accessibility)
15757 {
15758 case DW_ACCESS_private:
15759 fnp->is_private = 1;
15760 break;
15761 case DW_ACCESS_protected:
15762 fnp->is_protected = 1;
15763 break;
15764 }
15765
15766 /* Check for artificial methods. */
15767 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15768 if (attr && attr->as_boolean ())
15769 fnp->is_artificial = 1;
15770
15771 /* Check for defaulted methods. */
15772 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15773 if (attr != nullptr)
15774 fnp->defaulted = attr->defaulted ();
15775
15776 /* Check for deleted methods. */
15777 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15778 if (attr != nullptr && attr->as_boolean ())
15779 fnp->is_deleted = 1;
15780
15781 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15782
15783 /* Get index in virtual function table if it is a virtual member
15784 function. For older versions of GCC, this is an offset in the
15785 appropriate virtual table, as specified by DW_AT_containing_type.
15786 For everyone else, it is an expression to be evaluated relative
15787 to the object address. */
15788
15789 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15790 if (attr != nullptr)
15791 {
15792 if (attr->form_is_block () && attr->as_block ()->size > 0)
15793 {
15794 struct dwarf_block *block = attr->as_block ();
15795
15796 if (block->data[0] == DW_OP_constu)
15797 {
15798 /* Old-style GCC. */
15799 fnp->voffset = decode_locdesc (block, cu) + 2;
15800 }
15801 else if (block->data[0] == DW_OP_deref
15802 || (block->size > 1
15803 && block->data[0] == DW_OP_deref_size
15804 && block->data[1] == cu->header.addr_size))
15805 {
15806 fnp->voffset = decode_locdesc (block, cu);
15807 if ((fnp->voffset % cu->header.addr_size) != 0)
15808 dwarf2_complex_location_expr_complaint ();
15809 else
15810 fnp->voffset /= cu->header.addr_size;
15811 fnp->voffset += 2;
15812 }
15813 else
15814 dwarf2_complex_location_expr_complaint ();
15815
15816 if (!fnp->fcontext)
15817 {
15818 /* If there is no `this' field and no DW_AT_containing_type,
15819 we cannot actually find a base class context for the
15820 vtable! */
15821 if (this_type->num_fields () == 0
15822 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15823 {
15824 complaint (_("cannot determine context for virtual member "
15825 "function \"%s\" (offset %s)"),
15826 fieldname, sect_offset_str (die->sect_off));
15827 }
15828 else
15829 {
15830 fnp->fcontext
15831 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15832 }
15833 }
15834 }
15835 else if (attr->form_is_section_offset ())
15836 {
15837 dwarf2_complex_location_expr_complaint ();
15838 }
15839 else
15840 {
15841 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15842 fieldname);
15843 }
15844 }
15845 else
15846 {
15847 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15848 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15849 {
15850 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15851 complaint (_("Member function \"%s\" (offset %s) is virtual "
15852 "but the vtable offset is not specified"),
15853 fieldname, sect_offset_str (die->sect_off));
15854 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15855 TYPE_CPLUS_DYNAMIC (type) = 1;
15856 }
15857 }
15858 }
15859
15860 /* Create the vector of member function fields, and attach it to the type. */
15861
15862 static void
15863 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15864 struct dwarf2_cu *cu)
15865 {
15866 if (cu->language == language_ada)
15867 error (_("unexpected member functions in Ada type"));
15868
15869 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15870 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15871 TYPE_ALLOC (type,
15872 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15873
15874 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15875 {
15876 struct fnfieldlist &nf = fip->fnfieldlists[i];
15877 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15878
15879 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15880 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15881 fn_flp->fn_fields = (struct fn_field *)
15882 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15883
15884 for (int k = 0; k < nf.fnfields.size (); ++k)
15885 fn_flp->fn_fields[k] = nf.fnfields[k];
15886 }
15887
15888 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15889 }
15890
15891 /* Returns non-zero if NAME is the name of a vtable member in CU's
15892 language, zero otherwise. */
15893 static int
15894 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15895 {
15896 static const char vptr[] = "_vptr";
15897
15898 /* Look for the C++ form of the vtable. */
15899 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15900 return 1;
15901
15902 return 0;
15903 }
15904
15905 /* GCC outputs unnamed structures that are really pointers to member
15906 functions, with the ABI-specified layout. If TYPE describes
15907 such a structure, smash it into a member function type.
15908
15909 GCC shouldn't do this; it should just output pointer to member DIEs.
15910 This is GCC PR debug/28767. */
15911
15912 static void
15913 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15914 {
15915 struct type *pfn_type, *self_type, *new_type;
15916
15917 /* Check for a structure with no name and two children. */
15918 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15919 return;
15920
15921 /* Check for __pfn and __delta members. */
15922 if (TYPE_FIELD_NAME (type, 0) == NULL
15923 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15924 || TYPE_FIELD_NAME (type, 1) == NULL
15925 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15926 return;
15927
15928 /* Find the type of the method. */
15929 pfn_type = type->field (0).type ();
15930 if (pfn_type == NULL
15931 || pfn_type->code () != TYPE_CODE_PTR
15932 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15933 return;
15934
15935 /* Look for the "this" argument. */
15936 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15937 if (pfn_type->num_fields () == 0
15938 /* || pfn_type->field (0).type () == NULL */
15939 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15940 return;
15941
15942 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15943 new_type = alloc_type (objfile);
15944 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15945 pfn_type->fields (), pfn_type->num_fields (),
15946 pfn_type->has_varargs ());
15947 smash_to_methodptr_type (type, new_type);
15948 }
15949
15950 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15951 requires rewriting, then copy it and return the updated copy.
15952 Otherwise return nullptr. */
15953
15954 static struct type *
15955 rewrite_array_type (struct type *type)
15956 {
15957 if (type->code () != TYPE_CODE_ARRAY)
15958 return nullptr;
15959
15960 struct type *index_type = type->index_type ();
15961 range_bounds *current_bounds = index_type->bounds ();
15962
15963 /* Handle multi-dimensional arrays. */
15964 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15965 if (new_target == nullptr)
15966 {
15967 /* Maybe we don't need to rewrite this array. */
15968 if (current_bounds->low.kind () == PROP_CONST
15969 && current_bounds->high.kind () == PROP_CONST)
15970 return nullptr;
15971 }
15972
15973 /* Either the target type was rewritten, or the bounds have to be
15974 updated. Either way we want to copy the type and update
15975 everything. */
15976 struct type *copy = copy_type (type);
15977 int nfields = copy->num_fields ();
15978 field *new_fields
15979 = ((struct field *) TYPE_ZALLOC (copy,
15980 nfields * sizeof (struct field)));
15981 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15982 copy->set_fields (new_fields);
15983 if (new_target != nullptr)
15984 TYPE_TARGET_TYPE (copy) = new_target;
15985
15986 struct type *index_copy = copy_type (index_type);
15987 range_bounds *bounds
15988 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15989 sizeof (range_bounds));
15990 *bounds = *current_bounds;
15991 bounds->low.set_const_val (1);
15992 bounds->high.set_const_val (0);
15993 index_copy->set_bounds (bounds);
15994 copy->set_index_type (index_copy);
15995
15996 return copy;
15997 }
15998
15999 /* While some versions of GCC will generate complicated DWARF for an
16000 array (see quirk_ada_thick_pointer), more recent versions were
16001 modified to emit an explicit thick pointer structure. However, in
16002 this case, the array still has DWARF expressions for its ranges,
16003 and these must be ignored. */
16004
16005 static void
16006 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
16007 struct type *type)
16008 {
16009 gdb_assert (cu->language == language_ada);
16010
16011 /* Check for a structure with two children. */
16012 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
16013 return;
16014
16015 /* Check for P_ARRAY and P_BOUNDS members. */
16016 if (TYPE_FIELD_NAME (type, 0) == NULL
16017 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
16018 || TYPE_FIELD_NAME (type, 1) == NULL
16019 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
16020 return;
16021
16022 /* Make sure we're looking at a pointer to an array. */
16023 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
16024 return;
16025
16026 /* The Ada code already knows how to handle these types, so all that
16027 we need to do is turn the bounds into static bounds. However, we
16028 don't want to rewrite existing array or index types in-place,
16029 because those may be referenced in other contexts where this
16030 rewriting is undesirable. */
16031 struct type *new_ary_type
16032 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16033 if (new_ary_type != nullptr)
16034 type->field (0).set_type (lookup_pointer_type (new_ary_type));
16035 }
16036
16037 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
16038 appropriate error checking and issuing complaints if there is a
16039 problem. */
16040
16041 static ULONGEST
16042 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16043 {
16044 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16045
16046 if (attr == nullptr)
16047 return 0;
16048
16049 if (!attr->form_is_constant ())
16050 {
16051 complaint (_("DW_AT_alignment must have constant form"
16052 " - DIE at %s [in module %s]"),
16053 sect_offset_str (die->sect_off),
16054 objfile_name (cu->per_objfile->objfile));
16055 return 0;
16056 }
16057
16058 LONGEST val = attr->constant_value (0);
16059 if (val < 0)
16060 {
16061 complaint (_("DW_AT_alignment value must not be negative"
16062 " - DIE at %s [in module %s]"),
16063 sect_offset_str (die->sect_off),
16064 objfile_name (cu->per_objfile->objfile));
16065 return 0;
16066 }
16067 ULONGEST align = val;
16068
16069 if (align == 0)
16070 {
16071 complaint (_("DW_AT_alignment value must not be zero"
16072 " - DIE at %s [in module %s]"),
16073 sect_offset_str (die->sect_off),
16074 objfile_name (cu->per_objfile->objfile));
16075 return 0;
16076 }
16077 if ((align & (align - 1)) != 0)
16078 {
16079 complaint (_("DW_AT_alignment value must be a power of 2"
16080 " - DIE at %s [in module %s]"),
16081 sect_offset_str (die->sect_off),
16082 objfile_name (cu->per_objfile->objfile));
16083 return 0;
16084 }
16085
16086 return align;
16087 }
16088
16089 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16090 the alignment for TYPE. */
16091
16092 static void
16093 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16094 struct type *type)
16095 {
16096 if (!set_type_align (type, get_alignment (cu, die)))
16097 complaint (_("DW_AT_alignment value too large"
16098 " - DIE at %s [in module %s]"),
16099 sect_offset_str (die->sect_off),
16100 objfile_name (cu->per_objfile->objfile));
16101 }
16102
16103 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16104 constant for a type, according to DWARF5 spec, Table 5.5. */
16105
16106 static bool
16107 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16108 {
16109 switch (value)
16110 {
16111 case DW_CC_normal:
16112 case DW_CC_pass_by_reference:
16113 case DW_CC_pass_by_value:
16114 return true;
16115
16116 default:
16117 complaint (_("unrecognized DW_AT_calling_convention value "
16118 "(%s) for a type"), pulongest (value));
16119 return false;
16120 }
16121 }
16122
16123 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16124 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16125 also according to GNU-specific values (see include/dwarf2.h). */
16126
16127 static bool
16128 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16129 {
16130 switch (value)
16131 {
16132 case DW_CC_normal:
16133 case DW_CC_program:
16134 case DW_CC_nocall:
16135 return true;
16136
16137 case DW_CC_GNU_renesas_sh:
16138 case DW_CC_GNU_borland_fastcall_i386:
16139 case DW_CC_GDB_IBM_OpenCL:
16140 return true;
16141
16142 default:
16143 complaint (_("unrecognized DW_AT_calling_convention value "
16144 "(%s) for a subroutine"), pulongest (value));
16145 return false;
16146 }
16147 }
16148
16149 /* Called when we find the DIE that starts a structure or union scope
16150 (definition) to create a type for the structure or union. Fill in
16151 the type's name and general properties; the members will not be
16152 processed until process_structure_scope. A symbol table entry for
16153 the type will also not be done until process_structure_scope (assuming
16154 the type has a name).
16155
16156 NOTE: we need to call these functions regardless of whether or not the
16157 DIE has a DW_AT_name attribute, since it might be an anonymous
16158 structure or union. This gets the type entered into our set of
16159 user defined types. */
16160
16161 static struct type *
16162 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16163 {
16164 struct objfile *objfile = cu->per_objfile->objfile;
16165 struct type *type;
16166 struct attribute *attr;
16167 const char *name;
16168
16169 /* If the definition of this type lives in .debug_types, read that type.
16170 Don't follow DW_AT_specification though, that will take us back up
16171 the chain and we want to go down. */
16172 attr = die->attr (DW_AT_signature);
16173 if (attr != nullptr)
16174 {
16175 type = get_DW_AT_signature_type (die, attr, cu);
16176
16177 /* The type's CU may not be the same as CU.
16178 Ensure TYPE is recorded with CU in die_type_hash. */
16179 return set_die_type (die, type, cu);
16180 }
16181
16182 type = alloc_type (objfile);
16183 INIT_CPLUS_SPECIFIC (type);
16184
16185 name = dwarf2_name (die, cu);
16186 if (name != NULL)
16187 {
16188 if (cu->language == language_cplus
16189 || cu->language == language_d
16190 || cu->language == language_rust)
16191 {
16192 const char *full_name = dwarf2_full_name (name, die, cu);
16193
16194 /* dwarf2_full_name might have already finished building the DIE's
16195 type. If so, there is no need to continue. */
16196 if (get_die_type (die, cu) != NULL)
16197 return get_die_type (die, cu);
16198
16199 type->set_name (full_name);
16200 }
16201 else
16202 {
16203 /* The name is already allocated along with this objfile, so
16204 we don't need to duplicate it for the type. */
16205 type->set_name (name);
16206 }
16207 }
16208
16209 if (die->tag == DW_TAG_structure_type)
16210 {
16211 type->set_code (TYPE_CODE_STRUCT);
16212 }
16213 else if (die->tag == DW_TAG_union_type)
16214 {
16215 type->set_code (TYPE_CODE_UNION);
16216 }
16217 else
16218 {
16219 type->set_code (TYPE_CODE_STRUCT);
16220 }
16221
16222 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16223 TYPE_DECLARED_CLASS (type) = 1;
16224
16225 /* Store the calling convention in the type if it's available in
16226 the die. Otherwise the calling convention remains set to
16227 the default value DW_CC_normal. */
16228 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16229 if (attr != nullptr
16230 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16231 {
16232 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16233 TYPE_CPLUS_CALLING_CONVENTION (type)
16234 = (enum dwarf_calling_convention) (attr->constant_value (0));
16235 }
16236
16237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16238 if (attr != nullptr)
16239 {
16240 if (attr->form_is_constant ())
16241 TYPE_LENGTH (type) = attr->constant_value (0);
16242 else
16243 {
16244 struct dynamic_prop prop;
16245 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16246 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16247 TYPE_LENGTH (type) = 0;
16248 }
16249 }
16250 else
16251 {
16252 TYPE_LENGTH (type) = 0;
16253 }
16254
16255 maybe_set_alignment (cu, die, type);
16256
16257 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16258 {
16259 /* ICC<14 does not output the required DW_AT_declaration on
16260 incomplete types, but gives them a size of zero. */
16261 type->set_is_stub (true);
16262 }
16263 else
16264 type->set_stub_is_supported (true);
16265
16266 if (die_is_declaration (die, cu))
16267 type->set_is_stub (true);
16268 else if (attr == NULL && die->child == NULL
16269 && producer_is_realview (cu->producer))
16270 /* RealView does not output the required DW_AT_declaration
16271 on incomplete types. */
16272 type->set_is_stub (true);
16273
16274 /* We need to add the type field to the die immediately so we don't
16275 infinitely recurse when dealing with pointers to the structure
16276 type within the structure itself. */
16277 set_die_type (die, type, cu);
16278
16279 /* set_die_type should be already done. */
16280 set_descriptive_type (type, die, cu);
16281
16282 return type;
16283 }
16284
16285 static void handle_struct_member_die
16286 (struct die_info *child_die,
16287 struct type *type,
16288 struct field_info *fi,
16289 std::vector<struct symbol *> *template_args,
16290 struct dwarf2_cu *cu);
16291
16292 /* A helper for handle_struct_member_die that handles
16293 DW_TAG_variant_part. */
16294
16295 static void
16296 handle_variant_part (struct die_info *die, struct type *type,
16297 struct field_info *fi,
16298 std::vector<struct symbol *> *template_args,
16299 struct dwarf2_cu *cu)
16300 {
16301 variant_part_builder *new_part;
16302 if (fi->current_variant_part == nullptr)
16303 {
16304 fi->variant_parts.emplace_back ();
16305 new_part = &fi->variant_parts.back ();
16306 }
16307 else if (!fi->current_variant_part->processing_variant)
16308 {
16309 complaint (_("nested DW_TAG_variant_part seen "
16310 "- DIE at %s [in module %s]"),
16311 sect_offset_str (die->sect_off),
16312 objfile_name (cu->per_objfile->objfile));
16313 return;
16314 }
16315 else
16316 {
16317 variant_field &current = fi->current_variant_part->variants.back ();
16318 current.variant_parts.emplace_back ();
16319 new_part = &current.variant_parts.back ();
16320 }
16321
16322 /* When we recurse, we want callees to add to this new variant
16323 part. */
16324 scoped_restore save_current_variant_part
16325 = make_scoped_restore (&fi->current_variant_part, new_part);
16326
16327 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16328 if (discr == NULL)
16329 {
16330 /* It's a univariant form, an extension we support. */
16331 }
16332 else if (discr->form_is_ref ())
16333 {
16334 struct dwarf2_cu *target_cu = cu;
16335 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16336
16337 new_part->discriminant_offset = target_die->sect_off;
16338 }
16339 else
16340 {
16341 complaint (_("DW_AT_discr does not have DIE reference form"
16342 " - DIE at %s [in module %s]"),
16343 sect_offset_str (die->sect_off),
16344 objfile_name (cu->per_objfile->objfile));
16345 }
16346
16347 for (die_info *child_die = die->child;
16348 child_die != NULL;
16349 child_die = child_die->sibling)
16350 handle_struct_member_die (child_die, type, fi, template_args, cu);
16351 }
16352
16353 /* A helper for handle_struct_member_die that handles
16354 DW_TAG_variant. */
16355
16356 static void
16357 handle_variant (struct die_info *die, struct type *type,
16358 struct field_info *fi,
16359 std::vector<struct symbol *> *template_args,
16360 struct dwarf2_cu *cu)
16361 {
16362 if (fi->current_variant_part == nullptr)
16363 {
16364 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16365 "- DIE at %s [in module %s]"),
16366 sect_offset_str (die->sect_off),
16367 objfile_name (cu->per_objfile->objfile));
16368 return;
16369 }
16370 if (fi->current_variant_part->processing_variant)
16371 {
16372 complaint (_("nested DW_TAG_variant seen "
16373 "- DIE at %s [in module %s]"),
16374 sect_offset_str (die->sect_off),
16375 objfile_name (cu->per_objfile->objfile));
16376 return;
16377 }
16378
16379 scoped_restore save_processing_variant
16380 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16381 true);
16382
16383 fi->current_variant_part->variants.emplace_back ();
16384 variant_field &variant = fi->current_variant_part->variants.back ();
16385 variant.first_field = fi->fields.size ();
16386
16387 /* In a variant we want to get the discriminant and also add a
16388 field for our sole member child. */
16389 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16390 if (discr == nullptr || !discr->form_is_constant ())
16391 {
16392 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16393 if (discr == nullptr || discr->as_block ()->size == 0)
16394 variant.default_branch = true;
16395 else
16396 variant.discr_list_data = discr->as_block ();
16397 }
16398 else
16399 variant.discriminant_value = discr->constant_value (0);
16400
16401 for (die_info *variant_child = die->child;
16402 variant_child != NULL;
16403 variant_child = variant_child->sibling)
16404 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16405
16406 variant.last_field = fi->fields.size ();
16407 }
16408
16409 /* A helper for process_structure_scope that handles a single member
16410 DIE. */
16411
16412 static void
16413 handle_struct_member_die (struct die_info *child_die, struct type *type,
16414 struct field_info *fi,
16415 std::vector<struct symbol *> *template_args,
16416 struct dwarf2_cu *cu)
16417 {
16418 if (child_die->tag == DW_TAG_member
16419 || child_die->tag == DW_TAG_variable)
16420 {
16421 /* NOTE: carlton/2002-11-05: A C++ static data member
16422 should be a DW_TAG_member that is a declaration, but
16423 all versions of G++ as of this writing (so through at
16424 least 3.2.1) incorrectly generate DW_TAG_variable
16425 tags for them instead. */
16426 dwarf2_add_field (fi, child_die, cu);
16427 }
16428 else if (child_die->tag == DW_TAG_subprogram)
16429 {
16430 /* Rust doesn't have member functions in the C++ sense.
16431 However, it does emit ordinary functions as children
16432 of a struct DIE. */
16433 if (cu->language == language_rust)
16434 read_func_scope (child_die, cu);
16435 else
16436 {
16437 /* C++ member function. */
16438 dwarf2_add_member_fn (fi, child_die, type, cu);
16439 }
16440 }
16441 else if (child_die->tag == DW_TAG_inheritance)
16442 {
16443 /* C++ base class field. */
16444 dwarf2_add_field (fi, child_die, cu);
16445 }
16446 else if (type_can_define_types (child_die))
16447 dwarf2_add_type_defn (fi, child_die, cu);
16448 else if (child_die->tag == DW_TAG_template_type_param
16449 || child_die->tag == DW_TAG_template_value_param)
16450 {
16451 struct symbol *arg = new_symbol (child_die, NULL, cu);
16452
16453 if (arg != NULL)
16454 template_args->push_back (arg);
16455 }
16456 else if (child_die->tag == DW_TAG_variant_part)
16457 handle_variant_part (child_die, type, fi, template_args, cu);
16458 else if (child_die->tag == DW_TAG_variant)
16459 handle_variant (child_die, type, fi, template_args, cu);
16460 }
16461
16462 /* Finish creating a structure or union type, including filling in
16463 its members and creating a symbol for it. */
16464
16465 static void
16466 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16467 {
16468 struct objfile *objfile = cu->per_objfile->objfile;
16469 struct die_info *child_die;
16470 struct type *type;
16471
16472 type = get_die_type (die, cu);
16473 if (type == NULL)
16474 type = read_structure_type (die, cu);
16475
16476 bool has_template_parameters = false;
16477 if (die->child != NULL && ! die_is_declaration (die, cu))
16478 {
16479 struct field_info fi;
16480 std::vector<struct symbol *> template_args;
16481
16482 child_die = die->child;
16483
16484 while (child_die && child_die->tag)
16485 {
16486 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16487 child_die = child_die->sibling;
16488 }
16489
16490 /* Attach template arguments to type. */
16491 if (!template_args.empty ())
16492 {
16493 has_template_parameters = true;
16494 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16495 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16496 TYPE_TEMPLATE_ARGUMENTS (type)
16497 = XOBNEWVEC (&objfile->objfile_obstack,
16498 struct symbol *,
16499 TYPE_N_TEMPLATE_ARGUMENTS (type));
16500 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16501 template_args.data (),
16502 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16503 * sizeof (struct symbol *)));
16504 }
16505
16506 /* Attach fields and member functions to the type. */
16507 if (fi.nfields () > 0)
16508 dwarf2_attach_fields_to_type (&fi, type, cu);
16509 if (!fi.fnfieldlists.empty ())
16510 {
16511 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16512
16513 /* Get the type which refers to the base class (possibly this
16514 class itself) which contains the vtable pointer for the current
16515 class from the DW_AT_containing_type attribute. This use of
16516 DW_AT_containing_type is a GNU extension. */
16517
16518 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16519 {
16520 struct type *t = die_containing_type (die, cu);
16521
16522 set_type_vptr_basetype (type, t);
16523 if (type == t)
16524 {
16525 int i;
16526
16527 /* Our own class provides vtbl ptr. */
16528 for (i = t->num_fields () - 1;
16529 i >= TYPE_N_BASECLASSES (t);
16530 --i)
16531 {
16532 const char *fieldname = TYPE_FIELD_NAME (t, i);
16533
16534 if (is_vtable_name (fieldname, cu))
16535 {
16536 set_type_vptr_fieldno (type, i);
16537 break;
16538 }
16539 }
16540
16541 /* Complain if virtual function table field not found. */
16542 if (i < TYPE_N_BASECLASSES (t))
16543 complaint (_("virtual function table pointer "
16544 "not found when defining class '%s'"),
16545 type->name () ? type->name () : "");
16546 }
16547 else
16548 {
16549 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16550 }
16551 }
16552 else if (cu->producer
16553 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16554 {
16555 /* The IBM XLC compiler does not provide direct indication
16556 of the containing type, but the vtable pointer is
16557 always named __vfp. */
16558
16559 int i;
16560
16561 for (i = type->num_fields () - 1;
16562 i >= TYPE_N_BASECLASSES (type);
16563 --i)
16564 {
16565 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16566 {
16567 set_type_vptr_fieldno (type, i);
16568 set_type_vptr_basetype (type, type);
16569 break;
16570 }
16571 }
16572 }
16573 }
16574
16575 /* Copy fi.typedef_field_list linked list elements content into the
16576 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16577 if (!fi.typedef_field_list.empty ())
16578 {
16579 int count = fi.typedef_field_list.size ();
16580
16581 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16582 TYPE_TYPEDEF_FIELD_ARRAY (type)
16583 = ((struct decl_field *)
16584 TYPE_ALLOC (type,
16585 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16586 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16587
16588 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16589 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16590 }
16591
16592 /* Copy fi.nested_types_list linked list elements content into the
16593 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16594 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16595 {
16596 int count = fi.nested_types_list.size ();
16597
16598 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16599 TYPE_NESTED_TYPES_ARRAY (type)
16600 = ((struct decl_field *)
16601 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16602 TYPE_NESTED_TYPES_COUNT (type) = count;
16603
16604 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16605 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16606 }
16607 }
16608
16609 quirk_gcc_member_function_pointer (type, objfile);
16610 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16611 cu->rust_unions.push_back (type);
16612 else if (cu->language == language_ada)
16613 quirk_ada_thick_pointer_struct (die, cu, type);
16614
16615 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16616 snapshots) has been known to create a die giving a declaration
16617 for a class that has, as a child, a die giving a definition for a
16618 nested class. So we have to process our children even if the
16619 current die is a declaration. Normally, of course, a declaration
16620 won't have any children at all. */
16621
16622 child_die = die->child;
16623
16624 while (child_die != NULL && child_die->tag)
16625 {
16626 if (child_die->tag == DW_TAG_member
16627 || child_die->tag == DW_TAG_variable
16628 || child_die->tag == DW_TAG_inheritance
16629 || child_die->tag == DW_TAG_template_value_param
16630 || child_die->tag == DW_TAG_template_type_param)
16631 {
16632 /* Do nothing. */
16633 }
16634 else
16635 process_die (child_die, cu);
16636
16637 child_die = child_die->sibling;
16638 }
16639
16640 /* Do not consider external references. According to the DWARF standard,
16641 these DIEs are identified by the fact that they have no byte_size
16642 attribute, and a declaration attribute. */
16643 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16644 || !die_is_declaration (die, cu)
16645 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16646 {
16647 struct symbol *sym = new_symbol (die, type, cu);
16648
16649 if (has_template_parameters)
16650 {
16651 struct symtab *symtab;
16652 if (sym != nullptr)
16653 symtab = symbol_symtab (sym);
16654 else if (cu->line_header != nullptr)
16655 {
16656 /* Any related symtab will do. */
16657 symtab
16658 = cu->line_header->file_names ()[0].symtab;
16659 }
16660 else
16661 {
16662 symtab = nullptr;
16663 complaint (_("could not find suitable "
16664 "symtab for template parameter"
16665 " - DIE at %s [in module %s]"),
16666 sect_offset_str (die->sect_off),
16667 objfile_name (objfile));
16668 }
16669
16670 if (symtab != nullptr)
16671 {
16672 /* Make sure that the symtab is set on the new symbols.
16673 Even though they don't appear in this symtab directly,
16674 other parts of gdb assume that symbols do, and this is
16675 reasonably true. */
16676 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16677 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16678 }
16679 }
16680 }
16681 }
16682
16683 /* Assuming DIE is an enumeration type, and TYPE is its associated
16684 type, update TYPE using some information only available in DIE's
16685 children. In particular, the fields are computed. */
16686
16687 static void
16688 update_enumeration_type_from_children (struct die_info *die,
16689 struct type *type,
16690 struct dwarf2_cu *cu)
16691 {
16692 struct die_info *child_die;
16693 int unsigned_enum = 1;
16694 int flag_enum = 1;
16695
16696 auto_obstack obstack;
16697 std::vector<struct field> fields;
16698
16699 for (child_die = die->child;
16700 child_die != NULL && child_die->tag;
16701 child_die = child_die->sibling)
16702 {
16703 struct attribute *attr;
16704 LONGEST value;
16705 const gdb_byte *bytes;
16706 struct dwarf2_locexpr_baton *baton;
16707 const char *name;
16708
16709 if (child_die->tag != DW_TAG_enumerator)
16710 continue;
16711
16712 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16713 if (attr == NULL)
16714 continue;
16715
16716 name = dwarf2_name (child_die, cu);
16717 if (name == NULL)
16718 name = "<anonymous enumerator>";
16719
16720 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16721 &value, &bytes, &baton);
16722 if (value < 0)
16723 {
16724 unsigned_enum = 0;
16725 flag_enum = 0;
16726 }
16727 else
16728 {
16729 if (count_one_bits_ll (value) >= 2)
16730 flag_enum = 0;
16731 }
16732
16733 fields.emplace_back ();
16734 struct field &field = fields.back ();
16735 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16736 SET_FIELD_ENUMVAL (field, value);
16737 }
16738
16739 if (!fields.empty ())
16740 {
16741 type->set_num_fields (fields.size ());
16742 type->set_fields
16743 ((struct field *)
16744 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16745 memcpy (type->fields (), fields.data (),
16746 sizeof (struct field) * fields.size ());
16747 }
16748
16749 if (unsigned_enum)
16750 type->set_is_unsigned (true);
16751
16752 if (flag_enum)
16753 TYPE_FLAG_ENUM (type) = 1;
16754 }
16755
16756 /* Given a DW_AT_enumeration_type die, set its type. We do not
16757 complete the type's fields yet, or create any symbols. */
16758
16759 static struct type *
16760 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16761 {
16762 struct objfile *objfile = cu->per_objfile->objfile;
16763 struct type *type;
16764 struct attribute *attr;
16765 const char *name;
16766
16767 /* If the definition of this type lives in .debug_types, read that type.
16768 Don't follow DW_AT_specification though, that will take us back up
16769 the chain and we want to go down. */
16770 attr = die->attr (DW_AT_signature);
16771 if (attr != nullptr)
16772 {
16773 type = get_DW_AT_signature_type (die, attr, cu);
16774
16775 /* The type's CU may not be the same as CU.
16776 Ensure TYPE is recorded with CU in die_type_hash. */
16777 return set_die_type (die, type, cu);
16778 }
16779
16780 type = alloc_type (objfile);
16781
16782 type->set_code (TYPE_CODE_ENUM);
16783 name = dwarf2_full_name (NULL, die, cu);
16784 if (name != NULL)
16785 type->set_name (name);
16786
16787 attr = dwarf2_attr (die, DW_AT_type, cu);
16788 if (attr != NULL)
16789 {
16790 struct type *underlying_type = die_type (die, cu);
16791
16792 TYPE_TARGET_TYPE (type) = underlying_type;
16793 }
16794
16795 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16796 if (attr != nullptr)
16797 {
16798 TYPE_LENGTH (type) = attr->constant_value (0);
16799 }
16800 else
16801 {
16802 TYPE_LENGTH (type) = 0;
16803 }
16804
16805 maybe_set_alignment (cu, die, type);
16806
16807 /* The enumeration DIE can be incomplete. In Ada, any type can be
16808 declared as private in the package spec, and then defined only
16809 inside the package body. Such types are known as Taft Amendment
16810 Types. When another package uses such a type, an incomplete DIE
16811 may be generated by the compiler. */
16812 if (die_is_declaration (die, cu))
16813 type->set_is_stub (true);
16814
16815 /* If this type has an underlying type that is not a stub, then we
16816 may use its attributes. We always use the "unsigned" attribute
16817 in this situation, because ordinarily we guess whether the type
16818 is unsigned -- but the guess can be wrong and the underlying type
16819 can tell us the reality. However, we defer to a local size
16820 attribute if one exists, because this lets the compiler override
16821 the underlying type if needed. */
16822 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16823 {
16824 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16825 underlying_type = check_typedef (underlying_type);
16826
16827 type->set_is_unsigned (underlying_type->is_unsigned ());
16828
16829 if (TYPE_LENGTH (type) == 0)
16830 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16831
16832 if (TYPE_RAW_ALIGN (type) == 0
16833 && TYPE_RAW_ALIGN (underlying_type) != 0)
16834 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16835 }
16836
16837 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16838
16839 set_die_type (die, type, cu);
16840
16841 /* Finish the creation of this type by using the enum's children.
16842 Note that, as usual, this must come after set_die_type to avoid
16843 infinite recursion when trying to compute the names of the
16844 enumerators. */
16845 update_enumeration_type_from_children (die, type, cu);
16846
16847 return type;
16848 }
16849
16850 /* Given a pointer to a die which begins an enumeration, process all
16851 the dies that define the members of the enumeration, and create the
16852 symbol for the enumeration type.
16853
16854 NOTE: We reverse the order of the element list. */
16855
16856 static void
16857 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16858 {
16859 struct type *this_type;
16860
16861 this_type = get_die_type (die, cu);
16862 if (this_type == NULL)
16863 this_type = read_enumeration_type (die, cu);
16864
16865 if (die->child != NULL)
16866 {
16867 struct die_info *child_die;
16868 const char *name;
16869
16870 child_die = die->child;
16871 while (child_die && child_die->tag)
16872 {
16873 if (child_die->tag != DW_TAG_enumerator)
16874 {
16875 process_die (child_die, cu);
16876 }
16877 else
16878 {
16879 name = dwarf2_name (child_die, cu);
16880 if (name)
16881 new_symbol (child_die, this_type, cu);
16882 }
16883
16884 child_die = child_die->sibling;
16885 }
16886 }
16887
16888 /* If we are reading an enum from a .debug_types unit, and the enum
16889 is a declaration, and the enum is not the signatured type in the
16890 unit, then we do not want to add a symbol for it. Adding a
16891 symbol would in some cases obscure the true definition of the
16892 enum, giving users an incomplete type when the definition is
16893 actually available. Note that we do not want to do this for all
16894 enums which are just declarations, because C++0x allows forward
16895 enum declarations. */
16896 if (cu->per_cu->is_debug_types
16897 && die_is_declaration (die, cu))
16898 {
16899 struct signatured_type *sig_type;
16900
16901 sig_type = (struct signatured_type *) cu->per_cu;
16902 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16903 if (sig_type->type_offset_in_section != die->sect_off)
16904 return;
16905 }
16906
16907 new_symbol (die, this_type, cu);
16908 }
16909
16910 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16911 expression for an index type and finds the corresponding field
16912 offset in the hidden "P_BOUNDS" structure. Returns true on success
16913 and updates *FIELD, false if it fails to recognize an
16914 expression. */
16915
16916 static bool
16917 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16918 int *bounds_offset, struct field *field,
16919 struct dwarf2_cu *cu)
16920 {
16921 struct attribute *attr = dwarf2_attr (die, name, cu);
16922 if (attr == nullptr || !attr->form_is_block ())
16923 return false;
16924
16925 const struct dwarf_block *block = attr->as_block ();
16926 const gdb_byte *start = block->data;
16927 const gdb_byte *end = block->data + block->size;
16928
16929 /* The expression to recognize generally looks like:
16930
16931 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16932 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16933
16934 However, the second "plus_uconst" may be missing:
16935
16936 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16937 DW_OP_deref_size: 4)
16938
16939 This happens when the field is at the start of the structure.
16940
16941 Also, the final deref may not be sized:
16942
16943 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16944 DW_OP_deref)
16945
16946 This happens when the size of the index type happens to be the
16947 same as the architecture's word size. This can occur with or
16948 without the second plus_uconst. */
16949
16950 if (end - start < 2)
16951 return false;
16952 if (*start++ != DW_OP_push_object_address)
16953 return false;
16954 if (*start++ != DW_OP_plus_uconst)
16955 return false;
16956
16957 uint64_t this_bound_off;
16958 start = gdb_read_uleb128 (start, end, &this_bound_off);
16959 if (start == nullptr || (int) this_bound_off != this_bound_off)
16960 return false;
16961 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16962 is consistent among all bounds. */
16963 if (*bounds_offset == -1)
16964 *bounds_offset = this_bound_off;
16965 else if (*bounds_offset != this_bound_off)
16966 return false;
16967
16968 if (start == end || *start++ != DW_OP_deref)
16969 return false;
16970
16971 int offset = 0;
16972 if (start ==end)
16973 return false;
16974 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16975 {
16976 /* This means an offset of 0. */
16977 }
16978 else if (*start++ != DW_OP_plus_uconst)
16979 return false;
16980 else
16981 {
16982 /* The size is the parameter to DW_OP_plus_uconst. */
16983 uint64_t val;
16984 start = gdb_read_uleb128 (start, end, &val);
16985 if (start == nullptr)
16986 return false;
16987 if ((int) val != val)
16988 return false;
16989 offset = val;
16990 }
16991
16992 if (start == end)
16993 return false;
16994
16995 uint64_t size;
16996 if (*start == DW_OP_deref_size)
16997 {
16998 start = gdb_read_uleb128 (start + 1, end, &size);
16999 if (start == nullptr)
17000 return false;
17001 }
17002 else if (*start == DW_OP_deref)
17003 {
17004 size = cu->header.addr_size;
17005 ++start;
17006 }
17007 else
17008 return false;
17009
17010 SET_FIELD_BITPOS (*field, 8 * offset);
17011 if (size != TYPE_LENGTH (field->type ()))
17012 FIELD_BITSIZE (*field) = 8 * size;
17013
17014 return true;
17015 }
17016
17017 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
17018 some kinds of Ada arrays:
17019
17020 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
17021 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
17022 <11e0> DW_AT_data_location: 2 byte block: 97 6
17023 (DW_OP_push_object_address; DW_OP_deref)
17024 <11e3> DW_AT_type : <0x1173>
17025 <11e7> DW_AT_sibling : <0x1201>
17026 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
17027 <11ec> DW_AT_type : <0x1206>
17028 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
17029 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17030 DW_OP_deref_size: 4)
17031 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17032 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17033 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17034
17035 This actually represents a "thick pointer", which is a structure
17036 with two elements: one that is a pointer to the array data, and one
17037 that is a pointer to another structure; this second structure holds
17038 the array bounds.
17039
17040 This returns a new type on success, or nullptr if this didn't
17041 recognize the type. */
17042
17043 static struct type *
17044 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17045 struct type *type)
17046 {
17047 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17048 /* So far we've only seen this with block form. */
17049 if (attr == nullptr || !attr->form_is_block ())
17050 return nullptr;
17051
17052 /* Note that this will fail if the structure layout is changed by
17053 the compiler. However, we have no good way to recognize some
17054 other layout, because we don't know what expression the compiler
17055 might choose to emit should this happen. */
17056 struct dwarf_block *blk = attr->as_block ();
17057 if (blk->size != 2
17058 || blk->data[0] != DW_OP_push_object_address
17059 || blk->data[1] != DW_OP_deref)
17060 return nullptr;
17061
17062 int bounds_offset = -1;
17063 int max_align = -1;
17064 std::vector<struct field> range_fields;
17065 for (struct die_info *child_die = die->child;
17066 child_die;
17067 child_die = child_die->sibling)
17068 {
17069 if (child_die->tag == DW_TAG_subrange_type)
17070 {
17071 struct type *underlying = read_subrange_index_type (child_die, cu);
17072
17073 int this_align = type_align (underlying);
17074 if (this_align > max_align)
17075 max_align = this_align;
17076
17077 range_fields.emplace_back ();
17078 range_fields.emplace_back ();
17079
17080 struct field &lower = range_fields[range_fields.size () - 2];
17081 struct field &upper = range_fields[range_fields.size () - 1];
17082
17083 lower.set_type (underlying);
17084 FIELD_ARTIFICIAL (lower) = 1;
17085
17086 upper.set_type (underlying);
17087 FIELD_ARTIFICIAL (upper) = 1;
17088
17089 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17090 &bounds_offset, &lower, cu)
17091 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17092 &bounds_offset, &upper, cu))
17093 return nullptr;
17094 }
17095 }
17096
17097 /* This shouldn't really happen, but double-check that we found
17098 where the bounds are stored. */
17099 if (bounds_offset == -1)
17100 return nullptr;
17101
17102 struct objfile *objfile = cu->per_objfile->objfile;
17103 for (int i = 0; i < range_fields.size (); i += 2)
17104 {
17105 char name[20];
17106
17107 /* Set the name of each field in the bounds. */
17108 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17109 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17110 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17111 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17112 }
17113
17114 struct type *bounds = alloc_type (objfile);
17115 bounds->set_code (TYPE_CODE_STRUCT);
17116
17117 bounds->set_num_fields (range_fields.size ());
17118 bounds->set_fields
17119 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17120 * sizeof (struct field))));
17121 memcpy (bounds->fields (), range_fields.data (),
17122 bounds->num_fields () * sizeof (struct field));
17123
17124 int last_fieldno = range_fields.size () - 1;
17125 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17126 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17127 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17128
17129 /* Rewrite the existing array type in place. Specifically, we
17130 remove any dynamic properties we might have read, and we replace
17131 the index types. */
17132 struct type *iter = type;
17133 for (int i = 0; i < range_fields.size (); i += 2)
17134 {
17135 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17136 iter->main_type->dyn_prop_list = nullptr;
17137 iter->set_index_type
17138 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17139 iter = TYPE_TARGET_TYPE (iter);
17140 }
17141
17142 struct type *result = alloc_type (objfile);
17143 result->set_code (TYPE_CODE_STRUCT);
17144
17145 result->set_num_fields (2);
17146 result->set_fields
17147 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17148 * sizeof (struct field))));
17149
17150 /* The names are chosen to coincide with what the compiler does with
17151 -fgnat-encodings=all, which the Ada code in gdb already
17152 understands. */
17153 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17154 result->field (0).set_type (lookup_pointer_type (type));
17155
17156 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17157 result->field (1).set_type (lookup_pointer_type (bounds));
17158 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17159
17160 result->set_name (type->name ());
17161 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17162 + TYPE_LENGTH (result->field (1).type ()));
17163
17164 return result;
17165 }
17166
17167 /* Extract all information from a DW_TAG_array_type DIE and put it in
17168 the DIE's type field. For now, this only handles one dimensional
17169 arrays. */
17170
17171 static struct type *
17172 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17173 {
17174 struct objfile *objfile = cu->per_objfile->objfile;
17175 struct die_info *child_die;
17176 struct type *type;
17177 struct type *element_type, *range_type, *index_type;
17178 struct attribute *attr;
17179 const char *name;
17180 struct dynamic_prop *byte_stride_prop = NULL;
17181 unsigned int bit_stride = 0;
17182
17183 element_type = die_type (die, cu);
17184
17185 /* The die_type call above may have already set the type for this DIE. */
17186 type = get_die_type (die, cu);
17187 if (type)
17188 return type;
17189
17190 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17191 if (attr != NULL)
17192 {
17193 int stride_ok;
17194 struct type *prop_type = cu->addr_sized_int_type (false);
17195
17196 byte_stride_prop
17197 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17198 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17199 prop_type);
17200 if (!stride_ok)
17201 {
17202 complaint (_("unable to read array DW_AT_byte_stride "
17203 " - DIE at %s [in module %s]"),
17204 sect_offset_str (die->sect_off),
17205 objfile_name (cu->per_objfile->objfile));
17206 /* Ignore this attribute. We will likely not be able to print
17207 arrays of this type correctly, but there is little we can do
17208 to help if we cannot read the attribute's value. */
17209 byte_stride_prop = NULL;
17210 }
17211 }
17212
17213 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17214 if (attr != NULL)
17215 bit_stride = attr->constant_value (0);
17216
17217 /* Irix 6.2 native cc creates array types without children for
17218 arrays with unspecified length. */
17219 if (die->child == NULL)
17220 {
17221 index_type = objfile_type (objfile)->builtin_int;
17222 range_type = create_static_range_type (NULL, index_type, 0, -1);
17223 type = create_array_type_with_stride (NULL, element_type, range_type,
17224 byte_stride_prop, bit_stride);
17225 return set_die_type (die, type, cu);
17226 }
17227
17228 std::vector<struct type *> range_types;
17229 child_die = die->child;
17230 while (child_die && child_die->tag)
17231 {
17232 if (child_die->tag == DW_TAG_subrange_type)
17233 {
17234 struct type *child_type = read_type_die (child_die, cu);
17235
17236 if (child_type != NULL)
17237 {
17238 /* The range type was succesfully read. Save it for the
17239 array type creation. */
17240 range_types.push_back (child_type);
17241 }
17242 }
17243 child_die = child_die->sibling;
17244 }
17245
17246 if (range_types.empty ())
17247 {
17248 complaint (_("unable to find array range - DIE at %s [in module %s]"),
17249 sect_offset_str (die->sect_off),
17250 objfile_name (cu->per_objfile->objfile));
17251 return NULL;
17252 }
17253
17254 /* Dwarf2 dimensions are output from left to right, create the
17255 necessary array types in backwards order. */
17256
17257 type = element_type;
17258
17259 if (read_array_order (die, cu) == DW_ORD_col_major)
17260 {
17261 int i = 0;
17262
17263 while (i < range_types.size ())
17264 {
17265 type = create_array_type_with_stride (NULL, type, range_types[i++],
17266 byte_stride_prop, bit_stride);
17267 bit_stride = 0;
17268 byte_stride_prop = nullptr;
17269 }
17270 }
17271 else
17272 {
17273 size_t ndim = range_types.size ();
17274 while (ndim-- > 0)
17275 {
17276 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17277 byte_stride_prop, bit_stride);
17278 bit_stride = 0;
17279 byte_stride_prop = nullptr;
17280 }
17281 }
17282
17283 gdb_assert (type != element_type);
17284
17285 /* Understand Dwarf2 support for vector types (like they occur on
17286 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17287 array type. This is not part of the Dwarf2/3 standard yet, but a
17288 custom vendor extension. The main difference between a regular
17289 array and the vector variant is that vectors are passed by value
17290 to functions. */
17291 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17292 if (attr != nullptr)
17293 make_vector_type (type);
17294
17295 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17296 implementation may choose to implement triple vectors using this
17297 attribute. */
17298 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17299 if (attr != nullptr && attr->form_is_unsigned ())
17300 {
17301 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17302 TYPE_LENGTH (type) = attr->as_unsigned ();
17303 else
17304 complaint (_("DW_AT_byte_size for array type smaller "
17305 "than the total size of elements"));
17306 }
17307
17308 name = dwarf2_name (die, cu);
17309 if (name)
17310 type->set_name (name);
17311
17312 maybe_set_alignment (cu, die, type);
17313
17314 struct type *replacement_type = nullptr;
17315 if (cu->language == language_ada)
17316 {
17317 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17318 if (replacement_type != nullptr)
17319 type = replacement_type;
17320 }
17321
17322 /* Install the type in the die. */
17323 set_die_type (die, type, cu, replacement_type != nullptr);
17324
17325 /* set_die_type should be already done. */
17326 set_descriptive_type (type, die, cu);
17327
17328 return type;
17329 }
17330
17331 static enum dwarf_array_dim_ordering
17332 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17333 {
17334 struct attribute *attr;
17335
17336 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17337
17338 if (attr != nullptr)
17339 {
17340 LONGEST val = attr->constant_value (-1);
17341 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17342 return (enum dwarf_array_dim_ordering) val;
17343 }
17344
17345 /* GNU F77 is a special case, as at 08/2004 array type info is the
17346 opposite order to the dwarf2 specification, but data is still
17347 laid out as per normal fortran.
17348
17349 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17350 version checking. */
17351
17352 if (cu->language == language_fortran
17353 && cu->producer && strstr (cu->producer, "GNU F77"))
17354 {
17355 return DW_ORD_row_major;
17356 }
17357
17358 switch (cu->language_defn->array_ordering ())
17359 {
17360 case array_column_major:
17361 return DW_ORD_col_major;
17362 case array_row_major:
17363 default:
17364 return DW_ORD_row_major;
17365 };
17366 }
17367
17368 /* Extract all information from a DW_TAG_set_type DIE and put it in
17369 the DIE's type field. */
17370
17371 static struct type *
17372 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17373 {
17374 struct type *domain_type, *set_type;
17375 struct attribute *attr;
17376
17377 domain_type = die_type (die, cu);
17378
17379 /* The die_type call above may have already set the type for this DIE. */
17380 set_type = get_die_type (die, cu);
17381 if (set_type)
17382 return set_type;
17383
17384 set_type = create_set_type (NULL, domain_type);
17385
17386 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17387 if (attr != nullptr && attr->form_is_unsigned ())
17388 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17389
17390 maybe_set_alignment (cu, die, set_type);
17391
17392 return set_die_type (die, set_type, cu);
17393 }
17394
17395 /* A helper for read_common_block that creates a locexpr baton.
17396 SYM is the symbol which we are marking as computed.
17397 COMMON_DIE is the DIE for the common block.
17398 COMMON_LOC is the location expression attribute for the common
17399 block itself.
17400 MEMBER_LOC is the location expression attribute for the particular
17401 member of the common block that we are processing.
17402 CU is the CU from which the above come. */
17403
17404 static void
17405 mark_common_block_symbol_computed (struct symbol *sym,
17406 struct die_info *common_die,
17407 struct attribute *common_loc,
17408 struct attribute *member_loc,
17409 struct dwarf2_cu *cu)
17410 {
17411 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17412 struct objfile *objfile = per_objfile->objfile;
17413 struct dwarf2_locexpr_baton *baton;
17414 gdb_byte *ptr;
17415 unsigned int cu_off;
17416 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17417 LONGEST offset = 0;
17418
17419 gdb_assert (common_loc && member_loc);
17420 gdb_assert (common_loc->form_is_block ());
17421 gdb_assert (member_loc->form_is_block ()
17422 || member_loc->form_is_constant ());
17423
17424 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17425 baton->per_objfile = per_objfile;
17426 baton->per_cu = cu->per_cu;
17427 gdb_assert (baton->per_cu);
17428
17429 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17430
17431 if (member_loc->form_is_constant ())
17432 {
17433 offset = member_loc->constant_value (0);
17434 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17435 }
17436 else
17437 baton->size += member_loc->as_block ()->size;
17438
17439 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17440 baton->data = ptr;
17441
17442 *ptr++ = DW_OP_call4;
17443 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17444 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17445 ptr += 4;
17446
17447 if (member_loc->form_is_constant ())
17448 {
17449 *ptr++ = DW_OP_addr;
17450 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17451 ptr += cu->header.addr_size;
17452 }
17453 else
17454 {
17455 /* We have to copy the data here, because DW_OP_call4 will only
17456 use a DW_AT_location attribute. */
17457 struct dwarf_block *block = member_loc->as_block ();
17458 memcpy (ptr, block->data, block->size);
17459 ptr += block->size;
17460 }
17461
17462 *ptr++ = DW_OP_plus;
17463 gdb_assert (ptr - baton->data == baton->size);
17464
17465 SYMBOL_LOCATION_BATON (sym) = baton;
17466 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17467 }
17468
17469 /* Create appropriate locally-scoped variables for all the
17470 DW_TAG_common_block entries. Also create a struct common_block
17471 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17472 is used to separate the common blocks name namespace from regular
17473 variable names. */
17474
17475 static void
17476 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17477 {
17478 struct attribute *attr;
17479
17480 attr = dwarf2_attr (die, DW_AT_location, cu);
17481 if (attr != nullptr)
17482 {
17483 /* Support the .debug_loc offsets. */
17484 if (attr->form_is_block ())
17485 {
17486 /* Ok. */
17487 }
17488 else if (attr->form_is_section_offset ())
17489 {
17490 dwarf2_complex_location_expr_complaint ();
17491 attr = NULL;
17492 }
17493 else
17494 {
17495 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17496 "common block member");
17497 attr = NULL;
17498 }
17499 }
17500
17501 if (die->child != NULL)
17502 {
17503 struct objfile *objfile = cu->per_objfile->objfile;
17504 struct die_info *child_die;
17505 size_t n_entries = 0, size;
17506 struct common_block *common_block;
17507 struct symbol *sym;
17508
17509 for (child_die = die->child;
17510 child_die && child_die->tag;
17511 child_die = child_die->sibling)
17512 ++n_entries;
17513
17514 size = (sizeof (struct common_block)
17515 + (n_entries - 1) * sizeof (struct symbol *));
17516 common_block
17517 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17518 size);
17519 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17520 common_block->n_entries = 0;
17521
17522 for (child_die = die->child;
17523 child_die && child_die->tag;
17524 child_die = child_die->sibling)
17525 {
17526 /* Create the symbol in the DW_TAG_common_block block in the current
17527 symbol scope. */
17528 sym = new_symbol (child_die, NULL, cu);
17529 if (sym != NULL)
17530 {
17531 struct attribute *member_loc;
17532
17533 common_block->contents[common_block->n_entries++] = sym;
17534
17535 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17536 cu);
17537 if (member_loc)
17538 {
17539 /* GDB has handled this for a long time, but it is
17540 not specified by DWARF. It seems to have been
17541 emitted by gfortran at least as recently as:
17542 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17543 complaint (_("Variable in common block has "
17544 "DW_AT_data_member_location "
17545 "- DIE at %s [in module %s]"),
17546 sect_offset_str (child_die->sect_off),
17547 objfile_name (objfile));
17548
17549 if (member_loc->form_is_section_offset ())
17550 dwarf2_complex_location_expr_complaint ();
17551 else if (member_loc->form_is_constant ()
17552 || member_loc->form_is_block ())
17553 {
17554 if (attr != nullptr)
17555 mark_common_block_symbol_computed (sym, die, attr,
17556 member_loc, cu);
17557 }
17558 else
17559 dwarf2_complex_location_expr_complaint ();
17560 }
17561 }
17562 }
17563
17564 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17565 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17566 }
17567 }
17568
17569 /* Create a type for a C++ namespace. */
17570
17571 static struct type *
17572 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17573 {
17574 struct objfile *objfile = cu->per_objfile->objfile;
17575 const char *previous_prefix, *name;
17576 int is_anonymous;
17577 struct type *type;
17578
17579 /* For extensions, reuse the type of the original namespace. */
17580 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17581 {
17582 struct die_info *ext_die;
17583 struct dwarf2_cu *ext_cu = cu;
17584
17585 ext_die = dwarf2_extension (die, &ext_cu);
17586 type = read_type_die (ext_die, ext_cu);
17587
17588 /* EXT_CU may not be the same as CU.
17589 Ensure TYPE is recorded with CU in die_type_hash. */
17590 return set_die_type (die, type, cu);
17591 }
17592
17593 name = namespace_name (die, &is_anonymous, cu);
17594
17595 /* Now build the name of the current namespace. */
17596
17597 previous_prefix = determine_prefix (die, cu);
17598 if (previous_prefix[0] != '\0')
17599 name = typename_concat (&objfile->objfile_obstack,
17600 previous_prefix, name, 0, cu);
17601
17602 /* Create the type. */
17603 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17604
17605 return set_die_type (die, type, cu);
17606 }
17607
17608 /* Read a namespace scope. */
17609
17610 static void
17611 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17612 {
17613 struct objfile *objfile = cu->per_objfile->objfile;
17614 int is_anonymous;
17615
17616 /* Add a symbol associated to this if we haven't seen the namespace
17617 before. Also, add a using directive if it's an anonymous
17618 namespace. */
17619
17620 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17621 {
17622 struct type *type;
17623
17624 type = read_type_die (die, cu);
17625 new_symbol (die, type, cu);
17626
17627 namespace_name (die, &is_anonymous, cu);
17628 if (is_anonymous)
17629 {
17630 const char *previous_prefix = determine_prefix (die, cu);
17631
17632 std::vector<const char *> excludes;
17633 add_using_directive (using_directives (cu),
17634 previous_prefix, type->name (), NULL,
17635 NULL, excludes, 0, &objfile->objfile_obstack);
17636 }
17637 }
17638
17639 if (die->child != NULL)
17640 {
17641 struct die_info *child_die = die->child;
17642
17643 while (child_die && child_die->tag)
17644 {
17645 process_die (child_die, cu);
17646 child_die = child_die->sibling;
17647 }
17648 }
17649 }
17650
17651 /* Read a Fortran module as type. This DIE can be only a declaration used for
17652 imported module. Still we need that type as local Fortran "use ... only"
17653 declaration imports depend on the created type in determine_prefix. */
17654
17655 static struct type *
17656 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17657 {
17658 struct objfile *objfile = cu->per_objfile->objfile;
17659 const char *module_name;
17660 struct type *type;
17661
17662 module_name = dwarf2_name (die, cu);
17663 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17664
17665 return set_die_type (die, type, cu);
17666 }
17667
17668 /* Read a Fortran module. */
17669
17670 static void
17671 read_module (struct die_info *die, struct dwarf2_cu *cu)
17672 {
17673 struct die_info *child_die = die->child;
17674 struct type *type;
17675
17676 type = read_type_die (die, cu);
17677 new_symbol (die, type, cu);
17678
17679 while (child_die && child_die->tag)
17680 {
17681 process_die (child_die, cu);
17682 child_die = child_die->sibling;
17683 }
17684 }
17685
17686 /* Return the name of the namespace represented by DIE. Set
17687 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17688 namespace. */
17689
17690 static const char *
17691 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17692 {
17693 struct die_info *current_die;
17694 const char *name = NULL;
17695
17696 /* Loop through the extensions until we find a name. */
17697
17698 for (current_die = die;
17699 current_die != NULL;
17700 current_die = dwarf2_extension (die, &cu))
17701 {
17702 /* We don't use dwarf2_name here so that we can detect the absence
17703 of a name -> anonymous namespace. */
17704 name = dwarf2_string_attr (die, DW_AT_name, cu);
17705
17706 if (name != NULL)
17707 break;
17708 }
17709
17710 /* Is it an anonymous namespace? */
17711
17712 *is_anonymous = (name == NULL);
17713 if (*is_anonymous)
17714 name = CP_ANONYMOUS_NAMESPACE_STR;
17715
17716 return name;
17717 }
17718
17719 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17720 the user defined type vector. */
17721
17722 static struct type *
17723 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17724 {
17725 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17726 struct comp_unit_head *cu_header = &cu->header;
17727 struct type *type;
17728 struct attribute *attr_byte_size;
17729 struct attribute *attr_address_class;
17730 int byte_size, addr_class;
17731 struct type *target_type;
17732
17733 target_type = die_type (die, cu);
17734
17735 /* The die_type call above may have already set the type for this DIE. */
17736 type = get_die_type (die, cu);
17737 if (type)
17738 return type;
17739
17740 type = lookup_pointer_type (target_type);
17741
17742 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17743 if (attr_byte_size)
17744 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17745 else
17746 byte_size = cu_header->addr_size;
17747
17748 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17749 if (attr_address_class)
17750 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17751 else
17752 addr_class = DW_ADDR_none;
17753
17754 ULONGEST alignment = get_alignment (cu, die);
17755
17756 /* If the pointer size, alignment, or address class is different
17757 than the default, create a type variant marked as such and set
17758 the length accordingly. */
17759 if (TYPE_LENGTH (type) != byte_size
17760 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17761 && alignment != TYPE_RAW_ALIGN (type))
17762 || addr_class != DW_ADDR_none)
17763 {
17764 if (gdbarch_address_class_type_flags_p (gdbarch))
17765 {
17766 type_instance_flags type_flags
17767 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17768 addr_class);
17769 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17770 == 0);
17771 type = make_type_with_address_space (type, type_flags);
17772 }
17773 else if (TYPE_LENGTH (type) != byte_size)
17774 {
17775 complaint (_("invalid pointer size %d"), byte_size);
17776 }
17777 else if (TYPE_RAW_ALIGN (type) != alignment)
17778 {
17779 complaint (_("Invalid DW_AT_alignment"
17780 " - DIE at %s [in module %s]"),
17781 sect_offset_str (die->sect_off),
17782 objfile_name (cu->per_objfile->objfile));
17783 }
17784 else
17785 {
17786 /* Should we also complain about unhandled address classes? */
17787 }
17788 }
17789
17790 TYPE_LENGTH (type) = byte_size;
17791 set_type_align (type, alignment);
17792 return set_die_type (die, type, cu);
17793 }
17794
17795 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17796 the user defined type vector. */
17797
17798 static struct type *
17799 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17800 {
17801 struct type *type;
17802 struct type *to_type;
17803 struct type *domain;
17804
17805 to_type = die_type (die, cu);
17806 domain = die_containing_type (die, cu);
17807
17808 /* The calls above may have already set the type for this DIE. */
17809 type = get_die_type (die, cu);
17810 if (type)
17811 return type;
17812
17813 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17814 type = lookup_methodptr_type (to_type);
17815 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17816 {
17817 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17818
17819 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17820 to_type->fields (), to_type->num_fields (),
17821 to_type->has_varargs ());
17822 type = lookup_methodptr_type (new_type);
17823 }
17824 else
17825 type = lookup_memberptr_type (to_type, domain);
17826
17827 return set_die_type (die, type, cu);
17828 }
17829
17830 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17831 the user defined type vector. */
17832
17833 static struct type *
17834 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17835 enum type_code refcode)
17836 {
17837 struct comp_unit_head *cu_header = &cu->header;
17838 struct type *type, *target_type;
17839 struct attribute *attr;
17840
17841 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17842
17843 target_type = die_type (die, cu);
17844
17845 /* The die_type call above may have already set the type for this DIE. */
17846 type = get_die_type (die, cu);
17847 if (type)
17848 return type;
17849
17850 type = lookup_reference_type (target_type, refcode);
17851 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17852 if (attr != nullptr)
17853 {
17854 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17855 }
17856 else
17857 {
17858 TYPE_LENGTH (type) = cu_header->addr_size;
17859 }
17860 maybe_set_alignment (cu, die, type);
17861 return set_die_type (die, type, cu);
17862 }
17863
17864 /* Add the given cv-qualifiers to the element type of the array. GCC
17865 outputs DWARF type qualifiers that apply to an array, not the
17866 element type. But GDB relies on the array element type to carry
17867 the cv-qualifiers. This mimics section 6.7.3 of the C99
17868 specification. */
17869
17870 static struct type *
17871 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17872 struct type *base_type, int cnst, int voltl)
17873 {
17874 struct type *el_type, *inner_array;
17875
17876 base_type = copy_type (base_type);
17877 inner_array = base_type;
17878
17879 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17880 {
17881 TYPE_TARGET_TYPE (inner_array) =
17882 copy_type (TYPE_TARGET_TYPE (inner_array));
17883 inner_array = TYPE_TARGET_TYPE (inner_array);
17884 }
17885
17886 el_type = TYPE_TARGET_TYPE (inner_array);
17887 cnst |= TYPE_CONST (el_type);
17888 voltl |= TYPE_VOLATILE (el_type);
17889 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17890
17891 return set_die_type (die, base_type, cu);
17892 }
17893
17894 static struct type *
17895 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17896 {
17897 struct type *base_type, *cv_type;
17898
17899 base_type = die_type (die, cu);
17900
17901 /* The die_type call above may have already set the type for this DIE. */
17902 cv_type = get_die_type (die, cu);
17903 if (cv_type)
17904 return cv_type;
17905
17906 /* In case the const qualifier is applied to an array type, the element type
17907 is so qualified, not the array type (section 6.7.3 of C99). */
17908 if (base_type->code () == TYPE_CODE_ARRAY)
17909 return add_array_cv_type (die, cu, base_type, 1, 0);
17910
17911 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17912 return set_die_type (die, cv_type, cu);
17913 }
17914
17915 static struct type *
17916 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17917 {
17918 struct type *base_type, *cv_type;
17919
17920 base_type = die_type (die, cu);
17921
17922 /* The die_type call above may have already set the type for this DIE. */
17923 cv_type = get_die_type (die, cu);
17924 if (cv_type)
17925 return cv_type;
17926
17927 /* In case the volatile qualifier is applied to an array type, the
17928 element type is so qualified, not the array type (section 6.7.3
17929 of C99). */
17930 if (base_type->code () == TYPE_CODE_ARRAY)
17931 return add_array_cv_type (die, cu, base_type, 0, 1);
17932
17933 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17934 return set_die_type (die, cv_type, cu);
17935 }
17936
17937 /* Handle DW_TAG_restrict_type. */
17938
17939 static struct type *
17940 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17941 {
17942 struct type *base_type, *cv_type;
17943
17944 base_type = die_type (die, cu);
17945
17946 /* The die_type call above may have already set the type for this DIE. */
17947 cv_type = get_die_type (die, cu);
17948 if (cv_type)
17949 return cv_type;
17950
17951 cv_type = make_restrict_type (base_type);
17952 return set_die_type (die, cv_type, cu);
17953 }
17954
17955 /* Handle DW_TAG_atomic_type. */
17956
17957 static struct type *
17958 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17959 {
17960 struct type *base_type, *cv_type;
17961
17962 base_type = die_type (die, cu);
17963
17964 /* The die_type call above may have already set the type for this DIE. */
17965 cv_type = get_die_type (die, cu);
17966 if (cv_type)
17967 return cv_type;
17968
17969 cv_type = make_atomic_type (base_type);
17970 return set_die_type (die, cv_type, cu);
17971 }
17972
17973 /* Extract all information from a DW_TAG_string_type DIE and add to
17974 the user defined type vector. It isn't really a user defined type,
17975 but it behaves like one, with other DIE's using an AT_user_def_type
17976 attribute to reference it. */
17977
17978 static struct type *
17979 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17980 {
17981 struct objfile *objfile = cu->per_objfile->objfile;
17982 struct gdbarch *gdbarch = objfile->arch ();
17983 struct type *type, *range_type, *index_type, *char_type;
17984 struct attribute *attr;
17985 struct dynamic_prop prop;
17986 bool length_is_constant = true;
17987 LONGEST length;
17988
17989 /* There are a couple of places where bit sizes might be made use of
17990 when parsing a DW_TAG_string_type, however, no producer that we know
17991 of make use of these. Handling bit sizes that are a multiple of the
17992 byte size is easy enough, but what about other bit sizes? Lets deal
17993 with that problem when we have to. Warn about these attributes being
17994 unsupported, then parse the type and ignore them like we always
17995 have. */
17996 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17997 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17998 {
17999 static bool warning_printed = false;
18000 if (!warning_printed)
18001 {
18002 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
18003 "currently supported on DW_TAG_string_type."));
18004 warning_printed = true;
18005 }
18006 }
18007
18008 attr = dwarf2_attr (die, DW_AT_string_length, cu);
18009 if (attr != nullptr && !attr->form_is_constant ())
18010 {
18011 /* The string length describes the location at which the length of
18012 the string can be found. The size of the length field can be
18013 specified with one of the attributes below. */
18014 struct type *prop_type;
18015 struct attribute *len
18016 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
18017 if (len == nullptr)
18018 len = dwarf2_attr (die, DW_AT_byte_size, cu);
18019 if (len != nullptr && len->form_is_constant ())
18020 {
18021 /* Pass 0 as the default as we know this attribute is constant
18022 and the default value will not be returned. */
18023 LONGEST sz = len->constant_value (0);
18024 prop_type = cu->per_objfile->int_type (sz, true);
18025 }
18026 else
18027 {
18028 /* If the size is not specified then we assume it is the size of
18029 an address on this target. */
18030 prop_type = cu->addr_sized_int_type (true);
18031 }
18032
18033 /* Convert the attribute into a dynamic property. */
18034 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
18035 length = 1;
18036 else
18037 length_is_constant = false;
18038 }
18039 else if (attr != nullptr)
18040 {
18041 /* This DW_AT_string_length just contains the length with no
18042 indirection. There's no need to create a dynamic property in this
18043 case. Pass 0 for the default value as we know it will not be
18044 returned in this case. */
18045 length = attr->constant_value (0);
18046 }
18047 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
18048 {
18049 /* We don't currently support non-constant byte sizes for strings. */
18050 length = attr->constant_value (1);
18051 }
18052 else
18053 {
18054 /* Use 1 as a fallback length if we have nothing else. */
18055 length = 1;
18056 }
18057
18058 index_type = objfile_type (objfile)->builtin_int;
18059 if (length_is_constant)
18060 range_type = create_static_range_type (NULL, index_type, 1, length);
18061 else
18062 {
18063 struct dynamic_prop low_bound;
18064
18065 low_bound.set_const_val (1);
18066 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18067 }
18068 char_type = language_string_char_type (cu->language_defn, gdbarch);
18069 type = create_string_type (NULL, char_type, range_type);
18070
18071 return set_die_type (die, type, cu);
18072 }
18073
18074 /* Assuming that DIE corresponds to a function, returns nonzero
18075 if the function is prototyped. */
18076
18077 static int
18078 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18079 {
18080 struct attribute *attr;
18081
18082 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18083 if (attr && attr->as_boolean ())
18084 return 1;
18085
18086 /* The DWARF standard implies that the DW_AT_prototyped attribute
18087 is only meaningful for C, but the concept also extends to other
18088 languages that allow unprototyped functions (Eg: Objective C).
18089 For all other languages, assume that functions are always
18090 prototyped. */
18091 if (cu->language != language_c
18092 && cu->language != language_objc
18093 && cu->language != language_opencl)
18094 return 1;
18095
18096 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18097 prototyped and unprototyped functions; default to prototyped,
18098 since that is more common in modern code (and RealView warns
18099 about unprototyped functions). */
18100 if (producer_is_realview (cu->producer))
18101 return 1;
18102
18103 return 0;
18104 }
18105
18106 /* Handle DIES due to C code like:
18107
18108 struct foo
18109 {
18110 int (*funcp)(int a, long l);
18111 int b;
18112 };
18113
18114 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18115
18116 static struct type *
18117 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18118 {
18119 struct objfile *objfile = cu->per_objfile->objfile;
18120 struct type *type; /* Type that this function returns. */
18121 struct type *ftype; /* Function that returns above type. */
18122 struct attribute *attr;
18123
18124 type = die_type (die, cu);
18125
18126 /* The die_type call above may have already set the type for this DIE. */
18127 ftype = get_die_type (die, cu);
18128 if (ftype)
18129 return ftype;
18130
18131 ftype = lookup_function_type (type);
18132
18133 if (prototyped_function_p (die, cu))
18134 ftype->set_is_prototyped (true);
18135
18136 /* Store the calling convention in the type if it's available in
18137 the subroutine die. Otherwise set the calling convention to
18138 the default value DW_CC_normal. */
18139 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18140 if (attr != nullptr
18141 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18142 TYPE_CALLING_CONVENTION (ftype)
18143 = (enum dwarf_calling_convention) attr->constant_value (0);
18144 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18145 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18146 else
18147 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18148
18149 /* Record whether the function returns normally to its caller or not
18150 if the DWARF producer set that information. */
18151 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18152 if (attr && attr->as_boolean ())
18153 TYPE_NO_RETURN (ftype) = 1;
18154
18155 /* We need to add the subroutine type to the die immediately so
18156 we don't infinitely recurse when dealing with parameters
18157 declared as the same subroutine type. */
18158 set_die_type (die, ftype, cu);
18159
18160 if (die->child != NULL)
18161 {
18162 struct type *void_type = objfile_type (objfile)->builtin_void;
18163 struct die_info *child_die;
18164 int nparams, iparams;
18165
18166 /* Count the number of parameters.
18167 FIXME: GDB currently ignores vararg functions, but knows about
18168 vararg member functions. */
18169 nparams = 0;
18170 child_die = die->child;
18171 while (child_die && child_die->tag)
18172 {
18173 if (child_die->tag == DW_TAG_formal_parameter)
18174 nparams++;
18175 else if (child_die->tag == DW_TAG_unspecified_parameters)
18176 ftype->set_has_varargs (true);
18177
18178 child_die = child_die->sibling;
18179 }
18180
18181 /* Allocate storage for parameters and fill them in. */
18182 ftype->set_num_fields (nparams);
18183 ftype->set_fields
18184 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18185
18186 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18187 even if we error out during the parameters reading below. */
18188 for (iparams = 0; iparams < nparams; iparams++)
18189 ftype->field (iparams).set_type (void_type);
18190
18191 iparams = 0;
18192 child_die = die->child;
18193 while (child_die && child_die->tag)
18194 {
18195 if (child_die->tag == DW_TAG_formal_parameter)
18196 {
18197 struct type *arg_type;
18198
18199 /* DWARF version 2 has no clean way to discern C++
18200 static and non-static member functions. G++ helps
18201 GDB by marking the first parameter for non-static
18202 member functions (which is the this pointer) as
18203 artificial. We pass this information to
18204 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18205
18206 DWARF version 3 added DW_AT_object_pointer, which GCC
18207 4.5 does not yet generate. */
18208 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18209 if (attr != nullptr)
18210 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18211 else
18212 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18213 arg_type = die_type (child_die, cu);
18214
18215 /* RealView does not mark THIS as const, which the testsuite
18216 expects. GCC marks THIS as const in method definitions,
18217 but not in the class specifications (GCC PR 43053). */
18218 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18219 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18220 {
18221 int is_this = 0;
18222 struct dwarf2_cu *arg_cu = cu;
18223 const char *name = dwarf2_name (child_die, cu);
18224
18225 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18226 if (attr != nullptr)
18227 {
18228 /* If the compiler emits this, use it. */
18229 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18230 is_this = 1;
18231 }
18232 else if (name && strcmp (name, "this") == 0)
18233 /* Function definitions will have the argument names. */
18234 is_this = 1;
18235 else if (name == NULL && iparams == 0)
18236 /* Declarations may not have the names, so like
18237 elsewhere in GDB, assume an artificial first
18238 argument is "this". */
18239 is_this = 1;
18240
18241 if (is_this)
18242 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18243 arg_type, 0);
18244 }
18245
18246 ftype->field (iparams).set_type (arg_type);
18247 iparams++;
18248 }
18249 child_die = child_die->sibling;
18250 }
18251 }
18252
18253 return ftype;
18254 }
18255
18256 static struct type *
18257 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18258 {
18259 struct objfile *objfile = cu->per_objfile->objfile;
18260 const char *name = NULL;
18261 struct type *this_type, *target_type;
18262
18263 name = dwarf2_full_name (NULL, die, cu);
18264 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18265 this_type->set_target_is_stub (true);
18266 set_die_type (die, this_type, cu);
18267 target_type = die_type (die, cu);
18268 if (target_type != this_type)
18269 TYPE_TARGET_TYPE (this_type) = target_type;
18270 else
18271 {
18272 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18273 spec and cause infinite loops in GDB. */
18274 complaint (_("Self-referential DW_TAG_typedef "
18275 "- DIE at %s [in module %s]"),
18276 sect_offset_str (die->sect_off), objfile_name (objfile));
18277 TYPE_TARGET_TYPE (this_type) = NULL;
18278 }
18279 if (name == NULL)
18280 {
18281 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18282 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18283 Handle these by just returning the target type, rather than
18284 constructing an anonymous typedef type and trying to handle this
18285 elsewhere. */
18286 set_die_type (die, target_type, cu);
18287 return target_type;
18288 }
18289 return this_type;
18290 }
18291
18292 /* Helper for get_dwarf2_rational_constant that computes the value of
18293 a given gmp_mpz given an attribute. */
18294
18295 static void
18296 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18297 {
18298 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18299 location expression that pushes an implicit value. */
18300 if (attr->form == DW_FORM_exprloc)
18301 {
18302 dwarf_block *blk = attr->as_block ();
18303 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18304 {
18305 uint64_t len;
18306 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18307 blk->data + blk->size,
18308 &len);
18309 if (ptr - blk->data + len <= blk->size)
18310 {
18311 mpz_import (value->val, len,
18312 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18313 1, 0, 0, ptr);
18314 return;
18315 }
18316 }
18317
18318 /* On failure set it to 1. */
18319 *value = gdb_mpz (1);
18320 }
18321 else if (attr->form_is_block ())
18322 {
18323 dwarf_block *blk = attr->as_block ();
18324 mpz_import (value->val, blk->size,
18325 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18326 1, 0, 0, blk->data);
18327 }
18328 else
18329 *value = gdb_mpz (attr->constant_value (1));
18330 }
18331
18332 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18333 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18334
18335 If the numerator and/or numerator attribute is missing,
18336 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18337 untouched. */
18338
18339 static void
18340 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18341 gdb_mpz *numerator, gdb_mpz *denominator)
18342 {
18343 struct attribute *num_attr, *denom_attr;
18344
18345 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18346 if (num_attr == nullptr)
18347 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18348 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18349
18350 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18351 if (denom_attr == nullptr)
18352 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18353 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18354
18355 if (num_attr == nullptr || denom_attr == nullptr)
18356 return;
18357
18358 get_mpz (cu, numerator, num_attr);
18359 get_mpz (cu, denominator, denom_attr);
18360 }
18361
18362 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18363 rational constant, rather than a signed one.
18364
18365 If the rational constant has a negative value, a complaint
18366 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18367
18368 static void
18369 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18370 struct dwarf2_cu *cu,
18371 gdb_mpz *numerator,
18372 gdb_mpz *denominator)
18373 {
18374 gdb_mpz num (1);
18375 gdb_mpz denom (1);
18376
18377 get_dwarf2_rational_constant (die, cu, &num, &denom);
18378 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18379 {
18380 mpz_neg (num.val, num.val);
18381 mpz_neg (denom.val, denom.val);
18382 }
18383 else if (mpz_sgn (num.val) == -1)
18384 {
18385 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18386 " in DIE at %s"),
18387 sect_offset_str (die->sect_off));
18388 return;
18389 }
18390 else if (mpz_sgn (denom.val) == -1)
18391 {
18392 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18393 " in DIE at %s"),
18394 sect_offset_str (die->sect_off));
18395 return;
18396 }
18397
18398 *numerator = std::move (num);
18399 *denominator = std::move (denom);
18400 }
18401
18402 /* Assuming DIE corresponds to a fixed point type, finish the creation
18403 of the corresponding TYPE by setting its type-specific data.
18404 CU is the DIE's CU. */
18405
18406 static void
18407 finish_fixed_point_type (struct type *type, struct die_info *die,
18408 struct dwarf2_cu *cu)
18409 {
18410 struct attribute *attr;
18411
18412 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18413 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18414
18415 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18416 if (!attr)
18417 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18418 if (!attr)
18419 attr = dwarf2_attr (die, DW_AT_small, cu);
18420
18421 /* Numerator and denominator of our fixed-point type's scaling factor.
18422 The default is a scaling factor of 1, which we use as a fallback
18423 when we are not able to decode it (problem with the debugging info,
18424 unsupported forms, bug in GDB, etc...). Using that as the default
18425 allows us to at least print the unscaled value, which might still
18426 be useful to a user. */
18427 gdb_mpz scale_num (1);
18428 gdb_mpz scale_denom (1);
18429
18430 if (attr == nullptr)
18431 {
18432 /* Scaling factor not found. Assume a scaling factor of 1,
18433 and hope for the best. At least the user will be able to see
18434 the encoded value. */
18435 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18436 sect_offset_str (die->sect_off));
18437 }
18438 else if (attr->name == DW_AT_binary_scale)
18439 {
18440 LONGEST scale_exp = attr->constant_value (0);
18441 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18442
18443 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18444 }
18445 else if (attr->name == DW_AT_decimal_scale)
18446 {
18447 LONGEST scale_exp = attr->constant_value (0);
18448 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18449
18450 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18451 }
18452 else if (attr->name == DW_AT_small)
18453 {
18454 struct die_info *scale_die;
18455 struct dwarf2_cu *scale_cu = cu;
18456
18457 scale_die = follow_die_ref (die, attr, &scale_cu);
18458 if (scale_die->tag == DW_TAG_constant)
18459 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18460 &scale_num, &scale_denom);
18461 else
18462 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18463 " (DIE at %s)"),
18464 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18465 }
18466 else
18467 {
18468 complaint (_("unsupported scale attribute %s for fixed-point type"
18469 " (DIE at %s)"),
18470 dwarf_attr_name (attr->name),
18471 sect_offset_str (die->sect_off));
18472 }
18473
18474 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18475 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18476 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18477 mpq_canonicalize (scaling_factor.val);
18478 }
18479
18480 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18481 (which may be different from NAME) to the architecture back-end to allow
18482 it to guess the correct format if necessary. */
18483
18484 static struct type *
18485 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18486 const char *name_hint, enum bfd_endian byte_order)
18487 {
18488 struct gdbarch *gdbarch = objfile->arch ();
18489 const struct floatformat **format;
18490 struct type *type;
18491
18492 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18493 if (format)
18494 type = init_float_type (objfile, bits, name, format, byte_order);
18495 else
18496 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18497
18498 return type;
18499 }
18500
18501 /* Allocate an integer type of size BITS and name NAME. */
18502
18503 static struct type *
18504 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18505 int bits, int unsigned_p, const char *name)
18506 {
18507 struct type *type;
18508
18509 /* Versions of Intel's C Compiler generate an integer type called "void"
18510 instead of using DW_TAG_unspecified_type. This has been seen on
18511 at least versions 14, 17, and 18. */
18512 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18513 && strcmp (name, "void") == 0)
18514 type = objfile_type (objfile)->builtin_void;
18515 else
18516 type = init_integer_type (objfile, bits, unsigned_p, name);
18517
18518 return type;
18519 }
18520
18521 /* Return true if DIE has a DW_AT_small attribute whose value is
18522 a constant rational, where both the numerator and denominator
18523 are equal to zero.
18524
18525 CU is the DIE's Compilation Unit. */
18526
18527 static bool
18528 has_zero_over_zero_small_attribute (struct die_info *die,
18529 struct dwarf2_cu *cu)
18530 {
18531 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18532 if (attr == nullptr)
18533 return false;
18534
18535 struct dwarf2_cu *scale_cu = cu;
18536 struct die_info *scale_die
18537 = follow_die_ref (die, attr, &scale_cu);
18538
18539 if (scale_die->tag != DW_TAG_constant)
18540 return false;
18541
18542 gdb_mpz num (1), denom (1);
18543 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18544 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18545 }
18546
18547 /* Initialise and return a floating point type of size BITS suitable for
18548 use as a component of a complex number. The NAME_HINT is passed through
18549 when initialising the floating point type and is the name of the complex
18550 type.
18551
18552 As DWARF doesn't currently provide an explicit name for the components
18553 of a complex number, but it can be helpful to have these components
18554 named, we try to select a suitable name based on the size of the
18555 component. */
18556 static struct type *
18557 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18558 struct objfile *objfile,
18559 int bits, const char *name_hint,
18560 enum bfd_endian byte_order)
18561 {
18562 gdbarch *gdbarch = objfile->arch ();
18563 struct type *tt = nullptr;
18564
18565 /* Try to find a suitable floating point builtin type of size BITS.
18566 We're going to use the name of this type as the name for the complex
18567 target type that we are about to create. */
18568 switch (cu->language)
18569 {
18570 case language_fortran:
18571 switch (bits)
18572 {
18573 case 32:
18574 tt = builtin_f_type (gdbarch)->builtin_real;
18575 break;
18576 case 64:
18577 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18578 break;
18579 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18580 case 128:
18581 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18582 break;
18583 }
18584 break;
18585 default:
18586 switch (bits)
18587 {
18588 case 32:
18589 tt = builtin_type (gdbarch)->builtin_float;
18590 break;
18591 case 64:
18592 tt = builtin_type (gdbarch)->builtin_double;
18593 break;
18594 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18595 case 128:
18596 tt = builtin_type (gdbarch)->builtin_long_double;
18597 break;
18598 }
18599 break;
18600 }
18601
18602 /* If the type we found doesn't match the size we were looking for, then
18603 pretend we didn't find a type at all, the complex target type we
18604 create will then be nameless. */
18605 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18606 tt = nullptr;
18607
18608 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18609 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18610 }
18611
18612 /* Find a representation of a given base type and install
18613 it in the TYPE field of the die. */
18614
18615 static struct type *
18616 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18617 {
18618 struct objfile *objfile = cu->per_objfile->objfile;
18619 struct type *type;
18620 struct attribute *attr;
18621 int encoding = 0, bits = 0;
18622 const char *name;
18623 gdbarch *arch;
18624
18625 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18626 if (attr != nullptr && attr->form_is_constant ())
18627 encoding = attr->constant_value (0);
18628 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18629 if (attr != nullptr)
18630 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18631 name = dwarf2_name (die, cu);
18632 if (!name)
18633 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18634
18635 arch = objfile->arch ();
18636 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18637
18638 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18639 if (attr != nullptr && attr->form_is_constant ())
18640 {
18641 int endianity = attr->constant_value (0);
18642
18643 switch (endianity)
18644 {
18645 case DW_END_big:
18646 byte_order = BFD_ENDIAN_BIG;
18647 break;
18648 case DW_END_little:
18649 byte_order = BFD_ENDIAN_LITTLE;
18650 break;
18651 default:
18652 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18653 break;
18654 }
18655 }
18656
18657 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18658 && cu->language == language_ada
18659 && has_zero_over_zero_small_attribute (die, cu))
18660 {
18661 /* brobecker/2018-02-24: This is a fixed point type for which
18662 the scaling factor is represented as fraction whose value
18663 does not make sense (zero divided by zero), so we should
18664 normally never see these. However, there is a small category
18665 of fixed point types for which GNAT is unable to provide
18666 the scaling factor via the standard DWARF mechanisms, and
18667 for which the info is provided via the GNAT encodings instead.
18668 This is likely what this DIE is about.
18669
18670 Ideally, GNAT should be declaring this type the same way
18671 it declares other fixed point types when using the legacy
18672 GNAT encoding, which is to use a simple signed or unsigned
18673 base type. A report to the GNAT team has been created to
18674 look into it. In the meantime, pretend this type is a simple
18675 signed or unsigned integral, rather than a fixed point type,
18676 to avoid any confusion later on as to how to process this type. */
18677 encoding = (encoding == DW_ATE_signed_fixed
18678 ? DW_ATE_signed
18679 : DW_ATE_unsigned);
18680 }
18681
18682 switch (encoding)
18683 {
18684 case DW_ATE_address:
18685 /* Turn DW_ATE_address into a void * pointer. */
18686 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18687 type = init_pointer_type (objfile, bits, name, type);
18688 break;
18689 case DW_ATE_boolean:
18690 type = init_boolean_type (objfile, bits, 1, name);
18691 break;
18692 case DW_ATE_complex_float:
18693 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18694 byte_order);
18695 if (type->code () == TYPE_CODE_ERROR)
18696 {
18697 if (name == nullptr)
18698 {
18699 struct obstack *obstack
18700 = &cu->per_objfile->objfile->objfile_obstack;
18701 name = obconcat (obstack, "_Complex ", type->name (),
18702 nullptr);
18703 }
18704 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18705 }
18706 else
18707 type = init_complex_type (name, type);
18708 break;
18709 case DW_ATE_decimal_float:
18710 type = init_decfloat_type (objfile, bits, name);
18711 break;
18712 case DW_ATE_float:
18713 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18714 break;
18715 case DW_ATE_signed:
18716 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18717 break;
18718 case DW_ATE_unsigned:
18719 if (cu->language == language_fortran
18720 && name
18721 && startswith (name, "character("))
18722 type = init_character_type (objfile, bits, 1, name);
18723 else
18724 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18725 break;
18726 case DW_ATE_signed_char:
18727 if (cu->language == language_ada || cu->language == language_m2
18728 || cu->language == language_pascal
18729 || cu->language == language_fortran)
18730 type = init_character_type (objfile, bits, 0, name);
18731 else
18732 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18733 break;
18734 case DW_ATE_unsigned_char:
18735 if (cu->language == language_ada || cu->language == language_m2
18736 || cu->language == language_pascal
18737 || cu->language == language_fortran
18738 || cu->language == language_rust)
18739 type = init_character_type (objfile, bits, 1, name);
18740 else
18741 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18742 break;
18743 case DW_ATE_UTF:
18744 {
18745 if (bits == 16)
18746 type = builtin_type (arch)->builtin_char16;
18747 else if (bits == 32)
18748 type = builtin_type (arch)->builtin_char32;
18749 else
18750 {
18751 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18752 bits);
18753 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18754 }
18755 return set_die_type (die, type, cu);
18756 }
18757 break;
18758 case DW_ATE_signed_fixed:
18759 type = init_fixed_point_type (objfile, bits, 0, name);
18760 finish_fixed_point_type (type, die, cu);
18761 break;
18762 case DW_ATE_unsigned_fixed:
18763 type = init_fixed_point_type (objfile, bits, 1, name);
18764 finish_fixed_point_type (type, die, cu);
18765 break;
18766
18767 default:
18768 complaint (_("unsupported DW_AT_encoding: '%s'"),
18769 dwarf_type_encoding_name (encoding));
18770 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18771 break;
18772 }
18773
18774 if (name && strcmp (name, "char") == 0)
18775 type->set_has_no_signedness (true);
18776
18777 maybe_set_alignment (cu, die, type);
18778
18779 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18780
18781 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18782 {
18783 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18784 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18785 {
18786 unsigned real_bit_size = attr->as_unsigned ();
18787 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18788 /* Only use the attributes if they make sense together. */
18789 if (attr == nullptr
18790 || (attr->as_unsigned () + real_bit_size
18791 <= 8 * TYPE_LENGTH (type)))
18792 {
18793 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18794 = real_bit_size;
18795 if (attr != nullptr)
18796 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18797 = attr->as_unsigned ();
18798 }
18799 }
18800 }
18801
18802 return set_die_type (die, type, cu);
18803 }
18804
18805 /* Parse dwarf attribute if it's a block, reference or constant and put the
18806 resulting value of the attribute into struct bound_prop.
18807 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18808
18809 static int
18810 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18811 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18812 struct type *default_type)
18813 {
18814 struct dwarf2_property_baton *baton;
18815 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18816 struct objfile *objfile = per_objfile->objfile;
18817 struct obstack *obstack = &objfile->objfile_obstack;
18818
18819 gdb_assert (default_type != NULL);
18820
18821 if (attr == NULL || prop == NULL)
18822 return 0;
18823
18824 if (attr->form_is_block ())
18825 {
18826 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18827 baton->property_type = default_type;
18828 baton->locexpr.per_cu = cu->per_cu;
18829 baton->locexpr.per_objfile = per_objfile;
18830
18831 struct dwarf_block *block = attr->as_block ();
18832 baton->locexpr.size = block->size;
18833 baton->locexpr.data = block->data;
18834 switch (attr->name)
18835 {
18836 case DW_AT_string_length:
18837 baton->locexpr.is_reference = true;
18838 break;
18839 default:
18840 baton->locexpr.is_reference = false;
18841 break;
18842 }
18843
18844 prop->set_locexpr (baton);
18845 gdb_assert (prop->baton () != NULL);
18846 }
18847 else if (attr->form_is_ref ())
18848 {
18849 struct dwarf2_cu *target_cu = cu;
18850 struct die_info *target_die;
18851 struct attribute *target_attr;
18852
18853 target_die = follow_die_ref (die, attr, &target_cu);
18854 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18855 if (target_attr == NULL)
18856 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18857 target_cu);
18858 if (target_attr == NULL)
18859 return 0;
18860
18861 switch (target_attr->name)
18862 {
18863 case DW_AT_location:
18864 if (target_attr->form_is_section_offset ())
18865 {
18866 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18867 baton->property_type = die_type (target_die, target_cu);
18868 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18869 prop->set_loclist (baton);
18870 gdb_assert (prop->baton () != NULL);
18871 }
18872 else if (target_attr->form_is_block ())
18873 {
18874 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18875 baton->property_type = die_type (target_die, target_cu);
18876 baton->locexpr.per_cu = cu->per_cu;
18877 baton->locexpr.per_objfile = per_objfile;
18878 struct dwarf_block *block = target_attr->as_block ();
18879 baton->locexpr.size = block->size;
18880 baton->locexpr.data = block->data;
18881 baton->locexpr.is_reference = true;
18882 prop->set_locexpr (baton);
18883 gdb_assert (prop->baton () != NULL);
18884 }
18885 else
18886 {
18887 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18888 "dynamic property");
18889 return 0;
18890 }
18891 break;
18892 case DW_AT_data_member_location:
18893 {
18894 LONGEST offset;
18895
18896 if (!handle_data_member_location (target_die, target_cu,
18897 &offset))
18898 return 0;
18899
18900 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18901 baton->property_type = read_type_die (target_die->parent,
18902 target_cu);
18903 baton->offset_info.offset = offset;
18904 baton->offset_info.type = die_type (target_die, target_cu);
18905 prop->set_addr_offset (baton);
18906 break;
18907 }
18908 }
18909 }
18910 else if (attr->form_is_constant ())
18911 prop->set_const_val (attr->constant_value (0));
18912 else
18913 {
18914 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18915 dwarf2_name (die, cu));
18916 return 0;
18917 }
18918
18919 return 1;
18920 }
18921
18922 /* See read.h. */
18923
18924 struct type *
18925 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18926 {
18927 struct type *int_type;
18928
18929 /* Helper macro to examine the various builtin types. */
18930 #define TRY_TYPE(F) \
18931 int_type = (unsigned_p \
18932 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18933 : objfile_type (objfile)->builtin_ ## F); \
18934 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18935 return int_type
18936
18937 TRY_TYPE (char);
18938 TRY_TYPE (short);
18939 TRY_TYPE (int);
18940 TRY_TYPE (long);
18941 TRY_TYPE (long_long);
18942
18943 #undef TRY_TYPE
18944
18945 gdb_assert_not_reached ("unable to find suitable integer type");
18946 }
18947
18948 /* See read.h. */
18949
18950 struct type *
18951 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18952 {
18953 int addr_size = this->per_cu->addr_size ();
18954 return this->per_objfile->int_type (addr_size, unsigned_p);
18955 }
18956
18957 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18958 present (which is valid) then compute the default type based on the
18959 compilation units address size. */
18960
18961 static struct type *
18962 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18963 {
18964 struct type *index_type = die_type (die, cu);
18965
18966 /* Dwarf-2 specifications explicitly allows to create subrange types
18967 without specifying a base type.
18968 In that case, the base type must be set to the type of
18969 the lower bound, upper bound or count, in that order, if any of these
18970 three attributes references an object that has a type.
18971 If no base type is found, the Dwarf-2 specifications say that
18972 a signed integer type of size equal to the size of an address should
18973 be used.
18974 For the following C code: `extern char gdb_int [];'
18975 GCC produces an empty range DIE.
18976 FIXME: muller/2010-05-28: Possible references to object for low bound,
18977 high bound or count are not yet handled by this code. */
18978 if (index_type->code () == TYPE_CODE_VOID)
18979 index_type = cu->addr_sized_int_type (false);
18980
18981 return index_type;
18982 }
18983
18984 /* Read the given DW_AT_subrange DIE. */
18985
18986 static struct type *
18987 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18988 {
18989 struct type *base_type, *orig_base_type;
18990 struct type *range_type;
18991 struct attribute *attr;
18992 struct dynamic_prop low, high;
18993 int low_default_is_valid;
18994 int high_bound_is_count = 0;
18995 const char *name;
18996 ULONGEST negative_mask;
18997
18998 orig_base_type = read_subrange_index_type (die, cu);
18999
19000 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
19001 whereas the real type might be. So, we use ORIG_BASE_TYPE when
19002 creating the range type, but we use the result of check_typedef
19003 when examining properties of the type. */
19004 base_type = check_typedef (orig_base_type);
19005
19006 /* The die_type call above may have already set the type for this DIE. */
19007 range_type = get_die_type (die, cu);
19008 if (range_type)
19009 return range_type;
19010
19011 high.set_const_val (0);
19012
19013 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
19014 omitting DW_AT_lower_bound. */
19015 switch (cu->language)
19016 {
19017 case language_c:
19018 case language_cplus:
19019 low.set_const_val (0);
19020 low_default_is_valid = 1;
19021 break;
19022 case language_fortran:
19023 low.set_const_val (1);
19024 low_default_is_valid = 1;
19025 break;
19026 case language_d:
19027 case language_objc:
19028 case language_rust:
19029 low.set_const_val (0);
19030 low_default_is_valid = (cu->header.version >= 4);
19031 break;
19032 case language_ada:
19033 case language_m2:
19034 case language_pascal:
19035 low.set_const_val (1);
19036 low_default_is_valid = (cu->header.version >= 4);
19037 break;
19038 default:
19039 low.set_const_val (0);
19040 low_default_is_valid = 0;
19041 break;
19042 }
19043
19044 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19045 if (attr != nullptr)
19046 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19047 else if (!low_default_is_valid)
19048 complaint (_("Missing DW_AT_lower_bound "
19049 "- DIE at %s [in module %s]"),
19050 sect_offset_str (die->sect_off),
19051 objfile_name (cu->per_objfile->objfile));
19052
19053 struct attribute *attr_ub, *attr_count;
19054 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19055 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19056 {
19057 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19058 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19059 {
19060 /* If bounds are constant do the final calculation here. */
19061 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19062 high.set_const_val (low.const_val () + high.const_val () - 1);
19063 else
19064 high_bound_is_count = 1;
19065 }
19066 else
19067 {
19068 if (attr_ub != NULL)
19069 complaint (_("Unresolved DW_AT_upper_bound "
19070 "- DIE at %s [in module %s]"),
19071 sect_offset_str (die->sect_off),
19072 objfile_name (cu->per_objfile->objfile));
19073 if (attr_count != NULL)
19074 complaint (_("Unresolved DW_AT_count "
19075 "- DIE at %s [in module %s]"),
19076 sect_offset_str (die->sect_off),
19077 objfile_name (cu->per_objfile->objfile));
19078 }
19079 }
19080
19081 LONGEST bias = 0;
19082 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19083 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19084 bias = bias_attr->constant_value (0);
19085
19086 /* Normally, the DWARF producers are expected to use a signed
19087 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19088 But this is unfortunately not always the case, as witnessed
19089 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19090 is used instead. To work around that ambiguity, we treat
19091 the bounds as signed, and thus sign-extend their values, when
19092 the base type is signed. */
19093 negative_mask =
19094 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19095 if (low.kind () == PROP_CONST
19096 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19097 low.set_const_val (low.const_val () | negative_mask);
19098 if (high.kind () == PROP_CONST
19099 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19100 high.set_const_val (high.const_val () | negative_mask);
19101
19102 /* Check for bit and byte strides. */
19103 struct dynamic_prop byte_stride_prop;
19104 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19105 if (attr_byte_stride != nullptr)
19106 {
19107 struct type *prop_type = cu->addr_sized_int_type (false);
19108 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19109 prop_type);
19110 }
19111
19112 struct dynamic_prop bit_stride_prop;
19113 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19114 if (attr_bit_stride != nullptr)
19115 {
19116 /* It only makes sense to have either a bit or byte stride. */
19117 if (attr_byte_stride != nullptr)
19118 {
19119 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19120 "- DIE at %s [in module %s]"),
19121 sect_offset_str (die->sect_off),
19122 objfile_name (cu->per_objfile->objfile));
19123 attr_bit_stride = nullptr;
19124 }
19125 else
19126 {
19127 struct type *prop_type = cu->addr_sized_int_type (false);
19128 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19129 prop_type);
19130 }
19131 }
19132
19133 if (attr_byte_stride != nullptr
19134 || attr_bit_stride != nullptr)
19135 {
19136 bool byte_stride_p = (attr_byte_stride != nullptr);
19137 struct dynamic_prop *stride
19138 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19139
19140 range_type
19141 = create_range_type_with_stride (NULL, orig_base_type, &low,
19142 &high, bias, stride, byte_stride_p);
19143 }
19144 else
19145 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19146
19147 if (high_bound_is_count)
19148 range_type->bounds ()->flag_upper_bound_is_count = 1;
19149
19150 /* Ada expects an empty array on no boundary attributes. */
19151 if (attr == NULL && cu->language != language_ada)
19152 range_type->bounds ()->high.set_undefined ();
19153
19154 name = dwarf2_name (die, cu);
19155 if (name)
19156 range_type->set_name (name);
19157
19158 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19159 if (attr != nullptr)
19160 TYPE_LENGTH (range_type) = attr->constant_value (0);
19161
19162 maybe_set_alignment (cu, die, range_type);
19163
19164 set_die_type (die, range_type, cu);
19165
19166 /* set_die_type should be already done. */
19167 set_descriptive_type (range_type, die, cu);
19168
19169 return range_type;
19170 }
19171
19172 static struct type *
19173 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19174 {
19175 struct type *type;
19176
19177 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19178 type->set_name (dwarf2_name (die, cu));
19179
19180 /* In Ada, an unspecified type is typically used when the description
19181 of the type is deferred to a different unit. When encountering
19182 such a type, we treat it as a stub, and try to resolve it later on,
19183 when needed. */
19184 if (cu->language == language_ada)
19185 type->set_is_stub (true);
19186
19187 return set_die_type (die, type, cu);
19188 }
19189
19190 /* Read a single die and all its descendents. Set the die's sibling
19191 field to NULL; set other fields in the die correctly, and set all
19192 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19193 location of the info_ptr after reading all of those dies. PARENT
19194 is the parent of the die in question. */
19195
19196 static struct die_info *
19197 read_die_and_children (const struct die_reader_specs *reader,
19198 const gdb_byte *info_ptr,
19199 const gdb_byte **new_info_ptr,
19200 struct die_info *parent)
19201 {
19202 struct die_info *die;
19203 const gdb_byte *cur_ptr;
19204
19205 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19206 if (die == NULL)
19207 {
19208 *new_info_ptr = cur_ptr;
19209 return NULL;
19210 }
19211 store_in_ref_table (die, reader->cu);
19212
19213 if (die->has_children)
19214 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19215 else
19216 {
19217 die->child = NULL;
19218 *new_info_ptr = cur_ptr;
19219 }
19220
19221 die->sibling = NULL;
19222 die->parent = parent;
19223 return die;
19224 }
19225
19226 /* Read a die, all of its descendents, and all of its siblings; set
19227 all of the fields of all of the dies correctly. Arguments are as
19228 in read_die_and_children. */
19229
19230 static struct die_info *
19231 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19232 const gdb_byte *info_ptr,
19233 const gdb_byte **new_info_ptr,
19234 struct die_info *parent)
19235 {
19236 struct die_info *first_die, *last_sibling;
19237 const gdb_byte *cur_ptr;
19238
19239 cur_ptr = info_ptr;
19240 first_die = last_sibling = NULL;
19241
19242 while (1)
19243 {
19244 struct die_info *die
19245 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19246
19247 if (die == NULL)
19248 {
19249 *new_info_ptr = cur_ptr;
19250 return first_die;
19251 }
19252
19253 if (!first_die)
19254 first_die = die;
19255 else
19256 last_sibling->sibling = die;
19257
19258 last_sibling = die;
19259 }
19260 }
19261
19262 /* Read a die, all of its descendents, and all of its siblings; set
19263 all of the fields of all of the dies correctly. Arguments are as
19264 in read_die_and_children.
19265 This the main entry point for reading a DIE and all its children. */
19266
19267 static struct die_info *
19268 read_die_and_siblings (const struct die_reader_specs *reader,
19269 const gdb_byte *info_ptr,
19270 const gdb_byte **new_info_ptr,
19271 struct die_info *parent)
19272 {
19273 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19274 new_info_ptr, parent);
19275
19276 if (dwarf_die_debug)
19277 {
19278 fprintf_unfiltered (gdb_stdlog,
19279 "Read die from %s@0x%x of %s:\n",
19280 reader->die_section->get_name (),
19281 (unsigned) (info_ptr - reader->die_section->buffer),
19282 bfd_get_filename (reader->abfd));
19283 dump_die (die, dwarf_die_debug);
19284 }
19285
19286 return die;
19287 }
19288
19289 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19290 attributes.
19291 The caller is responsible for filling in the extra attributes
19292 and updating (*DIEP)->num_attrs.
19293 Set DIEP to point to a newly allocated die with its information,
19294 except for its child, sibling, and parent fields. */
19295
19296 static const gdb_byte *
19297 read_full_die_1 (const struct die_reader_specs *reader,
19298 struct die_info **diep, const gdb_byte *info_ptr,
19299 int num_extra_attrs)
19300 {
19301 unsigned int abbrev_number, bytes_read, i;
19302 struct abbrev_info *abbrev;
19303 struct die_info *die;
19304 struct dwarf2_cu *cu = reader->cu;
19305 bfd *abfd = reader->abfd;
19306
19307 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19308 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19309 info_ptr += bytes_read;
19310 if (!abbrev_number)
19311 {
19312 *diep = NULL;
19313 return info_ptr;
19314 }
19315
19316 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19317 if (!abbrev)
19318 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19319 abbrev_number,
19320 bfd_get_filename (abfd));
19321
19322 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19323 die->sect_off = sect_off;
19324 die->tag = abbrev->tag;
19325 die->abbrev = abbrev_number;
19326 die->has_children = abbrev->has_children;
19327
19328 /* Make the result usable.
19329 The caller needs to update num_attrs after adding the extra
19330 attributes. */
19331 die->num_attrs = abbrev->num_attrs;
19332
19333 bool any_need_reprocess = false;
19334 for (i = 0; i < abbrev->num_attrs; ++i)
19335 {
19336 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19337 info_ptr);
19338 if (die->attrs[i].requires_reprocessing_p ())
19339 any_need_reprocess = true;
19340 }
19341
19342 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19343 if (attr != nullptr && attr->form_is_unsigned ())
19344 cu->str_offsets_base = attr->as_unsigned ();
19345
19346 attr = die->attr (DW_AT_loclists_base);
19347 if (attr != nullptr)
19348 cu->loclist_base = attr->as_unsigned ();
19349
19350 auto maybe_addr_base = die->addr_base ();
19351 if (maybe_addr_base.has_value ())
19352 cu->addr_base = *maybe_addr_base;
19353
19354 attr = die->attr (DW_AT_rnglists_base);
19355 if (attr != nullptr)
19356 cu->rnglists_base = attr->as_unsigned ();
19357
19358 if (any_need_reprocess)
19359 {
19360 for (i = 0; i < abbrev->num_attrs; ++i)
19361 {
19362 if (die->attrs[i].requires_reprocessing_p ())
19363 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19364 }
19365 }
19366 *diep = die;
19367 return info_ptr;
19368 }
19369
19370 /* Read a die and all its attributes.
19371 Set DIEP to point to a newly allocated die with its information,
19372 except for its child, sibling, and parent fields. */
19373
19374 static const gdb_byte *
19375 read_full_die (const struct die_reader_specs *reader,
19376 struct die_info **diep, const gdb_byte *info_ptr)
19377 {
19378 const gdb_byte *result;
19379
19380 result = read_full_die_1 (reader, diep, info_ptr, 0);
19381
19382 if (dwarf_die_debug)
19383 {
19384 fprintf_unfiltered (gdb_stdlog,
19385 "Read die from %s@0x%x of %s:\n",
19386 reader->die_section->get_name (),
19387 (unsigned) (info_ptr - reader->die_section->buffer),
19388 bfd_get_filename (reader->abfd));
19389 dump_die (*diep, dwarf_die_debug);
19390 }
19391
19392 return result;
19393 }
19394 \f
19395
19396 /* Returns nonzero if TAG represents a type that we might generate a partial
19397 symbol for. */
19398
19399 static int
19400 is_type_tag_for_partial (int tag, enum language lang)
19401 {
19402 switch (tag)
19403 {
19404 #if 0
19405 /* Some types that would be reasonable to generate partial symbols for,
19406 that we don't at present. Note that normally this does not
19407 matter, mainly because C compilers don't give names to these
19408 types, but instead emit DW_TAG_typedef. */
19409 case DW_TAG_file_type:
19410 case DW_TAG_ptr_to_member_type:
19411 case DW_TAG_set_type:
19412 case DW_TAG_string_type:
19413 case DW_TAG_subroutine_type:
19414 #endif
19415
19416 /* GNAT may emit an array with a name, but no typedef, so we
19417 need to make a symbol in this case. */
19418 case DW_TAG_array_type:
19419 return lang == language_ada;
19420
19421 case DW_TAG_base_type:
19422 case DW_TAG_class_type:
19423 case DW_TAG_interface_type:
19424 case DW_TAG_enumeration_type:
19425 case DW_TAG_structure_type:
19426 case DW_TAG_subrange_type:
19427 case DW_TAG_typedef:
19428 case DW_TAG_union_type:
19429 return 1;
19430 default:
19431 return 0;
19432 }
19433 }
19434
19435 /* Load all DIEs that are interesting for partial symbols into memory. */
19436
19437 static struct partial_die_info *
19438 load_partial_dies (const struct die_reader_specs *reader,
19439 const gdb_byte *info_ptr, int building_psymtab)
19440 {
19441 struct dwarf2_cu *cu = reader->cu;
19442 struct objfile *objfile = cu->per_objfile->objfile;
19443 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19444 unsigned int bytes_read;
19445 unsigned int load_all = 0;
19446 int nesting_level = 1;
19447
19448 parent_die = NULL;
19449 last_die = NULL;
19450
19451 gdb_assert (cu->per_cu != NULL);
19452 if (cu->per_cu->load_all_dies)
19453 load_all = 1;
19454
19455 cu->partial_dies
19456 = htab_create_alloc_ex (cu->header.length / 12,
19457 partial_die_hash,
19458 partial_die_eq,
19459 NULL,
19460 &cu->comp_unit_obstack,
19461 hashtab_obstack_allocate,
19462 dummy_obstack_deallocate);
19463
19464 while (1)
19465 {
19466 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19467
19468 /* A NULL abbrev means the end of a series of children. */
19469 if (abbrev == NULL)
19470 {
19471 if (--nesting_level == 0)
19472 return first_die;
19473
19474 info_ptr += bytes_read;
19475 last_die = parent_die;
19476 parent_die = parent_die->die_parent;
19477 continue;
19478 }
19479
19480 /* Check for template arguments. We never save these; if
19481 they're seen, we just mark the parent, and go on our way. */
19482 if (parent_die != NULL
19483 && cu->language == language_cplus
19484 && (abbrev->tag == DW_TAG_template_type_param
19485 || abbrev->tag == DW_TAG_template_value_param))
19486 {
19487 parent_die->has_template_arguments = 1;
19488
19489 if (!load_all)
19490 {
19491 /* We don't need a partial DIE for the template argument. */
19492 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19493 continue;
19494 }
19495 }
19496
19497 /* We only recurse into c++ subprograms looking for template arguments.
19498 Skip their other children. */
19499 if (!load_all
19500 && cu->language == language_cplus
19501 && parent_die != NULL
19502 && parent_die->tag == DW_TAG_subprogram
19503 && abbrev->tag != DW_TAG_inlined_subroutine)
19504 {
19505 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19506 continue;
19507 }
19508
19509 /* Check whether this DIE is interesting enough to save. Normally
19510 we would not be interested in members here, but there may be
19511 later variables referencing them via DW_AT_specification (for
19512 static members). */
19513 if (!load_all
19514 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19515 && abbrev->tag != DW_TAG_constant
19516 && abbrev->tag != DW_TAG_enumerator
19517 && abbrev->tag != DW_TAG_subprogram
19518 && abbrev->tag != DW_TAG_inlined_subroutine
19519 && abbrev->tag != DW_TAG_lexical_block
19520 && abbrev->tag != DW_TAG_variable
19521 && abbrev->tag != DW_TAG_namespace
19522 && abbrev->tag != DW_TAG_module
19523 && abbrev->tag != DW_TAG_member
19524 && abbrev->tag != DW_TAG_imported_unit
19525 && abbrev->tag != DW_TAG_imported_declaration)
19526 {
19527 /* Otherwise we skip to the next sibling, if any. */
19528 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19529 continue;
19530 }
19531
19532 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19533 abbrev);
19534
19535 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19536
19537 /* This two-pass algorithm for processing partial symbols has a
19538 high cost in cache pressure. Thus, handle some simple cases
19539 here which cover the majority of C partial symbols. DIEs
19540 which neither have specification tags in them, nor could have
19541 specification tags elsewhere pointing at them, can simply be
19542 processed and discarded.
19543
19544 This segment is also optional; scan_partial_symbols and
19545 add_partial_symbol will handle these DIEs if we chain
19546 them in normally. When compilers which do not emit large
19547 quantities of duplicate debug information are more common,
19548 this code can probably be removed. */
19549
19550 /* Any complete simple types at the top level (pretty much all
19551 of them, for a language without namespaces), can be processed
19552 directly. */
19553 if (parent_die == NULL
19554 && pdi.has_specification == 0
19555 && pdi.is_declaration == 0
19556 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19557 || pdi.tag == DW_TAG_base_type
19558 || pdi.tag == DW_TAG_array_type
19559 || pdi.tag == DW_TAG_subrange_type))
19560 {
19561 if (building_psymtab && pdi.raw_name != NULL)
19562 add_partial_symbol (&pdi, cu);
19563
19564 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19565 continue;
19566 }
19567
19568 /* The exception for DW_TAG_typedef with has_children above is
19569 a workaround of GCC PR debug/47510. In the case of this complaint
19570 type_name_or_error will error on such types later.
19571
19572 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19573 it could not find the child DIEs referenced later, this is checked
19574 above. In correct DWARF DW_TAG_typedef should have no children. */
19575
19576 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19577 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19578 "- DIE at %s [in module %s]"),
19579 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19580
19581 /* If we're at the second level, and we're an enumerator, and
19582 our parent has no specification (meaning possibly lives in a
19583 namespace elsewhere), then we can add the partial symbol now
19584 instead of queueing it. */
19585 if (pdi.tag == DW_TAG_enumerator
19586 && parent_die != NULL
19587 && parent_die->die_parent == NULL
19588 && parent_die->tag == DW_TAG_enumeration_type
19589 && parent_die->has_specification == 0)
19590 {
19591 if (pdi.raw_name == NULL)
19592 complaint (_("malformed enumerator DIE ignored"));
19593 else if (building_psymtab)
19594 add_partial_symbol (&pdi, cu);
19595
19596 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19597 continue;
19598 }
19599
19600 struct partial_die_info *part_die
19601 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19602
19603 /* We'll save this DIE so link it in. */
19604 part_die->die_parent = parent_die;
19605 part_die->die_sibling = NULL;
19606 part_die->die_child = NULL;
19607
19608 if (last_die && last_die == parent_die)
19609 last_die->die_child = part_die;
19610 else if (last_die)
19611 last_die->die_sibling = part_die;
19612
19613 last_die = part_die;
19614
19615 if (first_die == NULL)
19616 first_die = part_die;
19617
19618 /* Maybe add the DIE to the hash table. Not all DIEs that we
19619 find interesting need to be in the hash table, because we
19620 also have the parent/sibling/child chains; only those that we
19621 might refer to by offset later during partial symbol reading.
19622
19623 For now this means things that might have be the target of a
19624 DW_AT_specification, DW_AT_abstract_origin, or
19625 DW_AT_extension. DW_AT_extension will refer only to
19626 namespaces; DW_AT_abstract_origin refers to functions (and
19627 many things under the function DIE, but we do not recurse
19628 into function DIEs during partial symbol reading) and
19629 possibly variables as well; DW_AT_specification refers to
19630 declarations. Declarations ought to have the DW_AT_declaration
19631 flag. It happens that GCC forgets to put it in sometimes, but
19632 only for functions, not for types.
19633
19634 Adding more things than necessary to the hash table is harmless
19635 except for the performance cost. Adding too few will result in
19636 wasted time in find_partial_die, when we reread the compilation
19637 unit with load_all_dies set. */
19638
19639 if (load_all
19640 || abbrev->tag == DW_TAG_constant
19641 || abbrev->tag == DW_TAG_subprogram
19642 || abbrev->tag == DW_TAG_variable
19643 || abbrev->tag == DW_TAG_namespace
19644 || part_die->is_declaration)
19645 {
19646 void **slot;
19647
19648 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19649 to_underlying (part_die->sect_off),
19650 INSERT);
19651 *slot = part_die;
19652 }
19653
19654 /* For some DIEs we want to follow their children (if any). For C
19655 we have no reason to follow the children of structures; for other
19656 languages we have to, so that we can get at method physnames
19657 to infer fully qualified class names, for DW_AT_specification,
19658 and for C++ template arguments. For C++, we also look one level
19659 inside functions to find template arguments (if the name of the
19660 function does not already contain the template arguments).
19661
19662 For Ada and Fortran, we need to scan the children of subprograms
19663 and lexical blocks as well because these languages allow the
19664 definition of nested entities that could be interesting for the
19665 debugger, such as nested subprograms for instance. */
19666 if (last_die->has_children
19667 && (load_all
19668 || last_die->tag == DW_TAG_namespace
19669 || last_die->tag == DW_TAG_module
19670 || last_die->tag == DW_TAG_enumeration_type
19671 || (cu->language == language_cplus
19672 && last_die->tag == DW_TAG_subprogram
19673 && (last_die->raw_name == NULL
19674 || strchr (last_die->raw_name, '<') == NULL))
19675 || (cu->language != language_c
19676 && (last_die->tag == DW_TAG_class_type
19677 || last_die->tag == DW_TAG_interface_type
19678 || last_die->tag == DW_TAG_structure_type
19679 || last_die->tag == DW_TAG_union_type))
19680 || ((cu->language == language_ada
19681 || cu->language == language_fortran)
19682 && (last_die->tag == DW_TAG_subprogram
19683 || last_die->tag == DW_TAG_lexical_block))))
19684 {
19685 nesting_level++;
19686 parent_die = last_die;
19687 continue;
19688 }
19689
19690 /* Otherwise we skip to the next sibling, if any. */
19691 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19692
19693 /* Back to the top, do it again. */
19694 }
19695 }
19696
19697 partial_die_info::partial_die_info (sect_offset sect_off_,
19698 struct abbrev_info *abbrev)
19699 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19700 {
19701 }
19702
19703 /* See class definition. */
19704
19705 const char *
19706 partial_die_info::name (dwarf2_cu *cu)
19707 {
19708 if (!canonical_name && raw_name != nullptr)
19709 {
19710 struct objfile *objfile = cu->per_objfile->objfile;
19711 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19712 canonical_name = 1;
19713 }
19714
19715 return raw_name;
19716 }
19717
19718 /* Read a minimal amount of information into the minimal die structure.
19719 INFO_PTR should point just after the initial uleb128 of a DIE. */
19720
19721 const gdb_byte *
19722 partial_die_info::read (const struct die_reader_specs *reader,
19723 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19724 {
19725 struct dwarf2_cu *cu = reader->cu;
19726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19727 unsigned int i;
19728 int has_low_pc_attr = 0;
19729 int has_high_pc_attr = 0;
19730 int high_pc_relative = 0;
19731
19732 for (i = 0; i < abbrev.num_attrs; ++i)
19733 {
19734 attribute attr;
19735 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19736 /* String and address offsets that need to do the reprocessing have
19737 already been read at this point, so there is no need to wait until
19738 the loop terminates to do the reprocessing. */
19739 if (attr.requires_reprocessing_p ())
19740 read_attribute_reprocess (reader, &attr, tag);
19741 /* Store the data if it is of an attribute we want to keep in a
19742 partial symbol table. */
19743 switch (attr.name)
19744 {
19745 case DW_AT_name:
19746 switch (tag)
19747 {
19748 case DW_TAG_compile_unit:
19749 case DW_TAG_partial_unit:
19750 case DW_TAG_type_unit:
19751 /* Compilation units have a DW_AT_name that is a filename, not
19752 a source language identifier. */
19753 case DW_TAG_enumeration_type:
19754 case DW_TAG_enumerator:
19755 /* These tags always have simple identifiers already; no need
19756 to canonicalize them. */
19757 canonical_name = 1;
19758 raw_name = attr.as_string ();
19759 break;
19760 default:
19761 canonical_name = 0;
19762 raw_name = attr.as_string ();
19763 break;
19764 }
19765 break;
19766 case DW_AT_linkage_name:
19767 case DW_AT_MIPS_linkage_name:
19768 /* Note that both forms of linkage name might appear. We
19769 assume they will be the same, and we only store the last
19770 one we see. */
19771 linkage_name = attr.as_string ();
19772 break;
19773 case DW_AT_low_pc:
19774 has_low_pc_attr = 1;
19775 lowpc = attr.as_address ();
19776 break;
19777 case DW_AT_high_pc:
19778 has_high_pc_attr = 1;
19779 highpc = attr.as_address ();
19780 if (cu->header.version >= 4 && attr.form_is_constant ())
19781 high_pc_relative = 1;
19782 break;
19783 case DW_AT_location:
19784 /* Support the .debug_loc offsets. */
19785 if (attr.form_is_block ())
19786 {
19787 d.locdesc = attr.as_block ();
19788 }
19789 else if (attr.form_is_section_offset ())
19790 {
19791 dwarf2_complex_location_expr_complaint ();
19792 }
19793 else
19794 {
19795 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19796 "partial symbol information");
19797 }
19798 break;
19799 case DW_AT_external:
19800 is_external = attr.as_boolean ();
19801 break;
19802 case DW_AT_declaration:
19803 is_declaration = attr.as_boolean ();
19804 break;
19805 case DW_AT_type:
19806 has_type = 1;
19807 break;
19808 case DW_AT_abstract_origin:
19809 case DW_AT_specification:
19810 case DW_AT_extension:
19811 has_specification = 1;
19812 spec_offset = attr.get_ref_die_offset ();
19813 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19814 || cu->per_cu->is_dwz);
19815 break;
19816 case DW_AT_sibling:
19817 /* Ignore absolute siblings, they might point outside of
19818 the current compile unit. */
19819 if (attr.form == DW_FORM_ref_addr)
19820 complaint (_("ignoring absolute DW_AT_sibling"));
19821 else
19822 {
19823 const gdb_byte *buffer = reader->buffer;
19824 sect_offset off = attr.get_ref_die_offset ();
19825 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19826
19827 if (sibling_ptr < info_ptr)
19828 complaint (_("DW_AT_sibling points backwards"));
19829 else if (sibling_ptr > reader->buffer_end)
19830 reader->die_section->overflow_complaint ();
19831 else
19832 sibling = sibling_ptr;
19833 }
19834 break;
19835 case DW_AT_byte_size:
19836 has_byte_size = 1;
19837 break;
19838 case DW_AT_const_value:
19839 has_const_value = 1;
19840 break;
19841 case DW_AT_calling_convention:
19842 /* DWARF doesn't provide a way to identify a program's source-level
19843 entry point. DW_AT_calling_convention attributes are only meant
19844 to describe functions' calling conventions.
19845
19846 However, because it's a necessary piece of information in
19847 Fortran, and before DWARF 4 DW_CC_program was the only
19848 piece of debugging information whose definition refers to
19849 a 'main program' at all, several compilers marked Fortran
19850 main programs with DW_CC_program --- even when those
19851 functions use the standard calling conventions.
19852
19853 Although DWARF now specifies a way to provide this
19854 information, we support this practice for backward
19855 compatibility. */
19856 if (attr.constant_value (0) == DW_CC_program
19857 && cu->language == language_fortran)
19858 main_subprogram = 1;
19859 break;
19860 case DW_AT_inline:
19861 {
19862 LONGEST value = attr.constant_value (-1);
19863 if (value == DW_INL_inlined
19864 || value == DW_INL_declared_inlined)
19865 may_be_inlined = 1;
19866 }
19867 break;
19868
19869 case DW_AT_import:
19870 if (tag == DW_TAG_imported_unit)
19871 {
19872 d.sect_off = attr.get_ref_die_offset ();
19873 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19874 || cu->per_cu->is_dwz);
19875 }
19876 break;
19877
19878 case DW_AT_main_subprogram:
19879 main_subprogram = attr.as_boolean ();
19880 break;
19881
19882 case DW_AT_ranges:
19883 {
19884 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19885 on DWARF version). */
19886 ULONGEST ranges_offset = attr.as_unsigned ();
19887
19888 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19889 this value. */
19890 if (tag != DW_TAG_compile_unit)
19891 ranges_offset += cu->gnu_ranges_base;
19892
19893 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19894 nullptr, tag))
19895 has_pc_info = 1;
19896 }
19897 break;
19898
19899 default:
19900 break;
19901 }
19902 }
19903
19904 /* For Ada, if both the name and the linkage name appear, we prefer
19905 the latter. This lets "catch exception" work better, regardless
19906 of the order in which the name and linkage name were emitted.
19907 Really, though, this is just a workaround for the fact that gdb
19908 doesn't store both the name and the linkage name. */
19909 if (cu->language == language_ada && linkage_name != nullptr)
19910 raw_name = linkage_name;
19911
19912 if (high_pc_relative)
19913 highpc += lowpc;
19914
19915 if (has_low_pc_attr && has_high_pc_attr)
19916 {
19917 /* When using the GNU linker, .gnu.linkonce. sections are used to
19918 eliminate duplicate copies of functions and vtables and such.
19919 The linker will arbitrarily choose one and discard the others.
19920 The AT_*_pc values for such functions refer to local labels in
19921 these sections. If the section from that file was discarded, the
19922 labels are not in the output, so the relocs get a value of 0.
19923 If this is a discarded function, mark the pc bounds as invalid,
19924 so that GDB will ignore it. */
19925 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19926 {
19927 struct objfile *objfile = per_objfile->objfile;
19928 struct gdbarch *gdbarch = objfile->arch ();
19929
19930 complaint (_("DW_AT_low_pc %s is zero "
19931 "for DIE at %s [in module %s]"),
19932 paddress (gdbarch, lowpc),
19933 sect_offset_str (sect_off),
19934 objfile_name (objfile));
19935 }
19936 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19937 else if (lowpc >= highpc)
19938 {
19939 struct objfile *objfile = per_objfile->objfile;
19940 struct gdbarch *gdbarch = objfile->arch ();
19941
19942 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19943 "for DIE at %s [in module %s]"),
19944 paddress (gdbarch, lowpc),
19945 paddress (gdbarch, highpc),
19946 sect_offset_str (sect_off),
19947 objfile_name (objfile));
19948 }
19949 else
19950 has_pc_info = 1;
19951 }
19952
19953 return info_ptr;
19954 }
19955
19956 /* Find a cached partial DIE at OFFSET in CU. */
19957
19958 struct partial_die_info *
19959 dwarf2_cu::find_partial_die (sect_offset sect_off)
19960 {
19961 struct partial_die_info *lookup_die = NULL;
19962 struct partial_die_info part_die (sect_off);
19963
19964 lookup_die = ((struct partial_die_info *)
19965 htab_find_with_hash (partial_dies, &part_die,
19966 to_underlying (sect_off)));
19967
19968 return lookup_die;
19969 }
19970
19971 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19972 except in the case of .debug_types DIEs which do not reference
19973 outside their CU (they do however referencing other types via
19974 DW_FORM_ref_sig8). */
19975
19976 static const struct cu_partial_die_info
19977 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19978 {
19979 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19980 struct objfile *objfile = per_objfile->objfile;
19981 struct partial_die_info *pd = NULL;
19982
19983 if (offset_in_dwz == cu->per_cu->is_dwz
19984 && cu->header.offset_in_cu_p (sect_off))
19985 {
19986 pd = cu->find_partial_die (sect_off);
19987 if (pd != NULL)
19988 return { cu, pd };
19989 /* We missed recording what we needed.
19990 Load all dies and try again. */
19991 }
19992 else
19993 {
19994 /* TUs don't reference other CUs/TUs (except via type signatures). */
19995 if (cu->per_cu->is_debug_types)
19996 {
19997 error (_("Dwarf Error: Type Unit at offset %s contains"
19998 " external reference to offset %s [in module %s].\n"),
19999 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
20000 bfd_get_filename (objfile->obfd));
20001 }
20002 dwarf2_per_cu_data *per_cu
20003 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
20004 per_objfile);
20005
20006 cu = per_objfile->get_cu (per_cu);
20007 if (cu == NULL || cu->partial_dies == NULL)
20008 load_partial_comp_unit (per_cu, per_objfile, nullptr);
20009
20010 cu = per_objfile->get_cu (per_cu);
20011
20012 cu->last_used = 0;
20013 pd = cu->find_partial_die (sect_off);
20014 }
20015
20016 /* If we didn't find it, and not all dies have been loaded,
20017 load them all and try again. */
20018
20019 if (pd == NULL && cu->per_cu->load_all_dies == 0)
20020 {
20021 cu->per_cu->load_all_dies = 1;
20022
20023 /* This is nasty. When we reread the DIEs, somewhere up the call chain
20024 THIS_CU->cu may already be in use. So we can't just free it and
20025 replace its DIEs with the ones we read in. Instead, we leave those
20026 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
20027 and clobber THIS_CU->cu->partial_dies with the hash table for the new
20028 set. */
20029 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
20030
20031 pd = cu->find_partial_die (sect_off);
20032 }
20033
20034 if (pd == NULL)
20035 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
20036 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
20037 return { cu, pd };
20038 }
20039
20040 /* See if we can figure out if the class lives in a namespace. We do
20041 this by looking for a member function; its demangled name will
20042 contain namespace info, if there is any. */
20043
20044 static void
20045 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20046 struct dwarf2_cu *cu)
20047 {
20048 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20049 what template types look like, because the demangler
20050 frequently doesn't give the same name as the debug info. We
20051 could fix this by only using the demangled name to get the
20052 prefix (but see comment in read_structure_type). */
20053
20054 struct partial_die_info *real_pdi;
20055 struct partial_die_info *child_pdi;
20056
20057 /* If this DIE (this DIE's specification, if any) has a parent, then
20058 we should not do this. We'll prepend the parent's fully qualified
20059 name when we create the partial symbol. */
20060
20061 real_pdi = struct_pdi;
20062 while (real_pdi->has_specification)
20063 {
20064 auto res = find_partial_die (real_pdi->spec_offset,
20065 real_pdi->spec_is_dwz, cu);
20066 real_pdi = res.pdi;
20067 cu = res.cu;
20068 }
20069
20070 if (real_pdi->die_parent != NULL)
20071 return;
20072
20073 for (child_pdi = struct_pdi->die_child;
20074 child_pdi != NULL;
20075 child_pdi = child_pdi->die_sibling)
20076 {
20077 if (child_pdi->tag == DW_TAG_subprogram
20078 && child_pdi->linkage_name != NULL)
20079 {
20080 gdb::unique_xmalloc_ptr<char> actual_class_name
20081 (cu->language_defn->class_name_from_physname
20082 (child_pdi->linkage_name));
20083 if (actual_class_name != NULL)
20084 {
20085 struct objfile *objfile = cu->per_objfile->objfile;
20086 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20087 struct_pdi->canonical_name = 1;
20088 }
20089 break;
20090 }
20091 }
20092 }
20093
20094 /* Return true if a DIE with TAG may have the DW_AT_const_value
20095 attribute. */
20096
20097 static bool
20098 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20099 {
20100 switch (tag)
20101 {
20102 case DW_TAG_constant:
20103 case DW_TAG_enumerator:
20104 case DW_TAG_formal_parameter:
20105 case DW_TAG_template_value_param:
20106 case DW_TAG_variable:
20107 return true;
20108 }
20109
20110 return false;
20111 }
20112
20113 void
20114 partial_die_info::fixup (struct dwarf2_cu *cu)
20115 {
20116 /* Once we've fixed up a die, there's no point in doing so again.
20117 This also avoids a memory leak if we were to call
20118 guess_partial_die_structure_name multiple times. */
20119 if (fixup_called)
20120 return;
20121
20122 /* If we found a reference attribute and the DIE has no name, try
20123 to find a name in the referred to DIE. */
20124
20125 if (raw_name == NULL && has_specification)
20126 {
20127 struct partial_die_info *spec_die;
20128
20129 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20130 spec_die = res.pdi;
20131 cu = res.cu;
20132
20133 spec_die->fixup (cu);
20134
20135 if (spec_die->raw_name)
20136 {
20137 raw_name = spec_die->raw_name;
20138 canonical_name = spec_die->canonical_name;
20139
20140 /* Copy DW_AT_external attribute if it is set. */
20141 if (spec_die->is_external)
20142 is_external = spec_die->is_external;
20143 }
20144 }
20145
20146 if (!has_const_value && has_specification
20147 && can_have_DW_AT_const_value_p (tag))
20148 {
20149 struct partial_die_info *spec_die;
20150
20151 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20152 spec_die = res.pdi;
20153 cu = res.cu;
20154
20155 spec_die->fixup (cu);
20156
20157 if (spec_die->has_const_value)
20158 {
20159 /* Copy DW_AT_const_value attribute if it is set. */
20160 has_const_value = spec_die->has_const_value;
20161 }
20162 }
20163
20164 /* Set default names for some unnamed DIEs. */
20165
20166 if (raw_name == NULL && tag == DW_TAG_namespace)
20167 {
20168 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20169 canonical_name = 1;
20170 }
20171
20172 /* If there is no parent die to provide a namespace, and there are
20173 children, see if we can determine the namespace from their linkage
20174 name. */
20175 if (cu->language == language_cplus
20176 && !cu->per_objfile->per_bfd->types.empty ()
20177 && die_parent == NULL
20178 && has_children
20179 && (tag == DW_TAG_class_type
20180 || tag == DW_TAG_structure_type
20181 || tag == DW_TAG_union_type))
20182 guess_partial_die_structure_name (this, cu);
20183
20184 /* GCC might emit a nameless struct or union that has a linkage
20185 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20186 if (raw_name == NULL
20187 && (tag == DW_TAG_class_type
20188 || tag == DW_TAG_interface_type
20189 || tag == DW_TAG_structure_type
20190 || tag == DW_TAG_union_type)
20191 && linkage_name != NULL)
20192 {
20193 gdb::unique_xmalloc_ptr<char> demangled
20194 (gdb_demangle (linkage_name, DMGL_TYPES));
20195 if (demangled != nullptr)
20196 {
20197 const char *base;
20198
20199 /* Strip any leading namespaces/classes, keep only the base name.
20200 DW_AT_name for named DIEs does not contain the prefixes. */
20201 base = strrchr (demangled.get (), ':');
20202 if (base && base > demangled.get () && base[-1] == ':')
20203 base++;
20204 else
20205 base = demangled.get ();
20206
20207 struct objfile *objfile = cu->per_objfile->objfile;
20208 raw_name = objfile->intern (base);
20209 canonical_name = 1;
20210 }
20211 }
20212
20213 fixup_called = 1;
20214 }
20215
20216 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20217 contents from the given SECTION in the HEADER.
20218
20219 HEADER_OFFSET is the offset of the header in the section. */
20220 static void
20221 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20222 struct dwarf2_section_info *section,
20223 sect_offset header_offset)
20224 {
20225 unsigned int bytes_read;
20226 bfd *abfd = section->get_bfd_owner ();
20227 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20228
20229 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20230 info_ptr += bytes_read;
20231
20232 header->version = read_2_bytes (abfd, info_ptr);
20233 info_ptr += 2;
20234
20235 header->addr_size = read_1_byte (abfd, info_ptr);
20236 info_ptr += 1;
20237
20238 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20239 info_ptr += 1;
20240
20241 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20242 }
20243
20244 /* Return the DW_AT_loclists_base value for the CU. */
20245 static ULONGEST
20246 lookup_loclist_base (struct dwarf2_cu *cu)
20247 {
20248 /* For the .dwo unit, the loclist_base points to the first offset following
20249 the header. The header consists of the following entities-
20250 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20251 bit format)
20252 2. version (2 bytes)
20253 3. address size (1 byte)
20254 4. segment selector size (1 byte)
20255 5. offset entry count (4 bytes)
20256 These sizes are derived as per the DWARFv5 standard. */
20257 if (cu->dwo_unit != nullptr)
20258 {
20259 if (cu->header.initial_length_size == 4)
20260 return LOCLIST_HEADER_SIZE32;
20261 return LOCLIST_HEADER_SIZE64;
20262 }
20263 return cu->loclist_base;
20264 }
20265
20266 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20267 array of offsets in the .debug_loclists section. */
20268
20269 static sect_offset
20270 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20271 {
20272 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20273 struct objfile *objfile = per_objfile->objfile;
20274 bfd *abfd = objfile->obfd;
20275 ULONGEST loclist_header_size =
20276 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20277 : LOCLIST_HEADER_SIZE64);
20278 ULONGEST loclist_base = lookup_loclist_base (cu);
20279
20280 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20281 ULONGEST start_offset =
20282 loclist_base + loclist_index * cu->header.offset_size;
20283
20284 /* Get loclists section. */
20285 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20286
20287 /* Read the loclists section content. */
20288 section->read (objfile);
20289 if (section->buffer == NULL)
20290 error (_("DW_FORM_loclistx used without .debug_loclists "
20291 "section [in module %s]"), objfile_name (objfile));
20292
20293 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20294 so if loclist_base is smaller than the header size, we have a problem. */
20295 if (loclist_base < loclist_header_size)
20296 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20297 objfile_name (objfile));
20298
20299 /* Read the header of the loclists contribution. */
20300 struct loclists_rnglists_header header;
20301 read_loclists_rnglists_header (&header, section,
20302 (sect_offset) (loclist_base - loclist_header_size));
20303
20304 /* Verify the loclist index is valid. */
20305 if (loclist_index >= header.offset_entry_count)
20306 error (_("DW_FORM_loclistx pointing outside of "
20307 ".debug_loclists offset array [in module %s]"),
20308 objfile_name (objfile));
20309
20310 /* Validate that reading won't go beyond the end of the section. */
20311 if (start_offset + cu->header.offset_size > section->size)
20312 error (_("Reading DW_FORM_loclistx index beyond end of"
20313 ".debug_loclists section [in module %s]"),
20314 objfile_name (objfile));
20315
20316 const gdb_byte *info_ptr = section->buffer + start_offset;
20317
20318 if (cu->header.offset_size == 4)
20319 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20320 else
20321 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20322 }
20323
20324 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20325 array of offsets in the .debug_rnglists section. */
20326
20327 static sect_offset
20328 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20329 dwarf_tag tag)
20330 {
20331 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20332 struct objfile *objfile = dwarf2_per_objfile->objfile;
20333 bfd *abfd = objfile->obfd;
20334 ULONGEST rnglist_header_size =
20335 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20336 : RNGLIST_HEADER_SIZE64);
20337
20338 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20339 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20340 doesn't apply. */
20341 ULONGEST rnglist_base =
20342 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20343
20344 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20345 ULONGEST start_offset =
20346 rnglist_base + rnglist_index * cu->header.offset_size;
20347
20348 /* Get rnglists section. */
20349 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20350
20351 /* Read the rnglists section content. */
20352 section->read (objfile);
20353 if (section->buffer == nullptr)
20354 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20355 "[in module %s]"),
20356 objfile_name (objfile));
20357
20358 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20359 so if rnglist_base is smaller than the header size, we have a problem. */
20360 if (rnglist_base < rnglist_header_size)
20361 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20362 objfile_name (objfile));
20363
20364 /* Read the header of the rnglists contribution. */
20365 struct loclists_rnglists_header header;
20366 read_loclists_rnglists_header (&header, section,
20367 (sect_offset) (rnglist_base - rnglist_header_size));
20368
20369 /* Verify the rnglist index is valid. */
20370 if (rnglist_index >= header.offset_entry_count)
20371 error (_("DW_FORM_rnglistx index pointing outside of "
20372 ".debug_rnglists offset array [in module %s]"),
20373 objfile_name (objfile));
20374
20375 /* Validate that reading won't go beyond the end of the section. */
20376 if (start_offset + cu->header.offset_size > section->size)
20377 error (_("Reading DW_FORM_rnglistx index beyond end of"
20378 ".debug_rnglists section [in module %s]"),
20379 objfile_name (objfile));
20380
20381 const gdb_byte *info_ptr = section->buffer + start_offset;
20382
20383 if (cu->header.offset_size == 4)
20384 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20385 else
20386 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20387 }
20388
20389 /* Process the attributes that had to be skipped in the first round. These
20390 attributes are the ones that need str_offsets_base or addr_base attributes.
20391 They could not have been processed in the first round, because at the time
20392 the values of str_offsets_base or addr_base may not have been known. */
20393 static void
20394 read_attribute_reprocess (const struct die_reader_specs *reader,
20395 struct attribute *attr, dwarf_tag tag)
20396 {
20397 struct dwarf2_cu *cu = reader->cu;
20398 switch (attr->form)
20399 {
20400 case DW_FORM_addrx:
20401 case DW_FORM_GNU_addr_index:
20402 attr->set_address (read_addr_index (cu,
20403 attr->as_unsigned_reprocess ()));
20404 break;
20405 case DW_FORM_loclistx:
20406 {
20407 sect_offset loclists_sect_off
20408 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20409
20410 attr->set_unsigned (to_underlying (loclists_sect_off));
20411 }
20412 break;
20413 case DW_FORM_rnglistx:
20414 {
20415 sect_offset rnglists_sect_off
20416 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20417
20418 attr->set_unsigned (to_underlying (rnglists_sect_off));
20419 }
20420 break;
20421 case DW_FORM_strx:
20422 case DW_FORM_strx1:
20423 case DW_FORM_strx2:
20424 case DW_FORM_strx3:
20425 case DW_FORM_strx4:
20426 case DW_FORM_GNU_str_index:
20427 {
20428 unsigned int str_index = attr->as_unsigned_reprocess ();
20429 gdb_assert (!attr->canonical_string_p ());
20430 if (reader->dwo_file != NULL)
20431 attr->set_string_noncanonical (read_dwo_str_index (reader,
20432 str_index));
20433 else
20434 attr->set_string_noncanonical (read_stub_str_index (cu,
20435 str_index));
20436 break;
20437 }
20438 default:
20439 gdb_assert_not_reached (_("Unexpected DWARF form."));
20440 }
20441 }
20442
20443 /* Read an attribute value described by an attribute form. */
20444
20445 static const gdb_byte *
20446 read_attribute_value (const struct die_reader_specs *reader,
20447 struct attribute *attr, unsigned form,
20448 LONGEST implicit_const, const gdb_byte *info_ptr)
20449 {
20450 struct dwarf2_cu *cu = reader->cu;
20451 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20452 struct objfile *objfile = per_objfile->objfile;
20453 bfd *abfd = reader->abfd;
20454 struct comp_unit_head *cu_header = &cu->header;
20455 unsigned int bytes_read;
20456 struct dwarf_block *blk;
20457
20458 attr->form = (enum dwarf_form) form;
20459 switch (form)
20460 {
20461 case DW_FORM_ref_addr:
20462 if (cu->header.version == 2)
20463 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20464 &bytes_read));
20465 else
20466 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20467 &bytes_read));
20468 info_ptr += bytes_read;
20469 break;
20470 case DW_FORM_GNU_ref_alt:
20471 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20472 &bytes_read));
20473 info_ptr += bytes_read;
20474 break;
20475 case DW_FORM_addr:
20476 {
20477 struct gdbarch *gdbarch = objfile->arch ();
20478 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20479 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20480 attr->set_address (addr);
20481 info_ptr += bytes_read;
20482 }
20483 break;
20484 case DW_FORM_block2:
20485 blk = dwarf_alloc_block (cu);
20486 blk->size = read_2_bytes (abfd, info_ptr);
20487 info_ptr += 2;
20488 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20489 info_ptr += blk->size;
20490 attr->set_block (blk);
20491 break;
20492 case DW_FORM_block4:
20493 blk = dwarf_alloc_block (cu);
20494 blk->size = read_4_bytes (abfd, info_ptr);
20495 info_ptr += 4;
20496 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20497 info_ptr += blk->size;
20498 attr->set_block (blk);
20499 break;
20500 case DW_FORM_data2:
20501 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20502 info_ptr += 2;
20503 break;
20504 case DW_FORM_data4:
20505 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20506 info_ptr += 4;
20507 break;
20508 case DW_FORM_data8:
20509 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20510 info_ptr += 8;
20511 break;
20512 case DW_FORM_data16:
20513 blk = dwarf_alloc_block (cu);
20514 blk->size = 16;
20515 blk->data = read_n_bytes (abfd, info_ptr, 16);
20516 info_ptr += 16;
20517 attr->set_block (blk);
20518 break;
20519 case DW_FORM_sec_offset:
20520 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20521 &bytes_read));
20522 info_ptr += bytes_read;
20523 break;
20524 case DW_FORM_loclistx:
20525 {
20526 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20527 &bytes_read));
20528 info_ptr += bytes_read;
20529 }
20530 break;
20531 case DW_FORM_string:
20532 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20533 &bytes_read));
20534 info_ptr += bytes_read;
20535 break;
20536 case DW_FORM_strp:
20537 if (!cu->per_cu->is_dwz)
20538 {
20539 attr->set_string_noncanonical
20540 (read_indirect_string (per_objfile,
20541 abfd, info_ptr, cu_header,
20542 &bytes_read));
20543 info_ptr += bytes_read;
20544 break;
20545 }
20546 /* FALLTHROUGH */
20547 case DW_FORM_line_strp:
20548 if (!cu->per_cu->is_dwz)
20549 {
20550 attr->set_string_noncanonical
20551 (per_objfile->read_line_string (info_ptr, cu_header,
20552 &bytes_read));
20553 info_ptr += bytes_read;
20554 break;
20555 }
20556 /* FALLTHROUGH */
20557 case DW_FORM_GNU_strp_alt:
20558 {
20559 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20560 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20561 &bytes_read);
20562
20563 attr->set_string_noncanonical
20564 (dwz->read_string (objfile, str_offset));
20565 info_ptr += bytes_read;
20566 }
20567 break;
20568 case DW_FORM_exprloc:
20569 case DW_FORM_block:
20570 blk = dwarf_alloc_block (cu);
20571 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20572 info_ptr += bytes_read;
20573 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20574 info_ptr += blk->size;
20575 attr->set_block (blk);
20576 break;
20577 case DW_FORM_block1:
20578 blk = dwarf_alloc_block (cu);
20579 blk->size = read_1_byte (abfd, info_ptr);
20580 info_ptr += 1;
20581 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20582 info_ptr += blk->size;
20583 attr->set_block (blk);
20584 break;
20585 case DW_FORM_data1:
20586 case DW_FORM_flag:
20587 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20588 info_ptr += 1;
20589 break;
20590 case DW_FORM_flag_present:
20591 attr->set_unsigned (1);
20592 break;
20593 case DW_FORM_sdata:
20594 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20595 info_ptr += bytes_read;
20596 break;
20597 case DW_FORM_rnglistx:
20598 {
20599 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20600 &bytes_read));
20601 info_ptr += bytes_read;
20602 }
20603 break;
20604 case DW_FORM_udata:
20605 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20606 info_ptr += bytes_read;
20607 break;
20608 case DW_FORM_ref1:
20609 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20610 + read_1_byte (abfd, info_ptr)));
20611 info_ptr += 1;
20612 break;
20613 case DW_FORM_ref2:
20614 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20615 + read_2_bytes (abfd, info_ptr)));
20616 info_ptr += 2;
20617 break;
20618 case DW_FORM_ref4:
20619 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20620 + read_4_bytes (abfd, info_ptr)));
20621 info_ptr += 4;
20622 break;
20623 case DW_FORM_ref8:
20624 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20625 + read_8_bytes (abfd, info_ptr)));
20626 info_ptr += 8;
20627 break;
20628 case DW_FORM_ref_sig8:
20629 attr->set_signature (read_8_bytes (abfd, info_ptr));
20630 info_ptr += 8;
20631 break;
20632 case DW_FORM_ref_udata:
20633 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20634 + read_unsigned_leb128 (abfd, info_ptr,
20635 &bytes_read)));
20636 info_ptr += bytes_read;
20637 break;
20638 case DW_FORM_indirect:
20639 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20640 info_ptr += bytes_read;
20641 if (form == DW_FORM_implicit_const)
20642 {
20643 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20644 info_ptr += bytes_read;
20645 }
20646 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20647 info_ptr);
20648 break;
20649 case DW_FORM_implicit_const:
20650 attr->set_signed (implicit_const);
20651 break;
20652 case DW_FORM_addrx:
20653 case DW_FORM_GNU_addr_index:
20654 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20655 &bytes_read));
20656 info_ptr += bytes_read;
20657 break;
20658 case DW_FORM_strx:
20659 case DW_FORM_strx1:
20660 case DW_FORM_strx2:
20661 case DW_FORM_strx3:
20662 case DW_FORM_strx4:
20663 case DW_FORM_GNU_str_index:
20664 {
20665 ULONGEST str_index;
20666 if (form == DW_FORM_strx1)
20667 {
20668 str_index = read_1_byte (abfd, info_ptr);
20669 info_ptr += 1;
20670 }
20671 else if (form == DW_FORM_strx2)
20672 {
20673 str_index = read_2_bytes (abfd, info_ptr);
20674 info_ptr += 2;
20675 }
20676 else if (form == DW_FORM_strx3)
20677 {
20678 str_index = read_3_bytes (abfd, info_ptr);
20679 info_ptr += 3;
20680 }
20681 else if (form == DW_FORM_strx4)
20682 {
20683 str_index = read_4_bytes (abfd, info_ptr);
20684 info_ptr += 4;
20685 }
20686 else
20687 {
20688 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20689 info_ptr += bytes_read;
20690 }
20691 attr->set_unsigned_reprocess (str_index);
20692 }
20693 break;
20694 default:
20695 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20696 dwarf_form_name (form),
20697 bfd_get_filename (abfd));
20698 }
20699
20700 /* Super hack. */
20701 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20702 attr->form = DW_FORM_GNU_ref_alt;
20703
20704 /* We have seen instances where the compiler tried to emit a byte
20705 size attribute of -1 which ended up being encoded as an unsigned
20706 0xffffffff. Although 0xffffffff is technically a valid size value,
20707 an object of this size seems pretty unlikely so we can relatively
20708 safely treat these cases as if the size attribute was invalid and
20709 treat them as zero by default. */
20710 if (attr->name == DW_AT_byte_size
20711 && form == DW_FORM_data4
20712 && attr->as_unsigned () >= 0xffffffff)
20713 {
20714 complaint
20715 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20716 hex_string (attr->as_unsigned ()));
20717 attr->set_unsigned (0);
20718 }
20719
20720 return info_ptr;
20721 }
20722
20723 /* Read an attribute described by an abbreviated attribute. */
20724
20725 static const gdb_byte *
20726 read_attribute (const struct die_reader_specs *reader,
20727 struct attribute *attr, struct attr_abbrev *abbrev,
20728 const gdb_byte *info_ptr)
20729 {
20730 attr->name = abbrev->name;
20731 attr->string_is_canonical = 0;
20732 attr->requires_reprocessing = 0;
20733 return read_attribute_value (reader, attr, abbrev->form,
20734 abbrev->implicit_const, info_ptr);
20735 }
20736
20737 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20738
20739 static const char *
20740 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20741 LONGEST str_offset)
20742 {
20743 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20744 str_offset, "DW_FORM_strp");
20745 }
20746
20747 /* Return pointer to string at .debug_str offset as read from BUF.
20748 BUF is assumed to be in a compilation unit described by CU_HEADER.
20749 Return *BYTES_READ_PTR count of bytes read from BUF. */
20750
20751 static const char *
20752 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20753 const gdb_byte *buf,
20754 const struct comp_unit_head *cu_header,
20755 unsigned int *bytes_read_ptr)
20756 {
20757 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20758
20759 return read_indirect_string_at_offset (per_objfile, str_offset);
20760 }
20761
20762 /* See read.h. */
20763
20764 const char *
20765 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20766 const struct comp_unit_head *cu_header,
20767 unsigned int *bytes_read_ptr)
20768 {
20769 bfd *abfd = objfile->obfd;
20770 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20771
20772 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20773 }
20774
20775 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20776 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20777 ADDR_SIZE is the size of addresses from the CU header. */
20778
20779 static CORE_ADDR
20780 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20781 gdb::optional<ULONGEST> addr_base, int addr_size)
20782 {
20783 struct objfile *objfile = per_objfile->objfile;
20784 bfd *abfd = objfile->obfd;
20785 const gdb_byte *info_ptr;
20786 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20787
20788 per_objfile->per_bfd->addr.read (objfile);
20789 if (per_objfile->per_bfd->addr.buffer == NULL)
20790 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20791 objfile_name (objfile));
20792 if (addr_base_or_zero + addr_index * addr_size
20793 >= per_objfile->per_bfd->addr.size)
20794 error (_("DW_FORM_addr_index pointing outside of "
20795 ".debug_addr section [in module %s]"),
20796 objfile_name (objfile));
20797 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20798 + addr_index * addr_size);
20799 if (addr_size == 4)
20800 return bfd_get_32 (abfd, info_ptr);
20801 else
20802 return bfd_get_64 (abfd, info_ptr);
20803 }
20804
20805 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20806
20807 static CORE_ADDR
20808 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20809 {
20810 return read_addr_index_1 (cu->per_objfile, addr_index,
20811 cu->addr_base, cu->header.addr_size);
20812 }
20813
20814 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20815
20816 static CORE_ADDR
20817 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20818 unsigned int *bytes_read)
20819 {
20820 bfd *abfd = cu->per_objfile->objfile->obfd;
20821 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20822
20823 return read_addr_index (cu, addr_index);
20824 }
20825
20826 /* See read.h. */
20827
20828 CORE_ADDR
20829 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20830 dwarf2_per_objfile *per_objfile,
20831 unsigned int addr_index)
20832 {
20833 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20834 gdb::optional<ULONGEST> addr_base;
20835 int addr_size;
20836
20837 /* We need addr_base and addr_size.
20838 If we don't have PER_CU->cu, we have to get it.
20839 Nasty, but the alternative is storing the needed info in PER_CU,
20840 which at this point doesn't seem justified: it's not clear how frequently
20841 it would get used and it would increase the size of every PER_CU.
20842 Entry points like dwarf2_per_cu_addr_size do a similar thing
20843 so we're not in uncharted territory here.
20844 Alas we need to be a bit more complicated as addr_base is contained
20845 in the DIE.
20846
20847 We don't need to read the entire CU(/TU).
20848 We just need the header and top level die.
20849
20850 IWBN to use the aging mechanism to let us lazily later discard the CU.
20851 For now we skip this optimization. */
20852
20853 if (cu != NULL)
20854 {
20855 addr_base = cu->addr_base;
20856 addr_size = cu->header.addr_size;
20857 }
20858 else
20859 {
20860 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20861 addr_base = reader.cu->addr_base;
20862 addr_size = reader.cu->header.addr_size;
20863 }
20864
20865 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20866 }
20867
20868 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20869 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20870 DWO file. */
20871
20872 static const char *
20873 read_str_index (struct dwarf2_cu *cu,
20874 struct dwarf2_section_info *str_section,
20875 struct dwarf2_section_info *str_offsets_section,
20876 ULONGEST str_offsets_base, ULONGEST str_index)
20877 {
20878 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20879 struct objfile *objfile = per_objfile->objfile;
20880 const char *objf_name = objfile_name (objfile);
20881 bfd *abfd = objfile->obfd;
20882 const gdb_byte *info_ptr;
20883 ULONGEST str_offset;
20884 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20885
20886 str_section->read (objfile);
20887 str_offsets_section->read (objfile);
20888 if (str_section->buffer == NULL)
20889 error (_("%s used without %s section"
20890 " in CU at offset %s [in module %s]"),
20891 form_name, str_section->get_name (),
20892 sect_offset_str (cu->header.sect_off), objf_name);
20893 if (str_offsets_section->buffer == NULL)
20894 error (_("%s used without %s section"
20895 " in CU at offset %s [in module %s]"),
20896 form_name, str_section->get_name (),
20897 sect_offset_str (cu->header.sect_off), objf_name);
20898 info_ptr = (str_offsets_section->buffer
20899 + str_offsets_base
20900 + str_index * cu->header.offset_size);
20901 if (cu->header.offset_size == 4)
20902 str_offset = bfd_get_32 (abfd, info_ptr);
20903 else
20904 str_offset = bfd_get_64 (abfd, info_ptr);
20905 if (str_offset >= str_section->size)
20906 error (_("Offset from %s pointing outside of"
20907 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20908 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20909 return (const char *) (str_section->buffer + str_offset);
20910 }
20911
20912 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20913
20914 static const char *
20915 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20916 {
20917 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20918 ? reader->cu->header.addr_size : 0;
20919 return read_str_index (reader->cu,
20920 &reader->dwo_file->sections.str,
20921 &reader->dwo_file->sections.str_offsets,
20922 str_offsets_base, str_index);
20923 }
20924
20925 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20926
20927 static const char *
20928 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20929 {
20930 struct objfile *objfile = cu->per_objfile->objfile;
20931 const char *objf_name = objfile_name (objfile);
20932 static const char form_name[] = "DW_FORM_GNU_str_index";
20933 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20934
20935 if (!cu->str_offsets_base.has_value ())
20936 error (_("%s used in Fission stub without %s"
20937 " in CU at offset 0x%lx [in module %s]"),
20938 form_name, str_offsets_attr_name,
20939 (long) cu->header.offset_size, objf_name);
20940
20941 return read_str_index (cu,
20942 &cu->per_objfile->per_bfd->str,
20943 &cu->per_objfile->per_bfd->str_offsets,
20944 *cu->str_offsets_base, str_index);
20945 }
20946
20947 /* Return the length of an LEB128 number in BUF. */
20948
20949 static int
20950 leb128_size (const gdb_byte *buf)
20951 {
20952 const gdb_byte *begin = buf;
20953 gdb_byte byte;
20954
20955 while (1)
20956 {
20957 byte = *buf++;
20958 if ((byte & 128) == 0)
20959 return buf - begin;
20960 }
20961 }
20962
20963 static void
20964 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20965 {
20966 switch (lang)
20967 {
20968 case DW_LANG_C89:
20969 case DW_LANG_C99:
20970 case DW_LANG_C11:
20971 case DW_LANG_C:
20972 case DW_LANG_UPC:
20973 cu->language = language_c;
20974 break;
20975 case DW_LANG_Java:
20976 case DW_LANG_C_plus_plus:
20977 case DW_LANG_C_plus_plus_11:
20978 case DW_LANG_C_plus_plus_14:
20979 cu->language = language_cplus;
20980 break;
20981 case DW_LANG_D:
20982 cu->language = language_d;
20983 break;
20984 case DW_LANG_Fortran77:
20985 case DW_LANG_Fortran90:
20986 case DW_LANG_Fortran95:
20987 case DW_LANG_Fortran03:
20988 case DW_LANG_Fortran08:
20989 cu->language = language_fortran;
20990 break;
20991 case DW_LANG_Go:
20992 cu->language = language_go;
20993 break;
20994 case DW_LANG_Mips_Assembler:
20995 cu->language = language_asm;
20996 break;
20997 case DW_LANG_Ada83:
20998 case DW_LANG_Ada95:
20999 cu->language = language_ada;
21000 break;
21001 case DW_LANG_Modula2:
21002 cu->language = language_m2;
21003 break;
21004 case DW_LANG_Pascal83:
21005 cu->language = language_pascal;
21006 break;
21007 case DW_LANG_ObjC:
21008 cu->language = language_objc;
21009 break;
21010 case DW_LANG_Rust:
21011 case DW_LANG_Rust_old:
21012 cu->language = language_rust;
21013 break;
21014 case DW_LANG_Cobol74:
21015 case DW_LANG_Cobol85:
21016 default:
21017 cu->language = language_minimal;
21018 break;
21019 }
21020 cu->language_defn = language_def (cu->language);
21021 }
21022
21023 /* Return the named attribute or NULL if not there. */
21024
21025 static struct attribute *
21026 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21027 {
21028 for (;;)
21029 {
21030 unsigned int i;
21031 struct attribute *spec = NULL;
21032
21033 for (i = 0; i < die->num_attrs; ++i)
21034 {
21035 if (die->attrs[i].name == name)
21036 return &die->attrs[i];
21037 if (die->attrs[i].name == DW_AT_specification
21038 || die->attrs[i].name == DW_AT_abstract_origin)
21039 spec = &die->attrs[i];
21040 }
21041
21042 if (!spec)
21043 break;
21044
21045 die = follow_die_ref (die, spec, &cu);
21046 }
21047
21048 return NULL;
21049 }
21050
21051 /* Return the string associated with a string-typed attribute, or NULL if it
21052 is either not found or is of an incorrect type. */
21053
21054 static const char *
21055 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21056 {
21057 struct attribute *attr;
21058 const char *str = NULL;
21059
21060 attr = dwarf2_attr (die, name, cu);
21061
21062 if (attr != NULL)
21063 {
21064 str = attr->as_string ();
21065 if (str == nullptr)
21066 complaint (_("string type expected for attribute %s for "
21067 "DIE at %s in module %s"),
21068 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21069 objfile_name (cu->per_objfile->objfile));
21070 }
21071
21072 return str;
21073 }
21074
21075 /* Return the dwo name or NULL if not present. If present, it is in either
21076 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21077 static const char *
21078 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21079 {
21080 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21081 if (dwo_name == nullptr)
21082 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21083 return dwo_name;
21084 }
21085
21086 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21087 and holds a non-zero value. This function should only be used for
21088 DW_FORM_flag or DW_FORM_flag_present attributes. */
21089
21090 static int
21091 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21092 {
21093 struct attribute *attr = dwarf2_attr (die, name, cu);
21094
21095 return attr != nullptr && attr->as_boolean ();
21096 }
21097
21098 static int
21099 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21100 {
21101 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21102 which value is non-zero. However, we have to be careful with
21103 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21104 (via dwarf2_flag_true_p) follows this attribute. So we may
21105 end up accidently finding a declaration attribute that belongs
21106 to a different DIE referenced by the specification attribute,
21107 even though the given DIE does not have a declaration attribute. */
21108 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21109 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21110 }
21111
21112 /* Return the die giving the specification for DIE, if there is
21113 one. *SPEC_CU is the CU containing DIE on input, and the CU
21114 containing the return value on output. If there is no
21115 specification, but there is an abstract origin, that is
21116 returned. */
21117
21118 static struct die_info *
21119 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21120 {
21121 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21122 *spec_cu);
21123
21124 if (spec_attr == NULL)
21125 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21126
21127 if (spec_attr == NULL)
21128 return NULL;
21129 else
21130 return follow_die_ref (die, spec_attr, spec_cu);
21131 }
21132
21133 /* Stub for free_line_header to match void * callback types. */
21134
21135 static void
21136 free_line_header_voidp (void *arg)
21137 {
21138 struct line_header *lh = (struct line_header *) arg;
21139
21140 delete lh;
21141 }
21142
21143 /* A convenience function to find the proper .debug_line section for a CU. */
21144
21145 static struct dwarf2_section_info *
21146 get_debug_line_section (struct dwarf2_cu *cu)
21147 {
21148 struct dwarf2_section_info *section;
21149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21150
21151 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21152 DWO file. */
21153 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21154 section = &cu->dwo_unit->dwo_file->sections.line;
21155 else if (cu->per_cu->is_dwz)
21156 {
21157 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
21158
21159 section = &dwz->line;
21160 }
21161 else
21162 section = &per_objfile->per_bfd->line;
21163
21164 return section;
21165 }
21166
21167 /* Read the statement program header starting at OFFSET in
21168 .debug_line, or .debug_line.dwo. Return a pointer
21169 to a struct line_header, allocated using xmalloc.
21170 Returns NULL if there is a problem reading the header, e.g., if it
21171 has a version we don't understand.
21172
21173 NOTE: the strings in the include directory and file name tables of
21174 the returned object point into the dwarf line section buffer,
21175 and must not be freed. */
21176
21177 static line_header_up
21178 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21179 {
21180 struct dwarf2_section_info *section;
21181 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21182
21183 section = get_debug_line_section (cu);
21184 section->read (per_objfile->objfile);
21185 if (section->buffer == NULL)
21186 {
21187 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21188 complaint (_("missing .debug_line.dwo section"));
21189 else
21190 complaint (_("missing .debug_line section"));
21191 return 0;
21192 }
21193
21194 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21195 per_objfile, section, &cu->header);
21196 }
21197
21198 /* Subroutine of dwarf_decode_lines to simplify it.
21199 Return the file name of the psymtab for the given file_entry.
21200 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21201 If space for the result is malloc'd, *NAME_HOLDER will be set.
21202 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21203
21204 static const char *
21205 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21206 const dwarf2_psymtab *pst,
21207 const char *comp_dir,
21208 gdb::unique_xmalloc_ptr<char> *name_holder)
21209 {
21210 const char *include_name = fe.name;
21211 const char *include_name_to_compare = include_name;
21212 const char *pst_filename;
21213 int file_is_pst;
21214
21215 const char *dir_name = fe.include_dir (lh);
21216
21217 gdb::unique_xmalloc_ptr<char> hold_compare;
21218 if (!IS_ABSOLUTE_PATH (include_name)
21219 && (dir_name != NULL || comp_dir != NULL))
21220 {
21221 /* Avoid creating a duplicate psymtab for PST.
21222 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21223 Before we do the comparison, however, we need to account
21224 for DIR_NAME and COMP_DIR.
21225 First prepend dir_name (if non-NULL). If we still don't
21226 have an absolute path prepend comp_dir (if non-NULL).
21227 However, the directory we record in the include-file's
21228 psymtab does not contain COMP_DIR (to match the
21229 corresponding symtab(s)).
21230
21231 Example:
21232
21233 bash$ cd /tmp
21234 bash$ gcc -g ./hello.c
21235 include_name = "hello.c"
21236 dir_name = "."
21237 DW_AT_comp_dir = comp_dir = "/tmp"
21238 DW_AT_name = "./hello.c"
21239
21240 */
21241
21242 if (dir_name != NULL)
21243 {
21244 name_holder->reset (concat (dir_name, SLASH_STRING,
21245 include_name, (char *) NULL));
21246 include_name = name_holder->get ();
21247 include_name_to_compare = include_name;
21248 }
21249 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21250 {
21251 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21252 include_name, (char *) NULL));
21253 include_name_to_compare = hold_compare.get ();
21254 }
21255 }
21256
21257 pst_filename = pst->filename;
21258 gdb::unique_xmalloc_ptr<char> copied_name;
21259 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21260 {
21261 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21262 pst_filename, (char *) NULL));
21263 pst_filename = copied_name.get ();
21264 }
21265
21266 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21267
21268 if (file_is_pst)
21269 return NULL;
21270 return include_name;
21271 }
21272
21273 /* State machine to track the state of the line number program. */
21274
21275 class lnp_state_machine
21276 {
21277 public:
21278 /* Initialize a machine state for the start of a line number
21279 program. */
21280 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21281 bool record_lines_p);
21282
21283 file_entry *current_file ()
21284 {
21285 /* lh->file_names is 0-based, but the file name numbers in the
21286 statement program are 1-based. */
21287 return m_line_header->file_name_at (m_file);
21288 }
21289
21290 /* Record the line in the state machine. END_SEQUENCE is true if
21291 we're processing the end of a sequence. */
21292 void record_line (bool end_sequence);
21293
21294 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21295 nop-out rest of the lines in this sequence. */
21296 void check_line_address (struct dwarf2_cu *cu,
21297 const gdb_byte *line_ptr,
21298 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21299
21300 void handle_set_discriminator (unsigned int discriminator)
21301 {
21302 m_discriminator = discriminator;
21303 m_line_has_non_zero_discriminator |= discriminator != 0;
21304 }
21305
21306 /* Handle DW_LNE_set_address. */
21307 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21308 {
21309 m_op_index = 0;
21310 address += baseaddr;
21311 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21312 }
21313
21314 /* Handle DW_LNS_advance_pc. */
21315 void handle_advance_pc (CORE_ADDR adjust);
21316
21317 /* Handle a special opcode. */
21318 void handle_special_opcode (unsigned char op_code);
21319
21320 /* Handle DW_LNS_advance_line. */
21321 void handle_advance_line (int line_delta)
21322 {
21323 advance_line (line_delta);
21324 }
21325
21326 /* Handle DW_LNS_set_file. */
21327 void handle_set_file (file_name_index file);
21328
21329 /* Handle DW_LNS_negate_stmt. */
21330 void handle_negate_stmt ()
21331 {
21332 m_is_stmt = !m_is_stmt;
21333 }
21334
21335 /* Handle DW_LNS_const_add_pc. */
21336 void handle_const_add_pc ();
21337
21338 /* Handle DW_LNS_fixed_advance_pc. */
21339 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21340 {
21341 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21342 m_op_index = 0;
21343 }
21344
21345 /* Handle DW_LNS_copy. */
21346 void handle_copy ()
21347 {
21348 record_line (false);
21349 m_discriminator = 0;
21350 }
21351
21352 /* Handle DW_LNE_end_sequence. */
21353 void handle_end_sequence ()
21354 {
21355 m_currently_recording_lines = true;
21356 }
21357
21358 private:
21359 /* Advance the line by LINE_DELTA. */
21360 void advance_line (int line_delta)
21361 {
21362 m_line += line_delta;
21363
21364 if (line_delta != 0)
21365 m_line_has_non_zero_discriminator = m_discriminator != 0;
21366 }
21367
21368 struct dwarf2_cu *m_cu;
21369
21370 gdbarch *m_gdbarch;
21371
21372 /* True if we're recording lines.
21373 Otherwise we're building partial symtabs and are just interested in
21374 finding include files mentioned by the line number program. */
21375 bool m_record_lines_p;
21376
21377 /* The line number header. */
21378 line_header *m_line_header;
21379
21380 /* These are part of the standard DWARF line number state machine,
21381 and initialized according to the DWARF spec. */
21382
21383 unsigned char m_op_index = 0;
21384 /* The line table index of the current file. */
21385 file_name_index m_file = 1;
21386 unsigned int m_line = 1;
21387
21388 /* These are initialized in the constructor. */
21389
21390 CORE_ADDR m_address;
21391 bool m_is_stmt;
21392 unsigned int m_discriminator;
21393
21394 /* Additional bits of state we need to track. */
21395
21396 /* The last file that we called dwarf2_start_subfile for.
21397 This is only used for TLLs. */
21398 unsigned int m_last_file = 0;
21399 /* The last file a line number was recorded for. */
21400 struct subfile *m_last_subfile = NULL;
21401
21402 /* The address of the last line entry. */
21403 CORE_ADDR m_last_address;
21404
21405 /* Set to true when a previous line at the same address (using
21406 m_last_address) had m_is_stmt true. This is reset to false when a
21407 line entry at a new address (m_address different to m_last_address) is
21408 processed. */
21409 bool m_stmt_at_address = false;
21410
21411 /* When true, record the lines we decode. */
21412 bool m_currently_recording_lines = false;
21413
21414 /* The last line number that was recorded, used to coalesce
21415 consecutive entries for the same line. This can happen, for
21416 example, when discriminators are present. PR 17276. */
21417 unsigned int m_last_line = 0;
21418 bool m_line_has_non_zero_discriminator = false;
21419 };
21420
21421 void
21422 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21423 {
21424 CORE_ADDR addr_adj = (((m_op_index + adjust)
21425 / m_line_header->maximum_ops_per_instruction)
21426 * m_line_header->minimum_instruction_length);
21427 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21428 m_op_index = ((m_op_index + adjust)
21429 % m_line_header->maximum_ops_per_instruction);
21430 }
21431
21432 void
21433 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21434 {
21435 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21436 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21437 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21438 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21439 / m_line_header->maximum_ops_per_instruction)
21440 * m_line_header->minimum_instruction_length);
21441 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21442 m_op_index = ((m_op_index + adj_opcode_d)
21443 % m_line_header->maximum_ops_per_instruction);
21444
21445 int line_delta = m_line_header->line_base + adj_opcode_r;
21446 advance_line (line_delta);
21447 record_line (false);
21448 m_discriminator = 0;
21449 }
21450
21451 void
21452 lnp_state_machine::handle_set_file (file_name_index file)
21453 {
21454 m_file = file;
21455
21456 const file_entry *fe = current_file ();
21457 if (fe == NULL)
21458 dwarf2_debug_line_missing_file_complaint ();
21459 else if (m_record_lines_p)
21460 {
21461 const char *dir = fe->include_dir (m_line_header);
21462
21463 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21464 m_line_has_non_zero_discriminator = m_discriminator != 0;
21465 dwarf2_start_subfile (m_cu, fe->name, dir);
21466 }
21467 }
21468
21469 void
21470 lnp_state_machine::handle_const_add_pc ()
21471 {
21472 CORE_ADDR adjust
21473 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21474
21475 CORE_ADDR addr_adj
21476 = (((m_op_index + adjust)
21477 / m_line_header->maximum_ops_per_instruction)
21478 * m_line_header->minimum_instruction_length);
21479
21480 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21481 m_op_index = ((m_op_index + adjust)
21482 % m_line_header->maximum_ops_per_instruction);
21483 }
21484
21485 /* Return non-zero if we should add LINE to the line number table.
21486 LINE is the line to add, LAST_LINE is the last line that was added,
21487 LAST_SUBFILE is the subfile for LAST_LINE.
21488 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21489 had a non-zero discriminator.
21490
21491 We have to be careful in the presence of discriminators.
21492 E.g., for this line:
21493
21494 for (i = 0; i < 100000; i++);
21495
21496 clang can emit four line number entries for that one line,
21497 each with a different discriminator.
21498 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21499
21500 However, we want gdb to coalesce all four entries into one.
21501 Otherwise the user could stepi into the middle of the line and
21502 gdb would get confused about whether the pc really was in the
21503 middle of the line.
21504
21505 Things are further complicated by the fact that two consecutive
21506 line number entries for the same line is a heuristic used by gcc
21507 to denote the end of the prologue. So we can't just discard duplicate
21508 entries, we have to be selective about it. The heuristic we use is
21509 that we only collapse consecutive entries for the same line if at least
21510 one of those entries has a non-zero discriminator. PR 17276.
21511
21512 Note: Addresses in the line number state machine can never go backwards
21513 within one sequence, thus this coalescing is ok. */
21514
21515 static int
21516 dwarf_record_line_p (struct dwarf2_cu *cu,
21517 unsigned int line, unsigned int last_line,
21518 int line_has_non_zero_discriminator,
21519 struct subfile *last_subfile)
21520 {
21521 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21522 return 1;
21523 if (line != last_line)
21524 return 1;
21525 /* Same line for the same file that we've seen already.
21526 As a last check, for pr 17276, only record the line if the line
21527 has never had a non-zero discriminator. */
21528 if (!line_has_non_zero_discriminator)
21529 return 1;
21530 return 0;
21531 }
21532
21533 /* Use the CU's builder to record line number LINE beginning at
21534 address ADDRESS in the line table of subfile SUBFILE. */
21535
21536 static void
21537 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21538 unsigned int line, CORE_ADDR address, bool is_stmt,
21539 struct dwarf2_cu *cu)
21540 {
21541 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21542
21543 if (dwarf_line_debug)
21544 {
21545 fprintf_unfiltered (gdb_stdlog,
21546 "Recording line %u, file %s, address %s\n",
21547 line, lbasename (subfile->name),
21548 paddress (gdbarch, address));
21549 }
21550
21551 if (cu != nullptr)
21552 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21553 }
21554
21555 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21556 Mark the end of a set of line number records.
21557 The arguments are the same as for dwarf_record_line_1.
21558 If SUBFILE is NULL the request is ignored. */
21559
21560 static void
21561 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21562 CORE_ADDR address, struct dwarf2_cu *cu)
21563 {
21564 if (subfile == NULL)
21565 return;
21566
21567 if (dwarf_line_debug)
21568 {
21569 fprintf_unfiltered (gdb_stdlog,
21570 "Finishing current line, file %s, address %s\n",
21571 lbasename (subfile->name),
21572 paddress (gdbarch, address));
21573 }
21574
21575 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21576 }
21577
21578 void
21579 lnp_state_machine::record_line (bool end_sequence)
21580 {
21581 if (dwarf_line_debug)
21582 {
21583 fprintf_unfiltered (gdb_stdlog,
21584 "Processing actual line %u: file %u,"
21585 " address %s, is_stmt %u, discrim %u%s\n",
21586 m_line, m_file,
21587 paddress (m_gdbarch, m_address),
21588 m_is_stmt, m_discriminator,
21589 (end_sequence ? "\t(end sequence)" : ""));
21590 }
21591
21592 file_entry *fe = current_file ();
21593
21594 if (fe == NULL)
21595 dwarf2_debug_line_missing_file_complaint ();
21596 /* For now we ignore lines not starting on an instruction boundary.
21597 But not when processing end_sequence for compatibility with the
21598 previous version of the code. */
21599 else if (m_op_index == 0 || end_sequence)
21600 {
21601 fe->included_p = 1;
21602 if (m_record_lines_p)
21603 {
21604 /* When we switch files we insert an end maker in the first file,
21605 switch to the second file and add a new line entry. The
21606 problem is that the end marker inserted in the first file will
21607 discard any previous line entries at the same address. If the
21608 line entries in the first file are marked as is-stmt, while
21609 the new line in the second file is non-stmt, then this means
21610 the end marker will discard is-stmt lines so we can have a
21611 non-stmt line. This means that there are less addresses at
21612 which the user can insert a breakpoint.
21613
21614 To improve this we track the last address in m_last_address,
21615 and whether we have seen an is-stmt at this address. Then
21616 when switching files, if we have seen a stmt at the current
21617 address, and we are switching to create a non-stmt line, then
21618 discard the new line. */
21619 bool file_changed
21620 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21621 bool ignore_this_line
21622 = ((file_changed && !end_sequence && m_last_address == m_address
21623 && !m_is_stmt && m_stmt_at_address)
21624 || (!end_sequence && m_line == 0));
21625
21626 if ((file_changed && !ignore_this_line) || end_sequence)
21627 {
21628 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21629 m_currently_recording_lines ? m_cu : nullptr);
21630 }
21631
21632 if (!end_sequence && !ignore_this_line)
21633 {
21634 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21635
21636 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21637 m_line_has_non_zero_discriminator,
21638 m_last_subfile))
21639 {
21640 buildsym_compunit *builder = m_cu->get_builder ();
21641 dwarf_record_line_1 (m_gdbarch,
21642 builder->get_current_subfile (),
21643 m_line, m_address, is_stmt,
21644 m_currently_recording_lines ? m_cu : nullptr);
21645 }
21646 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21647 m_last_line = m_line;
21648 }
21649 }
21650 }
21651
21652 /* Track whether we have seen any m_is_stmt true at m_address in case we
21653 have multiple line table entries all at m_address. */
21654 if (m_last_address != m_address)
21655 {
21656 m_stmt_at_address = false;
21657 m_last_address = m_address;
21658 }
21659 m_stmt_at_address |= m_is_stmt;
21660 }
21661
21662 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21663 line_header *lh, bool record_lines_p)
21664 {
21665 m_cu = cu;
21666 m_gdbarch = arch;
21667 m_record_lines_p = record_lines_p;
21668 m_line_header = lh;
21669
21670 m_currently_recording_lines = true;
21671
21672 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21673 was a line entry for it so that the backend has a chance to adjust it
21674 and also record it in case it needs it. This is currently used by MIPS
21675 code, cf. `mips_adjust_dwarf2_line'. */
21676 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21677 m_is_stmt = lh->default_is_stmt;
21678 m_discriminator = 0;
21679
21680 m_last_address = m_address;
21681 m_stmt_at_address = false;
21682 }
21683
21684 void
21685 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21686 const gdb_byte *line_ptr,
21687 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21688 {
21689 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21690 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21691 located at 0x0. In this case, additionally check that if
21692 ADDRESS < UNRELOCATED_LOWPC. */
21693
21694 if ((address == 0 && address < unrelocated_lowpc)
21695 || address == (CORE_ADDR) -1)
21696 {
21697 /* This line table is for a function which has been
21698 GCd by the linker. Ignore it. PR gdb/12528 */
21699
21700 struct objfile *objfile = cu->per_objfile->objfile;
21701 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21702
21703 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21704 line_offset, objfile_name (objfile));
21705 m_currently_recording_lines = false;
21706 /* Note: m_currently_recording_lines is left as false until we see
21707 DW_LNE_end_sequence. */
21708 }
21709 }
21710
21711 /* Subroutine of dwarf_decode_lines to simplify it.
21712 Process the line number information in LH.
21713 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21714 program in order to set included_p for every referenced header. */
21715
21716 static void
21717 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21718 const int decode_for_pst_p, CORE_ADDR lowpc)
21719 {
21720 const gdb_byte *line_ptr, *extended_end;
21721 const gdb_byte *line_end;
21722 unsigned int bytes_read, extended_len;
21723 unsigned char op_code, extended_op;
21724 CORE_ADDR baseaddr;
21725 struct objfile *objfile = cu->per_objfile->objfile;
21726 bfd *abfd = objfile->obfd;
21727 struct gdbarch *gdbarch = objfile->arch ();
21728 /* True if we're recording line info (as opposed to building partial
21729 symtabs and just interested in finding include files mentioned by
21730 the line number program). */
21731 bool record_lines_p = !decode_for_pst_p;
21732
21733 baseaddr = objfile->text_section_offset ();
21734
21735 line_ptr = lh->statement_program_start;
21736 line_end = lh->statement_program_end;
21737
21738 /* Read the statement sequences until there's nothing left. */
21739 while (line_ptr < line_end)
21740 {
21741 /* The DWARF line number program state machine. Reset the state
21742 machine at the start of each sequence. */
21743 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21744 bool end_sequence = false;
21745
21746 if (record_lines_p)
21747 {
21748 /* Start a subfile for the current file of the state
21749 machine. */
21750 const file_entry *fe = state_machine.current_file ();
21751
21752 if (fe != NULL)
21753 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21754 }
21755
21756 /* Decode the table. */
21757 while (line_ptr < line_end && !end_sequence)
21758 {
21759 op_code = read_1_byte (abfd, line_ptr);
21760 line_ptr += 1;
21761
21762 if (op_code >= lh->opcode_base)
21763 {
21764 /* Special opcode. */
21765 state_machine.handle_special_opcode (op_code);
21766 }
21767 else switch (op_code)
21768 {
21769 case DW_LNS_extended_op:
21770 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21771 &bytes_read);
21772 line_ptr += bytes_read;
21773 extended_end = line_ptr + extended_len;
21774 extended_op = read_1_byte (abfd, line_ptr);
21775 line_ptr += 1;
21776 if (DW_LNE_lo_user <= extended_op
21777 && extended_op <= DW_LNE_hi_user)
21778 {
21779 /* Vendor extension, ignore. */
21780 line_ptr = extended_end;
21781 break;
21782 }
21783 switch (extended_op)
21784 {
21785 case DW_LNE_end_sequence:
21786 state_machine.handle_end_sequence ();
21787 end_sequence = true;
21788 break;
21789 case DW_LNE_set_address:
21790 {
21791 CORE_ADDR address
21792 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21793 line_ptr += bytes_read;
21794
21795 state_machine.check_line_address (cu, line_ptr,
21796 lowpc - baseaddr, address);
21797 state_machine.handle_set_address (baseaddr, address);
21798 }
21799 break;
21800 case DW_LNE_define_file:
21801 {
21802 const char *cur_file;
21803 unsigned int mod_time, length;
21804 dir_index dindex;
21805
21806 cur_file = read_direct_string (abfd, line_ptr,
21807 &bytes_read);
21808 line_ptr += bytes_read;
21809 dindex = (dir_index)
21810 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21811 line_ptr += bytes_read;
21812 mod_time =
21813 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21814 line_ptr += bytes_read;
21815 length =
21816 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21817 line_ptr += bytes_read;
21818 lh->add_file_name (cur_file, dindex, mod_time, length);
21819 }
21820 break;
21821 case DW_LNE_set_discriminator:
21822 {
21823 /* The discriminator is not interesting to the
21824 debugger; just ignore it. We still need to
21825 check its value though:
21826 if there are consecutive entries for the same
21827 (non-prologue) line we want to coalesce them.
21828 PR 17276. */
21829 unsigned int discr
21830 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21831 line_ptr += bytes_read;
21832
21833 state_machine.handle_set_discriminator (discr);
21834 }
21835 break;
21836 default:
21837 complaint (_("mangled .debug_line section"));
21838 return;
21839 }
21840 /* Make sure that we parsed the extended op correctly. If e.g.
21841 we expected a different address size than the producer used,
21842 we may have read the wrong number of bytes. */
21843 if (line_ptr != extended_end)
21844 {
21845 complaint (_("mangled .debug_line section"));
21846 return;
21847 }
21848 break;
21849 case DW_LNS_copy:
21850 state_machine.handle_copy ();
21851 break;
21852 case DW_LNS_advance_pc:
21853 {
21854 CORE_ADDR adjust
21855 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21856 line_ptr += bytes_read;
21857
21858 state_machine.handle_advance_pc (adjust);
21859 }
21860 break;
21861 case DW_LNS_advance_line:
21862 {
21863 int line_delta
21864 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21865 line_ptr += bytes_read;
21866
21867 state_machine.handle_advance_line (line_delta);
21868 }
21869 break;
21870 case DW_LNS_set_file:
21871 {
21872 file_name_index file
21873 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21874 &bytes_read);
21875 line_ptr += bytes_read;
21876
21877 state_machine.handle_set_file (file);
21878 }
21879 break;
21880 case DW_LNS_set_column:
21881 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21882 line_ptr += bytes_read;
21883 break;
21884 case DW_LNS_negate_stmt:
21885 state_machine.handle_negate_stmt ();
21886 break;
21887 case DW_LNS_set_basic_block:
21888 break;
21889 /* Add to the address register of the state machine the
21890 address increment value corresponding to special opcode
21891 255. I.e., this value is scaled by the minimum
21892 instruction length since special opcode 255 would have
21893 scaled the increment. */
21894 case DW_LNS_const_add_pc:
21895 state_machine.handle_const_add_pc ();
21896 break;
21897 case DW_LNS_fixed_advance_pc:
21898 {
21899 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21900 line_ptr += 2;
21901
21902 state_machine.handle_fixed_advance_pc (addr_adj);
21903 }
21904 break;
21905 default:
21906 {
21907 /* Unknown standard opcode, ignore it. */
21908 int i;
21909
21910 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21911 {
21912 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21913 line_ptr += bytes_read;
21914 }
21915 }
21916 }
21917 }
21918
21919 if (!end_sequence)
21920 dwarf2_debug_line_missing_end_sequence_complaint ();
21921
21922 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21923 in which case we still finish recording the last line). */
21924 state_machine.record_line (true);
21925 }
21926 }
21927
21928 /* Decode the Line Number Program (LNP) for the given line_header
21929 structure and CU. The actual information extracted and the type
21930 of structures created from the LNP depends on the value of PST.
21931
21932 1. If PST is NULL, then this procedure uses the data from the program
21933 to create all necessary symbol tables, and their linetables.
21934
21935 2. If PST is not NULL, this procedure reads the program to determine
21936 the list of files included by the unit represented by PST, and
21937 builds all the associated partial symbol tables.
21938
21939 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21940 It is used for relative paths in the line table.
21941 NOTE: When processing partial symtabs (pst != NULL),
21942 comp_dir == pst->dirname.
21943
21944 NOTE: It is important that psymtabs have the same file name (via strcmp)
21945 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21946 symtab we don't use it in the name of the psymtabs we create.
21947 E.g. expand_line_sal requires this when finding psymtabs to expand.
21948 A good testcase for this is mb-inline.exp.
21949
21950 LOWPC is the lowest address in CU (or 0 if not known).
21951
21952 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21953 for its PC<->lines mapping information. Otherwise only the filename
21954 table is read in. */
21955
21956 static void
21957 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21958 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21959 CORE_ADDR lowpc, int decode_mapping)
21960 {
21961 struct objfile *objfile = cu->per_objfile->objfile;
21962 const int decode_for_pst_p = (pst != NULL);
21963
21964 if (decode_mapping)
21965 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21966
21967 if (decode_for_pst_p)
21968 {
21969 /* Now that we're done scanning the Line Header Program, we can
21970 create the psymtab of each included file. */
21971 for (auto &file_entry : lh->file_names ())
21972 if (file_entry.included_p == 1)
21973 {
21974 gdb::unique_xmalloc_ptr<char> name_holder;
21975 const char *include_name =
21976 psymtab_include_file_name (lh, file_entry, pst,
21977 comp_dir, &name_holder);
21978 if (include_name != NULL)
21979 dwarf2_create_include_psymtab (include_name, pst, objfile);
21980 }
21981 }
21982 else
21983 {
21984 /* Make sure a symtab is created for every file, even files
21985 which contain only variables (i.e. no code with associated
21986 line numbers). */
21987 buildsym_compunit *builder = cu->get_builder ();
21988 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21989
21990 for (auto &fe : lh->file_names ())
21991 {
21992 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21993 if (builder->get_current_subfile ()->symtab == NULL)
21994 {
21995 builder->get_current_subfile ()->symtab
21996 = allocate_symtab (cust,
21997 builder->get_current_subfile ()->name);
21998 }
21999 fe.symtab = builder->get_current_subfile ()->symtab;
22000 }
22001 }
22002 }
22003
22004 /* Start a subfile for DWARF. FILENAME is the name of the file and
22005 DIRNAME the name of the source directory which contains FILENAME
22006 or NULL if not known.
22007 This routine tries to keep line numbers from identical absolute and
22008 relative file names in a common subfile.
22009
22010 Using the `list' example from the GDB testsuite, which resides in
22011 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
22012 of /srcdir/list0.c yields the following debugging information for list0.c:
22013
22014 DW_AT_name: /srcdir/list0.c
22015 DW_AT_comp_dir: /compdir
22016 files.files[0].name: list0.h
22017 files.files[0].dir: /srcdir
22018 files.files[1].name: list0.c
22019 files.files[1].dir: /srcdir
22020
22021 The line number information for list0.c has to end up in a single
22022 subfile, so that `break /srcdir/list0.c:1' works as expected.
22023 start_subfile will ensure that this happens provided that we pass the
22024 concatenation of files.files[1].dir and files.files[1].name as the
22025 subfile's name. */
22026
22027 static void
22028 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
22029 const char *dirname)
22030 {
22031 gdb::unique_xmalloc_ptr<char> copy;
22032
22033 /* In order not to lose the line information directory,
22034 we concatenate it to the filename when it makes sense.
22035 Note that the Dwarf3 standard says (speaking of filenames in line
22036 information): ``The directory index is ignored for file names
22037 that represent full path names''. Thus ignoring dirname in the
22038 `else' branch below isn't an issue. */
22039
22040 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
22041 {
22042 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22043 filename = copy.get ();
22044 }
22045
22046 cu->get_builder ()->start_subfile (filename);
22047 }
22048
22049 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22050 buildsym_compunit constructor. */
22051
22052 struct compunit_symtab *
22053 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22054 CORE_ADDR low_pc)
22055 {
22056 gdb_assert (m_builder == nullptr);
22057
22058 m_builder.reset (new struct buildsym_compunit
22059 (this->per_objfile->objfile,
22060 name, comp_dir, language, low_pc));
22061
22062 list_in_scope = get_builder ()->get_file_symbols ();
22063
22064 get_builder ()->record_debugformat ("DWARF 2");
22065 get_builder ()->record_producer (producer);
22066
22067 processing_has_namespace_info = false;
22068
22069 return get_builder ()->get_compunit_symtab ();
22070 }
22071
22072 static void
22073 var_decode_location (struct attribute *attr, struct symbol *sym,
22074 struct dwarf2_cu *cu)
22075 {
22076 struct objfile *objfile = cu->per_objfile->objfile;
22077 struct comp_unit_head *cu_header = &cu->header;
22078
22079 /* NOTE drow/2003-01-30: There used to be a comment and some special
22080 code here to turn a symbol with DW_AT_external and a
22081 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22082 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22083 with some versions of binutils) where shared libraries could have
22084 relocations against symbols in their debug information - the
22085 minimal symbol would have the right address, but the debug info
22086 would not. It's no longer necessary, because we will explicitly
22087 apply relocations when we read in the debug information now. */
22088
22089 /* A DW_AT_location attribute with no contents indicates that a
22090 variable has been optimized away. */
22091 if (attr->form_is_block () && attr->as_block ()->size == 0)
22092 {
22093 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22094 return;
22095 }
22096
22097 /* Handle one degenerate form of location expression specially, to
22098 preserve GDB's previous behavior when section offsets are
22099 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22100 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22101
22102 if (attr->form_is_block ())
22103 {
22104 struct dwarf_block *block = attr->as_block ();
22105
22106 if ((block->data[0] == DW_OP_addr
22107 && block->size == 1 + cu_header->addr_size)
22108 || ((block->data[0] == DW_OP_GNU_addr_index
22109 || block->data[0] == DW_OP_addrx)
22110 && (block->size
22111 == 1 + leb128_size (&block->data[1]))))
22112 {
22113 unsigned int dummy;
22114
22115 if (block->data[0] == DW_OP_addr)
22116 SET_SYMBOL_VALUE_ADDRESS
22117 (sym, cu->header.read_address (objfile->obfd,
22118 block->data + 1,
22119 &dummy));
22120 else
22121 SET_SYMBOL_VALUE_ADDRESS
22122 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22123 &dummy));
22124 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22125 fixup_symbol_section (sym, objfile);
22126 SET_SYMBOL_VALUE_ADDRESS
22127 (sym,
22128 SYMBOL_VALUE_ADDRESS (sym)
22129 + objfile->section_offsets[sym->section_index ()]);
22130 return;
22131 }
22132 }
22133
22134 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22135 expression evaluator, and use LOC_COMPUTED only when necessary
22136 (i.e. when the value of a register or memory location is
22137 referenced, or a thread-local block, etc.). Then again, it might
22138 not be worthwhile. I'm assuming that it isn't unless performance
22139 or memory numbers show me otherwise. */
22140
22141 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22142
22143 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22144 cu->has_loclist = true;
22145 }
22146
22147 /* Given a pointer to a DWARF information entry, figure out if we need
22148 to make a symbol table entry for it, and if so, create a new entry
22149 and return a pointer to it.
22150 If TYPE is NULL, determine symbol type from the die, otherwise
22151 used the passed type.
22152 If SPACE is not NULL, use it to hold the new symbol. If it is
22153 NULL, allocate a new symbol on the objfile's obstack. */
22154
22155 static struct symbol *
22156 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22157 struct symbol *space)
22158 {
22159 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22160 struct objfile *objfile = per_objfile->objfile;
22161 struct gdbarch *gdbarch = objfile->arch ();
22162 struct symbol *sym = NULL;
22163 const char *name;
22164 struct attribute *attr = NULL;
22165 struct attribute *attr2 = NULL;
22166 CORE_ADDR baseaddr;
22167 struct pending **list_to_add = NULL;
22168
22169 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22170
22171 baseaddr = objfile->text_section_offset ();
22172
22173 name = dwarf2_name (die, cu);
22174 if (name)
22175 {
22176 int suppress_add = 0;
22177
22178 if (space)
22179 sym = space;
22180 else
22181 sym = new (&objfile->objfile_obstack) symbol;
22182 OBJSTAT (objfile, n_syms++);
22183
22184 /* Cache this symbol's name and the name's demangled form (if any). */
22185 sym->set_language (cu->language, &objfile->objfile_obstack);
22186 /* Fortran does not have mangling standard and the mangling does differ
22187 between gfortran, iFort etc. */
22188 const char *physname
22189 = (cu->language == language_fortran
22190 ? dwarf2_full_name (name, die, cu)
22191 : dwarf2_physname (name, die, cu));
22192 const char *linkagename = dw2_linkage_name (die, cu);
22193
22194 if (linkagename == nullptr || cu->language == language_ada)
22195 sym->set_linkage_name (physname);
22196 else
22197 {
22198 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22199 sym->set_linkage_name (linkagename);
22200 }
22201
22202 /* Default assumptions.
22203 Use the passed type or decode it from the die. */
22204 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22205 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22206 if (type != NULL)
22207 SYMBOL_TYPE (sym) = type;
22208 else
22209 SYMBOL_TYPE (sym) = die_type (die, cu);
22210 attr = dwarf2_attr (die,
22211 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22212 cu);
22213 if (attr != nullptr)
22214 SYMBOL_LINE (sym) = attr->constant_value (0);
22215
22216 attr = dwarf2_attr (die,
22217 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22218 cu);
22219 if (attr != nullptr && attr->form_is_unsigned ())
22220 {
22221 file_name_index file_index
22222 = (file_name_index) attr->as_unsigned ();
22223 struct file_entry *fe;
22224
22225 if (cu->line_header != NULL)
22226 fe = cu->line_header->file_name_at (file_index);
22227 else
22228 fe = NULL;
22229
22230 if (fe == NULL)
22231 complaint (_("file index out of range"));
22232 else
22233 symbol_set_symtab (sym, fe->symtab);
22234 }
22235
22236 switch (die->tag)
22237 {
22238 case DW_TAG_label:
22239 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22240 if (attr != nullptr)
22241 {
22242 CORE_ADDR addr;
22243
22244 addr = attr->as_address ();
22245 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22246 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22247 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22248 }
22249 else
22250 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22251 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22252 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22253 add_symbol_to_list (sym, cu->list_in_scope);
22254 break;
22255 case DW_TAG_subprogram:
22256 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22257 finish_block. */
22258 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22259 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22260 if ((attr2 != nullptr && attr2->as_boolean ())
22261 || cu->language == language_ada
22262 || cu->language == language_fortran)
22263 {
22264 /* Subprograms marked external are stored as a global symbol.
22265 Ada and Fortran subprograms, whether marked external or
22266 not, are always stored as a global symbol, because we want
22267 to be able to access them globally. For instance, we want
22268 to be able to break on a nested subprogram without having
22269 to specify the context. */
22270 list_to_add = cu->get_builder ()->get_global_symbols ();
22271 }
22272 else
22273 {
22274 list_to_add = cu->list_in_scope;
22275 }
22276 break;
22277 case DW_TAG_inlined_subroutine:
22278 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22279 finish_block. */
22280 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22281 SYMBOL_INLINED (sym) = 1;
22282 list_to_add = cu->list_in_scope;
22283 break;
22284 case DW_TAG_template_value_param:
22285 suppress_add = 1;
22286 /* Fall through. */
22287 case DW_TAG_constant:
22288 case DW_TAG_variable:
22289 case DW_TAG_member:
22290 /* Compilation with minimal debug info may result in
22291 variables with missing type entries. Change the
22292 misleading `void' type to something sensible. */
22293 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22294 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22295
22296 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22297 /* In the case of DW_TAG_member, we should only be called for
22298 static const members. */
22299 if (die->tag == DW_TAG_member)
22300 {
22301 /* dwarf2_add_field uses die_is_declaration,
22302 so we do the same. */
22303 gdb_assert (die_is_declaration (die, cu));
22304 gdb_assert (attr);
22305 }
22306 if (attr != nullptr)
22307 {
22308 dwarf2_const_value (attr, sym, cu);
22309 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22310 if (!suppress_add)
22311 {
22312 if (attr2 != nullptr && attr2->as_boolean ())
22313 list_to_add = cu->get_builder ()->get_global_symbols ();
22314 else
22315 list_to_add = cu->list_in_scope;
22316 }
22317 break;
22318 }
22319 attr = dwarf2_attr (die, DW_AT_location, cu);
22320 if (attr != nullptr)
22321 {
22322 var_decode_location (attr, sym, cu);
22323 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22324
22325 /* Fortran explicitly imports any global symbols to the local
22326 scope by DW_TAG_common_block. */
22327 if (cu->language == language_fortran && die->parent
22328 && die->parent->tag == DW_TAG_common_block)
22329 attr2 = NULL;
22330
22331 if (SYMBOL_CLASS (sym) == LOC_STATIC
22332 && SYMBOL_VALUE_ADDRESS (sym) == 0
22333 && !per_objfile->per_bfd->has_section_at_zero)
22334 {
22335 /* When a static variable is eliminated by the linker,
22336 the corresponding debug information is not stripped
22337 out, but the variable address is set to null;
22338 do not add such variables into symbol table. */
22339 }
22340 else if (attr2 != nullptr && attr2->as_boolean ())
22341 {
22342 if (SYMBOL_CLASS (sym) == LOC_STATIC
22343 && (objfile->flags & OBJF_MAINLINE) == 0
22344 && per_objfile->per_bfd->can_copy)
22345 {
22346 /* A global static variable might be subject to
22347 copy relocation. We first check for a local
22348 minsym, though, because maybe the symbol was
22349 marked hidden, in which case this would not
22350 apply. */
22351 bound_minimal_symbol found
22352 = (lookup_minimal_symbol_linkage
22353 (sym->linkage_name (), objfile));
22354 if (found.minsym != nullptr)
22355 sym->maybe_copied = 1;
22356 }
22357
22358 /* A variable with DW_AT_external is never static,
22359 but it may be block-scoped. */
22360 list_to_add
22361 = ((cu->list_in_scope
22362 == cu->get_builder ()->get_file_symbols ())
22363 ? cu->get_builder ()->get_global_symbols ()
22364 : cu->list_in_scope);
22365 }
22366 else
22367 list_to_add = cu->list_in_scope;
22368 }
22369 else
22370 {
22371 /* We do not know the address of this symbol.
22372 If it is an external symbol and we have type information
22373 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22374 The address of the variable will then be determined from
22375 the minimal symbol table whenever the variable is
22376 referenced. */
22377 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22378
22379 /* Fortran explicitly imports any global symbols to the local
22380 scope by DW_TAG_common_block. */
22381 if (cu->language == language_fortran && die->parent
22382 && die->parent->tag == DW_TAG_common_block)
22383 {
22384 /* SYMBOL_CLASS doesn't matter here because
22385 read_common_block is going to reset it. */
22386 if (!suppress_add)
22387 list_to_add = cu->list_in_scope;
22388 }
22389 else if (attr2 != nullptr && attr2->as_boolean ()
22390 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22391 {
22392 /* A variable with DW_AT_external is never static, but it
22393 may be block-scoped. */
22394 list_to_add
22395 = ((cu->list_in_scope
22396 == cu->get_builder ()->get_file_symbols ())
22397 ? cu->get_builder ()->get_global_symbols ()
22398 : cu->list_in_scope);
22399
22400 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22401 }
22402 else if (!die_is_declaration (die, cu))
22403 {
22404 /* Use the default LOC_OPTIMIZED_OUT class. */
22405 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22406 if (!suppress_add)
22407 list_to_add = cu->list_in_scope;
22408 }
22409 }
22410 break;
22411 case DW_TAG_formal_parameter:
22412 {
22413 /* If we are inside a function, mark this as an argument. If
22414 not, we might be looking at an argument to an inlined function
22415 when we do not have enough information to show inlined frames;
22416 pretend it's a local variable in that case so that the user can
22417 still see it. */
22418 struct context_stack *curr
22419 = cu->get_builder ()->get_current_context_stack ();
22420 if (curr != nullptr && curr->name != nullptr)
22421 SYMBOL_IS_ARGUMENT (sym) = 1;
22422 attr = dwarf2_attr (die, DW_AT_location, cu);
22423 if (attr != nullptr)
22424 {
22425 var_decode_location (attr, sym, cu);
22426 }
22427 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22428 if (attr != nullptr)
22429 {
22430 dwarf2_const_value (attr, sym, cu);
22431 }
22432
22433 list_to_add = cu->list_in_scope;
22434 }
22435 break;
22436 case DW_TAG_unspecified_parameters:
22437 /* From varargs functions; gdb doesn't seem to have any
22438 interest in this information, so just ignore it for now.
22439 (FIXME?) */
22440 break;
22441 case DW_TAG_template_type_param:
22442 suppress_add = 1;
22443 /* Fall through. */
22444 case DW_TAG_class_type:
22445 case DW_TAG_interface_type:
22446 case DW_TAG_structure_type:
22447 case DW_TAG_union_type:
22448 case DW_TAG_set_type:
22449 case DW_TAG_enumeration_type:
22450 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22451 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22452
22453 {
22454 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22455 really ever be static objects: otherwise, if you try
22456 to, say, break of a class's method and you're in a file
22457 which doesn't mention that class, it won't work unless
22458 the check for all static symbols in lookup_symbol_aux
22459 saves you. See the OtherFileClass tests in
22460 gdb.c++/namespace.exp. */
22461
22462 if (!suppress_add)
22463 {
22464 buildsym_compunit *builder = cu->get_builder ();
22465 list_to_add
22466 = (cu->list_in_scope == builder->get_file_symbols ()
22467 && cu->language == language_cplus
22468 ? builder->get_global_symbols ()
22469 : cu->list_in_scope);
22470
22471 /* The semantics of C++ state that "struct foo {
22472 ... }" also defines a typedef for "foo". */
22473 if (cu->language == language_cplus
22474 || cu->language == language_ada
22475 || cu->language == language_d
22476 || cu->language == language_rust)
22477 {
22478 /* The symbol's name is already allocated along
22479 with this objfile, so we don't need to
22480 duplicate it for the type. */
22481 if (SYMBOL_TYPE (sym)->name () == 0)
22482 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22483 }
22484 }
22485 }
22486 break;
22487 case DW_TAG_typedef:
22488 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22489 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22490 list_to_add = cu->list_in_scope;
22491 break;
22492 case DW_TAG_array_type:
22493 case DW_TAG_base_type:
22494 case DW_TAG_subrange_type:
22495 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22496 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22497 list_to_add = cu->list_in_scope;
22498 break;
22499 case DW_TAG_enumerator:
22500 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22501 if (attr != nullptr)
22502 {
22503 dwarf2_const_value (attr, sym, cu);
22504 }
22505 {
22506 /* NOTE: carlton/2003-11-10: See comment above in the
22507 DW_TAG_class_type, etc. block. */
22508
22509 list_to_add
22510 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22511 && cu->language == language_cplus
22512 ? cu->get_builder ()->get_global_symbols ()
22513 : cu->list_in_scope);
22514 }
22515 break;
22516 case DW_TAG_imported_declaration:
22517 case DW_TAG_namespace:
22518 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22519 list_to_add = cu->get_builder ()->get_global_symbols ();
22520 break;
22521 case DW_TAG_module:
22522 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22523 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22524 list_to_add = cu->get_builder ()->get_global_symbols ();
22525 break;
22526 case DW_TAG_common_block:
22527 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22528 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22529 add_symbol_to_list (sym, cu->list_in_scope);
22530 break;
22531 default:
22532 /* Not a tag we recognize. Hopefully we aren't processing
22533 trash data, but since we must specifically ignore things
22534 we don't recognize, there is nothing else we should do at
22535 this point. */
22536 complaint (_("unsupported tag: '%s'"),
22537 dwarf_tag_name (die->tag));
22538 break;
22539 }
22540
22541 if (suppress_add)
22542 {
22543 sym->hash_next = objfile->template_symbols;
22544 objfile->template_symbols = sym;
22545 list_to_add = NULL;
22546 }
22547
22548 if (list_to_add != NULL)
22549 add_symbol_to_list (sym, list_to_add);
22550
22551 /* For the benefit of old versions of GCC, check for anonymous
22552 namespaces based on the demangled name. */
22553 if (!cu->processing_has_namespace_info
22554 && cu->language == language_cplus)
22555 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22556 }
22557 return (sym);
22558 }
22559
22560 /* Given an attr with a DW_FORM_dataN value in host byte order,
22561 zero-extend it as appropriate for the symbol's type. The DWARF
22562 standard (v4) is not entirely clear about the meaning of using
22563 DW_FORM_dataN for a constant with a signed type, where the type is
22564 wider than the data. The conclusion of a discussion on the DWARF
22565 list was that this is unspecified. We choose to always zero-extend
22566 because that is the interpretation long in use by GCC. */
22567
22568 static gdb_byte *
22569 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22570 struct dwarf2_cu *cu, LONGEST *value, int bits)
22571 {
22572 struct objfile *objfile = cu->per_objfile->objfile;
22573 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22574 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22575 LONGEST l = attr->constant_value (0);
22576
22577 if (bits < sizeof (*value) * 8)
22578 {
22579 l &= ((LONGEST) 1 << bits) - 1;
22580 *value = l;
22581 }
22582 else if (bits == sizeof (*value) * 8)
22583 *value = l;
22584 else
22585 {
22586 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22587 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22588 return bytes;
22589 }
22590
22591 return NULL;
22592 }
22593
22594 /* Read a constant value from an attribute. Either set *VALUE, or if
22595 the value does not fit in *VALUE, set *BYTES - either already
22596 allocated on the objfile obstack, or newly allocated on OBSTACK,
22597 or, set *BATON, if we translated the constant to a location
22598 expression. */
22599
22600 static void
22601 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22602 const char *name, struct obstack *obstack,
22603 struct dwarf2_cu *cu,
22604 LONGEST *value, const gdb_byte **bytes,
22605 struct dwarf2_locexpr_baton **baton)
22606 {
22607 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22608 struct objfile *objfile = per_objfile->objfile;
22609 struct comp_unit_head *cu_header = &cu->header;
22610 struct dwarf_block *blk;
22611 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22612 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22613
22614 *value = 0;
22615 *bytes = NULL;
22616 *baton = NULL;
22617
22618 switch (attr->form)
22619 {
22620 case DW_FORM_addr:
22621 case DW_FORM_addrx:
22622 case DW_FORM_GNU_addr_index:
22623 {
22624 gdb_byte *data;
22625
22626 if (TYPE_LENGTH (type) != cu_header->addr_size)
22627 dwarf2_const_value_length_mismatch_complaint (name,
22628 cu_header->addr_size,
22629 TYPE_LENGTH (type));
22630 /* Symbols of this form are reasonably rare, so we just
22631 piggyback on the existing location code rather than writing
22632 a new implementation of symbol_computed_ops. */
22633 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22634 (*baton)->per_objfile = per_objfile;
22635 (*baton)->per_cu = cu->per_cu;
22636 gdb_assert ((*baton)->per_cu);
22637
22638 (*baton)->size = 2 + cu_header->addr_size;
22639 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22640 (*baton)->data = data;
22641
22642 data[0] = DW_OP_addr;
22643 store_unsigned_integer (&data[1], cu_header->addr_size,
22644 byte_order, attr->as_address ());
22645 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22646 }
22647 break;
22648 case DW_FORM_string:
22649 case DW_FORM_strp:
22650 case DW_FORM_strx:
22651 case DW_FORM_GNU_str_index:
22652 case DW_FORM_GNU_strp_alt:
22653 /* The string is already allocated on the objfile obstack, point
22654 directly to it. */
22655 *bytes = (const gdb_byte *) attr->as_string ();
22656 break;
22657 case DW_FORM_block1:
22658 case DW_FORM_block2:
22659 case DW_FORM_block4:
22660 case DW_FORM_block:
22661 case DW_FORM_exprloc:
22662 case DW_FORM_data16:
22663 blk = attr->as_block ();
22664 if (TYPE_LENGTH (type) != blk->size)
22665 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22666 TYPE_LENGTH (type));
22667 *bytes = blk->data;
22668 break;
22669
22670 /* The DW_AT_const_value attributes are supposed to carry the
22671 symbol's value "represented as it would be on the target
22672 architecture." By the time we get here, it's already been
22673 converted to host endianness, so we just need to sign- or
22674 zero-extend it as appropriate. */
22675 case DW_FORM_data1:
22676 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22677 break;
22678 case DW_FORM_data2:
22679 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22680 break;
22681 case DW_FORM_data4:
22682 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22683 break;
22684 case DW_FORM_data8:
22685 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22686 break;
22687
22688 case DW_FORM_sdata:
22689 case DW_FORM_implicit_const:
22690 *value = attr->as_signed ();
22691 break;
22692
22693 case DW_FORM_udata:
22694 *value = attr->as_unsigned ();
22695 break;
22696
22697 default:
22698 complaint (_("unsupported const value attribute form: '%s'"),
22699 dwarf_form_name (attr->form));
22700 *value = 0;
22701 break;
22702 }
22703 }
22704
22705
22706 /* Copy constant value from an attribute to a symbol. */
22707
22708 static void
22709 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22710 struct dwarf2_cu *cu)
22711 {
22712 struct objfile *objfile = cu->per_objfile->objfile;
22713 LONGEST value;
22714 const gdb_byte *bytes;
22715 struct dwarf2_locexpr_baton *baton;
22716
22717 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22718 sym->print_name (),
22719 &objfile->objfile_obstack, cu,
22720 &value, &bytes, &baton);
22721
22722 if (baton != NULL)
22723 {
22724 SYMBOL_LOCATION_BATON (sym) = baton;
22725 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22726 }
22727 else if (bytes != NULL)
22728 {
22729 SYMBOL_VALUE_BYTES (sym) = bytes;
22730 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22731 }
22732 else
22733 {
22734 SYMBOL_VALUE (sym) = value;
22735 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22736 }
22737 }
22738
22739 /* Return the type of the die in question using its DW_AT_type attribute. */
22740
22741 static struct type *
22742 die_type (struct die_info *die, struct dwarf2_cu *cu)
22743 {
22744 struct attribute *type_attr;
22745
22746 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22747 if (!type_attr)
22748 {
22749 struct objfile *objfile = cu->per_objfile->objfile;
22750 /* A missing DW_AT_type represents a void type. */
22751 return objfile_type (objfile)->builtin_void;
22752 }
22753
22754 return lookup_die_type (die, type_attr, cu);
22755 }
22756
22757 /* True iff CU's producer generates GNAT Ada auxiliary information
22758 that allows to find parallel types through that information instead
22759 of having to do expensive parallel lookups by type name. */
22760
22761 static int
22762 need_gnat_info (struct dwarf2_cu *cu)
22763 {
22764 /* Assume that the Ada compiler was GNAT, which always produces
22765 the auxiliary information. */
22766 return (cu->language == language_ada);
22767 }
22768
22769 /* Return the auxiliary type of the die in question using its
22770 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22771 attribute is not present. */
22772
22773 static struct type *
22774 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22775 {
22776 struct attribute *type_attr;
22777
22778 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22779 if (!type_attr)
22780 return NULL;
22781
22782 return lookup_die_type (die, type_attr, cu);
22783 }
22784
22785 /* If DIE has a descriptive_type attribute, then set the TYPE's
22786 descriptive type accordingly. */
22787
22788 static void
22789 set_descriptive_type (struct type *type, struct die_info *die,
22790 struct dwarf2_cu *cu)
22791 {
22792 struct type *descriptive_type = die_descriptive_type (die, cu);
22793
22794 if (descriptive_type)
22795 {
22796 ALLOCATE_GNAT_AUX_TYPE (type);
22797 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22798 }
22799 }
22800
22801 /* Return the containing type of the die in question using its
22802 DW_AT_containing_type attribute. */
22803
22804 static struct type *
22805 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22806 {
22807 struct attribute *type_attr;
22808 struct objfile *objfile = cu->per_objfile->objfile;
22809
22810 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22811 if (!type_attr)
22812 error (_("Dwarf Error: Problem turning containing type into gdb type "
22813 "[in module %s]"), objfile_name (objfile));
22814
22815 return lookup_die_type (die, type_attr, cu);
22816 }
22817
22818 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22819
22820 static struct type *
22821 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22822 {
22823 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22824 struct objfile *objfile = per_objfile->objfile;
22825 char *saved;
22826
22827 std::string message
22828 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22829 objfile_name (objfile),
22830 sect_offset_str (cu->header.sect_off),
22831 sect_offset_str (die->sect_off));
22832 saved = obstack_strdup (&objfile->objfile_obstack, message);
22833
22834 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22835 }
22836
22837 /* Look up the type of DIE in CU using its type attribute ATTR.
22838 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22839 DW_AT_containing_type.
22840 If there is no type substitute an error marker. */
22841
22842 static struct type *
22843 lookup_die_type (struct die_info *die, const struct attribute *attr,
22844 struct dwarf2_cu *cu)
22845 {
22846 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22847 struct objfile *objfile = per_objfile->objfile;
22848 struct type *this_type;
22849
22850 gdb_assert (attr->name == DW_AT_type
22851 || attr->name == DW_AT_GNAT_descriptive_type
22852 || attr->name == DW_AT_containing_type);
22853
22854 /* First see if we have it cached. */
22855
22856 if (attr->form == DW_FORM_GNU_ref_alt)
22857 {
22858 struct dwarf2_per_cu_data *per_cu;
22859 sect_offset sect_off = attr->get_ref_die_offset ();
22860
22861 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22862 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22863 }
22864 else if (attr->form_is_ref ())
22865 {
22866 sect_offset sect_off = attr->get_ref_die_offset ();
22867
22868 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22869 }
22870 else if (attr->form == DW_FORM_ref_sig8)
22871 {
22872 ULONGEST signature = attr->as_signature ();
22873
22874 return get_signatured_type (die, signature, cu);
22875 }
22876 else
22877 {
22878 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22879 " at %s [in module %s]"),
22880 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22881 objfile_name (objfile));
22882 return build_error_marker_type (cu, die);
22883 }
22884
22885 /* If not cached we need to read it in. */
22886
22887 if (this_type == NULL)
22888 {
22889 struct die_info *type_die = NULL;
22890 struct dwarf2_cu *type_cu = cu;
22891
22892 if (attr->form_is_ref ())
22893 type_die = follow_die_ref (die, attr, &type_cu);
22894 if (type_die == NULL)
22895 return build_error_marker_type (cu, die);
22896 /* If we find the type now, it's probably because the type came
22897 from an inter-CU reference and the type's CU got expanded before
22898 ours. */
22899 this_type = read_type_die (type_die, type_cu);
22900 }
22901
22902 /* If we still don't have a type use an error marker. */
22903
22904 if (this_type == NULL)
22905 return build_error_marker_type (cu, die);
22906
22907 return this_type;
22908 }
22909
22910 /* Return the type in DIE, CU.
22911 Returns NULL for invalid types.
22912
22913 This first does a lookup in die_type_hash,
22914 and only reads the die in if necessary.
22915
22916 NOTE: This can be called when reading in partial or full symbols. */
22917
22918 static struct type *
22919 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22920 {
22921 struct type *this_type;
22922
22923 this_type = get_die_type (die, cu);
22924 if (this_type)
22925 return this_type;
22926
22927 return read_type_die_1 (die, cu);
22928 }
22929
22930 /* Read the type in DIE, CU.
22931 Returns NULL for invalid types. */
22932
22933 static struct type *
22934 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22935 {
22936 struct type *this_type = NULL;
22937
22938 switch (die->tag)
22939 {
22940 case DW_TAG_class_type:
22941 case DW_TAG_interface_type:
22942 case DW_TAG_structure_type:
22943 case DW_TAG_union_type:
22944 this_type = read_structure_type (die, cu);
22945 break;
22946 case DW_TAG_enumeration_type:
22947 this_type = read_enumeration_type (die, cu);
22948 break;
22949 case DW_TAG_subprogram:
22950 case DW_TAG_subroutine_type:
22951 case DW_TAG_inlined_subroutine:
22952 this_type = read_subroutine_type (die, cu);
22953 break;
22954 case DW_TAG_array_type:
22955 this_type = read_array_type (die, cu);
22956 break;
22957 case DW_TAG_set_type:
22958 this_type = read_set_type (die, cu);
22959 break;
22960 case DW_TAG_pointer_type:
22961 this_type = read_tag_pointer_type (die, cu);
22962 break;
22963 case DW_TAG_ptr_to_member_type:
22964 this_type = read_tag_ptr_to_member_type (die, cu);
22965 break;
22966 case DW_TAG_reference_type:
22967 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22968 break;
22969 case DW_TAG_rvalue_reference_type:
22970 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22971 break;
22972 case DW_TAG_const_type:
22973 this_type = read_tag_const_type (die, cu);
22974 break;
22975 case DW_TAG_volatile_type:
22976 this_type = read_tag_volatile_type (die, cu);
22977 break;
22978 case DW_TAG_restrict_type:
22979 this_type = read_tag_restrict_type (die, cu);
22980 break;
22981 case DW_TAG_string_type:
22982 this_type = read_tag_string_type (die, cu);
22983 break;
22984 case DW_TAG_typedef:
22985 this_type = read_typedef (die, cu);
22986 break;
22987 case DW_TAG_subrange_type:
22988 this_type = read_subrange_type (die, cu);
22989 break;
22990 case DW_TAG_base_type:
22991 this_type = read_base_type (die, cu);
22992 break;
22993 case DW_TAG_unspecified_type:
22994 this_type = read_unspecified_type (die, cu);
22995 break;
22996 case DW_TAG_namespace:
22997 this_type = read_namespace_type (die, cu);
22998 break;
22999 case DW_TAG_module:
23000 this_type = read_module_type (die, cu);
23001 break;
23002 case DW_TAG_atomic_type:
23003 this_type = read_tag_atomic_type (die, cu);
23004 break;
23005 default:
23006 complaint (_("unexpected tag in read_type_die: '%s'"),
23007 dwarf_tag_name (die->tag));
23008 break;
23009 }
23010
23011 return this_type;
23012 }
23013
23014 /* See if we can figure out if the class lives in a namespace. We do
23015 this by looking for a member function; its demangled name will
23016 contain namespace info, if there is any.
23017 Return the computed name or NULL.
23018 Space for the result is allocated on the objfile's obstack.
23019 This is the full-die version of guess_partial_die_structure_name.
23020 In this case we know DIE has no useful parent. */
23021
23022 static const char *
23023 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
23024 {
23025 struct die_info *spec_die;
23026 struct dwarf2_cu *spec_cu;
23027 struct die_info *child;
23028 struct objfile *objfile = cu->per_objfile->objfile;
23029
23030 spec_cu = cu;
23031 spec_die = die_specification (die, &spec_cu);
23032 if (spec_die != NULL)
23033 {
23034 die = spec_die;
23035 cu = spec_cu;
23036 }
23037
23038 for (child = die->child;
23039 child != NULL;
23040 child = child->sibling)
23041 {
23042 if (child->tag == DW_TAG_subprogram)
23043 {
23044 const char *linkage_name = dw2_linkage_name (child, cu);
23045
23046 if (linkage_name != NULL)
23047 {
23048 gdb::unique_xmalloc_ptr<char> actual_name
23049 (cu->language_defn->class_name_from_physname (linkage_name));
23050 const char *name = NULL;
23051
23052 if (actual_name != NULL)
23053 {
23054 const char *die_name = dwarf2_name (die, cu);
23055
23056 if (die_name != NULL
23057 && strcmp (die_name, actual_name.get ()) != 0)
23058 {
23059 /* Strip off the class name from the full name.
23060 We want the prefix. */
23061 int die_name_len = strlen (die_name);
23062 int actual_name_len = strlen (actual_name.get ());
23063 const char *ptr = actual_name.get ();
23064
23065 /* Test for '::' as a sanity check. */
23066 if (actual_name_len > die_name_len + 2
23067 && ptr[actual_name_len - die_name_len - 1] == ':')
23068 name = obstack_strndup (
23069 &objfile->per_bfd->storage_obstack,
23070 ptr, actual_name_len - die_name_len - 2);
23071 }
23072 }
23073 return name;
23074 }
23075 }
23076 }
23077
23078 return NULL;
23079 }
23080
23081 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23082 prefix part in such case. See
23083 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23084
23085 static const char *
23086 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23087 {
23088 struct attribute *attr;
23089 const char *base;
23090
23091 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23092 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23093 return NULL;
23094
23095 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23096 return NULL;
23097
23098 attr = dw2_linkage_name_attr (die, cu);
23099 const char *attr_name = attr->as_string ();
23100 if (attr == NULL || attr_name == NULL)
23101 return NULL;
23102
23103 /* dwarf2_name had to be already called. */
23104 gdb_assert (attr->canonical_string_p ());
23105
23106 /* Strip the base name, keep any leading namespaces/classes. */
23107 base = strrchr (attr_name, ':');
23108 if (base == NULL || base == attr_name || base[-1] != ':')
23109 return "";
23110
23111 struct objfile *objfile = cu->per_objfile->objfile;
23112 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23113 attr_name,
23114 &base[-1] - attr_name);
23115 }
23116
23117 /* Return the name of the namespace/class that DIE is defined within,
23118 or "" if we can't tell. The caller should not xfree the result.
23119
23120 For example, if we're within the method foo() in the following
23121 code:
23122
23123 namespace N {
23124 class C {
23125 void foo () {
23126 }
23127 };
23128 }
23129
23130 then determine_prefix on foo's die will return "N::C". */
23131
23132 static const char *
23133 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23134 {
23135 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23136 struct die_info *parent, *spec_die;
23137 struct dwarf2_cu *spec_cu;
23138 struct type *parent_type;
23139 const char *retval;
23140
23141 if (cu->language != language_cplus
23142 && cu->language != language_fortran && cu->language != language_d
23143 && cu->language != language_rust)
23144 return "";
23145
23146 retval = anonymous_struct_prefix (die, cu);
23147 if (retval)
23148 return retval;
23149
23150 /* We have to be careful in the presence of DW_AT_specification.
23151 For example, with GCC 3.4, given the code
23152
23153 namespace N {
23154 void foo() {
23155 // Definition of N::foo.
23156 }
23157 }
23158
23159 then we'll have a tree of DIEs like this:
23160
23161 1: DW_TAG_compile_unit
23162 2: DW_TAG_namespace // N
23163 3: DW_TAG_subprogram // declaration of N::foo
23164 4: DW_TAG_subprogram // definition of N::foo
23165 DW_AT_specification // refers to die #3
23166
23167 Thus, when processing die #4, we have to pretend that we're in
23168 the context of its DW_AT_specification, namely the contex of die
23169 #3. */
23170 spec_cu = cu;
23171 spec_die = die_specification (die, &spec_cu);
23172 if (spec_die == NULL)
23173 parent = die->parent;
23174 else
23175 {
23176 parent = spec_die->parent;
23177 cu = spec_cu;
23178 }
23179
23180 if (parent == NULL)
23181 return "";
23182 else if (parent->building_fullname)
23183 {
23184 const char *name;
23185 const char *parent_name;
23186
23187 /* It has been seen on RealView 2.2 built binaries,
23188 DW_TAG_template_type_param types actually _defined_ as
23189 children of the parent class:
23190
23191 enum E {};
23192 template class <class Enum> Class{};
23193 Class<enum E> class_e;
23194
23195 1: DW_TAG_class_type (Class)
23196 2: DW_TAG_enumeration_type (E)
23197 3: DW_TAG_enumerator (enum1:0)
23198 3: DW_TAG_enumerator (enum2:1)
23199 ...
23200 2: DW_TAG_template_type_param
23201 DW_AT_type DW_FORM_ref_udata (E)
23202
23203 Besides being broken debug info, it can put GDB into an
23204 infinite loop. Consider:
23205
23206 When we're building the full name for Class<E>, we'll start
23207 at Class, and go look over its template type parameters,
23208 finding E. We'll then try to build the full name of E, and
23209 reach here. We're now trying to build the full name of E,
23210 and look over the parent DIE for containing scope. In the
23211 broken case, if we followed the parent DIE of E, we'd again
23212 find Class, and once again go look at its template type
23213 arguments, etc., etc. Simply don't consider such parent die
23214 as source-level parent of this die (it can't be, the language
23215 doesn't allow it), and break the loop here. */
23216 name = dwarf2_name (die, cu);
23217 parent_name = dwarf2_name (parent, cu);
23218 complaint (_("template param type '%s' defined within parent '%s'"),
23219 name ? name : "<unknown>",
23220 parent_name ? parent_name : "<unknown>");
23221 return "";
23222 }
23223 else
23224 switch (parent->tag)
23225 {
23226 case DW_TAG_namespace:
23227 parent_type = read_type_die (parent, cu);
23228 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23229 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23230 Work around this problem here. */
23231 if (cu->language == language_cplus
23232 && strcmp (parent_type->name (), "::") == 0)
23233 return "";
23234 /* We give a name to even anonymous namespaces. */
23235 return parent_type->name ();
23236 case DW_TAG_class_type:
23237 case DW_TAG_interface_type:
23238 case DW_TAG_structure_type:
23239 case DW_TAG_union_type:
23240 case DW_TAG_module:
23241 parent_type = read_type_die (parent, cu);
23242 if (parent_type->name () != NULL)
23243 return parent_type->name ();
23244 else
23245 /* An anonymous structure is only allowed non-static data
23246 members; no typedefs, no member functions, et cetera.
23247 So it does not need a prefix. */
23248 return "";
23249 case DW_TAG_compile_unit:
23250 case DW_TAG_partial_unit:
23251 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23252 if (cu->language == language_cplus
23253 && !per_objfile->per_bfd->types.empty ()
23254 && die->child != NULL
23255 && (die->tag == DW_TAG_class_type
23256 || die->tag == DW_TAG_structure_type
23257 || die->tag == DW_TAG_union_type))
23258 {
23259 const char *name = guess_full_die_structure_name (die, cu);
23260 if (name != NULL)
23261 return name;
23262 }
23263 return "";
23264 case DW_TAG_subprogram:
23265 /* Nested subroutines in Fortran get a prefix with the name
23266 of the parent's subroutine. */
23267 if (cu->language == language_fortran)
23268 {
23269 if ((die->tag == DW_TAG_subprogram)
23270 && (dwarf2_name (parent, cu) != NULL))
23271 return dwarf2_name (parent, cu);
23272 }
23273 return determine_prefix (parent, cu);
23274 case DW_TAG_enumeration_type:
23275 parent_type = read_type_die (parent, cu);
23276 if (TYPE_DECLARED_CLASS (parent_type))
23277 {
23278 if (parent_type->name () != NULL)
23279 return parent_type->name ();
23280 return "";
23281 }
23282 /* Fall through. */
23283 default:
23284 return determine_prefix (parent, cu);
23285 }
23286 }
23287
23288 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23289 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23290 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23291 an obconcat, otherwise allocate storage for the result. The CU argument is
23292 used to determine the language and hence, the appropriate separator. */
23293
23294 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23295
23296 static char *
23297 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23298 int physname, struct dwarf2_cu *cu)
23299 {
23300 const char *lead = "";
23301 const char *sep;
23302
23303 if (suffix == NULL || suffix[0] == '\0'
23304 || prefix == NULL || prefix[0] == '\0')
23305 sep = "";
23306 else if (cu->language == language_d)
23307 {
23308 /* For D, the 'main' function could be defined in any module, but it
23309 should never be prefixed. */
23310 if (strcmp (suffix, "D main") == 0)
23311 {
23312 prefix = "";
23313 sep = "";
23314 }
23315 else
23316 sep = ".";
23317 }
23318 else if (cu->language == language_fortran && physname)
23319 {
23320 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23321 DW_AT_MIPS_linkage_name is preferred and used instead. */
23322
23323 lead = "__";
23324 sep = "_MOD_";
23325 }
23326 else
23327 sep = "::";
23328
23329 if (prefix == NULL)
23330 prefix = "";
23331 if (suffix == NULL)
23332 suffix = "";
23333
23334 if (obs == NULL)
23335 {
23336 char *retval
23337 = ((char *)
23338 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23339
23340 strcpy (retval, lead);
23341 strcat (retval, prefix);
23342 strcat (retval, sep);
23343 strcat (retval, suffix);
23344 return retval;
23345 }
23346 else
23347 {
23348 /* We have an obstack. */
23349 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23350 }
23351 }
23352
23353 /* Get name of a die, return NULL if not found. */
23354
23355 static const char *
23356 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23357 struct objfile *objfile)
23358 {
23359 if (name && cu->language == language_cplus)
23360 {
23361 gdb::unique_xmalloc_ptr<char> canon_name
23362 = cp_canonicalize_string (name);
23363
23364 if (canon_name != nullptr)
23365 name = objfile->intern (canon_name.get ());
23366 }
23367
23368 return name;
23369 }
23370
23371 /* Get name of a die, return NULL if not found.
23372 Anonymous namespaces are converted to their magic string. */
23373
23374 static const char *
23375 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23376 {
23377 struct attribute *attr;
23378 struct objfile *objfile = cu->per_objfile->objfile;
23379
23380 attr = dwarf2_attr (die, DW_AT_name, cu);
23381 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23382 if (attr_name == nullptr
23383 && die->tag != DW_TAG_namespace
23384 && die->tag != DW_TAG_class_type
23385 && die->tag != DW_TAG_interface_type
23386 && die->tag != DW_TAG_structure_type
23387 && die->tag != DW_TAG_union_type)
23388 return NULL;
23389
23390 switch (die->tag)
23391 {
23392 case DW_TAG_compile_unit:
23393 case DW_TAG_partial_unit:
23394 /* Compilation units have a DW_AT_name that is a filename, not
23395 a source language identifier. */
23396 case DW_TAG_enumeration_type:
23397 case DW_TAG_enumerator:
23398 /* These tags always have simple identifiers already; no need
23399 to canonicalize them. */
23400 return attr_name;
23401
23402 case DW_TAG_namespace:
23403 if (attr_name != nullptr)
23404 return attr_name;
23405 return CP_ANONYMOUS_NAMESPACE_STR;
23406
23407 case DW_TAG_class_type:
23408 case DW_TAG_interface_type:
23409 case DW_TAG_structure_type:
23410 case DW_TAG_union_type:
23411 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23412 structures or unions. These were of the form "._%d" in GCC 4.1,
23413 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23414 and GCC 4.4. We work around this problem by ignoring these. */
23415 if (attr_name != nullptr
23416 && (startswith (attr_name, "._")
23417 || startswith (attr_name, "<anonymous")))
23418 return NULL;
23419
23420 /* GCC might emit a nameless typedef that has a linkage name. See
23421 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23422 if (!attr || attr_name == NULL)
23423 {
23424 attr = dw2_linkage_name_attr (die, cu);
23425 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23426 if (attr == NULL || attr_name == NULL)
23427 return NULL;
23428
23429 /* Avoid demangling attr_name the second time on a second
23430 call for the same DIE. */
23431 if (!attr->canonical_string_p ())
23432 {
23433 gdb::unique_xmalloc_ptr<char> demangled
23434 (gdb_demangle (attr_name, DMGL_TYPES));
23435 if (demangled == nullptr)
23436 return nullptr;
23437
23438 attr->set_string_canonical (objfile->intern (demangled.get ()));
23439 attr_name = attr->as_string ();
23440 }
23441
23442 /* Strip any leading namespaces/classes, keep only the
23443 base name. DW_AT_name for named DIEs does not
23444 contain the prefixes. */
23445 const char *base = strrchr (attr_name, ':');
23446 if (base && base > attr_name && base[-1] == ':')
23447 return &base[1];
23448 else
23449 return attr_name;
23450 }
23451 break;
23452
23453 default:
23454 break;
23455 }
23456
23457 if (!attr->canonical_string_p ())
23458 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23459 objfile));
23460 return attr->as_string ();
23461 }
23462
23463 /* Return the die that this die in an extension of, or NULL if there
23464 is none. *EXT_CU is the CU containing DIE on input, and the CU
23465 containing the return value on output. */
23466
23467 static struct die_info *
23468 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23469 {
23470 struct attribute *attr;
23471
23472 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23473 if (attr == NULL)
23474 return NULL;
23475
23476 return follow_die_ref (die, attr, ext_cu);
23477 }
23478
23479 static void
23480 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23481 {
23482 unsigned int i;
23483
23484 print_spaces (indent, f);
23485 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23486 dwarf_tag_name (die->tag), die->abbrev,
23487 sect_offset_str (die->sect_off));
23488
23489 if (die->parent != NULL)
23490 {
23491 print_spaces (indent, f);
23492 fprintf_unfiltered (f, " parent at offset: %s\n",
23493 sect_offset_str (die->parent->sect_off));
23494 }
23495
23496 print_spaces (indent, f);
23497 fprintf_unfiltered (f, " has children: %s\n",
23498 dwarf_bool_name (die->child != NULL));
23499
23500 print_spaces (indent, f);
23501 fprintf_unfiltered (f, " attributes:\n");
23502
23503 for (i = 0; i < die->num_attrs; ++i)
23504 {
23505 print_spaces (indent, f);
23506 fprintf_unfiltered (f, " %s (%s) ",
23507 dwarf_attr_name (die->attrs[i].name),
23508 dwarf_form_name (die->attrs[i].form));
23509
23510 switch (die->attrs[i].form)
23511 {
23512 case DW_FORM_addr:
23513 case DW_FORM_addrx:
23514 case DW_FORM_GNU_addr_index:
23515 fprintf_unfiltered (f, "address: ");
23516 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23517 break;
23518 case DW_FORM_block2:
23519 case DW_FORM_block4:
23520 case DW_FORM_block:
23521 case DW_FORM_block1:
23522 fprintf_unfiltered (f, "block: size %s",
23523 pulongest (die->attrs[i].as_block ()->size));
23524 break;
23525 case DW_FORM_exprloc:
23526 fprintf_unfiltered (f, "expression: size %s",
23527 pulongest (die->attrs[i].as_block ()->size));
23528 break;
23529 case DW_FORM_data16:
23530 fprintf_unfiltered (f, "constant of 16 bytes");
23531 break;
23532 case DW_FORM_ref_addr:
23533 fprintf_unfiltered (f, "ref address: ");
23534 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23535 break;
23536 case DW_FORM_GNU_ref_alt:
23537 fprintf_unfiltered (f, "alt ref address: ");
23538 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23539 break;
23540 case DW_FORM_ref1:
23541 case DW_FORM_ref2:
23542 case DW_FORM_ref4:
23543 case DW_FORM_ref8:
23544 case DW_FORM_ref_udata:
23545 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23546 (long) (die->attrs[i].as_unsigned ()));
23547 break;
23548 case DW_FORM_data1:
23549 case DW_FORM_data2:
23550 case DW_FORM_data4:
23551 case DW_FORM_data8:
23552 case DW_FORM_udata:
23553 fprintf_unfiltered (f, "constant: %s",
23554 pulongest (die->attrs[i].as_unsigned ()));
23555 break;
23556 case DW_FORM_sec_offset:
23557 fprintf_unfiltered (f, "section offset: %s",
23558 pulongest (die->attrs[i].as_unsigned ()));
23559 break;
23560 case DW_FORM_ref_sig8:
23561 fprintf_unfiltered (f, "signature: %s",
23562 hex_string (die->attrs[i].as_signature ()));
23563 break;
23564 case DW_FORM_string:
23565 case DW_FORM_strp:
23566 case DW_FORM_line_strp:
23567 case DW_FORM_strx:
23568 case DW_FORM_GNU_str_index:
23569 case DW_FORM_GNU_strp_alt:
23570 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23571 die->attrs[i].as_string ()
23572 ? die->attrs[i].as_string () : "",
23573 die->attrs[i].canonical_string_p () ? "is" : "not");
23574 break;
23575 case DW_FORM_flag:
23576 if (die->attrs[i].as_boolean ())
23577 fprintf_unfiltered (f, "flag: TRUE");
23578 else
23579 fprintf_unfiltered (f, "flag: FALSE");
23580 break;
23581 case DW_FORM_flag_present:
23582 fprintf_unfiltered (f, "flag: TRUE");
23583 break;
23584 case DW_FORM_indirect:
23585 /* The reader will have reduced the indirect form to
23586 the "base form" so this form should not occur. */
23587 fprintf_unfiltered (f,
23588 "unexpected attribute form: DW_FORM_indirect");
23589 break;
23590 case DW_FORM_sdata:
23591 case DW_FORM_implicit_const:
23592 fprintf_unfiltered (f, "constant: %s",
23593 plongest (die->attrs[i].as_signed ()));
23594 break;
23595 default:
23596 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23597 die->attrs[i].form);
23598 break;
23599 }
23600 fprintf_unfiltered (f, "\n");
23601 }
23602 }
23603
23604 static void
23605 dump_die_for_error (struct die_info *die)
23606 {
23607 dump_die_shallow (gdb_stderr, 0, die);
23608 }
23609
23610 static void
23611 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23612 {
23613 int indent = level * 4;
23614
23615 gdb_assert (die != NULL);
23616
23617 if (level >= max_level)
23618 return;
23619
23620 dump_die_shallow (f, indent, die);
23621
23622 if (die->child != NULL)
23623 {
23624 print_spaces (indent, f);
23625 fprintf_unfiltered (f, " Children:");
23626 if (level + 1 < max_level)
23627 {
23628 fprintf_unfiltered (f, "\n");
23629 dump_die_1 (f, level + 1, max_level, die->child);
23630 }
23631 else
23632 {
23633 fprintf_unfiltered (f,
23634 " [not printed, max nesting level reached]\n");
23635 }
23636 }
23637
23638 if (die->sibling != NULL && level > 0)
23639 {
23640 dump_die_1 (f, level, max_level, die->sibling);
23641 }
23642 }
23643
23644 /* This is called from the pdie macro in gdbinit.in.
23645 It's not static so gcc will keep a copy callable from gdb. */
23646
23647 void
23648 dump_die (struct die_info *die, int max_level)
23649 {
23650 dump_die_1 (gdb_stdlog, 0, max_level, die);
23651 }
23652
23653 static void
23654 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23655 {
23656 void **slot;
23657
23658 slot = htab_find_slot_with_hash (cu->die_hash, die,
23659 to_underlying (die->sect_off),
23660 INSERT);
23661
23662 *slot = die;
23663 }
23664
23665 /* Follow reference or signature attribute ATTR of SRC_DIE.
23666 On entry *REF_CU is the CU of SRC_DIE.
23667 On exit *REF_CU is the CU of the result. */
23668
23669 static struct die_info *
23670 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23671 struct dwarf2_cu **ref_cu)
23672 {
23673 struct die_info *die;
23674
23675 if (attr->form_is_ref ())
23676 die = follow_die_ref (src_die, attr, ref_cu);
23677 else if (attr->form == DW_FORM_ref_sig8)
23678 die = follow_die_sig (src_die, attr, ref_cu);
23679 else
23680 {
23681 dump_die_for_error (src_die);
23682 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23683 objfile_name ((*ref_cu)->per_objfile->objfile));
23684 }
23685
23686 return die;
23687 }
23688
23689 /* Follow reference OFFSET.
23690 On entry *REF_CU is the CU of the source die referencing OFFSET.
23691 On exit *REF_CU is the CU of the result.
23692 Returns NULL if OFFSET is invalid. */
23693
23694 static struct die_info *
23695 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23696 struct dwarf2_cu **ref_cu)
23697 {
23698 struct die_info temp_die;
23699 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23700 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23701
23702 gdb_assert (cu->per_cu != NULL);
23703
23704 target_cu = cu;
23705
23706 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23707 "source CU contains target offset: %d",
23708 sect_offset_str (cu->per_cu->sect_off),
23709 sect_offset_str (sect_off),
23710 cu->header.offset_in_cu_p (sect_off));
23711
23712 if (cu->per_cu->is_debug_types)
23713 {
23714 /* .debug_types CUs cannot reference anything outside their CU.
23715 If they need to, they have to reference a signatured type via
23716 DW_FORM_ref_sig8. */
23717 if (!cu->header.offset_in_cu_p (sect_off))
23718 return NULL;
23719 }
23720 else if (offset_in_dwz != cu->per_cu->is_dwz
23721 || !cu->header.offset_in_cu_p (sect_off))
23722 {
23723 struct dwarf2_per_cu_data *per_cu;
23724
23725 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23726 per_objfile);
23727
23728 dwarf_read_debug_printf_v ("target CU offset: %s, "
23729 "target CU DIEs loaded: %d",
23730 sect_offset_str (per_cu->sect_off),
23731 per_objfile->get_cu (per_cu) != nullptr);
23732
23733 /* If necessary, add it to the queue and load its DIEs.
23734
23735 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23736 it doesn't mean they are currently loaded. Since we require them
23737 to be loaded, we must check for ourselves. */
23738 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23739 || per_objfile->get_cu (per_cu) == nullptr)
23740 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23741 false, cu->language);
23742
23743 target_cu = per_objfile->get_cu (per_cu);
23744 gdb_assert (target_cu != nullptr);
23745 }
23746 else if (cu->dies == NULL)
23747 {
23748 /* We're loading full DIEs during partial symbol reading. */
23749 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23750 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23751 language_minimal);
23752 }
23753
23754 *ref_cu = target_cu;
23755 temp_die.sect_off = sect_off;
23756
23757 if (target_cu != cu)
23758 target_cu->ancestor = cu;
23759
23760 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23761 &temp_die,
23762 to_underlying (sect_off));
23763 }
23764
23765 /* Follow reference attribute ATTR of SRC_DIE.
23766 On entry *REF_CU is the CU of SRC_DIE.
23767 On exit *REF_CU is the CU of the result. */
23768
23769 static struct die_info *
23770 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23771 struct dwarf2_cu **ref_cu)
23772 {
23773 sect_offset sect_off = attr->get_ref_die_offset ();
23774 struct dwarf2_cu *cu = *ref_cu;
23775 struct die_info *die;
23776
23777 die = follow_die_offset (sect_off,
23778 (attr->form == DW_FORM_GNU_ref_alt
23779 || cu->per_cu->is_dwz),
23780 ref_cu);
23781 if (!die)
23782 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23783 "at %s [in module %s]"),
23784 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23785 objfile_name (cu->per_objfile->objfile));
23786
23787 return die;
23788 }
23789
23790 /* See read.h. */
23791
23792 struct dwarf2_locexpr_baton
23793 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23794 dwarf2_per_cu_data *per_cu,
23795 dwarf2_per_objfile *per_objfile,
23796 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23797 bool resolve_abstract_p)
23798 {
23799 struct die_info *die;
23800 struct attribute *attr;
23801 struct dwarf2_locexpr_baton retval;
23802 struct objfile *objfile = per_objfile->objfile;
23803
23804 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23805 if (cu == nullptr)
23806 cu = load_cu (per_cu, per_objfile, false);
23807
23808 if (cu == nullptr)
23809 {
23810 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23811 Instead just throw an error, not much else we can do. */
23812 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23813 sect_offset_str (sect_off), objfile_name (objfile));
23814 }
23815
23816 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23817 if (!die)
23818 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23819 sect_offset_str (sect_off), objfile_name (objfile));
23820
23821 attr = dwarf2_attr (die, DW_AT_location, cu);
23822 if (!attr && resolve_abstract_p
23823 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23824 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23825 {
23826 CORE_ADDR pc = get_frame_pc ();
23827 CORE_ADDR baseaddr = objfile->text_section_offset ();
23828 struct gdbarch *gdbarch = objfile->arch ();
23829
23830 for (const auto &cand_off
23831 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23832 {
23833 struct dwarf2_cu *cand_cu = cu;
23834 struct die_info *cand
23835 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23836 if (!cand
23837 || !cand->parent
23838 || cand->parent->tag != DW_TAG_subprogram)
23839 continue;
23840
23841 CORE_ADDR pc_low, pc_high;
23842 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23843 if (pc_low == ((CORE_ADDR) -1))
23844 continue;
23845 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23846 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23847 if (!(pc_low <= pc && pc < pc_high))
23848 continue;
23849
23850 die = cand;
23851 attr = dwarf2_attr (die, DW_AT_location, cu);
23852 break;
23853 }
23854 }
23855
23856 if (!attr)
23857 {
23858 /* DWARF: "If there is no such attribute, then there is no effect.".
23859 DATA is ignored if SIZE is 0. */
23860
23861 retval.data = NULL;
23862 retval.size = 0;
23863 }
23864 else if (attr->form_is_section_offset ())
23865 {
23866 struct dwarf2_loclist_baton loclist_baton;
23867 CORE_ADDR pc = get_frame_pc ();
23868 size_t size;
23869
23870 fill_in_loclist_baton (cu, &loclist_baton, attr);
23871
23872 retval.data = dwarf2_find_location_expression (&loclist_baton,
23873 &size, pc);
23874 retval.size = size;
23875 }
23876 else
23877 {
23878 if (!attr->form_is_block ())
23879 error (_("Dwarf Error: DIE at %s referenced in module %s "
23880 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23881 sect_offset_str (sect_off), objfile_name (objfile));
23882
23883 struct dwarf_block *block = attr->as_block ();
23884 retval.data = block->data;
23885 retval.size = block->size;
23886 }
23887 retval.per_objfile = per_objfile;
23888 retval.per_cu = cu->per_cu;
23889
23890 per_objfile->age_comp_units ();
23891
23892 return retval;
23893 }
23894
23895 /* See read.h. */
23896
23897 struct dwarf2_locexpr_baton
23898 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23899 dwarf2_per_cu_data *per_cu,
23900 dwarf2_per_objfile *per_objfile,
23901 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23902 {
23903 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23904
23905 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23906 get_frame_pc);
23907 }
23908
23909 /* Write a constant of a given type as target-ordered bytes into
23910 OBSTACK. */
23911
23912 static const gdb_byte *
23913 write_constant_as_bytes (struct obstack *obstack,
23914 enum bfd_endian byte_order,
23915 struct type *type,
23916 ULONGEST value,
23917 LONGEST *len)
23918 {
23919 gdb_byte *result;
23920
23921 *len = TYPE_LENGTH (type);
23922 result = (gdb_byte *) obstack_alloc (obstack, *len);
23923 store_unsigned_integer (result, *len, byte_order, value);
23924
23925 return result;
23926 }
23927
23928 /* See read.h. */
23929
23930 const gdb_byte *
23931 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23932 dwarf2_per_cu_data *per_cu,
23933 dwarf2_per_objfile *per_objfile,
23934 obstack *obstack,
23935 LONGEST *len)
23936 {
23937 struct die_info *die;
23938 struct attribute *attr;
23939 const gdb_byte *result = NULL;
23940 struct type *type;
23941 LONGEST value;
23942 enum bfd_endian byte_order;
23943 struct objfile *objfile = per_objfile->objfile;
23944
23945 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23946 if (cu == nullptr)
23947 cu = load_cu (per_cu, per_objfile, false);
23948
23949 if (cu == nullptr)
23950 {
23951 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23952 Instead just throw an error, not much else we can do. */
23953 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23954 sect_offset_str (sect_off), objfile_name (objfile));
23955 }
23956
23957 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23958 if (!die)
23959 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23960 sect_offset_str (sect_off), objfile_name (objfile));
23961
23962 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23963 if (attr == NULL)
23964 return NULL;
23965
23966 byte_order = (bfd_big_endian (objfile->obfd)
23967 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23968
23969 switch (attr->form)
23970 {
23971 case DW_FORM_addr:
23972 case DW_FORM_addrx:
23973 case DW_FORM_GNU_addr_index:
23974 {
23975 gdb_byte *tem;
23976
23977 *len = cu->header.addr_size;
23978 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23979 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23980 result = tem;
23981 }
23982 break;
23983 case DW_FORM_string:
23984 case DW_FORM_strp:
23985 case DW_FORM_strx:
23986 case DW_FORM_GNU_str_index:
23987 case DW_FORM_GNU_strp_alt:
23988 /* The string is already allocated on the objfile obstack, point
23989 directly to it. */
23990 {
23991 const char *attr_name = attr->as_string ();
23992 result = (const gdb_byte *) attr_name;
23993 *len = strlen (attr_name);
23994 }
23995 break;
23996 case DW_FORM_block1:
23997 case DW_FORM_block2:
23998 case DW_FORM_block4:
23999 case DW_FORM_block:
24000 case DW_FORM_exprloc:
24001 case DW_FORM_data16:
24002 {
24003 struct dwarf_block *block = attr->as_block ();
24004 result = block->data;
24005 *len = block->size;
24006 }
24007 break;
24008
24009 /* The DW_AT_const_value attributes are supposed to carry the
24010 symbol's value "represented as it would be on the target
24011 architecture." By the time we get here, it's already been
24012 converted to host endianness, so we just need to sign- or
24013 zero-extend it as appropriate. */
24014 case DW_FORM_data1:
24015 type = die_type (die, cu);
24016 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
24017 if (result == NULL)
24018 result = write_constant_as_bytes (obstack, byte_order,
24019 type, value, len);
24020 break;
24021 case DW_FORM_data2:
24022 type = die_type (die, cu);
24023 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
24024 if (result == NULL)
24025 result = write_constant_as_bytes (obstack, byte_order,
24026 type, value, len);
24027 break;
24028 case DW_FORM_data4:
24029 type = die_type (die, cu);
24030 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
24031 if (result == NULL)
24032 result = write_constant_as_bytes (obstack, byte_order,
24033 type, value, len);
24034 break;
24035 case DW_FORM_data8:
24036 type = die_type (die, cu);
24037 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
24038 if (result == NULL)
24039 result = write_constant_as_bytes (obstack, byte_order,
24040 type, value, len);
24041 break;
24042
24043 case DW_FORM_sdata:
24044 case DW_FORM_implicit_const:
24045 type = die_type (die, cu);
24046 result = write_constant_as_bytes (obstack, byte_order,
24047 type, attr->as_signed (), len);
24048 break;
24049
24050 case DW_FORM_udata:
24051 type = die_type (die, cu);
24052 result = write_constant_as_bytes (obstack, byte_order,
24053 type, attr->as_unsigned (), len);
24054 break;
24055
24056 default:
24057 complaint (_("unsupported const value attribute form: '%s'"),
24058 dwarf_form_name (attr->form));
24059 break;
24060 }
24061
24062 return result;
24063 }
24064
24065 /* See read.h. */
24066
24067 struct type *
24068 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24069 dwarf2_per_cu_data *per_cu,
24070 dwarf2_per_objfile *per_objfile)
24071 {
24072 struct die_info *die;
24073
24074 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24075 if (cu == nullptr)
24076 cu = load_cu (per_cu, per_objfile, false);
24077
24078 if (cu == nullptr)
24079 return nullptr;
24080
24081 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24082 if (!die)
24083 return NULL;
24084
24085 return die_type (die, cu);
24086 }
24087
24088 /* See read.h. */
24089
24090 struct type *
24091 dwarf2_get_die_type (cu_offset die_offset,
24092 dwarf2_per_cu_data *per_cu,
24093 dwarf2_per_objfile *per_objfile)
24094 {
24095 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24096 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24097 }
24098
24099 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24100 On entry *REF_CU is the CU of SRC_DIE.
24101 On exit *REF_CU is the CU of the result.
24102 Returns NULL if the referenced DIE isn't found. */
24103
24104 static struct die_info *
24105 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24106 struct dwarf2_cu **ref_cu)
24107 {
24108 struct die_info temp_die;
24109 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24110 struct die_info *die;
24111 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24112
24113
24114 /* While it might be nice to assert sig_type->type == NULL here,
24115 we can get here for DW_AT_imported_declaration where we need
24116 the DIE not the type. */
24117
24118 /* If necessary, add it to the queue and load its DIEs.
24119
24120 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
24121 it doesn't mean they are currently loaded. Since we require them
24122 to be loaded, we must check for ourselves. */
24123 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24124 language_minimal)
24125 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
24126 read_signatured_type (sig_type, per_objfile);
24127
24128 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24129 gdb_assert (sig_cu != NULL);
24130 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24131 temp_die.sect_off = sig_type->type_offset_in_section;
24132 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24133 to_underlying (temp_die.sect_off));
24134 if (die)
24135 {
24136 /* For .gdb_index version 7 keep track of included TUs.
24137 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24138 if (per_objfile->per_bfd->index_table != NULL
24139 && per_objfile->per_bfd->index_table->version <= 7)
24140 {
24141 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24142 }
24143
24144 *ref_cu = sig_cu;
24145 if (sig_cu != cu)
24146 sig_cu->ancestor = cu;
24147
24148 return die;
24149 }
24150
24151 return NULL;
24152 }
24153
24154 /* Follow signatured type referenced by ATTR in SRC_DIE.
24155 On entry *REF_CU is the CU of SRC_DIE.
24156 On exit *REF_CU is the CU of the result.
24157 The result is the DIE of the type.
24158 If the referenced type cannot be found an error is thrown. */
24159
24160 static struct die_info *
24161 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24162 struct dwarf2_cu **ref_cu)
24163 {
24164 ULONGEST signature = attr->as_signature ();
24165 struct signatured_type *sig_type;
24166 struct die_info *die;
24167
24168 gdb_assert (attr->form == DW_FORM_ref_sig8);
24169
24170 sig_type = lookup_signatured_type (*ref_cu, signature);
24171 /* sig_type will be NULL if the signatured type is missing from
24172 the debug info. */
24173 if (sig_type == NULL)
24174 {
24175 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24176 " from DIE at %s [in module %s]"),
24177 hex_string (signature), sect_offset_str (src_die->sect_off),
24178 objfile_name ((*ref_cu)->per_objfile->objfile));
24179 }
24180
24181 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24182 if (die == NULL)
24183 {
24184 dump_die_for_error (src_die);
24185 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24186 " from DIE at %s [in module %s]"),
24187 hex_string (signature), sect_offset_str (src_die->sect_off),
24188 objfile_name ((*ref_cu)->per_objfile->objfile));
24189 }
24190
24191 return die;
24192 }
24193
24194 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24195 reading in and processing the type unit if necessary. */
24196
24197 static struct type *
24198 get_signatured_type (struct die_info *die, ULONGEST signature,
24199 struct dwarf2_cu *cu)
24200 {
24201 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24202 struct signatured_type *sig_type;
24203 struct dwarf2_cu *type_cu;
24204 struct die_info *type_die;
24205 struct type *type;
24206
24207 sig_type = lookup_signatured_type (cu, signature);
24208 /* sig_type will be NULL if the signatured type is missing from
24209 the debug info. */
24210 if (sig_type == NULL)
24211 {
24212 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24213 " from DIE at %s [in module %s]"),
24214 hex_string (signature), sect_offset_str (die->sect_off),
24215 objfile_name (per_objfile->objfile));
24216 return build_error_marker_type (cu, die);
24217 }
24218
24219 /* If we already know the type we're done. */
24220 type = per_objfile->get_type_for_signatured_type (sig_type);
24221 if (type != nullptr)
24222 return type;
24223
24224 type_cu = cu;
24225 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24226 if (type_die != NULL)
24227 {
24228 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24229 is created. This is important, for example, because for c++ classes
24230 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24231 type = read_type_die (type_die, type_cu);
24232 if (type == NULL)
24233 {
24234 complaint (_("Dwarf Error: Cannot build signatured type %s"
24235 " referenced from DIE at %s [in module %s]"),
24236 hex_string (signature), sect_offset_str (die->sect_off),
24237 objfile_name (per_objfile->objfile));
24238 type = build_error_marker_type (cu, die);
24239 }
24240 }
24241 else
24242 {
24243 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24244 " from DIE at %s [in module %s]"),
24245 hex_string (signature), sect_offset_str (die->sect_off),
24246 objfile_name (per_objfile->objfile));
24247 type = build_error_marker_type (cu, die);
24248 }
24249
24250 per_objfile->set_type_for_signatured_type (sig_type, type);
24251
24252 return type;
24253 }
24254
24255 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24256 reading in and processing the type unit if necessary. */
24257
24258 static struct type *
24259 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24260 struct dwarf2_cu *cu) /* ARI: editCase function */
24261 {
24262 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24263 if (attr->form_is_ref ())
24264 {
24265 struct dwarf2_cu *type_cu = cu;
24266 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24267
24268 return read_type_die (type_die, type_cu);
24269 }
24270 else if (attr->form == DW_FORM_ref_sig8)
24271 {
24272 return get_signatured_type (die, attr->as_signature (), cu);
24273 }
24274 else
24275 {
24276 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24277
24278 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24279 " at %s [in module %s]"),
24280 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24281 objfile_name (per_objfile->objfile));
24282 return build_error_marker_type (cu, die);
24283 }
24284 }
24285
24286 /* Load the DIEs associated with type unit PER_CU into memory. */
24287
24288 static void
24289 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24290 dwarf2_per_objfile *per_objfile)
24291 {
24292 struct signatured_type *sig_type;
24293
24294 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24295 gdb_assert (! per_cu->type_unit_group_p ());
24296
24297 /* We have the per_cu, but we need the signatured_type.
24298 Fortunately this is an easy translation. */
24299 gdb_assert (per_cu->is_debug_types);
24300 sig_type = (struct signatured_type *) per_cu;
24301
24302 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24303
24304 read_signatured_type (sig_type, per_objfile);
24305
24306 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24307 }
24308
24309 /* Read in a signatured type and build its CU and DIEs.
24310 If the type is a stub for the real type in a DWO file,
24311 read in the real type from the DWO file as well. */
24312
24313 static void
24314 read_signatured_type (signatured_type *sig_type,
24315 dwarf2_per_objfile *per_objfile)
24316 {
24317 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24318
24319 gdb_assert (per_cu->is_debug_types);
24320 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24321
24322 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24323
24324 if (!reader.dummy_p)
24325 {
24326 struct dwarf2_cu *cu = reader.cu;
24327 const gdb_byte *info_ptr = reader.info_ptr;
24328
24329 gdb_assert (cu->die_hash == NULL);
24330 cu->die_hash =
24331 htab_create_alloc_ex (cu->header.length / 12,
24332 die_hash,
24333 die_eq,
24334 NULL,
24335 &cu->comp_unit_obstack,
24336 hashtab_obstack_allocate,
24337 dummy_obstack_deallocate);
24338
24339 if (reader.comp_unit_die->has_children)
24340 reader.comp_unit_die->child
24341 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24342 reader.comp_unit_die);
24343 cu->dies = reader.comp_unit_die;
24344 /* comp_unit_die is not stored in die_hash, no need. */
24345
24346 /* We try not to read any attributes in this function, because
24347 not all CUs needed for references have been loaded yet, and
24348 symbol table processing isn't initialized. But we have to
24349 set the CU language, or we won't be able to build types
24350 correctly. Similarly, if we do not read the producer, we can
24351 not apply producer-specific interpretation. */
24352 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24353
24354 reader.keep ();
24355 }
24356
24357 sig_type->per_cu.tu_read = 1;
24358 }
24359
24360 /* Decode simple location descriptions.
24361 Given a pointer to a dwarf block that defines a location, compute
24362 the location and return the value. If COMPUTED is non-null, it is
24363 set to true to indicate that decoding was successful, and false
24364 otherwise. If COMPUTED is null, then this function may emit a
24365 complaint. */
24366
24367 static CORE_ADDR
24368 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24369 {
24370 struct objfile *objfile = cu->per_objfile->objfile;
24371 size_t i;
24372 size_t size = blk->size;
24373 const gdb_byte *data = blk->data;
24374 CORE_ADDR stack[64];
24375 int stacki;
24376 unsigned int bytes_read, unsnd;
24377 gdb_byte op;
24378
24379 if (computed != nullptr)
24380 *computed = false;
24381
24382 i = 0;
24383 stacki = 0;
24384 stack[stacki] = 0;
24385 stack[++stacki] = 0;
24386
24387 while (i < size)
24388 {
24389 op = data[i++];
24390 switch (op)
24391 {
24392 case DW_OP_lit0:
24393 case DW_OP_lit1:
24394 case DW_OP_lit2:
24395 case DW_OP_lit3:
24396 case DW_OP_lit4:
24397 case DW_OP_lit5:
24398 case DW_OP_lit6:
24399 case DW_OP_lit7:
24400 case DW_OP_lit8:
24401 case DW_OP_lit9:
24402 case DW_OP_lit10:
24403 case DW_OP_lit11:
24404 case DW_OP_lit12:
24405 case DW_OP_lit13:
24406 case DW_OP_lit14:
24407 case DW_OP_lit15:
24408 case DW_OP_lit16:
24409 case DW_OP_lit17:
24410 case DW_OP_lit18:
24411 case DW_OP_lit19:
24412 case DW_OP_lit20:
24413 case DW_OP_lit21:
24414 case DW_OP_lit22:
24415 case DW_OP_lit23:
24416 case DW_OP_lit24:
24417 case DW_OP_lit25:
24418 case DW_OP_lit26:
24419 case DW_OP_lit27:
24420 case DW_OP_lit28:
24421 case DW_OP_lit29:
24422 case DW_OP_lit30:
24423 case DW_OP_lit31:
24424 stack[++stacki] = op - DW_OP_lit0;
24425 break;
24426
24427 case DW_OP_reg0:
24428 case DW_OP_reg1:
24429 case DW_OP_reg2:
24430 case DW_OP_reg3:
24431 case DW_OP_reg4:
24432 case DW_OP_reg5:
24433 case DW_OP_reg6:
24434 case DW_OP_reg7:
24435 case DW_OP_reg8:
24436 case DW_OP_reg9:
24437 case DW_OP_reg10:
24438 case DW_OP_reg11:
24439 case DW_OP_reg12:
24440 case DW_OP_reg13:
24441 case DW_OP_reg14:
24442 case DW_OP_reg15:
24443 case DW_OP_reg16:
24444 case DW_OP_reg17:
24445 case DW_OP_reg18:
24446 case DW_OP_reg19:
24447 case DW_OP_reg20:
24448 case DW_OP_reg21:
24449 case DW_OP_reg22:
24450 case DW_OP_reg23:
24451 case DW_OP_reg24:
24452 case DW_OP_reg25:
24453 case DW_OP_reg26:
24454 case DW_OP_reg27:
24455 case DW_OP_reg28:
24456 case DW_OP_reg29:
24457 case DW_OP_reg30:
24458 case DW_OP_reg31:
24459 stack[++stacki] = op - DW_OP_reg0;
24460 if (i < size)
24461 {
24462 if (computed == nullptr)
24463 dwarf2_complex_location_expr_complaint ();
24464 else
24465 return 0;
24466 }
24467 break;
24468
24469 case DW_OP_regx:
24470 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24471 i += bytes_read;
24472 stack[++stacki] = unsnd;
24473 if (i < size)
24474 {
24475 if (computed == nullptr)
24476 dwarf2_complex_location_expr_complaint ();
24477 else
24478 return 0;
24479 }
24480 break;
24481
24482 case DW_OP_addr:
24483 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24484 &bytes_read);
24485 i += bytes_read;
24486 break;
24487
24488 case DW_OP_const1u:
24489 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24490 i += 1;
24491 break;
24492
24493 case DW_OP_const1s:
24494 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24495 i += 1;
24496 break;
24497
24498 case DW_OP_const2u:
24499 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24500 i += 2;
24501 break;
24502
24503 case DW_OP_const2s:
24504 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24505 i += 2;
24506 break;
24507
24508 case DW_OP_const4u:
24509 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24510 i += 4;
24511 break;
24512
24513 case DW_OP_const4s:
24514 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24515 i += 4;
24516 break;
24517
24518 case DW_OP_const8u:
24519 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24520 i += 8;
24521 break;
24522
24523 case DW_OP_constu:
24524 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24525 &bytes_read);
24526 i += bytes_read;
24527 break;
24528
24529 case DW_OP_consts:
24530 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24531 i += bytes_read;
24532 break;
24533
24534 case DW_OP_dup:
24535 stack[stacki + 1] = stack[stacki];
24536 stacki++;
24537 break;
24538
24539 case DW_OP_plus:
24540 stack[stacki - 1] += stack[stacki];
24541 stacki--;
24542 break;
24543
24544 case DW_OP_plus_uconst:
24545 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24546 &bytes_read);
24547 i += bytes_read;
24548 break;
24549
24550 case DW_OP_minus:
24551 stack[stacki - 1] -= stack[stacki];
24552 stacki--;
24553 break;
24554
24555 case DW_OP_deref:
24556 /* If we're not the last op, then we definitely can't encode
24557 this using GDB's address_class enum. This is valid for partial
24558 global symbols, although the variable's address will be bogus
24559 in the psymtab. */
24560 if (i < size)
24561 {
24562 if (computed == nullptr)
24563 dwarf2_complex_location_expr_complaint ();
24564 else
24565 return 0;
24566 }
24567 break;
24568
24569 case DW_OP_GNU_push_tls_address:
24570 case DW_OP_form_tls_address:
24571 /* The top of the stack has the offset from the beginning
24572 of the thread control block at which the variable is located. */
24573 /* Nothing should follow this operator, so the top of stack would
24574 be returned. */
24575 /* This is valid for partial global symbols, but the variable's
24576 address will be bogus in the psymtab. Make it always at least
24577 non-zero to not look as a variable garbage collected by linker
24578 which have DW_OP_addr 0. */
24579 if (i < size)
24580 {
24581 if (computed == nullptr)
24582 dwarf2_complex_location_expr_complaint ();
24583 else
24584 return 0;
24585 }
24586 stack[stacki]++;
24587 break;
24588
24589 case DW_OP_GNU_uninit:
24590 if (computed != nullptr)
24591 return 0;
24592 break;
24593
24594 case DW_OP_addrx:
24595 case DW_OP_GNU_addr_index:
24596 case DW_OP_GNU_const_index:
24597 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24598 &bytes_read);
24599 i += bytes_read;
24600 break;
24601
24602 default:
24603 if (computed == nullptr)
24604 {
24605 const char *name = get_DW_OP_name (op);
24606
24607 if (name)
24608 complaint (_("unsupported stack op: '%s'"),
24609 name);
24610 else
24611 complaint (_("unsupported stack op: '%02x'"),
24612 op);
24613 }
24614
24615 return (stack[stacki]);
24616 }
24617
24618 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24619 outside of the allocated space. Also enforce minimum>0. */
24620 if (stacki >= ARRAY_SIZE (stack) - 1)
24621 {
24622 if (computed == nullptr)
24623 complaint (_("location description stack overflow"));
24624 return 0;
24625 }
24626
24627 if (stacki <= 0)
24628 {
24629 if (computed == nullptr)
24630 complaint (_("location description stack underflow"));
24631 return 0;
24632 }
24633 }
24634
24635 if (computed != nullptr)
24636 *computed = true;
24637 return (stack[stacki]);
24638 }
24639
24640 /* memory allocation interface */
24641
24642 static struct dwarf_block *
24643 dwarf_alloc_block (struct dwarf2_cu *cu)
24644 {
24645 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24646 }
24647
24648 static struct die_info *
24649 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24650 {
24651 struct die_info *die;
24652 size_t size = sizeof (struct die_info);
24653
24654 if (num_attrs > 1)
24655 size += (num_attrs - 1) * sizeof (struct attribute);
24656
24657 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24658 memset (die, 0, sizeof (struct die_info));
24659 return (die);
24660 }
24661
24662 \f
24663
24664 /* Macro support. */
24665
24666 /* An overload of dwarf_decode_macros that finds the correct section
24667 and ensures it is read in before calling the other overload. */
24668
24669 static void
24670 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24671 int section_is_gnu)
24672 {
24673 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24674 struct objfile *objfile = per_objfile->objfile;
24675 const struct line_header *lh = cu->line_header;
24676 unsigned int offset_size = cu->header.offset_size;
24677 struct dwarf2_section_info *section;
24678 const char *section_name;
24679
24680 if (cu->dwo_unit != nullptr)
24681 {
24682 if (section_is_gnu)
24683 {
24684 section = &cu->dwo_unit->dwo_file->sections.macro;
24685 section_name = ".debug_macro.dwo";
24686 }
24687 else
24688 {
24689 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24690 section_name = ".debug_macinfo.dwo";
24691 }
24692 }
24693 else
24694 {
24695 if (section_is_gnu)
24696 {
24697 section = &per_objfile->per_bfd->macro;
24698 section_name = ".debug_macro";
24699 }
24700 else
24701 {
24702 section = &per_objfile->per_bfd->macinfo;
24703 section_name = ".debug_macinfo";
24704 }
24705 }
24706
24707 section->read (objfile);
24708 if (section->buffer == nullptr)
24709 {
24710 complaint (_("missing %s section"), section_name);
24711 return;
24712 }
24713
24714 buildsym_compunit *builder = cu->get_builder ();
24715
24716 struct dwarf2_section_info *str_offsets_section;
24717 struct dwarf2_section_info *str_section;
24718 ULONGEST str_offsets_base;
24719
24720 if (cu->dwo_unit != nullptr)
24721 {
24722 str_offsets_section = &cu->dwo_unit->dwo_file
24723 ->sections.str_offsets;
24724 str_section = &cu->dwo_unit->dwo_file->sections.str;
24725 str_offsets_base = cu->header.addr_size;
24726 }
24727 else
24728 {
24729 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24730 str_section = &per_objfile->per_bfd->str;
24731 str_offsets_base = *cu->str_offsets_base;
24732 }
24733
24734 dwarf_decode_macros (per_objfile, builder, section, lh,
24735 offset_size, offset, str_section, str_offsets_section,
24736 str_offsets_base, section_is_gnu);
24737 }
24738
24739 /* Return the .debug_loc section to use for CU.
24740 For DWO files use .debug_loc.dwo. */
24741
24742 static struct dwarf2_section_info *
24743 cu_debug_loc_section (struct dwarf2_cu *cu)
24744 {
24745 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24746
24747 if (cu->dwo_unit)
24748 {
24749 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24750
24751 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24752 }
24753 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24754 : &per_objfile->per_bfd->loc);
24755 }
24756
24757 /* Return the .debug_rnglists section to use for CU. */
24758 static struct dwarf2_section_info *
24759 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24760 {
24761 if (cu->header.version < 5)
24762 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24763 cu->header.version);
24764 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24765
24766 /* Make sure we read the .debug_rnglists section from the file that
24767 contains the DW_AT_ranges attribute we are reading. Normally that
24768 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24769 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24770 program. */
24771 if (cu->dwo_unit != nullptr
24772 && tag != DW_TAG_compile_unit
24773 && tag != DW_TAG_skeleton_unit)
24774 {
24775 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24776
24777 if (sections->rnglists.size > 0)
24778 return &sections->rnglists;
24779 else
24780 error (_(".debug_rnglists section is missing from .dwo file."));
24781 }
24782 return &dwarf2_per_objfile->per_bfd->rnglists;
24783 }
24784
24785 /* A helper function that fills in a dwarf2_loclist_baton. */
24786
24787 static void
24788 fill_in_loclist_baton (struct dwarf2_cu *cu,
24789 struct dwarf2_loclist_baton *baton,
24790 const struct attribute *attr)
24791 {
24792 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24793 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24794
24795 section->read (per_objfile->objfile);
24796
24797 baton->per_objfile = per_objfile;
24798 baton->per_cu = cu->per_cu;
24799 gdb_assert (baton->per_cu);
24800 /* We don't know how long the location list is, but make sure we
24801 don't run off the edge of the section. */
24802 baton->size = section->size - attr->as_unsigned ();
24803 baton->data = section->buffer + attr->as_unsigned ();
24804 if (cu->base_address.has_value ())
24805 baton->base_address = *cu->base_address;
24806 else
24807 baton->base_address = 0;
24808 baton->from_dwo = cu->dwo_unit != NULL;
24809 }
24810
24811 static void
24812 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24813 struct dwarf2_cu *cu, int is_block)
24814 {
24815 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24816 struct objfile *objfile = per_objfile->objfile;
24817 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24818
24819 if (attr->form_is_section_offset ()
24820 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24821 the section. If so, fall through to the complaint in the
24822 other branch. */
24823 && attr->as_unsigned () < section->get_size (objfile))
24824 {
24825 struct dwarf2_loclist_baton *baton;
24826
24827 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24828
24829 fill_in_loclist_baton (cu, baton, attr);
24830
24831 if (!cu->base_address.has_value ())
24832 complaint (_("Location list used without "
24833 "specifying the CU base address."));
24834
24835 SYMBOL_ACLASS_INDEX (sym) = (is_block
24836 ? dwarf2_loclist_block_index
24837 : dwarf2_loclist_index);
24838 SYMBOL_LOCATION_BATON (sym) = baton;
24839 }
24840 else
24841 {
24842 struct dwarf2_locexpr_baton *baton;
24843
24844 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24845 baton->per_objfile = per_objfile;
24846 baton->per_cu = cu->per_cu;
24847 gdb_assert (baton->per_cu);
24848
24849 if (attr->form_is_block ())
24850 {
24851 /* Note that we're just copying the block's data pointer
24852 here, not the actual data. We're still pointing into the
24853 info_buffer for SYM's objfile; right now we never release
24854 that buffer, but when we do clean up properly this may
24855 need to change. */
24856 struct dwarf_block *block = attr->as_block ();
24857 baton->size = block->size;
24858 baton->data = block->data;
24859 }
24860 else
24861 {
24862 dwarf2_invalid_attrib_class_complaint ("location description",
24863 sym->natural_name ());
24864 baton->size = 0;
24865 }
24866
24867 SYMBOL_ACLASS_INDEX (sym) = (is_block
24868 ? dwarf2_locexpr_block_index
24869 : dwarf2_locexpr_index);
24870 SYMBOL_LOCATION_BATON (sym) = baton;
24871 }
24872 }
24873
24874 /* See read.h. */
24875
24876 const comp_unit_head *
24877 dwarf2_per_cu_data::get_header () const
24878 {
24879 if (!m_header_read_in)
24880 {
24881 const gdb_byte *info_ptr
24882 = this->section->buffer + to_underlying (this->sect_off);
24883
24884 memset (&m_header, 0, sizeof (m_header));
24885
24886 read_comp_unit_head (&m_header, info_ptr, this->section,
24887 rcuh_kind::COMPILE);
24888 }
24889
24890 return &m_header;
24891 }
24892
24893 /* See read.h. */
24894
24895 int
24896 dwarf2_per_cu_data::addr_size () const
24897 {
24898 return this->get_header ()->addr_size;
24899 }
24900
24901 /* See read.h. */
24902
24903 int
24904 dwarf2_per_cu_data::offset_size () const
24905 {
24906 return this->get_header ()->offset_size;
24907 }
24908
24909 /* See read.h. */
24910
24911 int
24912 dwarf2_per_cu_data::ref_addr_size () const
24913 {
24914 const comp_unit_head *header = this->get_header ();
24915
24916 if (header->version == 2)
24917 return header->addr_size;
24918 else
24919 return header->offset_size;
24920 }
24921
24922 /* See read.h. */
24923
24924 struct type *
24925 dwarf2_cu::addr_type () const
24926 {
24927 struct objfile *objfile = this->per_objfile->objfile;
24928 struct type *void_type = objfile_type (objfile)->builtin_void;
24929 struct type *addr_type = lookup_pointer_type (void_type);
24930 int addr_size = this->per_cu->addr_size ();
24931
24932 if (TYPE_LENGTH (addr_type) == addr_size)
24933 return addr_type;
24934
24935 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24936 return addr_type;
24937 }
24938
24939 /* A helper function for dwarf2_find_containing_comp_unit that returns
24940 the index of the result, and that searches a vector. It will
24941 return a result even if the offset in question does not actually
24942 occur in any CU. This is separate so that it can be unit
24943 tested. */
24944
24945 static int
24946 dwarf2_find_containing_comp_unit
24947 (sect_offset sect_off,
24948 unsigned int offset_in_dwz,
24949 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24950 {
24951 int low, high;
24952
24953 low = 0;
24954 high = all_comp_units.size () - 1;
24955 while (high > low)
24956 {
24957 struct dwarf2_per_cu_data *mid_cu;
24958 int mid = low + (high - low) / 2;
24959
24960 mid_cu = all_comp_units[mid];
24961 if (mid_cu->is_dwz > offset_in_dwz
24962 || (mid_cu->is_dwz == offset_in_dwz
24963 && mid_cu->sect_off + mid_cu->length > sect_off))
24964 high = mid;
24965 else
24966 low = mid + 1;
24967 }
24968 gdb_assert (low == high);
24969 return low;
24970 }
24971
24972 /* Locate the .debug_info compilation unit from CU's objfile which contains
24973 the DIE at OFFSET. Raises an error on failure. */
24974
24975 static struct dwarf2_per_cu_data *
24976 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24977 unsigned int offset_in_dwz,
24978 dwarf2_per_objfile *per_objfile)
24979 {
24980 int low = dwarf2_find_containing_comp_unit
24981 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24982 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24983
24984 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24985 {
24986 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24987 error (_("Dwarf Error: could not find partial DIE containing "
24988 "offset %s [in module %s]"),
24989 sect_offset_str (sect_off),
24990 bfd_get_filename (per_objfile->objfile->obfd));
24991
24992 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24993 <= sect_off);
24994 return per_objfile->per_bfd->all_comp_units[low-1];
24995 }
24996 else
24997 {
24998 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24999 && sect_off >= this_cu->sect_off + this_cu->length)
25000 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
25001 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
25002 return this_cu;
25003 }
25004 }
25005
25006 #if GDB_SELF_TEST
25007
25008 namespace selftests {
25009 namespace find_containing_comp_unit {
25010
25011 static void
25012 run_test ()
25013 {
25014 struct dwarf2_per_cu_data one {};
25015 struct dwarf2_per_cu_data two {};
25016 struct dwarf2_per_cu_data three {};
25017 struct dwarf2_per_cu_data four {};
25018
25019 one.length = 5;
25020 two.sect_off = sect_offset (one.length);
25021 two.length = 7;
25022
25023 three.length = 5;
25024 three.is_dwz = 1;
25025 four.sect_off = sect_offset (three.length);
25026 four.length = 7;
25027 four.is_dwz = 1;
25028
25029 std::vector<dwarf2_per_cu_data *> units;
25030 units.push_back (&one);
25031 units.push_back (&two);
25032 units.push_back (&three);
25033 units.push_back (&four);
25034
25035 int result;
25036
25037 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
25038 SELF_CHECK (units[result] == &one);
25039 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
25040 SELF_CHECK (units[result] == &one);
25041 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
25042 SELF_CHECK (units[result] == &two);
25043
25044 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
25045 SELF_CHECK (units[result] == &three);
25046 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
25047 SELF_CHECK (units[result] == &three);
25048 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
25049 SELF_CHECK (units[result] == &four);
25050 }
25051
25052 }
25053 }
25054
25055 #endif /* GDB_SELF_TEST */
25056
25057 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25058
25059 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25060 dwarf2_per_objfile *per_objfile)
25061 : per_cu (per_cu),
25062 per_objfile (per_objfile),
25063 mark (false),
25064 has_loclist (false),
25065 checked_producer (false),
25066 producer_is_gxx_lt_4_6 (false),
25067 producer_is_gcc_lt_4_3 (false),
25068 producer_is_icc (false),
25069 producer_is_icc_lt_14 (false),
25070 producer_is_codewarrior (false),
25071 processing_has_namespace_info (false)
25072 {
25073 }
25074
25075 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25076
25077 static void
25078 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25079 enum language pretend_language)
25080 {
25081 struct attribute *attr;
25082
25083 /* Set the language we're debugging. */
25084 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25085 if (attr != nullptr)
25086 set_cu_language (attr->constant_value (0), cu);
25087 else
25088 {
25089 cu->language = pretend_language;
25090 cu->language_defn = language_def (cu->language);
25091 }
25092
25093 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25094 }
25095
25096 /* See read.h. */
25097
25098 dwarf2_cu *
25099 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25100 {
25101 auto it = m_dwarf2_cus.find (per_cu);
25102 if (it == m_dwarf2_cus.end ())
25103 return nullptr;
25104
25105 return it->second;
25106 }
25107
25108 /* See read.h. */
25109
25110 void
25111 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25112 {
25113 gdb_assert (this->get_cu (per_cu) == nullptr);
25114
25115 m_dwarf2_cus[per_cu] = cu;
25116 }
25117
25118 /* See read.h. */
25119
25120 void
25121 dwarf2_per_objfile::age_comp_units ()
25122 {
25123 dwarf_read_debug_printf_v ("running");
25124
25125 /* Start by clearing all marks. */
25126 for (auto pair : m_dwarf2_cus)
25127 pair.second->mark = false;
25128
25129 /* Traverse all CUs, mark them and their dependencies if used recently
25130 enough. */
25131 for (auto pair : m_dwarf2_cus)
25132 {
25133 dwarf2_cu *cu = pair.second;
25134
25135 cu->last_used++;
25136 if (cu->last_used <= dwarf_max_cache_age)
25137 dwarf2_mark (cu);
25138 }
25139
25140 /* Delete all CUs still not marked. */
25141 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25142 {
25143 dwarf2_cu *cu = it->second;
25144
25145 if (!cu->mark)
25146 {
25147 dwarf_read_debug_printf_v ("deleting old CU %s",
25148 sect_offset_str (cu->per_cu->sect_off));
25149 delete cu;
25150 it = m_dwarf2_cus.erase (it);
25151 }
25152 else
25153 it++;
25154 }
25155 }
25156
25157 /* See read.h. */
25158
25159 void
25160 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25161 {
25162 auto it = m_dwarf2_cus.find (per_cu);
25163 if (it == m_dwarf2_cus.end ())
25164 return;
25165
25166 delete it->second;
25167
25168 m_dwarf2_cus.erase (it);
25169 }
25170
25171 dwarf2_per_objfile::~dwarf2_per_objfile ()
25172 {
25173 remove_all_cus ();
25174 }
25175
25176 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25177 We store these in a hash table separate from the DIEs, and preserve them
25178 when the DIEs are flushed out of cache.
25179
25180 The CU "per_cu" pointer is needed because offset alone is not enough to
25181 uniquely identify the type. A file may have multiple .debug_types sections,
25182 or the type may come from a DWO file. Furthermore, while it's more logical
25183 to use per_cu->section+offset, with Fission the section with the data is in
25184 the DWO file but we don't know that section at the point we need it.
25185 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25186 because we can enter the lookup routine, get_die_type_at_offset, from
25187 outside this file, and thus won't necessarily have PER_CU->cu.
25188 Fortunately, PER_CU is stable for the life of the objfile. */
25189
25190 struct dwarf2_per_cu_offset_and_type
25191 {
25192 const struct dwarf2_per_cu_data *per_cu;
25193 sect_offset sect_off;
25194 struct type *type;
25195 };
25196
25197 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25198
25199 static hashval_t
25200 per_cu_offset_and_type_hash (const void *item)
25201 {
25202 const struct dwarf2_per_cu_offset_and_type *ofs
25203 = (const struct dwarf2_per_cu_offset_and_type *) item;
25204
25205 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25206 }
25207
25208 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25209
25210 static int
25211 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25212 {
25213 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25214 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25215 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25216 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25217
25218 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25219 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25220 }
25221
25222 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25223 table if necessary. For convenience, return TYPE.
25224
25225 The DIEs reading must have careful ordering to:
25226 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25227 reading current DIE.
25228 * Not trying to dereference contents of still incompletely read in types
25229 while reading in other DIEs.
25230 * Enable referencing still incompletely read in types just by a pointer to
25231 the type without accessing its fields.
25232
25233 Therefore caller should follow these rules:
25234 * Try to fetch any prerequisite types we may need to build this DIE type
25235 before building the type and calling set_die_type.
25236 * After building type call set_die_type for current DIE as soon as
25237 possible before fetching more types to complete the current type.
25238 * Make the type as complete as possible before fetching more types. */
25239
25240 static struct type *
25241 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25242 bool skip_data_location)
25243 {
25244 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25245 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25246 struct objfile *objfile = per_objfile->objfile;
25247 struct attribute *attr;
25248 struct dynamic_prop prop;
25249
25250 /* For Ada types, make sure that the gnat-specific data is always
25251 initialized (if not already set). There are a few types where
25252 we should not be doing so, because the type-specific area is
25253 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25254 where the type-specific area is used to store the floatformat).
25255 But this is not a problem, because the gnat-specific information
25256 is actually not needed for these types. */
25257 if (need_gnat_info (cu)
25258 && type->code () != TYPE_CODE_FUNC
25259 && type->code () != TYPE_CODE_FLT
25260 && type->code () != TYPE_CODE_METHODPTR
25261 && type->code () != TYPE_CODE_MEMBERPTR
25262 && type->code () != TYPE_CODE_METHOD
25263 && type->code () != TYPE_CODE_FIXED_POINT
25264 && !HAVE_GNAT_AUX_INFO (type))
25265 INIT_GNAT_SPECIFIC (type);
25266
25267 /* Read DW_AT_allocated and set in type. */
25268 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25269 if (attr != NULL)
25270 {
25271 struct type *prop_type = cu->addr_sized_int_type (false);
25272 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25273 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25274 }
25275
25276 /* Read DW_AT_associated and set in type. */
25277 attr = dwarf2_attr (die, DW_AT_associated, cu);
25278 if (attr != NULL)
25279 {
25280 struct type *prop_type = cu->addr_sized_int_type (false);
25281 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25282 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25283 }
25284
25285 /* Read DW_AT_data_location and set in type. */
25286 if (!skip_data_location)
25287 {
25288 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25289 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25290 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25291 }
25292
25293 if (per_objfile->die_type_hash == NULL)
25294 per_objfile->die_type_hash
25295 = htab_up (htab_create_alloc (127,
25296 per_cu_offset_and_type_hash,
25297 per_cu_offset_and_type_eq,
25298 NULL, xcalloc, xfree));
25299
25300 ofs.per_cu = cu->per_cu;
25301 ofs.sect_off = die->sect_off;
25302 ofs.type = type;
25303 slot = (struct dwarf2_per_cu_offset_and_type **)
25304 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25305 if (*slot)
25306 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25307 sect_offset_str (die->sect_off));
25308 *slot = XOBNEW (&objfile->objfile_obstack,
25309 struct dwarf2_per_cu_offset_and_type);
25310 **slot = ofs;
25311 return type;
25312 }
25313
25314 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25315 or return NULL if the die does not have a saved type. */
25316
25317 static struct type *
25318 get_die_type_at_offset (sect_offset sect_off,
25319 dwarf2_per_cu_data *per_cu,
25320 dwarf2_per_objfile *per_objfile)
25321 {
25322 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25323
25324 if (per_objfile->die_type_hash == NULL)
25325 return NULL;
25326
25327 ofs.per_cu = per_cu;
25328 ofs.sect_off = sect_off;
25329 slot = ((struct dwarf2_per_cu_offset_and_type *)
25330 htab_find (per_objfile->die_type_hash.get (), &ofs));
25331 if (slot)
25332 return slot->type;
25333 else
25334 return NULL;
25335 }
25336
25337 /* Look up the type for DIE in CU in die_type_hash,
25338 or return NULL if DIE does not have a saved type. */
25339
25340 static struct type *
25341 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25342 {
25343 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25344 }
25345
25346 /* Add a dependence relationship from CU to REF_PER_CU. */
25347
25348 static void
25349 dwarf2_add_dependence (struct dwarf2_cu *cu,
25350 struct dwarf2_per_cu_data *ref_per_cu)
25351 {
25352 void **slot;
25353
25354 if (cu->dependencies == NULL)
25355 cu->dependencies
25356 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25357 NULL, &cu->comp_unit_obstack,
25358 hashtab_obstack_allocate,
25359 dummy_obstack_deallocate);
25360
25361 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25362 if (*slot == NULL)
25363 *slot = ref_per_cu;
25364 }
25365
25366 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25367 Set the mark field in every compilation unit in the
25368 cache that we must keep because we are keeping CU.
25369
25370 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25371
25372 static int
25373 dwarf2_mark_helper (void **slot, void *data)
25374 {
25375 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25376 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25377 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25378
25379 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25380 reading of the chain. As such dependencies remain valid it is not much
25381 useful to track and undo them during QUIT cleanups. */
25382 if (cu == nullptr)
25383 return 1;
25384
25385 if (cu->mark)
25386 return 1;
25387
25388 cu->mark = true;
25389
25390 if (cu->dependencies != nullptr)
25391 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25392
25393 return 1;
25394 }
25395
25396 /* Set the mark field in CU and in every other compilation unit in the
25397 cache that we must keep because we are keeping CU. */
25398
25399 static void
25400 dwarf2_mark (struct dwarf2_cu *cu)
25401 {
25402 if (cu->mark)
25403 return;
25404
25405 cu->mark = true;
25406
25407 if (cu->dependencies != nullptr)
25408 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25409 }
25410
25411 /* Trivial hash function for partial_die_info: the hash value of a DIE
25412 is its offset in .debug_info for this objfile. */
25413
25414 static hashval_t
25415 partial_die_hash (const void *item)
25416 {
25417 const struct partial_die_info *part_die
25418 = (const struct partial_die_info *) item;
25419
25420 return to_underlying (part_die->sect_off);
25421 }
25422
25423 /* Trivial comparison function for partial_die_info structures: two DIEs
25424 are equal if they have the same offset. */
25425
25426 static int
25427 partial_die_eq (const void *item_lhs, const void *item_rhs)
25428 {
25429 const struct partial_die_info *part_die_lhs
25430 = (const struct partial_die_info *) item_lhs;
25431 const struct partial_die_info *part_die_rhs
25432 = (const struct partial_die_info *) item_rhs;
25433
25434 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25435 }
25436
25437 struct cmd_list_element *set_dwarf_cmdlist;
25438 struct cmd_list_element *show_dwarf_cmdlist;
25439
25440 static void
25441 show_check_physname (struct ui_file *file, int from_tty,
25442 struct cmd_list_element *c, const char *value)
25443 {
25444 fprintf_filtered (file,
25445 _("Whether to check \"physname\" is %s.\n"),
25446 value);
25447 }
25448
25449 void _initialize_dwarf2_read ();
25450 void
25451 _initialize_dwarf2_read ()
25452 {
25453 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25454 Set DWARF specific variables.\n\
25455 Configure DWARF variables such as the cache size."),
25456 &set_dwarf_cmdlist, "maintenance set dwarf ",
25457 0/*allow-unknown*/, &maintenance_set_cmdlist);
25458
25459 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25460 Show DWARF specific variables.\n\
25461 Show DWARF variables such as the cache size."),
25462 &show_dwarf_cmdlist, "maintenance show dwarf ",
25463 0/*allow-unknown*/, &maintenance_show_cmdlist);
25464
25465 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25466 &dwarf_max_cache_age, _("\
25467 Set the upper bound on the age of cached DWARF compilation units."), _("\
25468 Show the upper bound on the age of cached DWARF compilation units."), _("\
25469 A higher limit means that cached compilation units will be stored\n\
25470 in memory longer, and more total memory will be used. Zero disables\n\
25471 caching, which can slow down startup."),
25472 NULL,
25473 show_dwarf_max_cache_age,
25474 &set_dwarf_cmdlist,
25475 &show_dwarf_cmdlist);
25476
25477 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25478 Set debugging of the DWARF reader."), _("\
25479 Show debugging of the DWARF reader."), _("\
25480 When enabled (non-zero), debugging messages are printed during DWARF\n\
25481 reading and symtab expansion. A value of 1 (one) provides basic\n\
25482 information. A value greater than 1 provides more verbose information."),
25483 NULL,
25484 NULL,
25485 &setdebuglist, &showdebuglist);
25486
25487 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25488 Set debugging of the DWARF DIE reader."), _("\
25489 Show debugging of the DWARF DIE reader."), _("\
25490 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25491 The value is the maximum depth to print."),
25492 NULL,
25493 NULL,
25494 &setdebuglist, &showdebuglist);
25495
25496 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25497 Set debugging of the dwarf line reader."), _("\
25498 Show debugging of the dwarf line reader."), _("\
25499 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25500 A value of 1 (one) provides basic information.\n\
25501 A value greater than 1 provides more verbose information."),
25502 NULL,
25503 NULL,
25504 &setdebuglist, &showdebuglist);
25505
25506 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25507 Set cross-checking of \"physname\" code against demangler."), _("\
25508 Show cross-checking of \"physname\" code against demangler."), _("\
25509 When enabled, GDB's internal \"physname\" code is checked against\n\
25510 the demangler."),
25511 NULL, show_check_physname,
25512 &setdebuglist, &showdebuglist);
25513
25514 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25515 no_class, &use_deprecated_index_sections, _("\
25516 Set whether to use deprecated gdb_index sections."), _("\
25517 Show whether to use deprecated gdb_index sections."), _("\
25518 When enabled, deprecated .gdb_index sections are used anyway.\n\
25519 Normally they are ignored either because of a missing feature or\n\
25520 performance issue.\n\
25521 Warning: This option must be enabled before gdb reads the file."),
25522 NULL,
25523 NULL,
25524 &setlist, &showlist);
25525
25526 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25527 &dwarf2_locexpr_funcs);
25528 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25529 &dwarf2_loclist_funcs);
25530
25531 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25532 &dwarf2_block_frame_base_locexpr_funcs);
25533 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25534 &dwarf2_block_frame_base_loclist_funcs);
25535
25536 #if GDB_SELF_TEST
25537 selftests::register_test ("dw2_expand_symtabs_matching",
25538 selftests::dw2_expand_symtabs_matching::run_test);
25539 selftests::register_test ("dwarf2_find_containing_comp_unit",
25540 selftests::find_containing_comp_unit::run_test);
25541 #endif
25542 }