gdb/dwarf: make read_{loc,rng}list_index return sect_offset
[deliverable/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 of
5345 the next CU as end of this CU. We create the CUs here with length 0,
5346 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5347 dwarf2_per_cu_data *per_cu
5348 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5349 per_bfd->all_comp_units.push_back (per_cu);
5350 }
5351 }
5352
5353 sect_offset sect_off_prev;
5354 for (uint32_t i = 0; i <= map.cu_count; ++i)
5355 {
5356 sect_offset sect_off_next;
5357 if (i < map.cu_count)
5358 {
5359 sect_off_next
5360 = (sect_offset) (extract_unsigned_integer
5361 (map.cu_table_reordered + i * map.offset_size,
5362 map.offset_size,
5363 map.dwarf5_byte_order));
5364 }
5365 else
5366 sect_off_next = (sect_offset) section.size;
5367 if (i >= 1)
5368 {
5369 const ULONGEST length = sect_off_next - sect_off_prev;
5370 dwarf2_per_cu_data *per_cu
5371 = create_cu_from_index_list (per_bfd, &section, is_dwz,
5372 sect_off_prev, length);
5373 per_bfd->all_comp_units.push_back (per_cu);
5374 }
5375 sect_off_prev = sect_off_next;
5376 }
5377 }
5378
5379 /* Read the CU list from the mapped index, and use it to create all
5380 the CU objects for this dwarf2_per_objfile. */
5381
5382 static void
5383 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5384 const mapped_debug_names &map,
5385 const mapped_debug_names &dwz_map)
5386 {
5387 gdb_assert (per_bfd->all_comp_units.empty ());
5388 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5389
5390 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5391 false /* is_dwz */);
5392
5393 if (dwz_map.cu_count == 0)
5394 return;
5395
5396 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5397 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5398 true /* is_dwz */);
5399 }
5400
5401 /* Read .debug_names. If everything went ok, initialize the "quick"
5402 elements of all the CUs and return true. Otherwise, return false. */
5403
5404 static bool
5405 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5406 {
5407 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5408 mapped_debug_names dwz_map;
5409 struct objfile *objfile = per_objfile->objfile;
5410 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5411
5412 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5413 &per_objfile->per_bfd->debug_names, *map))
5414 return false;
5415
5416 /* Don't use the index if it's empty. */
5417 if (map->name_count == 0)
5418 return false;
5419
5420 /* If there is a .dwz file, read it so we can get its CU list as
5421 well. */
5422 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5423 if (dwz != NULL)
5424 {
5425 if (!read_debug_names_from_section (objfile,
5426 bfd_get_filename (dwz->dwz_bfd.get ()),
5427 &dwz->debug_names, dwz_map))
5428 {
5429 warning (_("could not read '.debug_names' section from %s; skipping"),
5430 bfd_get_filename (dwz->dwz_bfd.get ()));
5431 return false;
5432 }
5433 }
5434
5435 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5436
5437 if (map->tu_count != 0)
5438 {
5439 /* We can only handle a single .debug_types when we have an
5440 index. */
5441 if (per_bfd->types.size () != 1)
5442 return false;
5443
5444 dwarf2_section_info *section = &per_bfd->types[0];
5445
5446 create_signatured_type_table_from_debug_names
5447 (per_objfile, *map, section, &per_bfd->abbrev);
5448 }
5449
5450 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5451
5452 per_bfd->debug_names_table = std::move (map);
5453 per_bfd->using_index = 1;
5454 per_bfd->quick_file_names_table =
5455 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
5456
5457 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5458 objfiles using the same BFD. */
5459 gdb_assert (per_bfd->partial_symtabs == nullptr);
5460 per_bfd->partial_symtabs = objfile->partial_symtabs;
5461
5462 return true;
5463 }
5464
5465 /* Type used to manage iterating over all CUs looking for a symbol for
5466 .debug_names. */
5467
5468 class dw2_debug_names_iterator
5469 {
5470 public:
5471 dw2_debug_names_iterator (const mapped_debug_names &map,
5472 gdb::optional<block_enum> block_index,
5473 domain_enum domain,
5474 const char *name, dwarf2_per_objfile *per_objfile)
5475 : m_map (map), m_block_index (block_index), m_domain (domain),
5476 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5477 m_per_objfile (per_objfile)
5478 {}
5479
5480 dw2_debug_names_iterator (const mapped_debug_names &map,
5481 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
5482 : m_map (map),
5483 m_search (search),
5484 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5485 m_per_objfile (per_objfile)
5486 {}
5487
5488 dw2_debug_names_iterator (const mapped_debug_names &map,
5489 block_enum block_index, domain_enum domain,
5490 uint32_t namei, dwarf2_per_objfile *per_objfile)
5491 : m_map (map), m_block_index (block_index), m_domain (domain),
5492 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5493 m_per_objfile (per_objfile)
5494 {}
5495
5496 /* Return the next matching CU or NULL if there are no more. */
5497 dwarf2_per_cu_data *next ();
5498
5499 private:
5500 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5501 const char *name,
5502 dwarf2_per_objfile *per_objfile);
5503 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5504 uint32_t namei,
5505 dwarf2_per_objfile *per_objfile);
5506
5507 /* The internalized form of .debug_names. */
5508 const mapped_debug_names &m_map;
5509
5510 /* If set, only look for symbols that match that block. Valid values are
5511 GLOBAL_BLOCK and STATIC_BLOCK. */
5512 const gdb::optional<block_enum> m_block_index;
5513
5514 /* The kind of symbol we're looking for. */
5515 const domain_enum m_domain = UNDEF_DOMAIN;
5516 const search_domain m_search = ALL_DOMAIN;
5517
5518 /* The list of CUs from the index entry of the symbol, or NULL if
5519 not found. */
5520 const gdb_byte *m_addr;
5521
5522 dwarf2_per_objfile *m_per_objfile;
5523 };
5524
5525 const char *
5526 mapped_debug_names::namei_to_name
5527 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5528 {
5529 const ULONGEST namei_string_offs
5530 = extract_unsigned_integer ((name_table_string_offs_reordered
5531 + namei * offset_size),
5532 offset_size,
5533 dwarf5_byte_order);
5534 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5535 }
5536
5537 /* Find a slot in .debug_names for the object named NAME. If NAME is
5538 found, return pointer to its pool data. If NAME cannot be found,
5539 return NULL. */
5540
5541 const gdb_byte *
5542 dw2_debug_names_iterator::find_vec_in_debug_names
5543 (const mapped_debug_names &map, const char *name,
5544 dwarf2_per_objfile *per_objfile)
5545 {
5546 int (*cmp) (const char *, const char *);
5547
5548 gdb::unique_xmalloc_ptr<char> without_params;
5549 if (current_language->la_language == language_cplus
5550 || current_language->la_language == language_fortran
5551 || current_language->la_language == language_d)
5552 {
5553 /* NAME is already canonical. Drop any qualifiers as
5554 .debug_names does not contain any. */
5555
5556 if (strchr (name, '(') != NULL)
5557 {
5558 without_params = cp_remove_params (name);
5559 if (without_params != NULL)
5560 name = without_params.get ();
5561 }
5562 }
5563
5564 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5565
5566 const uint32_t full_hash = dwarf5_djb_hash (name);
5567 uint32_t namei
5568 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5569 (map.bucket_table_reordered
5570 + (full_hash % map.bucket_count)), 4,
5571 map.dwarf5_byte_order);
5572 if (namei == 0)
5573 return NULL;
5574 --namei;
5575 if (namei >= map.name_count)
5576 {
5577 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5578 "[in module %s]"),
5579 namei, map.name_count,
5580 objfile_name (per_objfile->objfile));
5581 return NULL;
5582 }
5583
5584 for (;;)
5585 {
5586 const uint32_t namei_full_hash
5587 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5588 (map.hash_table_reordered + namei), 4,
5589 map.dwarf5_byte_order);
5590 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5591 return NULL;
5592
5593 if (full_hash == namei_full_hash)
5594 {
5595 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5596
5597 #if 0 /* An expensive sanity check. */
5598 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5599 {
5600 complaint (_("Wrong .debug_names hash for string at index %u "
5601 "[in module %s]"),
5602 namei, objfile_name (dwarf2_per_objfile->objfile));
5603 return NULL;
5604 }
5605 #endif
5606
5607 if (cmp (namei_string, name) == 0)
5608 {
5609 const ULONGEST namei_entry_offs
5610 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5611 + namei * map.offset_size),
5612 map.offset_size, map.dwarf5_byte_order);
5613 return map.entry_pool + namei_entry_offs;
5614 }
5615 }
5616
5617 ++namei;
5618 if (namei >= map.name_count)
5619 return NULL;
5620 }
5621 }
5622
5623 const gdb_byte *
5624 dw2_debug_names_iterator::find_vec_in_debug_names
5625 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5626 {
5627 if (namei >= map.name_count)
5628 {
5629 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5630 "[in module %s]"),
5631 namei, map.name_count,
5632 objfile_name (per_objfile->objfile));
5633 return NULL;
5634 }
5635
5636 const ULONGEST namei_entry_offs
5637 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5638 + namei * map.offset_size),
5639 map.offset_size, map.dwarf5_byte_order);
5640 return map.entry_pool + namei_entry_offs;
5641 }
5642
5643 /* See dw2_debug_names_iterator. */
5644
5645 dwarf2_per_cu_data *
5646 dw2_debug_names_iterator::next ()
5647 {
5648 if (m_addr == NULL)
5649 return NULL;
5650
5651 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5652 struct objfile *objfile = m_per_objfile->objfile;
5653 bfd *const abfd = objfile->obfd;
5654
5655 again:
5656
5657 unsigned int bytes_read;
5658 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5659 m_addr += bytes_read;
5660 if (abbrev == 0)
5661 return NULL;
5662
5663 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5664 if (indexval_it == m_map.abbrev_map.cend ())
5665 {
5666 complaint (_("Wrong .debug_names undefined abbrev code %s "
5667 "[in module %s]"),
5668 pulongest (abbrev), objfile_name (objfile));
5669 return NULL;
5670 }
5671 const mapped_debug_names::index_val &indexval = indexval_it->second;
5672 enum class symbol_linkage {
5673 unknown,
5674 static_,
5675 extern_,
5676 } symbol_linkage_ = symbol_linkage::unknown;
5677 dwarf2_per_cu_data *per_cu = NULL;
5678 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5679 {
5680 ULONGEST ull;
5681 switch (attr.form)
5682 {
5683 case DW_FORM_implicit_const:
5684 ull = attr.implicit_const;
5685 break;
5686 case DW_FORM_flag_present:
5687 ull = 1;
5688 break;
5689 case DW_FORM_udata:
5690 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5691 m_addr += bytes_read;
5692 break;
5693 case DW_FORM_ref4:
5694 ull = read_4_bytes (abfd, m_addr);
5695 m_addr += 4;
5696 break;
5697 case DW_FORM_ref8:
5698 ull = read_8_bytes (abfd, m_addr);
5699 m_addr += 8;
5700 break;
5701 case DW_FORM_ref_sig8:
5702 ull = read_8_bytes (abfd, m_addr);
5703 m_addr += 8;
5704 break;
5705 default:
5706 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5707 dwarf_form_name (attr.form),
5708 objfile_name (objfile));
5709 return NULL;
5710 }
5711 switch (attr.dw_idx)
5712 {
5713 case DW_IDX_compile_unit:
5714 /* Don't crash on bad data. */
5715 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
5716 {
5717 complaint (_(".debug_names entry has bad CU index %s"
5718 " [in module %s]"),
5719 pulongest (ull),
5720 objfile_name (objfile));
5721 continue;
5722 }
5723 per_cu = per_bfd->get_cutu (ull);
5724 break;
5725 case DW_IDX_type_unit:
5726 /* Don't crash on bad data. */
5727 if (ull >= per_bfd->all_type_units.size ())
5728 {
5729 complaint (_(".debug_names entry has bad TU index %s"
5730 " [in module %s]"),
5731 pulongest (ull),
5732 objfile_name (objfile));
5733 continue;
5734 }
5735 per_cu = &per_bfd->get_tu (ull)->per_cu;
5736 break;
5737 case DW_IDX_die_offset:
5738 /* In a per-CU index (as opposed to a per-module index), index
5739 entries without CU attribute implicitly refer to the single CU. */
5740 if (per_cu == NULL)
5741 per_cu = per_bfd->get_cu (0);
5742 break;
5743 case DW_IDX_GNU_internal:
5744 if (!m_map.augmentation_is_gdb)
5745 break;
5746 symbol_linkage_ = symbol_linkage::static_;
5747 break;
5748 case DW_IDX_GNU_external:
5749 if (!m_map.augmentation_is_gdb)
5750 break;
5751 symbol_linkage_ = symbol_linkage::extern_;
5752 break;
5753 }
5754 }
5755
5756 /* Skip if already read in. */
5757 if (m_per_objfile->symtab_set_p (per_cu))
5758 goto again;
5759
5760 /* Check static vs global. */
5761 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
5762 {
5763 const bool want_static = *m_block_index == STATIC_BLOCK;
5764 const bool symbol_is_static =
5765 symbol_linkage_ == symbol_linkage::static_;
5766 if (want_static != symbol_is_static)
5767 goto again;
5768 }
5769
5770 /* Match dw2_symtab_iter_next, symbol_kind
5771 and debug_names::psymbol_tag. */
5772 switch (m_domain)
5773 {
5774 case VAR_DOMAIN:
5775 switch (indexval.dwarf_tag)
5776 {
5777 case DW_TAG_variable:
5778 case DW_TAG_subprogram:
5779 /* Some types are also in VAR_DOMAIN. */
5780 case DW_TAG_typedef:
5781 case DW_TAG_structure_type:
5782 break;
5783 default:
5784 goto again;
5785 }
5786 break;
5787 case STRUCT_DOMAIN:
5788 switch (indexval.dwarf_tag)
5789 {
5790 case DW_TAG_typedef:
5791 case DW_TAG_structure_type:
5792 break;
5793 default:
5794 goto again;
5795 }
5796 break;
5797 case LABEL_DOMAIN:
5798 switch (indexval.dwarf_tag)
5799 {
5800 case 0:
5801 case DW_TAG_variable:
5802 break;
5803 default:
5804 goto again;
5805 }
5806 break;
5807 case MODULE_DOMAIN:
5808 switch (indexval.dwarf_tag)
5809 {
5810 case DW_TAG_module:
5811 break;
5812 default:
5813 goto again;
5814 }
5815 break;
5816 default:
5817 break;
5818 }
5819
5820 /* Match dw2_expand_symtabs_matching, symbol_kind and
5821 debug_names::psymbol_tag. */
5822 switch (m_search)
5823 {
5824 case VARIABLES_DOMAIN:
5825 switch (indexval.dwarf_tag)
5826 {
5827 case DW_TAG_variable:
5828 break;
5829 default:
5830 goto again;
5831 }
5832 break;
5833 case FUNCTIONS_DOMAIN:
5834 switch (indexval.dwarf_tag)
5835 {
5836 case DW_TAG_subprogram:
5837 break;
5838 default:
5839 goto again;
5840 }
5841 break;
5842 case TYPES_DOMAIN:
5843 switch (indexval.dwarf_tag)
5844 {
5845 case DW_TAG_typedef:
5846 case DW_TAG_structure_type:
5847 break;
5848 default:
5849 goto again;
5850 }
5851 break;
5852 case MODULES_DOMAIN:
5853 switch (indexval.dwarf_tag)
5854 {
5855 case DW_TAG_module:
5856 break;
5857 default:
5858 goto again;
5859 }
5860 default:
5861 break;
5862 }
5863
5864 return per_cu;
5865 }
5866
5867 static struct compunit_symtab *
5868 dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
5869 const char *name, domain_enum domain)
5870 {
5871 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5872
5873 const auto &mapp = per_objfile->per_bfd->debug_names_table;
5874 if (!mapp)
5875 {
5876 /* index is NULL if OBJF_READNOW. */
5877 return NULL;
5878 }
5879 const auto &map = *mapp;
5880
5881 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
5882
5883 struct compunit_symtab *stab_best = NULL;
5884 struct dwarf2_per_cu_data *per_cu;
5885 while ((per_cu = iter.next ()) != NULL)
5886 {
5887 struct symbol *sym, *with_opaque = NULL;
5888 compunit_symtab *stab
5889 = dw2_instantiate_symtab (per_cu, per_objfile, false);
5890 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
5891 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
5892
5893 sym = block_find_symbol (block, name, domain,
5894 block_find_non_opaque_type_preferred,
5895 &with_opaque);
5896
5897 /* Some caution must be observed with overloaded functions and
5898 methods, since the index will not contain any overload
5899 information (but NAME might contain it). */
5900
5901 if (sym != NULL
5902 && strcmp_iw (sym->search_name (), name) == 0)
5903 return stab;
5904 if (with_opaque != NULL
5905 && strcmp_iw (with_opaque->search_name (), name) == 0)
5906 stab_best = stab;
5907
5908 /* Keep looking through other CUs. */
5909 }
5910
5911 return stab_best;
5912 }
5913
5914 /* This dumps minimal information about .debug_names. It is called
5915 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5916 uses this to verify that .debug_names has been loaded. */
5917
5918 static void
5919 dw2_debug_names_dump (struct objfile *objfile)
5920 {
5921 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5922
5923 gdb_assert (per_objfile->per_bfd->using_index);
5924 printf_filtered (".debug_names:");
5925 if (per_objfile->per_bfd->debug_names_table)
5926 printf_filtered (" exists\n");
5927 else
5928 printf_filtered (" faked for \"readnow\"\n");
5929 printf_filtered ("\n");
5930 }
5931
5932 static void
5933 dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5934 const char *func_name)
5935 {
5936 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5937
5938 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5939 if (per_objfile->per_bfd->debug_names_table)
5940 {
5941 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5942
5943 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
5944 per_objfile);
5945
5946 struct dwarf2_per_cu_data *per_cu;
5947 while ((per_cu = iter.next ()) != NULL)
5948 dw2_instantiate_symtab (per_cu, per_objfile, false);
5949 }
5950 }
5951
5952 static void
5953 dw2_debug_names_map_matching_symbols
5954 (struct objfile *objfile,
5955 const lookup_name_info &name, domain_enum domain,
5956 int global,
5957 gdb::function_view<symbol_found_callback_ftype> callback,
5958 symbol_compare_ftype *ordered_compare)
5959 {
5960 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5961
5962 /* debug_names_table is NULL if OBJF_READNOW. */
5963 if (!per_objfile->per_bfd->debug_names_table)
5964 return;
5965
5966 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5967 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5968
5969 const char *match_name = name.ada ().lookup_name ().c_str ();
5970 auto matcher = [&] (const char *symname)
5971 {
5972 if (ordered_compare == nullptr)
5973 return true;
5974 return ordered_compare (symname, match_name) == 0;
5975 };
5976
5977 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5978 [&] (offset_type namei)
5979 {
5980 /* The name was matched, now expand corresponding CUs that were
5981 marked. */
5982 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
5983 per_objfile);
5984
5985 struct dwarf2_per_cu_data *per_cu;
5986 while ((per_cu = iter.next ()) != NULL)
5987 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5988 nullptr);
5989 return true;
5990 }, per_objfile);
5991
5992 /* It's a shame we couldn't do this inside the
5993 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5994 that have already been expanded. Instead, this loop matches what
5995 the psymtab code does. */
5996 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5997 {
5998 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5999 if (symtab != nullptr)
6000 {
6001 const struct block *block
6002 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
6003 if (!iterate_over_symbols_terminated (block, name,
6004 domain, callback))
6005 break;
6006 }
6007 }
6008 }
6009
6010 static void
6011 dw2_debug_names_expand_symtabs_matching
6012 (struct objfile *objfile,
6013 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6014 const lookup_name_info *lookup_name,
6015 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6016 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6017 enum search_domain kind)
6018 {
6019 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6020
6021 /* debug_names_table is NULL if OBJF_READNOW. */
6022 if (!per_objfile->per_bfd->debug_names_table)
6023 return;
6024
6025 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
6026
6027 if (symbol_matcher == NULL && lookup_name == NULL)
6028 {
6029 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
6030 {
6031 QUIT;
6032
6033 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6034 expansion_notify);
6035 }
6036 return;
6037 }
6038
6039 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
6040
6041 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
6042 symbol_matcher,
6043 kind, [&] (offset_type namei)
6044 {
6045 /* The name was matched, now expand corresponding CUs that were
6046 marked. */
6047 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
6048
6049 struct dwarf2_per_cu_data *per_cu;
6050 while ((per_cu = iter.next ()) != NULL)
6051 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6052 expansion_notify);
6053 return true;
6054 }, per_objfile);
6055 }
6056
6057 const struct quick_symbol_functions dwarf2_debug_names_functions =
6058 {
6059 dw2_has_symbols,
6060 dw2_find_last_source_symtab,
6061 dw2_forget_cached_source_info,
6062 dw2_map_symtabs_matching_filename,
6063 dw2_debug_names_lookup_symbol,
6064 NULL,
6065 dw2_print_stats,
6066 dw2_debug_names_dump,
6067 dw2_debug_names_expand_symtabs_for_function,
6068 dw2_expand_all_symtabs,
6069 dw2_expand_symtabs_with_fullname,
6070 dw2_debug_names_map_matching_symbols,
6071 dw2_debug_names_expand_symtabs_matching,
6072 dw2_find_pc_sect_compunit_symtab,
6073 NULL,
6074 dw2_map_symbol_filenames
6075 };
6076
6077 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6078 to either a dwarf2_per_bfd or dwz_file object. */
6079
6080 template <typename T>
6081 static gdb::array_view<const gdb_byte>
6082 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6083 {
6084 dwarf2_section_info *section = &section_owner->gdb_index;
6085
6086 if (section->empty ())
6087 return {};
6088
6089 /* Older elfutils strip versions could keep the section in the main
6090 executable while splitting it for the separate debug info file. */
6091 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
6092 return {};
6093
6094 section->read (obj);
6095
6096 /* dwarf2_section_info::size is a bfd_size_type, while
6097 gdb::array_view works with size_t. On 32-bit hosts, with
6098 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6099 is 32-bit. So we need an explicit narrowing conversion here.
6100 This is fine, because it's impossible to allocate or mmap an
6101 array/buffer larger than what size_t can represent. */
6102 return gdb::make_array_view (section->buffer, section->size);
6103 }
6104
6105 /* Lookup the index cache for the contents of the index associated to
6106 DWARF2_OBJ. */
6107
6108 static gdb::array_view<const gdb_byte>
6109 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
6110 {
6111 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6112 if (build_id == nullptr)
6113 return {};
6114
6115 return global_index_cache.lookup_gdb_index (build_id,
6116 &dwarf2_per_bfd->index_cache_res);
6117 }
6118
6119 /* Same as the above, but for DWZ. */
6120
6121 static gdb::array_view<const gdb_byte>
6122 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6123 {
6124 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6125 if (build_id == nullptr)
6126 return {};
6127
6128 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6129 }
6130
6131 /* See symfile.h. */
6132
6133 bool
6134 dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
6135 {
6136 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6137 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6138
6139 /* If we're about to read full symbols, don't bother with the
6140 indices. In this case we also don't care if some other debug
6141 format is making psymtabs, because they are all about to be
6142 expanded anyway. */
6143 if ((objfile->flags & OBJF_READNOW))
6144 {
6145 /* When using READNOW, the using_index flag (set below) indicates that
6146 PER_BFD was already initialized, when we loaded some other objfile. */
6147 if (per_bfd->using_index)
6148 {
6149 *index_kind = dw_index_kind::GDB_INDEX;
6150 per_objfile->resize_symtabs ();
6151 return true;
6152 }
6153
6154 per_bfd->using_index = 1;
6155 create_all_comp_units (per_objfile);
6156 create_all_type_units (per_objfile);
6157 per_bfd->quick_file_names_table
6158 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
6159 per_objfile->resize_symtabs ();
6160
6161 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6162 + per_bfd->all_type_units.size ()); ++i)
6163 {
6164 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
6165
6166 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
6167 struct dwarf2_per_cu_quick_data);
6168 }
6169
6170 /* Return 1 so that gdb sees the "quick" functions. However,
6171 these functions will be no-ops because we will have expanded
6172 all symtabs. */
6173 *index_kind = dw_index_kind::GDB_INDEX;
6174 return true;
6175 }
6176
6177 /* Was a debug names index already read when we processed an objfile sharing
6178 PER_BFD? */
6179 if (per_bfd->debug_names_table != nullptr)
6180 {
6181 *index_kind = dw_index_kind::DEBUG_NAMES;
6182 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6183 per_objfile->resize_symtabs ();
6184 return true;
6185 }
6186
6187 /* Was a GDB index already read when we processed an objfile sharing
6188 PER_BFD? */
6189 if (per_bfd->index_table != nullptr)
6190 {
6191 *index_kind = dw_index_kind::GDB_INDEX;
6192 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
6193 per_objfile->resize_symtabs ();
6194 return true;
6195 }
6196
6197 /* There might already be partial symtabs built for this BFD. This happens
6198 when loading the same binary twice with the index-cache enabled. If so,
6199 don't try to read an index. The objfile / per_objfile initialization will
6200 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6201 code path. */
6202 if (per_bfd->partial_symtabs != nullptr)
6203 return false;
6204
6205 if (dwarf2_read_debug_names (per_objfile))
6206 {
6207 *index_kind = dw_index_kind::DEBUG_NAMES;
6208 per_objfile->resize_symtabs ();
6209 return true;
6210 }
6211
6212 if (dwarf2_read_gdb_index (per_objfile,
6213 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
6214 get_gdb_index_contents_from_section<dwz_file>))
6215 {
6216 *index_kind = dw_index_kind::GDB_INDEX;
6217 per_objfile->resize_symtabs ();
6218 return true;
6219 }
6220
6221 /* ... otherwise, try to find the index in the index cache. */
6222 if (dwarf2_read_gdb_index (per_objfile,
6223 get_gdb_index_contents_from_cache,
6224 get_gdb_index_contents_from_cache_dwz))
6225 {
6226 global_index_cache.hit ();
6227 *index_kind = dw_index_kind::GDB_INDEX;
6228 per_objfile->resize_symtabs ();
6229 return true;
6230 }
6231
6232 global_index_cache.miss ();
6233 return false;
6234 }
6235
6236 \f
6237
6238 /* Build a partial symbol table. */
6239
6240 void
6241 dwarf2_build_psymtabs (struct objfile *objfile)
6242 {
6243 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6244 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6245
6246 if (per_bfd->partial_symtabs != nullptr)
6247 {
6248 /* Partial symbols were already read, so now we can simply
6249 attach them. */
6250 objfile->partial_symtabs = per_bfd->partial_symtabs;
6251 per_objfile->resize_symtabs ();
6252 return;
6253 }
6254
6255 try
6256 {
6257 /* This isn't really ideal: all the data we allocate on the
6258 objfile's obstack is still uselessly kept around. However,
6259 freeing it seems unsafe. */
6260 psymtab_discarder psymtabs (objfile);
6261 dwarf2_build_psymtabs_hard (per_objfile);
6262 psymtabs.keep ();
6263
6264 per_objfile->resize_symtabs ();
6265
6266 /* (maybe) store an index in the cache. */
6267 global_index_cache.store (per_objfile);
6268 }
6269 catch (const gdb_exception_error &except)
6270 {
6271 exception_print (gdb_stderr, except);
6272 }
6273
6274 /* Finish by setting the local reference to partial symtabs, so that
6275 we don't try to read them again if reading another objfile with the same
6276 BFD. If we can't in fact share, this won't make a difference anyway as
6277 the dwarf2_per_bfd object won't be shared. */
6278 per_bfd->partial_symtabs = objfile->partial_symtabs;
6279 }
6280
6281 /* Find the base address of the compilation unit for range lists and
6282 location lists. It will normally be specified by DW_AT_low_pc.
6283 In DWARF-3 draft 4, the base address could be overridden by
6284 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6285 compilation units with discontinuous ranges. */
6286
6287 static void
6288 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6289 {
6290 struct attribute *attr;
6291
6292 cu->base_address.reset ();
6293
6294 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6295 if (attr != nullptr)
6296 cu->base_address = attr->as_address ();
6297 else
6298 {
6299 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6300 if (attr != nullptr)
6301 cu->base_address = attr->as_address ();
6302 }
6303 }
6304
6305 /* Helper function that returns the proper abbrev section for
6306 THIS_CU. */
6307
6308 static struct dwarf2_section_info *
6309 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6310 {
6311 struct dwarf2_section_info *abbrev;
6312 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
6313
6314 if (this_cu->is_dwz)
6315 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
6316 else
6317 abbrev = &per_bfd->abbrev;
6318
6319 return abbrev;
6320 }
6321
6322 /* Fetch the abbreviation table offset from a comp or type unit header. */
6323
6324 static sect_offset
6325 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
6326 struct dwarf2_section_info *section,
6327 sect_offset sect_off)
6328 {
6329 bfd *abfd = section->get_bfd_owner ();
6330 const gdb_byte *info_ptr;
6331 unsigned int initial_length_size, offset_size;
6332 uint16_t version;
6333
6334 section->read (per_objfile->objfile);
6335 info_ptr = section->buffer + to_underlying (sect_off);
6336 read_initial_length (abfd, info_ptr, &initial_length_size);
6337 offset_size = initial_length_size == 4 ? 4 : 8;
6338 info_ptr += initial_length_size;
6339
6340 version = read_2_bytes (abfd, info_ptr);
6341 info_ptr += 2;
6342 if (version >= 5)
6343 {
6344 /* Skip unit type and address size. */
6345 info_ptr += 2;
6346 }
6347
6348 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
6349 }
6350
6351 /* A partial symtab that is used only for include files. */
6352 struct dwarf2_include_psymtab : public partial_symtab
6353 {
6354 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6355 : partial_symtab (filename, objfile)
6356 {
6357 }
6358
6359 void read_symtab (struct objfile *objfile) override
6360 {
6361 /* It's an include file, no symbols to read for it.
6362 Everything is in the includer symtab. */
6363
6364 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6365 expansion of the includer psymtab. We use the dependencies[0] field to
6366 model the includer. But if we go the regular route of calling
6367 expand_psymtab here, and having expand_psymtab call expand_dependencies
6368 to expand the includer, we'll only use expand_psymtab on the includer
6369 (making it a non-toplevel psymtab), while if we expand the includer via
6370 another path, we'll use read_symtab (making it a toplevel psymtab).
6371 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6372 psymtab, and trigger read_symtab on the includer here directly. */
6373 includer ()->read_symtab (objfile);
6374 }
6375
6376 void expand_psymtab (struct objfile *objfile) override
6377 {
6378 /* This is not called by read_symtab, and should not be called by any
6379 expand_dependencies. */
6380 gdb_assert (false);
6381 }
6382
6383 bool readin_p (struct objfile *objfile) const override
6384 {
6385 return includer ()->readin_p (objfile);
6386 }
6387
6388 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
6389 {
6390 return nullptr;
6391 }
6392
6393 private:
6394 partial_symtab *includer () const
6395 {
6396 /* An include psymtab has exactly one dependency: the psymtab that
6397 includes it. */
6398 gdb_assert (this->number_of_dependencies == 1);
6399 return this->dependencies[0];
6400 }
6401 };
6402
6403 /* Allocate a new partial symtab for file named NAME and mark this new
6404 partial symtab as being an include of PST. */
6405
6406 static void
6407 dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
6408 struct objfile *objfile)
6409 {
6410 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
6411
6412 if (!IS_ABSOLUTE_PATH (subpst->filename))
6413 subpst->dirname = pst->dirname;
6414
6415 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
6416 subpst->dependencies[0] = pst;
6417 subpst->number_of_dependencies = 1;
6418 }
6419
6420 /* Read the Line Number Program data and extract the list of files
6421 included by the source file represented by PST. Build an include
6422 partial symtab for each of these included files. */
6423
6424 static void
6425 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6426 struct die_info *die,
6427 dwarf2_psymtab *pst)
6428 {
6429 line_header_up lh;
6430 struct attribute *attr;
6431
6432 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6433 if (attr != nullptr && attr->form_is_unsigned ())
6434 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6435 if (lh == NULL)
6436 return; /* No linetable, so no includes. */
6437
6438 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6439 that we pass in the raw text_low here; that is ok because we're
6440 only decoding the line table to make include partial symtabs, and
6441 so the addresses aren't really used. */
6442 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6443 pst->raw_text_low (), 1);
6444 }
6445
6446 static hashval_t
6447 hash_signatured_type (const void *item)
6448 {
6449 const struct signatured_type *sig_type
6450 = (const struct signatured_type *) item;
6451
6452 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6453 return sig_type->signature;
6454 }
6455
6456 static int
6457 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6458 {
6459 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6460 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6461
6462 return lhs->signature == rhs->signature;
6463 }
6464
6465 /* Allocate a hash table for signatured types. */
6466
6467 static htab_up
6468 allocate_signatured_type_table ()
6469 {
6470 return htab_up (htab_create_alloc (41,
6471 hash_signatured_type,
6472 eq_signatured_type,
6473 NULL, xcalloc, xfree));
6474 }
6475
6476 /* A helper function to add a signatured type CU to a table. */
6477
6478 static int
6479 add_signatured_type_cu_to_table (void **slot, void *datum)
6480 {
6481 struct signatured_type *sigt = (struct signatured_type *) *slot;
6482 std::vector<signatured_type *> *all_type_units
6483 = (std::vector<signatured_type *> *) datum;
6484
6485 all_type_units->push_back (sigt);
6486
6487 return 1;
6488 }
6489
6490 /* A helper for create_debug_types_hash_table. Read types from SECTION
6491 and fill them into TYPES_HTAB. It will process only type units,
6492 therefore DW_UT_type. */
6493
6494 static void
6495 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6496 struct dwo_file *dwo_file,
6497 dwarf2_section_info *section, htab_up &types_htab,
6498 rcuh_kind section_kind)
6499 {
6500 struct objfile *objfile = per_objfile->objfile;
6501 struct dwarf2_section_info *abbrev_section;
6502 bfd *abfd;
6503 const gdb_byte *info_ptr, *end_ptr;
6504
6505 abbrev_section = (dwo_file != NULL
6506 ? &dwo_file->sections.abbrev
6507 : &per_objfile->per_bfd->abbrev);
6508
6509 dwarf_read_debug_printf ("Reading %s for %s:",
6510 section->get_name (),
6511 abbrev_section->get_file_name ());
6512
6513 section->read (objfile);
6514 info_ptr = section->buffer;
6515
6516 if (info_ptr == NULL)
6517 return;
6518
6519 /* We can't set abfd until now because the section may be empty or
6520 not present, in which case the bfd is unknown. */
6521 abfd = section->get_bfd_owner ();
6522
6523 /* We don't use cutu_reader here because we don't need to read
6524 any dies: the signature is in the header. */
6525
6526 end_ptr = info_ptr + section->size;
6527 while (info_ptr < end_ptr)
6528 {
6529 struct signatured_type *sig_type;
6530 struct dwo_unit *dwo_tu;
6531 void **slot;
6532 const gdb_byte *ptr = info_ptr;
6533 struct comp_unit_head header;
6534 unsigned int length;
6535
6536 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6537
6538 /* Initialize it due to a false compiler warning. */
6539 header.signature = -1;
6540 header.type_cu_offset_in_tu = (cu_offset) -1;
6541
6542 /* We need to read the type's signature in order to build the hash
6543 table, but we don't need anything else just yet. */
6544
6545 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6546 abbrev_section, ptr, section_kind);
6547
6548 length = header.get_length ();
6549
6550 /* Skip dummy type units. */
6551 if (ptr >= info_ptr + length
6552 || peek_abbrev_code (abfd, ptr) == 0
6553 || (header.unit_type != DW_UT_type
6554 && header.unit_type != DW_UT_split_type))
6555 {
6556 info_ptr += length;
6557 continue;
6558 }
6559
6560 if (types_htab == NULL)
6561 {
6562 if (dwo_file)
6563 types_htab = allocate_dwo_unit_table ();
6564 else
6565 types_htab = allocate_signatured_type_table ();
6566 }
6567
6568 if (dwo_file)
6569 {
6570 sig_type = NULL;
6571 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6572 dwo_tu->dwo_file = dwo_file;
6573 dwo_tu->signature = header.signature;
6574 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6575 dwo_tu->section = section;
6576 dwo_tu->sect_off = sect_off;
6577 dwo_tu->length = length;
6578 }
6579 else
6580 {
6581 /* N.B.: type_offset is not usable if this type uses a DWO file.
6582 The real type_offset is in the DWO file. */
6583 dwo_tu = NULL;
6584 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6585 sig_type->signature = header.signature;
6586 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6587 sig_type->per_cu.is_debug_types = 1;
6588 sig_type->per_cu.section = section;
6589 sig_type->per_cu.sect_off = sect_off;
6590 sig_type->per_cu.length = length;
6591 }
6592
6593 slot = htab_find_slot (types_htab.get (),
6594 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6595 INSERT);
6596 gdb_assert (slot != NULL);
6597 if (*slot != NULL)
6598 {
6599 sect_offset dup_sect_off;
6600
6601 if (dwo_file)
6602 {
6603 const struct dwo_unit *dup_tu
6604 = (const struct dwo_unit *) *slot;
6605
6606 dup_sect_off = dup_tu->sect_off;
6607 }
6608 else
6609 {
6610 const struct signatured_type *dup_tu
6611 = (const struct signatured_type *) *slot;
6612
6613 dup_sect_off = dup_tu->per_cu.sect_off;
6614 }
6615
6616 complaint (_("debug type entry at offset %s is duplicate to"
6617 " the entry at offset %s, signature %s"),
6618 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6619 hex_string (header.signature));
6620 }
6621 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6622
6623 dwarf_read_debug_printf_v (" offset %s, signature %s",
6624 sect_offset_str (sect_off),
6625 hex_string (header.signature));
6626
6627 info_ptr += length;
6628 }
6629 }
6630
6631 /* Create the hash table of all entries in the .debug_types
6632 (or .debug_types.dwo) section(s).
6633 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6634 otherwise it is NULL.
6635
6636 The result is a pointer to the hash table or NULL if there are no types.
6637
6638 Note: This function processes DWO files only, not DWP files. */
6639
6640 static void
6641 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6642 struct dwo_file *dwo_file,
6643 gdb::array_view<dwarf2_section_info> type_sections,
6644 htab_up &types_htab)
6645 {
6646 for (dwarf2_section_info &section : type_sections)
6647 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6648 rcuh_kind::TYPE);
6649 }
6650
6651 /* Create the hash table of all entries in the .debug_types section,
6652 and initialize all_type_units.
6653 The result is zero if there is an error (e.g. missing .debug_types section),
6654 otherwise non-zero. */
6655
6656 static int
6657 create_all_type_units (dwarf2_per_objfile *per_objfile)
6658 {
6659 htab_up types_htab;
6660
6661 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6662 types_htab, rcuh_kind::COMPILE);
6663 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6664 types_htab);
6665 if (types_htab == NULL)
6666 {
6667 per_objfile->per_bfd->signatured_types = NULL;
6668 return 0;
6669 }
6670
6671 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6672
6673 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6674 per_objfile->per_bfd->all_type_units.reserve
6675 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6676
6677 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6678 add_signatured_type_cu_to_table,
6679 &per_objfile->per_bfd->all_type_units);
6680
6681 return 1;
6682 }
6683
6684 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6685 If SLOT is non-NULL, it is the entry to use in the hash table.
6686 Otherwise we find one. */
6687
6688 static struct signatured_type *
6689 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6690 {
6691 if (per_objfile->per_bfd->all_type_units.size ()
6692 == per_objfile->per_bfd->all_type_units.capacity ())
6693 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6694
6695 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6696
6697 per_objfile->resize_symtabs ();
6698
6699 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6700 sig_type->signature = sig;
6701 sig_type->per_cu.is_debug_types = 1;
6702 if (per_objfile->per_bfd->using_index)
6703 {
6704 sig_type->per_cu.v.quick =
6705 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6706 struct dwarf2_per_cu_quick_data);
6707 }
6708
6709 if (slot == NULL)
6710 {
6711 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6712 sig_type, INSERT);
6713 }
6714 gdb_assert (*slot == NULL);
6715 *slot = sig_type;
6716 /* The rest of sig_type must be filled in by the caller. */
6717 return sig_type;
6718 }
6719
6720 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6721 Fill in SIG_ENTRY with DWO_ENTRY. */
6722
6723 static void
6724 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6725 struct signatured_type *sig_entry,
6726 struct dwo_unit *dwo_entry)
6727 {
6728 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6729
6730 /* Make sure we're not clobbering something we don't expect to. */
6731 gdb_assert (! sig_entry->per_cu.queued);
6732 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6733 if (per_bfd->using_index)
6734 {
6735 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6736 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6737 }
6738 else
6739 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6740 gdb_assert (sig_entry->signature == dwo_entry->signature);
6741 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6742 gdb_assert (sig_entry->type_unit_group == NULL);
6743 gdb_assert (sig_entry->dwo_unit == NULL);
6744
6745 sig_entry->per_cu.section = dwo_entry->section;
6746 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6747 sig_entry->per_cu.length = dwo_entry->length;
6748 sig_entry->per_cu.reading_dwo_directly = 1;
6749 sig_entry->per_cu.per_bfd = per_bfd;
6750 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6751 sig_entry->dwo_unit = dwo_entry;
6752 }
6753
6754 /* Subroutine of lookup_signatured_type.
6755 If we haven't read the TU yet, create the signatured_type data structure
6756 for a TU to be read in directly from a DWO file, bypassing the stub.
6757 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6758 using .gdb_index, then when reading a CU we want to stay in the DWO file
6759 containing that CU. Otherwise we could end up reading several other DWO
6760 files (due to comdat folding) to process the transitive closure of all the
6761 mentioned TUs, and that can be slow. The current DWO file will have every
6762 type signature that it needs.
6763 We only do this for .gdb_index because in the psymtab case we already have
6764 to read all the DWOs to build the type unit groups. */
6765
6766 static struct signatured_type *
6767 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6768 {
6769 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6770 struct dwo_file *dwo_file;
6771 struct dwo_unit find_dwo_entry, *dwo_entry;
6772 struct signatured_type find_sig_entry, *sig_entry;
6773 void **slot;
6774
6775 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6776
6777 /* If TU skeletons have been removed then we may not have read in any
6778 TUs yet. */
6779 if (per_objfile->per_bfd->signatured_types == NULL)
6780 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6781
6782 /* We only ever need to read in one copy of a signatured type.
6783 Use the global signatured_types array to do our own comdat-folding
6784 of types. If this is the first time we're reading this TU, and
6785 the TU has an entry in .gdb_index, replace the recorded data from
6786 .gdb_index with this TU. */
6787
6788 find_sig_entry.signature = sig;
6789 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6790 &find_sig_entry, INSERT);
6791 sig_entry = (struct signatured_type *) *slot;
6792
6793 /* We can get here with the TU already read, *or* in the process of being
6794 read. Don't reassign the global entry to point to this DWO if that's
6795 the case. Also note that if the TU is already being read, it may not
6796 have come from a DWO, the program may be a mix of Fission-compiled
6797 code and non-Fission-compiled code. */
6798
6799 /* Have we already tried to read this TU?
6800 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6801 needn't exist in the global table yet). */
6802 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6803 return sig_entry;
6804
6805 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6806 dwo_unit of the TU itself. */
6807 dwo_file = cu->dwo_unit->dwo_file;
6808
6809 /* Ok, this is the first time we're reading this TU. */
6810 if (dwo_file->tus == NULL)
6811 return NULL;
6812 find_dwo_entry.signature = sig;
6813 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6814 &find_dwo_entry);
6815 if (dwo_entry == NULL)
6816 return NULL;
6817
6818 /* If the global table doesn't have an entry for this TU, add one. */
6819 if (sig_entry == NULL)
6820 sig_entry = add_type_unit (per_objfile, sig, slot);
6821
6822 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6823 sig_entry->per_cu.tu_read = 1;
6824 return sig_entry;
6825 }
6826
6827 /* Subroutine of lookup_signatured_type.
6828 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6829 then try the DWP file. If the TU stub (skeleton) has been removed then
6830 it won't be in .gdb_index. */
6831
6832 static struct signatured_type *
6833 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6834 {
6835 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6836 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6837 struct dwo_unit *dwo_entry;
6838 struct signatured_type find_sig_entry, *sig_entry;
6839 void **slot;
6840
6841 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6842 gdb_assert (dwp_file != NULL);
6843
6844 /* If TU skeletons have been removed then we may not have read in any
6845 TUs yet. */
6846 if (per_objfile->per_bfd->signatured_types == NULL)
6847 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6848
6849 find_sig_entry.signature = sig;
6850 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6851 &find_sig_entry, INSERT);
6852 sig_entry = (struct signatured_type *) *slot;
6853
6854 /* Have we already tried to read this TU?
6855 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6856 needn't exist in the global table yet). */
6857 if (sig_entry != NULL)
6858 return sig_entry;
6859
6860 if (dwp_file->tus == NULL)
6861 return NULL;
6862 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6863 1 /* is_debug_types */);
6864 if (dwo_entry == NULL)
6865 return NULL;
6866
6867 sig_entry = add_type_unit (per_objfile, sig, slot);
6868 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6869
6870 return sig_entry;
6871 }
6872
6873 /* Lookup a signature based type for DW_FORM_ref_sig8.
6874 Returns NULL if signature SIG is not present in the table.
6875 It is up to the caller to complain about this. */
6876
6877 static struct signatured_type *
6878 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6879 {
6880 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6881
6882 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6883 {
6884 /* We're in a DWO/DWP file, and we're using .gdb_index.
6885 These cases require special processing. */
6886 if (get_dwp_file (per_objfile) == NULL)
6887 return lookup_dwo_signatured_type (cu, sig);
6888 else
6889 return lookup_dwp_signatured_type (cu, sig);
6890 }
6891 else
6892 {
6893 struct signatured_type find_entry, *entry;
6894
6895 if (per_objfile->per_bfd->signatured_types == NULL)
6896 return NULL;
6897 find_entry.signature = sig;
6898 entry = ((struct signatured_type *)
6899 htab_find (per_objfile->per_bfd->signatured_types.get (),
6900 &find_entry));
6901 return entry;
6902 }
6903 }
6904
6905 /* Low level DIE reading support. */
6906
6907 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6908
6909 static void
6910 init_cu_die_reader (struct die_reader_specs *reader,
6911 struct dwarf2_cu *cu,
6912 struct dwarf2_section_info *section,
6913 struct dwo_file *dwo_file,
6914 struct abbrev_table *abbrev_table)
6915 {
6916 gdb_assert (section->readin && section->buffer != NULL);
6917 reader->abfd = section->get_bfd_owner ();
6918 reader->cu = cu;
6919 reader->dwo_file = dwo_file;
6920 reader->die_section = section;
6921 reader->buffer = section->buffer;
6922 reader->buffer_end = section->buffer + section->size;
6923 reader->abbrev_table = abbrev_table;
6924 }
6925
6926 /* Subroutine of cutu_reader to simplify it.
6927 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6928 There's just a lot of work to do, and cutu_reader is big enough
6929 already.
6930
6931 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6932 from it to the DIE in the DWO. If NULL we are skipping the stub.
6933 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6934 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6935 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6936 STUB_COMP_DIR may be non-NULL.
6937 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6938 are filled in with the info of the DIE from the DWO file.
6939 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6940 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6941 kept around for at least as long as *RESULT_READER.
6942
6943 The result is non-zero if a valid (non-dummy) DIE was found. */
6944
6945 static int
6946 read_cutu_die_from_dwo (dwarf2_cu *cu,
6947 struct dwo_unit *dwo_unit,
6948 struct die_info *stub_comp_unit_die,
6949 const char *stub_comp_dir,
6950 struct die_reader_specs *result_reader,
6951 const gdb_byte **result_info_ptr,
6952 struct die_info **result_comp_unit_die,
6953 abbrev_table_up *result_dwo_abbrev_table)
6954 {
6955 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6956 dwarf2_per_cu_data *per_cu = cu->per_cu;
6957 struct objfile *objfile = per_objfile->objfile;
6958 bfd *abfd;
6959 const gdb_byte *begin_info_ptr, *info_ptr;
6960 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6961 int i,num_extra_attrs;
6962 struct dwarf2_section_info *dwo_abbrev_section;
6963 struct die_info *comp_unit_die;
6964
6965 /* At most one of these may be provided. */
6966 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6967
6968 /* These attributes aren't processed until later:
6969 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6970 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6971 referenced later. However, these attributes are found in the stub
6972 which we won't have later. In order to not impose this complication
6973 on the rest of the code, we read them here and copy them to the
6974 DWO CU/TU die. */
6975
6976 stmt_list = NULL;
6977 low_pc = NULL;
6978 high_pc = NULL;
6979 ranges = NULL;
6980 comp_dir = NULL;
6981
6982 if (stub_comp_unit_die != NULL)
6983 {
6984 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6985 DWO file. */
6986 if (!per_cu->is_debug_types)
6987 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6988 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6989 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6990 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6991 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6992
6993 cu->addr_base = stub_comp_unit_die->addr_base ();
6994
6995 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6996 We need the value before we can process DW_AT_ranges values from the
6997 DWO. */
6998 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6999
7000 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
7001 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
7002 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
7003 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
7004 section. */
7005 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
7006 }
7007 else if (stub_comp_dir != NULL)
7008 {
7009 /* Reconstruct the comp_dir attribute to simplify the code below. */
7010 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
7011 comp_dir->name = DW_AT_comp_dir;
7012 comp_dir->form = DW_FORM_string;
7013 comp_dir->set_string_noncanonical (stub_comp_dir);
7014 }
7015
7016 /* Set up for reading the DWO CU/TU. */
7017 cu->dwo_unit = dwo_unit;
7018 dwarf2_section_info *section = dwo_unit->section;
7019 section->read (objfile);
7020 abfd = section->get_bfd_owner ();
7021 begin_info_ptr = info_ptr = (section->buffer
7022 + to_underlying (dwo_unit->sect_off));
7023 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
7024
7025 if (per_cu->is_debug_types)
7026 {
7027 signatured_type *sig_type = (struct signatured_type *) per_cu;
7028
7029 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7030 section, dwo_abbrev_section,
7031 info_ptr, rcuh_kind::TYPE);
7032 /* This is not an assert because it can be caused by bad debug info. */
7033 if (sig_type->signature != cu->header.signature)
7034 {
7035 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
7036 " TU at offset %s [in module %s]"),
7037 hex_string (sig_type->signature),
7038 hex_string (cu->header.signature),
7039 sect_offset_str (dwo_unit->sect_off),
7040 bfd_get_filename (abfd));
7041 }
7042 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7043 /* For DWOs coming from DWP files, we don't know the CU length
7044 nor the type's offset in the TU until now. */
7045 dwo_unit->length = cu->header.get_length ();
7046 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
7047
7048 /* Establish the type offset that can be used to lookup the type.
7049 For DWO files, we don't know it until now. */
7050 sig_type->type_offset_in_section
7051 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
7052 }
7053 else
7054 {
7055 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7056 section, dwo_abbrev_section,
7057 info_ptr, rcuh_kind::COMPILE);
7058 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
7059 /* For DWOs coming from DWP files, we don't know the CU length
7060 until now. */
7061 dwo_unit->length = cu->header.get_length ();
7062 }
7063
7064 dwo_abbrev_section->read (objfile);
7065 *result_dwo_abbrev_table
7066 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
7067 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7068 result_dwo_abbrev_table->get ());
7069
7070 /* Read in the die, but leave space to copy over the attributes
7071 from the stub. This has the benefit of simplifying the rest of
7072 the code - all the work to maintain the illusion of a single
7073 DW_TAG_{compile,type}_unit DIE is done here. */
7074 num_extra_attrs = ((stmt_list != NULL)
7075 + (low_pc != NULL)
7076 + (high_pc != NULL)
7077 + (ranges != NULL)
7078 + (comp_dir != NULL));
7079 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7080 num_extra_attrs);
7081
7082 /* Copy over the attributes from the stub to the DIE we just read in. */
7083 comp_unit_die = *result_comp_unit_die;
7084 i = comp_unit_die->num_attrs;
7085 if (stmt_list != NULL)
7086 comp_unit_die->attrs[i++] = *stmt_list;
7087 if (low_pc != NULL)
7088 comp_unit_die->attrs[i++] = *low_pc;
7089 if (high_pc != NULL)
7090 comp_unit_die->attrs[i++] = *high_pc;
7091 if (ranges != NULL)
7092 comp_unit_die->attrs[i++] = *ranges;
7093 if (comp_dir != NULL)
7094 comp_unit_die->attrs[i++] = *comp_dir;
7095 comp_unit_die->num_attrs += num_extra_attrs;
7096
7097 if (dwarf_die_debug)
7098 {
7099 fprintf_unfiltered (gdb_stdlog,
7100 "Read die from %s@0x%x of %s:\n",
7101 section->get_name (),
7102 (unsigned) (begin_info_ptr - section->buffer),
7103 bfd_get_filename (abfd));
7104 dump_die (comp_unit_die, dwarf_die_debug);
7105 }
7106
7107 /* Skip dummy compilation units. */
7108 if (info_ptr >= begin_info_ptr + dwo_unit->length
7109 || peek_abbrev_code (abfd, info_ptr) == 0)
7110 return 0;
7111
7112 *result_info_ptr = info_ptr;
7113 return 1;
7114 }
7115
7116 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
7117 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7118 signature is part of the header. */
7119 static gdb::optional<ULONGEST>
7120 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7121 {
7122 if (cu->header.version >= 5)
7123 return cu->header.signature;
7124 struct attribute *attr;
7125 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7126 if (attr == nullptr || !attr->form_is_unsigned ())
7127 return gdb::optional<ULONGEST> ();
7128 return attr->as_unsigned ();
7129 }
7130
7131 /* Subroutine of cutu_reader to simplify it.
7132 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
7133 Returns NULL if the specified DWO unit cannot be found. */
7134
7135 static struct dwo_unit *
7136 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
7137 {
7138 dwarf2_per_cu_data *per_cu = cu->per_cu;
7139 struct dwo_unit *dwo_unit;
7140 const char *comp_dir;
7141
7142 gdb_assert (cu != NULL);
7143
7144 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7145 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7146 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7147
7148 if (per_cu->is_debug_types)
7149 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
7150 else
7151 {
7152 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7153
7154 if (!signature.has_value ())
7155 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7156 " [in module %s]"),
7157 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7158
7159 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
7160 }
7161
7162 return dwo_unit;
7163 }
7164
7165 /* Subroutine of cutu_reader to simplify it.
7166 See it for a description of the parameters.
7167 Read a TU directly from a DWO file, bypassing the stub. */
7168
7169 void
7170 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7171 dwarf2_per_objfile *per_objfile,
7172 dwarf2_cu *existing_cu)
7173 {
7174 struct signatured_type *sig_type;
7175
7176 /* Verify we can do the following downcast, and that we have the
7177 data we need. */
7178 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7179 sig_type = (struct signatured_type *) this_cu;
7180 gdb_assert (sig_type->dwo_unit != NULL);
7181
7182 dwarf2_cu *cu;
7183
7184 if (existing_cu != nullptr)
7185 {
7186 cu = existing_cu;
7187 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
7188 /* There's no need to do the rereading_dwo_cu handling that
7189 cutu_reader does since we don't read the stub. */
7190 }
7191 else
7192 {
7193 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7194 in per_objfile yet. */
7195 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7196 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7197 cu = m_new_cu.get ();
7198 }
7199
7200 /* A future optimization, if needed, would be to use an existing
7201 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7202 could share abbrev tables. */
7203
7204 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
7205 NULL /* stub_comp_unit_die */,
7206 sig_type->dwo_unit->dwo_file->comp_dir,
7207 this, &info_ptr,
7208 &comp_unit_die,
7209 &m_dwo_abbrev_table) == 0)
7210 {
7211 /* Dummy die. */
7212 dummy_p = true;
7213 }
7214 }
7215
7216 /* Initialize a CU (or TU) and read its DIEs.
7217 If the CU defers to a DWO file, read the DWO file as well.
7218
7219 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7220 Otherwise the table specified in the comp unit header is read in and used.
7221 This is an optimization for when we already have the abbrev table.
7222
7223 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7224 allocated. */
7225
7226 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7227 dwarf2_per_objfile *per_objfile,
7228 struct abbrev_table *abbrev_table,
7229 dwarf2_cu *existing_cu,
7230 bool skip_partial)
7231 : die_reader_specs {},
7232 m_this_cu (this_cu)
7233 {
7234 struct objfile *objfile = per_objfile->objfile;
7235 struct dwarf2_section_info *section = this_cu->section;
7236 bfd *abfd = section->get_bfd_owner ();
7237 const gdb_byte *begin_info_ptr;
7238 struct signatured_type *sig_type = NULL;
7239 struct dwarf2_section_info *abbrev_section;
7240 /* Non-zero if CU currently points to a DWO file and we need to
7241 reread it. When this happens we need to reread the skeleton die
7242 before we can reread the DWO file (this only applies to CUs, not TUs). */
7243 int rereading_dwo_cu = 0;
7244
7245 if (dwarf_die_debug)
7246 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7247 this_cu->is_debug_types ? "type" : "comp",
7248 sect_offset_str (this_cu->sect_off));
7249
7250 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7251 file (instead of going through the stub), short-circuit all of this. */
7252 if (this_cu->reading_dwo_directly)
7253 {
7254 /* Narrow down the scope of possibilities to have to understand. */
7255 gdb_assert (this_cu->is_debug_types);
7256 gdb_assert (abbrev_table == NULL);
7257 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
7258 return;
7259 }
7260
7261 /* This is cheap if the section is already read in. */
7262 section->read (objfile);
7263
7264 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7265
7266 abbrev_section = get_abbrev_section_for_cu (this_cu);
7267
7268 dwarf2_cu *cu;
7269
7270 if (existing_cu != nullptr)
7271 {
7272 cu = existing_cu;
7273 /* If this CU is from a DWO file we need to start over, we need to
7274 refetch the attributes from the skeleton CU.
7275 This could be optimized by retrieving those attributes from when we
7276 were here the first time: the previous comp_unit_die was stored in
7277 comp_unit_obstack. But there's no data yet that we need this
7278 optimization. */
7279 if (cu->dwo_unit != NULL)
7280 rereading_dwo_cu = 1;
7281 }
7282 else
7283 {
7284 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
7285 in per_objfile yet. */
7286 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7287 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7288 cu = m_new_cu.get ();
7289 }
7290
7291 /* Get the header. */
7292 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
7293 {
7294 /* We already have the header, there's no need to read it in again. */
7295 info_ptr += to_underlying (cu->header.first_die_cu_offset);
7296 }
7297 else
7298 {
7299 if (this_cu->is_debug_types)
7300 {
7301 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7302 section, abbrev_section,
7303 info_ptr, rcuh_kind::TYPE);
7304
7305 /* Since per_cu is the first member of struct signatured_type,
7306 we can go from a pointer to one to a pointer to the other. */
7307 sig_type = (struct signatured_type *) this_cu;
7308 gdb_assert (sig_type->signature == cu->header.signature);
7309 gdb_assert (sig_type->type_offset_in_tu
7310 == cu->header.type_cu_offset_in_tu);
7311 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7312
7313 /* LENGTH has not been set yet for type units if we're
7314 using .gdb_index. */
7315 this_cu->length = cu->header.get_length ();
7316
7317 /* Establish the type offset that can be used to lookup the type. */
7318 sig_type->type_offset_in_section =
7319 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
7320
7321 this_cu->dwarf_version = cu->header.version;
7322 }
7323 else
7324 {
7325 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7326 section, abbrev_section,
7327 info_ptr,
7328 rcuh_kind::COMPILE);
7329
7330 gdb_assert (this_cu->sect_off == cu->header.sect_off);
7331 if (this_cu->length == 0)
7332 this_cu->length = cu->header.get_length ();
7333 else
7334 gdb_assert (this_cu->length == cu->header.get_length ());
7335 this_cu->dwarf_version = cu->header.version;
7336 }
7337 }
7338
7339 /* Skip dummy compilation units. */
7340 if (info_ptr >= begin_info_ptr + this_cu->length
7341 || peek_abbrev_code (abfd, info_ptr) == 0)
7342 {
7343 dummy_p = true;
7344 return;
7345 }
7346
7347 /* If we don't have them yet, read the abbrevs for this compilation unit.
7348 And if we need to read them now, make sure they're freed when we're
7349 done. */
7350 if (abbrev_table != NULL)
7351 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7352 else
7353 {
7354 abbrev_section->read (objfile);
7355 m_abbrev_table_holder
7356 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
7357 abbrev_table = m_abbrev_table_holder.get ();
7358 }
7359
7360 /* Read the top level CU/TU die. */
7361 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
7362 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7363
7364 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7365 {
7366 dummy_p = true;
7367 return;
7368 }
7369
7370 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
7371 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7372 table from the DWO file and pass the ownership over to us. It will be
7373 referenced from READER, so we must make sure to free it after we're done
7374 with READER.
7375
7376 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7377 DWO CU, that this test will fail (the attribute will not be present). */
7378 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7379 if (dwo_name != nullptr)
7380 {
7381 struct dwo_unit *dwo_unit;
7382 struct die_info *dwo_comp_unit_die;
7383
7384 if (comp_unit_die->has_children)
7385 {
7386 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
7387 " has children (offset %s) [in module %s]"),
7388 sect_offset_str (this_cu->sect_off),
7389 bfd_get_filename (abfd));
7390 }
7391 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
7392 if (dwo_unit != NULL)
7393 {
7394 if (read_cutu_die_from_dwo (cu, dwo_unit,
7395 comp_unit_die, NULL,
7396 this, &info_ptr,
7397 &dwo_comp_unit_die,
7398 &m_dwo_abbrev_table) == 0)
7399 {
7400 /* Dummy die. */
7401 dummy_p = true;
7402 return;
7403 }
7404 comp_unit_die = dwo_comp_unit_die;
7405 }
7406 else
7407 {
7408 /* Yikes, we couldn't find the rest of the DIE, we only have
7409 the stub. A complaint has already been logged. There's
7410 not much more we can do except pass on the stub DIE to
7411 die_reader_func. We don't want to throw an error on bad
7412 debug info. */
7413 }
7414 }
7415 }
7416
7417 void
7418 cutu_reader::keep ()
7419 {
7420 /* Done, clean up. */
7421 gdb_assert (!dummy_p);
7422 if (m_new_cu != NULL)
7423 {
7424 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7425 now. */
7426 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7427 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7428 }
7429 }
7430
7431 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7432 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7433 assumed to have already done the lookup to find the DWO file).
7434
7435 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7436 THIS_CU->is_debug_types, but nothing else.
7437
7438 We fill in THIS_CU->length.
7439
7440 THIS_CU->cu is always freed when done.
7441 This is done in order to not leave THIS_CU->cu in a state where we have
7442 to care whether it refers to the "main" CU or the DWO CU.
7443
7444 When parent_cu is passed, it is used to provide a default value for
7445 str_offsets_base and addr_base from the parent. */
7446
7447 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7448 dwarf2_per_objfile *per_objfile,
7449 struct dwarf2_cu *parent_cu,
7450 struct dwo_file *dwo_file)
7451 : die_reader_specs {},
7452 m_this_cu (this_cu)
7453 {
7454 struct objfile *objfile = per_objfile->objfile;
7455 struct dwarf2_section_info *section = this_cu->section;
7456 bfd *abfd = section->get_bfd_owner ();
7457 struct dwarf2_section_info *abbrev_section;
7458 const gdb_byte *begin_info_ptr, *info_ptr;
7459
7460 if (dwarf_die_debug)
7461 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7462 this_cu->is_debug_types ? "type" : "comp",
7463 sect_offset_str (this_cu->sect_off));
7464
7465 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7466
7467 abbrev_section = (dwo_file != NULL
7468 ? &dwo_file->sections.abbrev
7469 : get_abbrev_section_for_cu (this_cu));
7470
7471 /* This is cheap if the section is already read in. */
7472 section->read (objfile);
7473
7474 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7475
7476 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7477 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7478 section, abbrev_section, info_ptr,
7479 (this_cu->is_debug_types
7480 ? rcuh_kind::TYPE
7481 : rcuh_kind::COMPILE));
7482
7483 if (parent_cu != nullptr)
7484 {
7485 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7486 m_new_cu->addr_base = parent_cu->addr_base;
7487 }
7488 this_cu->length = m_new_cu->header.get_length ();
7489
7490 /* Skip dummy compilation units. */
7491 if (info_ptr >= begin_info_ptr + this_cu->length
7492 || peek_abbrev_code (abfd, info_ptr) == 0)
7493 {
7494 dummy_p = true;
7495 return;
7496 }
7497
7498 abbrev_section->read (objfile);
7499 m_abbrev_table_holder
7500 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7501
7502 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7503 m_abbrev_table_holder.get ());
7504 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7505 }
7506
7507 \f
7508 /* Type Unit Groups.
7509
7510 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7511 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7512 so that all types coming from the same compilation (.o file) are grouped
7513 together. A future step could be to put the types in the same symtab as
7514 the CU the types ultimately came from. */
7515
7516 static hashval_t
7517 hash_type_unit_group (const void *item)
7518 {
7519 const struct type_unit_group *tu_group
7520 = (const struct type_unit_group *) item;
7521
7522 return hash_stmt_list_entry (&tu_group->hash);
7523 }
7524
7525 static int
7526 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7527 {
7528 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7529 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7530
7531 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7532 }
7533
7534 /* Allocate a hash table for type unit groups. */
7535
7536 static htab_up
7537 allocate_type_unit_groups_table ()
7538 {
7539 return htab_up (htab_create_alloc (3,
7540 hash_type_unit_group,
7541 eq_type_unit_group,
7542 NULL, xcalloc, xfree));
7543 }
7544
7545 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7546 partial symtabs. We combine several TUs per psymtab to not let the size
7547 of any one psymtab grow too big. */
7548 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7549 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7550
7551 /* Helper routine for get_type_unit_group.
7552 Create the type_unit_group object used to hold one or more TUs. */
7553
7554 static struct type_unit_group *
7555 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7556 {
7557 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7558 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7559 struct dwarf2_per_cu_data *per_cu;
7560 struct type_unit_group *tu_group;
7561
7562 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
7563 per_cu = &tu_group->per_cu;
7564 per_cu->per_bfd = per_bfd;
7565
7566 if (per_bfd->using_index)
7567 {
7568 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7569 struct dwarf2_per_cu_quick_data);
7570 }
7571 else
7572 {
7573 unsigned int line_offset = to_underlying (line_offset_struct);
7574 dwarf2_psymtab *pst;
7575 std::string name;
7576
7577 /* Give the symtab a useful name for debug purposes. */
7578 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7579 name = string_printf ("<type_units_%d>",
7580 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7581 else
7582 name = string_printf ("<type_units_at_0x%x>", line_offset);
7583
7584 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7585 pst->anonymous = true;
7586 }
7587
7588 tu_group->hash.dwo_unit = cu->dwo_unit;
7589 tu_group->hash.line_sect_off = line_offset_struct;
7590
7591 return tu_group;
7592 }
7593
7594 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7595 STMT_LIST is a DW_AT_stmt_list attribute. */
7596
7597 static struct type_unit_group *
7598 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7599 {
7600 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7601 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7602 struct type_unit_group *tu_group;
7603 void **slot;
7604 unsigned int line_offset;
7605 struct type_unit_group type_unit_group_for_lookup;
7606
7607 if (per_objfile->per_bfd->type_unit_groups == NULL)
7608 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7609
7610 /* Do we need to create a new group, or can we use an existing one? */
7611
7612 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7613 {
7614 line_offset = stmt_list->as_unsigned ();
7615 ++tu_stats->nr_symtab_sharers;
7616 }
7617 else
7618 {
7619 /* Ugh, no stmt_list. Rare, but we have to handle it.
7620 We can do various things here like create one group per TU or
7621 spread them over multiple groups to split up the expansion work.
7622 To avoid worst case scenarios (too many groups or too large groups)
7623 we, umm, group them in bunches. */
7624 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7625 | (tu_stats->nr_stmt_less_type_units
7626 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7627 ++tu_stats->nr_stmt_less_type_units;
7628 }
7629
7630 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7631 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7632 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7633 &type_unit_group_for_lookup, INSERT);
7634 if (*slot != NULL)
7635 {
7636 tu_group = (struct type_unit_group *) *slot;
7637 gdb_assert (tu_group != NULL);
7638 }
7639 else
7640 {
7641 sect_offset line_offset_struct = (sect_offset) line_offset;
7642 tu_group = create_type_unit_group (cu, line_offset_struct);
7643 *slot = tu_group;
7644 ++tu_stats->nr_symtabs;
7645 }
7646
7647 return tu_group;
7648 }
7649 \f
7650 /* Partial symbol tables. */
7651
7652 /* Create a psymtab named NAME and assign it to PER_CU.
7653
7654 The caller must fill in the following details:
7655 dirname, textlow, texthigh. */
7656
7657 static dwarf2_psymtab *
7658 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7659 dwarf2_per_objfile *per_objfile,
7660 const char *name)
7661 {
7662 struct objfile *objfile = per_objfile->objfile;
7663 dwarf2_psymtab *pst;
7664
7665 pst = new dwarf2_psymtab (name, objfile, per_cu);
7666
7667 pst->psymtabs_addrmap_supported = true;
7668
7669 /* This is the glue that links PST into GDB's symbol API. */
7670 per_cu->v.psymtab = pst;
7671
7672 return pst;
7673 }
7674
7675 /* DIE reader function for process_psymtab_comp_unit. */
7676
7677 static void
7678 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7679 const gdb_byte *info_ptr,
7680 struct die_info *comp_unit_die,
7681 enum language pretend_language)
7682 {
7683 struct dwarf2_cu *cu = reader->cu;
7684 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7685 struct objfile *objfile = per_objfile->objfile;
7686 struct gdbarch *gdbarch = objfile->arch ();
7687 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7688 CORE_ADDR baseaddr;
7689 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7690 dwarf2_psymtab *pst;
7691 enum pc_bounds_kind cu_bounds_kind;
7692 const char *filename;
7693
7694 gdb_assert (! per_cu->is_debug_types);
7695
7696 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7697
7698 /* Allocate a new partial symbol table structure. */
7699 gdb::unique_xmalloc_ptr<char> debug_filename;
7700 static const char artificial[] = "<artificial>";
7701 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7702 if (filename == NULL)
7703 filename = "";
7704 else if (strcmp (filename, artificial) == 0)
7705 {
7706 debug_filename.reset (concat (artificial, "@",
7707 sect_offset_str (per_cu->sect_off),
7708 (char *) NULL));
7709 filename = debug_filename.get ();
7710 }
7711
7712 pst = create_partial_symtab (per_cu, per_objfile, filename);
7713
7714 /* This must be done before calling dwarf2_build_include_psymtabs. */
7715 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7716
7717 baseaddr = objfile->text_section_offset ();
7718
7719 dwarf2_find_base_address (comp_unit_die, cu);
7720
7721 /* Possibly set the default values of LOWPC and HIGHPC from
7722 `DW_AT_ranges'. */
7723 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7724 &best_highpc, cu, pst);
7725 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7726 {
7727 CORE_ADDR low
7728 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7729 - baseaddr);
7730 CORE_ADDR high
7731 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7732 - baseaddr - 1);
7733 /* Store the contiguous range if it is not empty; it can be
7734 empty for CUs with no code. */
7735 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7736 low, high, pst);
7737 }
7738
7739 /* Check if comp unit has_children.
7740 If so, read the rest of the partial symbols from this comp unit.
7741 If not, there's no more debug_info for this comp unit. */
7742 if (comp_unit_die->has_children)
7743 {
7744 struct partial_die_info *first_die;
7745 CORE_ADDR lowpc, highpc;
7746
7747 lowpc = ((CORE_ADDR) -1);
7748 highpc = ((CORE_ADDR) 0);
7749
7750 first_die = load_partial_dies (reader, info_ptr, 1);
7751
7752 scan_partial_symbols (first_die, &lowpc, &highpc,
7753 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7754
7755 /* If we didn't find a lowpc, set it to highpc to avoid
7756 complaints from `maint check'. */
7757 if (lowpc == ((CORE_ADDR) -1))
7758 lowpc = highpc;
7759
7760 /* If the compilation unit didn't have an explicit address range,
7761 then use the information extracted from its child dies. */
7762 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7763 {
7764 best_lowpc = lowpc;
7765 best_highpc = highpc;
7766 }
7767 }
7768 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7769 best_lowpc + baseaddr)
7770 - baseaddr);
7771 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7772 best_highpc + baseaddr)
7773 - baseaddr);
7774
7775 pst->end ();
7776
7777 if (!cu->per_cu->imported_symtabs_empty ())
7778 {
7779 int i;
7780 int len = cu->per_cu->imported_symtabs_size ();
7781
7782 /* Fill in 'dependencies' here; we fill in 'users' in a
7783 post-pass. */
7784 pst->number_of_dependencies = len;
7785 pst->dependencies
7786 = objfile->partial_symtabs->allocate_dependencies (len);
7787 for (i = 0; i < len; ++i)
7788 {
7789 pst->dependencies[i]
7790 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7791 }
7792
7793 cu->per_cu->imported_symtabs_free ();
7794 }
7795
7796 /* Get the list of files included in the current compilation unit,
7797 and build a psymtab for each of them. */
7798 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7799
7800 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7801 ", %d global, %d static syms",
7802 per_cu->is_debug_types ? "type" : "comp",
7803 sect_offset_str (per_cu->sect_off),
7804 paddress (gdbarch, pst->text_low (objfile)),
7805 paddress (gdbarch, pst->text_high (objfile)),
7806 (int) pst->global_psymbols.size (),
7807 (int) pst->static_psymbols.size ());
7808 }
7809
7810 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7811 Process compilation unit THIS_CU for a psymtab. */
7812
7813 static void
7814 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7815 dwarf2_per_objfile *per_objfile,
7816 bool want_partial_unit,
7817 enum language pretend_language)
7818 {
7819 /* If this compilation unit was already read in, free the
7820 cached copy in order to read it in again. This is
7821 necessary because we skipped some symbols when we first
7822 read in the compilation unit (see load_partial_dies).
7823 This problem could be avoided, but the benefit is unclear. */
7824 per_objfile->remove_cu (this_cu);
7825
7826 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7827
7828 switch (reader.comp_unit_die->tag)
7829 {
7830 case DW_TAG_compile_unit:
7831 this_cu->unit_type = DW_UT_compile;
7832 break;
7833 case DW_TAG_partial_unit:
7834 this_cu->unit_type = DW_UT_partial;
7835 break;
7836 default:
7837 abort ();
7838 }
7839
7840 if (reader.dummy_p)
7841 {
7842 /* Nothing. */
7843 }
7844 else if (this_cu->is_debug_types)
7845 build_type_psymtabs_reader (&reader, reader.info_ptr,
7846 reader.comp_unit_die);
7847 else if (want_partial_unit
7848 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7849 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7850 reader.comp_unit_die,
7851 pretend_language);
7852
7853 this_cu->lang = reader.cu->language;
7854
7855 /* Age out any secondary CUs. */
7856 per_objfile->age_comp_units ();
7857 }
7858
7859 /* Reader function for build_type_psymtabs. */
7860
7861 static void
7862 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7863 const gdb_byte *info_ptr,
7864 struct die_info *type_unit_die)
7865 {
7866 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7867 struct dwarf2_cu *cu = reader->cu;
7868 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7869 struct signatured_type *sig_type;
7870 struct type_unit_group *tu_group;
7871 struct attribute *attr;
7872 struct partial_die_info *first_die;
7873 CORE_ADDR lowpc, highpc;
7874 dwarf2_psymtab *pst;
7875
7876 gdb_assert (per_cu->is_debug_types);
7877 sig_type = (struct signatured_type *) per_cu;
7878
7879 if (! type_unit_die->has_children)
7880 return;
7881
7882 attr = type_unit_die->attr (DW_AT_stmt_list);
7883 tu_group = get_type_unit_group (cu, attr);
7884
7885 if (tu_group->tus == nullptr)
7886 tu_group->tus = new std::vector<signatured_type *>;
7887 tu_group->tus->push_back (sig_type);
7888
7889 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7890 pst = create_partial_symtab (per_cu, per_objfile, "");
7891 pst->anonymous = true;
7892
7893 first_die = load_partial_dies (reader, info_ptr, 1);
7894
7895 lowpc = (CORE_ADDR) -1;
7896 highpc = (CORE_ADDR) 0;
7897 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7898
7899 pst->end ();
7900 }
7901
7902 /* Struct used to sort TUs by their abbreviation table offset. */
7903
7904 struct tu_abbrev_offset
7905 {
7906 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7907 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7908 {}
7909
7910 signatured_type *sig_type;
7911 sect_offset abbrev_offset;
7912 };
7913
7914 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7915
7916 static bool
7917 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7918 const struct tu_abbrev_offset &b)
7919 {
7920 return a.abbrev_offset < b.abbrev_offset;
7921 }
7922
7923 /* Efficiently read all the type units.
7924 This does the bulk of the work for build_type_psymtabs.
7925
7926 The efficiency is because we sort TUs by the abbrev table they use and
7927 only read each abbrev table once. In one program there are 200K TUs
7928 sharing 8K abbrev tables.
7929
7930 The main purpose of this function is to support building the
7931 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7932 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7933 can collapse the search space by grouping them by stmt_list.
7934 The savings can be significant, in the same program from above the 200K TUs
7935 share 8K stmt_list tables.
7936
7937 FUNC is expected to call get_type_unit_group, which will create the
7938 struct type_unit_group if necessary and add it to
7939 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7940
7941 static void
7942 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7943 {
7944 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7945 abbrev_table_up abbrev_table;
7946 sect_offset abbrev_offset;
7947
7948 /* It's up to the caller to not call us multiple times. */
7949 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7950
7951 if (per_objfile->per_bfd->all_type_units.empty ())
7952 return;
7953
7954 /* TUs typically share abbrev tables, and there can be way more TUs than
7955 abbrev tables. Sort by abbrev table to reduce the number of times we
7956 read each abbrev table in.
7957 Alternatives are to punt or to maintain a cache of abbrev tables.
7958 This is simpler and efficient enough for now.
7959
7960 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7961 symtab to use). Typically TUs with the same abbrev offset have the same
7962 stmt_list value too so in practice this should work well.
7963
7964 The basic algorithm here is:
7965
7966 sort TUs by abbrev table
7967 for each TU with same abbrev table:
7968 read abbrev table if first user
7969 read TU top level DIE
7970 [IWBN if DWO skeletons had DW_AT_stmt_list]
7971 call FUNC */
7972
7973 dwarf_read_debug_printf ("Building type unit groups ...");
7974
7975 /* Sort in a separate table to maintain the order of all_type_units
7976 for .gdb_index: TU indices directly index all_type_units. */
7977 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7978 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7979
7980 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7981 sorted_by_abbrev.emplace_back
7982 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7983 sig_type->per_cu.sect_off));
7984
7985 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7986 sort_tu_by_abbrev_offset);
7987
7988 abbrev_offset = (sect_offset) ~(unsigned) 0;
7989
7990 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7991 {
7992 /* Switch to the next abbrev table if necessary. */
7993 if (abbrev_table == NULL
7994 || tu.abbrev_offset != abbrev_offset)
7995 {
7996 abbrev_offset = tu.abbrev_offset;
7997 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7998 abbrev_table =
7999 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
8000 ++tu_stats->nr_uniq_abbrev_tables;
8001 }
8002
8003 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
8004 abbrev_table.get (), nullptr, false);
8005 if (!reader.dummy_p)
8006 build_type_psymtabs_reader (&reader, reader.info_ptr,
8007 reader.comp_unit_die);
8008 }
8009 }
8010
8011 /* Print collected type unit statistics. */
8012
8013 static void
8014 print_tu_stats (dwarf2_per_objfile *per_objfile)
8015 {
8016 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
8017
8018 dwarf_read_debug_printf ("Type unit statistics:");
8019 dwarf_read_debug_printf (" %zu TUs",
8020 per_objfile->per_bfd->all_type_units.size ());
8021 dwarf_read_debug_printf (" %d uniq abbrev tables",
8022 tu_stats->nr_uniq_abbrev_tables);
8023 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
8024 tu_stats->nr_symtabs);
8025 dwarf_read_debug_printf (" %d symtab sharers",
8026 tu_stats->nr_symtab_sharers);
8027 dwarf_read_debug_printf (" %d type units without a stmt_list",
8028 tu_stats->nr_stmt_less_type_units);
8029 dwarf_read_debug_printf (" %d all_type_units reallocs",
8030 tu_stats->nr_all_type_units_reallocs);
8031 }
8032
8033 /* Traversal function for build_type_psymtabs. */
8034
8035 static int
8036 build_type_psymtab_dependencies (void **slot, void *info)
8037 {
8038 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8039 struct objfile *objfile = per_objfile->objfile;
8040 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
8041 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
8042 dwarf2_psymtab *pst = per_cu->v.psymtab;
8043 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
8044 int i;
8045
8046 gdb_assert (len > 0);
8047 gdb_assert (per_cu->type_unit_group_p ());
8048
8049 pst->number_of_dependencies = len;
8050 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
8051 for (i = 0; i < len; ++i)
8052 {
8053 struct signatured_type *iter = tu_group->tus->at (i);
8054 gdb_assert (iter->per_cu.is_debug_types);
8055 pst->dependencies[i] = iter->per_cu.v.psymtab;
8056 iter->type_unit_group = tu_group;
8057 }
8058
8059 delete tu_group->tus;
8060 tu_group->tus = nullptr;
8061
8062 return 1;
8063 }
8064
8065 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8066 Build partial symbol tables for the .debug_types comp-units. */
8067
8068 static void
8069 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
8070 {
8071 if (! create_all_type_units (per_objfile))
8072 return;
8073
8074 build_type_psymtabs_1 (per_objfile);
8075 }
8076
8077 /* Traversal function for process_skeletonless_type_unit.
8078 Read a TU in a DWO file and build partial symbols for it. */
8079
8080 static int
8081 process_skeletonless_type_unit (void **slot, void *info)
8082 {
8083 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
8084 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8085 struct signatured_type find_entry, *entry;
8086
8087 /* If this TU doesn't exist in the global table, add it and read it in. */
8088
8089 if (per_objfile->per_bfd->signatured_types == NULL)
8090 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
8091
8092 find_entry.signature = dwo_unit->signature;
8093 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
8094 &find_entry, INSERT);
8095 /* If we've already seen this type there's nothing to do. What's happening
8096 is we're doing our own version of comdat-folding here. */
8097 if (*slot != NULL)
8098 return 1;
8099
8100 /* This does the job that create_all_type_units would have done for
8101 this TU. */
8102 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8103 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
8104 *slot = entry;
8105
8106 /* This does the job that build_type_psymtabs_1 would have done. */
8107 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
8108 if (!reader.dummy_p)
8109 build_type_psymtabs_reader (&reader, reader.info_ptr,
8110 reader.comp_unit_die);
8111
8112 return 1;
8113 }
8114
8115 /* Traversal function for process_skeletonless_type_units. */
8116
8117 static int
8118 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8119 {
8120 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8121
8122 if (dwo_file->tus != NULL)
8123 htab_traverse_noresize (dwo_file->tus.get (),
8124 process_skeletonless_type_unit, info);
8125
8126 return 1;
8127 }
8128
8129 /* Scan all TUs of DWO files, verifying we've processed them.
8130 This is needed in case a TU was emitted without its skeleton.
8131 Note: This can't be done until we know what all the DWO files are. */
8132
8133 static void
8134 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
8135 {
8136 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
8137 if (get_dwp_file (per_objfile) == NULL
8138 && per_objfile->per_bfd->dwo_files != NULL)
8139 {
8140 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
8141 process_dwo_file_for_skeletonless_type_units,
8142 per_objfile);
8143 }
8144 }
8145
8146 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
8147
8148 static void
8149 set_partial_user (dwarf2_per_objfile *per_objfile)
8150 {
8151 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8152 {
8153 dwarf2_psymtab *pst = per_cu->v.psymtab;
8154
8155 if (pst == NULL)
8156 continue;
8157
8158 for (int j = 0; j < pst->number_of_dependencies; ++j)
8159 {
8160 /* Set the 'user' field only if it is not already set. */
8161 if (pst->dependencies[j]->user == NULL)
8162 pst->dependencies[j]->user = pst;
8163 }
8164 }
8165 }
8166
8167 /* Build the partial symbol table by doing a quick pass through the
8168 .debug_info and .debug_abbrev sections. */
8169
8170 static void
8171 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
8172 {
8173 struct objfile *objfile = per_objfile->objfile;
8174
8175 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8176 objfile_name (objfile));
8177
8178 scoped_restore restore_reading_psyms
8179 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
8180 true);
8181
8182 per_objfile->per_bfd->info.read (objfile);
8183
8184 /* Any cached compilation units will be linked by the per-objfile
8185 read_in_chain. Make sure to free them when we're done. */
8186 free_cached_comp_units freer (per_objfile);
8187
8188 build_type_psymtabs (per_objfile);
8189
8190 create_all_comp_units (per_objfile);
8191
8192 /* Create a temporary address map on a temporary obstack. We later
8193 copy this to the final obstack. */
8194 auto_obstack temp_obstack;
8195
8196 scoped_restore save_psymtabs_addrmap
8197 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
8198 addrmap_create_mutable (&temp_obstack));
8199
8200 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
8201 {
8202 if (per_cu->v.psymtab != NULL)
8203 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8204 continue;
8205 process_psymtab_comp_unit (per_cu, per_objfile, false,
8206 language_minimal);
8207 }
8208
8209 /* This has to wait until we read the CUs, we need the list of DWOs. */
8210 process_skeletonless_type_units (per_objfile);
8211
8212 /* Now that all TUs have been processed we can fill in the dependencies. */
8213 if (per_objfile->per_bfd->type_unit_groups != NULL)
8214 {
8215 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8216 build_type_psymtab_dependencies, per_objfile);
8217 }
8218
8219 if (dwarf_read_debug > 0)
8220 print_tu_stats (per_objfile);
8221
8222 set_partial_user (per_objfile);
8223
8224 objfile->partial_symtabs->psymtabs_addrmap
8225 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
8226 objfile->partial_symtabs->obstack ());
8227 /* At this point we want to keep the address map. */
8228 save_psymtabs_addrmap.release ();
8229
8230 dwarf_read_debug_printf ("Done building psymtabs of %s",
8231 objfile_name (objfile));
8232 }
8233
8234 /* Load the partial DIEs for a secondary CU into memory.
8235 This is also used when rereading a primary CU with load_all_dies. */
8236
8237 static void
8238 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
8239 dwarf2_per_objfile *per_objfile,
8240 dwarf2_cu *existing_cu)
8241 {
8242 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
8243
8244 if (!reader.dummy_p)
8245 {
8246 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8247 language_minimal);
8248
8249 /* Check if comp unit has_children.
8250 If so, read the rest of the partial symbols from this comp unit.
8251 If not, there's no more debug_info for this comp unit. */
8252 if (reader.comp_unit_die->has_children)
8253 load_partial_dies (&reader, reader.info_ptr, 0);
8254
8255 reader.keep ();
8256 }
8257 }
8258
8259 static void
8260 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
8261 struct dwarf2_section_info *section,
8262 struct dwarf2_section_info *abbrev_section,
8263 unsigned int is_dwz)
8264 {
8265 const gdb_byte *info_ptr;
8266 struct objfile *objfile = per_objfile->objfile;
8267
8268 dwarf_read_debug_printf ("Reading %s for %s",
8269 section->get_name (),
8270 section->get_file_name ());
8271
8272 section->read (objfile);
8273
8274 info_ptr = section->buffer;
8275
8276 while (info_ptr < section->buffer + section->size)
8277 {
8278 struct dwarf2_per_cu_data *this_cu;
8279
8280 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
8281
8282 comp_unit_head cu_header;
8283 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
8284 abbrev_section, info_ptr,
8285 rcuh_kind::COMPILE);
8286
8287 /* Save the compilation unit for later lookup. */
8288 if (cu_header.unit_type != DW_UT_type)
8289 this_cu = per_objfile->per_bfd->allocate_per_cu ();
8290 else
8291 {
8292 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
8293 sig_type->signature = cu_header.signature;
8294 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8295 this_cu = &sig_type->per_cu;
8296 }
8297 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
8298 this_cu->sect_off = sect_off;
8299 this_cu->length = cu_header.length + cu_header.initial_length_size;
8300 this_cu->is_dwz = is_dwz;
8301 this_cu->section = section;
8302
8303 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
8304
8305 info_ptr = info_ptr + this_cu->length;
8306 }
8307 }
8308
8309 /* Create a list of all compilation units in OBJFILE.
8310 This is only done for -readnow and building partial symtabs. */
8311
8312 static void
8313 create_all_comp_units (dwarf2_per_objfile *per_objfile)
8314 {
8315 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8316 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8317 &per_objfile->per_bfd->abbrev, 0);
8318
8319 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8320 if (dwz != NULL)
8321 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
8322 }
8323
8324 /* Process all loaded DIEs for compilation unit CU, starting at
8325 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
8326 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
8327 DW_AT_ranges). See the comments of add_partial_subprogram on how
8328 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
8329
8330 static void
8331 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
8332 CORE_ADDR *highpc, int set_addrmap,
8333 struct dwarf2_cu *cu)
8334 {
8335 struct partial_die_info *pdi;
8336
8337 /* Now, march along the PDI's, descending into ones which have
8338 interesting children but skipping the children of the other ones,
8339 until we reach the end of the compilation unit. */
8340
8341 pdi = first_die;
8342
8343 while (pdi != NULL)
8344 {
8345 pdi->fixup (cu);
8346
8347 /* Anonymous namespaces or modules have no name but have interesting
8348 children, so we need to look at them. Ditto for anonymous
8349 enums. */
8350
8351 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
8352 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
8353 || pdi->tag == DW_TAG_imported_unit
8354 || pdi->tag == DW_TAG_inlined_subroutine)
8355 {
8356 switch (pdi->tag)
8357 {
8358 case DW_TAG_subprogram:
8359 case DW_TAG_inlined_subroutine:
8360 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8361 if (cu->language == language_cplus)
8362 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8363 set_addrmap, cu);
8364 break;
8365 case DW_TAG_constant:
8366 case DW_TAG_variable:
8367 case DW_TAG_typedef:
8368 case DW_TAG_union_type:
8369 if (!pdi->is_declaration
8370 || (pdi->tag == DW_TAG_variable && pdi->is_external))
8371 {
8372 add_partial_symbol (pdi, cu);
8373 }
8374 break;
8375 case DW_TAG_class_type:
8376 case DW_TAG_interface_type:
8377 case DW_TAG_structure_type:
8378 if (!pdi->is_declaration)
8379 {
8380 add_partial_symbol (pdi, cu);
8381 }
8382 if ((cu->language == language_rust
8383 || cu->language == language_cplus) && pdi->has_children)
8384 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8385 set_addrmap, cu);
8386 break;
8387 case DW_TAG_enumeration_type:
8388 if (!pdi->is_declaration)
8389 add_partial_enumeration (pdi, cu);
8390 break;
8391 case DW_TAG_base_type:
8392 case DW_TAG_subrange_type:
8393 /* File scope base type definitions are added to the partial
8394 symbol table. */
8395 add_partial_symbol (pdi, cu);
8396 break;
8397 case DW_TAG_namespace:
8398 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
8399 break;
8400 case DW_TAG_module:
8401 if (!pdi->is_declaration)
8402 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
8403 break;
8404 case DW_TAG_imported_unit:
8405 {
8406 struct dwarf2_per_cu_data *per_cu;
8407
8408 /* For now we don't handle imported units in type units. */
8409 if (cu->per_cu->is_debug_types)
8410 {
8411 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8412 " supported in type units [in module %s]"),
8413 objfile_name (cu->per_objfile->objfile));
8414 }
8415
8416 per_cu = dwarf2_find_containing_comp_unit
8417 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8418
8419 /* Go read the partial unit, if needed. */
8420 if (per_cu->v.psymtab == NULL)
8421 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8422 cu->language);
8423
8424 cu->per_cu->imported_symtabs_push (per_cu);
8425 }
8426 break;
8427 case DW_TAG_imported_declaration:
8428 add_partial_symbol (pdi, cu);
8429 break;
8430 default:
8431 break;
8432 }
8433 }
8434
8435 /* If the die has a sibling, skip to the sibling. */
8436
8437 pdi = pdi->die_sibling;
8438 }
8439 }
8440
8441 /* Functions used to compute the fully scoped name of a partial DIE.
8442
8443 Normally, this is simple. For C++, the parent DIE's fully scoped
8444 name is concatenated with "::" and the partial DIE's name.
8445 Enumerators are an exception; they use the scope of their parent
8446 enumeration type, i.e. the name of the enumeration type is not
8447 prepended to the enumerator.
8448
8449 There are two complexities. One is DW_AT_specification; in this
8450 case "parent" means the parent of the target of the specification,
8451 instead of the direct parent of the DIE. The other is compilers
8452 which do not emit DW_TAG_namespace; in this case we try to guess
8453 the fully qualified name of structure types from their members'
8454 linkage names. This must be done using the DIE's children rather
8455 than the children of any DW_AT_specification target. We only need
8456 to do this for structures at the top level, i.e. if the target of
8457 any DW_AT_specification (if any; otherwise the DIE itself) does not
8458 have a parent. */
8459
8460 /* Compute the scope prefix associated with PDI's parent, in
8461 compilation unit CU. The result will be allocated on CU's
8462 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8463 field. NULL is returned if no prefix is necessary. */
8464 static const char *
8465 partial_die_parent_scope (struct partial_die_info *pdi,
8466 struct dwarf2_cu *cu)
8467 {
8468 const char *grandparent_scope;
8469 struct partial_die_info *parent, *real_pdi;
8470
8471 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8472 then this means the parent of the specification DIE. */
8473
8474 real_pdi = pdi;
8475 while (real_pdi->has_specification)
8476 {
8477 auto res = find_partial_die (real_pdi->spec_offset,
8478 real_pdi->spec_is_dwz, cu);
8479 real_pdi = res.pdi;
8480 cu = res.cu;
8481 }
8482
8483 parent = real_pdi->die_parent;
8484 if (parent == NULL)
8485 return NULL;
8486
8487 if (parent->scope_set)
8488 return parent->scope;
8489
8490 parent->fixup (cu);
8491
8492 grandparent_scope = partial_die_parent_scope (parent, cu);
8493
8494 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8495 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8496 Work around this problem here. */
8497 if (cu->language == language_cplus
8498 && parent->tag == DW_TAG_namespace
8499 && strcmp (parent->name (cu), "::") == 0
8500 && grandparent_scope == NULL)
8501 {
8502 parent->scope = NULL;
8503 parent->scope_set = 1;
8504 return NULL;
8505 }
8506
8507 /* Nested subroutines in Fortran get a prefix. */
8508 if (pdi->tag == DW_TAG_enumerator)
8509 /* Enumerators should not get the name of the enumeration as a prefix. */
8510 parent->scope = grandparent_scope;
8511 else if (parent->tag == DW_TAG_namespace
8512 || parent->tag == DW_TAG_module
8513 || parent->tag == DW_TAG_structure_type
8514 || parent->tag == DW_TAG_class_type
8515 || parent->tag == DW_TAG_interface_type
8516 || parent->tag == DW_TAG_union_type
8517 || parent->tag == DW_TAG_enumeration_type
8518 || (cu->language == language_fortran
8519 && parent->tag == DW_TAG_subprogram
8520 && pdi->tag == DW_TAG_subprogram))
8521 {
8522 if (grandparent_scope == NULL)
8523 parent->scope = parent->name (cu);
8524 else
8525 parent->scope = typename_concat (&cu->comp_unit_obstack,
8526 grandparent_scope,
8527 parent->name (cu), 0, cu);
8528 }
8529 else
8530 {
8531 /* FIXME drow/2004-04-01: What should we be doing with
8532 function-local names? For partial symbols, we should probably be
8533 ignoring them. */
8534 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8535 dwarf_tag_name (parent->tag),
8536 sect_offset_str (pdi->sect_off));
8537 parent->scope = grandparent_scope;
8538 }
8539
8540 parent->scope_set = 1;
8541 return parent->scope;
8542 }
8543
8544 /* Return the fully scoped name associated with PDI, from compilation unit
8545 CU. The result will be allocated with malloc. */
8546
8547 static gdb::unique_xmalloc_ptr<char>
8548 partial_die_full_name (struct partial_die_info *pdi,
8549 struct dwarf2_cu *cu)
8550 {
8551 const char *parent_scope;
8552
8553 /* If this is a template instantiation, we can not work out the
8554 template arguments from partial DIEs. So, unfortunately, we have
8555 to go through the full DIEs. At least any work we do building
8556 types here will be reused if full symbols are loaded later. */
8557 if (pdi->has_template_arguments)
8558 {
8559 pdi->fixup (cu);
8560
8561 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8562 {
8563 struct die_info *die;
8564 struct attribute attr;
8565 struct dwarf2_cu *ref_cu = cu;
8566
8567 /* DW_FORM_ref_addr is using section offset. */
8568 attr.name = (enum dwarf_attribute) 0;
8569 attr.form = DW_FORM_ref_addr;
8570 attr.u.unsnd = to_underlying (pdi->sect_off);
8571 die = follow_die_ref (NULL, &attr, &ref_cu);
8572
8573 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8574 }
8575 }
8576
8577 parent_scope = partial_die_parent_scope (pdi, cu);
8578 if (parent_scope == NULL)
8579 return NULL;
8580 else
8581 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8582 pdi->name (cu),
8583 0, cu));
8584 }
8585
8586 static void
8587 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8588 {
8589 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8590 struct objfile *objfile = per_objfile->objfile;
8591 struct gdbarch *gdbarch = objfile->arch ();
8592 CORE_ADDR addr = 0;
8593 const char *actual_name = NULL;
8594 CORE_ADDR baseaddr;
8595
8596 baseaddr = objfile->text_section_offset ();
8597
8598 gdb::unique_xmalloc_ptr<char> built_actual_name
8599 = partial_die_full_name (pdi, cu);
8600 if (built_actual_name != NULL)
8601 actual_name = built_actual_name.get ();
8602
8603 if (actual_name == NULL)
8604 actual_name = pdi->name (cu);
8605
8606 partial_symbol psymbol;
8607 memset (&psymbol, 0, sizeof (psymbol));
8608 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8609 psymbol.ginfo.section = -1;
8610
8611 /* The code below indicates that the psymbol should be installed by
8612 setting this. */
8613 gdb::optional<psymbol_placement> where;
8614
8615 switch (pdi->tag)
8616 {
8617 case DW_TAG_inlined_subroutine:
8618 case DW_TAG_subprogram:
8619 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8620 - baseaddr);
8621 if (pdi->is_external
8622 || cu->language == language_ada
8623 || (cu->language == language_fortran
8624 && pdi->die_parent != NULL
8625 && pdi->die_parent->tag == DW_TAG_subprogram))
8626 {
8627 /* Normally, only "external" DIEs are part of the global scope.
8628 But in Ada and Fortran, we want to be able to access nested
8629 procedures globally. So all Ada and Fortran subprograms are
8630 stored in the global scope. */
8631 where = psymbol_placement::GLOBAL;
8632 }
8633 else
8634 where = psymbol_placement::STATIC;
8635
8636 psymbol.domain = VAR_DOMAIN;
8637 psymbol.aclass = LOC_BLOCK;
8638 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8639 psymbol.ginfo.value.address = addr;
8640
8641 if (pdi->main_subprogram && actual_name != NULL)
8642 set_objfile_main_name (objfile, actual_name, cu->language);
8643 break;
8644 case DW_TAG_constant:
8645 psymbol.domain = VAR_DOMAIN;
8646 psymbol.aclass = LOC_STATIC;
8647 where = (pdi->is_external
8648 ? psymbol_placement::GLOBAL
8649 : psymbol_placement::STATIC);
8650 break;
8651 case DW_TAG_variable:
8652 if (pdi->d.locdesc)
8653 addr = decode_locdesc (pdi->d.locdesc, cu);
8654
8655 if (pdi->d.locdesc
8656 && addr == 0
8657 && !per_objfile->per_bfd->has_section_at_zero)
8658 {
8659 /* A global or static variable may also have been stripped
8660 out by the linker if unused, in which case its address
8661 will be nullified; do not add such variables into partial
8662 symbol table then. */
8663 }
8664 else if (pdi->is_external)
8665 {
8666 /* Global Variable.
8667 Don't enter into the minimal symbol tables as there is
8668 a minimal symbol table entry from the ELF symbols already.
8669 Enter into partial symbol table if it has a location
8670 descriptor or a type.
8671 If the location descriptor is missing, new_symbol will create
8672 a LOC_UNRESOLVED symbol, the address of the variable will then
8673 be determined from the minimal symbol table whenever the variable
8674 is referenced.
8675 The address for the partial symbol table entry is not
8676 used by GDB, but it comes in handy for debugging partial symbol
8677 table building. */
8678
8679 if (pdi->d.locdesc || pdi->has_type)
8680 {
8681 psymbol.domain = VAR_DOMAIN;
8682 psymbol.aclass = LOC_STATIC;
8683 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8684 psymbol.ginfo.value.address = addr;
8685 where = psymbol_placement::GLOBAL;
8686 }
8687 }
8688 else
8689 {
8690 int has_loc = pdi->d.locdesc != NULL;
8691
8692 /* Static Variable. Skip symbols whose value we cannot know (those
8693 without location descriptors or constant values). */
8694 if (!has_loc && !pdi->has_const_value)
8695 return;
8696
8697 psymbol.domain = VAR_DOMAIN;
8698 psymbol.aclass = LOC_STATIC;
8699 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8700 if (has_loc)
8701 psymbol.ginfo.value.address = addr;
8702 where = psymbol_placement::STATIC;
8703 }
8704 break;
8705 case DW_TAG_array_type:
8706 case DW_TAG_typedef:
8707 case DW_TAG_base_type:
8708 case DW_TAG_subrange_type:
8709 psymbol.domain = VAR_DOMAIN;
8710 psymbol.aclass = LOC_TYPEDEF;
8711 where = psymbol_placement::STATIC;
8712 break;
8713 case DW_TAG_imported_declaration:
8714 case DW_TAG_namespace:
8715 psymbol.domain = VAR_DOMAIN;
8716 psymbol.aclass = LOC_TYPEDEF;
8717 where = psymbol_placement::GLOBAL;
8718 break;
8719 case DW_TAG_module:
8720 /* With Fortran 77 there might be a "BLOCK DATA" module
8721 available without any name. If so, we skip the module as it
8722 doesn't bring any value. */
8723 if (actual_name != nullptr)
8724 {
8725 psymbol.domain = MODULE_DOMAIN;
8726 psymbol.aclass = LOC_TYPEDEF;
8727 where = psymbol_placement::GLOBAL;
8728 }
8729 break;
8730 case DW_TAG_class_type:
8731 case DW_TAG_interface_type:
8732 case DW_TAG_structure_type:
8733 case DW_TAG_union_type:
8734 case DW_TAG_enumeration_type:
8735 /* Skip external references. The DWARF standard says in the section
8736 about "Structure, Union, and Class Type Entries": "An incomplete
8737 structure, union or class type is represented by a structure,
8738 union or class entry that does not have a byte size attribute
8739 and that has a DW_AT_declaration attribute." */
8740 if (!pdi->has_byte_size && pdi->is_declaration)
8741 return;
8742
8743 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8744 static vs. global. */
8745 psymbol.domain = STRUCT_DOMAIN;
8746 psymbol.aclass = LOC_TYPEDEF;
8747 where = (cu->language == language_cplus
8748 ? psymbol_placement::GLOBAL
8749 : psymbol_placement::STATIC);
8750 break;
8751 case DW_TAG_enumerator:
8752 psymbol.domain = VAR_DOMAIN;
8753 psymbol.aclass = LOC_CONST;
8754 where = (cu->language == language_cplus
8755 ? psymbol_placement::GLOBAL
8756 : psymbol_placement::STATIC);
8757 break;
8758 default:
8759 break;
8760 }
8761
8762 if (where.has_value ())
8763 {
8764 if (built_actual_name != nullptr)
8765 actual_name = objfile->intern (actual_name);
8766 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8767 psymbol.ginfo.set_linkage_name (actual_name);
8768 else
8769 {
8770 psymbol.ginfo.set_demangled_name (actual_name,
8771 &objfile->objfile_obstack);
8772 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8773 }
8774 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
8775 }
8776 }
8777
8778 /* Read a partial die corresponding to a namespace; also, add a symbol
8779 corresponding to that namespace to the symbol table. NAMESPACE is
8780 the name of the enclosing namespace. */
8781
8782 static void
8783 add_partial_namespace (struct partial_die_info *pdi,
8784 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8785 int set_addrmap, struct dwarf2_cu *cu)
8786 {
8787 /* Add a symbol for the namespace. */
8788
8789 add_partial_symbol (pdi, cu);
8790
8791 /* Now scan partial symbols in that namespace. */
8792
8793 if (pdi->has_children)
8794 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8795 }
8796
8797 /* Read a partial die corresponding to a Fortran module. */
8798
8799 static void
8800 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8801 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8802 {
8803 /* Add a symbol for the namespace. */
8804
8805 add_partial_symbol (pdi, cu);
8806
8807 /* Now scan partial symbols in that module. */
8808
8809 if (pdi->has_children)
8810 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8811 }
8812
8813 /* Read a partial die corresponding to a subprogram or an inlined
8814 subprogram and create a partial symbol for that subprogram.
8815 When the CU language allows it, this routine also defines a partial
8816 symbol for each nested subprogram that this subprogram contains.
8817 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8818 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8819
8820 PDI may also be a lexical block, in which case we simply search
8821 recursively for subprograms defined inside that lexical block.
8822 Again, this is only performed when the CU language allows this
8823 type of definitions. */
8824
8825 static void
8826 add_partial_subprogram (struct partial_die_info *pdi,
8827 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8828 int set_addrmap, struct dwarf2_cu *cu)
8829 {
8830 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8831 {
8832 if (pdi->has_pc_info)
8833 {
8834 if (pdi->lowpc < *lowpc)
8835 *lowpc = pdi->lowpc;
8836 if (pdi->highpc > *highpc)
8837 *highpc = pdi->highpc;
8838 if (set_addrmap)
8839 {
8840 struct objfile *objfile = cu->per_objfile->objfile;
8841 struct gdbarch *gdbarch = objfile->arch ();
8842 CORE_ADDR baseaddr;
8843 CORE_ADDR this_highpc;
8844 CORE_ADDR this_lowpc;
8845
8846 baseaddr = objfile->text_section_offset ();
8847 this_lowpc
8848 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8849 pdi->lowpc + baseaddr)
8850 - baseaddr);
8851 this_highpc
8852 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8853 pdi->highpc + baseaddr)
8854 - baseaddr);
8855 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8856 this_lowpc, this_highpc - 1,
8857 cu->per_cu->v.psymtab);
8858 }
8859 }
8860
8861 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8862 {
8863 if (!pdi->is_declaration)
8864 /* Ignore subprogram DIEs that do not have a name, they are
8865 illegal. Do not emit a complaint at this point, we will
8866 do so when we convert this psymtab into a symtab. */
8867 if (pdi->name (cu))
8868 add_partial_symbol (pdi, cu);
8869 }
8870 }
8871
8872 if (! pdi->has_children)
8873 return;
8874
8875 if (cu->language == language_ada || cu->language == language_fortran)
8876 {
8877 pdi = pdi->die_child;
8878 while (pdi != NULL)
8879 {
8880 pdi->fixup (cu);
8881 if (pdi->tag == DW_TAG_subprogram
8882 || pdi->tag == DW_TAG_inlined_subroutine
8883 || pdi->tag == DW_TAG_lexical_block)
8884 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8885 pdi = pdi->die_sibling;
8886 }
8887 }
8888 }
8889
8890 /* Read a partial die corresponding to an enumeration type. */
8891
8892 static void
8893 add_partial_enumeration (struct partial_die_info *enum_pdi,
8894 struct dwarf2_cu *cu)
8895 {
8896 struct partial_die_info *pdi;
8897
8898 if (enum_pdi->name (cu) != NULL)
8899 add_partial_symbol (enum_pdi, cu);
8900
8901 pdi = enum_pdi->die_child;
8902 while (pdi)
8903 {
8904 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8905 complaint (_("malformed enumerator DIE ignored"));
8906 else
8907 add_partial_symbol (pdi, cu);
8908 pdi = pdi->die_sibling;
8909 }
8910 }
8911
8912 /* Return the initial uleb128 in the die at INFO_PTR. */
8913
8914 static unsigned int
8915 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8916 {
8917 unsigned int bytes_read;
8918
8919 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8920 }
8921
8922 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8923 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8924
8925 Return the corresponding abbrev, or NULL if the number is zero (indicating
8926 an empty DIE). In either case *BYTES_READ will be set to the length of
8927 the initial number. */
8928
8929 static struct abbrev_info *
8930 peek_die_abbrev (const die_reader_specs &reader,
8931 const gdb_byte *info_ptr, unsigned int *bytes_read)
8932 {
8933 dwarf2_cu *cu = reader.cu;
8934 bfd *abfd = cu->per_objfile->objfile->obfd;
8935 unsigned int abbrev_number
8936 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8937
8938 if (abbrev_number == 0)
8939 return NULL;
8940
8941 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
8942 if (!abbrev)
8943 {
8944 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8945 " at offset %s [in module %s]"),
8946 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8947 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8948 }
8949
8950 return abbrev;
8951 }
8952
8953 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8954 Returns a pointer to the end of a series of DIEs, terminated by an empty
8955 DIE. Any children of the skipped DIEs will also be skipped. */
8956
8957 static const gdb_byte *
8958 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8959 {
8960 while (1)
8961 {
8962 unsigned int bytes_read;
8963 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8964
8965 if (abbrev == NULL)
8966 return info_ptr + bytes_read;
8967 else
8968 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8969 }
8970 }
8971
8972 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8973 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8974 abbrev corresponding to that skipped uleb128 should be passed in
8975 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8976 children. */
8977
8978 static const gdb_byte *
8979 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8980 struct abbrev_info *abbrev)
8981 {
8982 unsigned int bytes_read;
8983 struct attribute attr;
8984 bfd *abfd = reader->abfd;
8985 struct dwarf2_cu *cu = reader->cu;
8986 const gdb_byte *buffer = reader->buffer;
8987 const gdb_byte *buffer_end = reader->buffer_end;
8988 unsigned int form, i;
8989
8990 for (i = 0; i < abbrev->num_attrs; i++)
8991 {
8992 /* The only abbrev we care about is DW_AT_sibling. */
8993 if (abbrev->attrs[i].name == DW_AT_sibling)
8994 {
8995 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8996 if (attr.form == DW_FORM_ref_addr)
8997 complaint (_("ignoring absolute DW_AT_sibling"));
8998 else
8999 {
9000 sect_offset off = attr.get_ref_die_offset ();
9001 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
9002
9003 if (sibling_ptr < info_ptr)
9004 complaint (_("DW_AT_sibling points backwards"));
9005 else if (sibling_ptr > reader->buffer_end)
9006 reader->die_section->overflow_complaint ();
9007 else
9008 return sibling_ptr;
9009 }
9010 }
9011
9012 /* If it isn't DW_AT_sibling, skip this attribute. */
9013 form = abbrev->attrs[i].form;
9014 skip_attribute:
9015 switch (form)
9016 {
9017 case DW_FORM_ref_addr:
9018 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9019 and later it is offset sized. */
9020 if (cu->header.version == 2)
9021 info_ptr += cu->header.addr_size;
9022 else
9023 info_ptr += cu->header.offset_size;
9024 break;
9025 case DW_FORM_GNU_ref_alt:
9026 info_ptr += cu->header.offset_size;
9027 break;
9028 case DW_FORM_addr:
9029 info_ptr += cu->header.addr_size;
9030 break;
9031 case DW_FORM_data1:
9032 case DW_FORM_ref1:
9033 case DW_FORM_flag:
9034 case DW_FORM_strx1:
9035 info_ptr += 1;
9036 break;
9037 case DW_FORM_flag_present:
9038 case DW_FORM_implicit_const:
9039 break;
9040 case DW_FORM_data2:
9041 case DW_FORM_ref2:
9042 case DW_FORM_strx2:
9043 info_ptr += 2;
9044 break;
9045 case DW_FORM_strx3:
9046 info_ptr += 3;
9047 break;
9048 case DW_FORM_data4:
9049 case DW_FORM_ref4:
9050 case DW_FORM_strx4:
9051 info_ptr += 4;
9052 break;
9053 case DW_FORM_data8:
9054 case DW_FORM_ref8:
9055 case DW_FORM_ref_sig8:
9056 info_ptr += 8;
9057 break;
9058 case DW_FORM_data16:
9059 info_ptr += 16;
9060 break;
9061 case DW_FORM_string:
9062 read_direct_string (abfd, info_ptr, &bytes_read);
9063 info_ptr += bytes_read;
9064 break;
9065 case DW_FORM_sec_offset:
9066 case DW_FORM_strp:
9067 case DW_FORM_GNU_strp_alt:
9068 info_ptr += cu->header.offset_size;
9069 break;
9070 case DW_FORM_exprloc:
9071 case DW_FORM_block:
9072 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9073 info_ptr += bytes_read;
9074 break;
9075 case DW_FORM_block1:
9076 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9077 break;
9078 case DW_FORM_block2:
9079 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9080 break;
9081 case DW_FORM_block4:
9082 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9083 break;
9084 case DW_FORM_addrx:
9085 case DW_FORM_strx:
9086 case DW_FORM_sdata:
9087 case DW_FORM_udata:
9088 case DW_FORM_ref_udata:
9089 case DW_FORM_GNU_addr_index:
9090 case DW_FORM_GNU_str_index:
9091 case DW_FORM_rnglistx:
9092 case DW_FORM_loclistx:
9093 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
9094 break;
9095 case DW_FORM_indirect:
9096 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9097 info_ptr += bytes_read;
9098 /* We need to continue parsing from here, so just go back to
9099 the top. */
9100 goto skip_attribute;
9101
9102 default:
9103 error (_("Dwarf Error: Cannot handle %s "
9104 "in DWARF reader [in module %s]"),
9105 dwarf_form_name (form),
9106 bfd_get_filename (abfd));
9107 }
9108 }
9109
9110 if (abbrev->has_children)
9111 return skip_children (reader, info_ptr);
9112 else
9113 return info_ptr;
9114 }
9115
9116 /* Locate ORIG_PDI's sibling.
9117 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
9118
9119 static const gdb_byte *
9120 locate_pdi_sibling (const struct die_reader_specs *reader,
9121 struct partial_die_info *orig_pdi,
9122 const gdb_byte *info_ptr)
9123 {
9124 /* Do we know the sibling already? */
9125
9126 if (orig_pdi->sibling)
9127 return orig_pdi->sibling;
9128
9129 /* Are there any children to deal with? */
9130
9131 if (!orig_pdi->has_children)
9132 return info_ptr;
9133
9134 /* Skip the children the long way. */
9135
9136 return skip_children (reader, info_ptr);
9137 }
9138
9139 /* Expand this partial symbol table into a full symbol table. SELF is
9140 not NULL. */
9141
9142 void
9143 dwarf2_psymtab::read_symtab (struct objfile *objfile)
9144 {
9145 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9146
9147 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
9148
9149 /* If this psymtab is constructed from a debug-only objfile, the
9150 has_section_at_zero flag will not necessarily be correct. We
9151 can get the correct value for this flag by looking at the data
9152 associated with the (presumably stripped) associated objfile. */
9153 if (objfile->separate_debug_objfile_backlink)
9154 {
9155 dwarf2_per_objfile *per_objfile_backlink
9156 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9157
9158 per_objfile->per_bfd->has_section_at_zero
9159 = per_objfile_backlink->per_bfd->has_section_at_zero;
9160 }
9161
9162 expand_psymtab (objfile);
9163
9164 process_cu_includes (per_objfile);
9165 }
9166 \f
9167 /* Reading in full CUs. */
9168
9169 /* Add PER_CU to the queue. */
9170
9171 static void
9172 queue_comp_unit (dwarf2_per_cu_data *per_cu,
9173 dwarf2_per_objfile *per_objfile,
9174 enum language pretend_language)
9175 {
9176 per_cu->queued = 1;
9177 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
9178 }
9179
9180 /* If PER_CU is not yet queued, add it to the queue.
9181 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9182 dependency.
9183 The result is non-zero if PER_CU was queued, otherwise the result is zero
9184 meaning either PER_CU is already queued or it is already loaded.
9185
9186 N.B. There is an invariant here that if a CU is queued then it is loaded.
9187 The caller is required to load PER_CU if we return non-zero. */
9188
9189 static int
9190 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
9191 dwarf2_per_cu_data *per_cu,
9192 dwarf2_per_objfile *per_objfile,
9193 enum language pretend_language)
9194 {
9195 /* We may arrive here during partial symbol reading, if we need full
9196 DIEs to process an unusual case (e.g. template arguments). Do
9197 not queue PER_CU, just tell our caller to load its DIEs. */
9198 if (per_cu->per_bfd->reading_partial_symbols)
9199 {
9200 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9201
9202 if (cu == NULL || cu->dies == NULL)
9203 return 1;
9204 return 0;
9205 }
9206
9207 /* Mark the dependence relation so that we don't flush PER_CU
9208 too early. */
9209 if (dependent_cu != NULL)
9210 dwarf2_add_dependence (dependent_cu, per_cu);
9211
9212 /* If it's already on the queue, we have nothing to do. */
9213 if (per_cu->queued)
9214 {
9215 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9216 loaded. */
9217 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9218 return 0;
9219 }
9220
9221 /* If the compilation unit is already loaded, just mark it as
9222 used. */
9223 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9224 if (cu != nullptr)
9225 {
9226 cu->last_used = 0;
9227 return 0;
9228 }
9229
9230 /* Add it to the queue. */
9231 queue_comp_unit (per_cu, per_objfile, pretend_language);
9232
9233 return 1;
9234 }
9235
9236 /* Process the queue. */
9237
9238 static void
9239 process_queue (dwarf2_per_objfile *per_objfile)
9240 {
9241 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9242 objfile_name (per_objfile->objfile));
9243
9244 /* The queue starts out with one item, but following a DIE reference
9245 may load a new CU, adding it to the end of the queue. */
9246 while (!per_objfile->per_bfd->queue.empty ())
9247 {
9248 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
9249 dwarf2_per_cu_data *per_cu = item.per_cu;
9250
9251 if (!per_objfile->symtab_set_p (per_cu))
9252 {
9253 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9254
9255 /* Skip dummy CUs. */
9256 if (cu != nullptr)
9257 {
9258 unsigned int debug_print_threshold;
9259 char buf[100];
9260
9261 if (per_cu->is_debug_types)
9262 {
9263 struct signatured_type *sig_type =
9264 (struct signatured_type *) per_cu;
9265
9266 sprintf (buf, "TU %s at offset %s",
9267 hex_string (sig_type->signature),
9268 sect_offset_str (per_cu->sect_off));
9269 /* There can be 100s of TUs.
9270 Only print them in verbose mode. */
9271 debug_print_threshold = 2;
9272 }
9273 else
9274 {
9275 sprintf (buf, "CU at offset %s",
9276 sect_offset_str (per_cu->sect_off));
9277 debug_print_threshold = 1;
9278 }
9279
9280 if (dwarf_read_debug >= debug_print_threshold)
9281 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
9282
9283 if (per_cu->is_debug_types)
9284 process_full_type_unit (cu, item.pretend_language);
9285 else
9286 process_full_comp_unit (cu, item.pretend_language);
9287
9288 if (dwarf_read_debug >= debug_print_threshold)
9289 dwarf_read_debug_printf ("Done expanding %s", buf);
9290 }
9291 }
9292
9293 per_cu->queued = 0;
9294 per_objfile->per_bfd->queue.pop ();
9295 }
9296
9297 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9298 objfile_name (per_objfile->objfile));
9299 }
9300
9301 /* Read in full symbols for PST, and anything it depends on. */
9302
9303 void
9304 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
9305 {
9306 gdb_assert (!readin_p (objfile));
9307
9308 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9309 free_cached_comp_units freer (per_objfile);
9310 expand_dependencies (objfile);
9311
9312 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
9313 gdb_assert (get_compunit_symtab (objfile) != nullptr);
9314 }
9315
9316 /* See psympriv.h. */
9317
9318 bool
9319 dwarf2_psymtab::readin_p (struct objfile *objfile) const
9320 {
9321 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9322 return per_objfile->symtab_set_p (per_cu_data);
9323 }
9324
9325 /* See psympriv.h. */
9326
9327 compunit_symtab *
9328 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9329 {
9330 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9331 return per_objfile->get_symtab (per_cu_data);
9332 }
9333
9334 /* Trivial hash function for die_info: the hash value of a DIE
9335 is its offset in .debug_info for this objfile. */
9336
9337 static hashval_t
9338 die_hash (const void *item)
9339 {
9340 const struct die_info *die = (const struct die_info *) item;
9341
9342 return to_underlying (die->sect_off);
9343 }
9344
9345 /* Trivial comparison function for die_info structures: two DIEs
9346 are equal if they have the same offset. */
9347
9348 static int
9349 die_eq (const void *item_lhs, const void *item_rhs)
9350 {
9351 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9352 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
9353
9354 return die_lhs->sect_off == die_rhs->sect_off;
9355 }
9356
9357 /* Load the DIEs associated with PER_CU into memory.
9358
9359 In some cases, the caller, while reading partial symbols, will need to load
9360 the full symbols for the CU for some reason. It will already have a
9361 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9362 rather than creating a new one. */
9363
9364 static void
9365 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9366 dwarf2_per_objfile *per_objfile,
9367 dwarf2_cu *existing_cu,
9368 bool skip_partial,
9369 enum language pretend_language)
9370 {
9371 gdb_assert (! this_cu->is_debug_types);
9372
9373 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
9374 if (reader.dummy_p)
9375 return;
9376
9377 struct dwarf2_cu *cu = reader.cu;
9378 const gdb_byte *info_ptr = reader.info_ptr;
9379
9380 gdb_assert (cu->die_hash == NULL);
9381 cu->die_hash =
9382 htab_create_alloc_ex (cu->header.length / 12,
9383 die_hash,
9384 die_eq,
9385 NULL,
9386 &cu->comp_unit_obstack,
9387 hashtab_obstack_allocate,
9388 dummy_obstack_deallocate);
9389
9390 if (reader.comp_unit_die->has_children)
9391 reader.comp_unit_die->child
9392 = read_die_and_siblings (&reader, reader.info_ptr,
9393 &info_ptr, reader.comp_unit_die);
9394 cu->dies = reader.comp_unit_die;
9395 /* comp_unit_die is not stored in die_hash, no need. */
9396
9397 /* We try not to read any attributes in this function, because not
9398 all CUs needed for references have been loaded yet, and symbol
9399 table processing isn't initialized. But we have to set the CU language,
9400 or we won't be able to build types correctly.
9401 Similarly, if we do not read the producer, we can not apply
9402 producer-specific interpretation. */
9403 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9404
9405 reader.keep ();
9406 }
9407
9408 /* Add a DIE to the delayed physname list. */
9409
9410 static void
9411 add_to_method_list (struct type *type, int fnfield_index, int index,
9412 const char *name, struct die_info *die,
9413 struct dwarf2_cu *cu)
9414 {
9415 struct delayed_method_info mi;
9416 mi.type = type;
9417 mi.fnfield_index = fnfield_index;
9418 mi.index = index;
9419 mi.name = name;
9420 mi.die = die;
9421 cu->method_list.push_back (mi);
9422 }
9423
9424 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9425 "const" / "volatile". If so, decrements LEN by the length of the
9426 modifier and return true. Otherwise return false. */
9427
9428 template<size_t N>
9429 static bool
9430 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9431 {
9432 size_t mod_len = sizeof (mod) - 1;
9433 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9434 {
9435 len -= mod_len;
9436 return true;
9437 }
9438 return false;
9439 }
9440
9441 /* Compute the physnames of any methods on the CU's method list.
9442
9443 The computation of method physnames is delayed in order to avoid the
9444 (bad) condition that one of the method's formal parameters is of an as yet
9445 incomplete type. */
9446
9447 static void
9448 compute_delayed_physnames (struct dwarf2_cu *cu)
9449 {
9450 /* Only C++ delays computing physnames. */
9451 if (cu->method_list.empty ())
9452 return;
9453 gdb_assert (cu->language == language_cplus);
9454
9455 for (const delayed_method_info &mi : cu->method_list)
9456 {
9457 const char *physname;
9458 struct fn_fieldlist *fn_flp
9459 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9460 physname = dwarf2_physname (mi.name, mi.die, cu);
9461 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9462 = physname ? physname : "";
9463
9464 /* Since there's no tag to indicate whether a method is a
9465 const/volatile overload, extract that information out of the
9466 demangled name. */
9467 if (physname != NULL)
9468 {
9469 size_t len = strlen (physname);
9470
9471 while (1)
9472 {
9473 if (physname[len] == ')') /* shortcut */
9474 break;
9475 else if (check_modifier (physname, len, " const"))
9476 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9477 else if (check_modifier (physname, len, " volatile"))
9478 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9479 else
9480 break;
9481 }
9482 }
9483 }
9484
9485 /* The list is no longer needed. */
9486 cu->method_list.clear ();
9487 }
9488
9489 /* Go objects should be embedded in a DW_TAG_module DIE,
9490 and it's not clear if/how imported objects will appear.
9491 To keep Go support simple until that's worked out,
9492 go back through what we've read and create something usable.
9493 We could do this while processing each DIE, and feels kinda cleaner,
9494 but that way is more invasive.
9495 This is to, for example, allow the user to type "p var" or "b main"
9496 without having to specify the package name, and allow lookups
9497 of module.object to work in contexts that use the expression
9498 parser. */
9499
9500 static void
9501 fixup_go_packaging (struct dwarf2_cu *cu)
9502 {
9503 gdb::unique_xmalloc_ptr<char> package_name;
9504 struct pending *list;
9505 int i;
9506
9507 for (list = *cu->get_builder ()->get_global_symbols ();
9508 list != NULL;
9509 list = list->next)
9510 {
9511 for (i = 0; i < list->nsyms; ++i)
9512 {
9513 struct symbol *sym = list->symbol[i];
9514
9515 if (sym->language () == language_go
9516 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9517 {
9518 gdb::unique_xmalloc_ptr<char> this_package_name
9519 (go_symbol_package_name (sym));
9520
9521 if (this_package_name == NULL)
9522 continue;
9523 if (package_name == NULL)
9524 package_name = std::move (this_package_name);
9525 else
9526 {
9527 struct objfile *objfile = cu->per_objfile->objfile;
9528 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9529 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9530 (symbol_symtab (sym) != NULL
9531 ? symtab_to_filename_for_display
9532 (symbol_symtab (sym))
9533 : objfile_name (objfile)),
9534 this_package_name.get (), package_name.get ());
9535 }
9536 }
9537 }
9538 }
9539
9540 if (package_name != NULL)
9541 {
9542 struct objfile *objfile = cu->per_objfile->objfile;
9543 const char *saved_package_name = objfile->intern (package_name.get ());
9544 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9545 saved_package_name);
9546 struct symbol *sym;
9547
9548 sym = new (&objfile->objfile_obstack) symbol;
9549 sym->set_language (language_go, &objfile->objfile_obstack);
9550 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9551 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9552 e.g., "main" finds the "main" module and not C's main(). */
9553 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9554 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9555 SYMBOL_TYPE (sym) = type;
9556
9557 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9558 }
9559 }
9560
9561 /* Allocate a fully-qualified name consisting of the two parts on the
9562 obstack. */
9563
9564 static const char *
9565 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9566 {
9567 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9568 }
9569
9570 /* A helper that allocates a variant part to attach to a Rust enum
9571 type. OBSTACK is where the results should be allocated. TYPE is
9572 the type we're processing. DISCRIMINANT_INDEX is the index of the
9573 discriminant. It must be the index of one of the fields of TYPE,
9574 or -1 to mean there is no discriminant (univariant enum).
9575 DEFAULT_INDEX is the index of the default field; or -1 if there is
9576 no default. RANGES is indexed by "effective" field number (the
9577 field index, but omitting the discriminant and default fields) and
9578 must hold the discriminant values used by the variants. Note that
9579 RANGES must have a lifetime at least as long as OBSTACK -- either
9580 already allocated on it, or static. */
9581
9582 static void
9583 alloc_rust_variant (struct obstack *obstack, struct type *type,
9584 int discriminant_index, int default_index,
9585 gdb::array_view<discriminant_range> ranges)
9586 {
9587 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9588 gdb_assert (discriminant_index == -1
9589 || (discriminant_index >= 0
9590 && discriminant_index < type->num_fields ()));
9591 gdb_assert (default_index == -1
9592 || (default_index >= 0 && default_index < type->num_fields ()));
9593
9594 /* We have one variant for each non-discriminant field. */
9595 int n_variants = type->num_fields ();
9596 if (discriminant_index != -1)
9597 --n_variants;
9598
9599 variant *variants = new (obstack) variant[n_variants];
9600 int var_idx = 0;
9601 int range_idx = 0;
9602 for (int i = 0; i < type->num_fields (); ++i)
9603 {
9604 if (i == discriminant_index)
9605 continue;
9606
9607 variants[var_idx].first_field = i;
9608 variants[var_idx].last_field = i + 1;
9609
9610 /* The default field does not need a range, but other fields do.
9611 We skipped the discriminant above. */
9612 if (i != default_index)
9613 {
9614 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9615 ++range_idx;
9616 }
9617
9618 ++var_idx;
9619 }
9620
9621 gdb_assert (range_idx == ranges.size ());
9622 gdb_assert (var_idx == n_variants);
9623
9624 variant_part *part = new (obstack) variant_part;
9625 part->discriminant_index = discriminant_index;
9626 /* If there is no discriminant, then whether it is signed is of no
9627 consequence. */
9628 part->is_unsigned
9629 = (discriminant_index == -1
9630 ? false
9631 : type->field (discriminant_index).type ()->is_unsigned ());
9632 part->variants = gdb::array_view<variant> (variants, n_variants);
9633
9634 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9635 gdb::array_view<variant_part> *prop_value
9636 = new (storage) gdb::array_view<variant_part> (part, 1);
9637
9638 struct dynamic_prop prop;
9639 prop.set_variant_parts (prop_value);
9640
9641 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9642 }
9643
9644 /* Some versions of rustc emitted enums in an unusual way.
9645
9646 Ordinary enums were emitted as unions. The first element of each
9647 structure in the union was named "RUST$ENUM$DISR". This element
9648 held the discriminant.
9649
9650 These versions of Rust also implemented the "non-zero"
9651 optimization. When the enum had two values, and one is empty and
9652 the other holds a pointer that cannot be zero, the pointer is used
9653 as the discriminant, with a zero value meaning the empty variant.
9654 Here, the union's first member is of the form
9655 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9656 where the fieldnos are the indices of the fields that should be
9657 traversed in order to find the field (which may be several fields deep)
9658 and the variantname is the name of the variant of the case when the
9659 field is zero.
9660
9661 This function recognizes whether TYPE is of one of these forms,
9662 and, if so, smashes it to be a variant type. */
9663
9664 static void
9665 quirk_rust_enum (struct type *type, struct objfile *objfile)
9666 {
9667 gdb_assert (type->code () == TYPE_CODE_UNION);
9668
9669 /* We don't need to deal with empty enums. */
9670 if (type->num_fields () == 0)
9671 return;
9672
9673 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9674 if (type->num_fields () == 1
9675 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9676 {
9677 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9678
9679 /* Decode the field name to find the offset of the
9680 discriminant. */
9681 ULONGEST bit_offset = 0;
9682 struct type *field_type = type->field (0).type ();
9683 while (name[0] >= '0' && name[0] <= '9')
9684 {
9685 char *tail;
9686 unsigned long index = strtoul (name, &tail, 10);
9687 name = tail;
9688 if (*name != '$'
9689 || index >= field_type->num_fields ()
9690 || (TYPE_FIELD_LOC_KIND (field_type, index)
9691 != FIELD_LOC_KIND_BITPOS))
9692 {
9693 complaint (_("Could not parse Rust enum encoding string \"%s\""
9694 "[in module %s]"),
9695 TYPE_FIELD_NAME (type, 0),
9696 objfile_name (objfile));
9697 return;
9698 }
9699 ++name;
9700
9701 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9702 field_type = field_type->field (index).type ();
9703 }
9704
9705 /* Smash this type to be a structure type. We have to do this
9706 because the type has already been recorded. */
9707 type->set_code (TYPE_CODE_STRUCT);
9708 type->set_num_fields (3);
9709 /* Save the field we care about. */
9710 struct field saved_field = type->field (0);
9711 type->set_fields
9712 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9713
9714 /* Put the discriminant at index 0. */
9715 type->field (0).set_type (field_type);
9716 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9717 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9718 SET_FIELD_BITPOS (type->field (0), bit_offset);
9719
9720 /* The order of fields doesn't really matter, so put the real
9721 field at index 1 and the data-less field at index 2. */
9722 type->field (1) = saved_field;
9723 TYPE_FIELD_NAME (type, 1)
9724 = rust_last_path_segment (type->field (1).type ()->name ());
9725 type->field (1).type ()->set_name
9726 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9727 TYPE_FIELD_NAME (type, 1)));
9728
9729 const char *dataless_name
9730 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9731 name);
9732 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9733 dataless_name);
9734 type->field (2).set_type (dataless_type);
9735 /* NAME points into the original discriminant name, which
9736 already has the correct lifetime. */
9737 TYPE_FIELD_NAME (type, 2) = name;
9738 SET_FIELD_BITPOS (type->field (2), 0);
9739
9740 /* Indicate that this is a variant type. */
9741 static discriminant_range ranges[1] = { { 0, 0 } };
9742 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9743 }
9744 /* A union with a single anonymous field is probably an old-style
9745 univariant enum. */
9746 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9747 {
9748 /* Smash this type to be a structure type. We have to do this
9749 because the type has already been recorded. */
9750 type->set_code (TYPE_CODE_STRUCT);
9751
9752 struct type *field_type = type->field (0).type ();
9753 const char *variant_name
9754 = rust_last_path_segment (field_type->name ());
9755 TYPE_FIELD_NAME (type, 0) = variant_name;
9756 field_type->set_name
9757 (rust_fully_qualify (&objfile->objfile_obstack,
9758 type->name (), variant_name));
9759
9760 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9761 }
9762 else
9763 {
9764 struct type *disr_type = nullptr;
9765 for (int i = 0; i < type->num_fields (); ++i)
9766 {
9767 disr_type = type->field (i).type ();
9768
9769 if (disr_type->code () != TYPE_CODE_STRUCT)
9770 {
9771 /* All fields of a true enum will be structs. */
9772 return;
9773 }
9774 else if (disr_type->num_fields () == 0)
9775 {
9776 /* Could be data-less variant, so keep going. */
9777 disr_type = nullptr;
9778 }
9779 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9780 "RUST$ENUM$DISR") != 0)
9781 {
9782 /* Not a Rust enum. */
9783 return;
9784 }
9785 else
9786 {
9787 /* Found one. */
9788 break;
9789 }
9790 }
9791
9792 /* If we got here without a discriminant, then it's probably
9793 just a union. */
9794 if (disr_type == nullptr)
9795 return;
9796
9797 /* Smash this type to be a structure type. We have to do this
9798 because the type has already been recorded. */
9799 type->set_code (TYPE_CODE_STRUCT);
9800
9801 /* Make space for the discriminant field. */
9802 struct field *disr_field = &disr_type->field (0);
9803 field *new_fields
9804 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9805 * sizeof (struct field)));
9806 memcpy (new_fields + 1, type->fields (),
9807 type->num_fields () * sizeof (struct field));
9808 type->set_fields (new_fields);
9809 type->set_num_fields (type->num_fields () + 1);
9810
9811 /* Install the discriminant at index 0 in the union. */
9812 type->field (0) = *disr_field;
9813 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9814 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9815
9816 /* We need a way to find the correct discriminant given a
9817 variant name. For convenience we build a map here. */
9818 struct type *enum_type = disr_field->type ();
9819 std::unordered_map<std::string, ULONGEST> discriminant_map;
9820 for (int i = 0; i < enum_type->num_fields (); ++i)
9821 {
9822 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9823 {
9824 const char *name
9825 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9826 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9827 }
9828 }
9829
9830 int n_fields = type->num_fields ();
9831 /* We don't need a range entry for the discriminant, but we do
9832 need one for every other field, as there is no default
9833 variant. */
9834 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9835 discriminant_range,
9836 n_fields - 1);
9837 /* Skip the discriminant here. */
9838 for (int i = 1; i < n_fields; ++i)
9839 {
9840 /* Find the final word in the name of this variant's type.
9841 That name can be used to look up the correct
9842 discriminant. */
9843 const char *variant_name
9844 = rust_last_path_segment (type->field (i).type ()->name ());
9845
9846 auto iter = discriminant_map.find (variant_name);
9847 if (iter != discriminant_map.end ())
9848 {
9849 ranges[i - 1].low = iter->second;
9850 ranges[i - 1].high = iter->second;
9851 }
9852
9853 /* In Rust, each element should have the size of the
9854 enclosing enum. */
9855 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9856
9857 /* Remove the discriminant field, if it exists. */
9858 struct type *sub_type = type->field (i).type ();
9859 if (sub_type->num_fields () > 0)
9860 {
9861 sub_type->set_num_fields (sub_type->num_fields () - 1);
9862 sub_type->set_fields (sub_type->fields () + 1);
9863 }
9864 TYPE_FIELD_NAME (type, i) = variant_name;
9865 sub_type->set_name
9866 (rust_fully_qualify (&objfile->objfile_obstack,
9867 type->name (), variant_name));
9868 }
9869
9870 /* Indicate that this is a variant type. */
9871 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9872 gdb::array_view<discriminant_range> (ranges,
9873 n_fields - 1));
9874 }
9875 }
9876
9877 /* Rewrite some Rust unions to be structures with variants parts. */
9878
9879 static void
9880 rust_union_quirks (struct dwarf2_cu *cu)
9881 {
9882 gdb_assert (cu->language == language_rust);
9883 for (type *type_ : cu->rust_unions)
9884 quirk_rust_enum (type_, cu->per_objfile->objfile);
9885 /* We don't need this any more. */
9886 cu->rust_unions.clear ();
9887 }
9888
9889 /* See read.h. */
9890
9891 type_unit_group_unshareable *
9892 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9893 {
9894 auto iter = this->m_type_units.find (tu_group);
9895 if (iter != this->m_type_units.end ())
9896 return iter->second.get ();
9897
9898 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9899 type_unit_group_unshareable *result = uniq.get ();
9900 this->m_type_units[tu_group] = std::move (uniq);
9901 return result;
9902 }
9903
9904 struct type *
9905 dwarf2_per_objfile::get_type_for_signatured_type
9906 (signatured_type *sig_type) const
9907 {
9908 auto iter = this->m_type_map.find (sig_type);
9909 if (iter == this->m_type_map.end ())
9910 return nullptr;
9911
9912 return iter->second;
9913 }
9914
9915 void dwarf2_per_objfile::set_type_for_signatured_type
9916 (signatured_type *sig_type, struct type *type)
9917 {
9918 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9919
9920 this->m_type_map[sig_type] = type;
9921 }
9922
9923 /* A helper function for computing the list of all symbol tables
9924 included by PER_CU. */
9925
9926 static void
9927 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9928 htab_t all_children, htab_t all_type_symtabs,
9929 dwarf2_per_cu_data *per_cu,
9930 dwarf2_per_objfile *per_objfile,
9931 struct compunit_symtab *immediate_parent)
9932 {
9933 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9934 if (*slot != NULL)
9935 {
9936 /* This inclusion and its children have been processed. */
9937 return;
9938 }
9939
9940 *slot = per_cu;
9941
9942 /* Only add a CU if it has a symbol table. */
9943 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9944 if (cust != NULL)
9945 {
9946 /* If this is a type unit only add its symbol table if we haven't
9947 seen it yet (type unit per_cu's can share symtabs). */
9948 if (per_cu->is_debug_types)
9949 {
9950 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9951 if (*slot == NULL)
9952 {
9953 *slot = cust;
9954 result->push_back (cust);
9955 if (cust->user == NULL)
9956 cust->user = immediate_parent;
9957 }
9958 }
9959 else
9960 {
9961 result->push_back (cust);
9962 if (cust->user == NULL)
9963 cust->user = immediate_parent;
9964 }
9965 }
9966
9967 if (!per_cu->imported_symtabs_empty ())
9968 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9969 {
9970 recursively_compute_inclusions (result, all_children,
9971 all_type_symtabs, ptr, per_objfile,
9972 cust);
9973 }
9974 }
9975
9976 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9977 PER_CU. */
9978
9979 static void
9980 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9981 dwarf2_per_objfile *per_objfile)
9982 {
9983 gdb_assert (! per_cu->is_debug_types);
9984
9985 if (!per_cu->imported_symtabs_empty ())
9986 {
9987 int len;
9988 std::vector<compunit_symtab *> result_symtabs;
9989 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9990
9991 /* If we don't have a symtab, we can just skip this case. */
9992 if (cust == NULL)
9993 return;
9994
9995 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9996 htab_eq_pointer,
9997 NULL, xcalloc, xfree));
9998 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9999 htab_eq_pointer,
10000 NULL, xcalloc, xfree));
10001
10002 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10003 {
10004 recursively_compute_inclusions (&result_symtabs, all_children.get (),
10005 all_type_symtabs.get (), ptr,
10006 per_objfile, cust);
10007 }
10008
10009 /* Now we have a transitive closure of all the included symtabs. */
10010 len = result_symtabs.size ();
10011 cust->includes
10012 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
10013 struct compunit_symtab *, len + 1);
10014 memcpy (cust->includes, result_symtabs.data (),
10015 len * sizeof (compunit_symtab *));
10016 cust->includes[len] = NULL;
10017 }
10018 }
10019
10020 /* Compute the 'includes' field for the symtabs of all the CUs we just
10021 read. */
10022
10023 static void
10024 process_cu_includes (dwarf2_per_objfile *per_objfile)
10025 {
10026 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
10027 {
10028 if (! iter->is_debug_types)
10029 compute_compunit_symtab_includes (iter, per_objfile);
10030 }
10031
10032 per_objfile->per_bfd->just_read_cus.clear ();
10033 }
10034
10035 /* Generate full symbol information for CU, whose DIEs have
10036 already been loaded into memory. */
10037
10038 static void
10039 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10040 {
10041 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10042 struct objfile *objfile = per_objfile->objfile;
10043 struct gdbarch *gdbarch = objfile->arch ();
10044 CORE_ADDR lowpc, highpc;
10045 struct compunit_symtab *cust;
10046 CORE_ADDR baseaddr;
10047 struct block *static_block;
10048 CORE_ADDR addr;
10049
10050 baseaddr = objfile->text_section_offset ();
10051
10052 /* Clear the list here in case something was left over. */
10053 cu->method_list.clear ();
10054
10055 cu->language = pretend_language;
10056 cu->language_defn = language_def (cu->language);
10057
10058 dwarf2_find_base_address (cu->dies, cu);
10059
10060 /* Do line number decoding in read_file_scope () */
10061 process_die (cu->dies, cu);
10062
10063 /* For now fudge the Go package. */
10064 if (cu->language == language_go)
10065 fixup_go_packaging (cu);
10066
10067 /* Now that we have processed all the DIEs in the CU, all the types
10068 should be complete, and it should now be safe to compute all of the
10069 physnames. */
10070 compute_delayed_physnames (cu);
10071
10072 if (cu->language == language_rust)
10073 rust_union_quirks (cu);
10074
10075 /* Some compilers don't define a DW_AT_high_pc attribute for the
10076 compilation unit. If the DW_AT_high_pc is missing, synthesize
10077 it, by scanning the DIE's below the compilation unit. */
10078 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
10079
10080 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10081 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
10082
10083 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10084 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10085 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10086 addrmap to help ensure it has an accurate map of pc values belonging to
10087 this comp unit. */
10088 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10089
10090 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
10091 SECT_OFF_TEXT (objfile),
10092 0);
10093
10094 if (cust != NULL)
10095 {
10096 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
10097
10098 /* Set symtab language to language from DW_AT_language. If the
10099 compilation is from a C file generated by language preprocessors, do
10100 not set the language if it was already deduced by start_subfile. */
10101 if (!(cu->language == language_c
10102 && COMPUNIT_FILETABS (cust)->language != language_unknown))
10103 COMPUNIT_FILETABS (cust)->language = cu->language;
10104
10105 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10106 produce DW_AT_location with location lists but it can be possibly
10107 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10108 there were bugs in prologue debug info, fixed later in GCC-4.5
10109 by "unwind info for epilogues" patch (which is not directly related).
10110
10111 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10112 needed, it would be wrong due to missing DW_AT_producer there.
10113
10114 Still one can confuse GDB by using non-standard GCC compilation
10115 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10116 */
10117 if (cu->has_loclist && gcc_4_minor >= 5)
10118 cust->locations_valid = 1;
10119
10120 if (gcc_4_minor >= 5)
10121 cust->epilogue_unwind_valid = 1;
10122
10123 cust->call_site_htab = cu->call_site_htab;
10124 }
10125
10126 per_objfile->set_symtab (cu->per_cu, cust);
10127
10128 /* Push it for inclusion processing later. */
10129 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
10130
10131 /* Not needed any more. */
10132 cu->reset_builder ();
10133 }
10134
10135 /* Generate full symbol information for type unit CU, whose DIEs have
10136 already been loaded into memory. */
10137
10138 static void
10139 process_full_type_unit (dwarf2_cu *cu,
10140 enum language pretend_language)
10141 {
10142 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10143 struct objfile *objfile = per_objfile->objfile;
10144 struct compunit_symtab *cust;
10145 struct signatured_type *sig_type;
10146
10147 gdb_assert (cu->per_cu->is_debug_types);
10148 sig_type = (struct signatured_type *) cu->per_cu;
10149
10150 /* Clear the list here in case something was left over. */
10151 cu->method_list.clear ();
10152
10153 cu->language = pretend_language;
10154 cu->language_defn = language_def (cu->language);
10155
10156 /* The symbol tables are set up in read_type_unit_scope. */
10157 process_die (cu->dies, cu);
10158
10159 /* For now fudge the Go package. */
10160 if (cu->language == language_go)
10161 fixup_go_packaging (cu);
10162
10163 /* Now that we have processed all the DIEs in the CU, all the types
10164 should be complete, and it should now be safe to compute all of the
10165 physnames. */
10166 compute_delayed_physnames (cu);
10167
10168 if (cu->language == language_rust)
10169 rust_union_quirks (cu);
10170
10171 /* TUs share symbol tables.
10172 If this is the first TU to use this symtab, complete the construction
10173 of it with end_expandable_symtab. Otherwise, complete the addition of
10174 this TU's symbols to the existing symtab. */
10175 type_unit_group_unshareable *tug_unshare =
10176 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
10177 if (tug_unshare->compunit_symtab == NULL)
10178 {
10179 buildsym_compunit *builder = cu->get_builder ();
10180 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10181 tug_unshare->compunit_symtab = cust;
10182
10183 if (cust != NULL)
10184 {
10185 /* Set symtab language to language from DW_AT_language. If the
10186 compilation is from a C file generated by language preprocessors,
10187 do not set the language if it was already deduced by
10188 start_subfile. */
10189 if (!(cu->language == language_c
10190 && COMPUNIT_FILETABS (cust)->language != language_c))
10191 COMPUNIT_FILETABS (cust)->language = cu->language;
10192 }
10193 }
10194 else
10195 {
10196 cu->get_builder ()->augment_type_symtab ();
10197 cust = tug_unshare->compunit_symtab;
10198 }
10199
10200 per_objfile->set_symtab (cu->per_cu, cust);
10201
10202 /* Not needed any more. */
10203 cu->reset_builder ();
10204 }
10205
10206 /* Process an imported unit DIE. */
10207
10208 static void
10209 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10210 {
10211 struct attribute *attr;
10212
10213 /* For now we don't handle imported units in type units. */
10214 if (cu->per_cu->is_debug_types)
10215 {
10216 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10217 " supported in type units [in module %s]"),
10218 objfile_name (cu->per_objfile->objfile));
10219 }
10220
10221 attr = dwarf2_attr (die, DW_AT_import, cu);
10222 if (attr != NULL)
10223 {
10224 sect_offset sect_off = attr->get_ref_die_offset ();
10225 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10226 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10227 dwarf2_per_cu_data *per_cu
10228 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
10229
10230 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10231 into another compilation unit, at root level. Regard this as a hint,
10232 and ignore it. */
10233 if (die->parent && die->parent->parent == NULL
10234 && per_cu->unit_type == DW_UT_compile
10235 && per_cu->lang == language_cplus)
10236 return;
10237
10238 /* If necessary, add it to the queue and load its DIEs. */
10239 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
10240 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10241 false, cu->language);
10242
10243 cu->per_cu->imported_symtabs_push (per_cu);
10244 }
10245 }
10246
10247 /* RAII object that represents a process_die scope: i.e.,
10248 starts/finishes processing a DIE. */
10249 class process_die_scope
10250 {
10251 public:
10252 process_die_scope (die_info *die, dwarf2_cu *cu)
10253 : m_die (die), m_cu (cu)
10254 {
10255 /* We should only be processing DIEs not already in process. */
10256 gdb_assert (!m_die->in_process);
10257 m_die->in_process = true;
10258 }
10259
10260 ~process_die_scope ()
10261 {
10262 m_die->in_process = false;
10263
10264 /* If we're done processing the DIE for the CU that owns the line
10265 header, we don't need the line header anymore. */
10266 if (m_cu->line_header_die_owner == m_die)
10267 {
10268 delete m_cu->line_header;
10269 m_cu->line_header = NULL;
10270 m_cu->line_header_die_owner = NULL;
10271 }
10272 }
10273
10274 private:
10275 die_info *m_die;
10276 dwarf2_cu *m_cu;
10277 };
10278
10279 /* Process a die and its children. */
10280
10281 static void
10282 process_die (struct die_info *die, struct dwarf2_cu *cu)
10283 {
10284 process_die_scope scope (die, cu);
10285
10286 switch (die->tag)
10287 {
10288 case DW_TAG_padding:
10289 break;
10290 case DW_TAG_compile_unit:
10291 case DW_TAG_partial_unit:
10292 read_file_scope (die, cu);
10293 break;
10294 case DW_TAG_type_unit:
10295 read_type_unit_scope (die, cu);
10296 break;
10297 case DW_TAG_subprogram:
10298 /* Nested subprograms in Fortran get a prefix. */
10299 if (cu->language == language_fortran
10300 && die->parent != NULL
10301 && die->parent->tag == DW_TAG_subprogram)
10302 cu->processing_has_namespace_info = true;
10303 /* Fall through. */
10304 case DW_TAG_inlined_subroutine:
10305 read_func_scope (die, cu);
10306 break;
10307 case DW_TAG_lexical_block:
10308 case DW_TAG_try_block:
10309 case DW_TAG_catch_block:
10310 read_lexical_block_scope (die, cu);
10311 break;
10312 case DW_TAG_call_site:
10313 case DW_TAG_GNU_call_site:
10314 read_call_site_scope (die, cu);
10315 break;
10316 case DW_TAG_class_type:
10317 case DW_TAG_interface_type:
10318 case DW_TAG_structure_type:
10319 case DW_TAG_union_type:
10320 process_structure_scope (die, cu);
10321 break;
10322 case DW_TAG_enumeration_type:
10323 process_enumeration_scope (die, cu);
10324 break;
10325
10326 /* These dies have a type, but processing them does not create
10327 a symbol or recurse to process the children. Therefore we can
10328 read them on-demand through read_type_die. */
10329 case DW_TAG_subroutine_type:
10330 case DW_TAG_set_type:
10331 case DW_TAG_pointer_type:
10332 case DW_TAG_ptr_to_member_type:
10333 case DW_TAG_reference_type:
10334 case DW_TAG_rvalue_reference_type:
10335 case DW_TAG_string_type:
10336 break;
10337
10338 case DW_TAG_array_type:
10339 /* We only need to handle this case for Ada -- in other
10340 languages, it's normal for the compiler to emit a typedef
10341 instead. */
10342 if (cu->language != language_ada)
10343 break;
10344 /* FALLTHROUGH */
10345 case DW_TAG_base_type:
10346 case DW_TAG_subrange_type:
10347 case DW_TAG_typedef:
10348 /* Add a typedef symbol for the type definition, if it has a
10349 DW_AT_name. */
10350 new_symbol (die, read_type_die (die, cu), cu);
10351 break;
10352 case DW_TAG_common_block:
10353 read_common_block (die, cu);
10354 break;
10355 case DW_TAG_common_inclusion:
10356 break;
10357 case DW_TAG_namespace:
10358 cu->processing_has_namespace_info = true;
10359 read_namespace (die, cu);
10360 break;
10361 case DW_TAG_module:
10362 cu->processing_has_namespace_info = true;
10363 read_module (die, cu);
10364 break;
10365 case DW_TAG_imported_declaration:
10366 cu->processing_has_namespace_info = true;
10367 if (read_namespace_alias (die, cu))
10368 break;
10369 /* The declaration is not a global namespace alias. */
10370 /* Fall through. */
10371 case DW_TAG_imported_module:
10372 cu->processing_has_namespace_info = true;
10373 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10374 || cu->language != language_fortran))
10375 complaint (_("Tag '%s' has unexpected children"),
10376 dwarf_tag_name (die->tag));
10377 read_import_statement (die, cu);
10378 break;
10379
10380 case DW_TAG_imported_unit:
10381 process_imported_unit_die (die, cu);
10382 break;
10383
10384 case DW_TAG_variable:
10385 read_variable (die, cu);
10386 break;
10387
10388 default:
10389 new_symbol (die, NULL, cu);
10390 break;
10391 }
10392 }
10393 \f
10394 /* DWARF name computation. */
10395
10396 /* A helper function for dwarf2_compute_name which determines whether DIE
10397 needs to have the name of the scope prepended to the name listed in the
10398 die. */
10399
10400 static int
10401 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10402 {
10403 struct attribute *attr;
10404
10405 switch (die->tag)
10406 {
10407 case DW_TAG_namespace:
10408 case DW_TAG_typedef:
10409 case DW_TAG_class_type:
10410 case DW_TAG_interface_type:
10411 case DW_TAG_structure_type:
10412 case DW_TAG_union_type:
10413 case DW_TAG_enumeration_type:
10414 case DW_TAG_enumerator:
10415 case DW_TAG_subprogram:
10416 case DW_TAG_inlined_subroutine:
10417 case DW_TAG_member:
10418 case DW_TAG_imported_declaration:
10419 return 1;
10420
10421 case DW_TAG_variable:
10422 case DW_TAG_constant:
10423 /* We only need to prefix "globally" visible variables. These include
10424 any variable marked with DW_AT_external or any variable that
10425 lives in a namespace. [Variables in anonymous namespaces
10426 require prefixing, but they are not DW_AT_external.] */
10427
10428 if (dwarf2_attr (die, DW_AT_specification, cu))
10429 {
10430 struct dwarf2_cu *spec_cu = cu;
10431
10432 return die_needs_namespace (die_specification (die, &spec_cu),
10433 spec_cu);
10434 }
10435
10436 attr = dwarf2_attr (die, DW_AT_external, cu);
10437 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10438 && die->parent->tag != DW_TAG_module)
10439 return 0;
10440 /* A variable in a lexical block of some kind does not need a
10441 namespace, even though in C++ such variables may be external
10442 and have a mangled name. */
10443 if (die->parent->tag == DW_TAG_lexical_block
10444 || die->parent->tag == DW_TAG_try_block
10445 || die->parent->tag == DW_TAG_catch_block
10446 || die->parent->tag == DW_TAG_subprogram)
10447 return 0;
10448 return 1;
10449
10450 default:
10451 return 0;
10452 }
10453 }
10454
10455 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10456 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10457 defined for the given DIE. */
10458
10459 static struct attribute *
10460 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10461 {
10462 struct attribute *attr;
10463
10464 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10465 if (attr == NULL)
10466 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10467
10468 return attr;
10469 }
10470
10471 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10472 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10473 defined for the given DIE. */
10474
10475 static const char *
10476 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10477 {
10478 const char *linkage_name;
10479
10480 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10481 if (linkage_name == NULL)
10482 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10483
10484 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10485 See https://github.com/rust-lang/rust/issues/32925. */
10486 if (cu->language == language_rust && linkage_name != NULL
10487 && strchr (linkage_name, '{') != NULL)
10488 linkage_name = NULL;
10489
10490 return linkage_name;
10491 }
10492
10493 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10494 compute the physname for the object, which include a method's:
10495 - formal parameters (C++),
10496 - receiver type (Go),
10497
10498 The term "physname" is a bit confusing.
10499 For C++, for example, it is the demangled name.
10500 For Go, for example, it's the mangled name.
10501
10502 For Ada, return the DIE's linkage name rather than the fully qualified
10503 name. PHYSNAME is ignored..
10504
10505 The result is allocated on the objfile->per_bfd's obstack and
10506 canonicalized. */
10507
10508 static const char *
10509 dwarf2_compute_name (const char *name,
10510 struct die_info *die, struct dwarf2_cu *cu,
10511 int physname)
10512 {
10513 struct objfile *objfile = cu->per_objfile->objfile;
10514
10515 if (name == NULL)
10516 name = dwarf2_name (die, cu);
10517
10518 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10519 but otherwise compute it by typename_concat inside GDB.
10520 FIXME: Actually this is not really true, or at least not always true.
10521 It's all very confusing. compute_and_set_names doesn't try to demangle
10522 Fortran names because there is no mangling standard. So new_symbol
10523 will set the demangled name to the result of dwarf2_full_name, and it is
10524 the demangled name that GDB uses if it exists. */
10525 if (cu->language == language_ada
10526 || (cu->language == language_fortran && physname))
10527 {
10528 /* For Ada unit, we prefer the linkage name over the name, as
10529 the former contains the exported name, which the user expects
10530 to be able to reference. Ideally, we want the user to be able
10531 to reference this entity using either natural or linkage name,
10532 but we haven't started looking at this enhancement yet. */
10533 const char *linkage_name = dw2_linkage_name (die, cu);
10534
10535 if (linkage_name != NULL)
10536 return linkage_name;
10537 }
10538
10539 /* These are the only languages we know how to qualify names in. */
10540 if (name != NULL
10541 && (cu->language == language_cplus
10542 || cu->language == language_fortran || cu->language == language_d
10543 || cu->language == language_rust))
10544 {
10545 if (die_needs_namespace (die, cu))
10546 {
10547 const char *prefix;
10548 const char *canonical_name = NULL;
10549
10550 string_file buf;
10551
10552 prefix = determine_prefix (die, cu);
10553 if (*prefix != '\0')
10554 {
10555 gdb::unique_xmalloc_ptr<char> prefixed_name
10556 (typename_concat (NULL, prefix, name, physname, cu));
10557
10558 buf.puts (prefixed_name.get ());
10559 }
10560 else
10561 buf.puts (name);
10562
10563 /* Template parameters may be specified in the DIE's DW_AT_name, or
10564 as children with DW_TAG_template_type_param or
10565 DW_TAG_value_type_param. If the latter, add them to the name
10566 here. If the name already has template parameters, then
10567 skip this step; some versions of GCC emit both, and
10568 it is more efficient to use the pre-computed name.
10569
10570 Something to keep in mind about this process: it is very
10571 unlikely, or in some cases downright impossible, to produce
10572 something that will match the mangled name of a function.
10573 If the definition of the function has the same debug info,
10574 we should be able to match up with it anyway. But fallbacks
10575 using the minimal symbol, for instance to find a method
10576 implemented in a stripped copy of libstdc++, will not work.
10577 If we do not have debug info for the definition, we will have to
10578 match them up some other way.
10579
10580 When we do name matching there is a related problem with function
10581 templates; two instantiated function templates are allowed to
10582 differ only by their return types, which we do not add here. */
10583
10584 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10585 {
10586 struct attribute *attr;
10587 struct die_info *child;
10588 int first = 1;
10589 const language_defn *cplus_lang = language_def (cu->language);
10590
10591 die->building_fullname = 1;
10592
10593 for (child = die->child; child != NULL; child = child->sibling)
10594 {
10595 struct type *type;
10596 LONGEST value;
10597 const gdb_byte *bytes;
10598 struct dwarf2_locexpr_baton *baton;
10599 struct value *v;
10600
10601 if (child->tag != DW_TAG_template_type_param
10602 && child->tag != DW_TAG_template_value_param)
10603 continue;
10604
10605 if (first)
10606 {
10607 buf.puts ("<");
10608 first = 0;
10609 }
10610 else
10611 buf.puts (", ");
10612
10613 attr = dwarf2_attr (child, DW_AT_type, cu);
10614 if (attr == NULL)
10615 {
10616 complaint (_("template parameter missing DW_AT_type"));
10617 buf.puts ("UNKNOWN_TYPE");
10618 continue;
10619 }
10620 type = die_type (child, cu);
10621
10622 if (child->tag == DW_TAG_template_type_param)
10623 {
10624 cplus_lang->print_type (type, "", &buf, -1, 0,
10625 &type_print_raw_options);
10626 continue;
10627 }
10628
10629 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10630 if (attr == NULL)
10631 {
10632 complaint (_("template parameter missing "
10633 "DW_AT_const_value"));
10634 buf.puts ("UNKNOWN_VALUE");
10635 continue;
10636 }
10637
10638 dwarf2_const_value_attr (attr, type, name,
10639 &cu->comp_unit_obstack, cu,
10640 &value, &bytes, &baton);
10641
10642 if (type->has_no_signedness ())
10643 /* GDB prints characters as NUMBER 'CHAR'. If that's
10644 changed, this can use value_print instead. */
10645 cplus_lang->printchar (value, type, &buf);
10646 else
10647 {
10648 struct value_print_options opts;
10649
10650 if (baton != NULL)
10651 v = dwarf2_evaluate_loc_desc (type, NULL,
10652 baton->data,
10653 baton->size,
10654 baton->per_cu,
10655 baton->per_objfile);
10656 else if (bytes != NULL)
10657 {
10658 v = allocate_value (type);
10659 memcpy (value_contents_writeable (v), bytes,
10660 TYPE_LENGTH (type));
10661 }
10662 else
10663 v = value_from_longest (type, value);
10664
10665 /* Specify decimal so that we do not depend on
10666 the radix. */
10667 get_formatted_print_options (&opts, 'd');
10668 opts.raw = 1;
10669 value_print (v, &buf, &opts);
10670 release_value (v);
10671 }
10672 }
10673
10674 die->building_fullname = 0;
10675
10676 if (!first)
10677 {
10678 /* Close the argument list, with a space if necessary
10679 (nested templates). */
10680 if (!buf.empty () && buf.string ().back () == '>')
10681 buf.puts (" >");
10682 else
10683 buf.puts (">");
10684 }
10685 }
10686
10687 /* For C++ methods, append formal parameter type
10688 information, if PHYSNAME. */
10689
10690 if (physname && die->tag == DW_TAG_subprogram
10691 && cu->language == language_cplus)
10692 {
10693 struct type *type = read_type_die (die, cu);
10694
10695 c_type_print_args (type, &buf, 1, cu->language,
10696 &type_print_raw_options);
10697
10698 if (cu->language == language_cplus)
10699 {
10700 /* Assume that an artificial first parameter is
10701 "this", but do not crash if it is not. RealView
10702 marks unnamed (and thus unused) parameters as
10703 artificial; there is no way to differentiate
10704 the two cases. */
10705 if (type->num_fields () > 0
10706 && TYPE_FIELD_ARTIFICIAL (type, 0)
10707 && type->field (0).type ()->code () == TYPE_CODE_PTR
10708 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10709 buf.puts (" const");
10710 }
10711 }
10712
10713 const std::string &intermediate_name = buf.string ();
10714
10715 if (cu->language == language_cplus)
10716 canonical_name
10717 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10718 objfile);
10719
10720 /* If we only computed INTERMEDIATE_NAME, or if
10721 INTERMEDIATE_NAME is already canonical, then we need to
10722 intern it. */
10723 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10724 name = objfile->intern (intermediate_name);
10725 else
10726 name = canonical_name;
10727 }
10728 }
10729
10730 return name;
10731 }
10732
10733 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10734 If scope qualifiers are appropriate they will be added. The result
10735 will be allocated on the storage_obstack, or NULL if the DIE does
10736 not have a name. NAME may either be from a previous call to
10737 dwarf2_name or NULL.
10738
10739 The output string will be canonicalized (if C++). */
10740
10741 static const char *
10742 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10743 {
10744 return dwarf2_compute_name (name, die, cu, 0);
10745 }
10746
10747 /* Construct a physname for the given DIE in CU. NAME may either be
10748 from a previous call to dwarf2_name or NULL. The result will be
10749 allocated on the objfile_objstack or NULL if the DIE does not have a
10750 name.
10751
10752 The output string will be canonicalized (if C++). */
10753
10754 static const char *
10755 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10756 {
10757 struct objfile *objfile = cu->per_objfile->objfile;
10758 const char *retval, *mangled = NULL, *canon = NULL;
10759 int need_copy = 1;
10760
10761 /* In this case dwarf2_compute_name is just a shortcut not building anything
10762 on its own. */
10763 if (!die_needs_namespace (die, cu))
10764 return dwarf2_compute_name (name, die, cu, 1);
10765
10766 if (cu->language != language_rust)
10767 mangled = dw2_linkage_name (die, cu);
10768
10769 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10770 has computed. */
10771 gdb::unique_xmalloc_ptr<char> demangled;
10772 if (mangled != NULL)
10773 {
10774
10775 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10776 {
10777 /* Do nothing (do not demangle the symbol name). */
10778 }
10779 else
10780 {
10781 /* Use DMGL_RET_DROP for C++ template functions to suppress
10782 their return type. It is easier for GDB users to search
10783 for such functions as `name(params)' than `long name(params)'.
10784 In such case the minimal symbol names do not match the full
10785 symbol names but for template functions there is never a need
10786 to look up their definition from their declaration so
10787 the only disadvantage remains the minimal symbol variant
10788 `long name(params)' does not have the proper inferior type. */
10789 demangled.reset (gdb_demangle (mangled,
10790 (DMGL_PARAMS | DMGL_ANSI
10791 | DMGL_RET_DROP)));
10792 }
10793 if (demangled)
10794 canon = demangled.get ();
10795 else
10796 {
10797 canon = mangled;
10798 need_copy = 0;
10799 }
10800 }
10801
10802 if (canon == NULL || check_physname)
10803 {
10804 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10805
10806 if (canon != NULL && strcmp (physname, canon) != 0)
10807 {
10808 /* It may not mean a bug in GDB. The compiler could also
10809 compute DW_AT_linkage_name incorrectly. But in such case
10810 GDB would need to be bug-to-bug compatible. */
10811
10812 complaint (_("Computed physname <%s> does not match demangled <%s> "
10813 "(from linkage <%s>) - DIE at %s [in module %s]"),
10814 physname, canon, mangled, sect_offset_str (die->sect_off),
10815 objfile_name (objfile));
10816
10817 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10818 is available here - over computed PHYSNAME. It is safer
10819 against both buggy GDB and buggy compilers. */
10820
10821 retval = canon;
10822 }
10823 else
10824 {
10825 retval = physname;
10826 need_copy = 0;
10827 }
10828 }
10829 else
10830 retval = canon;
10831
10832 if (need_copy)
10833 retval = objfile->intern (retval);
10834
10835 return retval;
10836 }
10837
10838 /* Inspect DIE in CU for a namespace alias. If one exists, record
10839 a new symbol for it.
10840
10841 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10842
10843 static int
10844 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10845 {
10846 struct attribute *attr;
10847
10848 /* If the die does not have a name, this is not a namespace
10849 alias. */
10850 attr = dwarf2_attr (die, DW_AT_name, cu);
10851 if (attr != NULL)
10852 {
10853 int num;
10854 struct die_info *d = die;
10855 struct dwarf2_cu *imported_cu = cu;
10856
10857 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10858 keep inspecting DIEs until we hit the underlying import. */
10859 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10860 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10861 {
10862 attr = dwarf2_attr (d, DW_AT_import, cu);
10863 if (attr == NULL)
10864 break;
10865
10866 d = follow_die_ref (d, attr, &imported_cu);
10867 if (d->tag != DW_TAG_imported_declaration)
10868 break;
10869 }
10870
10871 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10872 {
10873 complaint (_("DIE at %s has too many recursively imported "
10874 "declarations"), sect_offset_str (d->sect_off));
10875 return 0;
10876 }
10877
10878 if (attr != NULL)
10879 {
10880 struct type *type;
10881 sect_offset sect_off = attr->get_ref_die_offset ();
10882
10883 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10884 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10885 {
10886 /* This declaration is a global namespace alias. Add
10887 a symbol for it whose type is the aliased namespace. */
10888 new_symbol (die, type, cu);
10889 return 1;
10890 }
10891 }
10892 }
10893
10894 return 0;
10895 }
10896
10897 /* Return the using directives repository (global or local?) to use in the
10898 current context for CU.
10899
10900 For Ada, imported declarations can materialize renamings, which *may* be
10901 global. However it is impossible (for now?) in DWARF to distinguish
10902 "external" imported declarations and "static" ones. As all imported
10903 declarations seem to be static in all other languages, make them all CU-wide
10904 global only in Ada. */
10905
10906 static struct using_direct **
10907 using_directives (struct dwarf2_cu *cu)
10908 {
10909 if (cu->language == language_ada
10910 && cu->get_builder ()->outermost_context_p ())
10911 return cu->get_builder ()->get_global_using_directives ();
10912 else
10913 return cu->get_builder ()->get_local_using_directives ();
10914 }
10915
10916 /* Read the import statement specified by the given die and record it. */
10917
10918 static void
10919 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10920 {
10921 struct objfile *objfile = cu->per_objfile->objfile;
10922 struct attribute *import_attr;
10923 struct die_info *imported_die, *child_die;
10924 struct dwarf2_cu *imported_cu;
10925 const char *imported_name;
10926 const char *imported_name_prefix;
10927 const char *canonical_name;
10928 const char *import_alias;
10929 const char *imported_declaration = NULL;
10930 const char *import_prefix;
10931 std::vector<const char *> excludes;
10932
10933 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10934 if (import_attr == NULL)
10935 {
10936 complaint (_("Tag '%s' has no DW_AT_import"),
10937 dwarf_tag_name (die->tag));
10938 return;
10939 }
10940
10941 imported_cu = cu;
10942 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10943 imported_name = dwarf2_name (imported_die, imported_cu);
10944 if (imported_name == NULL)
10945 {
10946 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10947
10948 The import in the following code:
10949 namespace A
10950 {
10951 typedef int B;
10952 }
10953
10954 int main ()
10955 {
10956 using A::B;
10957 B b;
10958 return b;
10959 }
10960
10961 ...
10962 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10963 <52> DW_AT_decl_file : 1
10964 <53> DW_AT_decl_line : 6
10965 <54> DW_AT_import : <0x75>
10966 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10967 <59> DW_AT_name : B
10968 <5b> DW_AT_decl_file : 1
10969 <5c> DW_AT_decl_line : 2
10970 <5d> DW_AT_type : <0x6e>
10971 ...
10972 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10973 <76> DW_AT_byte_size : 4
10974 <77> DW_AT_encoding : 5 (signed)
10975
10976 imports the wrong die ( 0x75 instead of 0x58 ).
10977 This case will be ignored until the gcc bug is fixed. */
10978 return;
10979 }
10980
10981 /* Figure out the local name after import. */
10982 import_alias = dwarf2_name (die, cu);
10983
10984 /* Figure out where the statement is being imported to. */
10985 import_prefix = determine_prefix (die, cu);
10986
10987 /* Figure out what the scope of the imported die is and prepend it
10988 to the name of the imported die. */
10989 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10990
10991 if (imported_die->tag != DW_TAG_namespace
10992 && imported_die->tag != DW_TAG_module)
10993 {
10994 imported_declaration = imported_name;
10995 canonical_name = imported_name_prefix;
10996 }
10997 else if (strlen (imported_name_prefix) > 0)
10998 canonical_name = obconcat (&objfile->objfile_obstack,
10999 imported_name_prefix,
11000 (cu->language == language_d ? "." : "::"),
11001 imported_name, (char *) NULL);
11002 else
11003 canonical_name = imported_name;
11004
11005 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11006 for (child_die = die->child; child_die && child_die->tag;
11007 child_die = child_die->sibling)
11008 {
11009 /* DWARF-4: A Fortran use statement with a “rename list” may be
11010 represented by an imported module entry with an import attribute
11011 referring to the module and owned entries corresponding to those
11012 entities that are renamed as part of being imported. */
11013
11014 if (child_die->tag != DW_TAG_imported_declaration)
11015 {
11016 complaint (_("child DW_TAG_imported_declaration expected "
11017 "- DIE at %s [in module %s]"),
11018 sect_offset_str (child_die->sect_off),
11019 objfile_name (objfile));
11020 continue;
11021 }
11022
11023 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11024 if (import_attr == NULL)
11025 {
11026 complaint (_("Tag '%s' has no DW_AT_import"),
11027 dwarf_tag_name (child_die->tag));
11028 continue;
11029 }
11030
11031 imported_cu = cu;
11032 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11033 &imported_cu);
11034 imported_name = dwarf2_name (imported_die, imported_cu);
11035 if (imported_name == NULL)
11036 {
11037 complaint (_("child DW_TAG_imported_declaration has unknown "
11038 "imported name - DIE at %s [in module %s]"),
11039 sect_offset_str (child_die->sect_off),
11040 objfile_name (objfile));
11041 continue;
11042 }
11043
11044 excludes.push_back (imported_name);
11045
11046 process_die (child_die, cu);
11047 }
11048
11049 add_using_directive (using_directives (cu),
11050 import_prefix,
11051 canonical_name,
11052 import_alias,
11053 imported_declaration,
11054 excludes,
11055 0,
11056 &objfile->objfile_obstack);
11057 }
11058
11059 /* ICC<14 does not output the required DW_AT_declaration on incomplete
11060 types, but gives them a size of zero. Starting with version 14,
11061 ICC is compatible with GCC. */
11062
11063 static bool
11064 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11065 {
11066 if (!cu->checked_producer)
11067 check_producer (cu);
11068
11069 return cu->producer_is_icc_lt_14;
11070 }
11071
11072 /* ICC generates a DW_AT_type for C void functions. This was observed on
11073 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11074 which says that void functions should not have a DW_AT_type. */
11075
11076 static bool
11077 producer_is_icc (struct dwarf2_cu *cu)
11078 {
11079 if (!cu->checked_producer)
11080 check_producer (cu);
11081
11082 return cu->producer_is_icc;
11083 }
11084
11085 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11086 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11087 this, it was first present in GCC release 4.3.0. */
11088
11089 static bool
11090 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11091 {
11092 if (!cu->checked_producer)
11093 check_producer (cu);
11094
11095 return cu->producer_is_gcc_lt_4_3;
11096 }
11097
11098 static file_and_directory
11099 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
11100 {
11101 file_and_directory res;
11102
11103 /* Find the filename. Do not use dwarf2_name here, since the filename
11104 is not a source language identifier. */
11105 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11106 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
11107
11108 if (res.comp_dir == NULL
11109 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11110 && IS_ABSOLUTE_PATH (res.name))
11111 {
11112 res.comp_dir_storage = ldirname (res.name);
11113 if (!res.comp_dir_storage.empty ())
11114 res.comp_dir = res.comp_dir_storage.c_str ();
11115 }
11116 if (res.comp_dir != NULL)
11117 {
11118 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11119 directory, get rid of it. */
11120 const char *cp = strchr (res.comp_dir, ':');
11121
11122 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11123 res.comp_dir = cp + 1;
11124 }
11125
11126 if (res.name == NULL)
11127 res.name = "<unknown>";
11128
11129 return res;
11130 }
11131
11132 /* Handle DW_AT_stmt_list for a compilation unit.
11133 DIE is the DW_TAG_compile_unit die for CU.
11134 COMP_DIR is the compilation directory. LOWPC is passed to
11135 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
11136
11137 static void
11138 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
11139 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
11140 {
11141 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11142 struct attribute *attr;
11143 struct line_header line_header_local;
11144 hashval_t line_header_local_hash;
11145 void **slot;
11146 int decode_mapping;
11147
11148 gdb_assert (! cu->per_cu->is_debug_types);
11149
11150 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
11151 if (attr == NULL || !attr->form_is_unsigned ())
11152 return;
11153
11154 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11155
11156 /* The line header hash table is only created if needed (it exists to
11157 prevent redundant reading of the line table for partial_units).
11158 If we're given a partial_unit, we'll need it. If we're given a
11159 compile_unit, then use the line header hash table if it's already
11160 created, but don't create one just yet. */
11161
11162 if (per_objfile->line_header_hash == NULL
11163 && die->tag == DW_TAG_partial_unit)
11164 {
11165 per_objfile->line_header_hash
11166 .reset (htab_create_alloc (127, line_header_hash_voidp,
11167 line_header_eq_voidp,
11168 free_line_header_voidp,
11169 xcalloc, xfree));
11170 }
11171
11172 line_header_local.sect_off = line_offset;
11173 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11174 line_header_local_hash = line_header_hash (&line_header_local);
11175 if (per_objfile->line_header_hash != NULL)
11176 {
11177 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11178 &line_header_local,
11179 line_header_local_hash, NO_INSERT);
11180
11181 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11182 is not present in *SLOT (since if there is something in *SLOT then
11183 it will be for a partial_unit). */
11184 if (die->tag == DW_TAG_partial_unit && slot != NULL)
11185 {
11186 gdb_assert (*slot != NULL);
11187 cu->line_header = (struct line_header *) *slot;
11188 return;
11189 }
11190 }
11191
11192 /* dwarf_decode_line_header does not yet provide sufficient information.
11193 We always have to call also dwarf_decode_lines for it. */
11194 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11195 if (lh == NULL)
11196 return;
11197
11198 cu->line_header = lh.release ();
11199 cu->line_header_die_owner = die;
11200
11201 if (per_objfile->line_header_hash == NULL)
11202 slot = NULL;
11203 else
11204 {
11205 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
11206 &line_header_local,
11207 line_header_local_hash, INSERT);
11208 gdb_assert (slot != NULL);
11209 }
11210 if (slot != NULL && *slot == NULL)
11211 {
11212 /* This newly decoded line number information unit will be owned
11213 by line_header_hash hash table. */
11214 *slot = cu->line_header;
11215 cu->line_header_die_owner = NULL;
11216 }
11217 else
11218 {
11219 /* We cannot free any current entry in (*slot) as that struct line_header
11220 may be already used by multiple CUs. Create only temporary decoded
11221 line_header for this CU - it may happen at most once for each line
11222 number information unit. And if we're not using line_header_hash
11223 then this is what we want as well. */
11224 gdb_assert (die->tag != DW_TAG_partial_unit);
11225 }
11226 decode_mapping = (die->tag != DW_TAG_partial_unit);
11227 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11228 decode_mapping);
11229
11230 }
11231
11232 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
11233
11234 static void
11235 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
11236 {
11237 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11238 struct objfile *objfile = per_objfile->objfile;
11239 struct gdbarch *gdbarch = objfile->arch ();
11240 CORE_ADDR lowpc = ((CORE_ADDR) -1);
11241 CORE_ADDR highpc = ((CORE_ADDR) 0);
11242 struct attribute *attr;
11243 struct die_info *child_die;
11244 CORE_ADDR baseaddr;
11245
11246 prepare_one_comp_unit (cu, die, cu->language);
11247 baseaddr = objfile->text_section_offset ();
11248
11249 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
11250
11251 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11252 from finish_block. */
11253 if (lowpc == ((CORE_ADDR) -1))
11254 lowpc = highpc;
11255 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
11256
11257 file_and_directory fnd = find_file_and_directory (die, cu);
11258
11259 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11260 standardised yet. As a workaround for the language detection we fall
11261 back to the DW_AT_producer string. */
11262 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11263 cu->language = language_opencl;
11264
11265 /* Similar hack for Go. */
11266 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11267 set_cu_language (DW_LANG_Go, cu);
11268
11269 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
11270
11271 /* Decode line number information if present. We do this before
11272 processing child DIEs, so that the line header table is available
11273 for DW_AT_decl_file. */
11274 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
11275
11276 /* Process all dies in compilation unit. */
11277 if (die->child != NULL)
11278 {
11279 child_die = die->child;
11280 while (child_die && child_die->tag)
11281 {
11282 process_die (child_die, cu);
11283 child_die = child_die->sibling;
11284 }
11285 }
11286
11287 /* Decode macro information, if present. Dwarf 2 macro information
11288 refers to information in the line number info statement program
11289 header, so we can only read it if we've read the header
11290 successfully. */
11291 attr = dwarf2_attr (die, DW_AT_macros, cu);
11292 if (attr == NULL)
11293 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
11294 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11295 {
11296 if (dwarf2_attr (die, DW_AT_macro_info, cu))
11297 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
11298
11299 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
11300 }
11301 else
11302 {
11303 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11304 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
11305 {
11306 unsigned int macro_offset = attr->as_unsigned ();
11307
11308 dwarf_decode_macros (cu, macro_offset, 0);
11309 }
11310 }
11311 }
11312
11313 void
11314 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
11315 {
11316 struct type_unit_group *tu_group;
11317 int first_time;
11318 struct attribute *attr;
11319 unsigned int i;
11320 struct signatured_type *sig_type;
11321
11322 gdb_assert (per_cu->is_debug_types);
11323 sig_type = (struct signatured_type *) per_cu;
11324
11325 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
11326
11327 /* If we're using .gdb_index (includes -readnow) then
11328 per_cu->type_unit_group may not have been set up yet. */
11329 if (sig_type->type_unit_group == NULL)
11330 sig_type->type_unit_group = get_type_unit_group (this, attr);
11331 tu_group = sig_type->type_unit_group;
11332
11333 /* If we've already processed this stmt_list there's no real need to
11334 do it again, we could fake it and just recreate the part we need
11335 (file name,index -> symtab mapping). If data shows this optimization
11336 is useful we can do it then. */
11337 type_unit_group_unshareable *tug_unshare
11338 = per_objfile->get_type_unit_group_unshareable (tu_group);
11339 first_time = tug_unshare->compunit_symtab == NULL;
11340
11341 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11342 debug info. */
11343 line_header_up lh;
11344 if (attr != NULL && attr->form_is_unsigned ())
11345 {
11346 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
11347 lh = dwarf_decode_line_header (line_offset, this);
11348 }
11349 if (lh == NULL)
11350 {
11351 if (first_time)
11352 start_symtab ("", NULL, 0);
11353 else
11354 {
11355 gdb_assert (tug_unshare->symtabs == NULL);
11356 gdb_assert (m_builder == nullptr);
11357 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11358 m_builder.reset (new struct buildsym_compunit
11359 (COMPUNIT_OBJFILE (cust), "",
11360 COMPUNIT_DIRNAME (cust),
11361 compunit_language (cust),
11362 0, cust));
11363 list_in_scope = get_builder ()->get_file_symbols ();
11364 }
11365 return;
11366 }
11367
11368 line_header = lh.release ();
11369 line_header_die_owner = die;
11370
11371 if (first_time)
11372 {
11373 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11374
11375 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11376 still initializing it, and our caller (a few levels up)
11377 process_full_type_unit still needs to know if this is the first
11378 time. */
11379
11380 tug_unshare->symtabs
11381 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11382 struct symtab *, line_header->file_names_size ());
11383
11384 auto &file_names = line_header->file_names ();
11385 for (i = 0; i < file_names.size (); ++i)
11386 {
11387 file_entry &fe = file_names[i];
11388 dwarf2_start_subfile (this, fe.name,
11389 fe.include_dir (line_header));
11390 buildsym_compunit *b = get_builder ();
11391 if (b->get_current_subfile ()->symtab == NULL)
11392 {
11393 /* NOTE: start_subfile will recognize when it's been
11394 passed a file it has already seen. So we can't
11395 assume there's a simple mapping from
11396 cu->line_header->file_names to subfiles, plus
11397 cu->line_header->file_names may contain dups. */
11398 b->get_current_subfile ()->symtab
11399 = allocate_symtab (cust, b->get_current_subfile ()->name);
11400 }
11401
11402 fe.symtab = b->get_current_subfile ()->symtab;
11403 tug_unshare->symtabs[i] = fe.symtab;
11404 }
11405 }
11406 else
11407 {
11408 gdb_assert (m_builder == nullptr);
11409 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11410 m_builder.reset (new struct buildsym_compunit
11411 (COMPUNIT_OBJFILE (cust), "",
11412 COMPUNIT_DIRNAME (cust),
11413 compunit_language (cust),
11414 0, cust));
11415 list_in_scope = get_builder ()->get_file_symbols ();
11416
11417 auto &file_names = line_header->file_names ();
11418 for (i = 0; i < file_names.size (); ++i)
11419 {
11420 file_entry &fe = file_names[i];
11421 fe.symtab = tug_unshare->symtabs[i];
11422 }
11423 }
11424
11425 /* The main symtab is allocated last. Type units don't have DW_AT_name
11426 so they don't have a "real" (so to speak) symtab anyway.
11427 There is later code that will assign the main symtab to all symbols
11428 that don't have one. We need to handle the case of a symbol with a
11429 missing symtab (DW_AT_decl_file) anyway. */
11430 }
11431
11432 /* Process DW_TAG_type_unit.
11433 For TUs we want to skip the first top level sibling if it's not the
11434 actual type being defined by this TU. In this case the first top
11435 level sibling is there to provide context only. */
11436
11437 static void
11438 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11439 {
11440 struct die_info *child_die;
11441
11442 prepare_one_comp_unit (cu, die, language_minimal);
11443
11444 /* Initialize (or reinitialize) the machinery for building symtabs.
11445 We do this before processing child DIEs, so that the line header table
11446 is available for DW_AT_decl_file. */
11447 cu->setup_type_unit_groups (die);
11448
11449 if (die->child != NULL)
11450 {
11451 child_die = die->child;
11452 while (child_die && child_die->tag)
11453 {
11454 process_die (child_die, cu);
11455 child_die = child_die->sibling;
11456 }
11457 }
11458 }
11459 \f
11460 /* DWO/DWP files.
11461
11462 http://gcc.gnu.org/wiki/DebugFission
11463 http://gcc.gnu.org/wiki/DebugFissionDWP
11464
11465 To simplify handling of both DWO files ("object" files with the DWARF info)
11466 and DWP files (a file with the DWOs packaged up into one file), we treat
11467 DWP files as having a collection of virtual DWO files. */
11468
11469 static hashval_t
11470 hash_dwo_file (const void *item)
11471 {
11472 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11473 hashval_t hash;
11474
11475 hash = htab_hash_string (dwo_file->dwo_name);
11476 if (dwo_file->comp_dir != NULL)
11477 hash += htab_hash_string (dwo_file->comp_dir);
11478 return hash;
11479 }
11480
11481 static int
11482 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11483 {
11484 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11485 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11486
11487 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11488 return 0;
11489 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11490 return lhs->comp_dir == rhs->comp_dir;
11491 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11492 }
11493
11494 /* Allocate a hash table for DWO files. */
11495
11496 static htab_up
11497 allocate_dwo_file_hash_table ()
11498 {
11499 auto delete_dwo_file = [] (void *item)
11500 {
11501 struct dwo_file *dwo_file = (struct dwo_file *) item;
11502
11503 delete dwo_file;
11504 };
11505
11506 return htab_up (htab_create_alloc (41,
11507 hash_dwo_file,
11508 eq_dwo_file,
11509 delete_dwo_file,
11510 xcalloc, xfree));
11511 }
11512
11513 /* Lookup DWO file DWO_NAME. */
11514
11515 static void **
11516 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11517 const char *dwo_name,
11518 const char *comp_dir)
11519 {
11520 struct dwo_file find_entry;
11521 void **slot;
11522
11523 if (per_objfile->per_bfd->dwo_files == NULL)
11524 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11525
11526 find_entry.dwo_name = dwo_name;
11527 find_entry.comp_dir = comp_dir;
11528 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11529 INSERT);
11530
11531 return slot;
11532 }
11533
11534 static hashval_t
11535 hash_dwo_unit (const void *item)
11536 {
11537 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11538
11539 /* This drops the top 32 bits of the id, but is ok for a hash. */
11540 return dwo_unit->signature;
11541 }
11542
11543 static int
11544 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11545 {
11546 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11547 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11548
11549 /* The signature is assumed to be unique within the DWO file.
11550 So while object file CU dwo_id's always have the value zero,
11551 that's OK, assuming each object file DWO file has only one CU,
11552 and that's the rule for now. */
11553 return lhs->signature == rhs->signature;
11554 }
11555
11556 /* Allocate a hash table for DWO CUs,TUs.
11557 There is one of these tables for each of CUs,TUs for each DWO file. */
11558
11559 static htab_up
11560 allocate_dwo_unit_table ()
11561 {
11562 /* Start out with a pretty small number.
11563 Generally DWO files contain only one CU and maybe some TUs. */
11564 return htab_up (htab_create_alloc (3,
11565 hash_dwo_unit,
11566 eq_dwo_unit,
11567 NULL, xcalloc, xfree));
11568 }
11569
11570 /* die_reader_func for create_dwo_cu. */
11571
11572 static void
11573 create_dwo_cu_reader (const struct die_reader_specs *reader,
11574 const gdb_byte *info_ptr,
11575 struct die_info *comp_unit_die,
11576 struct dwo_file *dwo_file,
11577 struct dwo_unit *dwo_unit)
11578 {
11579 struct dwarf2_cu *cu = reader->cu;
11580 sect_offset sect_off = cu->per_cu->sect_off;
11581 struct dwarf2_section_info *section = cu->per_cu->section;
11582
11583 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11584 if (!signature.has_value ())
11585 {
11586 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11587 " its dwo_id [in module %s]"),
11588 sect_offset_str (sect_off), dwo_file->dwo_name);
11589 return;
11590 }
11591
11592 dwo_unit->dwo_file = dwo_file;
11593 dwo_unit->signature = *signature;
11594 dwo_unit->section = section;
11595 dwo_unit->sect_off = sect_off;
11596 dwo_unit->length = cu->per_cu->length;
11597
11598 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11599 sect_offset_str (sect_off),
11600 hex_string (dwo_unit->signature));
11601 }
11602
11603 /* Create the dwo_units for the CUs in a DWO_FILE.
11604 Note: This function processes DWO files only, not DWP files. */
11605
11606 static void
11607 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11608 dwarf2_cu *cu, struct dwo_file &dwo_file,
11609 dwarf2_section_info &section, htab_up &cus_htab)
11610 {
11611 struct objfile *objfile = per_objfile->objfile;
11612 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11613 const gdb_byte *info_ptr, *end_ptr;
11614
11615 section.read (objfile);
11616 info_ptr = section.buffer;
11617
11618 if (info_ptr == NULL)
11619 return;
11620
11621 dwarf_read_debug_printf ("Reading %s for %s:",
11622 section.get_name (),
11623 section.get_file_name ());
11624
11625 end_ptr = info_ptr + section.size;
11626 while (info_ptr < end_ptr)
11627 {
11628 struct dwarf2_per_cu_data per_cu;
11629 struct dwo_unit read_unit {};
11630 struct dwo_unit *dwo_unit;
11631 void **slot;
11632 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11633
11634 memset (&per_cu, 0, sizeof (per_cu));
11635 per_cu.per_bfd = per_bfd;
11636 per_cu.is_debug_types = 0;
11637 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11638 per_cu.section = &section;
11639
11640 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11641 if (!reader.dummy_p)
11642 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11643 &dwo_file, &read_unit);
11644 info_ptr += per_cu.length;
11645
11646 // If the unit could not be parsed, skip it.
11647 if (read_unit.dwo_file == NULL)
11648 continue;
11649
11650 if (cus_htab == NULL)
11651 cus_htab = allocate_dwo_unit_table ();
11652
11653 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11654 struct dwo_unit);
11655 *dwo_unit = read_unit;
11656 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11657 gdb_assert (slot != NULL);
11658 if (*slot != NULL)
11659 {
11660 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11661 sect_offset dup_sect_off = dup_cu->sect_off;
11662
11663 complaint (_("debug cu entry at offset %s is duplicate to"
11664 " the entry at offset %s, signature %s"),
11665 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11666 hex_string (dwo_unit->signature));
11667 }
11668 *slot = (void *)dwo_unit;
11669 }
11670 }
11671
11672 /* DWP file .debug_{cu,tu}_index section format:
11673 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11674 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11675
11676 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11677 officially standard DWP format was published with DWARF v5 and is called
11678 Version 5. There are no versions 3 or 4.
11679
11680 DWP Version 1:
11681
11682 Both index sections have the same format, and serve to map a 64-bit
11683 signature to a set of section numbers. Each section begins with a header,
11684 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11685 indexes, and a pool of 32-bit section numbers. The index sections will be
11686 aligned at 8-byte boundaries in the file.
11687
11688 The index section header consists of:
11689
11690 V, 32 bit version number
11691 -, 32 bits unused
11692 N, 32 bit number of compilation units or type units in the index
11693 M, 32 bit number of slots in the hash table
11694
11695 Numbers are recorded using the byte order of the application binary.
11696
11697 The hash table begins at offset 16 in the section, and consists of an array
11698 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11699 order of the application binary). Unused slots in the hash table are 0.
11700 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11701
11702 The parallel table begins immediately after the hash table
11703 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11704 array of 32-bit indexes (using the byte order of the application binary),
11705 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11706 table contains a 32-bit index into the pool of section numbers. For unused
11707 hash table slots, the corresponding entry in the parallel table will be 0.
11708
11709 The pool of section numbers begins immediately following the hash table
11710 (at offset 16 + 12 * M from the beginning of the section). The pool of
11711 section numbers consists of an array of 32-bit words (using the byte order
11712 of the application binary). Each item in the array is indexed starting
11713 from 0. The hash table entry provides the index of the first section
11714 number in the set. Additional section numbers in the set follow, and the
11715 set is terminated by a 0 entry (section number 0 is not used in ELF).
11716
11717 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11718 section must be the first entry in the set, and the .debug_abbrev.dwo must
11719 be the second entry. Other members of the set may follow in any order.
11720
11721 ---
11722
11723 DWP Versions 2 and 5:
11724
11725 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11726 and the entries in the index tables are now offsets into these sections.
11727 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11728 section.
11729
11730 Index Section Contents:
11731 Header
11732 Hash Table of Signatures dwp_hash_table.hash_table
11733 Parallel Table of Indices dwp_hash_table.unit_table
11734 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11735 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11736
11737 The index section header consists of:
11738
11739 V, 32 bit version number
11740 L, 32 bit number of columns in the table of section offsets
11741 N, 32 bit number of compilation units or type units in the index
11742 M, 32 bit number of slots in the hash table
11743
11744 Numbers are recorded using the byte order of the application binary.
11745
11746 The hash table has the same format as version 1.
11747 The parallel table of indices has the same format as version 1,
11748 except that the entries are origin-1 indices into the table of sections
11749 offsets and the table of section sizes.
11750
11751 The table of offsets begins immediately following the parallel table
11752 (at offset 16 + 12 * M from the beginning of the section). The table is
11753 a two-dimensional array of 32-bit words (using the byte order of the
11754 application binary), with L columns and N+1 rows, in row-major order.
11755 Each row in the array is indexed starting from 0. The first row provides
11756 a key to the remaining rows: each column in this row provides an identifier
11757 for a debug section, and the offsets in the same column of subsequent rows
11758 refer to that section. The section identifiers for Version 2 are:
11759
11760 DW_SECT_INFO 1 .debug_info.dwo
11761 DW_SECT_TYPES 2 .debug_types.dwo
11762 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11763 DW_SECT_LINE 4 .debug_line.dwo
11764 DW_SECT_LOC 5 .debug_loc.dwo
11765 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11766 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11767 DW_SECT_MACRO 8 .debug_macro.dwo
11768
11769 The section identifiers for Version 5 are:
11770
11771 DW_SECT_INFO_V5 1 .debug_info.dwo
11772 DW_SECT_RESERVED_V5 2 --
11773 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11774 DW_SECT_LINE_V5 4 .debug_line.dwo
11775 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11776 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11777 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11778 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11779
11780 The offsets provided by the CU and TU index sections are the base offsets
11781 for the contributions made by each CU or TU to the corresponding section
11782 in the package file. Each CU and TU header contains an abbrev_offset
11783 field, used to find the abbreviations table for that CU or TU within the
11784 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11785 be interpreted as relative to the base offset given in the index section.
11786 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11787 should be interpreted as relative to the base offset for .debug_line.dwo,
11788 and offsets into other debug sections obtained from DWARF attributes should
11789 also be interpreted as relative to the corresponding base offset.
11790
11791 The table of sizes begins immediately following the table of offsets.
11792 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11793 with L columns and N rows, in row-major order. Each row in the array is
11794 indexed starting from 1 (row 0 is shared by the two tables).
11795
11796 ---
11797
11798 Hash table lookup is handled the same in version 1 and 2:
11799
11800 We assume that N and M will not exceed 2^32 - 1.
11801 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11802
11803 Given a 64-bit compilation unit signature or a type signature S, an entry
11804 in the hash table is located as follows:
11805
11806 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11807 the low-order k bits all set to 1.
11808
11809 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11810
11811 3) If the hash table entry at index H matches the signature, use that
11812 entry. If the hash table entry at index H is unused (all zeroes),
11813 terminate the search: the signature is not present in the table.
11814
11815 4) Let H = (H + H') modulo M. Repeat at Step 3.
11816
11817 Because M > N and H' and M are relatively prime, the search is guaranteed
11818 to stop at an unused slot or find the match. */
11819
11820 /* Create a hash table to map DWO IDs to their CU/TU entry in
11821 .debug_{info,types}.dwo in DWP_FILE.
11822 Returns NULL if there isn't one.
11823 Note: This function processes DWP files only, not DWO files. */
11824
11825 static struct dwp_hash_table *
11826 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11827 struct dwp_file *dwp_file, int is_debug_types)
11828 {
11829 struct objfile *objfile = per_objfile->objfile;
11830 bfd *dbfd = dwp_file->dbfd.get ();
11831 const gdb_byte *index_ptr, *index_end;
11832 struct dwarf2_section_info *index;
11833 uint32_t version, nr_columns, nr_units, nr_slots;
11834 struct dwp_hash_table *htab;
11835
11836 if (is_debug_types)
11837 index = &dwp_file->sections.tu_index;
11838 else
11839 index = &dwp_file->sections.cu_index;
11840
11841 if (index->empty ())
11842 return NULL;
11843 index->read (objfile);
11844
11845 index_ptr = index->buffer;
11846 index_end = index_ptr + index->size;
11847
11848 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11849 For now it's safe to just read 4 bytes (particularly as it's difficult to
11850 tell if you're dealing with Version 5 before you've read the version). */
11851 version = read_4_bytes (dbfd, index_ptr);
11852 index_ptr += 4;
11853 if (version == 2 || version == 5)
11854 nr_columns = read_4_bytes (dbfd, index_ptr);
11855 else
11856 nr_columns = 0;
11857 index_ptr += 4;
11858 nr_units = read_4_bytes (dbfd, index_ptr);
11859 index_ptr += 4;
11860 nr_slots = read_4_bytes (dbfd, index_ptr);
11861 index_ptr += 4;
11862
11863 if (version != 1 && version != 2 && version != 5)
11864 {
11865 error (_("Dwarf Error: unsupported DWP file version (%s)"
11866 " [in module %s]"),
11867 pulongest (version), dwp_file->name);
11868 }
11869 if (nr_slots != (nr_slots & -nr_slots))
11870 {
11871 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11872 " is not power of 2 [in module %s]"),
11873 pulongest (nr_slots), dwp_file->name);
11874 }
11875
11876 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11877 htab->version = version;
11878 htab->nr_columns = nr_columns;
11879 htab->nr_units = nr_units;
11880 htab->nr_slots = nr_slots;
11881 htab->hash_table = index_ptr;
11882 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11883
11884 /* Exit early if the table is empty. */
11885 if (nr_slots == 0 || nr_units == 0
11886 || (version == 2 && nr_columns == 0)
11887 || (version == 5 && nr_columns == 0))
11888 {
11889 /* All must be zero. */
11890 if (nr_slots != 0 || nr_units != 0
11891 || (version == 2 && nr_columns != 0)
11892 || (version == 5 && nr_columns != 0))
11893 {
11894 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11895 " all zero [in modules %s]"),
11896 dwp_file->name);
11897 }
11898 return htab;
11899 }
11900
11901 if (version == 1)
11902 {
11903 htab->section_pool.v1.indices =
11904 htab->unit_table + sizeof (uint32_t) * nr_slots;
11905 /* It's harder to decide whether the section is too small in v1.
11906 V1 is deprecated anyway so we punt. */
11907 }
11908 else if (version == 2)
11909 {
11910 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11911 int *ids = htab->section_pool.v2.section_ids;
11912 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11913 /* Reverse map for error checking. */
11914 int ids_seen[DW_SECT_MAX + 1];
11915 int i;
11916
11917 if (nr_columns < 2)
11918 {
11919 error (_("Dwarf Error: bad DWP hash table, too few columns"
11920 " in section table [in module %s]"),
11921 dwp_file->name);
11922 }
11923 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11924 {
11925 error (_("Dwarf Error: bad DWP hash table, too many columns"
11926 " in section table [in module %s]"),
11927 dwp_file->name);
11928 }
11929 memset (ids, 255, sizeof_ids);
11930 memset (ids_seen, 255, sizeof (ids_seen));
11931 for (i = 0; i < nr_columns; ++i)
11932 {
11933 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11934
11935 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11936 {
11937 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11938 " in section table [in module %s]"),
11939 id, dwp_file->name);
11940 }
11941 if (ids_seen[id] != -1)
11942 {
11943 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11944 " id %d in section table [in module %s]"),
11945 id, dwp_file->name);
11946 }
11947 ids_seen[id] = i;
11948 ids[i] = id;
11949 }
11950 /* Must have exactly one info or types section. */
11951 if (((ids_seen[DW_SECT_INFO] != -1)
11952 + (ids_seen[DW_SECT_TYPES] != -1))
11953 != 1)
11954 {
11955 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11956 " DWO info/types section [in module %s]"),
11957 dwp_file->name);
11958 }
11959 /* Must have an abbrev section. */
11960 if (ids_seen[DW_SECT_ABBREV] == -1)
11961 {
11962 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11963 " section [in module %s]"),
11964 dwp_file->name);
11965 }
11966 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11967 htab->section_pool.v2.sizes =
11968 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11969 * nr_units * nr_columns);
11970 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11971 * nr_units * nr_columns))
11972 > index_end)
11973 {
11974 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11975 " [in module %s]"),
11976 dwp_file->name);
11977 }
11978 }
11979 else /* version == 5 */
11980 {
11981 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11982 int *ids = htab->section_pool.v5.section_ids;
11983 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11984 /* Reverse map for error checking. */
11985 int ids_seen[DW_SECT_MAX_V5 + 1];
11986
11987 if (nr_columns < 2)
11988 {
11989 error (_("Dwarf Error: bad DWP hash table, too few columns"
11990 " in section table [in module %s]"),
11991 dwp_file->name);
11992 }
11993 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11994 {
11995 error (_("Dwarf Error: bad DWP hash table, too many columns"
11996 " in section table [in module %s]"),
11997 dwp_file->name);
11998 }
11999 memset (ids, 255, sizeof_ids);
12000 memset (ids_seen, 255, sizeof (ids_seen));
12001 for (int i = 0; i < nr_columns; ++i)
12002 {
12003 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12004
12005 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
12006 {
12007 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12008 " in section table [in module %s]"),
12009 id, dwp_file->name);
12010 }
12011 if (ids_seen[id] != -1)
12012 {
12013 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12014 " id %d in section table [in module %s]"),
12015 id, dwp_file->name);
12016 }
12017 ids_seen[id] = i;
12018 ids[i] = id;
12019 }
12020 /* Must have seen an info section. */
12021 if (ids_seen[DW_SECT_INFO_V5] == -1)
12022 {
12023 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12024 " DWO info/types section [in module %s]"),
12025 dwp_file->name);
12026 }
12027 /* Must have an abbrev section. */
12028 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
12029 {
12030 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12031 " section [in module %s]"),
12032 dwp_file->name);
12033 }
12034 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12035 htab->section_pool.v5.sizes
12036 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12037 * nr_units * nr_columns);
12038 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12039 * nr_units * nr_columns))
12040 > index_end)
12041 {
12042 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12043 " [in module %s]"),
12044 dwp_file->name);
12045 }
12046 }
12047
12048 return htab;
12049 }
12050
12051 /* Update SECTIONS with the data from SECTP.
12052
12053 This function is like the other "locate" section routines, but in
12054 this context the sections to read comes from the DWP V1 hash table,
12055 not the full ELF section table.
12056
12057 The result is non-zero for success, or zero if an error was found. */
12058
12059 static int
12060 locate_v1_virtual_dwo_sections (asection *sectp,
12061 struct virtual_v1_dwo_sections *sections)
12062 {
12063 const struct dwop_section_names *names = &dwop_section_names;
12064
12065 if (section_is_p (sectp->name, &names->abbrev_dwo))
12066 {
12067 /* There can be only one. */
12068 if (sections->abbrev.s.section != NULL)
12069 return 0;
12070 sections->abbrev.s.section = sectp;
12071 sections->abbrev.size = bfd_section_size (sectp);
12072 }
12073 else if (section_is_p (sectp->name, &names->info_dwo)
12074 || section_is_p (sectp->name, &names->types_dwo))
12075 {
12076 /* There can be only one. */
12077 if (sections->info_or_types.s.section != NULL)
12078 return 0;
12079 sections->info_or_types.s.section = sectp;
12080 sections->info_or_types.size = bfd_section_size (sectp);
12081 }
12082 else if (section_is_p (sectp->name, &names->line_dwo))
12083 {
12084 /* There can be only one. */
12085 if (sections->line.s.section != NULL)
12086 return 0;
12087 sections->line.s.section = sectp;
12088 sections->line.size = bfd_section_size (sectp);
12089 }
12090 else if (section_is_p (sectp->name, &names->loc_dwo))
12091 {
12092 /* There can be only one. */
12093 if (sections->loc.s.section != NULL)
12094 return 0;
12095 sections->loc.s.section = sectp;
12096 sections->loc.size = bfd_section_size (sectp);
12097 }
12098 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12099 {
12100 /* There can be only one. */
12101 if (sections->macinfo.s.section != NULL)
12102 return 0;
12103 sections->macinfo.s.section = sectp;
12104 sections->macinfo.size = bfd_section_size (sectp);
12105 }
12106 else if (section_is_p (sectp->name, &names->macro_dwo))
12107 {
12108 /* There can be only one. */
12109 if (sections->macro.s.section != NULL)
12110 return 0;
12111 sections->macro.s.section = sectp;
12112 sections->macro.size = bfd_section_size (sectp);
12113 }
12114 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12115 {
12116 /* There can be only one. */
12117 if (sections->str_offsets.s.section != NULL)
12118 return 0;
12119 sections->str_offsets.s.section = sectp;
12120 sections->str_offsets.size = bfd_section_size (sectp);
12121 }
12122 else
12123 {
12124 /* No other kind of section is valid. */
12125 return 0;
12126 }
12127
12128 return 1;
12129 }
12130
12131 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12132 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12133 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12134 This is for DWP version 1 files. */
12135
12136 static struct dwo_unit *
12137 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
12138 struct dwp_file *dwp_file,
12139 uint32_t unit_index,
12140 const char *comp_dir,
12141 ULONGEST signature, int is_debug_types)
12142 {
12143 const struct dwp_hash_table *dwp_htab =
12144 is_debug_types ? dwp_file->tus : dwp_file->cus;
12145 bfd *dbfd = dwp_file->dbfd.get ();
12146 const char *kind = is_debug_types ? "TU" : "CU";
12147 struct dwo_file *dwo_file;
12148 struct dwo_unit *dwo_unit;
12149 struct virtual_v1_dwo_sections sections;
12150 void **dwo_file_slot;
12151 int i;
12152
12153 gdb_assert (dwp_file->version == 1);
12154
12155 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12156 kind, pulongest (unit_index), hex_string (signature),
12157 dwp_file->name);
12158
12159 /* Fetch the sections of this DWO unit.
12160 Put a limit on the number of sections we look for so that bad data
12161 doesn't cause us to loop forever. */
12162
12163 #define MAX_NR_V1_DWO_SECTIONS \
12164 (1 /* .debug_info or .debug_types */ \
12165 + 1 /* .debug_abbrev */ \
12166 + 1 /* .debug_line */ \
12167 + 1 /* .debug_loc */ \
12168 + 1 /* .debug_str_offsets */ \
12169 + 1 /* .debug_macro or .debug_macinfo */ \
12170 + 1 /* trailing zero */)
12171
12172 memset (&sections, 0, sizeof (sections));
12173
12174 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
12175 {
12176 asection *sectp;
12177 uint32_t section_nr =
12178 read_4_bytes (dbfd,
12179 dwp_htab->section_pool.v1.indices
12180 + (unit_index + i) * sizeof (uint32_t));
12181
12182 if (section_nr == 0)
12183 break;
12184 if (section_nr >= dwp_file->num_sections)
12185 {
12186 error (_("Dwarf Error: bad DWP hash table, section number too large"
12187 " [in module %s]"),
12188 dwp_file->name);
12189 }
12190
12191 sectp = dwp_file->elf_sections[section_nr];
12192 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
12193 {
12194 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12195 " [in module %s]"),
12196 dwp_file->name);
12197 }
12198 }
12199
12200 if (i < 2
12201 || sections.info_or_types.empty ()
12202 || sections.abbrev.empty ())
12203 {
12204 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12205 " [in module %s]"),
12206 dwp_file->name);
12207 }
12208 if (i == MAX_NR_V1_DWO_SECTIONS)
12209 {
12210 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12211 " [in module %s]"),
12212 dwp_file->name);
12213 }
12214
12215 /* It's easier for the rest of the code if we fake a struct dwo_file and
12216 have dwo_unit "live" in that. At least for now.
12217
12218 The DWP file can be made up of a random collection of CUs and TUs.
12219 However, for each CU + set of TUs that came from the same original DWO
12220 file, we can combine them back into a virtual DWO file to save space
12221 (fewer struct dwo_file objects to allocate). Remember that for really
12222 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12223
12224 std::string virtual_dwo_name =
12225 string_printf ("virtual-dwo/%d-%d-%d-%d",
12226 sections.abbrev.get_id (),
12227 sections.line.get_id (),
12228 sections.loc.get_id (),
12229 sections.str_offsets.get_id ());
12230 /* Can we use an existing virtual DWO file? */
12231 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12232 comp_dir);
12233 /* Create one if necessary. */
12234 if (*dwo_file_slot == NULL)
12235 {
12236 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12237 virtual_dwo_name.c_str ());
12238
12239 dwo_file = new struct dwo_file;
12240 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12241 dwo_file->comp_dir = comp_dir;
12242 dwo_file->sections.abbrev = sections.abbrev;
12243 dwo_file->sections.line = sections.line;
12244 dwo_file->sections.loc = sections.loc;
12245 dwo_file->sections.macinfo = sections.macinfo;
12246 dwo_file->sections.macro = sections.macro;
12247 dwo_file->sections.str_offsets = sections.str_offsets;
12248 /* The "str" section is global to the entire DWP file. */
12249 dwo_file->sections.str = dwp_file->sections.str;
12250 /* The info or types section is assigned below to dwo_unit,
12251 there's no need to record it in dwo_file.
12252 Also, we can't simply record type sections in dwo_file because
12253 we record a pointer into the vector in dwo_unit. As we collect more
12254 types we'll grow the vector and eventually have to reallocate space
12255 for it, invalidating all copies of pointers into the previous
12256 contents. */
12257 *dwo_file_slot = dwo_file;
12258 }
12259 else
12260 {
12261 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12262 virtual_dwo_name.c_str ());
12263
12264 dwo_file = (struct dwo_file *) *dwo_file_slot;
12265 }
12266
12267 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12268 dwo_unit->dwo_file = dwo_file;
12269 dwo_unit->signature = signature;
12270 dwo_unit->section =
12271 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12272 *dwo_unit->section = sections.info_or_types;
12273 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12274
12275 return dwo_unit;
12276 }
12277
12278 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12279 simplify them. Given a pointer to the containing section SECTION, and
12280 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12281 virtual section of just that piece. */
12282
12283 static struct dwarf2_section_info
12284 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12285 struct dwarf2_section_info *section,
12286 bfd_size_type offset, bfd_size_type size)
12287 {
12288 struct dwarf2_section_info result;
12289 asection *sectp;
12290
12291 gdb_assert (section != NULL);
12292 gdb_assert (!section->is_virtual);
12293
12294 memset (&result, 0, sizeof (result));
12295 result.s.containing_section = section;
12296 result.is_virtual = true;
12297
12298 if (size == 0)
12299 return result;
12300
12301 sectp = section->get_bfd_section ();
12302
12303 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12304 bounds of the real section. This is a pretty-rare event, so just
12305 flag an error (easier) instead of a warning and trying to cope. */
12306 if (sectp == NULL
12307 || offset + size > bfd_section_size (sectp))
12308 {
12309 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
12310 " in section %s [in module %s]"),
12311 sectp ? bfd_section_name (sectp) : "<unknown>",
12312 objfile_name (per_objfile->objfile));
12313 }
12314
12315 result.virtual_offset = offset;
12316 result.size = size;
12317 return result;
12318 }
12319
12320 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12321 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12322 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12323 This is for DWP version 2 files. */
12324
12325 static struct dwo_unit *
12326 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
12327 struct dwp_file *dwp_file,
12328 uint32_t unit_index,
12329 const char *comp_dir,
12330 ULONGEST signature, int is_debug_types)
12331 {
12332 const struct dwp_hash_table *dwp_htab =
12333 is_debug_types ? dwp_file->tus : dwp_file->cus;
12334 bfd *dbfd = dwp_file->dbfd.get ();
12335 const char *kind = is_debug_types ? "TU" : "CU";
12336 struct dwo_file *dwo_file;
12337 struct dwo_unit *dwo_unit;
12338 struct virtual_v2_or_v5_dwo_sections sections;
12339 void **dwo_file_slot;
12340 int i;
12341
12342 gdb_assert (dwp_file->version == 2);
12343
12344 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12345 kind, pulongest (unit_index), hex_string (signature),
12346 dwp_file->name);
12347
12348 /* Fetch the section offsets of this DWO unit. */
12349
12350 memset (&sections, 0, sizeof (sections));
12351
12352 for (i = 0; i < dwp_htab->nr_columns; ++i)
12353 {
12354 uint32_t offset = read_4_bytes (dbfd,
12355 dwp_htab->section_pool.v2.offsets
12356 + (((unit_index - 1) * dwp_htab->nr_columns
12357 + i)
12358 * sizeof (uint32_t)));
12359 uint32_t size = read_4_bytes (dbfd,
12360 dwp_htab->section_pool.v2.sizes
12361 + (((unit_index - 1) * dwp_htab->nr_columns
12362 + i)
12363 * sizeof (uint32_t)));
12364
12365 switch (dwp_htab->section_pool.v2.section_ids[i])
12366 {
12367 case DW_SECT_INFO:
12368 case DW_SECT_TYPES:
12369 sections.info_or_types_offset = offset;
12370 sections.info_or_types_size = size;
12371 break;
12372 case DW_SECT_ABBREV:
12373 sections.abbrev_offset = offset;
12374 sections.abbrev_size = size;
12375 break;
12376 case DW_SECT_LINE:
12377 sections.line_offset = offset;
12378 sections.line_size = size;
12379 break;
12380 case DW_SECT_LOC:
12381 sections.loc_offset = offset;
12382 sections.loc_size = size;
12383 break;
12384 case DW_SECT_STR_OFFSETS:
12385 sections.str_offsets_offset = offset;
12386 sections.str_offsets_size = size;
12387 break;
12388 case DW_SECT_MACINFO:
12389 sections.macinfo_offset = offset;
12390 sections.macinfo_size = size;
12391 break;
12392 case DW_SECT_MACRO:
12393 sections.macro_offset = offset;
12394 sections.macro_size = size;
12395 break;
12396 }
12397 }
12398
12399 /* It's easier for the rest of the code if we fake a struct dwo_file and
12400 have dwo_unit "live" in that. At least for now.
12401
12402 The DWP file can be made up of a random collection of CUs and TUs.
12403 However, for each CU + set of TUs that came from the same original DWO
12404 file, we can combine them back into a virtual DWO file to save space
12405 (fewer struct dwo_file objects to allocate). Remember that for really
12406 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12407
12408 std::string virtual_dwo_name =
12409 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12410 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12411 (long) (sections.line_size ? sections.line_offset : 0),
12412 (long) (sections.loc_size ? sections.loc_offset : 0),
12413 (long) (sections.str_offsets_size
12414 ? sections.str_offsets_offset : 0));
12415 /* Can we use an existing virtual DWO file? */
12416 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12417 comp_dir);
12418 /* Create one if necessary. */
12419 if (*dwo_file_slot == NULL)
12420 {
12421 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12422 virtual_dwo_name.c_str ());
12423
12424 dwo_file = new struct dwo_file;
12425 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12426 dwo_file->comp_dir = comp_dir;
12427 dwo_file->sections.abbrev =
12428 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12429 sections.abbrev_offset,
12430 sections.abbrev_size);
12431 dwo_file->sections.line =
12432 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12433 sections.line_offset,
12434 sections.line_size);
12435 dwo_file->sections.loc =
12436 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12437 sections.loc_offset, sections.loc_size);
12438 dwo_file->sections.macinfo =
12439 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12440 sections.macinfo_offset,
12441 sections.macinfo_size);
12442 dwo_file->sections.macro =
12443 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12444 sections.macro_offset,
12445 sections.macro_size);
12446 dwo_file->sections.str_offsets =
12447 create_dwp_v2_or_v5_section (per_objfile,
12448 &dwp_file->sections.str_offsets,
12449 sections.str_offsets_offset,
12450 sections.str_offsets_size);
12451 /* The "str" section is global to the entire DWP file. */
12452 dwo_file->sections.str = dwp_file->sections.str;
12453 /* The info or types section is assigned below to dwo_unit,
12454 there's no need to record it in dwo_file.
12455 Also, we can't simply record type sections in dwo_file because
12456 we record a pointer into the vector in dwo_unit. As we collect more
12457 types we'll grow the vector and eventually have to reallocate space
12458 for it, invalidating all copies of pointers into the previous
12459 contents. */
12460 *dwo_file_slot = dwo_file;
12461 }
12462 else
12463 {
12464 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12465 virtual_dwo_name.c_str ());
12466
12467 dwo_file = (struct dwo_file *) *dwo_file_slot;
12468 }
12469
12470 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12471 dwo_unit->dwo_file = dwo_file;
12472 dwo_unit->signature = signature;
12473 dwo_unit->section =
12474 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12475 *dwo_unit->section = create_dwp_v2_or_v5_section
12476 (per_objfile,
12477 is_debug_types
12478 ? &dwp_file->sections.types
12479 : &dwp_file->sections.info,
12480 sections.info_or_types_offset,
12481 sections.info_or_types_size);
12482 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12483
12484 return dwo_unit;
12485 }
12486
12487 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12488 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12489 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12490 This is for DWP version 5 files. */
12491
12492 static struct dwo_unit *
12493 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12494 struct dwp_file *dwp_file,
12495 uint32_t unit_index,
12496 const char *comp_dir,
12497 ULONGEST signature, int is_debug_types)
12498 {
12499 const struct dwp_hash_table *dwp_htab
12500 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12501 bfd *dbfd = dwp_file->dbfd.get ();
12502 const char *kind = is_debug_types ? "TU" : "CU";
12503 struct dwo_file *dwo_file;
12504 struct dwo_unit *dwo_unit;
12505 struct virtual_v2_or_v5_dwo_sections sections {};
12506 void **dwo_file_slot;
12507
12508 gdb_assert (dwp_file->version == 5);
12509
12510 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12511 kind, pulongest (unit_index), hex_string (signature),
12512 dwp_file->name);
12513
12514 /* Fetch the section offsets of this DWO unit. */
12515
12516 /* memset (&sections, 0, sizeof (sections)); */
12517
12518 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12519 {
12520 uint32_t offset = read_4_bytes (dbfd,
12521 dwp_htab->section_pool.v5.offsets
12522 + (((unit_index - 1)
12523 * dwp_htab->nr_columns
12524 + i)
12525 * sizeof (uint32_t)));
12526 uint32_t size = read_4_bytes (dbfd,
12527 dwp_htab->section_pool.v5.sizes
12528 + (((unit_index - 1) * dwp_htab->nr_columns
12529 + i)
12530 * sizeof (uint32_t)));
12531
12532 switch (dwp_htab->section_pool.v5.section_ids[i])
12533 {
12534 case DW_SECT_ABBREV_V5:
12535 sections.abbrev_offset = offset;
12536 sections.abbrev_size = size;
12537 break;
12538 case DW_SECT_INFO_V5:
12539 sections.info_or_types_offset = offset;
12540 sections.info_or_types_size = size;
12541 break;
12542 case DW_SECT_LINE_V5:
12543 sections.line_offset = offset;
12544 sections.line_size = size;
12545 break;
12546 case DW_SECT_LOCLISTS_V5:
12547 sections.loclists_offset = offset;
12548 sections.loclists_size = size;
12549 break;
12550 case DW_SECT_MACRO_V5:
12551 sections.macro_offset = offset;
12552 sections.macro_size = size;
12553 break;
12554 case DW_SECT_RNGLISTS_V5:
12555 sections.rnglists_offset = offset;
12556 sections.rnglists_size = size;
12557 break;
12558 case DW_SECT_STR_OFFSETS_V5:
12559 sections.str_offsets_offset = offset;
12560 sections.str_offsets_size = size;
12561 break;
12562 case DW_SECT_RESERVED_V5:
12563 default:
12564 break;
12565 }
12566 }
12567
12568 /* It's easier for the rest of the code if we fake a struct dwo_file and
12569 have dwo_unit "live" in that. At least for now.
12570
12571 The DWP file can be made up of a random collection of CUs and TUs.
12572 However, for each CU + set of TUs that came from the same original DWO
12573 file, we can combine them back into a virtual DWO file to save space
12574 (fewer struct dwo_file objects to allocate). Remember that for really
12575 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12576
12577 std::string virtual_dwo_name =
12578 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12579 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12580 (long) (sections.line_size ? sections.line_offset : 0),
12581 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12582 (long) (sections.str_offsets_size
12583 ? sections.str_offsets_offset : 0),
12584 (long) (sections.macro_size ? sections.macro_offset : 0),
12585 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12586 /* Can we use an existing virtual DWO file? */
12587 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12588 virtual_dwo_name.c_str (),
12589 comp_dir);
12590 /* Create one if necessary. */
12591 if (*dwo_file_slot == NULL)
12592 {
12593 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12594 virtual_dwo_name.c_str ());
12595
12596 dwo_file = new struct dwo_file;
12597 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12598 dwo_file->comp_dir = comp_dir;
12599 dwo_file->sections.abbrev =
12600 create_dwp_v2_or_v5_section (per_objfile,
12601 &dwp_file->sections.abbrev,
12602 sections.abbrev_offset,
12603 sections.abbrev_size);
12604 dwo_file->sections.line =
12605 create_dwp_v2_or_v5_section (per_objfile,
12606 &dwp_file->sections.line,
12607 sections.line_offset, sections.line_size);
12608 dwo_file->sections.macro =
12609 create_dwp_v2_or_v5_section (per_objfile,
12610 &dwp_file->sections.macro,
12611 sections.macro_offset,
12612 sections.macro_size);
12613 dwo_file->sections.loclists =
12614 create_dwp_v2_or_v5_section (per_objfile,
12615 &dwp_file->sections.loclists,
12616 sections.loclists_offset,
12617 sections.loclists_size);
12618 dwo_file->sections.rnglists =
12619 create_dwp_v2_or_v5_section (per_objfile,
12620 &dwp_file->sections.rnglists,
12621 sections.rnglists_offset,
12622 sections.rnglists_size);
12623 dwo_file->sections.str_offsets =
12624 create_dwp_v2_or_v5_section (per_objfile,
12625 &dwp_file->sections.str_offsets,
12626 sections.str_offsets_offset,
12627 sections.str_offsets_size);
12628 /* The "str" section is global to the entire DWP file. */
12629 dwo_file->sections.str = dwp_file->sections.str;
12630 /* The info or types section is assigned below to dwo_unit,
12631 there's no need to record it in dwo_file.
12632 Also, we can't simply record type sections in dwo_file because
12633 we record a pointer into the vector in dwo_unit. As we collect more
12634 types we'll grow the vector and eventually have to reallocate space
12635 for it, invalidating all copies of pointers into the previous
12636 contents. */
12637 *dwo_file_slot = dwo_file;
12638 }
12639 else
12640 {
12641 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12642 virtual_dwo_name.c_str ());
12643
12644 dwo_file = (struct dwo_file *) *dwo_file_slot;
12645 }
12646
12647 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12648 dwo_unit->dwo_file = dwo_file;
12649 dwo_unit->signature = signature;
12650 dwo_unit->section
12651 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12652 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12653 &dwp_file->sections.info,
12654 sections.info_or_types_offset,
12655 sections.info_or_types_size);
12656 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12657
12658 return dwo_unit;
12659 }
12660
12661 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12662 Returns NULL if the signature isn't found. */
12663
12664 static struct dwo_unit *
12665 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12666 struct dwp_file *dwp_file, const char *comp_dir,
12667 ULONGEST signature, int is_debug_types)
12668 {
12669 const struct dwp_hash_table *dwp_htab =
12670 is_debug_types ? dwp_file->tus : dwp_file->cus;
12671 bfd *dbfd = dwp_file->dbfd.get ();
12672 uint32_t mask = dwp_htab->nr_slots - 1;
12673 uint32_t hash = signature & mask;
12674 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12675 unsigned int i;
12676 void **slot;
12677 struct dwo_unit find_dwo_cu;
12678
12679 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12680 find_dwo_cu.signature = signature;
12681 slot = htab_find_slot (is_debug_types
12682 ? dwp_file->loaded_tus.get ()
12683 : dwp_file->loaded_cus.get (),
12684 &find_dwo_cu, INSERT);
12685
12686 if (*slot != NULL)
12687 return (struct dwo_unit *) *slot;
12688
12689 /* Use a for loop so that we don't loop forever on bad debug info. */
12690 for (i = 0; i < dwp_htab->nr_slots; ++i)
12691 {
12692 ULONGEST signature_in_table;
12693
12694 signature_in_table =
12695 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12696 if (signature_in_table == signature)
12697 {
12698 uint32_t unit_index =
12699 read_4_bytes (dbfd,
12700 dwp_htab->unit_table + hash * sizeof (uint32_t));
12701
12702 if (dwp_file->version == 1)
12703 {
12704 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12705 unit_index, comp_dir,
12706 signature, is_debug_types);
12707 }
12708 else if (dwp_file->version == 2)
12709 {
12710 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12711 unit_index, comp_dir,
12712 signature, is_debug_types);
12713 }
12714 else /* version == 5 */
12715 {
12716 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12717 unit_index, comp_dir,
12718 signature, is_debug_types);
12719 }
12720 return (struct dwo_unit *) *slot;
12721 }
12722 if (signature_in_table == 0)
12723 return NULL;
12724 hash = (hash + hash2) & mask;
12725 }
12726
12727 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12728 " [in module %s]"),
12729 dwp_file->name);
12730 }
12731
12732 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12733 Open the file specified by FILE_NAME and hand it off to BFD for
12734 preliminary analysis. Return a newly initialized bfd *, which
12735 includes a canonicalized copy of FILE_NAME.
12736 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12737 SEARCH_CWD is true if the current directory is to be searched.
12738 It will be searched before debug-file-directory.
12739 If successful, the file is added to the bfd include table of the
12740 objfile's bfd (see gdb_bfd_record_inclusion).
12741 If unable to find/open the file, return NULL.
12742 NOTE: This function is derived from symfile_bfd_open. */
12743
12744 static gdb_bfd_ref_ptr
12745 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12746 const char *file_name, int is_dwp, int search_cwd)
12747 {
12748 int desc;
12749 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12750 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12751 to debug_file_directory. */
12752 const char *search_path;
12753 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12754
12755 gdb::unique_xmalloc_ptr<char> search_path_holder;
12756 if (search_cwd)
12757 {
12758 if (*debug_file_directory != '\0')
12759 {
12760 search_path_holder.reset (concat (".", dirname_separator_string,
12761 debug_file_directory,
12762 (char *) NULL));
12763 search_path = search_path_holder.get ();
12764 }
12765 else
12766 search_path = ".";
12767 }
12768 else
12769 search_path = debug_file_directory;
12770
12771 openp_flags flags = OPF_RETURN_REALPATH;
12772 if (is_dwp)
12773 flags |= OPF_SEARCH_IN_PATH;
12774
12775 gdb::unique_xmalloc_ptr<char> absolute_name;
12776 desc = openp (search_path, flags, file_name,
12777 O_RDONLY | O_BINARY, &absolute_name);
12778 if (desc < 0)
12779 return NULL;
12780
12781 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12782 gnutarget, desc));
12783 if (sym_bfd == NULL)
12784 return NULL;
12785 bfd_set_cacheable (sym_bfd.get (), 1);
12786
12787 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12788 return NULL;
12789
12790 /* Success. Record the bfd as having been included by the objfile's bfd.
12791 This is important because things like demangled_names_hash lives in the
12792 objfile's per_bfd space and may have references to things like symbol
12793 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12794 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12795
12796 return sym_bfd;
12797 }
12798
12799 /* Try to open DWO file FILE_NAME.
12800 COMP_DIR is the DW_AT_comp_dir attribute.
12801 The result is the bfd handle of the file.
12802 If there is a problem finding or opening the file, return NULL.
12803 Upon success, the canonicalized path of the file is stored in the bfd,
12804 same as symfile_bfd_open. */
12805
12806 static gdb_bfd_ref_ptr
12807 open_dwo_file (dwarf2_per_objfile *per_objfile,
12808 const char *file_name, const char *comp_dir)
12809 {
12810 if (IS_ABSOLUTE_PATH (file_name))
12811 return try_open_dwop_file (per_objfile, file_name,
12812 0 /*is_dwp*/, 0 /*search_cwd*/);
12813
12814 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12815
12816 if (comp_dir != NULL)
12817 {
12818 gdb::unique_xmalloc_ptr<char> path_to_try
12819 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12820
12821 /* NOTE: If comp_dir is a relative path, this will also try the
12822 search path, which seems useful. */
12823 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12824 0 /*is_dwp*/,
12825 1 /*search_cwd*/));
12826 if (abfd != NULL)
12827 return abfd;
12828 }
12829
12830 /* That didn't work, try debug-file-directory, which, despite its name,
12831 is a list of paths. */
12832
12833 if (*debug_file_directory == '\0')
12834 return NULL;
12835
12836 return try_open_dwop_file (per_objfile, file_name,
12837 0 /*is_dwp*/, 1 /*search_cwd*/);
12838 }
12839
12840 /* This function is mapped across the sections and remembers the offset and
12841 size of each of the DWO debugging sections we are interested in. */
12842
12843 static void
12844 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12845 dwo_sections *dwo_sections)
12846 {
12847 const struct dwop_section_names *names = &dwop_section_names;
12848
12849 if (section_is_p (sectp->name, &names->abbrev_dwo))
12850 {
12851 dwo_sections->abbrev.s.section = sectp;
12852 dwo_sections->abbrev.size = bfd_section_size (sectp);
12853 }
12854 else if (section_is_p (sectp->name, &names->info_dwo))
12855 {
12856 dwo_sections->info.s.section = sectp;
12857 dwo_sections->info.size = bfd_section_size (sectp);
12858 }
12859 else if (section_is_p (sectp->name, &names->line_dwo))
12860 {
12861 dwo_sections->line.s.section = sectp;
12862 dwo_sections->line.size = bfd_section_size (sectp);
12863 }
12864 else if (section_is_p (sectp->name, &names->loc_dwo))
12865 {
12866 dwo_sections->loc.s.section = sectp;
12867 dwo_sections->loc.size = bfd_section_size (sectp);
12868 }
12869 else if (section_is_p (sectp->name, &names->loclists_dwo))
12870 {
12871 dwo_sections->loclists.s.section = sectp;
12872 dwo_sections->loclists.size = bfd_section_size (sectp);
12873 }
12874 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12875 {
12876 dwo_sections->macinfo.s.section = sectp;
12877 dwo_sections->macinfo.size = bfd_section_size (sectp);
12878 }
12879 else if (section_is_p (sectp->name, &names->macro_dwo))
12880 {
12881 dwo_sections->macro.s.section = sectp;
12882 dwo_sections->macro.size = bfd_section_size (sectp);
12883 }
12884 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12885 {
12886 dwo_sections->rnglists.s.section = sectp;
12887 dwo_sections->rnglists.size = bfd_section_size (sectp);
12888 }
12889 else if (section_is_p (sectp->name, &names->str_dwo))
12890 {
12891 dwo_sections->str.s.section = sectp;
12892 dwo_sections->str.size = bfd_section_size (sectp);
12893 }
12894 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12895 {
12896 dwo_sections->str_offsets.s.section = sectp;
12897 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12898 }
12899 else if (section_is_p (sectp->name, &names->types_dwo))
12900 {
12901 struct dwarf2_section_info type_section;
12902
12903 memset (&type_section, 0, sizeof (type_section));
12904 type_section.s.section = sectp;
12905 type_section.size = bfd_section_size (sectp);
12906 dwo_sections->types.push_back (type_section);
12907 }
12908 }
12909
12910 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12911 by PER_CU. This is for the non-DWP case.
12912 The result is NULL if DWO_NAME can't be found. */
12913
12914 static struct dwo_file *
12915 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12916 const char *comp_dir)
12917 {
12918 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12919
12920 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12921 if (dbfd == NULL)
12922 {
12923 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12924
12925 return NULL;
12926 }
12927
12928 dwo_file_up dwo_file (new struct dwo_file);
12929 dwo_file->dwo_name = dwo_name;
12930 dwo_file->comp_dir = comp_dir;
12931 dwo_file->dbfd = std::move (dbfd);
12932
12933 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12934 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12935 &dwo_file->sections);
12936
12937 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12938 dwo_file->cus);
12939
12940 if (cu->per_cu->dwarf_version < 5)
12941 {
12942 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12943 dwo_file->sections.types, dwo_file->tus);
12944 }
12945 else
12946 {
12947 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12948 &dwo_file->sections.info, dwo_file->tus,
12949 rcuh_kind::TYPE);
12950 }
12951
12952 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12953
12954 return dwo_file.release ();
12955 }
12956
12957 /* This function is mapped across the sections and remembers the offset and
12958 size of each of the DWP debugging sections common to version 1 and 2 that
12959 we are interested in. */
12960
12961 static void
12962 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12963 dwp_file *dwp_file)
12964 {
12965 const struct dwop_section_names *names = &dwop_section_names;
12966 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12967
12968 /* Record the ELF section number for later lookup: this is what the
12969 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12970 gdb_assert (elf_section_nr < dwp_file->num_sections);
12971 dwp_file->elf_sections[elf_section_nr] = sectp;
12972
12973 /* Look for specific sections that we need. */
12974 if (section_is_p (sectp->name, &names->str_dwo))
12975 {
12976 dwp_file->sections.str.s.section = sectp;
12977 dwp_file->sections.str.size = bfd_section_size (sectp);
12978 }
12979 else if (section_is_p (sectp->name, &names->cu_index))
12980 {
12981 dwp_file->sections.cu_index.s.section = sectp;
12982 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12983 }
12984 else if (section_is_p (sectp->name, &names->tu_index))
12985 {
12986 dwp_file->sections.tu_index.s.section = sectp;
12987 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12988 }
12989 }
12990
12991 /* This function is mapped across the sections and remembers the offset and
12992 size of each of the DWP version 2 debugging sections that we are interested
12993 in. This is split into a separate function because we don't know if we
12994 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12995
12996 static void
12997 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12998 {
12999 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13000 const struct dwop_section_names *names = &dwop_section_names;
13001 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13002
13003 /* Record the ELF section number for later lookup: this is what the
13004 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13005 gdb_assert (elf_section_nr < dwp_file->num_sections);
13006 dwp_file->elf_sections[elf_section_nr] = sectp;
13007
13008 /* Look for specific sections that we need. */
13009 if (section_is_p (sectp->name, &names->abbrev_dwo))
13010 {
13011 dwp_file->sections.abbrev.s.section = sectp;
13012 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13013 }
13014 else if (section_is_p (sectp->name, &names->info_dwo))
13015 {
13016 dwp_file->sections.info.s.section = sectp;
13017 dwp_file->sections.info.size = bfd_section_size (sectp);
13018 }
13019 else if (section_is_p (sectp->name, &names->line_dwo))
13020 {
13021 dwp_file->sections.line.s.section = sectp;
13022 dwp_file->sections.line.size = bfd_section_size (sectp);
13023 }
13024 else if (section_is_p (sectp->name, &names->loc_dwo))
13025 {
13026 dwp_file->sections.loc.s.section = sectp;
13027 dwp_file->sections.loc.size = bfd_section_size (sectp);
13028 }
13029 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13030 {
13031 dwp_file->sections.macinfo.s.section = sectp;
13032 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
13033 }
13034 else if (section_is_p (sectp->name, &names->macro_dwo))
13035 {
13036 dwp_file->sections.macro.s.section = sectp;
13037 dwp_file->sections.macro.size = bfd_section_size (sectp);
13038 }
13039 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13040 {
13041 dwp_file->sections.str_offsets.s.section = sectp;
13042 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13043 }
13044 else if (section_is_p (sectp->name, &names->types_dwo))
13045 {
13046 dwp_file->sections.types.s.section = sectp;
13047 dwp_file->sections.types.size = bfd_section_size (sectp);
13048 }
13049 }
13050
13051 /* This function is mapped across the sections and remembers the offset and
13052 size of each of the DWP version 5 debugging sections that we are interested
13053 in. This is split into a separate function because we don't know if we
13054 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13055
13056 static void
13057 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13058 {
13059 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13060 const struct dwop_section_names *names = &dwop_section_names;
13061 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13062
13063 /* Record the ELF section number for later lookup: this is what the
13064 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13065 gdb_assert (elf_section_nr < dwp_file->num_sections);
13066 dwp_file->elf_sections[elf_section_nr] = sectp;
13067
13068 /* Look for specific sections that we need. */
13069 if (section_is_p (sectp->name, &names->abbrev_dwo))
13070 {
13071 dwp_file->sections.abbrev.s.section = sectp;
13072 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13073 }
13074 else if (section_is_p (sectp->name, &names->info_dwo))
13075 {
13076 dwp_file->sections.info.s.section = sectp;
13077 dwp_file->sections.info.size = bfd_section_size (sectp);
13078 }
13079 else if (section_is_p (sectp->name, &names->line_dwo))
13080 {
13081 dwp_file->sections.line.s.section = sectp;
13082 dwp_file->sections.line.size = bfd_section_size (sectp);
13083 }
13084 else if (section_is_p (sectp->name, &names->loclists_dwo))
13085 {
13086 dwp_file->sections.loclists.s.section = sectp;
13087 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13088 }
13089 else if (section_is_p (sectp->name, &names->macro_dwo))
13090 {
13091 dwp_file->sections.macro.s.section = sectp;
13092 dwp_file->sections.macro.size = bfd_section_size (sectp);
13093 }
13094 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13095 {
13096 dwp_file->sections.rnglists.s.section = sectp;
13097 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13098 }
13099 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13100 {
13101 dwp_file->sections.str_offsets.s.section = sectp;
13102 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13103 }
13104 }
13105
13106 /* Hash function for dwp_file loaded CUs/TUs. */
13107
13108 static hashval_t
13109 hash_dwp_loaded_cutus (const void *item)
13110 {
13111 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
13112
13113 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13114 return dwo_unit->signature;
13115 }
13116
13117 /* Equality function for dwp_file loaded CUs/TUs. */
13118
13119 static int
13120 eq_dwp_loaded_cutus (const void *a, const void *b)
13121 {
13122 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13123 const struct dwo_unit *dub = (const struct dwo_unit *) b;
13124
13125 return dua->signature == dub->signature;
13126 }
13127
13128 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
13129
13130 static htab_up
13131 allocate_dwp_loaded_cutus_table ()
13132 {
13133 return htab_up (htab_create_alloc (3,
13134 hash_dwp_loaded_cutus,
13135 eq_dwp_loaded_cutus,
13136 NULL, xcalloc, xfree));
13137 }
13138
13139 /* Try to open DWP file FILE_NAME.
13140 The result is the bfd handle of the file.
13141 If there is a problem finding or opening the file, return NULL.
13142 Upon success, the canonicalized path of the file is stored in the bfd,
13143 same as symfile_bfd_open. */
13144
13145 static gdb_bfd_ref_ptr
13146 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
13147 {
13148 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
13149 1 /*is_dwp*/,
13150 1 /*search_cwd*/));
13151 if (abfd != NULL)
13152 return abfd;
13153
13154 /* Work around upstream bug 15652.
13155 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13156 [Whether that's a "bug" is debatable, but it is getting in our way.]
13157 We have no real idea where the dwp file is, because gdb's realpath-ing
13158 of the executable's path may have discarded the needed info.
13159 [IWBN if the dwp file name was recorded in the executable, akin to
13160 .gnu_debuglink, but that doesn't exist yet.]
13161 Strip the directory from FILE_NAME and search again. */
13162 if (*debug_file_directory != '\0')
13163 {
13164 /* Don't implicitly search the current directory here.
13165 If the user wants to search "." to handle this case,
13166 it must be added to debug-file-directory. */
13167 return try_open_dwop_file (per_objfile, lbasename (file_name),
13168 1 /*is_dwp*/,
13169 0 /*search_cwd*/);
13170 }
13171
13172 return NULL;
13173 }
13174
13175 /* Initialize the use of the DWP file for the current objfile.
13176 By convention the name of the DWP file is ${objfile}.dwp.
13177 The result is NULL if it can't be found. */
13178
13179 static std::unique_ptr<struct dwp_file>
13180 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
13181 {
13182 struct objfile *objfile = per_objfile->objfile;
13183
13184 /* Try to find first .dwp for the binary file before any symbolic links
13185 resolving. */
13186
13187 /* If the objfile is a debug file, find the name of the real binary
13188 file and get the name of dwp file from there. */
13189 std::string dwp_name;
13190 if (objfile->separate_debug_objfile_backlink != NULL)
13191 {
13192 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13193 const char *backlink_basename = lbasename (backlink->original_name);
13194
13195 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
13196 }
13197 else
13198 dwp_name = objfile->original_name;
13199
13200 dwp_name += ".dwp";
13201
13202 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
13203 if (dbfd == NULL
13204 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13205 {
13206 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
13207 dwp_name = objfile_name (objfile);
13208 dwp_name += ".dwp";
13209 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
13210 }
13211
13212 if (dbfd == NULL)
13213 {
13214 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13215
13216 return std::unique_ptr<dwp_file> ();
13217 }
13218
13219 const char *name = bfd_get_filename (dbfd.get ());
13220 std::unique_ptr<struct dwp_file> dwp_file
13221 (new struct dwp_file (name, std::move (dbfd)));
13222
13223 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
13224 dwp_file->elf_sections =
13225 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
13226 dwp_file->num_sections, asection *);
13227
13228 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13229 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13230 dwp_file.get ());
13231
13232 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
13233
13234 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
13235
13236 /* The DWP file version is stored in the hash table. Oh well. */
13237 if (dwp_file->cus && dwp_file->tus
13238 && dwp_file->cus->version != dwp_file->tus->version)
13239 {
13240 /* Technically speaking, we should try to limp along, but this is
13241 pretty bizarre. We use pulongest here because that's the established
13242 portability solution (e.g, we cannot use %u for uint32_t). */
13243 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13244 " TU version %s [in DWP file %s]"),
13245 pulongest (dwp_file->cus->version),
13246 pulongest (dwp_file->tus->version), dwp_name.c_str ());
13247 }
13248
13249 if (dwp_file->cus)
13250 dwp_file->version = dwp_file->cus->version;
13251 else if (dwp_file->tus)
13252 dwp_file->version = dwp_file->tus->version;
13253 else
13254 dwp_file->version = 2;
13255
13256 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13257 {
13258 if (dwp_file->version == 2)
13259 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13260 dwp_file.get ());
13261 else
13262 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13263 dwp_file.get ());
13264 }
13265
13266 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13267 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
13268
13269 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13270 dwarf_read_debug_printf (" %s CUs, %s TUs",
13271 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13272 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
13273
13274 return dwp_file;
13275 }
13276
13277 /* Wrapper around open_and_init_dwp_file, only open it once. */
13278
13279 static struct dwp_file *
13280 get_dwp_file (dwarf2_per_objfile *per_objfile)
13281 {
13282 if (!per_objfile->per_bfd->dwp_checked)
13283 {
13284 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13285 per_objfile->per_bfd->dwp_checked = 1;
13286 }
13287 return per_objfile->per_bfd->dwp_file.get ();
13288 }
13289
13290 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13291 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13292 or in the DWP file for the objfile, referenced by THIS_UNIT.
13293 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
13294 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13295
13296 This is called, for example, when wanting to read a variable with a
13297 complex location. Therefore we don't want to do file i/o for every call.
13298 Therefore we don't want to look for a DWO file on every call.
13299 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13300 then we check if we've already seen DWO_NAME, and only THEN do we check
13301 for a DWO file.
13302
13303 The result is a pointer to the dwo_unit object or NULL if we didn't find it
13304 (dwo_id mismatch or couldn't find the DWO/DWP file). */
13305
13306 static struct dwo_unit *
13307 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13308 ULONGEST signature, int is_debug_types)
13309 {
13310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13311 struct objfile *objfile = per_objfile->objfile;
13312 const char *kind = is_debug_types ? "TU" : "CU";
13313 void **dwo_file_slot;
13314 struct dwo_file *dwo_file;
13315 struct dwp_file *dwp_file;
13316
13317 /* First see if there's a DWP file.
13318 If we have a DWP file but didn't find the DWO inside it, don't
13319 look for the original DWO file. It makes gdb behave differently
13320 depending on whether one is debugging in the build tree. */
13321
13322 dwp_file = get_dwp_file (per_objfile);
13323 if (dwp_file != NULL)
13324 {
13325 const struct dwp_hash_table *dwp_htab =
13326 is_debug_types ? dwp_file->tus : dwp_file->cus;
13327
13328 if (dwp_htab != NULL)
13329 {
13330 struct dwo_unit *dwo_cutu =
13331 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13332 is_debug_types);
13333
13334 if (dwo_cutu != NULL)
13335 {
13336 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13337 kind, hex_string (signature),
13338 host_address_to_string (dwo_cutu));
13339
13340 return dwo_cutu;
13341 }
13342 }
13343 }
13344 else
13345 {
13346 /* No DWP file, look for the DWO file. */
13347
13348 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
13349 if (*dwo_file_slot == NULL)
13350 {
13351 /* Read in the file and build a table of the CUs/TUs it contains. */
13352 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
13353 }
13354 /* NOTE: This will be NULL if unable to open the file. */
13355 dwo_file = (struct dwo_file *) *dwo_file_slot;
13356
13357 if (dwo_file != NULL)
13358 {
13359 struct dwo_unit *dwo_cutu = NULL;
13360
13361 if (is_debug_types && dwo_file->tus)
13362 {
13363 struct dwo_unit find_dwo_cutu;
13364
13365 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13366 find_dwo_cutu.signature = signature;
13367 dwo_cutu
13368 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13369 &find_dwo_cutu);
13370 }
13371 else if (!is_debug_types && dwo_file->cus)
13372 {
13373 struct dwo_unit find_dwo_cutu;
13374
13375 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13376 find_dwo_cutu.signature = signature;
13377 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13378 &find_dwo_cutu);
13379 }
13380
13381 if (dwo_cutu != NULL)
13382 {
13383 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13384 kind, dwo_name, hex_string (signature),
13385 host_address_to_string (dwo_cutu));
13386
13387 return dwo_cutu;
13388 }
13389 }
13390 }
13391
13392 /* We didn't find it. This could mean a dwo_id mismatch, or
13393 someone deleted the DWO/DWP file, or the search path isn't set up
13394 correctly to find the file. */
13395
13396 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13397 kind, dwo_name, hex_string (signature));
13398
13399 /* This is a warning and not a complaint because it can be caused by
13400 pilot error (e.g., user accidentally deleting the DWO). */
13401 {
13402 /* Print the name of the DWP file if we looked there, helps the user
13403 better diagnose the problem. */
13404 std::string dwp_text;
13405
13406 if (dwp_file != NULL)
13407 dwp_text = string_printf (" [in DWP file %s]",
13408 lbasename (dwp_file->name));
13409
13410 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13411 " [in module %s]"),
13412 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13413 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13414 }
13415 return NULL;
13416 }
13417
13418 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13419 See lookup_dwo_cutu_unit for details. */
13420
13421 static struct dwo_unit *
13422 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13423 ULONGEST signature)
13424 {
13425 gdb_assert (!cu->per_cu->is_debug_types);
13426
13427 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13428 }
13429
13430 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13431 See lookup_dwo_cutu_unit for details. */
13432
13433 static struct dwo_unit *
13434 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13435 {
13436 gdb_assert (cu->per_cu->is_debug_types);
13437
13438 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13439
13440 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13441 }
13442
13443 /* Traversal function for queue_and_load_all_dwo_tus. */
13444
13445 static int
13446 queue_and_load_dwo_tu (void **slot, void *info)
13447 {
13448 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13449 dwarf2_cu *cu = (dwarf2_cu *) info;
13450 ULONGEST signature = dwo_unit->signature;
13451 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13452
13453 if (sig_type != NULL)
13454 {
13455 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13456
13457 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13458 a real dependency of PER_CU on SIG_TYPE. That is detected later
13459 while processing PER_CU. */
13460 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13461 load_full_type_unit (sig_cu, cu->per_objfile);
13462 cu->per_cu->imported_symtabs_push (sig_cu);
13463 }
13464
13465 return 1;
13466 }
13467
13468 /* Queue all TUs contained in the DWO of CU to be read in.
13469 The DWO may have the only definition of the type, though it may not be
13470 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13471 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13472
13473 static void
13474 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13475 {
13476 struct dwo_unit *dwo_unit;
13477 struct dwo_file *dwo_file;
13478
13479 gdb_assert (cu != nullptr);
13480 gdb_assert (!cu->per_cu->is_debug_types);
13481 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13482
13483 dwo_unit = cu->dwo_unit;
13484 gdb_assert (dwo_unit != NULL);
13485
13486 dwo_file = dwo_unit->dwo_file;
13487 if (dwo_file->tus != NULL)
13488 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13489 }
13490
13491 /* Read in various DIEs. */
13492
13493 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13494 Inherit only the children of the DW_AT_abstract_origin DIE not being
13495 already referenced by DW_AT_abstract_origin from the children of the
13496 current DIE. */
13497
13498 static void
13499 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13500 {
13501 struct die_info *child_die;
13502 sect_offset *offsetp;
13503 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13504 struct die_info *origin_die;
13505 /* Iterator of the ORIGIN_DIE children. */
13506 struct die_info *origin_child_die;
13507 struct attribute *attr;
13508 struct dwarf2_cu *origin_cu;
13509 struct pending **origin_previous_list_in_scope;
13510
13511 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13512 if (!attr)
13513 return;
13514
13515 /* Note that following die references may follow to a die in a
13516 different cu. */
13517
13518 origin_cu = cu;
13519 origin_die = follow_die_ref (die, attr, &origin_cu);
13520
13521 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13522 symbols in. */
13523 origin_previous_list_in_scope = origin_cu->list_in_scope;
13524 origin_cu->list_in_scope = cu->list_in_scope;
13525
13526 if (die->tag != origin_die->tag
13527 && !(die->tag == DW_TAG_inlined_subroutine
13528 && origin_die->tag == DW_TAG_subprogram))
13529 complaint (_("DIE %s and its abstract origin %s have different tags"),
13530 sect_offset_str (die->sect_off),
13531 sect_offset_str (origin_die->sect_off));
13532
13533 std::vector<sect_offset> offsets;
13534
13535 for (child_die = die->child;
13536 child_die && child_die->tag;
13537 child_die = child_die->sibling)
13538 {
13539 struct die_info *child_origin_die;
13540 struct dwarf2_cu *child_origin_cu;
13541
13542 /* We are trying to process concrete instance entries:
13543 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13544 it's not relevant to our analysis here. i.e. detecting DIEs that are
13545 present in the abstract instance but not referenced in the concrete
13546 one. */
13547 if (child_die->tag == DW_TAG_call_site
13548 || child_die->tag == DW_TAG_GNU_call_site)
13549 continue;
13550
13551 /* For each CHILD_DIE, find the corresponding child of
13552 ORIGIN_DIE. If there is more than one layer of
13553 DW_AT_abstract_origin, follow them all; there shouldn't be,
13554 but GCC versions at least through 4.4 generate this (GCC PR
13555 40573). */
13556 child_origin_die = child_die;
13557 child_origin_cu = cu;
13558 while (1)
13559 {
13560 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13561 child_origin_cu);
13562 if (attr == NULL)
13563 break;
13564 child_origin_die = follow_die_ref (child_origin_die, attr,
13565 &child_origin_cu);
13566 }
13567
13568 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13569 counterpart may exist. */
13570 if (child_origin_die != child_die)
13571 {
13572 if (child_die->tag != child_origin_die->tag
13573 && !(child_die->tag == DW_TAG_inlined_subroutine
13574 && child_origin_die->tag == DW_TAG_subprogram))
13575 complaint (_("Child DIE %s and its abstract origin %s have "
13576 "different tags"),
13577 sect_offset_str (child_die->sect_off),
13578 sect_offset_str (child_origin_die->sect_off));
13579 if (child_origin_die->parent != origin_die)
13580 complaint (_("Child DIE %s and its abstract origin %s have "
13581 "different parents"),
13582 sect_offset_str (child_die->sect_off),
13583 sect_offset_str (child_origin_die->sect_off));
13584 else
13585 offsets.push_back (child_origin_die->sect_off);
13586 }
13587 }
13588 std::sort (offsets.begin (), offsets.end ());
13589 sect_offset *offsets_end = offsets.data () + offsets.size ();
13590 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13591 if (offsetp[-1] == *offsetp)
13592 complaint (_("Multiple children of DIE %s refer "
13593 "to DIE %s as their abstract origin"),
13594 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13595
13596 offsetp = offsets.data ();
13597 origin_child_die = origin_die->child;
13598 while (origin_child_die && origin_child_die->tag)
13599 {
13600 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13601 while (offsetp < offsets_end
13602 && *offsetp < origin_child_die->sect_off)
13603 offsetp++;
13604 if (offsetp >= offsets_end
13605 || *offsetp > origin_child_die->sect_off)
13606 {
13607 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13608 Check whether we're already processing ORIGIN_CHILD_DIE.
13609 This can happen with mutually referenced abstract_origins.
13610 PR 16581. */
13611 if (!origin_child_die->in_process)
13612 process_die (origin_child_die, origin_cu);
13613 }
13614 origin_child_die = origin_child_die->sibling;
13615 }
13616 origin_cu->list_in_scope = origin_previous_list_in_scope;
13617
13618 if (cu != origin_cu)
13619 compute_delayed_physnames (origin_cu);
13620 }
13621
13622 static void
13623 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13624 {
13625 struct objfile *objfile = cu->per_objfile->objfile;
13626 struct gdbarch *gdbarch = objfile->arch ();
13627 struct context_stack *newobj;
13628 CORE_ADDR lowpc;
13629 CORE_ADDR highpc;
13630 struct die_info *child_die;
13631 struct attribute *attr, *call_line, *call_file;
13632 const char *name;
13633 CORE_ADDR baseaddr;
13634 struct block *block;
13635 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13636 std::vector<struct symbol *> template_args;
13637 struct template_symbol *templ_func = NULL;
13638
13639 if (inlined_func)
13640 {
13641 /* If we do not have call site information, we can't show the
13642 caller of this inlined function. That's too confusing, so
13643 only use the scope for local variables. */
13644 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13645 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13646 if (call_line == NULL || call_file == NULL)
13647 {
13648 read_lexical_block_scope (die, cu);
13649 return;
13650 }
13651 }
13652
13653 baseaddr = objfile->text_section_offset ();
13654
13655 name = dwarf2_name (die, cu);
13656
13657 /* Ignore functions with missing or empty names. These are actually
13658 illegal according to the DWARF standard. */
13659 if (name == NULL)
13660 {
13661 complaint (_("missing name for subprogram DIE at %s"),
13662 sect_offset_str (die->sect_off));
13663 return;
13664 }
13665
13666 /* Ignore functions with missing or invalid low and high pc attributes. */
13667 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13668 <= PC_BOUNDS_INVALID)
13669 {
13670 attr = dwarf2_attr (die, DW_AT_external, cu);
13671 if (attr == nullptr || !attr->as_boolean ())
13672 complaint (_("cannot get low and high bounds "
13673 "for subprogram DIE at %s"),
13674 sect_offset_str (die->sect_off));
13675 return;
13676 }
13677
13678 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13679 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13680
13681 /* If we have any template arguments, then we must allocate a
13682 different sort of symbol. */
13683 for (child_die = die->child; child_die; child_die = child_die->sibling)
13684 {
13685 if (child_die->tag == DW_TAG_template_type_param
13686 || child_die->tag == DW_TAG_template_value_param)
13687 {
13688 templ_func = new (&objfile->objfile_obstack) template_symbol;
13689 templ_func->subclass = SYMBOL_TEMPLATE;
13690 break;
13691 }
13692 }
13693
13694 newobj = cu->get_builder ()->push_context (0, lowpc);
13695 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13696 (struct symbol *) templ_func);
13697
13698 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13699 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13700 cu->language);
13701
13702 /* If there is a location expression for DW_AT_frame_base, record
13703 it. */
13704 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13705 if (attr != nullptr)
13706 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13707
13708 /* If there is a location for the static link, record it. */
13709 newobj->static_link = NULL;
13710 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13711 if (attr != nullptr)
13712 {
13713 newobj->static_link
13714 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13715 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13716 cu->addr_type ());
13717 }
13718
13719 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13720
13721 if (die->child != NULL)
13722 {
13723 child_die = die->child;
13724 while (child_die && child_die->tag)
13725 {
13726 if (child_die->tag == DW_TAG_template_type_param
13727 || child_die->tag == DW_TAG_template_value_param)
13728 {
13729 struct symbol *arg = new_symbol (child_die, NULL, cu);
13730
13731 if (arg != NULL)
13732 template_args.push_back (arg);
13733 }
13734 else
13735 process_die (child_die, cu);
13736 child_die = child_die->sibling;
13737 }
13738 }
13739
13740 inherit_abstract_dies (die, cu);
13741
13742 /* If we have a DW_AT_specification, we might need to import using
13743 directives from the context of the specification DIE. See the
13744 comment in determine_prefix. */
13745 if (cu->language == language_cplus
13746 && dwarf2_attr (die, DW_AT_specification, cu))
13747 {
13748 struct dwarf2_cu *spec_cu = cu;
13749 struct die_info *spec_die = die_specification (die, &spec_cu);
13750
13751 while (spec_die)
13752 {
13753 child_die = spec_die->child;
13754 while (child_die && child_die->tag)
13755 {
13756 if (child_die->tag == DW_TAG_imported_module)
13757 process_die (child_die, spec_cu);
13758 child_die = child_die->sibling;
13759 }
13760
13761 /* In some cases, GCC generates specification DIEs that
13762 themselves contain DW_AT_specification attributes. */
13763 spec_die = die_specification (spec_die, &spec_cu);
13764 }
13765 }
13766
13767 struct context_stack cstk = cu->get_builder ()->pop_context ();
13768 /* Make a block for the local symbols within. */
13769 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13770 cstk.static_link, lowpc, highpc);
13771
13772 /* For C++, set the block's scope. */
13773 if ((cu->language == language_cplus
13774 || cu->language == language_fortran
13775 || cu->language == language_d
13776 || cu->language == language_rust)
13777 && cu->processing_has_namespace_info)
13778 block_set_scope (block, determine_prefix (die, cu),
13779 &objfile->objfile_obstack);
13780
13781 /* If we have address ranges, record them. */
13782 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13783
13784 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13785
13786 /* Attach template arguments to function. */
13787 if (!template_args.empty ())
13788 {
13789 gdb_assert (templ_func != NULL);
13790
13791 templ_func->n_template_arguments = template_args.size ();
13792 templ_func->template_arguments
13793 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13794 templ_func->n_template_arguments);
13795 memcpy (templ_func->template_arguments,
13796 template_args.data (),
13797 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13798
13799 /* Make sure that the symtab is set on the new symbols. Even
13800 though they don't appear in this symtab directly, other parts
13801 of gdb assume that symbols do, and this is reasonably
13802 true. */
13803 for (symbol *sym : template_args)
13804 symbol_set_symtab (sym, symbol_symtab (templ_func));
13805 }
13806
13807 /* In C++, we can have functions nested inside functions (e.g., when
13808 a function declares a class that has methods). This means that
13809 when we finish processing a function scope, we may need to go
13810 back to building a containing block's symbol lists. */
13811 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13812 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13813
13814 /* If we've finished processing a top-level function, subsequent
13815 symbols go in the file symbol list. */
13816 if (cu->get_builder ()->outermost_context_p ())
13817 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13818 }
13819
13820 /* Process all the DIES contained within a lexical block scope. Start
13821 a new scope, process the dies, and then close the scope. */
13822
13823 static void
13824 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13825 {
13826 struct objfile *objfile = cu->per_objfile->objfile;
13827 struct gdbarch *gdbarch = objfile->arch ();
13828 CORE_ADDR lowpc, highpc;
13829 struct die_info *child_die;
13830 CORE_ADDR baseaddr;
13831
13832 baseaddr = objfile->text_section_offset ();
13833
13834 /* Ignore blocks with missing or invalid low and high pc attributes. */
13835 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13836 as multiple lexical blocks? Handling children in a sane way would
13837 be nasty. Might be easier to properly extend generic blocks to
13838 describe ranges. */
13839 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13840 {
13841 case PC_BOUNDS_NOT_PRESENT:
13842 /* DW_TAG_lexical_block has no attributes, process its children as if
13843 there was no wrapping by that DW_TAG_lexical_block.
13844 GCC does no longer produces such DWARF since GCC r224161. */
13845 for (child_die = die->child;
13846 child_die != NULL && child_die->tag;
13847 child_die = child_die->sibling)
13848 {
13849 /* We might already be processing this DIE. This can happen
13850 in an unusual circumstance -- where a subroutine A
13851 appears lexically in another subroutine B, but A actually
13852 inlines B. The recursion is broken here, rather than in
13853 inherit_abstract_dies, because it seems better to simply
13854 drop concrete children here. */
13855 if (!child_die->in_process)
13856 process_die (child_die, cu);
13857 }
13858 return;
13859 case PC_BOUNDS_INVALID:
13860 return;
13861 }
13862 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13863 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13864
13865 cu->get_builder ()->push_context (0, lowpc);
13866 if (die->child != NULL)
13867 {
13868 child_die = die->child;
13869 while (child_die && child_die->tag)
13870 {
13871 process_die (child_die, cu);
13872 child_die = child_die->sibling;
13873 }
13874 }
13875 inherit_abstract_dies (die, cu);
13876 struct context_stack cstk = cu->get_builder ()->pop_context ();
13877
13878 if (*cu->get_builder ()->get_local_symbols () != NULL
13879 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13880 {
13881 struct block *block
13882 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13883 cstk.start_addr, highpc);
13884
13885 /* Note that recording ranges after traversing children, as we
13886 do here, means that recording a parent's ranges entails
13887 walking across all its children's ranges as they appear in
13888 the address map, which is quadratic behavior.
13889
13890 It would be nicer to record the parent's ranges before
13891 traversing its children, simply overriding whatever you find
13892 there. But since we don't even decide whether to create a
13893 block until after we've traversed its children, that's hard
13894 to do. */
13895 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13896 }
13897 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13898 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13899 }
13900
13901 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13902
13903 static void
13904 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13905 {
13906 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13907 struct objfile *objfile = per_objfile->objfile;
13908 struct gdbarch *gdbarch = objfile->arch ();
13909 CORE_ADDR pc, baseaddr;
13910 struct attribute *attr;
13911 struct call_site *call_site, call_site_local;
13912 void **slot;
13913 int nparams;
13914 struct die_info *child_die;
13915
13916 baseaddr = objfile->text_section_offset ();
13917
13918 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13919 if (attr == NULL)
13920 {
13921 /* This was a pre-DWARF-5 GNU extension alias
13922 for DW_AT_call_return_pc. */
13923 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13924 }
13925 if (!attr)
13926 {
13927 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13928 "DIE %s [in module %s]"),
13929 sect_offset_str (die->sect_off), objfile_name (objfile));
13930 return;
13931 }
13932 pc = attr->as_address () + baseaddr;
13933 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13934
13935 if (cu->call_site_htab == NULL)
13936 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13937 NULL, &objfile->objfile_obstack,
13938 hashtab_obstack_allocate, NULL);
13939 call_site_local.pc = pc;
13940 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13941 if (*slot != NULL)
13942 {
13943 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13944 "DIE %s [in module %s]"),
13945 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13946 objfile_name (objfile));
13947 return;
13948 }
13949
13950 /* Count parameters at the caller. */
13951
13952 nparams = 0;
13953 for (child_die = die->child; child_die && child_die->tag;
13954 child_die = child_die->sibling)
13955 {
13956 if (child_die->tag != DW_TAG_call_site_parameter
13957 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13958 {
13959 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13960 "DW_TAG_call_site child DIE %s [in module %s]"),
13961 child_die->tag, sect_offset_str (child_die->sect_off),
13962 objfile_name (objfile));
13963 continue;
13964 }
13965
13966 nparams++;
13967 }
13968
13969 call_site
13970 = ((struct call_site *)
13971 obstack_alloc (&objfile->objfile_obstack,
13972 sizeof (*call_site)
13973 + (sizeof (*call_site->parameter) * (nparams - 1))));
13974 *slot = call_site;
13975 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13976 call_site->pc = pc;
13977
13978 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13979 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13980 {
13981 struct die_info *func_die;
13982
13983 /* Skip also over DW_TAG_inlined_subroutine. */
13984 for (func_die = die->parent;
13985 func_die && func_die->tag != DW_TAG_subprogram
13986 && func_die->tag != DW_TAG_subroutine_type;
13987 func_die = func_die->parent);
13988
13989 /* DW_AT_call_all_calls is a superset
13990 of DW_AT_call_all_tail_calls. */
13991 if (func_die
13992 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13993 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13994 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13995 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13996 {
13997 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13998 not complete. But keep CALL_SITE for look ups via call_site_htab,
13999 both the initial caller containing the real return address PC and
14000 the final callee containing the current PC of a chain of tail
14001 calls do not need to have the tail call list complete. But any
14002 function candidate for a virtual tail call frame searched via
14003 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14004 determined unambiguously. */
14005 }
14006 else
14007 {
14008 struct type *func_type = NULL;
14009
14010 if (func_die)
14011 func_type = get_die_type (func_die, cu);
14012 if (func_type != NULL)
14013 {
14014 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
14015
14016 /* Enlist this call site to the function. */
14017 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14018 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14019 }
14020 else
14021 complaint (_("Cannot find function owning DW_TAG_call_site "
14022 "DIE %s [in module %s]"),
14023 sect_offset_str (die->sect_off), objfile_name (objfile));
14024 }
14025 }
14026
14027 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14028 if (attr == NULL)
14029 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14030 if (attr == NULL)
14031 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
14032 if (attr == NULL)
14033 {
14034 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14035 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14036 }
14037 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14038 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
14039 /* Keep NULL DWARF_BLOCK. */;
14040 else if (attr->form_is_block ())
14041 {
14042 struct dwarf2_locexpr_baton *dlbaton;
14043 struct dwarf_block *block = attr->as_block ();
14044
14045 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
14046 dlbaton->data = block->data;
14047 dlbaton->size = block->size;
14048 dlbaton->per_objfile = per_objfile;
14049 dlbaton->per_cu = cu->per_cu;
14050
14051 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14052 }
14053 else if (attr->form_is_ref ())
14054 {
14055 struct dwarf2_cu *target_cu = cu;
14056 struct die_info *target_die;
14057
14058 target_die = follow_die_ref (die, attr, &target_cu);
14059 gdb_assert (target_cu->per_objfile->objfile == objfile);
14060 if (die_is_declaration (target_die, target_cu))
14061 {
14062 const char *target_physname;
14063
14064 /* Prefer the mangled name; otherwise compute the demangled one. */
14065 target_physname = dw2_linkage_name (target_die, target_cu);
14066 if (target_physname == NULL)
14067 target_physname = dwarf2_physname (NULL, target_die, target_cu);
14068 if (target_physname == NULL)
14069 complaint (_("DW_AT_call_target target DIE has invalid "
14070 "physname, for referencing DIE %s [in module %s]"),
14071 sect_offset_str (die->sect_off), objfile_name (objfile));
14072 else
14073 SET_FIELD_PHYSNAME (call_site->target, target_physname);
14074 }
14075 else
14076 {
14077 CORE_ADDR lowpc;
14078
14079 /* DW_AT_entry_pc should be preferred. */
14080 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
14081 <= PC_BOUNDS_INVALID)
14082 complaint (_("DW_AT_call_target target DIE has invalid "
14083 "low pc, for referencing DIE %s [in module %s]"),
14084 sect_offset_str (die->sect_off), objfile_name (objfile));
14085 else
14086 {
14087 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14088 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14089 }
14090 }
14091 }
14092 else
14093 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
14094 "block nor reference, for DIE %s [in module %s]"),
14095 sect_offset_str (die->sect_off), objfile_name (objfile));
14096
14097 call_site->per_cu = cu->per_cu;
14098 call_site->per_objfile = per_objfile;
14099
14100 for (child_die = die->child;
14101 child_die && child_die->tag;
14102 child_die = child_die->sibling)
14103 {
14104 struct call_site_parameter *parameter;
14105 struct attribute *loc, *origin;
14106
14107 if (child_die->tag != DW_TAG_call_site_parameter
14108 && child_die->tag != DW_TAG_GNU_call_site_parameter)
14109 {
14110 /* Already printed the complaint above. */
14111 continue;
14112 }
14113
14114 gdb_assert (call_site->parameter_count < nparams);
14115 parameter = &call_site->parameter[call_site->parameter_count];
14116
14117 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14118 specifies DW_TAG_formal_parameter. Value of the data assumed for the
14119 register is contained in DW_AT_call_value. */
14120
14121 loc = dwarf2_attr (child_die, DW_AT_location, cu);
14122 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14123 if (origin == NULL)
14124 {
14125 /* This was a pre-DWARF-5 GNU extension alias
14126 for DW_AT_call_parameter. */
14127 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14128 }
14129 if (loc == NULL && origin != NULL && origin->form_is_ref ())
14130 {
14131 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
14132
14133 sect_offset sect_off = origin->get_ref_die_offset ();
14134 if (!cu->header.offset_in_cu_p (sect_off))
14135 {
14136 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14137 binding can be done only inside one CU. Such referenced DIE
14138 therefore cannot be even moved to DW_TAG_partial_unit. */
14139 complaint (_("DW_AT_call_parameter offset is not in CU for "
14140 "DW_TAG_call_site child DIE %s [in module %s]"),
14141 sect_offset_str (child_die->sect_off),
14142 objfile_name (objfile));
14143 continue;
14144 }
14145 parameter->u.param_cu_off
14146 = (cu_offset) (sect_off - cu->header.sect_off);
14147 }
14148 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
14149 {
14150 complaint (_("No DW_FORM_block* DW_AT_location for "
14151 "DW_TAG_call_site child DIE %s [in module %s]"),
14152 sect_offset_str (child_die->sect_off), objfile_name (objfile));
14153 continue;
14154 }
14155 else
14156 {
14157 struct dwarf_block *block = loc->as_block ();
14158
14159 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14160 (block->data, &block->data[block->size]);
14161 if (parameter->u.dwarf_reg != -1)
14162 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14163 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14164 &block->data[block->size],
14165 &parameter->u.fb_offset))
14166 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14167 else
14168 {
14169 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
14170 "for DW_FORM_block* DW_AT_location is supported for "
14171 "DW_TAG_call_site child DIE %s "
14172 "[in module %s]"),
14173 sect_offset_str (child_die->sect_off),
14174 objfile_name (objfile));
14175 continue;
14176 }
14177 }
14178
14179 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14180 if (attr == NULL)
14181 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
14182 if (attr == NULL || !attr->form_is_block ())
14183 {
14184 complaint (_("No DW_FORM_block* DW_AT_call_value for "
14185 "DW_TAG_call_site child DIE %s [in module %s]"),
14186 sect_offset_str (child_die->sect_off),
14187 objfile_name (objfile));
14188 continue;
14189 }
14190
14191 struct dwarf_block *block = attr->as_block ();
14192 parameter->value = block->data;
14193 parameter->value_size = block->size;
14194
14195 /* Parameters are not pre-cleared by memset above. */
14196 parameter->data_value = NULL;
14197 parameter->data_value_size = 0;
14198 call_site->parameter_count++;
14199
14200 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14201 if (attr == NULL)
14202 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
14203 if (attr != nullptr)
14204 {
14205 if (!attr->form_is_block ())
14206 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
14207 "DW_TAG_call_site child DIE %s [in module %s]"),
14208 sect_offset_str (child_die->sect_off),
14209 objfile_name (objfile));
14210 else
14211 {
14212 block = attr->as_block ();
14213 parameter->data_value = block->data;
14214 parameter->data_value_size = block->size;
14215 }
14216 }
14217 }
14218 }
14219
14220 /* Helper function for read_variable. If DIE represents a virtual
14221 table, then return the type of the concrete object that is
14222 associated with the virtual table. Otherwise, return NULL. */
14223
14224 static struct type *
14225 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14226 {
14227 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14228 if (attr == NULL)
14229 return NULL;
14230
14231 /* Find the type DIE. */
14232 struct die_info *type_die = NULL;
14233 struct dwarf2_cu *type_cu = cu;
14234
14235 if (attr->form_is_ref ())
14236 type_die = follow_die_ref (die, attr, &type_cu);
14237 if (type_die == NULL)
14238 return NULL;
14239
14240 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14241 return NULL;
14242 return die_containing_type (type_die, type_cu);
14243 }
14244
14245 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14246
14247 static void
14248 read_variable (struct die_info *die, struct dwarf2_cu *cu)
14249 {
14250 struct rust_vtable_symbol *storage = NULL;
14251
14252 if (cu->language == language_rust)
14253 {
14254 struct type *containing_type = rust_containing_type (die, cu);
14255
14256 if (containing_type != NULL)
14257 {
14258 struct objfile *objfile = cu->per_objfile->objfile;
14259
14260 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
14261 storage->concrete_type = containing_type;
14262 storage->subclass = SYMBOL_RUST_VTABLE;
14263 }
14264 }
14265
14266 struct symbol *res = new_symbol (die, NULL, cu, storage);
14267 struct attribute *abstract_origin
14268 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14269 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14270 if (res == NULL && loc && abstract_origin)
14271 {
14272 /* We have a variable without a name, but with a location and an abstract
14273 origin. This may be a concrete instance of an abstract variable
14274 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14275 later. */
14276 struct dwarf2_cu *origin_cu = cu;
14277 struct die_info *origin_die
14278 = follow_die_ref (die, abstract_origin, &origin_cu);
14279 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14280 per_objfile->per_bfd->abstract_to_concrete
14281 [origin_die->sect_off].push_back (die->sect_off);
14282 }
14283 }
14284
14285 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14286 reading .debug_rnglists.
14287 Callback's type should be:
14288 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14289 Return true if the attributes are present and valid, otherwise,
14290 return false. */
14291
14292 template <typename Callback>
14293 static bool
14294 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14295 dwarf_tag tag, Callback &&callback)
14296 {
14297 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14298 struct objfile *objfile = per_objfile->objfile;
14299 bfd *obfd = objfile->obfd;
14300 /* Base address selection entry. */
14301 gdb::optional<CORE_ADDR> base;
14302 const gdb_byte *buffer;
14303 CORE_ADDR baseaddr;
14304 bool overflow = false;
14305 ULONGEST addr_index;
14306 struct dwarf2_section_info *rnglists_section;
14307
14308 base = cu->base_address;
14309 rnglists_section = cu_debug_rnglists_section (cu, tag);
14310 rnglists_section->read (objfile);
14311
14312 if (offset >= rnglists_section->size)
14313 {
14314 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14315 offset);
14316 return false;
14317 }
14318 buffer = rnglists_section->buffer + offset;
14319
14320 baseaddr = objfile->text_section_offset ();
14321
14322 while (1)
14323 {
14324 /* Initialize it due to a false compiler warning. */
14325 CORE_ADDR range_beginning = 0, range_end = 0;
14326 const gdb_byte *buf_end = (rnglists_section->buffer
14327 + rnglists_section->size);
14328 unsigned int bytes_read;
14329
14330 if (buffer == buf_end)
14331 {
14332 overflow = true;
14333 break;
14334 }
14335 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14336 switch (rlet)
14337 {
14338 case DW_RLE_end_of_list:
14339 break;
14340 case DW_RLE_base_address:
14341 if (buffer + cu->header.addr_size > buf_end)
14342 {
14343 overflow = true;
14344 break;
14345 }
14346 base = cu->header.read_address (obfd, buffer, &bytes_read);
14347 buffer += bytes_read;
14348 break;
14349 case DW_RLE_base_addressx:
14350 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14351 buffer += bytes_read;
14352 base = read_addr_index (cu, addr_index);
14353 break;
14354 case DW_RLE_start_length:
14355 if (buffer + cu->header.addr_size > buf_end)
14356 {
14357 overflow = true;
14358 break;
14359 }
14360 range_beginning = cu->header.read_address (obfd, buffer,
14361 &bytes_read);
14362 buffer += bytes_read;
14363 range_end = (range_beginning
14364 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14365 buffer += bytes_read;
14366 if (buffer > buf_end)
14367 {
14368 overflow = true;
14369 break;
14370 }
14371 break;
14372 case DW_RLE_startx_length:
14373 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14374 buffer += bytes_read;
14375 range_beginning = read_addr_index (cu, addr_index);
14376 if (buffer > buf_end)
14377 {
14378 overflow = true;
14379 break;
14380 }
14381 range_end = (range_beginning
14382 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14383 buffer += bytes_read;
14384 break;
14385 case DW_RLE_offset_pair:
14386 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14387 buffer += bytes_read;
14388 if (buffer > buf_end)
14389 {
14390 overflow = true;
14391 break;
14392 }
14393 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14394 buffer += bytes_read;
14395 if (buffer > buf_end)
14396 {
14397 overflow = true;
14398 break;
14399 }
14400 break;
14401 case DW_RLE_start_end:
14402 if (buffer + 2 * cu->header.addr_size > buf_end)
14403 {
14404 overflow = true;
14405 break;
14406 }
14407 range_beginning = cu->header.read_address (obfd, buffer,
14408 &bytes_read);
14409 buffer += bytes_read;
14410 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14411 buffer += bytes_read;
14412 break;
14413 case DW_RLE_startx_endx:
14414 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 range_beginning = read_addr_index (cu, addr_index);
14417 if (buffer > buf_end)
14418 {
14419 overflow = true;
14420 break;
14421 }
14422 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14423 buffer += bytes_read;
14424 range_end = read_addr_index (cu, addr_index);
14425 break;
14426 default:
14427 complaint (_("Invalid .debug_rnglists data (no base address)"));
14428 return false;
14429 }
14430 if (rlet == DW_RLE_end_of_list || overflow)
14431 break;
14432 if (rlet == DW_RLE_base_address)
14433 continue;
14434
14435 if (range_beginning > range_end)
14436 {
14437 /* Inverted range entries are invalid. */
14438 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14439 return false;
14440 }
14441
14442 /* Empty range entries have no effect. */
14443 if (range_beginning == range_end)
14444 continue;
14445
14446 /* Only DW_RLE_offset_pair needs the base address added. */
14447 if (rlet == DW_RLE_offset_pair)
14448 {
14449 if (!base.has_value ())
14450 {
14451 /* We have no valid base address for the DW_RLE_offset_pair. */
14452 complaint (_("Invalid .debug_rnglists data (no base address for "
14453 "DW_RLE_offset_pair)"));
14454 return false;
14455 }
14456
14457 range_beginning += *base;
14458 range_end += *base;
14459 }
14460
14461 /* A not-uncommon case of bad debug info.
14462 Don't pollute the addrmap with bad data. */
14463 if (range_beginning + baseaddr == 0
14464 && !per_objfile->per_bfd->has_section_at_zero)
14465 {
14466 complaint (_(".debug_rnglists entry has start address of zero"
14467 " [in module %s]"), objfile_name (objfile));
14468 continue;
14469 }
14470
14471 callback (range_beginning, range_end);
14472 }
14473
14474 if (overflow)
14475 {
14476 complaint (_("Offset %d is not terminated "
14477 "for DW_AT_ranges attribute"),
14478 offset);
14479 return false;
14480 }
14481
14482 return true;
14483 }
14484
14485 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14486 Callback's type should be:
14487 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14488 Return 1 if the attributes are present and valid, otherwise, return 0. */
14489
14490 template <typename Callback>
14491 static int
14492 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14493 Callback &&callback)
14494 {
14495 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14496 struct objfile *objfile = per_objfile->objfile;
14497 struct comp_unit_head *cu_header = &cu->header;
14498 bfd *obfd = objfile->obfd;
14499 unsigned int addr_size = cu_header->addr_size;
14500 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14501 /* Base address selection entry. */
14502 gdb::optional<CORE_ADDR> base;
14503 unsigned int dummy;
14504 const gdb_byte *buffer;
14505 CORE_ADDR baseaddr;
14506
14507 if (cu_header->version >= 5)
14508 return dwarf2_rnglists_process (offset, cu, tag, callback);
14509
14510 base = cu->base_address;
14511
14512 per_objfile->per_bfd->ranges.read (objfile);
14513 if (offset >= per_objfile->per_bfd->ranges.size)
14514 {
14515 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14516 offset);
14517 return 0;
14518 }
14519 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14520
14521 baseaddr = objfile->text_section_offset ();
14522
14523 while (1)
14524 {
14525 CORE_ADDR range_beginning, range_end;
14526
14527 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14528 buffer += addr_size;
14529 range_end = cu->header.read_address (obfd, buffer, &dummy);
14530 buffer += addr_size;
14531 offset += 2 * addr_size;
14532
14533 /* An end of list marker is a pair of zero addresses. */
14534 if (range_beginning == 0 && range_end == 0)
14535 /* Found the end of list entry. */
14536 break;
14537
14538 /* Each base address selection entry is a pair of 2 values.
14539 The first is the largest possible address, the second is
14540 the base address. Check for a base address here. */
14541 if ((range_beginning & mask) == mask)
14542 {
14543 /* If we found the largest possible address, then we already
14544 have the base address in range_end. */
14545 base = range_end;
14546 continue;
14547 }
14548
14549 if (!base.has_value ())
14550 {
14551 /* We have no valid base address for the ranges
14552 data. */
14553 complaint (_("Invalid .debug_ranges data (no base address)"));
14554 return 0;
14555 }
14556
14557 if (range_beginning > range_end)
14558 {
14559 /* Inverted range entries are invalid. */
14560 complaint (_("Invalid .debug_ranges data (inverted range)"));
14561 return 0;
14562 }
14563
14564 /* Empty range entries have no effect. */
14565 if (range_beginning == range_end)
14566 continue;
14567
14568 range_beginning += *base;
14569 range_end += *base;
14570
14571 /* A not-uncommon case of bad debug info.
14572 Don't pollute the addrmap with bad data. */
14573 if (range_beginning + baseaddr == 0
14574 && !per_objfile->per_bfd->has_section_at_zero)
14575 {
14576 complaint (_(".debug_ranges entry has start address of zero"
14577 " [in module %s]"), objfile_name (objfile));
14578 continue;
14579 }
14580
14581 callback (range_beginning, range_end);
14582 }
14583
14584 return 1;
14585 }
14586
14587 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14588 Return 1 if the attributes are present and valid, otherwise, return 0.
14589 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14590
14591 static int
14592 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14593 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14594 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14595 {
14596 struct objfile *objfile = cu->per_objfile->objfile;
14597 struct gdbarch *gdbarch = objfile->arch ();
14598 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14599 int low_set = 0;
14600 CORE_ADDR low = 0;
14601 CORE_ADDR high = 0;
14602 int retval;
14603
14604 retval = dwarf2_ranges_process (offset, cu, tag,
14605 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14606 {
14607 if (ranges_pst != NULL)
14608 {
14609 CORE_ADDR lowpc;
14610 CORE_ADDR highpc;
14611
14612 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14613 range_beginning + baseaddr)
14614 - baseaddr);
14615 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14616 range_end + baseaddr)
14617 - baseaddr);
14618 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14619 lowpc, highpc - 1, ranges_pst);
14620 }
14621
14622 /* FIXME: This is recording everything as a low-high
14623 segment of consecutive addresses. We should have a
14624 data structure for discontiguous block ranges
14625 instead. */
14626 if (! low_set)
14627 {
14628 low = range_beginning;
14629 high = range_end;
14630 low_set = 1;
14631 }
14632 else
14633 {
14634 if (range_beginning < low)
14635 low = range_beginning;
14636 if (range_end > high)
14637 high = range_end;
14638 }
14639 });
14640 if (!retval)
14641 return 0;
14642
14643 if (! low_set)
14644 /* If the first entry is an end-of-list marker, the range
14645 describes an empty scope, i.e. no instructions. */
14646 return 0;
14647
14648 if (low_return)
14649 *low_return = low;
14650 if (high_return)
14651 *high_return = high;
14652 return 1;
14653 }
14654
14655 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14656 definition for the return value. *LOWPC and *HIGHPC are set iff
14657 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14658
14659 static enum pc_bounds_kind
14660 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14661 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14662 dwarf2_psymtab *pst)
14663 {
14664 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14665 struct attribute *attr;
14666 struct attribute *attr_high;
14667 CORE_ADDR low = 0;
14668 CORE_ADDR high = 0;
14669 enum pc_bounds_kind ret;
14670
14671 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14672 if (attr_high)
14673 {
14674 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14675 if (attr != nullptr)
14676 {
14677 low = attr->as_address ();
14678 high = attr_high->as_address ();
14679 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14680 high += low;
14681 }
14682 else
14683 /* Found high w/o low attribute. */
14684 return PC_BOUNDS_INVALID;
14685
14686 /* Found consecutive range of addresses. */
14687 ret = PC_BOUNDS_HIGH_LOW;
14688 }
14689 else
14690 {
14691 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14692 if (attr != nullptr && attr->form_is_unsigned ())
14693 {
14694 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14695 on DWARF version). */
14696 ULONGEST ranges_offset = attr->as_unsigned ();
14697
14698 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14699 this value. */
14700 if (die->tag != DW_TAG_compile_unit)
14701 ranges_offset += cu->gnu_ranges_base;
14702
14703 /* Value of the DW_AT_ranges attribute is the offset in the
14704 .debug_ranges section. */
14705 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14706 die->tag))
14707 return PC_BOUNDS_INVALID;
14708 /* Found discontinuous range of addresses. */
14709 ret = PC_BOUNDS_RANGES;
14710 }
14711 else
14712 return PC_BOUNDS_NOT_PRESENT;
14713 }
14714
14715 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14716 if (high <= low)
14717 return PC_BOUNDS_INVALID;
14718
14719 /* When using the GNU linker, .gnu.linkonce. sections are used to
14720 eliminate duplicate copies of functions and vtables and such.
14721 The linker will arbitrarily choose one and discard the others.
14722 The AT_*_pc values for such functions refer to local labels in
14723 these sections. If the section from that file was discarded, the
14724 labels are not in the output, so the relocs get a value of 0.
14725 If this is a discarded function, mark the pc bounds as invalid,
14726 so that GDB will ignore it. */
14727 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14728 return PC_BOUNDS_INVALID;
14729
14730 *lowpc = low;
14731 if (highpc)
14732 *highpc = high;
14733 return ret;
14734 }
14735
14736 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14737 its low and high PC addresses. Do nothing if these addresses could not
14738 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14739 and HIGHPC to the high address if greater than HIGHPC. */
14740
14741 static void
14742 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14743 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14744 struct dwarf2_cu *cu)
14745 {
14746 CORE_ADDR low, high;
14747 struct die_info *child = die->child;
14748
14749 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14750 {
14751 *lowpc = std::min (*lowpc, low);
14752 *highpc = std::max (*highpc, high);
14753 }
14754
14755 /* If the language does not allow nested subprograms (either inside
14756 subprograms or lexical blocks), we're done. */
14757 if (cu->language != language_ada)
14758 return;
14759
14760 /* Check all the children of the given DIE. If it contains nested
14761 subprograms, then check their pc bounds. Likewise, we need to
14762 check lexical blocks as well, as they may also contain subprogram
14763 definitions. */
14764 while (child && child->tag)
14765 {
14766 if (child->tag == DW_TAG_subprogram
14767 || child->tag == DW_TAG_lexical_block)
14768 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14769 child = child->sibling;
14770 }
14771 }
14772
14773 /* Get the low and high pc's represented by the scope DIE, and store
14774 them in *LOWPC and *HIGHPC. If the correct values can't be
14775 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14776
14777 static void
14778 get_scope_pc_bounds (struct die_info *die,
14779 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14780 struct dwarf2_cu *cu)
14781 {
14782 CORE_ADDR best_low = (CORE_ADDR) -1;
14783 CORE_ADDR best_high = (CORE_ADDR) 0;
14784 CORE_ADDR current_low, current_high;
14785
14786 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14787 >= PC_BOUNDS_RANGES)
14788 {
14789 best_low = current_low;
14790 best_high = current_high;
14791 }
14792 else
14793 {
14794 struct die_info *child = die->child;
14795
14796 while (child && child->tag)
14797 {
14798 switch (child->tag) {
14799 case DW_TAG_subprogram:
14800 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14801 break;
14802 case DW_TAG_namespace:
14803 case DW_TAG_module:
14804 /* FIXME: carlton/2004-01-16: Should we do this for
14805 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14806 that current GCC's always emit the DIEs corresponding
14807 to definitions of methods of classes as children of a
14808 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14809 the DIEs giving the declarations, which could be
14810 anywhere). But I don't see any reason why the
14811 standards says that they have to be there. */
14812 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14813
14814 if (current_low != ((CORE_ADDR) -1))
14815 {
14816 best_low = std::min (best_low, current_low);
14817 best_high = std::max (best_high, current_high);
14818 }
14819 break;
14820 default:
14821 /* Ignore. */
14822 break;
14823 }
14824
14825 child = child->sibling;
14826 }
14827 }
14828
14829 *lowpc = best_low;
14830 *highpc = best_high;
14831 }
14832
14833 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14834 in DIE. */
14835
14836 static void
14837 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14838 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14839 {
14840 struct objfile *objfile = cu->per_objfile->objfile;
14841 struct gdbarch *gdbarch = objfile->arch ();
14842 struct attribute *attr;
14843 struct attribute *attr_high;
14844
14845 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14846 if (attr_high)
14847 {
14848 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14849 if (attr != nullptr)
14850 {
14851 CORE_ADDR low = attr->as_address ();
14852 CORE_ADDR high = attr_high->as_address ();
14853
14854 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14855 high += low;
14856
14857 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14858 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14859 cu->get_builder ()->record_block_range (block, low, high - 1);
14860 }
14861 }
14862
14863 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14864 if (attr != nullptr && attr->form_is_unsigned ())
14865 {
14866 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14867 on DWARF version). */
14868 ULONGEST ranges_offset = attr->as_unsigned ();
14869
14870 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14871 this value. */
14872 if (die->tag != DW_TAG_compile_unit)
14873 ranges_offset += cu->gnu_ranges_base;
14874
14875 std::vector<blockrange> blockvec;
14876 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14877 [&] (CORE_ADDR start, CORE_ADDR end)
14878 {
14879 start += baseaddr;
14880 end += baseaddr;
14881 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14882 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14883 cu->get_builder ()->record_block_range (block, start, end - 1);
14884 blockvec.emplace_back (start, end);
14885 });
14886
14887 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14888 }
14889 }
14890
14891 /* Check whether the producer field indicates either of GCC < 4.6, or the
14892 Intel C/C++ compiler, and cache the result in CU. */
14893
14894 static void
14895 check_producer (struct dwarf2_cu *cu)
14896 {
14897 int major, minor;
14898
14899 if (cu->producer == NULL)
14900 {
14901 /* For unknown compilers expect their behavior is DWARF version
14902 compliant.
14903
14904 GCC started to support .debug_types sections by -gdwarf-4 since
14905 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14906 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14907 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14908 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14909 }
14910 else if (producer_is_gcc (cu->producer, &major, &minor))
14911 {
14912 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14913 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14914 }
14915 else if (producer_is_icc (cu->producer, &major, &minor))
14916 {
14917 cu->producer_is_icc = true;
14918 cu->producer_is_icc_lt_14 = major < 14;
14919 }
14920 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14921 cu->producer_is_codewarrior = true;
14922 else
14923 {
14924 /* For other non-GCC compilers, expect their behavior is DWARF version
14925 compliant. */
14926 }
14927
14928 cu->checked_producer = true;
14929 }
14930
14931 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14932 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14933 during 4.6.0 experimental. */
14934
14935 static bool
14936 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14937 {
14938 if (!cu->checked_producer)
14939 check_producer (cu);
14940
14941 return cu->producer_is_gxx_lt_4_6;
14942 }
14943
14944
14945 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14946 with incorrect is_stmt attributes. */
14947
14948 static bool
14949 producer_is_codewarrior (struct dwarf2_cu *cu)
14950 {
14951 if (!cu->checked_producer)
14952 check_producer (cu);
14953
14954 return cu->producer_is_codewarrior;
14955 }
14956
14957 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14958 If that attribute is not available, return the appropriate
14959 default. */
14960
14961 static enum dwarf_access_attribute
14962 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14963 {
14964 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14965 if (attr != nullptr)
14966 {
14967 LONGEST value = attr->constant_value (-1);
14968 if (value == DW_ACCESS_public
14969 || value == DW_ACCESS_protected
14970 || value == DW_ACCESS_private)
14971 return (dwarf_access_attribute) value;
14972 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14973 plongest (value));
14974 }
14975
14976 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14977 {
14978 /* The default DWARF 2 accessibility for members is public, the default
14979 accessibility for inheritance is private. */
14980
14981 if (die->tag != DW_TAG_inheritance)
14982 return DW_ACCESS_public;
14983 else
14984 return DW_ACCESS_private;
14985 }
14986 else
14987 {
14988 /* DWARF 3+ defines the default accessibility a different way. The same
14989 rules apply now for DW_TAG_inheritance as for the members and it only
14990 depends on the container kind. */
14991
14992 if (die->parent->tag == DW_TAG_class_type)
14993 return DW_ACCESS_private;
14994 else
14995 return DW_ACCESS_public;
14996 }
14997 }
14998
14999 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
15000 offset. If the attribute was not found return 0, otherwise return
15001 1. If it was found but could not properly be handled, set *OFFSET
15002 to 0. */
15003
15004 static int
15005 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15006 LONGEST *offset)
15007 {
15008 struct attribute *attr;
15009
15010 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15011 if (attr != NULL)
15012 {
15013 *offset = 0;
15014
15015 /* Note that we do not check for a section offset first here.
15016 This is because DW_AT_data_member_location is new in DWARF 4,
15017 so if we see it, we can assume that a constant form is really
15018 a constant and not a section offset. */
15019 if (attr->form_is_constant ())
15020 *offset = attr->constant_value (0);
15021 else if (attr->form_is_section_offset ())
15022 dwarf2_complex_location_expr_complaint ();
15023 else if (attr->form_is_block ())
15024 *offset = decode_locdesc (attr->as_block (), cu);
15025 else
15026 dwarf2_complex_location_expr_complaint ();
15027
15028 return 1;
15029 }
15030
15031 return 0;
15032 }
15033
15034 /* Look for DW_AT_data_member_location and store the results in FIELD. */
15035
15036 static void
15037 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15038 struct field *field)
15039 {
15040 struct attribute *attr;
15041
15042 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15043 if (attr != NULL)
15044 {
15045 if (attr->form_is_constant ())
15046 {
15047 LONGEST offset = attr->constant_value (0);
15048 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15049 }
15050 else if (attr->form_is_section_offset ())
15051 dwarf2_complex_location_expr_complaint ();
15052 else if (attr->form_is_block ())
15053 {
15054 bool handled;
15055 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
15056 if (handled)
15057 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15058 else
15059 {
15060 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15061 struct objfile *objfile = per_objfile->objfile;
15062 struct dwarf2_locexpr_baton *dlbaton
15063 = XOBNEW (&objfile->objfile_obstack,
15064 struct dwarf2_locexpr_baton);
15065 dlbaton->data = attr->as_block ()->data;
15066 dlbaton->size = attr->as_block ()->size;
15067 /* When using this baton, we want to compute the address
15068 of the field, not the value. This is why
15069 is_reference is set to false here. */
15070 dlbaton->is_reference = false;
15071 dlbaton->per_objfile = per_objfile;
15072 dlbaton->per_cu = cu->per_cu;
15073
15074 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15075 }
15076 }
15077 else
15078 dwarf2_complex_location_expr_complaint ();
15079 }
15080 }
15081
15082 /* Add an aggregate field to the field list. */
15083
15084 static void
15085 dwarf2_add_field (struct field_info *fip, struct die_info *die,
15086 struct dwarf2_cu *cu)
15087 {
15088 struct objfile *objfile = cu->per_objfile->objfile;
15089 struct gdbarch *gdbarch = objfile->arch ();
15090 struct nextfield *new_field;
15091 struct attribute *attr;
15092 struct field *fp;
15093 const char *fieldname = "";
15094
15095 if (die->tag == DW_TAG_inheritance)
15096 {
15097 fip->baseclasses.emplace_back ();
15098 new_field = &fip->baseclasses.back ();
15099 }
15100 else
15101 {
15102 fip->fields.emplace_back ();
15103 new_field = &fip->fields.back ();
15104 }
15105
15106 new_field->offset = die->sect_off;
15107
15108 new_field->accessibility = dwarf2_access_attribute (die, cu);
15109 if (new_field->accessibility != DW_ACCESS_public)
15110 fip->non_public_fields = true;
15111
15112 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15113 if (attr != nullptr)
15114 new_field->virtuality = attr->as_virtuality ();
15115 else
15116 new_field->virtuality = DW_VIRTUALITY_none;
15117
15118 fp = &new_field->field;
15119
15120 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
15121 {
15122 /* Data member other than a C++ static data member. */
15123
15124 /* Get type of field. */
15125 fp->set_type (die_type (die, cu));
15126
15127 SET_FIELD_BITPOS (*fp, 0);
15128
15129 /* Get bit size of field (zero if none). */
15130 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
15131 if (attr != nullptr)
15132 {
15133 FIELD_BITSIZE (*fp) = attr->constant_value (0);
15134 }
15135 else
15136 {
15137 FIELD_BITSIZE (*fp) = 0;
15138 }
15139
15140 /* Get bit offset of field. */
15141 handle_data_member_location (die, cu, fp);
15142 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
15143 if (attr != nullptr && attr->form_is_constant ())
15144 {
15145 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
15146 {
15147 /* For big endian bits, the DW_AT_bit_offset gives the
15148 additional bit offset from the MSB of the containing
15149 anonymous object to the MSB of the field. We don't
15150 have to do anything special since we don't need to
15151 know the size of the anonymous object. */
15152 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15153 + attr->constant_value (0)));
15154 }
15155 else
15156 {
15157 /* For little endian bits, compute the bit offset to the
15158 MSB of the anonymous object, subtract off the number of
15159 bits from the MSB of the field to the MSB of the
15160 object, and then subtract off the number of bits of
15161 the field itself. The result is the bit offset of
15162 the LSB of the field. */
15163 int anonymous_size;
15164 int bit_offset = attr->constant_value (0);
15165
15166 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15167 if (attr != nullptr && attr->form_is_constant ())
15168 {
15169 /* The size of the anonymous object containing
15170 the bit field is explicit, so use the
15171 indicated size (in bytes). */
15172 anonymous_size = attr->constant_value (0);
15173 }
15174 else
15175 {
15176 /* The size of the anonymous object containing
15177 the bit field must be inferred from the type
15178 attribute of the data member containing the
15179 bit field. */
15180 anonymous_size = TYPE_LENGTH (fp->type ());
15181 }
15182 SET_FIELD_BITPOS (*fp,
15183 (FIELD_BITPOS (*fp)
15184 + anonymous_size * bits_per_byte
15185 - bit_offset - FIELD_BITSIZE (*fp)));
15186 }
15187 }
15188 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15189 if (attr != NULL)
15190 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15191 + attr->constant_value (0)));
15192
15193 /* Get name of field. */
15194 fieldname = dwarf2_name (die, cu);
15195 if (fieldname == NULL)
15196 fieldname = "";
15197
15198 /* The name is already allocated along with this objfile, so we don't
15199 need to duplicate it for the type. */
15200 fp->name = fieldname;
15201
15202 /* Change accessibility for artificial fields (e.g. virtual table
15203 pointer or virtual base class pointer) to private. */
15204 if (dwarf2_attr (die, DW_AT_artificial, cu))
15205 {
15206 FIELD_ARTIFICIAL (*fp) = 1;
15207 new_field->accessibility = DW_ACCESS_private;
15208 fip->non_public_fields = true;
15209 }
15210 }
15211 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
15212 {
15213 /* C++ static member. */
15214
15215 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15216 is a declaration, but all versions of G++ as of this writing
15217 (so through at least 3.2.1) incorrectly generate
15218 DW_TAG_variable tags. */
15219
15220 const char *physname;
15221
15222 /* Get name of field. */
15223 fieldname = dwarf2_name (die, cu);
15224 if (fieldname == NULL)
15225 return;
15226
15227 attr = dwarf2_attr (die, DW_AT_const_value, cu);
15228 if (attr
15229 /* Only create a symbol if this is an external value.
15230 new_symbol checks this and puts the value in the global symbol
15231 table, which we want. If it is not external, new_symbol
15232 will try to put the value in cu->list_in_scope which is wrong. */
15233 && dwarf2_flag_true_p (die, DW_AT_external, cu))
15234 {
15235 /* A static const member, not much different than an enum as far as
15236 we're concerned, except that we can support more types. */
15237 new_symbol (die, NULL, cu);
15238 }
15239
15240 /* Get physical name. */
15241 physname = dwarf2_physname (fieldname, die, cu);
15242
15243 /* The name is already allocated along with this objfile, so we don't
15244 need to duplicate it for the type. */
15245 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
15246 fp->set_type (die_type (die, cu));
15247 FIELD_NAME (*fp) = fieldname;
15248 }
15249 else if (die->tag == DW_TAG_inheritance)
15250 {
15251 /* C++ base class field. */
15252 handle_data_member_location (die, cu, fp);
15253 FIELD_BITSIZE (*fp) = 0;
15254 fp->set_type (die_type (die, cu));
15255 FIELD_NAME (*fp) = fp->type ()->name ();
15256 }
15257 else
15258 gdb_assert_not_reached ("missing case in dwarf2_add_field");
15259 }
15260
15261 /* Can the type given by DIE define another type? */
15262
15263 static bool
15264 type_can_define_types (const struct die_info *die)
15265 {
15266 switch (die->tag)
15267 {
15268 case DW_TAG_typedef:
15269 case DW_TAG_class_type:
15270 case DW_TAG_structure_type:
15271 case DW_TAG_union_type:
15272 case DW_TAG_enumeration_type:
15273 return true;
15274
15275 default:
15276 return false;
15277 }
15278 }
15279
15280 /* Add a type definition defined in the scope of the FIP's class. */
15281
15282 static void
15283 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15284 struct dwarf2_cu *cu)
15285 {
15286 struct decl_field fp;
15287 memset (&fp, 0, sizeof (fp));
15288
15289 gdb_assert (type_can_define_types (die));
15290
15291 /* Get name of field. NULL is okay here, meaning an anonymous type. */
15292 fp.name = dwarf2_name (die, cu);
15293 fp.type = read_type_die (die, cu);
15294
15295 /* Save accessibility. */
15296 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15297 switch (accessibility)
15298 {
15299 case DW_ACCESS_public:
15300 /* The assumed value if neither private nor protected. */
15301 break;
15302 case DW_ACCESS_private:
15303 fp.is_private = 1;
15304 break;
15305 case DW_ACCESS_protected:
15306 fp.is_protected = 1;
15307 break;
15308 }
15309
15310 if (die->tag == DW_TAG_typedef)
15311 fip->typedef_field_list.push_back (fp);
15312 else
15313 fip->nested_types_list.push_back (fp);
15314 }
15315
15316 /* A convenience typedef that's used when finding the discriminant
15317 field for a variant part. */
15318 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15319 offset_map_type;
15320
15321 /* Compute the discriminant range for a given variant. OBSTACK is
15322 where the results will be stored. VARIANT is the variant to
15323 process. IS_UNSIGNED indicates whether the discriminant is signed
15324 or unsigned. */
15325
15326 static const gdb::array_view<discriminant_range>
15327 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15328 bool is_unsigned)
15329 {
15330 std::vector<discriminant_range> ranges;
15331
15332 if (variant.default_branch)
15333 return {};
15334
15335 if (variant.discr_list_data == nullptr)
15336 {
15337 discriminant_range r
15338 = {variant.discriminant_value, variant.discriminant_value};
15339 ranges.push_back (r);
15340 }
15341 else
15342 {
15343 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15344 variant.discr_list_data->size);
15345 while (!data.empty ())
15346 {
15347 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15348 {
15349 complaint (_("invalid discriminant marker: %d"), data[0]);
15350 break;
15351 }
15352 bool is_range = data[0] == DW_DSC_range;
15353 data = data.slice (1);
15354
15355 ULONGEST low, high;
15356 unsigned int bytes_read;
15357
15358 if (data.empty ())
15359 {
15360 complaint (_("DW_AT_discr_list missing low value"));
15361 break;
15362 }
15363 if (is_unsigned)
15364 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15365 else
15366 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15367 &bytes_read);
15368 data = data.slice (bytes_read);
15369
15370 if (is_range)
15371 {
15372 if (data.empty ())
15373 {
15374 complaint (_("DW_AT_discr_list missing high value"));
15375 break;
15376 }
15377 if (is_unsigned)
15378 high = read_unsigned_leb128 (nullptr, data.data (),
15379 &bytes_read);
15380 else
15381 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15382 &bytes_read);
15383 data = data.slice (bytes_read);
15384 }
15385 else
15386 high = low;
15387
15388 ranges.push_back ({ low, high });
15389 }
15390 }
15391
15392 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15393 ranges.size ());
15394 std::copy (ranges.begin (), ranges.end (), result);
15395 return gdb::array_view<discriminant_range> (result, ranges.size ());
15396 }
15397
15398 static const gdb::array_view<variant_part> create_variant_parts
15399 (struct obstack *obstack,
15400 const offset_map_type &offset_map,
15401 struct field_info *fi,
15402 const std::vector<variant_part_builder> &variant_parts);
15403
15404 /* Fill in a "struct variant" for a given variant field. RESULT is
15405 the variant to fill in. OBSTACK is where any needed allocations
15406 will be done. OFFSET_MAP holds the mapping from section offsets to
15407 fields for the type. FI describes the fields of the type we're
15408 processing. FIELD is the variant field we're converting. */
15409
15410 static void
15411 create_one_variant (variant &result, struct obstack *obstack,
15412 const offset_map_type &offset_map,
15413 struct field_info *fi, const variant_field &field)
15414 {
15415 result.discriminants = convert_variant_range (obstack, field, false);
15416 result.first_field = field.first_field + fi->baseclasses.size ();
15417 result.last_field = field.last_field + fi->baseclasses.size ();
15418 result.parts = create_variant_parts (obstack, offset_map, fi,
15419 field.variant_parts);
15420 }
15421
15422 /* Fill in a "struct variant_part" for a given variant part. RESULT
15423 is the variant part to fill in. OBSTACK is where any needed
15424 allocations will be done. OFFSET_MAP holds the mapping from
15425 section offsets to fields for the type. FI describes the fields of
15426 the type we're processing. BUILDER is the variant part to be
15427 converted. */
15428
15429 static void
15430 create_one_variant_part (variant_part &result,
15431 struct obstack *obstack,
15432 const offset_map_type &offset_map,
15433 struct field_info *fi,
15434 const variant_part_builder &builder)
15435 {
15436 auto iter = offset_map.find (builder.discriminant_offset);
15437 if (iter == offset_map.end ())
15438 {
15439 result.discriminant_index = -1;
15440 /* Doesn't matter. */
15441 result.is_unsigned = false;
15442 }
15443 else
15444 {
15445 result.discriminant_index = iter->second;
15446 result.is_unsigned
15447 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15448 }
15449
15450 size_t n = builder.variants.size ();
15451 variant *output = new (obstack) variant[n];
15452 for (size_t i = 0; i < n; ++i)
15453 create_one_variant (output[i], obstack, offset_map, fi,
15454 builder.variants[i]);
15455
15456 result.variants = gdb::array_view<variant> (output, n);
15457 }
15458
15459 /* Create a vector of variant parts that can be attached to a type.
15460 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15461 holds the mapping from section offsets to fields for the type. FI
15462 describes the fields of the type we're processing. VARIANT_PARTS
15463 is the vector to convert. */
15464
15465 static const gdb::array_view<variant_part>
15466 create_variant_parts (struct obstack *obstack,
15467 const offset_map_type &offset_map,
15468 struct field_info *fi,
15469 const std::vector<variant_part_builder> &variant_parts)
15470 {
15471 if (variant_parts.empty ())
15472 return {};
15473
15474 size_t n = variant_parts.size ();
15475 variant_part *result = new (obstack) variant_part[n];
15476 for (size_t i = 0; i < n; ++i)
15477 create_one_variant_part (result[i], obstack, offset_map, fi,
15478 variant_parts[i]);
15479
15480 return gdb::array_view<variant_part> (result, n);
15481 }
15482
15483 /* Compute the variant part vector for FIP, attaching it to TYPE when
15484 done. */
15485
15486 static void
15487 add_variant_property (struct field_info *fip, struct type *type,
15488 struct dwarf2_cu *cu)
15489 {
15490 /* Map section offsets of fields to their field index. Note the
15491 field index here does not take the number of baseclasses into
15492 account. */
15493 offset_map_type offset_map;
15494 for (int i = 0; i < fip->fields.size (); ++i)
15495 offset_map[fip->fields[i].offset] = i;
15496
15497 struct objfile *objfile = cu->per_objfile->objfile;
15498 gdb::array_view<variant_part> parts
15499 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15500 fip->variant_parts);
15501
15502 struct dynamic_prop prop;
15503 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15504 obstack_copy (&objfile->objfile_obstack, &parts,
15505 sizeof (parts)));
15506
15507 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15508 }
15509
15510 /* Create the vector of fields, and attach it to the type. */
15511
15512 static void
15513 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15514 struct dwarf2_cu *cu)
15515 {
15516 int nfields = fip->nfields ();
15517
15518 /* Record the field count, allocate space for the array of fields,
15519 and create blank accessibility bitfields if necessary. */
15520 type->set_num_fields (nfields);
15521 type->set_fields
15522 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15523
15524 if (fip->non_public_fields && cu->language != language_ada)
15525 {
15526 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15527
15528 TYPE_FIELD_PRIVATE_BITS (type) =
15529 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15530 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15531
15532 TYPE_FIELD_PROTECTED_BITS (type) =
15533 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15534 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15535
15536 TYPE_FIELD_IGNORE_BITS (type) =
15537 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15538 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15539 }
15540
15541 /* If the type has baseclasses, allocate and clear a bit vector for
15542 TYPE_FIELD_VIRTUAL_BITS. */
15543 if (!fip->baseclasses.empty () && cu->language != language_ada)
15544 {
15545 int num_bytes = B_BYTES (fip->baseclasses.size ());
15546 unsigned char *pointer;
15547
15548 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15549 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15550 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15551 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15552 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15553 }
15554
15555 if (!fip->variant_parts.empty ())
15556 add_variant_property (fip, type, cu);
15557
15558 /* Copy the saved-up fields into the field vector. */
15559 for (int i = 0; i < nfields; ++i)
15560 {
15561 struct nextfield &field
15562 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15563 : fip->fields[i - fip->baseclasses.size ()]);
15564
15565 type->field (i) = field.field;
15566 switch (field.accessibility)
15567 {
15568 case DW_ACCESS_private:
15569 if (cu->language != language_ada)
15570 SET_TYPE_FIELD_PRIVATE (type, i);
15571 break;
15572
15573 case DW_ACCESS_protected:
15574 if (cu->language != language_ada)
15575 SET_TYPE_FIELD_PROTECTED (type, i);
15576 break;
15577
15578 case DW_ACCESS_public:
15579 break;
15580
15581 default:
15582 /* Unknown accessibility. Complain and treat it as public. */
15583 {
15584 complaint (_("unsupported accessibility %d"),
15585 field.accessibility);
15586 }
15587 break;
15588 }
15589 if (i < fip->baseclasses.size ())
15590 {
15591 switch (field.virtuality)
15592 {
15593 case DW_VIRTUALITY_virtual:
15594 case DW_VIRTUALITY_pure_virtual:
15595 if (cu->language == language_ada)
15596 error (_("unexpected virtuality in component of Ada type"));
15597 SET_TYPE_FIELD_VIRTUAL (type, i);
15598 break;
15599 }
15600 }
15601 }
15602 }
15603
15604 /* Return true if this member function is a constructor, false
15605 otherwise. */
15606
15607 static int
15608 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15609 {
15610 const char *fieldname;
15611 const char *type_name;
15612 int len;
15613
15614 if (die->parent == NULL)
15615 return 0;
15616
15617 if (die->parent->tag != DW_TAG_structure_type
15618 && die->parent->tag != DW_TAG_union_type
15619 && die->parent->tag != DW_TAG_class_type)
15620 return 0;
15621
15622 fieldname = dwarf2_name (die, cu);
15623 type_name = dwarf2_name (die->parent, cu);
15624 if (fieldname == NULL || type_name == NULL)
15625 return 0;
15626
15627 len = strlen (fieldname);
15628 return (strncmp (fieldname, type_name, len) == 0
15629 && (type_name[len] == '\0' || type_name[len] == '<'));
15630 }
15631
15632 /* Add a member function to the proper fieldlist. */
15633
15634 static void
15635 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15636 struct type *type, struct dwarf2_cu *cu)
15637 {
15638 struct objfile *objfile = cu->per_objfile->objfile;
15639 struct attribute *attr;
15640 int i;
15641 struct fnfieldlist *flp = nullptr;
15642 struct fn_field *fnp;
15643 const char *fieldname;
15644 struct type *this_type;
15645
15646 if (cu->language == language_ada)
15647 error (_("unexpected member function in Ada type"));
15648
15649 /* Get name of member function. */
15650 fieldname = dwarf2_name (die, cu);
15651 if (fieldname == NULL)
15652 return;
15653
15654 /* Look up member function name in fieldlist. */
15655 for (i = 0; i < fip->fnfieldlists.size (); i++)
15656 {
15657 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15658 {
15659 flp = &fip->fnfieldlists[i];
15660 break;
15661 }
15662 }
15663
15664 /* Create a new fnfieldlist if necessary. */
15665 if (flp == nullptr)
15666 {
15667 fip->fnfieldlists.emplace_back ();
15668 flp = &fip->fnfieldlists.back ();
15669 flp->name = fieldname;
15670 i = fip->fnfieldlists.size () - 1;
15671 }
15672
15673 /* Create a new member function field and add it to the vector of
15674 fnfieldlists. */
15675 flp->fnfields.emplace_back ();
15676 fnp = &flp->fnfields.back ();
15677
15678 /* Delay processing of the physname until later. */
15679 if (cu->language == language_cplus)
15680 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15681 die, cu);
15682 else
15683 {
15684 const char *physname = dwarf2_physname (fieldname, die, cu);
15685 fnp->physname = physname ? physname : "";
15686 }
15687
15688 fnp->type = alloc_type (objfile);
15689 this_type = read_type_die (die, cu);
15690 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15691 {
15692 int nparams = this_type->num_fields ();
15693
15694 /* TYPE is the domain of this method, and THIS_TYPE is the type
15695 of the method itself (TYPE_CODE_METHOD). */
15696 smash_to_method_type (fnp->type, type,
15697 TYPE_TARGET_TYPE (this_type),
15698 this_type->fields (),
15699 this_type->num_fields (),
15700 this_type->has_varargs ());
15701
15702 /* Handle static member functions.
15703 Dwarf2 has no clean way to discern C++ static and non-static
15704 member functions. G++ helps GDB by marking the first
15705 parameter for non-static member functions (which is the this
15706 pointer) as artificial. We obtain this information from
15707 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15708 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15709 fnp->voffset = VOFFSET_STATIC;
15710 }
15711 else
15712 complaint (_("member function type missing for '%s'"),
15713 dwarf2_full_name (fieldname, die, cu));
15714
15715 /* Get fcontext from DW_AT_containing_type if present. */
15716 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15717 fnp->fcontext = die_containing_type (die, cu);
15718
15719 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15720 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15721
15722 /* Get accessibility. */
15723 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15724 switch (accessibility)
15725 {
15726 case DW_ACCESS_private:
15727 fnp->is_private = 1;
15728 break;
15729 case DW_ACCESS_protected:
15730 fnp->is_protected = 1;
15731 break;
15732 }
15733
15734 /* Check for artificial methods. */
15735 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15736 if (attr && attr->as_boolean ())
15737 fnp->is_artificial = 1;
15738
15739 /* Check for defaulted methods. */
15740 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15741 if (attr != nullptr)
15742 fnp->defaulted = attr->defaulted ();
15743
15744 /* Check for deleted methods. */
15745 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15746 if (attr != nullptr && attr->as_boolean ())
15747 fnp->is_deleted = 1;
15748
15749 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15750
15751 /* Get index in virtual function table if it is a virtual member
15752 function. For older versions of GCC, this is an offset in the
15753 appropriate virtual table, as specified by DW_AT_containing_type.
15754 For everyone else, it is an expression to be evaluated relative
15755 to the object address. */
15756
15757 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15758 if (attr != nullptr)
15759 {
15760 if (attr->form_is_block () && attr->as_block ()->size > 0)
15761 {
15762 struct dwarf_block *block = attr->as_block ();
15763
15764 if (block->data[0] == DW_OP_constu)
15765 {
15766 /* Old-style GCC. */
15767 fnp->voffset = decode_locdesc (block, cu) + 2;
15768 }
15769 else if (block->data[0] == DW_OP_deref
15770 || (block->size > 1
15771 && block->data[0] == DW_OP_deref_size
15772 && block->data[1] == cu->header.addr_size))
15773 {
15774 fnp->voffset = decode_locdesc (block, cu);
15775 if ((fnp->voffset % cu->header.addr_size) != 0)
15776 dwarf2_complex_location_expr_complaint ();
15777 else
15778 fnp->voffset /= cu->header.addr_size;
15779 fnp->voffset += 2;
15780 }
15781 else
15782 dwarf2_complex_location_expr_complaint ();
15783
15784 if (!fnp->fcontext)
15785 {
15786 /* If there is no `this' field and no DW_AT_containing_type,
15787 we cannot actually find a base class context for the
15788 vtable! */
15789 if (this_type->num_fields () == 0
15790 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15791 {
15792 complaint (_("cannot determine context for virtual member "
15793 "function \"%s\" (offset %s)"),
15794 fieldname, sect_offset_str (die->sect_off));
15795 }
15796 else
15797 {
15798 fnp->fcontext
15799 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15800 }
15801 }
15802 }
15803 else if (attr->form_is_section_offset ())
15804 {
15805 dwarf2_complex_location_expr_complaint ();
15806 }
15807 else
15808 {
15809 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15810 fieldname);
15811 }
15812 }
15813 else
15814 {
15815 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15816 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15817 {
15818 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15819 complaint (_("Member function \"%s\" (offset %s) is virtual "
15820 "but the vtable offset is not specified"),
15821 fieldname, sect_offset_str (die->sect_off));
15822 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15823 TYPE_CPLUS_DYNAMIC (type) = 1;
15824 }
15825 }
15826 }
15827
15828 /* Create the vector of member function fields, and attach it to the type. */
15829
15830 static void
15831 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15832 struct dwarf2_cu *cu)
15833 {
15834 if (cu->language == language_ada)
15835 error (_("unexpected member functions in Ada type"));
15836
15837 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15838 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15839 TYPE_ALLOC (type,
15840 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15841
15842 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15843 {
15844 struct fnfieldlist &nf = fip->fnfieldlists[i];
15845 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15846
15847 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15848 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15849 fn_flp->fn_fields = (struct fn_field *)
15850 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15851
15852 for (int k = 0; k < nf.fnfields.size (); ++k)
15853 fn_flp->fn_fields[k] = nf.fnfields[k];
15854 }
15855
15856 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15857 }
15858
15859 /* Returns non-zero if NAME is the name of a vtable member in CU's
15860 language, zero otherwise. */
15861 static int
15862 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15863 {
15864 static const char vptr[] = "_vptr";
15865
15866 /* Look for the C++ form of the vtable. */
15867 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15868 return 1;
15869
15870 return 0;
15871 }
15872
15873 /* GCC outputs unnamed structures that are really pointers to member
15874 functions, with the ABI-specified layout. If TYPE describes
15875 such a structure, smash it into a member function type.
15876
15877 GCC shouldn't do this; it should just output pointer to member DIEs.
15878 This is GCC PR debug/28767. */
15879
15880 static void
15881 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15882 {
15883 struct type *pfn_type, *self_type, *new_type;
15884
15885 /* Check for a structure with no name and two children. */
15886 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15887 return;
15888
15889 /* Check for __pfn and __delta members. */
15890 if (TYPE_FIELD_NAME (type, 0) == NULL
15891 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15892 || TYPE_FIELD_NAME (type, 1) == NULL
15893 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15894 return;
15895
15896 /* Find the type of the method. */
15897 pfn_type = type->field (0).type ();
15898 if (pfn_type == NULL
15899 || pfn_type->code () != TYPE_CODE_PTR
15900 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15901 return;
15902
15903 /* Look for the "this" argument. */
15904 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15905 if (pfn_type->num_fields () == 0
15906 /* || pfn_type->field (0).type () == NULL */
15907 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15908 return;
15909
15910 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15911 new_type = alloc_type (objfile);
15912 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15913 pfn_type->fields (), pfn_type->num_fields (),
15914 pfn_type->has_varargs ());
15915 smash_to_methodptr_type (type, new_type);
15916 }
15917
15918 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15919 requires rewriting, then copy it and return the updated copy.
15920 Otherwise return nullptr. */
15921
15922 static struct type *
15923 rewrite_array_type (struct type *type)
15924 {
15925 if (type->code () != TYPE_CODE_ARRAY)
15926 return nullptr;
15927
15928 struct type *index_type = type->index_type ();
15929 range_bounds *current_bounds = index_type->bounds ();
15930
15931 /* Handle multi-dimensional arrays. */
15932 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15933 if (new_target == nullptr)
15934 {
15935 /* Maybe we don't need to rewrite this array. */
15936 if (current_bounds->low.kind () == PROP_CONST
15937 && current_bounds->high.kind () == PROP_CONST)
15938 return nullptr;
15939 }
15940
15941 /* Either the target type was rewritten, or the bounds have to be
15942 updated. Either way we want to copy the type and update
15943 everything. */
15944 struct type *copy = copy_type (type);
15945 int nfields = copy->num_fields ();
15946 field *new_fields
15947 = ((struct field *) TYPE_ZALLOC (copy,
15948 nfields * sizeof (struct field)));
15949 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15950 copy->set_fields (new_fields);
15951 if (new_target != nullptr)
15952 TYPE_TARGET_TYPE (copy) = new_target;
15953
15954 struct type *index_copy = copy_type (index_type);
15955 range_bounds *bounds
15956 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15957 sizeof (range_bounds));
15958 *bounds = *current_bounds;
15959 bounds->low.set_const_val (1);
15960 bounds->high.set_const_val (0);
15961 index_copy->set_bounds (bounds);
15962 copy->set_index_type (index_copy);
15963
15964 return copy;
15965 }
15966
15967 /* While some versions of GCC will generate complicated DWARF for an
15968 array (see quirk_ada_thick_pointer), more recent versions were
15969 modified to emit an explicit thick pointer structure. However, in
15970 this case, the array still has DWARF expressions for its ranges,
15971 and these must be ignored. */
15972
15973 static void
15974 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15975 struct type *type)
15976 {
15977 gdb_assert (cu->language == language_ada);
15978
15979 /* Check for a structure with two children. */
15980 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15981 return;
15982
15983 /* Check for P_ARRAY and P_BOUNDS members. */
15984 if (TYPE_FIELD_NAME (type, 0) == NULL
15985 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15986 || TYPE_FIELD_NAME (type, 1) == NULL
15987 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15988 return;
15989
15990 /* Make sure we're looking at a pointer to an array. */
15991 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15992 return;
15993
15994 /* The Ada code already knows how to handle these types, so all that
15995 we need to do is turn the bounds into static bounds. However, we
15996 don't want to rewrite existing array or index types in-place,
15997 because those may be referenced in other contexts where this
15998 rewriting is undesirable. */
15999 struct type *new_ary_type
16000 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
16001 if (new_ary_type != nullptr)
16002 type->field (0).set_type (lookup_pointer_type (new_ary_type));
16003 }
16004
16005 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
16006 appropriate error checking and issuing complaints if there is a
16007 problem. */
16008
16009 static ULONGEST
16010 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
16011 {
16012 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
16013
16014 if (attr == nullptr)
16015 return 0;
16016
16017 if (!attr->form_is_constant ())
16018 {
16019 complaint (_("DW_AT_alignment must have constant form"
16020 " - DIE at %s [in module %s]"),
16021 sect_offset_str (die->sect_off),
16022 objfile_name (cu->per_objfile->objfile));
16023 return 0;
16024 }
16025
16026 LONGEST val = attr->constant_value (0);
16027 if (val < 0)
16028 {
16029 complaint (_("DW_AT_alignment value must not be negative"
16030 " - DIE at %s [in module %s]"),
16031 sect_offset_str (die->sect_off),
16032 objfile_name (cu->per_objfile->objfile));
16033 return 0;
16034 }
16035 ULONGEST align = val;
16036
16037 if (align == 0)
16038 {
16039 complaint (_("DW_AT_alignment value must not be zero"
16040 " - DIE at %s [in module %s]"),
16041 sect_offset_str (die->sect_off),
16042 objfile_name (cu->per_objfile->objfile));
16043 return 0;
16044 }
16045 if ((align & (align - 1)) != 0)
16046 {
16047 complaint (_("DW_AT_alignment value must be a power of 2"
16048 " - DIE at %s [in module %s]"),
16049 sect_offset_str (die->sect_off),
16050 objfile_name (cu->per_objfile->objfile));
16051 return 0;
16052 }
16053
16054 return align;
16055 }
16056
16057 /* If the DIE has a DW_AT_alignment attribute, use its value to set
16058 the alignment for TYPE. */
16059
16060 static void
16061 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16062 struct type *type)
16063 {
16064 if (!set_type_align (type, get_alignment (cu, die)))
16065 complaint (_("DW_AT_alignment value too large"
16066 " - DIE at %s [in module %s]"),
16067 sect_offset_str (die->sect_off),
16068 objfile_name (cu->per_objfile->objfile));
16069 }
16070
16071 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16072 constant for a type, according to DWARF5 spec, Table 5.5. */
16073
16074 static bool
16075 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16076 {
16077 switch (value)
16078 {
16079 case DW_CC_normal:
16080 case DW_CC_pass_by_reference:
16081 case DW_CC_pass_by_value:
16082 return true;
16083
16084 default:
16085 complaint (_("unrecognized DW_AT_calling_convention value "
16086 "(%s) for a type"), pulongest (value));
16087 return false;
16088 }
16089 }
16090
16091 /* Check if the given VALUE is a valid enum dwarf_calling_convention
16092 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16093 also according to GNU-specific values (see include/dwarf2.h). */
16094
16095 static bool
16096 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16097 {
16098 switch (value)
16099 {
16100 case DW_CC_normal:
16101 case DW_CC_program:
16102 case DW_CC_nocall:
16103 return true;
16104
16105 case DW_CC_GNU_renesas_sh:
16106 case DW_CC_GNU_borland_fastcall_i386:
16107 case DW_CC_GDB_IBM_OpenCL:
16108 return true;
16109
16110 default:
16111 complaint (_("unrecognized DW_AT_calling_convention value "
16112 "(%s) for a subroutine"), pulongest (value));
16113 return false;
16114 }
16115 }
16116
16117 /* Called when we find the DIE that starts a structure or union scope
16118 (definition) to create a type for the structure or union. Fill in
16119 the type's name and general properties; the members will not be
16120 processed until process_structure_scope. A symbol table entry for
16121 the type will also not be done until process_structure_scope (assuming
16122 the type has a name).
16123
16124 NOTE: we need to call these functions regardless of whether or not the
16125 DIE has a DW_AT_name attribute, since it might be an anonymous
16126 structure or union. This gets the type entered into our set of
16127 user defined types. */
16128
16129 static struct type *
16130 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
16131 {
16132 struct objfile *objfile = cu->per_objfile->objfile;
16133 struct type *type;
16134 struct attribute *attr;
16135 const char *name;
16136
16137 /* If the definition of this type lives in .debug_types, read that type.
16138 Don't follow DW_AT_specification though, that will take us back up
16139 the chain and we want to go down. */
16140 attr = die->attr (DW_AT_signature);
16141 if (attr != nullptr)
16142 {
16143 type = get_DW_AT_signature_type (die, attr, cu);
16144
16145 /* The type's CU may not be the same as CU.
16146 Ensure TYPE is recorded with CU in die_type_hash. */
16147 return set_die_type (die, type, cu);
16148 }
16149
16150 type = alloc_type (objfile);
16151 INIT_CPLUS_SPECIFIC (type);
16152
16153 name = dwarf2_name (die, cu);
16154 if (name != NULL)
16155 {
16156 if (cu->language == language_cplus
16157 || cu->language == language_d
16158 || cu->language == language_rust)
16159 {
16160 const char *full_name = dwarf2_full_name (name, die, cu);
16161
16162 /* dwarf2_full_name might have already finished building the DIE's
16163 type. If so, there is no need to continue. */
16164 if (get_die_type (die, cu) != NULL)
16165 return get_die_type (die, cu);
16166
16167 type->set_name (full_name);
16168 }
16169 else
16170 {
16171 /* The name is already allocated along with this objfile, so
16172 we don't need to duplicate it for the type. */
16173 type->set_name (name);
16174 }
16175 }
16176
16177 if (die->tag == DW_TAG_structure_type)
16178 {
16179 type->set_code (TYPE_CODE_STRUCT);
16180 }
16181 else if (die->tag == DW_TAG_union_type)
16182 {
16183 type->set_code (TYPE_CODE_UNION);
16184 }
16185 else
16186 {
16187 type->set_code (TYPE_CODE_STRUCT);
16188 }
16189
16190 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16191 TYPE_DECLARED_CLASS (type) = 1;
16192
16193 /* Store the calling convention in the type if it's available in
16194 the die. Otherwise the calling convention remains set to
16195 the default value DW_CC_normal. */
16196 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16197 if (attr != nullptr
16198 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
16199 {
16200 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16201 TYPE_CPLUS_CALLING_CONVENTION (type)
16202 = (enum dwarf_calling_convention) (attr->constant_value (0));
16203 }
16204
16205 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16206 if (attr != nullptr)
16207 {
16208 if (attr->form_is_constant ())
16209 TYPE_LENGTH (type) = attr->constant_value (0);
16210 else
16211 {
16212 struct dynamic_prop prop;
16213 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
16214 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
16215 TYPE_LENGTH (type) = 0;
16216 }
16217 }
16218 else
16219 {
16220 TYPE_LENGTH (type) = 0;
16221 }
16222
16223 maybe_set_alignment (cu, die, type);
16224
16225 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
16226 {
16227 /* ICC<14 does not output the required DW_AT_declaration on
16228 incomplete types, but gives them a size of zero. */
16229 type->set_is_stub (true);
16230 }
16231 else
16232 type->set_stub_is_supported (true);
16233
16234 if (die_is_declaration (die, cu))
16235 type->set_is_stub (true);
16236 else if (attr == NULL && die->child == NULL
16237 && producer_is_realview (cu->producer))
16238 /* RealView does not output the required DW_AT_declaration
16239 on incomplete types. */
16240 type->set_is_stub (true);
16241
16242 /* We need to add the type field to the die immediately so we don't
16243 infinitely recurse when dealing with pointers to the structure
16244 type within the structure itself. */
16245 set_die_type (die, type, cu);
16246
16247 /* set_die_type should be already done. */
16248 set_descriptive_type (type, die, cu);
16249
16250 return type;
16251 }
16252
16253 static void handle_struct_member_die
16254 (struct die_info *child_die,
16255 struct type *type,
16256 struct field_info *fi,
16257 std::vector<struct symbol *> *template_args,
16258 struct dwarf2_cu *cu);
16259
16260 /* A helper for handle_struct_member_die that handles
16261 DW_TAG_variant_part. */
16262
16263 static void
16264 handle_variant_part (struct die_info *die, struct type *type,
16265 struct field_info *fi,
16266 std::vector<struct symbol *> *template_args,
16267 struct dwarf2_cu *cu)
16268 {
16269 variant_part_builder *new_part;
16270 if (fi->current_variant_part == nullptr)
16271 {
16272 fi->variant_parts.emplace_back ();
16273 new_part = &fi->variant_parts.back ();
16274 }
16275 else if (!fi->current_variant_part->processing_variant)
16276 {
16277 complaint (_("nested DW_TAG_variant_part seen "
16278 "- DIE at %s [in module %s]"),
16279 sect_offset_str (die->sect_off),
16280 objfile_name (cu->per_objfile->objfile));
16281 return;
16282 }
16283 else
16284 {
16285 variant_field &current = fi->current_variant_part->variants.back ();
16286 current.variant_parts.emplace_back ();
16287 new_part = &current.variant_parts.back ();
16288 }
16289
16290 /* When we recurse, we want callees to add to this new variant
16291 part. */
16292 scoped_restore save_current_variant_part
16293 = make_scoped_restore (&fi->current_variant_part, new_part);
16294
16295 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16296 if (discr == NULL)
16297 {
16298 /* It's a univariant form, an extension we support. */
16299 }
16300 else if (discr->form_is_ref ())
16301 {
16302 struct dwarf2_cu *target_cu = cu;
16303 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16304
16305 new_part->discriminant_offset = target_die->sect_off;
16306 }
16307 else
16308 {
16309 complaint (_("DW_AT_discr does not have DIE reference form"
16310 " - DIE at %s [in module %s]"),
16311 sect_offset_str (die->sect_off),
16312 objfile_name (cu->per_objfile->objfile));
16313 }
16314
16315 for (die_info *child_die = die->child;
16316 child_die != NULL;
16317 child_die = child_die->sibling)
16318 handle_struct_member_die (child_die, type, fi, template_args, cu);
16319 }
16320
16321 /* A helper for handle_struct_member_die that handles
16322 DW_TAG_variant. */
16323
16324 static void
16325 handle_variant (struct die_info *die, struct type *type,
16326 struct field_info *fi,
16327 std::vector<struct symbol *> *template_args,
16328 struct dwarf2_cu *cu)
16329 {
16330 if (fi->current_variant_part == nullptr)
16331 {
16332 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16333 "- DIE at %s [in module %s]"),
16334 sect_offset_str (die->sect_off),
16335 objfile_name (cu->per_objfile->objfile));
16336 return;
16337 }
16338 if (fi->current_variant_part->processing_variant)
16339 {
16340 complaint (_("nested DW_TAG_variant seen "
16341 "- DIE at %s [in module %s]"),
16342 sect_offset_str (die->sect_off),
16343 objfile_name (cu->per_objfile->objfile));
16344 return;
16345 }
16346
16347 scoped_restore save_processing_variant
16348 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16349 true);
16350
16351 fi->current_variant_part->variants.emplace_back ();
16352 variant_field &variant = fi->current_variant_part->variants.back ();
16353 variant.first_field = fi->fields.size ();
16354
16355 /* In a variant we want to get the discriminant and also add a
16356 field for our sole member child. */
16357 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16358 if (discr == nullptr || !discr->form_is_constant ())
16359 {
16360 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16361 if (discr == nullptr || discr->as_block ()->size == 0)
16362 variant.default_branch = true;
16363 else
16364 variant.discr_list_data = discr->as_block ();
16365 }
16366 else
16367 variant.discriminant_value = discr->constant_value (0);
16368
16369 for (die_info *variant_child = die->child;
16370 variant_child != NULL;
16371 variant_child = variant_child->sibling)
16372 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16373
16374 variant.last_field = fi->fields.size ();
16375 }
16376
16377 /* A helper for process_structure_scope that handles a single member
16378 DIE. */
16379
16380 static void
16381 handle_struct_member_die (struct die_info *child_die, struct type *type,
16382 struct field_info *fi,
16383 std::vector<struct symbol *> *template_args,
16384 struct dwarf2_cu *cu)
16385 {
16386 if (child_die->tag == DW_TAG_member
16387 || child_die->tag == DW_TAG_variable)
16388 {
16389 /* NOTE: carlton/2002-11-05: A C++ static data member
16390 should be a DW_TAG_member that is a declaration, but
16391 all versions of G++ as of this writing (so through at
16392 least 3.2.1) incorrectly generate DW_TAG_variable
16393 tags for them instead. */
16394 dwarf2_add_field (fi, child_die, cu);
16395 }
16396 else if (child_die->tag == DW_TAG_subprogram)
16397 {
16398 /* Rust doesn't have member functions in the C++ sense.
16399 However, it does emit ordinary functions as children
16400 of a struct DIE. */
16401 if (cu->language == language_rust)
16402 read_func_scope (child_die, cu);
16403 else
16404 {
16405 /* C++ member function. */
16406 dwarf2_add_member_fn (fi, child_die, type, cu);
16407 }
16408 }
16409 else if (child_die->tag == DW_TAG_inheritance)
16410 {
16411 /* C++ base class field. */
16412 dwarf2_add_field (fi, child_die, cu);
16413 }
16414 else if (type_can_define_types (child_die))
16415 dwarf2_add_type_defn (fi, child_die, cu);
16416 else if (child_die->tag == DW_TAG_template_type_param
16417 || child_die->tag == DW_TAG_template_value_param)
16418 {
16419 struct symbol *arg = new_symbol (child_die, NULL, cu);
16420
16421 if (arg != NULL)
16422 template_args->push_back (arg);
16423 }
16424 else if (child_die->tag == DW_TAG_variant_part)
16425 handle_variant_part (child_die, type, fi, template_args, cu);
16426 else if (child_die->tag == DW_TAG_variant)
16427 handle_variant (child_die, type, fi, template_args, cu);
16428 }
16429
16430 /* Finish creating a structure or union type, including filling in
16431 its members and creating a symbol for it. */
16432
16433 static void
16434 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16435 {
16436 struct objfile *objfile = cu->per_objfile->objfile;
16437 struct die_info *child_die;
16438 struct type *type;
16439
16440 type = get_die_type (die, cu);
16441 if (type == NULL)
16442 type = read_structure_type (die, cu);
16443
16444 bool has_template_parameters = false;
16445 if (die->child != NULL && ! die_is_declaration (die, cu))
16446 {
16447 struct field_info fi;
16448 std::vector<struct symbol *> template_args;
16449
16450 child_die = die->child;
16451
16452 while (child_die && child_die->tag)
16453 {
16454 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16455 child_die = child_die->sibling;
16456 }
16457
16458 /* Attach template arguments to type. */
16459 if (!template_args.empty ())
16460 {
16461 has_template_parameters = true;
16462 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16463 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16464 TYPE_TEMPLATE_ARGUMENTS (type)
16465 = XOBNEWVEC (&objfile->objfile_obstack,
16466 struct symbol *,
16467 TYPE_N_TEMPLATE_ARGUMENTS (type));
16468 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16469 template_args.data (),
16470 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16471 * sizeof (struct symbol *)));
16472 }
16473
16474 /* Attach fields and member functions to the type. */
16475 if (fi.nfields () > 0)
16476 dwarf2_attach_fields_to_type (&fi, type, cu);
16477 if (!fi.fnfieldlists.empty ())
16478 {
16479 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16480
16481 /* Get the type which refers to the base class (possibly this
16482 class itself) which contains the vtable pointer for the current
16483 class from the DW_AT_containing_type attribute. This use of
16484 DW_AT_containing_type is a GNU extension. */
16485
16486 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16487 {
16488 struct type *t = die_containing_type (die, cu);
16489
16490 set_type_vptr_basetype (type, t);
16491 if (type == t)
16492 {
16493 int i;
16494
16495 /* Our own class provides vtbl ptr. */
16496 for (i = t->num_fields () - 1;
16497 i >= TYPE_N_BASECLASSES (t);
16498 --i)
16499 {
16500 const char *fieldname = TYPE_FIELD_NAME (t, i);
16501
16502 if (is_vtable_name (fieldname, cu))
16503 {
16504 set_type_vptr_fieldno (type, i);
16505 break;
16506 }
16507 }
16508
16509 /* Complain if virtual function table field not found. */
16510 if (i < TYPE_N_BASECLASSES (t))
16511 complaint (_("virtual function table pointer "
16512 "not found when defining class '%s'"),
16513 type->name () ? type->name () : "");
16514 }
16515 else
16516 {
16517 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16518 }
16519 }
16520 else if (cu->producer
16521 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16522 {
16523 /* The IBM XLC compiler does not provide direct indication
16524 of the containing type, but the vtable pointer is
16525 always named __vfp. */
16526
16527 int i;
16528
16529 for (i = type->num_fields () - 1;
16530 i >= TYPE_N_BASECLASSES (type);
16531 --i)
16532 {
16533 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16534 {
16535 set_type_vptr_fieldno (type, i);
16536 set_type_vptr_basetype (type, type);
16537 break;
16538 }
16539 }
16540 }
16541 }
16542
16543 /* Copy fi.typedef_field_list linked list elements content into the
16544 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16545 if (!fi.typedef_field_list.empty ())
16546 {
16547 int count = fi.typedef_field_list.size ();
16548
16549 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16550 TYPE_TYPEDEF_FIELD_ARRAY (type)
16551 = ((struct decl_field *)
16552 TYPE_ALLOC (type,
16553 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16554 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16555
16556 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16557 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16558 }
16559
16560 /* Copy fi.nested_types_list linked list elements content into the
16561 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16562 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16563 {
16564 int count = fi.nested_types_list.size ();
16565
16566 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16567 TYPE_NESTED_TYPES_ARRAY (type)
16568 = ((struct decl_field *)
16569 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16570 TYPE_NESTED_TYPES_COUNT (type) = count;
16571
16572 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16573 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16574 }
16575 }
16576
16577 quirk_gcc_member_function_pointer (type, objfile);
16578 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16579 cu->rust_unions.push_back (type);
16580 else if (cu->language == language_ada)
16581 quirk_ada_thick_pointer_struct (die, cu, type);
16582
16583 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16584 snapshots) has been known to create a die giving a declaration
16585 for a class that has, as a child, a die giving a definition for a
16586 nested class. So we have to process our children even if the
16587 current die is a declaration. Normally, of course, a declaration
16588 won't have any children at all. */
16589
16590 child_die = die->child;
16591
16592 while (child_die != NULL && child_die->tag)
16593 {
16594 if (child_die->tag == DW_TAG_member
16595 || child_die->tag == DW_TAG_variable
16596 || child_die->tag == DW_TAG_inheritance
16597 || child_die->tag == DW_TAG_template_value_param
16598 || child_die->tag == DW_TAG_template_type_param)
16599 {
16600 /* Do nothing. */
16601 }
16602 else
16603 process_die (child_die, cu);
16604
16605 child_die = child_die->sibling;
16606 }
16607
16608 /* Do not consider external references. According to the DWARF standard,
16609 these DIEs are identified by the fact that they have no byte_size
16610 attribute, and a declaration attribute. */
16611 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16612 || !die_is_declaration (die, cu)
16613 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16614 {
16615 struct symbol *sym = new_symbol (die, type, cu);
16616
16617 if (has_template_parameters)
16618 {
16619 struct symtab *symtab;
16620 if (sym != nullptr)
16621 symtab = symbol_symtab (sym);
16622 else if (cu->line_header != nullptr)
16623 {
16624 /* Any related symtab will do. */
16625 symtab
16626 = cu->line_header->file_names ()[0].symtab;
16627 }
16628 else
16629 {
16630 symtab = nullptr;
16631 complaint (_("could not find suitable "
16632 "symtab for template parameter"
16633 " - DIE at %s [in module %s]"),
16634 sect_offset_str (die->sect_off),
16635 objfile_name (objfile));
16636 }
16637
16638 if (symtab != nullptr)
16639 {
16640 /* Make sure that the symtab is set on the new symbols.
16641 Even though they don't appear in this symtab directly,
16642 other parts of gdb assume that symbols do, and this is
16643 reasonably true. */
16644 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16645 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16646 }
16647 }
16648 }
16649 }
16650
16651 /* Assuming DIE is an enumeration type, and TYPE is its associated
16652 type, update TYPE using some information only available in DIE's
16653 children. In particular, the fields are computed. */
16654
16655 static void
16656 update_enumeration_type_from_children (struct die_info *die,
16657 struct type *type,
16658 struct dwarf2_cu *cu)
16659 {
16660 struct die_info *child_die;
16661 int unsigned_enum = 1;
16662 int flag_enum = 1;
16663
16664 auto_obstack obstack;
16665 std::vector<struct field> fields;
16666
16667 for (child_die = die->child;
16668 child_die != NULL && child_die->tag;
16669 child_die = child_die->sibling)
16670 {
16671 struct attribute *attr;
16672 LONGEST value;
16673 const gdb_byte *bytes;
16674 struct dwarf2_locexpr_baton *baton;
16675 const char *name;
16676
16677 if (child_die->tag != DW_TAG_enumerator)
16678 continue;
16679
16680 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16681 if (attr == NULL)
16682 continue;
16683
16684 name = dwarf2_name (child_die, cu);
16685 if (name == NULL)
16686 name = "<anonymous enumerator>";
16687
16688 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16689 &value, &bytes, &baton);
16690 if (value < 0)
16691 {
16692 unsigned_enum = 0;
16693 flag_enum = 0;
16694 }
16695 else
16696 {
16697 if (count_one_bits_ll (value) >= 2)
16698 flag_enum = 0;
16699 }
16700
16701 fields.emplace_back ();
16702 struct field &field = fields.back ();
16703 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16704 SET_FIELD_ENUMVAL (field, value);
16705 }
16706
16707 if (!fields.empty ())
16708 {
16709 type->set_num_fields (fields.size ());
16710 type->set_fields
16711 ((struct field *)
16712 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16713 memcpy (type->fields (), fields.data (),
16714 sizeof (struct field) * fields.size ());
16715 }
16716
16717 if (unsigned_enum)
16718 type->set_is_unsigned (true);
16719
16720 if (flag_enum)
16721 TYPE_FLAG_ENUM (type) = 1;
16722 }
16723
16724 /* Given a DW_AT_enumeration_type die, set its type. We do not
16725 complete the type's fields yet, or create any symbols. */
16726
16727 static struct type *
16728 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16729 {
16730 struct objfile *objfile = cu->per_objfile->objfile;
16731 struct type *type;
16732 struct attribute *attr;
16733 const char *name;
16734
16735 /* If the definition of this type lives in .debug_types, read that type.
16736 Don't follow DW_AT_specification though, that will take us back up
16737 the chain and we want to go down. */
16738 attr = die->attr (DW_AT_signature);
16739 if (attr != nullptr)
16740 {
16741 type = get_DW_AT_signature_type (die, attr, cu);
16742
16743 /* The type's CU may not be the same as CU.
16744 Ensure TYPE is recorded with CU in die_type_hash. */
16745 return set_die_type (die, type, cu);
16746 }
16747
16748 type = alloc_type (objfile);
16749
16750 type->set_code (TYPE_CODE_ENUM);
16751 name = dwarf2_full_name (NULL, die, cu);
16752 if (name != NULL)
16753 type->set_name (name);
16754
16755 attr = dwarf2_attr (die, DW_AT_type, cu);
16756 if (attr != NULL)
16757 {
16758 struct type *underlying_type = die_type (die, cu);
16759
16760 TYPE_TARGET_TYPE (type) = underlying_type;
16761 }
16762
16763 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16764 if (attr != nullptr)
16765 {
16766 TYPE_LENGTH (type) = attr->constant_value (0);
16767 }
16768 else
16769 {
16770 TYPE_LENGTH (type) = 0;
16771 }
16772
16773 maybe_set_alignment (cu, die, type);
16774
16775 /* The enumeration DIE can be incomplete. In Ada, any type can be
16776 declared as private in the package spec, and then defined only
16777 inside the package body. Such types are known as Taft Amendment
16778 Types. When another package uses such a type, an incomplete DIE
16779 may be generated by the compiler. */
16780 if (die_is_declaration (die, cu))
16781 type->set_is_stub (true);
16782
16783 /* If this type has an underlying type that is not a stub, then we
16784 may use its attributes. We always use the "unsigned" attribute
16785 in this situation, because ordinarily we guess whether the type
16786 is unsigned -- but the guess can be wrong and the underlying type
16787 can tell us the reality. However, we defer to a local size
16788 attribute if one exists, because this lets the compiler override
16789 the underlying type if needed. */
16790 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16791 {
16792 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16793 underlying_type = check_typedef (underlying_type);
16794
16795 type->set_is_unsigned (underlying_type->is_unsigned ());
16796
16797 if (TYPE_LENGTH (type) == 0)
16798 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16799
16800 if (TYPE_RAW_ALIGN (type) == 0
16801 && TYPE_RAW_ALIGN (underlying_type) != 0)
16802 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16803 }
16804
16805 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16806
16807 set_die_type (die, type, cu);
16808
16809 /* Finish the creation of this type by using the enum's children.
16810 Note that, as usual, this must come after set_die_type to avoid
16811 infinite recursion when trying to compute the names of the
16812 enumerators. */
16813 update_enumeration_type_from_children (die, type, cu);
16814
16815 return type;
16816 }
16817
16818 /* Given a pointer to a die which begins an enumeration, process all
16819 the dies that define the members of the enumeration, and create the
16820 symbol for the enumeration type.
16821
16822 NOTE: We reverse the order of the element list. */
16823
16824 static void
16825 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16826 {
16827 struct type *this_type;
16828
16829 this_type = get_die_type (die, cu);
16830 if (this_type == NULL)
16831 this_type = read_enumeration_type (die, cu);
16832
16833 if (die->child != NULL)
16834 {
16835 struct die_info *child_die;
16836 const char *name;
16837
16838 child_die = die->child;
16839 while (child_die && child_die->tag)
16840 {
16841 if (child_die->tag != DW_TAG_enumerator)
16842 {
16843 process_die (child_die, cu);
16844 }
16845 else
16846 {
16847 name = dwarf2_name (child_die, cu);
16848 if (name)
16849 new_symbol (child_die, this_type, cu);
16850 }
16851
16852 child_die = child_die->sibling;
16853 }
16854 }
16855
16856 /* If we are reading an enum from a .debug_types unit, and the enum
16857 is a declaration, and the enum is not the signatured type in the
16858 unit, then we do not want to add a symbol for it. Adding a
16859 symbol would in some cases obscure the true definition of the
16860 enum, giving users an incomplete type when the definition is
16861 actually available. Note that we do not want to do this for all
16862 enums which are just declarations, because C++0x allows forward
16863 enum declarations. */
16864 if (cu->per_cu->is_debug_types
16865 && die_is_declaration (die, cu))
16866 {
16867 struct signatured_type *sig_type;
16868
16869 sig_type = (struct signatured_type *) cu->per_cu;
16870 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16871 if (sig_type->type_offset_in_section != die->sect_off)
16872 return;
16873 }
16874
16875 new_symbol (die, this_type, cu);
16876 }
16877
16878 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16879 expression for an index type and finds the corresponding field
16880 offset in the hidden "P_BOUNDS" structure. Returns true on success
16881 and updates *FIELD, false if it fails to recognize an
16882 expression. */
16883
16884 static bool
16885 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16886 int *bounds_offset, struct field *field,
16887 struct dwarf2_cu *cu)
16888 {
16889 struct attribute *attr = dwarf2_attr (die, name, cu);
16890 if (attr == nullptr || !attr->form_is_block ())
16891 return false;
16892
16893 const struct dwarf_block *block = attr->as_block ();
16894 const gdb_byte *start = block->data;
16895 const gdb_byte *end = block->data + block->size;
16896
16897 /* The expression to recognize generally looks like:
16898
16899 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16900 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16901
16902 However, the second "plus_uconst" may be missing:
16903
16904 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16905 DW_OP_deref_size: 4)
16906
16907 This happens when the field is at the start of the structure.
16908
16909 Also, the final deref may not be sized:
16910
16911 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16912 DW_OP_deref)
16913
16914 This happens when the size of the index type happens to be the
16915 same as the architecture's word size. This can occur with or
16916 without the second plus_uconst. */
16917
16918 if (end - start < 2)
16919 return false;
16920 if (*start++ != DW_OP_push_object_address)
16921 return false;
16922 if (*start++ != DW_OP_plus_uconst)
16923 return false;
16924
16925 uint64_t this_bound_off;
16926 start = gdb_read_uleb128 (start, end, &this_bound_off);
16927 if (start == nullptr || (int) this_bound_off != this_bound_off)
16928 return false;
16929 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16930 is consistent among all bounds. */
16931 if (*bounds_offset == -1)
16932 *bounds_offset = this_bound_off;
16933 else if (*bounds_offset != this_bound_off)
16934 return false;
16935
16936 if (start == end || *start++ != DW_OP_deref)
16937 return false;
16938
16939 int offset = 0;
16940 if (start ==end)
16941 return false;
16942 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16943 {
16944 /* This means an offset of 0. */
16945 }
16946 else if (*start++ != DW_OP_plus_uconst)
16947 return false;
16948 else
16949 {
16950 /* The size is the parameter to DW_OP_plus_uconst. */
16951 uint64_t val;
16952 start = gdb_read_uleb128 (start, end, &val);
16953 if (start == nullptr)
16954 return false;
16955 if ((int) val != val)
16956 return false;
16957 offset = val;
16958 }
16959
16960 if (start == end)
16961 return false;
16962
16963 uint64_t size;
16964 if (*start == DW_OP_deref_size)
16965 {
16966 start = gdb_read_uleb128 (start + 1, end, &size);
16967 if (start == nullptr)
16968 return false;
16969 }
16970 else if (*start == DW_OP_deref)
16971 {
16972 size = cu->header.addr_size;
16973 ++start;
16974 }
16975 else
16976 return false;
16977
16978 SET_FIELD_BITPOS (*field, 8 * offset);
16979 if (size != TYPE_LENGTH (field->type ()))
16980 FIELD_BITSIZE (*field) = 8 * size;
16981
16982 return true;
16983 }
16984
16985 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16986 some kinds of Ada arrays:
16987
16988 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16989 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16990 <11e0> DW_AT_data_location: 2 byte block: 97 6
16991 (DW_OP_push_object_address; DW_OP_deref)
16992 <11e3> DW_AT_type : <0x1173>
16993 <11e7> DW_AT_sibling : <0x1201>
16994 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16995 <11ec> DW_AT_type : <0x1206>
16996 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16997 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16998 DW_OP_deref_size: 4)
16999 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
17000 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
17001 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
17002
17003 This actually represents a "thick pointer", which is a structure
17004 with two elements: one that is a pointer to the array data, and one
17005 that is a pointer to another structure; this second structure holds
17006 the array bounds.
17007
17008 This returns a new type on success, or nullptr if this didn't
17009 recognize the type. */
17010
17011 static struct type *
17012 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
17013 struct type *type)
17014 {
17015 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
17016 /* So far we've only seen this with block form. */
17017 if (attr == nullptr || !attr->form_is_block ())
17018 return nullptr;
17019
17020 /* Note that this will fail if the structure layout is changed by
17021 the compiler. However, we have no good way to recognize some
17022 other layout, because we don't know what expression the compiler
17023 might choose to emit should this happen. */
17024 struct dwarf_block *blk = attr->as_block ();
17025 if (blk->size != 2
17026 || blk->data[0] != DW_OP_push_object_address
17027 || blk->data[1] != DW_OP_deref)
17028 return nullptr;
17029
17030 int bounds_offset = -1;
17031 int max_align = -1;
17032 std::vector<struct field> range_fields;
17033 for (struct die_info *child_die = die->child;
17034 child_die;
17035 child_die = child_die->sibling)
17036 {
17037 if (child_die->tag == DW_TAG_subrange_type)
17038 {
17039 struct type *underlying = read_subrange_index_type (child_die, cu);
17040
17041 int this_align = type_align (underlying);
17042 if (this_align > max_align)
17043 max_align = this_align;
17044
17045 range_fields.emplace_back ();
17046 range_fields.emplace_back ();
17047
17048 struct field &lower = range_fields[range_fields.size () - 2];
17049 struct field &upper = range_fields[range_fields.size () - 1];
17050
17051 lower.set_type (underlying);
17052 FIELD_ARTIFICIAL (lower) = 1;
17053
17054 upper.set_type (underlying);
17055 FIELD_ARTIFICIAL (upper) = 1;
17056
17057 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17058 &bounds_offset, &lower, cu)
17059 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17060 &bounds_offset, &upper, cu))
17061 return nullptr;
17062 }
17063 }
17064
17065 /* This shouldn't really happen, but double-check that we found
17066 where the bounds are stored. */
17067 if (bounds_offset == -1)
17068 return nullptr;
17069
17070 struct objfile *objfile = cu->per_objfile->objfile;
17071 for (int i = 0; i < range_fields.size (); i += 2)
17072 {
17073 char name[20];
17074
17075 /* Set the name of each field in the bounds. */
17076 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17077 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17078 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17079 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17080 }
17081
17082 struct type *bounds = alloc_type (objfile);
17083 bounds->set_code (TYPE_CODE_STRUCT);
17084
17085 bounds->set_num_fields (range_fields.size ());
17086 bounds->set_fields
17087 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17088 * sizeof (struct field))));
17089 memcpy (bounds->fields (), range_fields.data (),
17090 bounds->num_fields () * sizeof (struct field));
17091
17092 int last_fieldno = range_fields.size () - 1;
17093 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17094 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17095 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17096
17097 /* Rewrite the existing array type in place. Specifically, we
17098 remove any dynamic properties we might have read, and we replace
17099 the index types. */
17100 struct type *iter = type;
17101 for (int i = 0; i < range_fields.size (); i += 2)
17102 {
17103 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17104 iter->main_type->dyn_prop_list = nullptr;
17105 iter->set_index_type
17106 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17107 iter = TYPE_TARGET_TYPE (iter);
17108 }
17109
17110 struct type *result = alloc_type (objfile);
17111 result->set_code (TYPE_CODE_STRUCT);
17112
17113 result->set_num_fields (2);
17114 result->set_fields
17115 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17116 * sizeof (struct field))));
17117
17118 /* The names are chosen to coincide with what the compiler does with
17119 -fgnat-encodings=all, which the Ada code in gdb already
17120 understands. */
17121 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17122 result->field (0).set_type (lookup_pointer_type (type));
17123
17124 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17125 result->field (1).set_type (lookup_pointer_type (bounds));
17126 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17127
17128 result->set_name (type->name ());
17129 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17130 + TYPE_LENGTH (result->field (1).type ()));
17131
17132 return result;
17133 }
17134
17135 /* Extract all information from a DW_TAG_array_type DIE and put it in
17136 the DIE's type field. For now, this only handles one dimensional
17137 arrays. */
17138
17139 static struct type *
17140 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
17141 {
17142 struct objfile *objfile = cu->per_objfile->objfile;
17143 struct die_info *child_die;
17144 struct type *type;
17145 struct type *element_type, *range_type, *index_type;
17146 struct attribute *attr;
17147 const char *name;
17148 struct dynamic_prop *byte_stride_prop = NULL;
17149 unsigned int bit_stride = 0;
17150
17151 element_type = die_type (die, cu);
17152
17153 /* The die_type call above may have already set the type for this DIE. */
17154 type = get_die_type (die, cu);
17155 if (type)
17156 return type;
17157
17158 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17159 if (attr != NULL)
17160 {
17161 int stride_ok;
17162 struct type *prop_type = cu->addr_sized_int_type (false);
17163
17164 byte_stride_prop
17165 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
17166 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17167 prop_type);
17168 if (!stride_ok)
17169 {
17170 complaint (_("unable to read array DW_AT_byte_stride "
17171 " - DIE at %s [in module %s]"),
17172 sect_offset_str (die->sect_off),
17173 objfile_name (cu->per_objfile->objfile));
17174 /* Ignore this attribute. We will likely not be able to print
17175 arrays of this type correctly, but there is little we can do
17176 to help if we cannot read the attribute's value. */
17177 byte_stride_prop = NULL;
17178 }
17179 }
17180
17181 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17182 if (attr != NULL)
17183 bit_stride = attr->constant_value (0);
17184
17185 /* Irix 6.2 native cc creates array types without children for
17186 arrays with unspecified length. */
17187 if (die->child == NULL)
17188 {
17189 index_type = objfile_type (objfile)->builtin_int;
17190 range_type = create_static_range_type (NULL, index_type, 0, -1);
17191 type = create_array_type_with_stride (NULL, element_type, range_type,
17192 byte_stride_prop, bit_stride);
17193 return set_die_type (die, type, cu);
17194 }
17195
17196 std::vector<struct type *> range_types;
17197 child_die = die->child;
17198 while (child_die && child_die->tag)
17199 {
17200 if (child_die->tag == DW_TAG_subrange_type)
17201 {
17202 struct type *child_type = read_type_die (child_die, cu);
17203
17204 if (child_type != NULL)
17205 {
17206 /* The range type was succesfully read. Save it for the
17207 array type creation. */
17208 range_types.push_back (child_type);
17209 }
17210 }
17211 child_die = child_die->sibling;
17212 }
17213
17214 /* Dwarf2 dimensions are output from left to right, create the
17215 necessary array types in backwards order. */
17216
17217 type = element_type;
17218
17219 if (read_array_order (die, cu) == DW_ORD_col_major)
17220 {
17221 int i = 0;
17222
17223 while (i < range_types.size ())
17224 {
17225 type = create_array_type_with_stride (NULL, type, range_types[i++],
17226 byte_stride_prop, bit_stride);
17227 bit_stride = 0;
17228 byte_stride_prop = nullptr;
17229 }
17230 }
17231 else
17232 {
17233 size_t ndim = range_types.size ();
17234 while (ndim-- > 0)
17235 {
17236 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17237 byte_stride_prop, bit_stride);
17238 bit_stride = 0;
17239 byte_stride_prop = nullptr;
17240 }
17241 }
17242
17243 /* Understand Dwarf2 support for vector types (like they occur on
17244 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17245 array type. This is not part of the Dwarf2/3 standard yet, but a
17246 custom vendor extension. The main difference between a regular
17247 array and the vector variant is that vectors are passed by value
17248 to functions. */
17249 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
17250 if (attr != nullptr)
17251 make_vector_type (type);
17252
17253 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17254 implementation may choose to implement triple vectors using this
17255 attribute. */
17256 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17257 if (attr != nullptr && attr->form_is_unsigned ())
17258 {
17259 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17260 TYPE_LENGTH (type) = attr->as_unsigned ();
17261 else
17262 complaint (_("DW_AT_byte_size for array type smaller "
17263 "than the total size of elements"));
17264 }
17265
17266 name = dwarf2_name (die, cu);
17267 if (name)
17268 type->set_name (name);
17269
17270 maybe_set_alignment (cu, die, type);
17271
17272 struct type *replacement_type = nullptr;
17273 if (cu->language == language_ada)
17274 {
17275 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17276 if (replacement_type != nullptr)
17277 type = replacement_type;
17278 }
17279
17280 /* Install the type in the die. */
17281 set_die_type (die, type, cu, replacement_type != nullptr);
17282
17283 /* set_die_type should be already done. */
17284 set_descriptive_type (type, die, cu);
17285
17286 return type;
17287 }
17288
17289 static enum dwarf_array_dim_ordering
17290 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
17291 {
17292 struct attribute *attr;
17293
17294 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17295
17296 if (attr != nullptr)
17297 {
17298 LONGEST val = attr->constant_value (-1);
17299 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17300 return (enum dwarf_array_dim_ordering) val;
17301 }
17302
17303 /* GNU F77 is a special case, as at 08/2004 array type info is the
17304 opposite order to the dwarf2 specification, but data is still
17305 laid out as per normal fortran.
17306
17307 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17308 version checking. */
17309
17310 if (cu->language == language_fortran
17311 && cu->producer && strstr (cu->producer, "GNU F77"))
17312 {
17313 return DW_ORD_row_major;
17314 }
17315
17316 switch (cu->language_defn->array_ordering ())
17317 {
17318 case array_column_major:
17319 return DW_ORD_col_major;
17320 case array_row_major:
17321 default:
17322 return DW_ORD_row_major;
17323 };
17324 }
17325
17326 /* Extract all information from a DW_TAG_set_type DIE and put it in
17327 the DIE's type field. */
17328
17329 static struct type *
17330 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17331 {
17332 struct type *domain_type, *set_type;
17333 struct attribute *attr;
17334
17335 domain_type = die_type (die, cu);
17336
17337 /* The die_type call above may have already set the type for this DIE. */
17338 set_type = get_die_type (die, cu);
17339 if (set_type)
17340 return set_type;
17341
17342 set_type = create_set_type (NULL, domain_type);
17343
17344 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17345 if (attr != nullptr && attr->form_is_unsigned ())
17346 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17347
17348 maybe_set_alignment (cu, die, set_type);
17349
17350 return set_die_type (die, set_type, cu);
17351 }
17352
17353 /* A helper for read_common_block that creates a locexpr baton.
17354 SYM is the symbol which we are marking as computed.
17355 COMMON_DIE is the DIE for the common block.
17356 COMMON_LOC is the location expression attribute for the common
17357 block itself.
17358 MEMBER_LOC is the location expression attribute for the particular
17359 member of the common block that we are processing.
17360 CU is the CU from which the above come. */
17361
17362 static void
17363 mark_common_block_symbol_computed (struct symbol *sym,
17364 struct die_info *common_die,
17365 struct attribute *common_loc,
17366 struct attribute *member_loc,
17367 struct dwarf2_cu *cu)
17368 {
17369 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17370 struct objfile *objfile = per_objfile->objfile;
17371 struct dwarf2_locexpr_baton *baton;
17372 gdb_byte *ptr;
17373 unsigned int cu_off;
17374 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17375 LONGEST offset = 0;
17376
17377 gdb_assert (common_loc && member_loc);
17378 gdb_assert (common_loc->form_is_block ());
17379 gdb_assert (member_loc->form_is_block ()
17380 || member_loc->form_is_constant ());
17381
17382 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17383 baton->per_objfile = per_objfile;
17384 baton->per_cu = cu->per_cu;
17385 gdb_assert (baton->per_cu);
17386
17387 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17388
17389 if (member_loc->form_is_constant ())
17390 {
17391 offset = member_loc->constant_value (0);
17392 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17393 }
17394 else
17395 baton->size += member_loc->as_block ()->size;
17396
17397 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17398 baton->data = ptr;
17399
17400 *ptr++ = DW_OP_call4;
17401 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17402 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17403 ptr += 4;
17404
17405 if (member_loc->form_is_constant ())
17406 {
17407 *ptr++ = DW_OP_addr;
17408 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17409 ptr += cu->header.addr_size;
17410 }
17411 else
17412 {
17413 /* We have to copy the data here, because DW_OP_call4 will only
17414 use a DW_AT_location attribute. */
17415 struct dwarf_block *block = member_loc->as_block ();
17416 memcpy (ptr, block->data, block->size);
17417 ptr += block->size;
17418 }
17419
17420 *ptr++ = DW_OP_plus;
17421 gdb_assert (ptr - baton->data == baton->size);
17422
17423 SYMBOL_LOCATION_BATON (sym) = baton;
17424 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17425 }
17426
17427 /* Create appropriate locally-scoped variables for all the
17428 DW_TAG_common_block entries. Also create a struct common_block
17429 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17430 is used to separate the common blocks name namespace from regular
17431 variable names. */
17432
17433 static void
17434 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17435 {
17436 struct attribute *attr;
17437
17438 attr = dwarf2_attr (die, DW_AT_location, cu);
17439 if (attr != nullptr)
17440 {
17441 /* Support the .debug_loc offsets. */
17442 if (attr->form_is_block ())
17443 {
17444 /* Ok. */
17445 }
17446 else if (attr->form_is_section_offset ())
17447 {
17448 dwarf2_complex_location_expr_complaint ();
17449 attr = NULL;
17450 }
17451 else
17452 {
17453 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17454 "common block member");
17455 attr = NULL;
17456 }
17457 }
17458
17459 if (die->child != NULL)
17460 {
17461 struct objfile *objfile = cu->per_objfile->objfile;
17462 struct die_info *child_die;
17463 size_t n_entries = 0, size;
17464 struct common_block *common_block;
17465 struct symbol *sym;
17466
17467 for (child_die = die->child;
17468 child_die && child_die->tag;
17469 child_die = child_die->sibling)
17470 ++n_entries;
17471
17472 size = (sizeof (struct common_block)
17473 + (n_entries - 1) * sizeof (struct symbol *));
17474 common_block
17475 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17476 size);
17477 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17478 common_block->n_entries = 0;
17479
17480 for (child_die = die->child;
17481 child_die && child_die->tag;
17482 child_die = child_die->sibling)
17483 {
17484 /* Create the symbol in the DW_TAG_common_block block in the current
17485 symbol scope. */
17486 sym = new_symbol (child_die, NULL, cu);
17487 if (sym != NULL)
17488 {
17489 struct attribute *member_loc;
17490
17491 common_block->contents[common_block->n_entries++] = sym;
17492
17493 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17494 cu);
17495 if (member_loc)
17496 {
17497 /* GDB has handled this for a long time, but it is
17498 not specified by DWARF. It seems to have been
17499 emitted by gfortran at least as recently as:
17500 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17501 complaint (_("Variable in common block has "
17502 "DW_AT_data_member_location "
17503 "- DIE at %s [in module %s]"),
17504 sect_offset_str (child_die->sect_off),
17505 objfile_name (objfile));
17506
17507 if (member_loc->form_is_section_offset ())
17508 dwarf2_complex_location_expr_complaint ();
17509 else if (member_loc->form_is_constant ()
17510 || member_loc->form_is_block ())
17511 {
17512 if (attr != nullptr)
17513 mark_common_block_symbol_computed (sym, die, attr,
17514 member_loc, cu);
17515 }
17516 else
17517 dwarf2_complex_location_expr_complaint ();
17518 }
17519 }
17520 }
17521
17522 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17523 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17524 }
17525 }
17526
17527 /* Create a type for a C++ namespace. */
17528
17529 static struct type *
17530 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17531 {
17532 struct objfile *objfile = cu->per_objfile->objfile;
17533 const char *previous_prefix, *name;
17534 int is_anonymous;
17535 struct type *type;
17536
17537 /* For extensions, reuse the type of the original namespace. */
17538 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17539 {
17540 struct die_info *ext_die;
17541 struct dwarf2_cu *ext_cu = cu;
17542
17543 ext_die = dwarf2_extension (die, &ext_cu);
17544 type = read_type_die (ext_die, ext_cu);
17545
17546 /* EXT_CU may not be the same as CU.
17547 Ensure TYPE is recorded with CU in die_type_hash. */
17548 return set_die_type (die, type, cu);
17549 }
17550
17551 name = namespace_name (die, &is_anonymous, cu);
17552
17553 /* Now build the name of the current namespace. */
17554
17555 previous_prefix = determine_prefix (die, cu);
17556 if (previous_prefix[0] != '\0')
17557 name = typename_concat (&objfile->objfile_obstack,
17558 previous_prefix, name, 0, cu);
17559
17560 /* Create the type. */
17561 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17562
17563 return set_die_type (die, type, cu);
17564 }
17565
17566 /* Read a namespace scope. */
17567
17568 static void
17569 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17570 {
17571 struct objfile *objfile = cu->per_objfile->objfile;
17572 int is_anonymous;
17573
17574 /* Add a symbol associated to this if we haven't seen the namespace
17575 before. Also, add a using directive if it's an anonymous
17576 namespace. */
17577
17578 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17579 {
17580 struct type *type;
17581
17582 type = read_type_die (die, cu);
17583 new_symbol (die, type, cu);
17584
17585 namespace_name (die, &is_anonymous, cu);
17586 if (is_anonymous)
17587 {
17588 const char *previous_prefix = determine_prefix (die, cu);
17589
17590 std::vector<const char *> excludes;
17591 add_using_directive (using_directives (cu),
17592 previous_prefix, type->name (), NULL,
17593 NULL, excludes, 0, &objfile->objfile_obstack);
17594 }
17595 }
17596
17597 if (die->child != NULL)
17598 {
17599 struct die_info *child_die = die->child;
17600
17601 while (child_die && child_die->tag)
17602 {
17603 process_die (child_die, cu);
17604 child_die = child_die->sibling;
17605 }
17606 }
17607 }
17608
17609 /* Read a Fortran module as type. This DIE can be only a declaration used for
17610 imported module. Still we need that type as local Fortran "use ... only"
17611 declaration imports depend on the created type in determine_prefix. */
17612
17613 static struct type *
17614 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17615 {
17616 struct objfile *objfile = cu->per_objfile->objfile;
17617 const char *module_name;
17618 struct type *type;
17619
17620 module_name = dwarf2_name (die, cu);
17621 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17622
17623 return set_die_type (die, type, cu);
17624 }
17625
17626 /* Read a Fortran module. */
17627
17628 static void
17629 read_module (struct die_info *die, struct dwarf2_cu *cu)
17630 {
17631 struct die_info *child_die = die->child;
17632 struct type *type;
17633
17634 type = read_type_die (die, cu);
17635 new_symbol (die, type, cu);
17636
17637 while (child_die && child_die->tag)
17638 {
17639 process_die (child_die, cu);
17640 child_die = child_die->sibling;
17641 }
17642 }
17643
17644 /* Return the name of the namespace represented by DIE. Set
17645 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17646 namespace. */
17647
17648 static const char *
17649 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17650 {
17651 struct die_info *current_die;
17652 const char *name = NULL;
17653
17654 /* Loop through the extensions until we find a name. */
17655
17656 for (current_die = die;
17657 current_die != NULL;
17658 current_die = dwarf2_extension (die, &cu))
17659 {
17660 /* We don't use dwarf2_name here so that we can detect the absence
17661 of a name -> anonymous namespace. */
17662 name = dwarf2_string_attr (die, DW_AT_name, cu);
17663
17664 if (name != NULL)
17665 break;
17666 }
17667
17668 /* Is it an anonymous namespace? */
17669
17670 *is_anonymous = (name == NULL);
17671 if (*is_anonymous)
17672 name = CP_ANONYMOUS_NAMESPACE_STR;
17673
17674 return name;
17675 }
17676
17677 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17678 the user defined type vector. */
17679
17680 static struct type *
17681 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17682 {
17683 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17684 struct comp_unit_head *cu_header = &cu->header;
17685 struct type *type;
17686 struct attribute *attr_byte_size;
17687 struct attribute *attr_address_class;
17688 int byte_size, addr_class;
17689 struct type *target_type;
17690
17691 target_type = die_type (die, cu);
17692
17693 /* The die_type call above may have already set the type for this DIE. */
17694 type = get_die_type (die, cu);
17695 if (type)
17696 return type;
17697
17698 type = lookup_pointer_type (target_type);
17699
17700 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17701 if (attr_byte_size)
17702 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17703 else
17704 byte_size = cu_header->addr_size;
17705
17706 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17707 if (attr_address_class)
17708 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17709 else
17710 addr_class = DW_ADDR_none;
17711
17712 ULONGEST alignment = get_alignment (cu, die);
17713
17714 /* If the pointer size, alignment, or address class is different
17715 than the default, create a type variant marked as such and set
17716 the length accordingly. */
17717 if (TYPE_LENGTH (type) != byte_size
17718 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17719 && alignment != TYPE_RAW_ALIGN (type))
17720 || addr_class != DW_ADDR_none)
17721 {
17722 if (gdbarch_address_class_type_flags_p (gdbarch))
17723 {
17724 type_instance_flags type_flags
17725 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17726 addr_class);
17727 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17728 == 0);
17729 type = make_type_with_address_space (type, type_flags);
17730 }
17731 else if (TYPE_LENGTH (type) != byte_size)
17732 {
17733 complaint (_("invalid pointer size %d"), byte_size);
17734 }
17735 else if (TYPE_RAW_ALIGN (type) != alignment)
17736 {
17737 complaint (_("Invalid DW_AT_alignment"
17738 " - DIE at %s [in module %s]"),
17739 sect_offset_str (die->sect_off),
17740 objfile_name (cu->per_objfile->objfile));
17741 }
17742 else
17743 {
17744 /* Should we also complain about unhandled address classes? */
17745 }
17746 }
17747
17748 TYPE_LENGTH (type) = byte_size;
17749 set_type_align (type, alignment);
17750 return set_die_type (die, type, cu);
17751 }
17752
17753 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17754 the user defined type vector. */
17755
17756 static struct type *
17757 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17758 {
17759 struct type *type;
17760 struct type *to_type;
17761 struct type *domain;
17762
17763 to_type = die_type (die, cu);
17764 domain = die_containing_type (die, cu);
17765
17766 /* The calls above may have already set the type for this DIE. */
17767 type = get_die_type (die, cu);
17768 if (type)
17769 return type;
17770
17771 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17772 type = lookup_methodptr_type (to_type);
17773 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17774 {
17775 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17776
17777 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17778 to_type->fields (), to_type->num_fields (),
17779 to_type->has_varargs ());
17780 type = lookup_methodptr_type (new_type);
17781 }
17782 else
17783 type = lookup_memberptr_type (to_type, domain);
17784
17785 return set_die_type (die, type, cu);
17786 }
17787
17788 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17789 the user defined type vector. */
17790
17791 static struct type *
17792 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17793 enum type_code refcode)
17794 {
17795 struct comp_unit_head *cu_header = &cu->header;
17796 struct type *type, *target_type;
17797 struct attribute *attr;
17798
17799 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17800
17801 target_type = die_type (die, cu);
17802
17803 /* The die_type call above may have already set the type for this DIE. */
17804 type = get_die_type (die, cu);
17805 if (type)
17806 return type;
17807
17808 type = lookup_reference_type (target_type, refcode);
17809 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17810 if (attr != nullptr)
17811 {
17812 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17813 }
17814 else
17815 {
17816 TYPE_LENGTH (type) = cu_header->addr_size;
17817 }
17818 maybe_set_alignment (cu, die, type);
17819 return set_die_type (die, type, cu);
17820 }
17821
17822 /* Add the given cv-qualifiers to the element type of the array. GCC
17823 outputs DWARF type qualifiers that apply to an array, not the
17824 element type. But GDB relies on the array element type to carry
17825 the cv-qualifiers. This mimics section 6.7.3 of the C99
17826 specification. */
17827
17828 static struct type *
17829 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17830 struct type *base_type, int cnst, int voltl)
17831 {
17832 struct type *el_type, *inner_array;
17833
17834 base_type = copy_type (base_type);
17835 inner_array = base_type;
17836
17837 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17838 {
17839 TYPE_TARGET_TYPE (inner_array) =
17840 copy_type (TYPE_TARGET_TYPE (inner_array));
17841 inner_array = TYPE_TARGET_TYPE (inner_array);
17842 }
17843
17844 el_type = TYPE_TARGET_TYPE (inner_array);
17845 cnst |= TYPE_CONST (el_type);
17846 voltl |= TYPE_VOLATILE (el_type);
17847 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17848
17849 return set_die_type (die, base_type, cu);
17850 }
17851
17852 static struct type *
17853 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17854 {
17855 struct type *base_type, *cv_type;
17856
17857 base_type = die_type (die, cu);
17858
17859 /* The die_type call above may have already set the type for this DIE. */
17860 cv_type = get_die_type (die, cu);
17861 if (cv_type)
17862 return cv_type;
17863
17864 /* In case the const qualifier is applied to an array type, the element type
17865 is so qualified, not the array type (section 6.7.3 of C99). */
17866 if (base_type->code () == TYPE_CODE_ARRAY)
17867 return add_array_cv_type (die, cu, base_type, 1, 0);
17868
17869 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17870 return set_die_type (die, cv_type, cu);
17871 }
17872
17873 static struct type *
17874 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17875 {
17876 struct type *base_type, *cv_type;
17877
17878 base_type = die_type (die, cu);
17879
17880 /* The die_type call above may have already set the type for this DIE. */
17881 cv_type = get_die_type (die, cu);
17882 if (cv_type)
17883 return cv_type;
17884
17885 /* In case the volatile qualifier is applied to an array type, the
17886 element type is so qualified, not the array type (section 6.7.3
17887 of C99). */
17888 if (base_type->code () == TYPE_CODE_ARRAY)
17889 return add_array_cv_type (die, cu, base_type, 0, 1);
17890
17891 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17892 return set_die_type (die, cv_type, cu);
17893 }
17894
17895 /* Handle DW_TAG_restrict_type. */
17896
17897 static struct type *
17898 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17899 {
17900 struct type *base_type, *cv_type;
17901
17902 base_type = die_type (die, cu);
17903
17904 /* The die_type call above may have already set the type for this DIE. */
17905 cv_type = get_die_type (die, cu);
17906 if (cv_type)
17907 return cv_type;
17908
17909 cv_type = make_restrict_type (base_type);
17910 return set_die_type (die, cv_type, cu);
17911 }
17912
17913 /* Handle DW_TAG_atomic_type. */
17914
17915 static struct type *
17916 read_tag_atomic_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 cv_type = make_atomic_type (base_type);
17928 return set_die_type (die, cv_type, cu);
17929 }
17930
17931 /* Extract all information from a DW_TAG_string_type DIE and add to
17932 the user defined type vector. It isn't really a user defined type,
17933 but it behaves like one, with other DIE's using an AT_user_def_type
17934 attribute to reference it. */
17935
17936 static struct type *
17937 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17938 {
17939 struct objfile *objfile = cu->per_objfile->objfile;
17940 struct gdbarch *gdbarch = objfile->arch ();
17941 struct type *type, *range_type, *index_type, *char_type;
17942 struct attribute *attr;
17943 struct dynamic_prop prop;
17944 bool length_is_constant = true;
17945 LONGEST length;
17946
17947 /* There are a couple of places where bit sizes might be made use of
17948 when parsing a DW_TAG_string_type, however, no producer that we know
17949 of make use of these. Handling bit sizes that are a multiple of the
17950 byte size is easy enough, but what about other bit sizes? Lets deal
17951 with that problem when we have to. Warn about these attributes being
17952 unsupported, then parse the type and ignore them like we always
17953 have. */
17954 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17955 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17956 {
17957 static bool warning_printed = false;
17958 if (!warning_printed)
17959 {
17960 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17961 "currently supported on DW_TAG_string_type."));
17962 warning_printed = true;
17963 }
17964 }
17965
17966 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17967 if (attr != nullptr && !attr->form_is_constant ())
17968 {
17969 /* The string length describes the location at which the length of
17970 the string can be found. The size of the length field can be
17971 specified with one of the attributes below. */
17972 struct type *prop_type;
17973 struct attribute *len
17974 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17975 if (len == nullptr)
17976 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17977 if (len != nullptr && len->form_is_constant ())
17978 {
17979 /* Pass 0 as the default as we know this attribute is constant
17980 and the default value will not be returned. */
17981 LONGEST sz = len->constant_value (0);
17982 prop_type = cu->per_objfile->int_type (sz, true);
17983 }
17984 else
17985 {
17986 /* If the size is not specified then we assume it is the size of
17987 an address on this target. */
17988 prop_type = cu->addr_sized_int_type (true);
17989 }
17990
17991 /* Convert the attribute into a dynamic property. */
17992 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17993 length = 1;
17994 else
17995 length_is_constant = false;
17996 }
17997 else if (attr != nullptr)
17998 {
17999 /* This DW_AT_string_length just contains the length with no
18000 indirection. There's no need to create a dynamic property in this
18001 case. Pass 0 for the default value as we know it will not be
18002 returned in this case. */
18003 length = attr->constant_value (0);
18004 }
18005 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
18006 {
18007 /* We don't currently support non-constant byte sizes for strings. */
18008 length = attr->constant_value (1);
18009 }
18010 else
18011 {
18012 /* Use 1 as a fallback length if we have nothing else. */
18013 length = 1;
18014 }
18015
18016 index_type = objfile_type (objfile)->builtin_int;
18017 if (length_is_constant)
18018 range_type = create_static_range_type (NULL, index_type, 1, length);
18019 else
18020 {
18021 struct dynamic_prop low_bound;
18022
18023 low_bound.set_const_val (1);
18024 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18025 }
18026 char_type = language_string_char_type (cu->language_defn, gdbarch);
18027 type = create_string_type (NULL, char_type, range_type);
18028
18029 return set_die_type (die, type, cu);
18030 }
18031
18032 /* Assuming that DIE corresponds to a function, returns nonzero
18033 if the function is prototyped. */
18034
18035 static int
18036 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18037 {
18038 struct attribute *attr;
18039
18040 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
18041 if (attr && attr->as_boolean ())
18042 return 1;
18043
18044 /* The DWARF standard implies that the DW_AT_prototyped attribute
18045 is only meaningful for C, but the concept also extends to other
18046 languages that allow unprototyped functions (Eg: Objective C).
18047 For all other languages, assume that functions are always
18048 prototyped. */
18049 if (cu->language != language_c
18050 && cu->language != language_objc
18051 && cu->language != language_opencl)
18052 return 1;
18053
18054 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18055 prototyped and unprototyped functions; default to prototyped,
18056 since that is more common in modern code (and RealView warns
18057 about unprototyped functions). */
18058 if (producer_is_realview (cu->producer))
18059 return 1;
18060
18061 return 0;
18062 }
18063
18064 /* Handle DIES due to C code like:
18065
18066 struct foo
18067 {
18068 int (*funcp)(int a, long l);
18069 int b;
18070 };
18071
18072 ('funcp' generates a DW_TAG_subroutine_type DIE). */
18073
18074 static struct type *
18075 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
18076 {
18077 struct objfile *objfile = cu->per_objfile->objfile;
18078 struct type *type; /* Type that this function returns. */
18079 struct type *ftype; /* Function that returns above type. */
18080 struct attribute *attr;
18081
18082 type = die_type (die, cu);
18083
18084 /* The die_type call above may have already set the type for this DIE. */
18085 ftype = get_die_type (die, cu);
18086 if (ftype)
18087 return ftype;
18088
18089 ftype = lookup_function_type (type);
18090
18091 if (prototyped_function_p (die, cu))
18092 ftype->set_is_prototyped (true);
18093
18094 /* Store the calling convention in the type if it's available in
18095 the subroutine die. Otherwise set the calling convention to
18096 the default value DW_CC_normal. */
18097 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
18098 if (attr != nullptr
18099 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
18100 TYPE_CALLING_CONVENTION (ftype)
18101 = (enum dwarf_calling_convention) attr->constant_value (0);
18102 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18103 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18104 else
18105 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
18106
18107 /* Record whether the function returns normally to its caller or not
18108 if the DWARF producer set that information. */
18109 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
18110 if (attr && attr->as_boolean ())
18111 TYPE_NO_RETURN (ftype) = 1;
18112
18113 /* We need to add the subroutine type to the die immediately so
18114 we don't infinitely recurse when dealing with parameters
18115 declared as the same subroutine type. */
18116 set_die_type (die, ftype, cu);
18117
18118 if (die->child != NULL)
18119 {
18120 struct type *void_type = objfile_type (objfile)->builtin_void;
18121 struct die_info *child_die;
18122 int nparams, iparams;
18123
18124 /* Count the number of parameters.
18125 FIXME: GDB currently ignores vararg functions, but knows about
18126 vararg member functions. */
18127 nparams = 0;
18128 child_die = die->child;
18129 while (child_die && child_die->tag)
18130 {
18131 if (child_die->tag == DW_TAG_formal_parameter)
18132 nparams++;
18133 else if (child_die->tag == DW_TAG_unspecified_parameters)
18134 ftype->set_has_varargs (true);
18135
18136 child_die = child_die->sibling;
18137 }
18138
18139 /* Allocate storage for parameters and fill them in. */
18140 ftype->set_num_fields (nparams);
18141 ftype->set_fields
18142 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
18143
18144 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18145 even if we error out during the parameters reading below. */
18146 for (iparams = 0; iparams < nparams; iparams++)
18147 ftype->field (iparams).set_type (void_type);
18148
18149 iparams = 0;
18150 child_die = die->child;
18151 while (child_die && child_die->tag)
18152 {
18153 if (child_die->tag == DW_TAG_formal_parameter)
18154 {
18155 struct type *arg_type;
18156
18157 /* DWARF version 2 has no clean way to discern C++
18158 static and non-static member functions. G++ helps
18159 GDB by marking the first parameter for non-static
18160 member functions (which is the this pointer) as
18161 artificial. We pass this information to
18162 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18163
18164 DWARF version 3 added DW_AT_object_pointer, which GCC
18165 4.5 does not yet generate. */
18166 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
18167 if (attr != nullptr)
18168 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
18169 else
18170 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
18171 arg_type = die_type (child_die, cu);
18172
18173 /* RealView does not mark THIS as const, which the testsuite
18174 expects. GCC marks THIS as const in method definitions,
18175 but not in the class specifications (GCC PR 43053). */
18176 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18177 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18178 {
18179 int is_this = 0;
18180 struct dwarf2_cu *arg_cu = cu;
18181 const char *name = dwarf2_name (child_die, cu);
18182
18183 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
18184 if (attr != nullptr)
18185 {
18186 /* If the compiler emits this, use it. */
18187 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18188 is_this = 1;
18189 }
18190 else if (name && strcmp (name, "this") == 0)
18191 /* Function definitions will have the argument names. */
18192 is_this = 1;
18193 else if (name == NULL && iparams == 0)
18194 /* Declarations may not have the names, so like
18195 elsewhere in GDB, assume an artificial first
18196 argument is "this". */
18197 is_this = 1;
18198
18199 if (is_this)
18200 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18201 arg_type, 0);
18202 }
18203
18204 ftype->field (iparams).set_type (arg_type);
18205 iparams++;
18206 }
18207 child_die = child_die->sibling;
18208 }
18209 }
18210
18211 return ftype;
18212 }
18213
18214 static struct type *
18215 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
18216 {
18217 struct objfile *objfile = cu->per_objfile->objfile;
18218 const char *name = NULL;
18219 struct type *this_type, *target_type;
18220
18221 name = dwarf2_full_name (NULL, die, cu);
18222 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
18223 this_type->set_target_is_stub (true);
18224 set_die_type (die, this_type, cu);
18225 target_type = die_type (die, cu);
18226 if (target_type != this_type)
18227 TYPE_TARGET_TYPE (this_type) = target_type;
18228 else
18229 {
18230 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18231 spec and cause infinite loops in GDB. */
18232 complaint (_("Self-referential DW_TAG_typedef "
18233 "- DIE at %s [in module %s]"),
18234 sect_offset_str (die->sect_off), objfile_name (objfile));
18235 TYPE_TARGET_TYPE (this_type) = NULL;
18236 }
18237 if (name == NULL)
18238 {
18239 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18240 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18241 Handle these by just returning the target type, rather than
18242 constructing an anonymous typedef type and trying to handle this
18243 elsewhere. */
18244 set_die_type (die, target_type, cu);
18245 return target_type;
18246 }
18247 return this_type;
18248 }
18249
18250 /* Helper for get_dwarf2_rational_constant that computes the value of
18251 a given gmp_mpz given an attribute. */
18252
18253 static void
18254 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18255 {
18256 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18257 location expression that pushes an implicit value. */
18258 if (attr->form == DW_FORM_exprloc)
18259 {
18260 dwarf_block *blk = attr->as_block ();
18261 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18262 {
18263 uint64_t len;
18264 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18265 blk->data + blk->size,
18266 &len);
18267 if (ptr - blk->data + len <= blk->size)
18268 {
18269 mpz_import (value->val, len,
18270 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18271 1, 0, 0, ptr);
18272 return;
18273 }
18274 }
18275
18276 /* On failure set it to 1. */
18277 *value = gdb_mpz (1);
18278 }
18279 else if (attr->form_is_block ())
18280 {
18281 dwarf_block *blk = attr->as_block ();
18282 mpz_import (value->val, blk->size,
18283 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18284 1, 0, 0, blk->data);
18285 }
18286 else
18287 *value = gdb_mpz (attr->constant_value (1));
18288 }
18289
18290 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18291 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18292
18293 If the numerator and/or numerator attribute is missing,
18294 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18295 untouched. */
18296
18297 static void
18298 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18299 gdb_mpz *numerator, gdb_mpz *denominator)
18300 {
18301 struct attribute *num_attr, *denom_attr;
18302
18303 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18304 if (num_attr == nullptr)
18305 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18306 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18307
18308 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18309 if (denom_attr == nullptr)
18310 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18311 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18312
18313 if (num_attr == nullptr || denom_attr == nullptr)
18314 return;
18315
18316 get_mpz (cu, numerator, num_attr);
18317 get_mpz (cu, denominator, denom_attr);
18318 }
18319
18320 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18321 rational constant, rather than a signed one.
18322
18323 If the rational constant has a negative value, a complaint
18324 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18325
18326 static void
18327 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18328 struct dwarf2_cu *cu,
18329 gdb_mpz *numerator,
18330 gdb_mpz *denominator)
18331 {
18332 gdb_mpz num (1);
18333 gdb_mpz denom (1);
18334
18335 get_dwarf2_rational_constant (die, cu, &num, &denom);
18336 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18337 {
18338 mpz_neg (num.val, num.val);
18339 mpz_neg (denom.val, denom.val);
18340 }
18341 else if (mpz_sgn (num.val) == -1)
18342 {
18343 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18344 " in DIE at %s"),
18345 sect_offset_str (die->sect_off));
18346 return;
18347 }
18348 else if (mpz_sgn (denom.val) == -1)
18349 {
18350 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18351 " in DIE at %s"),
18352 sect_offset_str (die->sect_off));
18353 return;
18354 }
18355
18356 *numerator = std::move (num);
18357 *denominator = std::move (denom);
18358 }
18359
18360 /* Assuming DIE corresponds to a fixed point type, finish the creation
18361 of the corresponding TYPE by setting its type-specific data.
18362 CU is the DIE's CU. */
18363
18364 static void
18365 finish_fixed_point_type (struct type *type, struct die_info *die,
18366 struct dwarf2_cu *cu)
18367 {
18368 struct attribute *attr;
18369
18370 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18371 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18372
18373 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18374 if (!attr)
18375 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18376 if (!attr)
18377 attr = dwarf2_attr (die, DW_AT_small, cu);
18378
18379 /* Numerator and denominator of our fixed-point type's scaling factor.
18380 The default is a scaling factor of 1, which we use as a fallback
18381 when we are not able to decode it (problem with the debugging info,
18382 unsupported forms, bug in GDB, etc...). Using that as the default
18383 allows us to at least print the unscaled value, which might still
18384 be useful to a user. */
18385 gdb_mpz scale_num (1);
18386 gdb_mpz scale_denom (1);
18387
18388 if (attr == nullptr)
18389 {
18390 /* Scaling factor not found. Assume a scaling factor of 1,
18391 and hope for the best. At least the user will be able to see
18392 the encoded value. */
18393 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18394 sect_offset_str (die->sect_off));
18395 }
18396 else if (attr->name == DW_AT_binary_scale)
18397 {
18398 LONGEST scale_exp = attr->constant_value (0);
18399 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18400
18401 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18402 }
18403 else if (attr->name == DW_AT_decimal_scale)
18404 {
18405 LONGEST scale_exp = attr->constant_value (0);
18406 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18407
18408 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18409 }
18410 else if (attr->name == DW_AT_small)
18411 {
18412 struct die_info *scale_die;
18413 struct dwarf2_cu *scale_cu = cu;
18414
18415 scale_die = follow_die_ref (die, attr, &scale_cu);
18416 if (scale_die->tag == DW_TAG_constant)
18417 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18418 &scale_num, &scale_denom);
18419 else
18420 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18421 " (DIE at %s)"),
18422 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18423 }
18424 else
18425 {
18426 complaint (_("unsupported scale attribute %s for fixed-point type"
18427 " (DIE at %s)"),
18428 dwarf_attr_name (attr->name),
18429 sect_offset_str (die->sect_off));
18430 }
18431
18432 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18433 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18434 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18435 mpq_canonicalize (scaling_factor.val);
18436 }
18437
18438 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18439 (which may be different from NAME) to the architecture back-end to allow
18440 it to guess the correct format if necessary. */
18441
18442 static struct type *
18443 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18444 const char *name_hint, enum bfd_endian byte_order)
18445 {
18446 struct gdbarch *gdbarch = objfile->arch ();
18447 const struct floatformat **format;
18448 struct type *type;
18449
18450 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18451 if (format)
18452 type = init_float_type (objfile, bits, name, format, byte_order);
18453 else
18454 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18455
18456 return type;
18457 }
18458
18459 /* Allocate an integer type of size BITS and name NAME. */
18460
18461 static struct type *
18462 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18463 int bits, int unsigned_p, const char *name)
18464 {
18465 struct type *type;
18466
18467 /* Versions of Intel's C Compiler generate an integer type called "void"
18468 instead of using DW_TAG_unspecified_type. This has been seen on
18469 at least versions 14, 17, and 18. */
18470 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18471 && strcmp (name, "void") == 0)
18472 type = objfile_type (objfile)->builtin_void;
18473 else
18474 type = init_integer_type (objfile, bits, unsigned_p, name);
18475
18476 return type;
18477 }
18478
18479 /* Return true if DIE has a DW_AT_small attribute whose value is
18480 a constant rational, where both the numerator and denominator
18481 are equal to zero.
18482
18483 CU is the DIE's Compilation Unit. */
18484
18485 static bool
18486 has_zero_over_zero_small_attribute (struct die_info *die,
18487 struct dwarf2_cu *cu)
18488 {
18489 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18490 if (attr == nullptr)
18491 return false;
18492
18493 struct dwarf2_cu *scale_cu = cu;
18494 struct die_info *scale_die
18495 = follow_die_ref (die, attr, &scale_cu);
18496
18497 if (scale_die->tag != DW_TAG_constant)
18498 return false;
18499
18500 gdb_mpz num (1), denom (1);
18501 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18502 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18503 }
18504
18505 /* Initialise and return a floating point type of size BITS suitable for
18506 use as a component of a complex number. The NAME_HINT is passed through
18507 when initialising the floating point type and is the name of the complex
18508 type.
18509
18510 As DWARF doesn't currently provide an explicit name for the components
18511 of a complex number, but it can be helpful to have these components
18512 named, we try to select a suitable name based on the size of the
18513 component. */
18514 static struct type *
18515 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18516 struct objfile *objfile,
18517 int bits, const char *name_hint,
18518 enum bfd_endian byte_order)
18519 {
18520 gdbarch *gdbarch = objfile->arch ();
18521 struct type *tt = nullptr;
18522
18523 /* Try to find a suitable floating point builtin type of size BITS.
18524 We're going to use the name of this type as the name for the complex
18525 target type that we are about to create. */
18526 switch (cu->language)
18527 {
18528 case language_fortran:
18529 switch (bits)
18530 {
18531 case 32:
18532 tt = builtin_f_type (gdbarch)->builtin_real;
18533 break;
18534 case 64:
18535 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18536 break;
18537 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18538 case 128:
18539 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18540 break;
18541 }
18542 break;
18543 default:
18544 switch (bits)
18545 {
18546 case 32:
18547 tt = builtin_type (gdbarch)->builtin_float;
18548 break;
18549 case 64:
18550 tt = builtin_type (gdbarch)->builtin_double;
18551 break;
18552 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18553 case 128:
18554 tt = builtin_type (gdbarch)->builtin_long_double;
18555 break;
18556 }
18557 break;
18558 }
18559
18560 /* If the type we found doesn't match the size we were looking for, then
18561 pretend we didn't find a type at all, the complex target type we
18562 create will then be nameless. */
18563 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18564 tt = nullptr;
18565
18566 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18567 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18568 }
18569
18570 /* Find a representation of a given base type and install
18571 it in the TYPE field of the die. */
18572
18573 static struct type *
18574 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18575 {
18576 struct objfile *objfile = cu->per_objfile->objfile;
18577 struct type *type;
18578 struct attribute *attr;
18579 int encoding = 0, bits = 0;
18580 const char *name;
18581 gdbarch *arch;
18582
18583 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18584 if (attr != nullptr && attr->form_is_constant ())
18585 encoding = attr->constant_value (0);
18586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18587 if (attr != nullptr)
18588 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18589 name = dwarf2_name (die, cu);
18590 if (!name)
18591 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18592
18593 arch = objfile->arch ();
18594 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18595
18596 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18597 if (attr != nullptr && attr->form_is_constant ())
18598 {
18599 int endianity = attr->constant_value (0);
18600
18601 switch (endianity)
18602 {
18603 case DW_END_big:
18604 byte_order = BFD_ENDIAN_BIG;
18605 break;
18606 case DW_END_little:
18607 byte_order = BFD_ENDIAN_LITTLE;
18608 break;
18609 default:
18610 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18611 break;
18612 }
18613 }
18614
18615 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18616 && cu->language == language_ada
18617 && has_zero_over_zero_small_attribute (die, cu))
18618 {
18619 /* brobecker/2018-02-24: This is a fixed point type for which
18620 the scaling factor is represented as fraction whose value
18621 does not make sense (zero divided by zero), so we should
18622 normally never see these. However, there is a small category
18623 of fixed point types for which GNAT is unable to provide
18624 the scaling factor via the standard DWARF mechanisms, and
18625 for which the info is provided via the GNAT encodings instead.
18626 This is likely what this DIE is about.
18627
18628 Ideally, GNAT should be declaring this type the same way
18629 it declares other fixed point types when using the legacy
18630 GNAT encoding, which is to use a simple signed or unsigned
18631 base type. A report to the GNAT team has been created to
18632 look into it. In the meantime, pretend this type is a simple
18633 signed or unsigned integral, rather than a fixed point type,
18634 to avoid any confusion later on as to how to process this type. */
18635 encoding = (encoding == DW_ATE_signed_fixed
18636 ? DW_ATE_signed
18637 : DW_ATE_unsigned);
18638 }
18639
18640 switch (encoding)
18641 {
18642 case DW_ATE_address:
18643 /* Turn DW_ATE_address into a void * pointer. */
18644 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18645 type = init_pointer_type (objfile, bits, name, type);
18646 break;
18647 case DW_ATE_boolean:
18648 type = init_boolean_type (objfile, bits, 1, name);
18649 break;
18650 case DW_ATE_complex_float:
18651 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18652 byte_order);
18653 if (type->code () == TYPE_CODE_ERROR)
18654 {
18655 if (name == nullptr)
18656 {
18657 struct obstack *obstack
18658 = &cu->per_objfile->objfile->objfile_obstack;
18659 name = obconcat (obstack, "_Complex ", type->name (),
18660 nullptr);
18661 }
18662 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18663 }
18664 else
18665 type = init_complex_type (name, type);
18666 break;
18667 case DW_ATE_decimal_float:
18668 type = init_decfloat_type (objfile, bits, name);
18669 break;
18670 case DW_ATE_float:
18671 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18672 break;
18673 case DW_ATE_signed:
18674 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18675 break;
18676 case DW_ATE_unsigned:
18677 if (cu->language == language_fortran
18678 && name
18679 && startswith (name, "character("))
18680 type = init_character_type (objfile, bits, 1, name);
18681 else
18682 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18683 break;
18684 case DW_ATE_signed_char:
18685 if (cu->language == language_ada || cu->language == language_m2
18686 || cu->language == language_pascal
18687 || cu->language == language_fortran)
18688 type = init_character_type (objfile, bits, 0, name);
18689 else
18690 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18691 break;
18692 case DW_ATE_unsigned_char:
18693 if (cu->language == language_ada || cu->language == language_m2
18694 || cu->language == language_pascal
18695 || cu->language == language_fortran
18696 || cu->language == language_rust)
18697 type = init_character_type (objfile, bits, 1, name);
18698 else
18699 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18700 break;
18701 case DW_ATE_UTF:
18702 {
18703 if (bits == 16)
18704 type = builtin_type (arch)->builtin_char16;
18705 else if (bits == 32)
18706 type = builtin_type (arch)->builtin_char32;
18707 else
18708 {
18709 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18710 bits);
18711 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18712 }
18713 return set_die_type (die, type, cu);
18714 }
18715 break;
18716 case DW_ATE_signed_fixed:
18717 type = init_fixed_point_type (objfile, bits, 0, name);
18718 finish_fixed_point_type (type, die, cu);
18719 break;
18720 case DW_ATE_unsigned_fixed:
18721 type = init_fixed_point_type (objfile, bits, 1, name);
18722 finish_fixed_point_type (type, die, cu);
18723 break;
18724
18725 default:
18726 complaint (_("unsupported DW_AT_encoding: '%s'"),
18727 dwarf_type_encoding_name (encoding));
18728 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18729 break;
18730 }
18731
18732 if (name && strcmp (name, "char") == 0)
18733 type->set_has_no_signedness (true);
18734
18735 maybe_set_alignment (cu, die, type);
18736
18737 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18738
18739 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18740 {
18741 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18742 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18743 {
18744 unsigned real_bit_size = attr->as_unsigned ();
18745 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18746 /* Only use the attributes if they make sense together. */
18747 if (attr == nullptr
18748 || (attr->as_unsigned () + real_bit_size
18749 <= 8 * TYPE_LENGTH (type)))
18750 {
18751 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18752 = real_bit_size;
18753 if (attr != nullptr)
18754 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18755 = attr->as_unsigned ();
18756 }
18757 }
18758 }
18759
18760 return set_die_type (die, type, cu);
18761 }
18762
18763 /* Parse dwarf attribute if it's a block, reference or constant and put the
18764 resulting value of the attribute into struct bound_prop.
18765 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18766
18767 static int
18768 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18769 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18770 struct type *default_type)
18771 {
18772 struct dwarf2_property_baton *baton;
18773 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18774 struct objfile *objfile = per_objfile->objfile;
18775 struct obstack *obstack = &objfile->objfile_obstack;
18776
18777 gdb_assert (default_type != NULL);
18778
18779 if (attr == NULL || prop == NULL)
18780 return 0;
18781
18782 if (attr->form_is_block ())
18783 {
18784 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18785 baton->property_type = default_type;
18786 baton->locexpr.per_cu = cu->per_cu;
18787 baton->locexpr.per_objfile = per_objfile;
18788
18789 struct dwarf_block *block = attr->as_block ();
18790 baton->locexpr.size = block->size;
18791 baton->locexpr.data = block->data;
18792 switch (attr->name)
18793 {
18794 case DW_AT_string_length:
18795 baton->locexpr.is_reference = true;
18796 break;
18797 default:
18798 baton->locexpr.is_reference = false;
18799 break;
18800 }
18801
18802 prop->set_locexpr (baton);
18803 gdb_assert (prop->baton () != NULL);
18804 }
18805 else if (attr->form_is_ref ())
18806 {
18807 struct dwarf2_cu *target_cu = cu;
18808 struct die_info *target_die;
18809 struct attribute *target_attr;
18810
18811 target_die = follow_die_ref (die, attr, &target_cu);
18812 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18813 if (target_attr == NULL)
18814 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18815 target_cu);
18816 if (target_attr == NULL)
18817 return 0;
18818
18819 switch (target_attr->name)
18820 {
18821 case DW_AT_location:
18822 if (target_attr->form_is_section_offset ())
18823 {
18824 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18825 baton->property_type = die_type (target_die, target_cu);
18826 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18827 prop->set_loclist (baton);
18828 gdb_assert (prop->baton () != NULL);
18829 }
18830 else if (target_attr->form_is_block ())
18831 {
18832 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18833 baton->property_type = die_type (target_die, target_cu);
18834 baton->locexpr.per_cu = cu->per_cu;
18835 baton->locexpr.per_objfile = per_objfile;
18836 struct dwarf_block *block = target_attr->as_block ();
18837 baton->locexpr.size = block->size;
18838 baton->locexpr.data = block->data;
18839 baton->locexpr.is_reference = true;
18840 prop->set_locexpr (baton);
18841 gdb_assert (prop->baton () != NULL);
18842 }
18843 else
18844 {
18845 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18846 "dynamic property");
18847 return 0;
18848 }
18849 break;
18850 case DW_AT_data_member_location:
18851 {
18852 LONGEST offset;
18853
18854 if (!handle_data_member_location (target_die, target_cu,
18855 &offset))
18856 return 0;
18857
18858 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18859 baton->property_type = read_type_die (target_die->parent,
18860 target_cu);
18861 baton->offset_info.offset = offset;
18862 baton->offset_info.type = die_type (target_die, target_cu);
18863 prop->set_addr_offset (baton);
18864 break;
18865 }
18866 }
18867 }
18868 else if (attr->form_is_constant ())
18869 prop->set_const_val (attr->constant_value (0));
18870 else
18871 {
18872 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18873 dwarf2_name (die, cu));
18874 return 0;
18875 }
18876
18877 return 1;
18878 }
18879
18880 /* See read.h. */
18881
18882 struct type *
18883 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18884 {
18885 struct type *int_type;
18886
18887 /* Helper macro to examine the various builtin types. */
18888 #define TRY_TYPE(F) \
18889 int_type = (unsigned_p \
18890 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18891 : objfile_type (objfile)->builtin_ ## F); \
18892 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18893 return int_type
18894
18895 TRY_TYPE (char);
18896 TRY_TYPE (short);
18897 TRY_TYPE (int);
18898 TRY_TYPE (long);
18899 TRY_TYPE (long_long);
18900
18901 #undef TRY_TYPE
18902
18903 gdb_assert_not_reached ("unable to find suitable integer type");
18904 }
18905
18906 /* See read.h. */
18907
18908 struct type *
18909 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18910 {
18911 int addr_size = this->per_cu->addr_size ();
18912 return this->per_objfile->int_type (addr_size, unsigned_p);
18913 }
18914
18915 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18916 present (which is valid) then compute the default type based on the
18917 compilation units address size. */
18918
18919 static struct type *
18920 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18921 {
18922 struct type *index_type = die_type (die, cu);
18923
18924 /* Dwarf-2 specifications explicitly allows to create subrange types
18925 without specifying a base type.
18926 In that case, the base type must be set to the type of
18927 the lower bound, upper bound or count, in that order, if any of these
18928 three attributes references an object that has a type.
18929 If no base type is found, the Dwarf-2 specifications say that
18930 a signed integer type of size equal to the size of an address should
18931 be used.
18932 For the following C code: `extern char gdb_int [];'
18933 GCC produces an empty range DIE.
18934 FIXME: muller/2010-05-28: Possible references to object for low bound,
18935 high bound or count are not yet handled by this code. */
18936 if (index_type->code () == TYPE_CODE_VOID)
18937 index_type = cu->addr_sized_int_type (false);
18938
18939 return index_type;
18940 }
18941
18942 /* Read the given DW_AT_subrange DIE. */
18943
18944 static struct type *
18945 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18946 {
18947 struct type *base_type, *orig_base_type;
18948 struct type *range_type;
18949 struct attribute *attr;
18950 struct dynamic_prop low, high;
18951 int low_default_is_valid;
18952 int high_bound_is_count = 0;
18953 const char *name;
18954 ULONGEST negative_mask;
18955
18956 orig_base_type = read_subrange_index_type (die, cu);
18957
18958 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18959 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18960 creating the range type, but we use the result of check_typedef
18961 when examining properties of the type. */
18962 base_type = check_typedef (orig_base_type);
18963
18964 /* The die_type call above may have already set the type for this DIE. */
18965 range_type = get_die_type (die, cu);
18966 if (range_type)
18967 return range_type;
18968
18969 high.set_const_val (0);
18970
18971 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18972 omitting DW_AT_lower_bound. */
18973 switch (cu->language)
18974 {
18975 case language_c:
18976 case language_cplus:
18977 low.set_const_val (0);
18978 low_default_is_valid = 1;
18979 break;
18980 case language_fortran:
18981 low.set_const_val (1);
18982 low_default_is_valid = 1;
18983 break;
18984 case language_d:
18985 case language_objc:
18986 case language_rust:
18987 low.set_const_val (0);
18988 low_default_is_valid = (cu->header.version >= 4);
18989 break;
18990 case language_ada:
18991 case language_m2:
18992 case language_pascal:
18993 low.set_const_val (1);
18994 low_default_is_valid = (cu->header.version >= 4);
18995 break;
18996 default:
18997 low.set_const_val (0);
18998 low_default_is_valid = 0;
18999 break;
19000 }
19001
19002 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
19003 if (attr != nullptr)
19004 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
19005 else if (!low_default_is_valid)
19006 complaint (_("Missing DW_AT_lower_bound "
19007 "- DIE at %s [in module %s]"),
19008 sect_offset_str (die->sect_off),
19009 objfile_name (cu->per_objfile->objfile));
19010
19011 struct attribute *attr_ub, *attr_count;
19012 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
19013 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19014 {
19015 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
19016 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
19017 {
19018 /* If bounds are constant do the final calculation here. */
19019 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19020 high.set_const_val (low.const_val () + high.const_val () - 1);
19021 else
19022 high_bound_is_count = 1;
19023 }
19024 else
19025 {
19026 if (attr_ub != NULL)
19027 complaint (_("Unresolved DW_AT_upper_bound "
19028 "- DIE at %s [in module %s]"),
19029 sect_offset_str (die->sect_off),
19030 objfile_name (cu->per_objfile->objfile));
19031 if (attr_count != NULL)
19032 complaint (_("Unresolved DW_AT_count "
19033 "- DIE at %s [in module %s]"),
19034 sect_offset_str (die->sect_off),
19035 objfile_name (cu->per_objfile->objfile));
19036 }
19037 }
19038
19039 LONGEST bias = 0;
19040 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
19041 if (bias_attr != nullptr && bias_attr->form_is_constant ())
19042 bias = bias_attr->constant_value (0);
19043
19044 /* Normally, the DWARF producers are expected to use a signed
19045 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19046 But this is unfortunately not always the case, as witnessed
19047 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19048 is used instead. To work around that ambiguity, we treat
19049 the bounds as signed, and thus sign-extend their values, when
19050 the base type is signed. */
19051 negative_mask =
19052 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
19053 if (low.kind () == PROP_CONST
19054 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
19055 low.set_const_val (low.const_val () | negative_mask);
19056 if (high.kind () == PROP_CONST
19057 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
19058 high.set_const_val (high.const_val () | negative_mask);
19059
19060 /* Check for bit and byte strides. */
19061 struct dynamic_prop byte_stride_prop;
19062 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19063 if (attr_byte_stride != nullptr)
19064 {
19065 struct type *prop_type = cu->addr_sized_int_type (false);
19066 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19067 prop_type);
19068 }
19069
19070 struct dynamic_prop bit_stride_prop;
19071 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19072 if (attr_bit_stride != nullptr)
19073 {
19074 /* It only makes sense to have either a bit or byte stride. */
19075 if (attr_byte_stride != nullptr)
19076 {
19077 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19078 "- DIE at %s [in module %s]"),
19079 sect_offset_str (die->sect_off),
19080 objfile_name (cu->per_objfile->objfile));
19081 attr_bit_stride = nullptr;
19082 }
19083 else
19084 {
19085 struct type *prop_type = cu->addr_sized_int_type (false);
19086 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19087 prop_type);
19088 }
19089 }
19090
19091 if (attr_byte_stride != nullptr
19092 || attr_bit_stride != nullptr)
19093 {
19094 bool byte_stride_p = (attr_byte_stride != nullptr);
19095 struct dynamic_prop *stride
19096 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19097
19098 range_type
19099 = create_range_type_with_stride (NULL, orig_base_type, &low,
19100 &high, bias, stride, byte_stride_p);
19101 }
19102 else
19103 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
19104
19105 if (high_bound_is_count)
19106 range_type->bounds ()->flag_upper_bound_is_count = 1;
19107
19108 /* Ada expects an empty array on no boundary attributes. */
19109 if (attr == NULL && cu->language != language_ada)
19110 range_type->bounds ()->high.set_undefined ();
19111
19112 name = dwarf2_name (die, cu);
19113 if (name)
19114 range_type->set_name (name);
19115
19116 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
19117 if (attr != nullptr)
19118 TYPE_LENGTH (range_type) = attr->constant_value (0);
19119
19120 maybe_set_alignment (cu, die, range_type);
19121
19122 set_die_type (die, range_type, cu);
19123
19124 /* set_die_type should be already done. */
19125 set_descriptive_type (range_type, die, cu);
19126
19127 return range_type;
19128 }
19129
19130 static struct type *
19131 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19132 {
19133 struct type *type;
19134
19135 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
19136 type->set_name (dwarf2_name (die, cu));
19137
19138 /* In Ada, an unspecified type is typically used when the description
19139 of the type is deferred to a different unit. When encountering
19140 such a type, we treat it as a stub, and try to resolve it later on,
19141 when needed. */
19142 if (cu->language == language_ada)
19143 type->set_is_stub (true);
19144
19145 return set_die_type (die, type, cu);
19146 }
19147
19148 /* Read a single die and all its descendents. Set the die's sibling
19149 field to NULL; set other fields in the die correctly, and set all
19150 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19151 location of the info_ptr after reading all of those dies. PARENT
19152 is the parent of the die in question. */
19153
19154 static struct die_info *
19155 read_die_and_children (const struct die_reader_specs *reader,
19156 const gdb_byte *info_ptr,
19157 const gdb_byte **new_info_ptr,
19158 struct die_info *parent)
19159 {
19160 struct die_info *die;
19161 const gdb_byte *cur_ptr;
19162
19163 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
19164 if (die == NULL)
19165 {
19166 *new_info_ptr = cur_ptr;
19167 return NULL;
19168 }
19169 store_in_ref_table (die, reader->cu);
19170
19171 if (die->has_children)
19172 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
19173 else
19174 {
19175 die->child = NULL;
19176 *new_info_ptr = cur_ptr;
19177 }
19178
19179 die->sibling = NULL;
19180 die->parent = parent;
19181 return die;
19182 }
19183
19184 /* Read a die, all of its descendents, and all of its siblings; set
19185 all of the fields of all of the dies correctly. Arguments are as
19186 in read_die_and_children. */
19187
19188 static struct die_info *
19189 read_die_and_siblings_1 (const struct die_reader_specs *reader,
19190 const gdb_byte *info_ptr,
19191 const gdb_byte **new_info_ptr,
19192 struct die_info *parent)
19193 {
19194 struct die_info *first_die, *last_sibling;
19195 const gdb_byte *cur_ptr;
19196
19197 cur_ptr = info_ptr;
19198 first_die = last_sibling = NULL;
19199
19200 while (1)
19201 {
19202 struct die_info *die
19203 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19204
19205 if (die == NULL)
19206 {
19207 *new_info_ptr = cur_ptr;
19208 return first_die;
19209 }
19210
19211 if (!first_die)
19212 first_die = die;
19213 else
19214 last_sibling->sibling = die;
19215
19216 last_sibling = die;
19217 }
19218 }
19219
19220 /* Read a die, all of its descendents, and all of its siblings; set
19221 all of the fields of all of the dies correctly. Arguments are as
19222 in read_die_and_children.
19223 This the main entry point for reading a DIE and all its children. */
19224
19225 static struct die_info *
19226 read_die_and_siblings (const struct die_reader_specs *reader,
19227 const gdb_byte *info_ptr,
19228 const gdb_byte **new_info_ptr,
19229 struct die_info *parent)
19230 {
19231 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19232 new_info_ptr, parent);
19233
19234 if (dwarf_die_debug)
19235 {
19236 fprintf_unfiltered (gdb_stdlog,
19237 "Read die from %s@0x%x of %s:\n",
19238 reader->die_section->get_name (),
19239 (unsigned) (info_ptr - reader->die_section->buffer),
19240 bfd_get_filename (reader->abfd));
19241 dump_die (die, dwarf_die_debug);
19242 }
19243
19244 return die;
19245 }
19246
19247 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19248 attributes.
19249 The caller is responsible for filling in the extra attributes
19250 and updating (*DIEP)->num_attrs.
19251 Set DIEP to point to a newly allocated die with its information,
19252 except for its child, sibling, and parent fields. */
19253
19254 static const gdb_byte *
19255 read_full_die_1 (const struct die_reader_specs *reader,
19256 struct die_info **diep, const gdb_byte *info_ptr,
19257 int num_extra_attrs)
19258 {
19259 unsigned int abbrev_number, bytes_read, i;
19260 struct abbrev_info *abbrev;
19261 struct die_info *die;
19262 struct dwarf2_cu *cu = reader->cu;
19263 bfd *abfd = reader->abfd;
19264
19265 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19266 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19267 info_ptr += bytes_read;
19268 if (!abbrev_number)
19269 {
19270 *diep = NULL;
19271 return info_ptr;
19272 }
19273
19274 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19275 if (!abbrev)
19276 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19277 abbrev_number,
19278 bfd_get_filename (abfd));
19279
19280 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19281 die->sect_off = sect_off;
19282 die->tag = abbrev->tag;
19283 die->abbrev = abbrev_number;
19284 die->has_children = abbrev->has_children;
19285
19286 /* Make the result usable.
19287 The caller needs to update num_attrs after adding the extra
19288 attributes. */
19289 die->num_attrs = abbrev->num_attrs;
19290
19291 bool any_need_reprocess = false;
19292 for (i = 0; i < abbrev->num_attrs; ++i)
19293 {
19294 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19295 info_ptr);
19296 if (die->attrs[i].requires_reprocessing_p ())
19297 any_need_reprocess = true;
19298 }
19299
19300 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19301 if (attr != nullptr && attr->form_is_unsigned ())
19302 cu->str_offsets_base = attr->as_unsigned ();
19303
19304 attr = die->attr (DW_AT_loclists_base);
19305 if (attr != nullptr)
19306 cu->loclist_base = attr->as_unsigned ();
19307
19308 auto maybe_addr_base = die->addr_base ();
19309 if (maybe_addr_base.has_value ())
19310 cu->addr_base = *maybe_addr_base;
19311
19312 attr = die->attr (DW_AT_rnglists_base);
19313 if (attr != nullptr)
19314 cu->rnglists_base = attr->as_unsigned ();
19315
19316 if (any_need_reprocess)
19317 {
19318 for (i = 0; i < abbrev->num_attrs; ++i)
19319 {
19320 if (die->attrs[i].requires_reprocessing_p ())
19321 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19322 }
19323 }
19324 *diep = die;
19325 return info_ptr;
19326 }
19327
19328 /* Read a die and all its attributes.
19329 Set DIEP to point to a newly allocated die with its information,
19330 except for its child, sibling, and parent fields. */
19331
19332 static const gdb_byte *
19333 read_full_die (const struct die_reader_specs *reader,
19334 struct die_info **diep, const gdb_byte *info_ptr)
19335 {
19336 const gdb_byte *result;
19337
19338 result = read_full_die_1 (reader, diep, info_ptr, 0);
19339
19340 if (dwarf_die_debug)
19341 {
19342 fprintf_unfiltered (gdb_stdlog,
19343 "Read die from %s@0x%x of %s:\n",
19344 reader->die_section->get_name (),
19345 (unsigned) (info_ptr - reader->die_section->buffer),
19346 bfd_get_filename (reader->abfd));
19347 dump_die (*diep, dwarf_die_debug);
19348 }
19349
19350 return result;
19351 }
19352 \f
19353
19354 /* Returns nonzero if TAG represents a type that we might generate a partial
19355 symbol for. */
19356
19357 static int
19358 is_type_tag_for_partial (int tag, enum language lang)
19359 {
19360 switch (tag)
19361 {
19362 #if 0
19363 /* Some types that would be reasonable to generate partial symbols for,
19364 that we don't at present. Note that normally this does not
19365 matter, mainly because C compilers don't give names to these
19366 types, but instead emit DW_TAG_typedef. */
19367 case DW_TAG_file_type:
19368 case DW_TAG_ptr_to_member_type:
19369 case DW_TAG_set_type:
19370 case DW_TAG_string_type:
19371 case DW_TAG_subroutine_type:
19372 #endif
19373
19374 /* GNAT may emit an array with a name, but no typedef, so we
19375 need to make a symbol in this case. */
19376 case DW_TAG_array_type:
19377 return lang == language_ada;
19378
19379 case DW_TAG_base_type:
19380 case DW_TAG_class_type:
19381 case DW_TAG_interface_type:
19382 case DW_TAG_enumeration_type:
19383 case DW_TAG_structure_type:
19384 case DW_TAG_subrange_type:
19385 case DW_TAG_typedef:
19386 case DW_TAG_union_type:
19387 return 1;
19388 default:
19389 return 0;
19390 }
19391 }
19392
19393 /* Load all DIEs that are interesting for partial symbols into memory. */
19394
19395 static struct partial_die_info *
19396 load_partial_dies (const struct die_reader_specs *reader,
19397 const gdb_byte *info_ptr, int building_psymtab)
19398 {
19399 struct dwarf2_cu *cu = reader->cu;
19400 struct objfile *objfile = cu->per_objfile->objfile;
19401 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19402 unsigned int bytes_read;
19403 unsigned int load_all = 0;
19404 int nesting_level = 1;
19405
19406 parent_die = NULL;
19407 last_die = NULL;
19408
19409 gdb_assert (cu->per_cu != NULL);
19410 if (cu->per_cu->load_all_dies)
19411 load_all = 1;
19412
19413 cu->partial_dies
19414 = htab_create_alloc_ex (cu->header.length / 12,
19415 partial_die_hash,
19416 partial_die_eq,
19417 NULL,
19418 &cu->comp_unit_obstack,
19419 hashtab_obstack_allocate,
19420 dummy_obstack_deallocate);
19421
19422 while (1)
19423 {
19424 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
19425
19426 /* A NULL abbrev means the end of a series of children. */
19427 if (abbrev == NULL)
19428 {
19429 if (--nesting_level == 0)
19430 return first_die;
19431
19432 info_ptr += bytes_read;
19433 last_die = parent_die;
19434 parent_die = parent_die->die_parent;
19435 continue;
19436 }
19437
19438 /* Check for template arguments. We never save these; if
19439 they're seen, we just mark the parent, and go on our way. */
19440 if (parent_die != NULL
19441 && cu->language == language_cplus
19442 && (abbrev->tag == DW_TAG_template_type_param
19443 || abbrev->tag == DW_TAG_template_value_param))
19444 {
19445 parent_die->has_template_arguments = 1;
19446
19447 if (!load_all)
19448 {
19449 /* We don't need a partial DIE for the template argument. */
19450 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19451 continue;
19452 }
19453 }
19454
19455 /* We only recurse into c++ subprograms looking for template arguments.
19456 Skip their other children. */
19457 if (!load_all
19458 && cu->language == language_cplus
19459 && parent_die != NULL
19460 && parent_die->tag == DW_TAG_subprogram
19461 && abbrev->tag != DW_TAG_inlined_subroutine)
19462 {
19463 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19464 continue;
19465 }
19466
19467 /* Check whether this DIE is interesting enough to save. Normally
19468 we would not be interested in members here, but there may be
19469 later variables referencing them via DW_AT_specification (for
19470 static members). */
19471 if (!load_all
19472 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19473 && abbrev->tag != DW_TAG_constant
19474 && abbrev->tag != DW_TAG_enumerator
19475 && abbrev->tag != DW_TAG_subprogram
19476 && abbrev->tag != DW_TAG_inlined_subroutine
19477 && abbrev->tag != DW_TAG_lexical_block
19478 && abbrev->tag != DW_TAG_variable
19479 && abbrev->tag != DW_TAG_namespace
19480 && abbrev->tag != DW_TAG_module
19481 && abbrev->tag != DW_TAG_member
19482 && abbrev->tag != DW_TAG_imported_unit
19483 && abbrev->tag != DW_TAG_imported_declaration)
19484 {
19485 /* Otherwise we skip to the next sibling, if any. */
19486 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19487 continue;
19488 }
19489
19490 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19491 abbrev);
19492
19493 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19494
19495 /* This two-pass algorithm for processing partial symbols has a
19496 high cost in cache pressure. Thus, handle some simple cases
19497 here which cover the majority of C partial symbols. DIEs
19498 which neither have specification tags in them, nor could have
19499 specification tags elsewhere pointing at them, can simply be
19500 processed and discarded.
19501
19502 This segment is also optional; scan_partial_symbols and
19503 add_partial_symbol will handle these DIEs if we chain
19504 them in normally. When compilers which do not emit large
19505 quantities of duplicate debug information are more common,
19506 this code can probably be removed. */
19507
19508 /* Any complete simple types at the top level (pretty much all
19509 of them, for a language without namespaces), can be processed
19510 directly. */
19511 if (parent_die == NULL
19512 && pdi.has_specification == 0
19513 && pdi.is_declaration == 0
19514 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19515 || pdi.tag == DW_TAG_base_type
19516 || pdi.tag == DW_TAG_array_type
19517 || pdi.tag == DW_TAG_subrange_type))
19518 {
19519 if (building_psymtab && pdi.raw_name != NULL)
19520 add_partial_symbol (&pdi, cu);
19521
19522 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19523 continue;
19524 }
19525
19526 /* The exception for DW_TAG_typedef with has_children above is
19527 a workaround of GCC PR debug/47510. In the case of this complaint
19528 type_name_or_error will error on such types later.
19529
19530 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19531 it could not find the child DIEs referenced later, this is checked
19532 above. In correct DWARF DW_TAG_typedef should have no children. */
19533
19534 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19535 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19536 "- DIE at %s [in module %s]"),
19537 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19538
19539 /* If we're at the second level, and we're an enumerator, and
19540 our parent has no specification (meaning possibly lives in a
19541 namespace elsewhere), then we can add the partial symbol now
19542 instead of queueing it. */
19543 if (pdi.tag == DW_TAG_enumerator
19544 && parent_die != NULL
19545 && parent_die->die_parent == NULL
19546 && parent_die->tag == DW_TAG_enumeration_type
19547 && parent_die->has_specification == 0)
19548 {
19549 if (pdi.raw_name == NULL)
19550 complaint (_("malformed enumerator DIE ignored"));
19551 else if (building_psymtab)
19552 add_partial_symbol (&pdi, cu);
19553
19554 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19555 continue;
19556 }
19557
19558 struct partial_die_info *part_die
19559 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19560
19561 /* We'll save this DIE so link it in. */
19562 part_die->die_parent = parent_die;
19563 part_die->die_sibling = NULL;
19564 part_die->die_child = NULL;
19565
19566 if (last_die && last_die == parent_die)
19567 last_die->die_child = part_die;
19568 else if (last_die)
19569 last_die->die_sibling = part_die;
19570
19571 last_die = part_die;
19572
19573 if (first_die == NULL)
19574 first_die = part_die;
19575
19576 /* Maybe add the DIE to the hash table. Not all DIEs that we
19577 find interesting need to be in the hash table, because we
19578 also have the parent/sibling/child chains; only those that we
19579 might refer to by offset later during partial symbol reading.
19580
19581 For now this means things that might have be the target of a
19582 DW_AT_specification, DW_AT_abstract_origin, or
19583 DW_AT_extension. DW_AT_extension will refer only to
19584 namespaces; DW_AT_abstract_origin refers to functions (and
19585 many things under the function DIE, but we do not recurse
19586 into function DIEs during partial symbol reading) and
19587 possibly variables as well; DW_AT_specification refers to
19588 declarations. Declarations ought to have the DW_AT_declaration
19589 flag. It happens that GCC forgets to put it in sometimes, but
19590 only for functions, not for types.
19591
19592 Adding more things than necessary to the hash table is harmless
19593 except for the performance cost. Adding too few will result in
19594 wasted time in find_partial_die, when we reread the compilation
19595 unit with load_all_dies set. */
19596
19597 if (load_all
19598 || abbrev->tag == DW_TAG_constant
19599 || abbrev->tag == DW_TAG_subprogram
19600 || abbrev->tag == DW_TAG_variable
19601 || abbrev->tag == DW_TAG_namespace
19602 || part_die->is_declaration)
19603 {
19604 void **slot;
19605
19606 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19607 to_underlying (part_die->sect_off),
19608 INSERT);
19609 *slot = part_die;
19610 }
19611
19612 /* For some DIEs we want to follow their children (if any). For C
19613 we have no reason to follow the children of structures; for other
19614 languages we have to, so that we can get at method physnames
19615 to infer fully qualified class names, for DW_AT_specification,
19616 and for C++ template arguments. For C++, we also look one level
19617 inside functions to find template arguments (if the name of the
19618 function does not already contain the template arguments).
19619
19620 For Ada and Fortran, we need to scan the children of subprograms
19621 and lexical blocks as well because these languages allow the
19622 definition of nested entities that could be interesting for the
19623 debugger, such as nested subprograms for instance. */
19624 if (last_die->has_children
19625 && (load_all
19626 || last_die->tag == DW_TAG_namespace
19627 || last_die->tag == DW_TAG_module
19628 || last_die->tag == DW_TAG_enumeration_type
19629 || (cu->language == language_cplus
19630 && last_die->tag == DW_TAG_subprogram
19631 && (last_die->raw_name == NULL
19632 || strchr (last_die->raw_name, '<') == NULL))
19633 || (cu->language != language_c
19634 && (last_die->tag == DW_TAG_class_type
19635 || last_die->tag == DW_TAG_interface_type
19636 || last_die->tag == DW_TAG_structure_type
19637 || last_die->tag == DW_TAG_union_type))
19638 || ((cu->language == language_ada
19639 || cu->language == language_fortran)
19640 && (last_die->tag == DW_TAG_subprogram
19641 || last_die->tag == DW_TAG_lexical_block))))
19642 {
19643 nesting_level++;
19644 parent_die = last_die;
19645 continue;
19646 }
19647
19648 /* Otherwise we skip to the next sibling, if any. */
19649 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19650
19651 /* Back to the top, do it again. */
19652 }
19653 }
19654
19655 partial_die_info::partial_die_info (sect_offset sect_off_,
19656 struct abbrev_info *abbrev)
19657 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19658 {
19659 }
19660
19661 /* See class definition. */
19662
19663 const char *
19664 partial_die_info::name (dwarf2_cu *cu)
19665 {
19666 if (!canonical_name && raw_name != nullptr)
19667 {
19668 struct objfile *objfile = cu->per_objfile->objfile;
19669 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19670 canonical_name = 1;
19671 }
19672
19673 return raw_name;
19674 }
19675
19676 /* Read a minimal amount of information into the minimal die structure.
19677 INFO_PTR should point just after the initial uleb128 of a DIE. */
19678
19679 const gdb_byte *
19680 partial_die_info::read (const struct die_reader_specs *reader,
19681 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19682 {
19683 struct dwarf2_cu *cu = reader->cu;
19684 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19685 unsigned int i;
19686 int has_low_pc_attr = 0;
19687 int has_high_pc_attr = 0;
19688 int high_pc_relative = 0;
19689
19690 for (i = 0; i < abbrev.num_attrs; ++i)
19691 {
19692 attribute attr;
19693 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19694 /* String and address offsets that need to do the reprocessing have
19695 already been read at this point, so there is no need to wait until
19696 the loop terminates to do the reprocessing. */
19697 if (attr.requires_reprocessing_p ())
19698 read_attribute_reprocess (reader, &attr, tag);
19699 /* Store the data if it is of an attribute we want to keep in a
19700 partial symbol table. */
19701 switch (attr.name)
19702 {
19703 case DW_AT_name:
19704 switch (tag)
19705 {
19706 case DW_TAG_compile_unit:
19707 case DW_TAG_partial_unit:
19708 case DW_TAG_type_unit:
19709 /* Compilation units have a DW_AT_name that is a filename, not
19710 a source language identifier. */
19711 case DW_TAG_enumeration_type:
19712 case DW_TAG_enumerator:
19713 /* These tags always have simple identifiers already; no need
19714 to canonicalize them. */
19715 canonical_name = 1;
19716 raw_name = attr.as_string ();
19717 break;
19718 default:
19719 canonical_name = 0;
19720 raw_name = attr.as_string ();
19721 break;
19722 }
19723 break;
19724 case DW_AT_linkage_name:
19725 case DW_AT_MIPS_linkage_name:
19726 /* Note that both forms of linkage name might appear. We
19727 assume they will be the same, and we only store the last
19728 one we see. */
19729 linkage_name = attr.as_string ();
19730 break;
19731 case DW_AT_low_pc:
19732 has_low_pc_attr = 1;
19733 lowpc = attr.as_address ();
19734 break;
19735 case DW_AT_high_pc:
19736 has_high_pc_attr = 1;
19737 highpc = attr.as_address ();
19738 if (cu->header.version >= 4 && attr.form_is_constant ())
19739 high_pc_relative = 1;
19740 break;
19741 case DW_AT_location:
19742 /* Support the .debug_loc offsets. */
19743 if (attr.form_is_block ())
19744 {
19745 d.locdesc = attr.as_block ();
19746 }
19747 else if (attr.form_is_section_offset ())
19748 {
19749 dwarf2_complex_location_expr_complaint ();
19750 }
19751 else
19752 {
19753 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19754 "partial symbol information");
19755 }
19756 break;
19757 case DW_AT_external:
19758 is_external = attr.as_boolean ();
19759 break;
19760 case DW_AT_declaration:
19761 is_declaration = attr.as_boolean ();
19762 break;
19763 case DW_AT_type:
19764 has_type = 1;
19765 break;
19766 case DW_AT_abstract_origin:
19767 case DW_AT_specification:
19768 case DW_AT_extension:
19769 has_specification = 1;
19770 spec_offset = attr.get_ref_die_offset ();
19771 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19772 || cu->per_cu->is_dwz);
19773 break;
19774 case DW_AT_sibling:
19775 /* Ignore absolute siblings, they might point outside of
19776 the current compile unit. */
19777 if (attr.form == DW_FORM_ref_addr)
19778 complaint (_("ignoring absolute DW_AT_sibling"));
19779 else
19780 {
19781 const gdb_byte *buffer = reader->buffer;
19782 sect_offset off = attr.get_ref_die_offset ();
19783 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19784
19785 if (sibling_ptr < info_ptr)
19786 complaint (_("DW_AT_sibling points backwards"));
19787 else if (sibling_ptr > reader->buffer_end)
19788 reader->die_section->overflow_complaint ();
19789 else
19790 sibling = sibling_ptr;
19791 }
19792 break;
19793 case DW_AT_byte_size:
19794 has_byte_size = 1;
19795 break;
19796 case DW_AT_const_value:
19797 has_const_value = 1;
19798 break;
19799 case DW_AT_calling_convention:
19800 /* DWARF doesn't provide a way to identify a program's source-level
19801 entry point. DW_AT_calling_convention attributes are only meant
19802 to describe functions' calling conventions.
19803
19804 However, because it's a necessary piece of information in
19805 Fortran, and before DWARF 4 DW_CC_program was the only
19806 piece of debugging information whose definition refers to
19807 a 'main program' at all, several compilers marked Fortran
19808 main programs with DW_CC_program --- even when those
19809 functions use the standard calling conventions.
19810
19811 Although DWARF now specifies a way to provide this
19812 information, we support this practice for backward
19813 compatibility. */
19814 if (attr.constant_value (0) == DW_CC_program
19815 && cu->language == language_fortran)
19816 main_subprogram = 1;
19817 break;
19818 case DW_AT_inline:
19819 {
19820 LONGEST value = attr.constant_value (-1);
19821 if (value == DW_INL_inlined
19822 || value == DW_INL_declared_inlined)
19823 may_be_inlined = 1;
19824 }
19825 break;
19826
19827 case DW_AT_import:
19828 if (tag == DW_TAG_imported_unit)
19829 {
19830 d.sect_off = attr.get_ref_die_offset ();
19831 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19832 || cu->per_cu->is_dwz);
19833 }
19834 break;
19835
19836 case DW_AT_main_subprogram:
19837 main_subprogram = attr.as_boolean ();
19838 break;
19839
19840 case DW_AT_ranges:
19841 {
19842 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19843 on DWARF version). */
19844 ULONGEST ranges_offset = attr.as_unsigned ();
19845
19846 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19847 this value. */
19848 if (tag != DW_TAG_compile_unit)
19849 ranges_offset += cu->gnu_ranges_base;
19850
19851 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19852 nullptr, tag))
19853 has_pc_info = 1;
19854 }
19855 break;
19856
19857 default:
19858 break;
19859 }
19860 }
19861
19862 /* For Ada, if both the name and the linkage name appear, we prefer
19863 the latter. This lets "catch exception" work better, regardless
19864 of the order in which the name and linkage name were emitted.
19865 Really, though, this is just a workaround for the fact that gdb
19866 doesn't store both the name and the linkage name. */
19867 if (cu->language == language_ada && linkage_name != nullptr)
19868 raw_name = linkage_name;
19869
19870 if (high_pc_relative)
19871 highpc += lowpc;
19872
19873 if (has_low_pc_attr && has_high_pc_attr)
19874 {
19875 /* When using the GNU linker, .gnu.linkonce. sections are used to
19876 eliminate duplicate copies of functions and vtables and such.
19877 The linker will arbitrarily choose one and discard the others.
19878 The AT_*_pc values for such functions refer to local labels in
19879 these sections. If the section from that file was discarded, the
19880 labels are not in the output, so the relocs get a value of 0.
19881 If this is a discarded function, mark the pc bounds as invalid,
19882 so that GDB will ignore it. */
19883 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19884 {
19885 struct objfile *objfile = per_objfile->objfile;
19886 struct gdbarch *gdbarch = objfile->arch ();
19887
19888 complaint (_("DW_AT_low_pc %s is zero "
19889 "for DIE at %s [in module %s]"),
19890 paddress (gdbarch, lowpc),
19891 sect_offset_str (sect_off),
19892 objfile_name (objfile));
19893 }
19894 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19895 else if (lowpc >= highpc)
19896 {
19897 struct objfile *objfile = per_objfile->objfile;
19898 struct gdbarch *gdbarch = objfile->arch ();
19899
19900 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19901 "for DIE at %s [in module %s]"),
19902 paddress (gdbarch, lowpc),
19903 paddress (gdbarch, highpc),
19904 sect_offset_str (sect_off),
19905 objfile_name (objfile));
19906 }
19907 else
19908 has_pc_info = 1;
19909 }
19910
19911 return info_ptr;
19912 }
19913
19914 /* Find a cached partial DIE at OFFSET in CU. */
19915
19916 struct partial_die_info *
19917 dwarf2_cu::find_partial_die (sect_offset sect_off)
19918 {
19919 struct partial_die_info *lookup_die = NULL;
19920 struct partial_die_info part_die (sect_off);
19921
19922 lookup_die = ((struct partial_die_info *)
19923 htab_find_with_hash (partial_dies, &part_die,
19924 to_underlying (sect_off)));
19925
19926 return lookup_die;
19927 }
19928
19929 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19930 except in the case of .debug_types DIEs which do not reference
19931 outside their CU (they do however referencing other types via
19932 DW_FORM_ref_sig8). */
19933
19934 static const struct cu_partial_die_info
19935 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19936 {
19937 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19938 struct objfile *objfile = per_objfile->objfile;
19939 struct partial_die_info *pd = NULL;
19940
19941 if (offset_in_dwz == cu->per_cu->is_dwz
19942 && cu->header.offset_in_cu_p (sect_off))
19943 {
19944 pd = cu->find_partial_die (sect_off);
19945 if (pd != NULL)
19946 return { cu, pd };
19947 /* We missed recording what we needed.
19948 Load all dies and try again. */
19949 }
19950 else
19951 {
19952 /* TUs don't reference other CUs/TUs (except via type signatures). */
19953 if (cu->per_cu->is_debug_types)
19954 {
19955 error (_("Dwarf Error: Type Unit at offset %s contains"
19956 " external reference to offset %s [in module %s].\n"),
19957 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19958 bfd_get_filename (objfile->obfd));
19959 }
19960 dwarf2_per_cu_data *per_cu
19961 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19962 per_objfile);
19963
19964 cu = per_objfile->get_cu (per_cu);
19965 if (cu == NULL || cu->partial_dies == NULL)
19966 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19967
19968 cu = per_objfile->get_cu (per_cu);
19969
19970 cu->last_used = 0;
19971 pd = cu->find_partial_die (sect_off);
19972 }
19973
19974 /* If we didn't find it, and not all dies have been loaded,
19975 load them all and try again. */
19976
19977 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19978 {
19979 cu->per_cu->load_all_dies = 1;
19980
19981 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19982 THIS_CU->cu may already be in use. So we can't just free it and
19983 replace its DIEs with the ones we read in. Instead, we leave those
19984 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19985 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19986 set. */
19987 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19988
19989 pd = cu->find_partial_die (sect_off);
19990 }
19991
19992 if (pd == NULL)
19993 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19994 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19995 return { cu, pd };
19996 }
19997
19998 /* See if we can figure out if the class lives in a namespace. We do
19999 this by looking for a member function; its demangled name will
20000 contain namespace info, if there is any. */
20001
20002 static void
20003 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
20004 struct dwarf2_cu *cu)
20005 {
20006 /* NOTE: carlton/2003-10-07: Getting the info this way changes
20007 what template types look like, because the demangler
20008 frequently doesn't give the same name as the debug info. We
20009 could fix this by only using the demangled name to get the
20010 prefix (but see comment in read_structure_type). */
20011
20012 struct partial_die_info *real_pdi;
20013 struct partial_die_info *child_pdi;
20014
20015 /* If this DIE (this DIE's specification, if any) has a parent, then
20016 we should not do this. We'll prepend the parent's fully qualified
20017 name when we create the partial symbol. */
20018
20019 real_pdi = struct_pdi;
20020 while (real_pdi->has_specification)
20021 {
20022 auto res = find_partial_die (real_pdi->spec_offset,
20023 real_pdi->spec_is_dwz, cu);
20024 real_pdi = res.pdi;
20025 cu = res.cu;
20026 }
20027
20028 if (real_pdi->die_parent != NULL)
20029 return;
20030
20031 for (child_pdi = struct_pdi->die_child;
20032 child_pdi != NULL;
20033 child_pdi = child_pdi->die_sibling)
20034 {
20035 if (child_pdi->tag == DW_TAG_subprogram
20036 && child_pdi->linkage_name != NULL)
20037 {
20038 gdb::unique_xmalloc_ptr<char> actual_class_name
20039 (cu->language_defn->class_name_from_physname
20040 (child_pdi->linkage_name));
20041 if (actual_class_name != NULL)
20042 {
20043 struct objfile *objfile = cu->per_objfile->objfile;
20044 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20045 struct_pdi->canonical_name = 1;
20046 }
20047 break;
20048 }
20049 }
20050 }
20051
20052 /* Return true if a DIE with TAG may have the DW_AT_const_value
20053 attribute. */
20054
20055 static bool
20056 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20057 {
20058 switch (tag)
20059 {
20060 case DW_TAG_constant:
20061 case DW_TAG_enumerator:
20062 case DW_TAG_formal_parameter:
20063 case DW_TAG_template_value_param:
20064 case DW_TAG_variable:
20065 return true;
20066 }
20067
20068 return false;
20069 }
20070
20071 void
20072 partial_die_info::fixup (struct dwarf2_cu *cu)
20073 {
20074 /* Once we've fixed up a die, there's no point in doing so again.
20075 This also avoids a memory leak if we were to call
20076 guess_partial_die_structure_name multiple times. */
20077 if (fixup_called)
20078 return;
20079
20080 /* If we found a reference attribute and the DIE has no name, try
20081 to find a name in the referred to DIE. */
20082
20083 if (raw_name == NULL && has_specification)
20084 {
20085 struct partial_die_info *spec_die;
20086
20087 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20088 spec_die = res.pdi;
20089 cu = res.cu;
20090
20091 spec_die->fixup (cu);
20092
20093 if (spec_die->raw_name)
20094 {
20095 raw_name = spec_die->raw_name;
20096 canonical_name = spec_die->canonical_name;
20097
20098 /* Copy DW_AT_external attribute if it is set. */
20099 if (spec_die->is_external)
20100 is_external = spec_die->is_external;
20101 }
20102 }
20103
20104 if (!has_const_value && has_specification
20105 && can_have_DW_AT_const_value_p (tag))
20106 {
20107 struct partial_die_info *spec_die;
20108
20109 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20110 spec_die = res.pdi;
20111 cu = res.cu;
20112
20113 spec_die->fixup (cu);
20114
20115 if (spec_die->has_const_value)
20116 {
20117 /* Copy DW_AT_const_value attribute if it is set. */
20118 has_const_value = spec_die->has_const_value;
20119 }
20120 }
20121
20122 /* Set default names for some unnamed DIEs. */
20123
20124 if (raw_name == NULL && tag == DW_TAG_namespace)
20125 {
20126 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20127 canonical_name = 1;
20128 }
20129
20130 /* If there is no parent die to provide a namespace, and there are
20131 children, see if we can determine the namespace from their linkage
20132 name. */
20133 if (cu->language == language_cplus
20134 && !cu->per_objfile->per_bfd->types.empty ()
20135 && die_parent == NULL
20136 && has_children
20137 && (tag == DW_TAG_class_type
20138 || tag == DW_TAG_structure_type
20139 || tag == DW_TAG_union_type))
20140 guess_partial_die_structure_name (this, cu);
20141
20142 /* GCC might emit a nameless struct or union that has a linkage
20143 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
20144 if (raw_name == NULL
20145 && (tag == DW_TAG_class_type
20146 || tag == DW_TAG_interface_type
20147 || tag == DW_TAG_structure_type
20148 || tag == DW_TAG_union_type)
20149 && linkage_name != NULL)
20150 {
20151 gdb::unique_xmalloc_ptr<char> demangled
20152 (gdb_demangle (linkage_name, DMGL_TYPES));
20153 if (demangled != nullptr)
20154 {
20155 const char *base;
20156
20157 /* Strip any leading namespaces/classes, keep only the base name.
20158 DW_AT_name for named DIEs does not contain the prefixes. */
20159 base = strrchr (demangled.get (), ':');
20160 if (base && base > demangled.get () && base[-1] == ':')
20161 base++;
20162 else
20163 base = demangled.get ();
20164
20165 struct objfile *objfile = cu->per_objfile->objfile;
20166 raw_name = objfile->intern (base);
20167 canonical_name = 1;
20168 }
20169 }
20170
20171 fixup_called = 1;
20172 }
20173
20174 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20175 contents from the given SECTION in the HEADER.
20176
20177 HEADER_OFFSET is the offset of the header in the section. */
20178 static void
20179 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20180 struct dwarf2_section_info *section,
20181 sect_offset header_offset)
20182 {
20183 unsigned int bytes_read;
20184 bfd *abfd = section->get_bfd_owner ();
20185 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20186
20187 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20188 info_ptr += bytes_read;
20189
20190 header->version = read_2_bytes (abfd, info_ptr);
20191 info_ptr += 2;
20192
20193 header->addr_size = read_1_byte (abfd, info_ptr);
20194 info_ptr += 1;
20195
20196 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20197 info_ptr += 1;
20198
20199 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20200 }
20201
20202 /* Return the DW_AT_loclists_base value for the CU. */
20203 static ULONGEST
20204 lookup_loclist_base (struct dwarf2_cu *cu)
20205 {
20206 /* For the .dwo unit, the loclist_base points to the first offset following
20207 the header. The header consists of the following entities-
20208 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20209 bit format)
20210 2. version (2 bytes)
20211 3. address size (1 byte)
20212 4. segment selector size (1 byte)
20213 5. offset entry count (4 bytes)
20214 These sizes are derived as per the DWARFv5 standard. */
20215 if (cu->dwo_unit != nullptr)
20216 {
20217 if (cu->header.initial_length_size == 4)
20218 return LOCLIST_HEADER_SIZE32;
20219 return LOCLIST_HEADER_SIZE64;
20220 }
20221 return cu->loclist_base;
20222 }
20223
20224 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20225 array of offsets in the .debug_loclists section. */
20226
20227 static sect_offset
20228 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20229 {
20230 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20231 struct objfile *objfile = per_objfile->objfile;
20232 bfd *abfd = objfile->obfd;
20233 ULONGEST loclist_header_size =
20234 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20235 : LOCLIST_HEADER_SIZE64);
20236 ULONGEST loclist_base = lookup_loclist_base (cu);
20237
20238 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20239 ULONGEST start_offset =
20240 loclist_base + loclist_index * cu->header.offset_size;
20241
20242 /* Get loclists section. */
20243 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20244
20245 /* Read the loclists section content. */
20246 section->read (objfile);
20247 if (section->buffer == NULL)
20248 error (_("DW_FORM_loclistx used without .debug_loclists "
20249 "section [in module %s]"), objfile_name (objfile));
20250
20251 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20252 so if loclist_base is smaller than the header size, we have a problem. */
20253 if (loclist_base < loclist_header_size)
20254 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20255 objfile_name (objfile));
20256
20257 /* Read the header of the loclists contribution. */
20258 struct loclists_rnglists_header header;
20259 read_loclists_rnglists_header (&header, section,
20260 (sect_offset) (loclist_base - loclist_header_size));
20261
20262 /* Verify the loclist index is valid. */
20263 if (loclist_index >= header.offset_entry_count)
20264 error (_("DW_FORM_loclistx pointing outside of "
20265 ".debug_loclists offset array [in module %s]"),
20266 objfile_name (objfile));
20267
20268 /* Validate that reading won't go beyond the end of the section. */
20269 if (start_offset + cu->header.offset_size > section->size)
20270 error (_("Reading DW_FORM_loclistx index beyond end of"
20271 ".debug_loclists section [in module %s]"),
20272 objfile_name (objfile));
20273
20274 const gdb_byte *info_ptr = section->buffer + start_offset;
20275
20276 if (cu->header.offset_size == 4)
20277 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20278 else
20279 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20280 }
20281
20282 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20283 array of offsets in the .debug_rnglists section. */
20284
20285 static sect_offset
20286 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20287 dwarf_tag tag)
20288 {
20289 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20290 struct objfile *objfile = dwarf2_per_objfile->objfile;
20291 bfd *abfd = objfile->obfd;
20292 ULONGEST rnglist_header_size =
20293 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20294 : RNGLIST_HEADER_SIZE64);
20295
20296 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20297 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20298 doesn't apply. */
20299 ULONGEST rnglist_base =
20300 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20301
20302 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20303 ULONGEST start_offset =
20304 rnglist_base + rnglist_index * cu->header.offset_size;
20305
20306 /* Get rnglists section. */
20307 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20308
20309 /* Read the rnglists section content. */
20310 section->read (objfile);
20311 if (section->buffer == nullptr)
20312 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20313 "[in module %s]"),
20314 objfile_name (objfile));
20315
20316 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20317 so if rnglist_base is smaller than the header size, we have a problem. */
20318 if (rnglist_base < rnglist_header_size)
20319 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20320 objfile_name (objfile));
20321
20322 /* Read the header of the rnglists contribution. */
20323 struct loclists_rnglists_header header;
20324 read_loclists_rnglists_header (&header, section,
20325 (sect_offset) (rnglist_base - rnglist_header_size));
20326
20327 /* Verify the rnglist index is valid. */
20328 if (rnglist_index >= header.offset_entry_count)
20329 error (_("DW_FORM_rnglistx index pointing outside of "
20330 ".debug_rnglists offset array [in module %s]"),
20331 objfile_name (objfile));
20332
20333 /* Validate that reading won't go beyond the end of the section. */
20334 if (start_offset + cu->header.offset_size > section->size)
20335 error (_("Reading DW_FORM_rnglistx index beyond end of"
20336 ".debug_rnglists section [in module %s]"),
20337 objfile_name (objfile));
20338
20339 const gdb_byte *info_ptr = section->buffer + start_offset;
20340
20341 if (cu->header.offset_size == 4)
20342 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20343 else
20344 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20345 }
20346
20347 /* Process the attributes that had to be skipped in the first round. These
20348 attributes are the ones that need str_offsets_base or addr_base attributes.
20349 They could not have been processed in the first round, because at the time
20350 the values of str_offsets_base or addr_base may not have been known. */
20351 static void
20352 read_attribute_reprocess (const struct die_reader_specs *reader,
20353 struct attribute *attr, dwarf_tag tag)
20354 {
20355 struct dwarf2_cu *cu = reader->cu;
20356 switch (attr->form)
20357 {
20358 case DW_FORM_addrx:
20359 case DW_FORM_GNU_addr_index:
20360 attr->set_address (read_addr_index (cu,
20361 attr->as_unsigned_reprocess ()));
20362 break;
20363 case DW_FORM_loclistx:
20364 {
20365 sect_offset loclists_sect_off
20366 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20367
20368 attr->set_unsigned (to_underlying (loclists_sect_off));
20369 }
20370 break;
20371 case DW_FORM_rnglistx:
20372 {
20373 sect_offset rnglists_sect_off
20374 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20375
20376 attr->set_unsigned (to_underlying (rnglists_sect_off));
20377 }
20378 break;
20379 case DW_FORM_strx:
20380 case DW_FORM_strx1:
20381 case DW_FORM_strx2:
20382 case DW_FORM_strx3:
20383 case DW_FORM_strx4:
20384 case DW_FORM_GNU_str_index:
20385 {
20386 unsigned int str_index = attr->as_unsigned_reprocess ();
20387 gdb_assert (!attr->canonical_string_p ());
20388 if (reader->dwo_file != NULL)
20389 attr->set_string_noncanonical (read_dwo_str_index (reader,
20390 str_index));
20391 else
20392 attr->set_string_noncanonical (read_stub_str_index (cu,
20393 str_index));
20394 break;
20395 }
20396 default:
20397 gdb_assert_not_reached (_("Unexpected DWARF form."));
20398 }
20399 }
20400
20401 /* Read an attribute value described by an attribute form. */
20402
20403 static const gdb_byte *
20404 read_attribute_value (const struct die_reader_specs *reader,
20405 struct attribute *attr, unsigned form,
20406 LONGEST implicit_const, const gdb_byte *info_ptr)
20407 {
20408 struct dwarf2_cu *cu = reader->cu;
20409 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20410 struct objfile *objfile = per_objfile->objfile;
20411 bfd *abfd = reader->abfd;
20412 struct comp_unit_head *cu_header = &cu->header;
20413 unsigned int bytes_read;
20414 struct dwarf_block *blk;
20415
20416 attr->form = (enum dwarf_form) form;
20417 switch (form)
20418 {
20419 case DW_FORM_ref_addr:
20420 if (cu->header.version == 2)
20421 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20422 &bytes_read));
20423 else
20424 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20425 &bytes_read));
20426 info_ptr += bytes_read;
20427 break;
20428 case DW_FORM_GNU_ref_alt:
20429 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20430 &bytes_read));
20431 info_ptr += bytes_read;
20432 break;
20433 case DW_FORM_addr:
20434 {
20435 struct gdbarch *gdbarch = objfile->arch ();
20436 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20437 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20438 attr->set_address (addr);
20439 info_ptr += bytes_read;
20440 }
20441 break;
20442 case DW_FORM_block2:
20443 blk = dwarf_alloc_block (cu);
20444 blk->size = read_2_bytes (abfd, info_ptr);
20445 info_ptr += 2;
20446 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20447 info_ptr += blk->size;
20448 attr->set_block (blk);
20449 break;
20450 case DW_FORM_block4:
20451 blk = dwarf_alloc_block (cu);
20452 blk->size = read_4_bytes (abfd, info_ptr);
20453 info_ptr += 4;
20454 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20455 info_ptr += blk->size;
20456 attr->set_block (blk);
20457 break;
20458 case DW_FORM_data2:
20459 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20460 info_ptr += 2;
20461 break;
20462 case DW_FORM_data4:
20463 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20464 info_ptr += 4;
20465 break;
20466 case DW_FORM_data8:
20467 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20468 info_ptr += 8;
20469 break;
20470 case DW_FORM_data16:
20471 blk = dwarf_alloc_block (cu);
20472 blk->size = 16;
20473 blk->data = read_n_bytes (abfd, info_ptr, 16);
20474 info_ptr += 16;
20475 attr->set_block (blk);
20476 break;
20477 case DW_FORM_sec_offset:
20478 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20479 &bytes_read));
20480 info_ptr += bytes_read;
20481 break;
20482 case DW_FORM_loclistx:
20483 {
20484 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20485 &bytes_read));
20486 info_ptr += bytes_read;
20487 }
20488 break;
20489 case DW_FORM_string:
20490 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20491 &bytes_read));
20492 info_ptr += bytes_read;
20493 break;
20494 case DW_FORM_strp:
20495 if (!cu->per_cu->is_dwz)
20496 {
20497 attr->set_string_noncanonical
20498 (read_indirect_string (per_objfile,
20499 abfd, info_ptr, cu_header,
20500 &bytes_read));
20501 info_ptr += bytes_read;
20502 break;
20503 }
20504 /* FALLTHROUGH */
20505 case DW_FORM_line_strp:
20506 if (!cu->per_cu->is_dwz)
20507 {
20508 attr->set_string_noncanonical
20509 (per_objfile->read_line_string (info_ptr, cu_header,
20510 &bytes_read));
20511 info_ptr += bytes_read;
20512 break;
20513 }
20514 /* FALLTHROUGH */
20515 case DW_FORM_GNU_strp_alt:
20516 {
20517 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
20518 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20519 &bytes_read);
20520
20521 attr->set_string_noncanonical
20522 (dwz->read_string (objfile, str_offset));
20523 info_ptr += bytes_read;
20524 }
20525 break;
20526 case DW_FORM_exprloc:
20527 case DW_FORM_block:
20528 blk = dwarf_alloc_block (cu);
20529 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20530 info_ptr += bytes_read;
20531 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20532 info_ptr += blk->size;
20533 attr->set_block (blk);
20534 break;
20535 case DW_FORM_block1:
20536 blk = dwarf_alloc_block (cu);
20537 blk->size = read_1_byte (abfd, info_ptr);
20538 info_ptr += 1;
20539 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20540 info_ptr += blk->size;
20541 attr->set_block (blk);
20542 break;
20543 case DW_FORM_data1:
20544 case DW_FORM_flag:
20545 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20546 info_ptr += 1;
20547 break;
20548 case DW_FORM_flag_present:
20549 attr->set_unsigned (1);
20550 break;
20551 case DW_FORM_sdata:
20552 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20553 info_ptr += bytes_read;
20554 break;
20555 case DW_FORM_rnglistx:
20556 {
20557 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20558 &bytes_read));
20559 info_ptr += bytes_read;
20560 }
20561 break;
20562 case DW_FORM_udata:
20563 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20564 info_ptr += bytes_read;
20565 break;
20566 case DW_FORM_ref1:
20567 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20568 + read_1_byte (abfd, info_ptr)));
20569 info_ptr += 1;
20570 break;
20571 case DW_FORM_ref2:
20572 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20573 + read_2_bytes (abfd, info_ptr)));
20574 info_ptr += 2;
20575 break;
20576 case DW_FORM_ref4:
20577 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20578 + read_4_bytes (abfd, info_ptr)));
20579 info_ptr += 4;
20580 break;
20581 case DW_FORM_ref8:
20582 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20583 + read_8_bytes (abfd, info_ptr)));
20584 info_ptr += 8;
20585 break;
20586 case DW_FORM_ref_sig8:
20587 attr->set_signature (read_8_bytes (abfd, info_ptr));
20588 info_ptr += 8;
20589 break;
20590 case DW_FORM_ref_udata:
20591 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20592 + read_unsigned_leb128 (abfd, info_ptr,
20593 &bytes_read)));
20594 info_ptr += bytes_read;
20595 break;
20596 case DW_FORM_indirect:
20597 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20598 info_ptr += bytes_read;
20599 if (form == DW_FORM_implicit_const)
20600 {
20601 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20602 info_ptr += bytes_read;
20603 }
20604 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20605 info_ptr);
20606 break;
20607 case DW_FORM_implicit_const:
20608 attr->set_signed (implicit_const);
20609 break;
20610 case DW_FORM_addrx:
20611 case DW_FORM_GNU_addr_index:
20612 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20613 &bytes_read));
20614 info_ptr += bytes_read;
20615 break;
20616 case DW_FORM_strx:
20617 case DW_FORM_strx1:
20618 case DW_FORM_strx2:
20619 case DW_FORM_strx3:
20620 case DW_FORM_strx4:
20621 case DW_FORM_GNU_str_index:
20622 {
20623 ULONGEST str_index;
20624 if (form == DW_FORM_strx1)
20625 {
20626 str_index = read_1_byte (abfd, info_ptr);
20627 info_ptr += 1;
20628 }
20629 else if (form == DW_FORM_strx2)
20630 {
20631 str_index = read_2_bytes (abfd, info_ptr);
20632 info_ptr += 2;
20633 }
20634 else if (form == DW_FORM_strx3)
20635 {
20636 str_index = read_3_bytes (abfd, info_ptr);
20637 info_ptr += 3;
20638 }
20639 else if (form == DW_FORM_strx4)
20640 {
20641 str_index = read_4_bytes (abfd, info_ptr);
20642 info_ptr += 4;
20643 }
20644 else
20645 {
20646 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20647 info_ptr += bytes_read;
20648 }
20649 attr->set_unsigned_reprocess (str_index);
20650 }
20651 break;
20652 default:
20653 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20654 dwarf_form_name (form),
20655 bfd_get_filename (abfd));
20656 }
20657
20658 /* Super hack. */
20659 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20660 attr->form = DW_FORM_GNU_ref_alt;
20661
20662 /* We have seen instances where the compiler tried to emit a byte
20663 size attribute of -1 which ended up being encoded as an unsigned
20664 0xffffffff. Although 0xffffffff is technically a valid size value,
20665 an object of this size seems pretty unlikely so we can relatively
20666 safely treat these cases as if the size attribute was invalid and
20667 treat them as zero by default. */
20668 if (attr->name == DW_AT_byte_size
20669 && form == DW_FORM_data4
20670 && attr->as_unsigned () >= 0xffffffff)
20671 {
20672 complaint
20673 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20674 hex_string (attr->as_unsigned ()));
20675 attr->set_unsigned (0);
20676 }
20677
20678 return info_ptr;
20679 }
20680
20681 /* Read an attribute described by an abbreviated attribute. */
20682
20683 static const gdb_byte *
20684 read_attribute (const struct die_reader_specs *reader,
20685 struct attribute *attr, struct attr_abbrev *abbrev,
20686 const gdb_byte *info_ptr)
20687 {
20688 attr->name = abbrev->name;
20689 attr->string_is_canonical = 0;
20690 attr->requires_reprocessing = 0;
20691 return read_attribute_value (reader, attr, abbrev->form,
20692 abbrev->implicit_const, info_ptr);
20693 }
20694
20695 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20696
20697 static const char *
20698 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20699 LONGEST str_offset)
20700 {
20701 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20702 str_offset, "DW_FORM_strp");
20703 }
20704
20705 /* Return pointer to string at .debug_str offset as read from BUF.
20706 BUF is assumed to be in a compilation unit described by CU_HEADER.
20707 Return *BYTES_READ_PTR count of bytes read from BUF. */
20708
20709 static const char *
20710 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20711 const gdb_byte *buf,
20712 const struct comp_unit_head *cu_header,
20713 unsigned int *bytes_read_ptr)
20714 {
20715 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20716
20717 return read_indirect_string_at_offset (per_objfile, str_offset);
20718 }
20719
20720 /* See read.h. */
20721
20722 const char *
20723 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20724 const struct comp_unit_head *cu_header,
20725 unsigned int *bytes_read_ptr)
20726 {
20727 bfd *abfd = objfile->obfd;
20728 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20729
20730 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20731 }
20732
20733 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20734 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20735 ADDR_SIZE is the size of addresses from the CU header. */
20736
20737 static CORE_ADDR
20738 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20739 gdb::optional<ULONGEST> addr_base, int addr_size)
20740 {
20741 struct objfile *objfile = per_objfile->objfile;
20742 bfd *abfd = objfile->obfd;
20743 const gdb_byte *info_ptr;
20744 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20745
20746 per_objfile->per_bfd->addr.read (objfile);
20747 if (per_objfile->per_bfd->addr.buffer == NULL)
20748 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20749 objfile_name (objfile));
20750 if (addr_base_or_zero + addr_index * addr_size
20751 >= per_objfile->per_bfd->addr.size)
20752 error (_("DW_FORM_addr_index pointing outside of "
20753 ".debug_addr section [in module %s]"),
20754 objfile_name (objfile));
20755 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20756 + addr_index * addr_size);
20757 if (addr_size == 4)
20758 return bfd_get_32 (abfd, info_ptr);
20759 else
20760 return bfd_get_64 (abfd, info_ptr);
20761 }
20762
20763 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20764
20765 static CORE_ADDR
20766 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20767 {
20768 return read_addr_index_1 (cu->per_objfile, addr_index,
20769 cu->addr_base, cu->header.addr_size);
20770 }
20771
20772 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20773
20774 static CORE_ADDR
20775 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20776 unsigned int *bytes_read)
20777 {
20778 bfd *abfd = cu->per_objfile->objfile->obfd;
20779 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20780
20781 return read_addr_index (cu, addr_index);
20782 }
20783
20784 /* See read.h. */
20785
20786 CORE_ADDR
20787 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20788 dwarf2_per_objfile *per_objfile,
20789 unsigned int addr_index)
20790 {
20791 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20792 gdb::optional<ULONGEST> addr_base;
20793 int addr_size;
20794
20795 /* We need addr_base and addr_size.
20796 If we don't have PER_CU->cu, we have to get it.
20797 Nasty, but the alternative is storing the needed info in PER_CU,
20798 which at this point doesn't seem justified: it's not clear how frequently
20799 it would get used and it would increase the size of every PER_CU.
20800 Entry points like dwarf2_per_cu_addr_size do a similar thing
20801 so we're not in uncharted territory here.
20802 Alas we need to be a bit more complicated as addr_base is contained
20803 in the DIE.
20804
20805 We don't need to read the entire CU(/TU).
20806 We just need the header and top level die.
20807
20808 IWBN to use the aging mechanism to let us lazily later discard the CU.
20809 For now we skip this optimization. */
20810
20811 if (cu != NULL)
20812 {
20813 addr_base = cu->addr_base;
20814 addr_size = cu->header.addr_size;
20815 }
20816 else
20817 {
20818 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20819 addr_base = reader.cu->addr_base;
20820 addr_size = reader.cu->header.addr_size;
20821 }
20822
20823 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20824 }
20825
20826 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20827 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20828 DWO file. */
20829
20830 static const char *
20831 read_str_index (struct dwarf2_cu *cu,
20832 struct dwarf2_section_info *str_section,
20833 struct dwarf2_section_info *str_offsets_section,
20834 ULONGEST str_offsets_base, ULONGEST str_index)
20835 {
20836 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20837 struct objfile *objfile = per_objfile->objfile;
20838 const char *objf_name = objfile_name (objfile);
20839 bfd *abfd = objfile->obfd;
20840 const gdb_byte *info_ptr;
20841 ULONGEST str_offset;
20842 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20843
20844 str_section->read (objfile);
20845 str_offsets_section->read (objfile);
20846 if (str_section->buffer == NULL)
20847 error (_("%s used without %s section"
20848 " in CU at offset %s [in module %s]"),
20849 form_name, str_section->get_name (),
20850 sect_offset_str (cu->header.sect_off), objf_name);
20851 if (str_offsets_section->buffer == NULL)
20852 error (_("%s used without %s section"
20853 " in CU at offset %s [in module %s]"),
20854 form_name, str_section->get_name (),
20855 sect_offset_str (cu->header.sect_off), objf_name);
20856 info_ptr = (str_offsets_section->buffer
20857 + str_offsets_base
20858 + str_index * cu->header.offset_size);
20859 if (cu->header.offset_size == 4)
20860 str_offset = bfd_get_32 (abfd, info_ptr);
20861 else
20862 str_offset = bfd_get_64 (abfd, info_ptr);
20863 if (str_offset >= str_section->size)
20864 error (_("Offset from %s pointing outside of"
20865 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20866 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20867 return (const char *) (str_section->buffer + str_offset);
20868 }
20869
20870 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20871
20872 static const char *
20873 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20874 {
20875 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20876 ? reader->cu->header.addr_size : 0;
20877 return read_str_index (reader->cu,
20878 &reader->dwo_file->sections.str,
20879 &reader->dwo_file->sections.str_offsets,
20880 str_offsets_base, str_index);
20881 }
20882
20883 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20884
20885 static const char *
20886 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20887 {
20888 struct objfile *objfile = cu->per_objfile->objfile;
20889 const char *objf_name = objfile_name (objfile);
20890 static const char form_name[] = "DW_FORM_GNU_str_index";
20891 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20892
20893 if (!cu->str_offsets_base.has_value ())
20894 error (_("%s used in Fission stub without %s"
20895 " in CU at offset 0x%lx [in module %s]"),
20896 form_name, str_offsets_attr_name,
20897 (long) cu->header.offset_size, objf_name);
20898
20899 return read_str_index (cu,
20900 &cu->per_objfile->per_bfd->str,
20901 &cu->per_objfile->per_bfd->str_offsets,
20902 *cu->str_offsets_base, str_index);
20903 }
20904
20905 /* Return the length of an LEB128 number in BUF. */
20906
20907 static int
20908 leb128_size (const gdb_byte *buf)
20909 {
20910 const gdb_byte *begin = buf;
20911 gdb_byte byte;
20912
20913 while (1)
20914 {
20915 byte = *buf++;
20916 if ((byte & 128) == 0)
20917 return buf - begin;
20918 }
20919 }
20920
20921 static void
20922 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20923 {
20924 switch (lang)
20925 {
20926 case DW_LANG_C89:
20927 case DW_LANG_C99:
20928 case DW_LANG_C11:
20929 case DW_LANG_C:
20930 case DW_LANG_UPC:
20931 cu->language = language_c;
20932 break;
20933 case DW_LANG_Java:
20934 case DW_LANG_C_plus_plus:
20935 case DW_LANG_C_plus_plus_11:
20936 case DW_LANG_C_plus_plus_14:
20937 cu->language = language_cplus;
20938 break;
20939 case DW_LANG_D:
20940 cu->language = language_d;
20941 break;
20942 case DW_LANG_Fortran77:
20943 case DW_LANG_Fortran90:
20944 case DW_LANG_Fortran95:
20945 case DW_LANG_Fortran03:
20946 case DW_LANG_Fortran08:
20947 cu->language = language_fortran;
20948 break;
20949 case DW_LANG_Go:
20950 cu->language = language_go;
20951 break;
20952 case DW_LANG_Mips_Assembler:
20953 cu->language = language_asm;
20954 break;
20955 case DW_LANG_Ada83:
20956 case DW_LANG_Ada95:
20957 cu->language = language_ada;
20958 break;
20959 case DW_LANG_Modula2:
20960 cu->language = language_m2;
20961 break;
20962 case DW_LANG_Pascal83:
20963 cu->language = language_pascal;
20964 break;
20965 case DW_LANG_ObjC:
20966 cu->language = language_objc;
20967 break;
20968 case DW_LANG_Rust:
20969 case DW_LANG_Rust_old:
20970 cu->language = language_rust;
20971 break;
20972 case DW_LANG_Cobol74:
20973 case DW_LANG_Cobol85:
20974 default:
20975 cu->language = language_minimal;
20976 break;
20977 }
20978 cu->language_defn = language_def (cu->language);
20979 }
20980
20981 /* Return the named attribute or NULL if not there. */
20982
20983 static struct attribute *
20984 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20985 {
20986 for (;;)
20987 {
20988 unsigned int i;
20989 struct attribute *spec = NULL;
20990
20991 for (i = 0; i < die->num_attrs; ++i)
20992 {
20993 if (die->attrs[i].name == name)
20994 return &die->attrs[i];
20995 if (die->attrs[i].name == DW_AT_specification
20996 || die->attrs[i].name == DW_AT_abstract_origin)
20997 spec = &die->attrs[i];
20998 }
20999
21000 if (!spec)
21001 break;
21002
21003 die = follow_die_ref (die, spec, &cu);
21004 }
21005
21006 return NULL;
21007 }
21008
21009 /* Return the string associated with a string-typed attribute, or NULL if it
21010 is either not found or is of an incorrect type. */
21011
21012 static const char *
21013 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21014 {
21015 struct attribute *attr;
21016 const char *str = NULL;
21017
21018 attr = dwarf2_attr (die, name, cu);
21019
21020 if (attr != NULL)
21021 {
21022 str = attr->as_string ();
21023 if (str == nullptr)
21024 complaint (_("string type expected for attribute %s for "
21025 "DIE at %s in module %s"),
21026 dwarf_attr_name (name), sect_offset_str (die->sect_off),
21027 objfile_name (cu->per_objfile->objfile));
21028 }
21029
21030 return str;
21031 }
21032
21033 /* Return the dwo name or NULL if not present. If present, it is in either
21034 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
21035 static const char *
21036 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21037 {
21038 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21039 if (dwo_name == nullptr)
21040 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21041 return dwo_name;
21042 }
21043
21044 /* Return non-zero iff the attribute NAME is defined for the given DIE,
21045 and holds a non-zero value. This function should only be used for
21046 DW_FORM_flag or DW_FORM_flag_present attributes. */
21047
21048 static int
21049 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21050 {
21051 struct attribute *attr = dwarf2_attr (die, name, cu);
21052
21053 return attr != nullptr && attr->as_boolean ();
21054 }
21055
21056 static int
21057 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
21058 {
21059 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21060 which value is non-zero. However, we have to be careful with
21061 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21062 (via dwarf2_flag_true_p) follows this attribute. So we may
21063 end up accidently finding a declaration attribute that belongs
21064 to a different DIE referenced by the specification attribute,
21065 even though the given DIE does not have a declaration attribute. */
21066 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21067 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
21068 }
21069
21070 /* Return the die giving the specification for DIE, if there is
21071 one. *SPEC_CU is the CU containing DIE on input, and the CU
21072 containing the return value on output. If there is no
21073 specification, but there is an abstract origin, that is
21074 returned. */
21075
21076 static struct die_info *
21077 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
21078 {
21079 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21080 *spec_cu);
21081
21082 if (spec_attr == NULL)
21083 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21084
21085 if (spec_attr == NULL)
21086 return NULL;
21087 else
21088 return follow_die_ref (die, spec_attr, spec_cu);
21089 }
21090
21091 /* Stub for free_line_header to match void * callback types. */
21092
21093 static void
21094 free_line_header_voidp (void *arg)
21095 {
21096 struct line_header *lh = (struct line_header *) arg;
21097
21098 delete lh;
21099 }
21100
21101 /* A convenience function to find the proper .debug_line section for a CU. */
21102
21103 static struct dwarf2_section_info *
21104 get_debug_line_section (struct dwarf2_cu *cu)
21105 {
21106 struct dwarf2_section_info *section;
21107 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21108
21109 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21110 DWO file. */
21111 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21112 section = &cu->dwo_unit->dwo_file->sections.line;
21113 else if (cu->per_cu->is_dwz)
21114 {
21115 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
21116
21117 section = &dwz->line;
21118 }
21119 else
21120 section = &per_objfile->per_bfd->line;
21121
21122 return section;
21123 }
21124
21125 /* Read the statement program header starting at OFFSET in
21126 .debug_line, or .debug_line.dwo. Return a pointer
21127 to a struct line_header, allocated using xmalloc.
21128 Returns NULL if there is a problem reading the header, e.g., if it
21129 has a version we don't understand.
21130
21131 NOTE: the strings in the include directory and file name tables of
21132 the returned object point into the dwarf line section buffer,
21133 and must not be freed. */
21134
21135 static line_header_up
21136 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
21137 {
21138 struct dwarf2_section_info *section;
21139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21140
21141 section = get_debug_line_section (cu);
21142 section->read (per_objfile->objfile);
21143 if (section->buffer == NULL)
21144 {
21145 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21146 complaint (_("missing .debug_line.dwo section"));
21147 else
21148 complaint (_("missing .debug_line section"));
21149 return 0;
21150 }
21151
21152 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
21153 per_objfile, section, &cu->header);
21154 }
21155
21156 /* Subroutine of dwarf_decode_lines to simplify it.
21157 Return the file name of the psymtab for the given file_entry.
21158 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21159 If space for the result is malloc'd, *NAME_HOLDER will be set.
21160 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
21161
21162 static const char *
21163 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
21164 const dwarf2_psymtab *pst,
21165 const char *comp_dir,
21166 gdb::unique_xmalloc_ptr<char> *name_holder)
21167 {
21168 const char *include_name = fe.name;
21169 const char *include_name_to_compare = include_name;
21170 const char *pst_filename;
21171 int file_is_pst;
21172
21173 const char *dir_name = fe.include_dir (lh);
21174
21175 gdb::unique_xmalloc_ptr<char> hold_compare;
21176 if (!IS_ABSOLUTE_PATH (include_name)
21177 && (dir_name != NULL || comp_dir != NULL))
21178 {
21179 /* Avoid creating a duplicate psymtab for PST.
21180 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21181 Before we do the comparison, however, we need to account
21182 for DIR_NAME and COMP_DIR.
21183 First prepend dir_name (if non-NULL). If we still don't
21184 have an absolute path prepend comp_dir (if non-NULL).
21185 However, the directory we record in the include-file's
21186 psymtab does not contain COMP_DIR (to match the
21187 corresponding symtab(s)).
21188
21189 Example:
21190
21191 bash$ cd /tmp
21192 bash$ gcc -g ./hello.c
21193 include_name = "hello.c"
21194 dir_name = "."
21195 DW_AT_comp_dir = comp_dir = "/tmp"
21196 DW_AT_name = "./hello.c"
21197
21198 */
21199
21200 if (dir_name != NULL)
21201 {
21202 name_holder->reset (concat (dir_name, SLASH_STRING,
21203 include_name, (char *) NULL));
21204 include_name = name_holder->get ();
21205 include_name_to_compare = include_name;
21206 }
21207 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21208 {
21209 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21210 include_name, (char *) NULL));
21211 include_name_to_compare = hold_compare.get ();
21212 }
21213 }
21214
21215 pst_filename = pst->filename;
21216 gdb::unique_xmalloc_ptr<char> copied_name;
21217 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21218 {
21219 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21220 pst_filename, (char *) NULL));
21221 pst_filename = copied_name.get ();
21222 }
21223
21224 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21225
21226 if (file_is_pst)
21227 return NULL;
21228 return include_name;
21229 }
21230
21231 /* State machine to track the state of the line number program. */
21232
21233 class lnp_state_machine
21234 {
21235 public:
21236 /* Initialize a machine state for the start of a line number
21237 program. */
21238 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21239 bool record_lines_p);
21240
21241 file_entry *current_file ()
21242 {
21243 /* lh->file_names is 0-based, but the file name numbers in the
21244 statement program are 1-based. */
21245 return m_line_header->file_name_at (m_file);
21246 }
21247
21248 /* Record the line in the state machine. END_SEQUENCE is true if
21249 we're processing the end of a sequence. */
21250 void record_line (bool end_sequence);
21251
21252 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21253 nop-out rest of the lines in this sequence. */
21254 void check_line_address (struct dwarf2_cu *cu,
21255 const gdb_byte *line_ptr,
21256 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21257
21258 void handle_set_discriminator (unsigned int discriminator)
21259 {
21260 m_discriminator = discriminator;
21261 m_line_has_non_zero_discriminator |= discriminator != 0;
21262 }
21263
21264 /* Handle DW_LNE_set_address. */
21265 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21266 {
21267 m_op_index = 0;
21268 address += baseaddr;
21269 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21270 }
21271
21272 /* Handle DW_LNS_advance_pc. */
21273 void handle_advance_pc (CORE_ADDR adjust);
21274
21275 /* Handle a special opcode. */
21276 void handle_special_opcode (unsigned char op_code);
21277
21278 /* Handle DW_LNS_advance_line. */
21279 void handle_advance_line (int line_delta)
21280 {
21281 advance_line (line_delta);
21282 }
21283
21284 /* Handle DW_LNS_set_file. */
21285 void handle_set_file (file_name_index file);
21286
21287 /* Handle DW_LNS_negate_stmt. */
21288 void handle_negate_stmt ()
21289 {
21290 m_is_stmt = !m_is_stmt;
21291 }
21292
21293 /* Handle DW_LNS_const_add_pc. */
21294 void handle_const_add_pc ();
21295
21296 /* Handle DW_LNS_fixed_advance_pc. */
21297 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21298 {
21299 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21300 m_op_index = 0;
21301 }
21302
21303 /* Handle DW_LNS_copy. */
21304 void handle_copy ()
21305 {
21306 record_line (false);
21307 m_discriminator = 0;
21308 }
21309
21310 /* Handle DW_LNE_end_sequence. */
21311 void handle_end_sequence ()
21312 {
21313 m_currently_recording_lines = true;
21314 }
21315
21316 private:
21317 /* Advance the line by LINE_DELTA. */
21318 void advance_line (int line_delta)
21319 {
21320 m_line += line_delta;
21321
21322 if (line_delta != 0)
21323 m_line_has_non_zero_discriminator = m_discriminator != 0;
21324 }
21325
21326 struct dwarf2_cu *m_cu;
21327
21328 gdbarch *m_gdbarch;
21329
21330 /* True if we're recording lines.
21331 Otherwise we're building partial symtabs and are just interested in
21332 finding include files mentioned by the line number program. */
21333 bool m_record_lines_p;
21334
21335 /* The line number header. */
21336 line_header *m_line_header;
21337
21338 /* These are part of the standard DWARF line number state machine,
21339 and initialized according to the DWARF spec. */
21340
21341 unsigned char m_op_index = 0;
21342 /* The line table index of the current file. */
21343 file_name_index m_file = 1;
21344 unsigned int m_line = 1;
21345
21346 /* These are initialized in the constructor. */
21347
21348 CORE_ADDR m_address;
21349 bool m_is_stmt;
21350 unsigned int m_discriminator;
21351
21352 /* Additional bits of state we need to track. */
21353
21354 /* The last file that we called dwarf2_start_subfile for.
21355 This is only used for TLLs. */
21356 unsigned int m_last_file = 0;
21357 /* The last file a line number was recorded for. */
21358 struct subfile *m_last_subfile = NULL;
21359
21360 /* The address of the last line entry. */
21361 CORE_ADDR m_last_address;
21362
21363 /* Set to true when a previous line at the same address (using
21364 m_last_address) had m_is_stmt true. This is reset to false when a
21365 line entry at a new address (m_address different to m_last_address) is
21366 processed. */
21367 bool m_stmt_at_address = false;
21368
21369 /* When true, record the lines we decode. */
21370 bool m_currently_recording_lines = false;
21371
21372 /* The last line number that was recorded, used to coalesce
21373 consecutive entries for the same line. This can happen, for
21374 example, when discriminators are present. PR 17276. */
21375 unsigned int m_last_line = 0;
21376 bool m_line_has_non_zero_discriminator = false;
21377 };
21378
21379 void
21380 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21381 {
21382 CORE_ADDR addr_adj = (((m_op_index + adjust)
21383 / m_line_header->maximum_ops_per_instruction)
21384 * m_line_header->minimum_instruction_length);
21385 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21386 m_op_index = ((m_op_index + adjust)
21387 % m_line_header->maximum_ops_per_instruction);
21388 }
21389
21390 void
21391 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21392 {
21393 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21394 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21395 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21396 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21397 / m_line_header->maximum_ops_per_instruction)
21398 * m_line_header->minimum_instruction_length);
21399 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21400 m_op_index = ((m_op_index + adj_opcode_d)
21401 % m_line_header->maximum_ops_per_instruction);
21402
21403 int line_delta = m_line_header->line_base + adj_opcode_r;
21404 advance_line (line_delta);
21405 record_line (false);
21406 m_discriminator = 0;
21407 }
21408
21409 void
21410 lnp_state_machine::handle_set_file (file_name_index file)
21411 {
21412 m_file = file;
21413
21414 const file_entry *fe = current_file ();
21415 if (fe == NULL)
21416 dwarf2_debug_line_missing_file_complaint ();
21417 else if (m_record_lines_p)
21418 {
21419 const char *dir = fe->include_dir (m_line_header);
21420
21421 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21422 m_line_has_non_zero_discriminator = m_discriminator != 0;
21423 dwarf2_start_subfile (m_cu, fe->name, dir);
21424 }
21425 }
21426
21427 void
21428 lnp_state_machine::handle_const_add_pc ()
21429 {
21430 CORE_ADDR adjust
21431 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21432
21433 CORE_ADDR addr_adj
21434 = (((m_op_index + adjust)
21435 / m_line_header->maximum_ops_per_instruction)
21436 * m_line_header->minimum_instruction_length);
21437
21438 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21439 m_op_index = ((m_op_index + adjust)
21440 % m_line_header->maximum_ops_per_instruction);
21441 }
21442
21443 /* Return non-zero if we should add LINE to the line number table.
21444 LINE is the line to add, LAST_LINE is the last line that was added,
21445 LAST_SUBFILE is the subfile for LAST_LINE.
21446 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21447 had a non-zero discriminator.
21448
21449 We have to be careful in the presence of discriminators.
21450 E.g., for this line:
21451
21452 for (i = 0; i < 100000; i++);
21453
21454 clang can emit four line number entries for that one line,
21455 each with a different discriminator.
21456 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21457
21458 However, we want gdb to coalesce all four entries into one.
21459 Otherwise the user could stepi into the middle of the line and
21460 gdb would get confused about whether the pc really was in the
21461 middle of the line.
21462
21463 Things are further complicated by the fact that two consecutive
21464 line number entries for the same line is a heuristic used by gcc
21465 to denote the end of the prologue. So we can't just discard duplicate
21466 entries, we have to be selective about it. The heuristic we use is
21467 that we only collapse consecutive entries for the same line if at least
21468 one of those entries has a non-zero discriminator. PR 17276.
21469
21470 Note: Addresses in the line number state machine can never go backwards
21471 within one sequence, thus this coalescing is ok. */
21472
21473 static int
21474 dwarf_record_line_p (struct dwarf2_cu *cu,
21475 unsigned int line, unsigned int last_line,
21476 int line_has_non_zero_discriminator,
21477 struct subfile *last_subfile)
21478 {
21479 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21480 return 1;
21481 if (line != last_line)
21482 return 1;
21483 /* Same line for the same file that we've seen already.
21484 As a last check, for pr 17276, only record the line if the line
21485 has never had a non-zero discriminator. */
21486 if (!line_has_non_zero_discriminator)
21487 return 1;
21488 return 0;
21489 }
21490
21491 /* Use the CU's builder to record line number LINE beginning at
21492 address ADDRESS in the line table of subfile SUBFILE. */
21493
21494 static void
21495 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21496 unsigned int line, CORE_ADDR address, bool is_stmt,
21497 struct dwarf2_cu *cu)
21498 {
21499 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21500
21501 if (dwarf_line_debug)
21502 {
21503 fprintf_unfiltered (gdb_stdlog,
21504 "Recording line %u, file %s, address %s\n",
21505 line, lbasename (subfile->name),
21506 paddress (gdbarch, address));
21507 }
21508
21509 if (cu != nullptr)
21510 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21511 }
21512
21513 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21514 Mark the end of a set of line number records.
21515 The arguments are the same as for dwarf_record_line_1.
21516 If SUBFILE is NULL the request is ignored. */
21517
21518 static void
21519 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21520 CORE_ADDR address, struct dwarf2_cu *cu)
21521 {
21522 if (subfile == NULL)
21523 return;
21524
21525 if (dwarf_line_debug)
21526 {
21527 fprintf_unfiltered (gdb_stdlog,
21528 "Finishing current line, file %s, address %s\n",
21529 lbasename (subfile->name),
21530 paddress (gdbarch, address));
21531 }
21532
21533 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21534 }
21535
21536 void
21537 lnp_state_machine::record_line (bool end_sequence)
21538 {
21539 if (dwarf_line_debug)
21540 {
21541 fprintf_unfiltered (gdb_stdlog,
21542 "Processing actual line %u: file %u,"
21543 " address %s, is_stmt %u, discrim %u%s\n",
21544 m_line, m_file,
21545 paddress (m_gdbarch, m_address),
21546 m_is_stmt, m_discriminator,
21547 (end_sequence ? "\t(end sequence)" : ""));
21548 }
21549
21550 file_entry *fe = current_file ();
21551
21552 if (fe == NULL)
21553 dwarf2_debug_line_missing_file_complaint ();
21554 /* For now we ignore lines not starting on an instruction boundary.
21555 But not when processing end_sequence for compatibility with the
21556 previous version of the code. */
21557 else if (m_op_index == 0 || end_sequence)
21558 {
21559 fe->included_p = 1;
21560 if (m_record_lines_p)
21561 {
21562 /* When we switch files we insert an end maker in the first file,
21563 switch to the second file and add a new line entry. The
21564 problem is that the end marker inserted in the first file will
21565 discard any previous line entries at the same address. If the
21566 line entries in the first file are marked as is-stmt, while
21567 the new line in the second file is non-stmt, then this means
21568 the end marker will discard is-stmt lines so we can have a
21569 non-stmt line. This means that there are less addresses at
21570 which the user can insert a breakpoint.
21571
21572 To improve this we track the last address in m_last_address,
21573 and whether we have seen an is-stmt at this address. Then
21574 when switching files, if we have seen a stmt at the current
21575 address, and we are switching to create a non-stmt line, then
21576 discard the new line. */
21577 bool file_changed
21578 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21579 bool ignore_this_line
21580 = ((file_changed && !end_sequence && m_last_address == m_address
21581 && !m_is_stmt && m_stmt_at_address)
21582 || (!end_sequence && m_line == 0));
21583
21584 if ((file_changed && !ignore_this_line) || end_sequence)
21585 {
21586 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21587 m_currently_recording_lines ? m_cu : nullptr);
21588 }
21589
21590 if (!end_sequence && !ignore_this_line)
21591 {
21592 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21593
21594 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21595 m_line_has_non_zero_discriminator,
21596 m_last_subfile))
21597 {
21598 buildsym_compunit *builder = m_cu->get_builder ();
21599 dwarf_record_line_1 (m_gdbarch,
21600 builder->get_current_subfile (),
21601 m_line, m_address, is_stmt,
21602 m_currently_recording_lines ? m_cu : nullptr);
21603 }
21604 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21605 m_last_line = m_line;
21606 }
21607 }
21608 }
21609
21610 /* Track whether we have seen any m_is_stmt true at m_address in case we
21611 have multiple line table entries all at m_address. */
21612 if (m_last_address != m_address)
21613 {
21614 m_stmt_at_address = false;
21615 m_last_address = m_address;
21616 }
21617 m_stmt_at_address |= m_is_stmt;
21618 }
21619
21620 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21621 line_header *lh, bool record_lines_p)
21622 {
21623 m_cu = cu;
21624 m_gdbarch = arch;
21625 m_record_lines_p = record_lines_p;
21626 m_line_header = lh;
21627
21628 m_currently_recording_lines = true;
21629
21630 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21631 was a line entry for it so that the backend has a chance to adjust it
21632 and also record it in case it needs it. This is currently used by MIPS
21633 code, cf. `mips_adjust_dwarf2_line'. */
21634 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21635 m_is_stmt = lh->default_is_stmt;
21636 m_discriminator = 0;
21637
21638 m_last_address = m_address;
21639 m_stmt_at_address = false;
21640 }
21641
21642 void
21643 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21644 const gdb_byte *line_ptr,
21645 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21646 {
21647 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21648 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21649 located at 0x0. In this case, additionally check that if
21650 ADDRESS < UNRELOCATED_LOWPC. */
21651
21652 if ((address == 0 && address < unrelocated_lowpc)
21653 || address == (CORE_ADDR) -1)
21654 {
21655 /* This line table is for a function which has been
21656 GCd by the linker. Ignore it. PR gdb/12528 */
21657
21658 struct objfile *objfile = cu->per_objfile->objfile;
21659 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21660
21661 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21662 line_offset, objfile_name (objfile));
21663 m_currently_recording_lines = false;
21664 /* Note: m_currently_recording_lines is left as false until we see
21665 DW_LNE_end_sequence. */
21666 }
21667 }
21668
21669 /* Subroutine of dwarf_decode_lines to simplify it.
21670 Process the line number information in LH.
21671 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21672 program in order to set included_p for every referenced header. */
21673
21674 static void
21675 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21676 const int decode_for_pst_p, CORE_ADDR lowpc)
21677 {
21678 const gdb_byte *line_ptr, *extended_end;
21679 const gdb_byte *line_end;
21680 unsigned int bytes_read, extended_len;
21681 unsigned char op_code, extended_op;
21682 CORE_ADDR baseaddr;
21683 struct objfile *objfile = cu->per_objfile->objfile;
21684 bfd *abfd = objfile->obfd;
21685 struct gdbarch *gdbarch = objfile->arch ();
21686 /* True if we're recording line info (as opposed to building partial
21687 symtabs and just interested in finding include files mentioned by
21688 the line number program). */
21689 bool record_lines_p = !decode_for_pst_p;
21690
21691 baseaddr = objfile->text_section_offset ();
21692
21693 line_ptr = lh->statement_program_start;
21694 line_end = lh->statement_program_end;
21695
21696 /* Read the statement sequences until there's nothing left. */
21697 while (line_ptr < line_end)
21698 {
21699 /* The DWARF line number program state machine. Reset the state
21700 machine at the start of each sequence. */
21701 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21702 bool end_sequence = false;
21703
21704 if (record_lines_p)
21705 {
21706 /* Start a subfile for the current file of the state
21707 machine. */
21708 const file_entry *fe = state_machine.current_file ();
21709
21710 if (fe != NULL)
21711 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21712 }
21713
21714 /* Decode the table. */
21715 while (line_ptr < line_end && !end_sequence)
21716 {
21717 op_code = read_1_byte (abfd, line_ptr);
21718 line_ptr += 1;
21719
21720 if (op_code >= lh->opcode_base)
21721 {
21722 /* Special opcode. */
21723 state_machine.handle_special_opcode (op_code);
21724 }
21725 else switch (op_code)
21726 {
21727 case DW_LNS_extended_op:
21728 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21729 &bytes_read);
21730 line_ptr += bytes_read;
21731 extended_end = line_ptr + extended_len;
21732 extended_op = read_1_byte (abfd, line_ptr);
21733 line_ptr += 1;
21734 if (DW_LNE_lo_user <= extended_op
21735 && extended_op <= DW_LNE_hi_user)
21736 {
21737 /* Vendor extension, ignore. */
21738 line_ptr = extended_end;
21739 break;
21740 }
21741 switch (extended_op)
21742 {
21743 case DW_LNE_end_sequence:
21744 state_machine.handle_end_sequence ();
21745 end_sequence = true;
21746 break;
21747 case DW_LNE_set_address:
21748 {
21749 CORE_ADDR address
21750 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21751 line_ptr += bytes_read;
21752
21753 state_machine.check_line_address (cu, line_ptr,
21754 lowpc - baseaddr, address);
21755 state_machine.handle_set_address (baseaddr, address);
21756 }
21757 break;
21758 case DW_LNE_define_file:
21759 {
21760 const char *cur_file;
21761 unsigned int mod_time, length;
21762 dir_index dindex;
21763
21764 cur_file = read_direct_string (abfd, line_ptr,
21765 &bytes_read);
21766 line_ptr += bytes_read;
21767 dindex = (dir_index)
21768 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21769 line_ptr += bytes_read;
21770 mod_time =
21771 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21772 line_ptr += bytes_read;
21773 length =
21774 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21775 line_ptr += bytes_read;
21776 lh->add_file_name (cur_file, dindex, mod_time, length);
21777 }
21778 break;
21779 case DW_LNE_set_discriminator:
21780 {
21781 /* The discriminator is not interesting to the
21782 debugger; just ignore it. We still need to
21783 check its value though:
21784 if there are consecutive entries for the same
21785 (non-prologue) line we want to coalesce them.
21786 PR 17276. */
21787 unsigned int discr
21788 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21789 line_ptr += bytes_read;
21790
21791 state_machine.handle_set_discriminator (discr);
21792 }
21793 break;
21794 default:
21795 complaint (_("mangled .debug_line section"));
21796 return;
21797 }
21798 /* Make sure that we parsed the extended op correctly. If e.g.
21799 we expected a different address size than the producer used,
21800 we may have read the wrong number of bytes. */
21801 if (line_ptr != extended_end)
21802 {
21803 complaint (_("mangled .debug_line section"));
21804 return;
21805 }
21806 break;
21807 case DW_LNS_copy:
21808 state_machine.handle_copy ();
21809 break;
21810 case DW_LNS_advance_pc:
21811 {
21812 CORE_ADDR adjust
21813 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21814 line_ptr += bytes_read;
21815
21816 state_machine.handle_advance_pc (adjust);
21817 }
21818 break;
21819 case DW_LNS_advance_line:
21820 {
21821 int line_delta
21822 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21823 line_ptr += bytes_read;
21824
21825 state_machine.handle_advance_line (line_delta);
21826 }
21827 break;
21828 case DW_LNS_set_file:
21829 {
21830 file_name_index file
21831 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21832 &bytes_read);
21833 line_ptr += bytes_read;
21834
21835 state_machine.handle_set_file (file);
21836 }
21837 break;
21838 case DW_LNS_set_column:
21839 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21840 line_ptr += bytes_read;
21841 break;
21842 case DW_LNS_negate_stmt:
21843 state_machine.handle_negate_stmt ();
21844 break;
21845 case DW_LNS_set_basic_block:
21846 break;
21847 /* Add to the address register of the state machine the
21848 address increment value corresponding to special opcode
21849 255. I.e., this value is scaled by the minimum
21850 instruction length since special opcode 255 would have
21851 scaled the increment. */
21852 case DW_LNS_const_add_pc:
21853 state_machine.handle_const_add_pc ();
21854 break;
21855 case DW_LNS_fixed_advance_pc:
21856 {
21857 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21858 line_ptr += 2;
21859
21860 state_machine.handle_fixed_advance_pc (addr_adj);
21861 }
21862 break;
21863 default:
21864 {
21865 /* Unknown standard opcode, ignore it. */
21866 int i;
21867
21868 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21869 {
21870 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21871 line_ptr += bytes_read;
21872 }
21873 }
21874 }
21875 }
21876
21877 if (!end_sequence)
21878 dwarf2_debug_line_missing_end_sequence_complaint ();
21879
21880 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21881 in which case we still finish recording the last line). */
21882 state_machine.record_line (true);
21883 }
21884 }
21885
21886 /* Decode the Line Number Program (LNP) for the given line_header
21887 structure and CU. The actual information extracted and the type
21888 of structures created from the LNP depends on the value of PST.
21889
21890 1. If PST is NULL, then this procedure uses the data from the program
21891 to create all necessary symbol tables, and their linetables.
21892
21893 2. If PST is not NULL, this procedure reads the program to determine
21894 the list of files included by the unit represented by PST, and
21895 builds all the associated partial symbol tables.
21896
21897 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21898 It is used for relative paths in the line table.
21899 NOTE: When processing partial symtabs (pst != NULL),
21900 comp_dir == pst->dirname.
21901
21902 NOTE: It is important that psymtabs have the same file name (via strcmp)
21903 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21904 symtab we don't use it in the name of the psymtabs we create.
21905 E.g. expand_line_sal requires this when finding psymtabs to expand.
21906 A good testcase for this is mb-inline.exp.
21907
21908 LOWPC is the lowest address in CU (or 0 if not known).
21909
21910 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21911 for its PC<->lines mapping information. Otherwise only the filename
21912 table is read in. */
21913
21914 static void
21915 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21916 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21917 CORE_ADDR lowpc, int decode_mapping)
21918 {
21919 struct objfile *objfile = cu->per_objfile->objfile;
21920 const int decode_for_pst_p = (pst != NULL);
21921
21922 if (decode_mapping)
21923 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21924
21925 if (decode_for_pst_p)
21926 {
21927 /* Now that we're done scanning the Line Header Program, we can
21928 create the psymtab of each included file. */
21929 for (auto &file_entry : lh->file_names ())
21930 if (file_entry.included_p == 1)
21931 {
21932 gdb::unique_xmalloc_ptr<char> name_holder;
21933 const char *include_name =
21934 psymtab_include_file_name (lh, file_entry, pst,
21935 comp_dir, &name_holder);
21936 if (include_name != NULL)
21937 dwarf2_create_include_psymtab (include_name, pst, objfile);
21938 }
21939 }
21940 else
21941 {
21942 /* Make sure a symtab is created for every file, even files
21943 which contain only variables (i.e. no code with associated
21944 line numbers). */
21945 buildsym_compunit *builder = cu->get_builder ();
21946 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21947
21948 for (auto &fe : lh->file_names ())
21949 {
21950 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21951 if (builder->get_current_subfile ()->symtab == NULL)
21952 {
21953 builder->get_current_subfile ()->symtab
21954 = allocate_symtab (cust,
21955 builder->get_current_subfile ()->name);
21956 }
21957 fe.symtab = builder->get_current_subfile ()->symtab;
21958 }
21959 }
21960 }
21961
21962 /* Start a subfile for DWARF. FILENAME is the name of the file and
21963 DIRNAME the name of the source directory which contains FILENAME
21964 or NULL if not known.
21965 This routine tries to keep line numbers from identical absolute and
21966 relative file names in a common subfile.
21967
21968 Using the `list' example from the GDB testsuite, which resides in
21969 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21970 of /srcdir/list0.c yields the following debugging information for list0.c:
21971
21972 DW_AT_name: /srcdir/list0.c
21973 DW_AT_comp_dir: /compdir
21974 files.files[0].name: list0.h
21975 files.files[0].dir: /srcdir
21976 files.files[1].name: list0.c
21977 files.files[1].dir: /srcdir
21978
21979 The line number information for list0.c has to end up in a single
21980 subfile, so that `break /srcdir/list0.c:1' works as expected.
21981 start_subfile will ensure that this happens provided that we pass the
21982 concatenation of files.files[1].dir and files.files[1].name as the
21983 subfile's name. */
21984
21985 static void
21986 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21987 const char *dirname)
21988 {
21989 gdb::unique_xmalloc_ptr<char> copy;
21990
21991 /* In order not to lose the line information directory,
21992 we concatenate it to the filename when it makes sense.
21993 Note that the Dwarf3 standard says (speaking of filenames in line
21994 information): ``The directory index is ignored for file names
21995 that represent full path names''. Thus ignoring dirname in the
21996 `else' branch below isn't an issue. */
21997
21998 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21999 {
22000 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
22001 filename = copy.get ();
22002 }
22003
22004 cu->get_builder ()->start_subfile (filename);
22005 }
22006
22007 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
22008 buildsym_compunit constructor. */
22009
22010 struct compunit_symtab *
22011 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
22012 CORE_ADDR low_pc)
22013 {
22014 gdb_assert (m_builder == nullptr);
22015
22016 m_builder.reset (new struct buildsym_compunit
22017 (this->per_objfile->objfile,
22018 name, comp_dir, language, low_pc));
22019
22020 list_in_scope = get_builder ()->get_file_symbols ();
22021
22022 get_builder ()->record_debugformat ("DWARF 2");
22023 get_builder ()->record_producer (producer);
22024
22025 processing_has_namespace_info = false;
22026
22027 return get_builder ()->get_compunit_symtab ();
22028 }
22029
22030 static void
22031 var_decode_location (struct attribute *attr, struct symbol *sym,
22032 struct dwarf2_cu *cu)
22033 {
22034 struct objfile *objfile = cu->per_objfile->objfile;
22035 struct comp_unit_head *cu_header = &cu->header;
22036
22037 /* NOTE drow/2003-01-30: There used to be a comment and some special
22038 code here to turn a symbol with DW_AT_external and a
22039 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22040 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22041 with some versions of binutils) where shared libraries could have
22042 relocations against symbols in their debug information - the
22043 minimal symbol would have the right address, but the debug info
22044 would not. It's no longer necessary, because we will explicitly
22045 apply relocations when we read in the debug information now. */
22046
22047 /* A DW_AT_location attribute with no contents indicates that a
22048 variable has been optimized away. */
22049 if (attr->form_is_block () && attr->as_block ()->size == 0)
22050 {
22051 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22052 return;
22053 }
22054
22055 /* Handle one degenerate form of location expression specially, to
22056 preserve GDB's previous behavior when section offsets are
22057 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22058 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
22059
22060 if (attr->form_is_block ())
22061 {
22062 struct dwarf_block *block = attr->as_block ();
22063
22064 if ((block->data[0] == DW_OP_addr
22065 && block->size == 1 + cu_header->addr_size)
22066 || ((block->data[0] == DW_OP_GNU_addr_index
22067 || block->data[0] == DW_OP_addrx)
22068 && (block->size
22069 == 1 + leb128_size (&block->data[1]))))
22070 {
22071 unsigned int dummy;
22072
22073 if (block->data[0] == DW_OP_addr)
22074 SET_SYMBOL_VALUE_ADDRESS
22075 (sym, cu->header.read_address (objfile->obfd,
22076 block->data + 1,
22077 &dummy));
22078 else
22079 SET_SYMBOL_VALUE_ADDRESS
22080 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22081 &dummy));
22082 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22083 fixup_symbol_section (sym, objfile);
22084 SET_SYMBOL_VALUE_ADDRESS
22085 (sym,
22086 SYMBOL_VALUE_ADDRESS (sym)
22087 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
22088 return;
22089 }
22090 }
22091
22092 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22093 expression evaluator, and use LOC_COMPUTED only when necessary
22094 (i.e. when the value of a register or memory location is
22095 referenced, or a thread-local block, etc.). Then again, it might
22096 not be worthwhile. I'm assuming that it isn't unless performance
22097 or memory numbers show me otherwise. */
22098
22099 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
22100
22101 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
22102 cu->has_loclist = true;
22103 }
22104
22105 /* Given a pointer to a DWARF information entry, figure out if we need
22106 to make a symbol table entry for it, and if so, create a new entry
22107 and return a pointer to it.
22108 If TYPE is NULL, determine symbol type from the die, otherwise
22109 used the passed type.
22110 If SPACE is not NULL, use it to hold the new symbol. If it is
22111 NULL, allocate a new symbol on the objfile's obstack. */
22112
22113 static struct symbol *
22114 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22115 struct symbol *space)
22116 {
22117 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22118 struct objfile *objfile = per_objfile->objfile;
22119 struct gdbarch *gdbarch = objfile->arch ();
22120 struct symbol *sym = NULL;
22121 const char *name;
22122 struct attribute *attr = NULL;
22123 struct attribute *attr2 = NULL;
22124 CORE_ADDR baseaddr;
22125 struct pending **list_to_add = NULL;
22126
22127 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
22128
22129 baseaddr = objfile->text_section_offset ();
22130
22131 name = dwarf2_name (die, cu);
22132 if (name)
22133 {
22134 int suppress_add = 0;
22135
22136 if (space)
22137 sym = space;
22138 else
22139 sym = new (&objfile->objfile_obstack) symbol;
22140 OBJSTAT (objfile, n_syms++);
22141
22142 /* Cache this symbol's name and the name's demangled form (if any). */
22143 sym->set_language (cu->language, &objfile->objfile_obstack);
22144 /* Fortran does not have mangling standard and the mangling does differ
22145 between gfortran, iFort etc. */
22146 const char *physname
22147 = (cu->language == language_fortran
22148 ? dwarf2_full_name (name, die, cu)
22149 : dwarf2_physname (name, die, cu));
22150 const char *linkagename = dw2_linkage_name (die, cu);
22151
22152 if (linkagename == nullptr || cu->language == language_ada)
22153 sym->set_linkage_name (physname);
22154 else
22155 {
22156 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22157 sym->set_linkage_name (linkagename);
22158 }
22159
22160 /* Default assumptions.
22161 Use the passed type or decode it from the die. */
22162 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22163 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22164 if (type != NULL)
22165 SYMBOL_TYPE (sym) = type;
22166 else
22167 SYMBOL_TYPE (sym) = die_type (die, cu);
22168 attr = dwarf2_attr (die,
22169 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22170 cu);
22171 if (attr != nullptr)
22172 SYMBOL_LINE (sym) = attr->constant_value (0);
22173
22174 attr = dwarf2_attr (die,
22175 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22176 cu);
22177 if (attr != nullptr && attr->form_is_unsigned ())
22178 {
22179 file_name_index file_index
22180 = (file_name_index) attr->as_unsigned ();
22181 struct file_entry *fe;
22182
22183 if (cu->line_header != NULL)
22184 fe = cu->line_header->file_name_at (file_index);
22185 else
22186 fe = NULL;
22187
22188 if (fe == NULL)
22189 complaint (_("file index out of range"));
22190 else
22191 symbol_set_symtab (sym, fe->symtab);
22192 }
22193
22194 switch (die->tag)
22195 {
22196 case DW_TAG_label:
22197 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22198 if (attr != nullptr)
22199 {
22200 CORE_ADDR addr;
22201
22202 addr = attr->as_address ();
22203 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22204 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22205 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22206 }
22207 else
22208 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22209 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22210 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22211 add_symbol_to_list (sym, cu->list_in_scope);
22212 break;
22213 case DW_TAG_subprogram:
22214 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22215 finish_block. */
22216 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22217 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22218 if ((attr2 != nullptr && attr2->as_boolean ())
22219 || cu->language == language_ada
22220 || cu->language == language_fortran)
22221 {
22222 /* Subprograms marked external are stored as a global symbol.
22223 Ada and Fortran subprograms, whether marked external or
22224 not, are always stored as a global symbol, because we want
22225 to be able to access them globally. For instance, we want
22226 to be able to break on a nested subprogram without having
22227 to specify the context. */
22228 list_to_add = cu->get_builder ()->get_global_symbols ();
22229 }
22230 else
22231 {
22232 list_to_add = cu->list_in_scope;
22233 }
22234 break;
22235 case DW_TAG_inlined_subroutine:
22236 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22237 finish_block. */
22238 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22239 SYMBOL_INLINED (sym) = 1;
22240 list_to_add = cu->list_in_scope;
22241 break;
22242 case DW_TAG_template_value_param:
22243 suppress_add = 1;
22244 /* Fall through. */
22245 case DW_TAG_constant:
22246 case DW_TAG_variable:
22247 case DW_TAG_member:
22248 /* Compilation with minimal debug info may result in
22249 variables with missing type entries. Change the
22250 misleading `void' type to something sensible. */
22251 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22252 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22253
22254 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22255 /* In the case of DW_TAG_member, we should only be called for
22256 static const members. */
22257 if (die->tag == DW_TAG_member)
22258 {
22259 /* dwarf2_add_field uses die_is_declaration,
22260 so we do the same. */
22261 gdb_assert (die_is_declaration (die, cu));
22262 gdb_assert (attr);
22263 }
22264 if (attr != nullptr)
22265 {
22266 dwarf2_const_value (attr, sym, cu);
22267 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22268 if (!suppress_add)
22269 {
22270 if (attr2 != nullptr && attr2->as_boolean ())
22271 list_to_add = cu->get_builder ()->get_global_symbols ();
22272 else
22273 list_to_add = cu->list_in_scope;
22274 }
22275 break;
22276 }
22277 attr = dwarf2_attr (die, DW_AT_location, cu);
22278 if (attr != nullptr)
22279 {
22280 var_decode_location (attr, sym, cu);
22281 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22282
22283 /* Fortran explicitly imports any global symbols to the local
22284 scope by DW_TAG_common_block. */
22285 if (cu->language == language_fortran && die->parent
22286 && die->parent->tag == DW_TAG_common_block)
22287 attr2 = NULL;
22288
22289 if (SYMBOL_CLASS (sym) == LOC_STATIC
22290 && SYMBOL_VALUE_ADDRESS (sym) == 0
22291 && !per_objfile->per_bfd->has_section_at_zero)
22292 {
22293 /* When a static variable is eliminated by the linker,
22294 the corresponding debug information is not stripped
22295 out, but the variable address is set to null;
22296 do not add such variables into symbol table. */
22297 }
22298 else if (attr2 != nullptr && attr2->as_boolean ())
22299 {
22300 if (SYMBOL_CLASS (sym) == LOC_STATIC
22301 && (objfile->flags & OBJF_MAINLINE) == 0
22302 && per_objfile->per_bfd->can_copy)
22303 {
22304 /* A global static variable might be subject to
22305 copy relocation. We first check for a local
22306 minsym, though, because maybe the symbol was
22307 marked hidden, in which case this would not
22308 apply. */
22309 bound_minimal_symbol found
22310 = (lookup_minimal_symbol_linkage
22311 (sym->linkage_name (), objfile));
22312 if (found.minsym != nullptr)
22313 sym->maybe_copied = 1;
22314 }
22315
22316 /* A variable with DW_AT_external is never static,
22317 but it may be block-scoped. */
22318 list_to_add
22319 = ((cu->list_in_scope
22320 == cu->get_builder ()->get_file_symbols ())
22321 ? cu->get_builder ()->get_global_symbols ()
22322 : cu->list_in_scope);
22323 }
22324 else
22325 list_to_add = cu->list_in_scope;
22326 }
22327 else
22328 {
22329 /* We do not know the address of this symbol.
22330 If it is an external symbol and we have type information
22331 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22332 The address of the variable will then be determined from
22333 the minimal symbol table whenever the variable is
22334 referenced. */
22335 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22336
22337 /* Fortran explicitly imports any global symbols to the local
22338 scope by DW_TAG_common_block. */
22339 if (cu->language == language_fortran && die->parent
22340 && die->parent->tag == DW_TAG_common_block)
22341 {
22342 /* SYMBOL_CLASS doesn't matter here because
22343 read_common_block is going to reset it. */
22344 if (!suppress_add)
22345 list_to_add = cu->list_in_scope;
22346 }
22347 else if (attr2 != nullptr && attr2->as_boolean ()
22348 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22349 {
22350 /* A variable with DW_AT_external is never static, but it
22351 may be block-scoped. */
22352 list_to_add
22353 = ((cu->list_in_scope
22354 == cu->get_builder ()->get_file_symbols ())
22355 ? cu->get_builder ()->get_global_symbols ()
22356 : cu->list_in_scope);
22357
22358 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22359 }
22360 else if (!die_is_declaration (die, cu))
22361 {
22362 /* Use the default LOC_OPTIMIZED_OUT class. */
22363 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22364 if (!suppress_add)
22365 list_to_add = cu->list_in_scope;
22366 }
22367 }
22368 break;
22369 case DW_TAG_formal_parameter:
22370 {
22371 /* If we are inside a function, mark this as an argument. If
22372 not, we might be looking at an argument to an inlined function
22373 when we do not have enough information to show inlined frames;
22374 pretend it's a local variable in that case so that the user can
22375 still see it. */
22376 struct context_stack *curr
22377 = cu->get_builder ()->get_current_context_stack ();
22378 if (curr != nullptr && curr->name != nullptr)
22379 SYMBOL_IS_ARGUMENT (sym) = 1;
22380 attr = dwarf2_attr (die, DW_AT_location, cu);
22381 if (attr != nullptr)
22382 {
22383 var_decode_location (attr, sym, cu);
22384 }
22385 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22386 if (attr != nullptr)
22387 {
22388 dwarf2_const_value (attr, sym, cu);
22389 }
22390
22391 list_to_add = cu->list_in_scope;
22392 }
22393 break;
22394 case DW_TAG_unspecified_parameters:
22395 /* From varargs functions; gdb doesn't seem to have any
22396 interest in this information, so just ignore it for now.
22397 (FIXME?) */
22398 break;
22399 case DW_TAG_template_type_param:
22400 suppress_add = 1;
22401 /* Fall through. */
22402 case DW_TAG_class_type:
22403 case DW_TAG_interface_type:
22404 case DW_TAG_structure_type:
22405 case DW_TAG_union_type:
22406 case DW_TAG_set_type:
22407 case DW_TAG_enumeration_type:
22408 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22409 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22410
22411 {
22412 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22413 really ever be static objects: otherwise, if you try
22414 to, say, break of a class's method and you're in a file
22415 which doesn't mention that class, it won't work unless
22416 the check for all static symbols in lookup_symbol_aux
22417 saves you. See the OtherFileClass tests in
22418 gdb.c++/namespace.exp. */
22419
22420 if (!suppress_add)
22421 {
22422 buildsym_compunit *builder = cu->get_builder ();
22423 list_to_add
22424 = (cu->list_in_scope == builder->get_file_symbols ()
22425 && cu->language == language_cplus
22426 ? builder->get_global_symbols ()
22427 : cu->list_in_scope);
22428
22429 /* The semantics of C++ state that "struct foo {
22430 ... }" also defines a typedef for "foo". */
22431 if (cu->language == language_cplus
22432 || cu->language == language_ada
22433 || cu->language == language_d
22434 || cu->language == language_rust)
22435 {
22436 /* The symbol's name is already allocated along
22437 with this objfile, so we don't need to
22438 duplicate it for the type. */
22439 if (SYMBOL_TYPE (sym)->name () == 0)
22440 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22441 }
22442 }
22443 }
22444 break;
22445 case DW_TAG_typedef:
22446 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22447 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22448 list_to_add = cu->list_in_scope;
22449 break;
22450 case DW_TAG_array_type:
22451 case DW_TAG_base_type:
22452 case DW_TAG_subrange_type:
22453 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22454 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22455 list_to_add = cu->list_in_scope;
22456 break;
22457 case DW_TAG_enumerator:
22458 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22459 if (attr != nullptr)
22460 {
22461 dwarf2_const_value (attr, sym, cu);
22462 }
22463 {
22464 /* NOTE: carlton/2003-11-10: See comment above in the
22465 DW_TAG_class_type, etc. block. */
22466
22467 list_to_add
22468 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22469 && cu->language == language_cplus
22470 ? cu->get_builder ()->get_global_symbols ()
22471 : cu->list_in_scope);
22472 }
22473 break;
22474 case DW_TAG_imported_declaration:
22475 case DW_TAG_namespace:
22476 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22477 list_to_add = cu->get_builder ()->get_global_symbols ();
22478 break;
22479 case DW_TAG_module:
22480 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22481 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22482 list_to_add = cu->get_builder ()->get_global_symbols ();
22483 break;
22484 case DW_TAG_common_block:
22485 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22486 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22487 add_symbol_to_list (sym, cu->list_in_scope);
22488 break;
22489 default:
22490 /* Not a tag we recognize. Hopefully we aren't processing
22491 trash data, but since we must specifically ignore things
22492 we don't recognize, there is nothing else we should do at
22493 this point. */
22494 complaint (_("unsupported tag: '%s'"),
22495 dwarf_tag_name (die->tag));
22496 break;
22497 }
22498
22499 if (suppress_add)
22500 {
22501 sym->hash_next = objfile->template_symbols;
22502 objfile->template_symbols = sym;
22503 list_to_add = NULL;
22504 }
22505
22506 if (list_to_add != NULL)
22507 add_symbol_to_list (sym, list_to_add);
22508
22509 /* For the benefit of old versions of GCC, check for anonymous
22510 namespaces based on the demangled name. */
22511 if (!cu->processing_has_namespace_info
22512 && cu->language == language_cplus)
22513 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22514 }
22515 return (sym);
22516 }
22517
22518 /* Given an attr with a DW_FORM_dataN value in host byte order,
22519 zero-extend it as appropriate for the symbol's type. The DWARF
22520 standard (v4) is not entirely clear about the meaning of using
22521 DW_FORM_dataN for a constant with a signed type, where the type is
22522 wider than the data. The conclusion of a discussion on the DWARF
22523 list was that this is unspecified. We choose to always zero-extend
22524 because that is the interpretation long in use by GCC. */
22525
22526 static gdb_byte *
22527 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22528 struct dwarf2_cu *cu, LONGEST *value, int bits)
22529 {
22530 struct objfile *objfile = cu->per_objfile->objfile;
22531 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22532 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22533 LONGEST l = attr->constant_value (0);
22534
22535 if (bits < sizeof (*value) * 8)
22536 {
22537 l &= ((LONGEST) 1 << bits) - 1;
22538 *value = l;
22539 }
22540 else if (bits == sizeof (*value) * 8)
22541 *value = l;
22542 else
22543 {
22544 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22545 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22546 return bytes;
22547 }
22548
22549 return NULL;
22550 }
22551
22552 /* Read a constant value from an attribute. Either set *VALUE, or if
22553 the value does not fit in *VALUE, set *BYTES - either already
22554 allocated on the objfile obstack, or newly allocated on OBSTACK,
22555 or, set *BATON, if we translated the constant to a location
22556 expression. */
22557
22558 static void
22559 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22560 const char *name, struct obstack *obstack,
22561 struct dwarf2_cu *cu,
22562 LONGEST *value, const gdb_byte **bytes,
22563 struct dwarf2_locexpr_baton **baton)
22564 {
22565 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22566 struct objfile *objfile = per_objfile->objfile;
22567 struct comp_unit_head *cu_header = &cu->header;
22568 struct dwarf_block *blk;
22569 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22570 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22571
22572 *value = 0;
22573 *bytes = NULL;
22574 *baton = NULL;
22575
22576 switch (attr->form)
22577 {
22578 case DW_FORM_addr:
22579 case DW_FORM_addrx:
22580 case DW_FORM_GNU_addr_index:
22581 {
22582 gdb_byte *data;
22583
22584 if (TYPE_LENGTH (type) != cu_header->addr_size)
22585 dwarf2_const_value_length_mismatch_complaint (name,
22586 cu_header->addr_size,
22587 TYPE_LENGTH (type));
22588 /* Symbols of this form are reasonably rare, so we just
22589 piggyback on the existing location code rather than writing
22590 a new implementation of symbol_computed_ops. */
22591 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22592 (*baton)->per_objfile = per_objfile;
22593 (*baton)->per_cu = cu->per_cu;
22594 gdb_assert ((*baton)->per_cu);
22595
22596 (*baton)->size = 2 + cu_header->addr_size;
22597 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22598 (*baton)->data = data;
22599
22600 data[0] = DW_OP_addr;
22601 store_unsigned_integer (&data[1], cu_header->addr_size,
22602 byte_order, attr->as_address ());
22603 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22604 }
22605 break;
22606 case DW_FORM_string:
22607 case DW_FORM_strp:
22608 case DW_FORM_strx:
22609 case DW_FORM_GNU_str_index:
22610 case DW_FORM_GNU_strp_alt:
22611 /* The string is already allocated on the objfile obstack, point
22612 directly to it. */
22613 *bytes = (const gdb_byte *) attr->as_string ();
22614 break;
22615 case DW_FORM_block1:
22616 case DW_FORM_block2:
22617 case DW_FORM_block4:
22618 case DW_FORM_block:
22619 case DW_FORM_exprloc:
22620 case DW_FORM_data16:
22621 blk = attr->as_block ();
22622 if (TYPE_LENGTH (type) != blk->size)
22623 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22624 TYPE_LENGTH (type));
22625 *bytes = blk->data;
22626 break;
22627
22628 /* The DW_AT_const_value attributes are supposed to carry the
22629 symbol's value "represented as it would be on the target
22630 architecture." By the time we get here, it's already been
22631 converted to host endianness, so we just need to sign- or
22632 zero-extend it as appropriate. */
22633 case DW_FORM_data1:
22634 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22635 break;
22636 case DW_FORM_data2:
22637 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22638 break;
22639 case DW_FORM_data4:
22640 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22641 break;
22642 case DW_FORM_data8:
22643 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22644 break;
22645
22646 case DW_FORM_sdata:
22647 case DW_FORM_implicit_const:
22648 *value = attr->as_signed ();
22649 break;
22650
22651 case DW_FORM_udata:
22652 *value = attr->as_unsigned ();
22653 break;
22654
22655 default:
22656 complaint (_("unsupported const value attribute form: '%s'"),
22657 dwarf_form_name (attr->form));
22658 *value = 0;
22659 break;
22660 }
22661 }
22662
22663
22664 /* Copy constant value from an attribute to a symbol. */
22665
22666 static void
22667 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22668 struct dwarf2_cu *cu)
22669 {
22670 struct objfile *objfile = cu->per_objfile->objfile;
22671 LONGEST value;
22672 const gdb_byte *bytes;
22673 struct dwarf2_locexpr_baton *baton;
22674
22675 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22676 sym->print_name (),
22677 &objfile->objfile_obstack, cu,
22678 &value, &bytes, &baton);
22679
22680 if (baton != NULL)
22681 {
22682 SYMBOL_LOCATION_BATON (sym) = baton;
22683 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22684 }
22685 else if (bytes != NULL)
22686 {
22687 SYMBOL_VALUE_BYTES (sym) = bytes;
22688 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22689 }
22690 else
22691 {
22692 SYMBOL_VALUE (sym) = value;
22693 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22694 }
22695 }
22696
22697 /* Return the type of the die in question using its DW_AT_type attribute. */
22698
22699 static struct type *
22700 die_type (struct die_info *die, struct dwarf2_cu *cu)
22701 {
22702 struct attribute *type_attr;
22703
22704 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22705 if (!type_attr)
22706 {
22707 struct objfile *objfile = cu->per_objfile->objfile;
22708 /* A missing DW_AT_type represents a void type. */
22709 return objfile_type (objfile)->builtin_void;
22710 }
22711
22712 return lookup_die_type (die, type_attr, cu);
22713 }
22714
22715 /* True iff CU's producer generates GNAT Ada auxiliary information
22716 that allows to find parallel types through that information instead
22717 of having to do expensive parallel lookups by type name. */
22718
22719 static int
22720 need_gnat_info (struct dwarf2_cu *cu)
22721 {
22722 /* Assume that the Ada compiler was GNAT, which always produces
22723 the auxiliary information. */
22724 return (cu->language == language_ada);
22725 }
22726
22727 /* Return the auxiliary type of the die in question using its
22728 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22729 attribute is not present. */
22730
22731 static struct type *
22732 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22733 {
22734 struct attribute *type_attr;
22735
22736 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22737 if (!type_attr)
22738 return NULL;
22739
22740 return lookup_die_type (die, type_attr, cu);
22741 }
22742
22743 /* If DIE has a descriptive_type attribute, then set the TYPE's
22744 descriptive type accordingly. */
22745
22746 static void
22747 set_descriptive_type (struct type *type, struct die_info *die,
22748 struct dwarf2_cu *cu)
22749 {
22750 struct type *descriptive_type = die_descriptive_type (die, cu);
22751
22752 if (descriptive_type)
22753 {
22754 ALLOCATE_GNAT_AUX_TYPE (type);
22755 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22756 }
22757 }
22758
22759 /* Return the containing type of the die in question using its
22760 DW_AT_containing_type attribute. */
22761
22762 static struct type *
22763 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22764 {
22765 struct attribute *type_attr;
22766 struct objfile *objfile = cu->per_objfile->objfile;
22767
22768 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22769 if (!type_attr)
22770 error (_("Dwarf Error: Problem turning containing type into gdb type "
22771 "[in module %s]"), objfile_name (objfile));
22772
22773 return lookup_die_type (die, type_attr, cu);
22774 }
22775
22776 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22777
22778 static struct type *
22779 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22780 {
22781 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22782 struct objfile *objfile = per_objfile->objfile;
22783 char *saved;
22784
22785 std::string message
22786 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22787 objfile_name (objfile),
22788 sect_offset_str (cu->header.sect_off),
22789 sect_offset_str (die->sect_off));
22790 saved = obstack_strdup (&objfile->objfile_obstack, message);
22791
22792 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22793 }
22794
22795 /* Look up the type of DIE in CU using its type attribute ATTR.
22796 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22797 DW_AT_containing_type.
22798 If there is no type substitute an error marker. */
22799
22800 static struct type *
22801 lookup_die_type (struct die_info *die, const struct attribute *attr,
22802 struct dwarf2_cu *cu)
22803 {
22804 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22805 struct objfile *objfile = per_objfile->objfile;
22806 struct type *this_type;
22807
22808 gdb_assert (attr->name == DW_AT_type
22809 || attr->name == DW_AT_GNAT_descriptive_type
22810 || attr->name == DW_AT_containing_type);
22811
22812 /* First see if we have it cached. */
22813
22814 if (attr->form == DW_FORM_GNU_ref_alt)
22815 {
22816 struct dwarf2_per_cu_data *per_cu;
22817 sect_offset sect_off = attr->get_ref_die_offset ();
22818
22819 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22820 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22821 }
22822 else if (attr->form_is_ref ())
22823 {
22824 sect_offset sect_off = attr->get_ref_die_offset ();
22825
22826 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22827 }
22828 else if (attr->form == DW_FORM_ref_sig8)
22829 {
22830 ULONGEST signature = attr->as_signature ();
22831
22832 return get_signatured_type (die, signature, cu);
22833 }
22834 else
22835 {
22836 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22837 " at %s [in module %s]"),
22838 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22839 objfile_name (objfile));
22840 return build_error_marker_type (cu, die);
22841 }
22842
22843 /* If not cached we need to read it in. */
22844
22845 if (this_type == NULL)
22846 {
22847 struct die_info *type_die = NULL;
22848 struct dwarf2_cu *type_cu = cu;
22849
22850 if (attr->form_is_ref ())
22851 type_die = follow_die_ref (die, attr, &type_cu);
22852 if (type_die == NULL)
22853 return build_error_marker_type (cu, die);
22854 /* If we find the type now, it's probably because the type came
22855 from an inter-CU reference and the type's CU got expanded before
22856 ours. */
22857 this_type = read_type_die (type_die, type_cu);
22858 }
22859
22860 /* If we still don't have a type use an error marker. */
22861
22862 if (this_type == NULL)
22863 return build_error_marker_type (cu, die);
22864
22865 return this_type;
22866 }
22867
22868 /* Return the type in DIE, CU.
22869 Returns NULL for invalid types.
22870
22871 This first does a lookup in die_type_hash,
22872 and only reads the die in if necessary.
22873
22874 NOTE: This can be called when reading in partial or full symbols. */
22875
22876 static struct type *
22877 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22878 {
22879 struct type *this_type;
22880
22881 this_type = get_die_type (die, cu);
22882 if (this_type)
22883 return this_type;
22884
22885 return read_type_die_1 (die, cu);
22886 }
22887
22888 /* Read the type in DIE, CU.
22889 Returns NULL for invalid types. */
22890
22891 static struct type *
22892 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22893 {
22894 struct type *this_type = NULL;
22895
22896 switch (die->tag)
22897 {
22898 case DW_TAG_class_type:
22899 case DW_TAG_interface_type:
22900 case DW_TAG_structure_type:
22901 case DW_TAG_union_type:
22902 this_type = read_structure_type (die, cu);
22903 break;
22904 case DW_TAG_enumeration_type:
22905 this_type = read_enumeration_type (die, cu);
22906 break;
22907 case DW_TAG_subprogram:
22908 case DW_TAG_subroutine_type:
22909 case DW_TAG_inlined_subroutine:
22910 this_type = read_subroutine_type (die, cu);
22911 break;
22912 case DW_TAG_array_type:
22913 this_type = read_array_type (die, cu);
22914 break;
22915 case DW_TAG_set_type:
22916 this_type = read_set_type (die, cu);
22917 break;
22918 case DW_TAG_pointer_type:
22919 this_type = read_tag_pointer_type (die, cu);
22920 break;
22921 case DW_TAG_ptr_to_member_type:
22922 this_type = read_tag_ptr_to_member_type (die, cu);
22923 break;
22924 case DW_TAG_reference_type:
22925 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22926 break;
22927 case DW_TAG_rvalue_reference_type:
22928 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22929 break;
22930 case DW_TAG_const_type:
22931 this_type = read_tag_const_type (die, cu);
22932 break;
22933 case DW_TAG_volatile_type:
22934 this_type = read_tag_volatile_type (die, cu);
22935 break;
22936 case DW_TAG_restrict_type:
22937 this_type = read_tag_restrict_type (die, cu);
22938 break;
22939 case DW_TAG_string_type:
22940 this_type = read_tag_string_type (die, cu);
22941 break;
22942 case DW_TAG_typedef:
22943 this_type = read_typedef (die, cu);
22944 break;
22945 case DW_TAG_subrange_type:
22946 this_type = read_subrange_type (die, cu);
22947 break;
22948 case DW_TAG_base_type:
22949 this_type = read_base_type (die, cu);
22950 break;
22951 case DW_TAG_unspecified_type:
22952 this_type = read_unspecified_type (die, cu);
22953 break;
22954 case DW_TAG_namespace:
22955 this_type = read_namespace_type (die, cu);
22956 break;
22957 case DW_TAG_module:
22958 this_type = read_module_type (die, cu);
22959 break;
22960 case DW_TAG_atomic_type:
22961 this_type = read_tag_atomic_type (die, cu);
22962 break;
22963 default:
22964 complaint (_("unexpected tag in read_type_die: '%s'"),
22965 dwarf_tag_name (die->tag));
22966 break;
22967 }
22968
22969 return this_type;
22970 }
22971
22972 /* See if we can figure out if the class lives in a namespace. We do
22973 this by looking for a member function; its demangled name will
22974 contain namespace info, if there is any.
22975 Return the computed name or NULL.
22976 Space for the result is allocated on the objfile's obstack.
22977 This is the full-die version of guess_partial_die_structure_name.
22978 In this case we know DIE has no useful parent. */
22979
22980 static const char *
22981 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22982 {
22983 struct die_info *spec_die;
22984 struct dwarf2_cu *spec_cu;
22985 struct die_info *child;
22986 struct objfile *objfile = cu->per_objfile->objfile;
22987
22988 spec_cu = cu;
22989 spec_die = die_specification (die, &spec_cu);
22990 if (spec_die != NULL)
22991 {
22992 die = spec_die;
22993 cu = spec_cu;
22994 }
22995
22996 for (child = die->child;
22997 child != NULL;
22998 child = child->sibling)
22999 {
23000 if (child->tag == DW_TAG_subprogram)
23001 {
23002 const char *linkage_name = dw2_linkage_name (child, cu);
23003
23004 if (linkage_name != NULL)
23005 {
23006 gdb::unique_xmalloc_ptr<char> actual_name
23007 (cu->language_defn->class_name_from_physname (linkage_name));
23008 const char *name = NULL;
23009
23010 if (actual_name != NULL)
23011 {
23012 const char *die_name = dwarf2_name (die, cu);
23013
23014 if (die_name != NULL
23015 && strcmp (die_name, actual_name.get ()) != 0)
23016 {
23017 /* Strip off the class name from the full name.
23018 We want the prefix. */
23019 int die_name_len = strlen (die_name);
23020 int actual_name_len = strlen (actual_name.get ());
23021 const char *ptr = actual_name.get ();
23022
23023 /* Test for '::' as a sanity check. */
23024 if (actual_name_len > die_name_len + 2
23025 && ptr[actual_name_len - die_name_len - 1] == ':')
23026 name = obstack_strndup (
23027 &objfile->per_bfd->storage_obstack,
23028 ptr, actual_name_len - die_name_len - 2);
23029 }
23030 }
23031 return name;
23032 }
23033 }
23034 }
23035
23036 return NULL;
23037 }
23038
23039 /* GCC might emit a nameless typedef that has a linkage name. Determine the
23040 prefix part in such case. See
23041 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23042
23043 static const char *
23044 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23045 {
23046 struct attribute *attr;
23047 const char *base;
23048
23049 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23050 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23051 return NULL;
23052
23053 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
23054 return NULL;
23055
23056 attr = dw2_linkage_name_attr (die, cu);
23057 const char *attr_name = attr->as_string ();
23058 if (attr == NULL || attr_name == NULL)
23059 return NULL;
23060
23061 /* dwarf2_name had to be already called. */
23062 gdb_assert (attr->canonical_string_p ());
23063
23064 /* Strip the base name, keep any leading namespaces/classes. */
23065 base = strrchr (attr_name, ':');
23066 if (base == NULL || base == attr_name || base[-1] != ':')
23067 return "";
23068
23069 struct objfile *objfile = cu->per_objfile->objfile;
23070 return obstack_strndup (&objfile->per_bfd->storage_obstack,
23071 attr_name,
23072 &base[-1] - attr_name);
23073 }
23074
23075 /* Return the name of the namespace/class that DIE is defined within,
23076 or "" if we can't tell. The caller should not xfree the result.
23077
23078 For example, if we're within the method foo() in the following
23079 code:
23080
23081 namespace N {
23082 class C {
23083 void foo () {
23084 }
23085 };
23086 }
23087
23088 then determine_prefix on foo's die will return "N::C". */
23089
23090 static const char *
23091 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
23092 {
23093 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23094 struct die_info *parent, *spec_die;
23095 struct dwarf2_cu *spec_cu;
23096 struct type *parent_type;
23097 const char *retval;
23098
23099 if (cu->language != language_cplus
23100 && cu->language != language_fortran && cu->language != language_d
23101 && cu->language != language_rust)
23102 return "";
23103
23104 retval = anonymous_struct_prefix (die, cu);
23105 if (retval)
23106 return retval;
23107
23108 /* We have to be careful in the presence of DW_AT_specification.
23109 For example, with GCC 3.4, given the code
23110
23111 namespace N {
23112 void foo() {
23113 // Definition of N::foo.
23114 }
23115 }
23116
23117 then we'll have a tree of DIEs like this:
23118
23119 1: DW_TAG_compile_unit
23120 2: DW_TAG_namespace // N
23121 3: DW_TAG_subprogram // declaration of N::foo
23122 4: DW_TAG_subprogram // definition of N::foo
23123 DW_AT_specification // refers to die #3
23124
23125 Thus, when processing die #4, we have to pretend that we're in
23126 the context of its DW_AT_specification, namely the contex of die
23127 #3. */
23128 spec_cu = cu;
23129 spec_die = die_specification (die, &spec_cu);
23130 if (spec_die == NULL)
23131 parent = die->parent;
23132 else
23133 {
23134 parent = spec_die->parent;
23135 cu = spec_cu;
23136 }
23137
23138 if (parent == NULL)
23139 return "";
23140 else if (parent->building_fullname)
23141 {
23142 const char *name;
23143 const char *parent_name;
23144
23145 /* It has been seen on RealView 2.2 built binaries,
23146 DW_TAG_template_type_param types actually _defined_ as
23147 children of the parent class:
23148
23149 enum E {};
23150 template class <class Enum> Class{};
23151 Class<enum E> class_e;
23152
23153 1: DW_TAG_class_type (Class)
23154 2: DW_TAG_enumeration_type (E)
23155 3: DW_TAG_enumerator (enum1:0)
23156 3: DW_TAG_enumerator (enum2:1)
23157 ...
23158 2: DW_TAG_template_type_param
23159 DW_AT_type DW_FORM_ref_udata (E)
23160
23161 Besides being broken debug info, it can put GDB into an
23162 infinite loop. Consider:
23163
23164 When we're building the full name for Class<E>, we'll start
23165 at Class, and go look over its template type parameters,
23166 finding E. We'll then try to build the full name of E, and
23167 reach here. We're now trying to build the full name of E,
23168 and look over the parent DIE for containing scope. In the
23169 broken case, if we followed the parent DIE of E, we'd again
23170 find Class, and once again go look at its template type
23171 arguments, etc., etc. Simply don't consider such parent die
23172 as source-level parent of this die (it can't be, the language
23173 doesn't allow it), and break the loop here. */
23174 name = dwarf2_name (die, cu);
23175 parent_name = dwarf2_name (parent, cu);
23176 complaint (_("template param type '%s' defined within parent '%s'"),
23177 name ? name : "<unknown>",
23178 parent_name ? parent_name : "<unknown>");
23179 return "";
23180 }
23181 else
23182 switch (parent->tag)
23183 {
23184 case DW_TAG_namespace:
23185 parent_type = read_type_die (parent, cu);
23186 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23187 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23188 Work around this problem here. */
23189 if (cu->language == language_cplus
23190 && strcmp (parent_type->name (), "::") == 0)
23191 return "";
23192 /* We give a name to even anonymous namespaces. */
23193 return parent_type->name ();
23194 case DW_TAG_class_type:
23195 case DW_TAG_interface_type:
23196 case DW_TAG_structure_type:
23197 case DW_TAG_union_type:
23198 case DW_TAG_module:
23199 parent_type = read_type_die (parent, cu);
23200 if (parent_type->name () != NULL)
23201 return parent_type->name ();
23202 else
23203 /* An anonymous structure is only allowed non-static data
23204 members; no typedefs, no member functions, et cetera.
23205 So it does not need a prefix. */
23206 return "";
23207 case DW_TAG_compile_unit:
23208 case DW_TAG_partial_unit:
23209 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23210 if (cu->language == language_cplus
23211 && !per_objfile->per_bfd->types.empty ()
23212 && die->child != NULL
23213 && (die->tag == DW_TAG_class_type
23214 || die->tag == DW_TAG_structure_type
23215 || die->tag == DW_TAG_union_type))
23216 {
23217 const char *name = guess_full_die_structure_name (die, cu);
23218 if (name != NULL)
23219 return name;
23220 }
23221 return "";
23222 case DW_TAG_subprogram:
23223 /* Nested subroutines in Fortran get a prefix with the name
23224 of the parent's subroutine. */
23225 if (cu->language == language_fortran)
23226 {
23227 if ((die->tag == DW_TAG_subprogram)
23228 && (dwarf2_name (parent, cu) != NULL))
23229 return dwarf2_name (parent, cu);
23230 }
23231 return determine_prefix (parent, cu);
23232 case DW_TAG_enumeration_type:
23233 parent_type = read_type_die (parent, cu);
23234 if (TYPE_DECLARED_CLASS (parent_type))
23235 {
23236 if (parent_type->name () != NULL)
23237 return parent_type->name ();
23238 return "";
23239 }
23240 /* Fall through. */
23241 default:
23242 return determine_prefix (parent, cu);
23243 }
23244 }
23245
23246 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23247 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23248 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23249 an obconcat, otherwise allocate storage for the result. The CU argument is
23250 used to determine the language and hence, the appropriate separator. */
23251
23252 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23253
23254 static char *
23255 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23256 int physname, struct dwarf2_cu *cu)
23257 {
23258 const char *lead = "";
23259 const char *sep;
23260
23261 if (suffix == NULL || suffix[0] == '\0'
23262 || prefix == NULL || prefix[0] == '\0')
23263 sep = "";
23264 else if (cu->language == language_d)
23265 {
23266 /* For D, the 'main' function could be defined in any module, but it
23267 should never be prefixed. */
23268 if (strcmp (suffix, "D main") == 0)
23269 {
23270 prefix = "";
23271 sep = "";
23272 }
23273 else
23274 sep = ".";
23275 }
23276 else if (cu->language == language_fortran && physname)
23277 {
23278 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23279 DW_AT_MIPS_linkage_name is preferred and used instead. */
23280
23281 lead = "__";
23282 sep = "_MOD_";
23283 }
23284 else
23285 sep = "::";
23286
23287 if (prefix == NULL)
23288 prefix = "";
23289 if (suffix == NULL)
23290 suffix = "";
23291
23292 if (obs == NULL)
23293 {
23294 char *retval
23295 = ((char *)
23296 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23297
23298 strcpy (retval, lead);
23299 strcat (retval, prefix);
23300 strcat (retval, sep);
23301 strcat (retval, suffix);
23302 return retval;
23303 }
23304 else
23305 {
23306 /* We have an obstack. */
23307 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23308 }
23309 }
23310
23311 /* Get name of a die, return NULL if not found. */
23312
23313 static const char *
23314 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23315 struct objfile *objfile)
23316 {
23317 if (name && cu->language == language_cplus)
23318 {
23319 gdb::unique_xmalloc_ptr<char> canon_name
23320 = cp_canonicalize_string (name);
23321
23322 if (canon_name != nullptr)
23323 name = objfile->intern (canon_name.get ());
23324 }
23325
23326 return name;
23327 }
23328
23329 /* Get name of a die, return NULL if not found.
23330 Anonymous namespaces are converted to their magic string. */
23331
23332 static const char *
23333 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23334 {
23335 struct attribute *attr;
23336 struct objfile *objfile = cu->per_objfile->objfile;
23337
23338 attr = dwarf2_attr (die, DW_AT_name, cu);
23339 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23340 if (attr_name == nullptr
23341 && die->tag != DW_TAG_namespace
23342 && die->tag != DW_TAG_class_type
23343 && die->tag != DW_TAG_interface_type
23344 && die->tag != DW_TAG_structure_type
23345 && die->tag != DW_TAG_union_type)
23346 return NULL;
23347
23348 switch (die->tag)
23349 {
23350 case DW_TAG_compile_unit:
23351 case DW_TAG_partial_unit:
23352 /* Compilation units have a DW_AT_name that is a filename, not
23353 a source language identifier. */
23354 case DW_TAG_enumeration_type:
23355 case DW_TAG_enumerator:
23356 /* These tags always have simple identifiers already; no need
23357 to canonicalize them. */
23358 return attr_name;
23359
23360 case DW_TAG_namespace:
23361 if (attr_name != nullptr)
23362 return attr_name;
23363 return CP_ANONYMOUS_NAMESPACE_STR;
23364
23365 case DW_TAG_class_type:
23366 case DW_TAG_interface_type:
23367 case DW_TAG_structure_type:
23368 case DW_TAG_union_type:
23369 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23370 structures or unions. These were of the form "._%d" in GCC 4.1,
23371 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23372 and GCC 4.4. We work around this problem by ignoring these. */
23373 if (attr_name != nullptr
23374 && (startswith (attr_name, "._")
23375 || startswith (attr_name, "<anonymous")))
23376 return NULL;
23377
23378 /* GCC might emit a nameless typedef that has a linkage name. See
23379 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23380 if (!attr || attr_name == NULL)
23381 {
23382 attr = dw2_linkage_name_attr (die, cu);
23383 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23384 if (attr == NULL || attr_name == NULL)
23385 return NULL;
23386
23387 /* Avoid demangling attr_name the second time on a second
23388 call for the same DIE. */
23389 if (!attr->canonical_string_p ())
23390 {
23391 gdb::unique_xmalloc_ptr<char> demangled
23392 (gdb_demangle (attr_name, DMGL_TYPES));
23393 if (demangled == nullptr)
23394 return nullptr;
23395
23396 attr->set_string_canonical (objfile->intern (demangled.get ()));
23397 attr_name = attr->as_string ();
23398 }
23399
23400 /* Strip any leading namespaces/classes, keep only the
23401 base name. DW_AT_name for named DIEs does not
23402 contain the prefixes. */
23403 const char *base = strrchr (attr_name, ':');
23404 if (base && base > attr_name && base[-1] == ':')
23405 return &base[1];
23406 else
23407 return attr_name;
23408 }
23409 break;
23410
23411 default:
23412 break;
23413 }
23414
23415 if (!attr->canonical_string_p ())
23416 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23417 objfile));
23418 return attr->as_string ();
23419 }
23420
23421 /* Return the die that this die in an extension of, or NULL if there
23422 is none. *EXT_CU is the CU containing DIE on input, and the CU
23423 containing the return value on output. */
23424
23425 static struct die_info *
23426 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23427 {
23428 struct attribute *attr;
23429
23430 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23431 if (attr == NULL)
23432 return NULL;
23433
23434 return follow_die_ref (die, attr, ext_cu);
23435 }
23436
23437 static void
23438 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23439 {
23440 unsigned int i;
23441
23442 print_spaces (indent, f);
23443 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23444 dwarf_tag_name (die->tag), die->abbrev,
23445 sect_offset_str (die->sect_off));
23446
23447 if (die->parent != NULL)
23448 {
23449 print_spaces (indent, f);
23450 fprintf_unfiltered (f, " parent at offset: %s\n",
23451 sect_offset_str (die->parent->sect_off));
23452 }
23453
23454 print_spaces (indent, f);
23455 fprintf_unfiltered (f, " has children: %s\n",
23456 dwarf_bool_name (die->child != NULL));
23457
23458 print_spaces (indent, f);
23459 fprintf_unfiltered (f, " attributes:\n");
23460
23461 for (i = 0; i < die->num_attrs; ++i)
23462 {
23463 print_spaces (indent, f);
23464 fprintf_unfiltered (f, " %s (%s) ",
23465 dwarf_attr_name (die->attrs[i].name),
23466 dwarf_form_name (die->attrs[i].form));
23467
23468 switch (die->attrs[i].form)
23469 {
23470 case DW_FORM_addr:
23471 case DW_FORM_addrx:
23472 case DW_FORM_GNU_addr_index:
23473 fprintf_unfiltered (f, "address: ");
23474 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23475 break;
23476 case DW_FORM_block2:
23477 case DW_FORM_block4:
23478 case DW_FORM_block:
23479 case DW_FORM_block1:
23480 fprintf_unfiltered (f, "block: size %s",
23481 pulongest (die->attrs[i].as_block ()->size));
23482 break;
23483 case DW_FORM_exprloc:
23484 fprintf_unfiltered (f, "expression: size %s",
23485 pulongest (die->attrs[i].as_block ()->size));
23486 break;
23487 case DW_FORM_data16:
23488 fprintf_unfiltered (f, "constant of 16 bytes");
23489 break;
23490 case DW_FORM_ref_addr:
23491 fprintf_unfiltered (f, "ref address: ");
23492 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23493 break;
23494 case DW_FORM_GNU_ref_alt:
23495 fprintf_unfiltered (f, "alt ref address: ");
23496 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23497 break;
23498 case DW_FORM_ref1:
23499 case DW_FORM_ref2:
23500 case DW_FORM_ref4:
23501 case DW_FORM_ref8:
23502 case DW_FORM_ref_udata:
23503 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23504 (long) (die->attrs[i].as_unsigned ()));
23505 break;
23506 case DW_FORM_data1:
23507 case DW_FORM_data2:
23508 case DW_FORM_data4:
23509 case DW_FORM_data8:
23510 case DW_FORM_udata:
23511 fprintf_unfiltered (f, "constant: %s",
23512 pulongest (die->attrs[i].as_unsigned ()));
23513 break;
23514 case DW_FORM_sec_offset:
23515 fprintf_unfiltered (f, "section offset: %s",
23516 pulongest (die->attrs[i].as_unsigned ()));
23517 break;
23518 case DW_FORM_ref_sig8:
23519 fprintf_unfiltered (f, "signature: %s",
23520 hex_string (die->attrs[i].as_signature ()));
23521 break;
23522 case DW_FORM_string:
23523 case DW_FORM_strp:
23524 case DW_FORM_line_strp:
23525 case DW_FORM_strx:
23526 case DW_FORM_GNU_str_index:
23527 case DW_FORM_GNU_strp_alt:
23528 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23529 die->attrs[i].as_string ()
23530 ? die->attrs[i].as_string () : "",
23531 die->attrs[i].canonical_string_p () ? "is" : "not");
23532 break;
23533 case DW_FORM_flag:
23534 if (die->attrs[i].as_boolean ())
23535 fprintf_unfiltered (f, "flag: TRUE");
23536 else
23537 fprintf_unfiltered (f, "flag: FALSE");
23538 break;
23539 case DW_FORM_flag_present:
23540 fprintf_unfiltered (f, "flag: TRUE");
23541 break;
23542 case DW_FORM_indirect:
23543 /* The reader will have reduced the indirect form to
23544 the "base form" so this form should not occur. */
23545 fprintf_unfiltered (f,
23546 "unexpected attribute form: DW_FORM_indirect");
23547 break;
23548 case DW_FORM_sdata:
23549 case DW_FORM_implicit_const:
23550 fprintf_unfiltered (f, "constant: %s",
23551 plongest (die->attrs[i].as_signed ()));
23552 break;
23553 default:
23554 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23555 die->attrs[i].form);
23556 break;
23557 }
23558 fprintf_unfiltered (f, "\n");
23559 }
23560 }
23561
23562 static void
23563 dump_die_for_error (struct die_info *die)
23564 {
23565 dump_die_shallow (gdb_stderr, 0, die);
23566 }
23567
23568 static void
23569 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23570 {
23571 int indent = level * 4;
23572
23573 gdb_assert (die != NULL);
23574
23575 if (level >= max_level)
23576 return;
23577
23578 dump_die_shallow (f, indent, die);
23579
23580 if (die->child != NULL)
23581 {
23582 print_spaces (indent, f);
23583 fprintf_unfiltered (f, " Children:");
23584 if (level + 1 < max_level)
23585 {
23586 fprintf_unfiltered (f, "\n");
23587 dump_die_1 (f, level + 1, max_level, die->child);
23588 }
23589 else
23590 {
23591 fprintf_unfiltered (f,
23592 " [not printed, max nesting level reached]\n");
23593 }
23594 }
23595
23596 if (die->sibling != NULL && level > 0)
23597 {
23598 dump_die_1 (f, level, max_level, die->sibling);
23599 }
23600 }
23601
23602 /* This is called from the pdie macro in gdbinit.in.
23603 It's not static so gcc will keep a copy callable from gdb. */
23604
23605 void
23606 dump_die (struct die_info *die, int max_level)
23607 {
23608 dump_die_1 (gdb_stdlog, 0, max_level, die);
23609 }
23610
23611 static void
23612 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23613 {
23614 void **slot;
23615
23616 slot = htab_find_slot_with_hash (cu->die_hash, die,
23617 to_underlying (die->sect_off),
23618 INSERT);
23619
23620 *slot = die;
23621 }
23622
23623 /* Follow reference or signature attribute ATTR of SRC_DIE.
23624 On entry *REF_CU is the CU of SRC_DIE.
23625 On exit *REF_CU is the CU of the result. */
23626
23627 static struct die_info *
23628 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23629 struct dwarf2_cu **ref_cu)
23630 {
23631 struct die_info *die;
23632
23633 if (attr->form_is_ref ())
23634 die = follow_die_ref (src_die, attr, ref_cu);
23635 else if (attr->form == DW_FORM_ref_sig8)
23636 die = follow_die_sig (src_die, attr, ref_cu);
23637 else
23638 {
23639 dump_die_for_error (src_die);
23640 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23641 objfile_name ((*ref_cu)->per_objfile->objfile));
23642 }
23643
23644 return die;
23645 }
23646
23647 /* Follow reference OFFSET.
23648 On entry *REF_CU is the CU of the source die referencing OFFSET.
23649 On exit *REF_CU is the CU of the result.
23650 Returns NULL if OFFSET is invalid. */
23651
23652 static struct die_info *
23653 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23654 struct dwarf2_cu **ref_cu)
23655 {
23656 struct die_info temp_die;
23657 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23658 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23659
23660 gdb_assert (cu->per_cu != NULL);
23661
23662 target_cu = cu;
23663
23664 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23665 "source CU contains target offset: %d",
23666 sect_offset_str (cu->per_cu->sect_off),
23667 sect_offset_str (sect_off),
23668 cu->header.offset_in_cu_p (sect_off));
23669
23670 if (cu->per_cu->is_debug_types)
23671 {
23672 /* .debug_types CUs cannot reference anything outside their CU.
23673 If they need to, they have to reference a signatured type via
23674 DW_FORM_ref_sig8. */
23675 if (!cu->header.offset_in_cu_p (sect_off))
23676 return NULL;
23677 }
23678 else if (offset_in_dwz != cu->per_cu->is_dwz
23679 || !cu->header.offset_in_cu_p (sect_off))
23680 {
23681 struct dwarf2_per_cu_data *per_cu;
23682
23683 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23684 per_objfile);
23685
23686 dwarf_read_debug_printf_v ("target CU offset: %s, "
23687 "target CU DIEs loaded: %d",
23688 sect_offset_str (per_cu->sect_off),
23689 per_objfile->get_cu (per_cu) != nullptr);
23690
23691 /* If necessary, add it to the queue and load its DIEs. */
23692 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
23693 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23694 false, cu->language);
23695
23696 target_cu = per_objfile->get_cu (per_cu);
23697 }
23698 else if (cu->dies == NULL)
23699 {
23700 /* We're loading full DIEs during partial symbol reading. */
23701 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23702 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23703 language_minimal);
23704 }
23705
23706 *ref_cu = target_cu;
23707 temp_die.sect_off = sect_off;
23708
23709 if (target_cu != cu)
23710 target_cu->ancestor = cu;
23711
23712 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23713 &temp_die,
23714 to_underlying (sect_off));
23715 }
23716
23717 /* Follow reference attribute ATTR of SRC_DIE.
23718 On entry *REF_CU is the CU of SRC_DIE.
23719 On exit *REF_CU is the CU of the result. */
23720
23721 static struct die_info *
23722 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23723 struct dwarf2_cu **ref_cu)
23724 {
23725 sect_offset sect_off = attr->get_ref_die_offset ();
23726 struct dwarf2_cu *cu = *ref_cu;
23727 struct die_info *die;
23728
23729 die = follow_die_offset (sect_off,
23730 (attr->form == DW_FORM_GNU_ref_alt
23731 || cu->per_cu->is_dwz),
23732 ref_cu);
23733 if (!die)
23734 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23735 "at %s [in module %s]"),
23736 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23737 objfile_name (cu->per_objfile->objfile));
23738
23739 return die;
23740 }
23741
23742 /* See read.h. */
23743
23744 struct dwarf2_locexpr_baton
23745 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23746 dwarf2_per_cu_data *per_cu,
23747 dwarf2_per_objfile *per_objfile,
23748 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23749 bool resolve_abstract_p)
23750 {
23751 struct die_info *die;
23752 struct attribute *attr;
23753 struct dwarf2_locexpr_baton retval;
23754 struct objfile *objfile = per_objfile->objfile;
23755
23756 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23757 if (cu == nullptr)
23758 cu = load_cu (per_cu, per_objfile, false);
23759
23760 if (cu == nullptr)
23761 {
23762 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23763 Instead just throw an error, not much else we can do. */
23764 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23765 sect_offset_str (sect_off), objfile_name (objfile));
23766 }
23767
23768 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23769 if (!die)
23770 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23771 sect_offset_str (sect_off), objfile_name (objfile));
23772
23773 attr = dwarf2_attr (die, DW_AT_location, cu);
23774 if (!attr && resolve_abstract_p
23775 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23776 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23777 {
23778 CORE_ADDR pc = get_frame_pc ();
23779 CORE_ADDR baseaddr = objfile->text_section_offset ();
23780 struct gdbarch *gdbarch = objfile->arch ();
23781
23782 for (const auto &cand_off
23783 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23784 {
23785 struct dwarf2_cu *cand_cu = cu;
23786 struct die_info *cand
23787 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23788 if (!cand
23789 || !cand->parent
23790 || cand->parent->tag != DW_TAG_subprogram)
23791 continue;
23792
23793 CORE_ADDR pc_low, pc_high;
23794 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23795 if (pc_low == ((CORE_ADDR) -1))
23796 continue;
23797 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23798 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23799 if (!(pc_low <= pc && pc < pc_high))
23800 continue;
23801
23802 die = cand;
23803 attr = dwarf2_attr (die, DW_AT_location, cu);
23804 break;
23805 }
23806 }
23807
23808 if (!attr)
23809 {
23810 /* DWARF: "If there is no such attribute, then there is no effect.".
23811 DATA is ignored if SIZE is 0. */
23812
23813 retval.data = NULL;
23814 retval.size = 0;
23815 }
23816 else if (attr->form_is_section_offset ())
23817 {
23818 struct dwarf2_loclist_baton loclist_baton;
23819 CORE_ADDR pc = get_frame_pc ();
23820 size_t size;
23821
23822 fill_in_loclist_baton (cu, &loclist_baton, attr);
23823
23824 retval.data = dwarf2_find_location_expression (&loclist_baton,
23825 &size, pc);
23826 retval.size = size;
23827 }
23828 else
23829 {
23830 if (!attr->form_is_block ())
23831 error (_("Dwarf Error: DIE at %s referenced in module %s "
23832 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23833 sect_offset_str (sect_off), objfile_name (objfile));
23834
23835 struct dwarf_block *block = attr->as_block ();
23836 retval.data = block->data;
23837 retval.size = block->size;
23838 }
23839 retval.per_objfile = per_objfile;
23840 retval.per_cu = cu->per_cu;
23841
23842 per_objfile->age_comp_units ();
23843
23844 return retval;
23845 }
23846
23847 /* See read.h. */
23848
23849 struct dwarf2_locexpr_baton
23850 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23851 dwarf2_per_cu_data *per_cu,
23852 dwarf2_per_objfile *per_objfile,
23853 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23854 {
23855 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23856
23857 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23858 get_frame_pc);
23859 }
23860
23861 /* Write a constant of a given type as target-ordered bytes into
23862 OBSTACK. */
23863
23864 static const gdb_byte *
23865 write_constant_as_bytes (struct obstack *obstack,
23866 enum bfd_endian byte_order,
23867 struct type *type,
23868 ULONGEST value,
23869 LONGEST *len)
23870 {
23871 gdb_byte *result;
23872
23873 *len = TYPE_LENGTH (type);
23874 result = (gdb_byte *) obstack_alloc (obstack, *len);
23875 store_unsigned_integer (result, *len, byte_order, value);
23876
23877 return result;
23878 }
23879
23880 /* See read.h. */
23881
23882 const gdb_byte *
23883 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23884 dwarf2_per_cu_data *per_cu,
23885 dwarf2_per_objfile *per_objfile,
23886 obstack *obstack,
23887 LONGEST *len)
23888 {
23889 struct die_info *die;
23890 struct attribute *attr;
23891 const gdb_byte *result = NULL;
23892 struct type *type;
23893 LONGEST value;
23894 enum bfd_endian byte_order;
23895 struct objfile *objfile = per_objfile->objfile;
23896
23897 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23898 if (cu == nullptr)
23899 cu = load_cu (per_cu, per_objfile, false);
23900
23901 if (cu == nullptr)
23902 {
23903 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23904 Instead just throw an error, not much else we can do. */
23905 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23906 sect_offset_str (sect_off), objfile_name (objfile));
23907 }
23908
23909 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23910 if (!die)
23911 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23912 sect_offset_str (sect_off), objfile_name (objfile));
23913
23914 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23915 if (attr == NULL)
23916 return NULL;
23917
23918 byte_order = (bfd_big_endian (objfile->obfd)
23919 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23920
23921 switch (attr->form)
23922 {
23923 case DW_FORM_addr:
23924 case DW_FORM_addrx:
23925 case DW_FORM_GNU_addr_index:
23926 {
23927 gdb_byte *tem;
23928
23929 *len = cu->header.addr_size;
23930 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23931 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23932 result = tem;
23933 }
23934 break;
23935 case DW_FORM_string:
23936 case DW_FORM_strp:
23937 case DW_FORM_strx:
23938 case DW_FORM_GNU_str_index:
23939 case DW_FORM_GNU_strp_alt:
23940 /* The string is already allocated on the objfile obstack, point
23941 directly to it. */
23942 {
23943 const char *attr_name = attr->as_string ();
23944 result = (const gdb_byte *) attr_name;
23945 *len = strlen (attr_name);
23946 }
23947 break;
23948 case DW_FORM_block1:
23949 case DW_FORM_block2:
23950 case DW_FORM_block4:
23951 case DW_FORM_block:
23952 case DW_FORM_exprloc:
23953 case DW_FORM_data16:
23954 {
23955 struct dwarf_block *block = attr->as_block ();
23956 result = block->data;
23957 *len = block->size;
23958 }
23959 break;
23960
23961 /* The DW_AT_const_value attributes are supposed to carry the
23962 symbol's value "represented as it would be on the target
23963 architecture." By the time we get here, it's already been
23964 converted to host endianness, so we just need to sign- or
23965 zero-extend it as appropriate. */
23966 case DW_FORM_data1:
23967 type = die_type (die, cu);
23968 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23969 if (result == NULL)
23970 result = write_constant_as_bytes (obstack, byte_order,
23971 type, value, len);
23972 break;
23973 case DW_FORM_data2:
23974 type = die_type (die, cu);
23975 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23976 if (result == NULL)
23977 result = write_constant_as_bytes (obstack, byte_order,
23978 type, value, len);
23979 break;
23980 case DW_FORM_data4:
23981 type = die_type (die, cu);
23982 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23983 if (result == NULL)
23984 result = write_constant_as_bytes (obstack, byte_order,
23985 type, value, len);
23986 break;
23987 case DW_FORM_data8:
23988 type = die_type (die, cu);
23989 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23990 if (result == NULL)
23991 result = write_constant_as_bytes (obstack, byte_order,
23992 type, value, len);
23993 break;
23994
23995 case DW_FORM_sdata:
23996 case DW_FORM_implicit_const:
23997 type = die_type (die, cu);
23998 result = write_constant_as_bytes (obstack, byte_order,
23999 type, attr->as_signed (), len);
24000 break;
24001
24002 case DW_FORM_udata:
24003 type = die_type (die, cu);
24004 result = write_constant_as_bytes (obstack, byte_order,
24005 type, attr->as_unsigned (), len);
24006 break;
24007
24008 default:
24009 complaint (_("unsupported const value attribute form: '%s'"),
24010 dwarf_form_name (attr->form));
24011 break;
24012 }
24013
24014 return result;
24015 }
24016
24017 /* See read.h. */
24018
24019 struct type *
24020 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
24021 dwarf2_per_cu_data *per_cu,
24022 dwarf2_per_objfile *per_objfile)
24023 {
24024 struct die_info *die;
24025
24026 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
24027 if (cu == nullptr)
24028 cu = load_cu (per_cu, per_objfile, false);
24029
24030 if (cu == nullptr)
24031 return nullptr;
24032
24033 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
24034 if (!die)
24035 return NULL;
24036
24037 return die_type (die, cu);
24038 }
24039
24040 /* See read.h. */
24041
24042 struct type *
24043 dwarf2_get_die_type (cu_offset die_offset,
24044 dwarf2_per_cu_data *per_cu,
24045 dwarf2_per_objfile *per_objfile)
24046 {
24047 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
24048 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
24049 }
24050
24051 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
24052 On entry *REF_CU is the CU of SRC_DIE.
24053 On exit *REF_CU is the CU of the result.
24054 Returns NULL if the referenced DIE isn't found. */
24055
24056 static struct die_info *
24057 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24058 struct dwarf2_cu **ref_cu)
24059 {
24060 struct die_info temp_die;
24061 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
24062 struct die_info *die;
24063 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
24064
24065
24066 /* While it might be nice to assert sig_type->type == NULL here,
24067 we can get here for DW_AT_imported_declaration where we need
24068 the DIE not the type. */
24069
24070 /* If necessary, add it to the queue and load its DIEs. */
24071
24072 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
24073 language_minimal))
24074 read_signatured_type (sig_type, per_objfile);
24075
24076 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
24077 gdb_assert (sig_cu != NULL);
24078 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24079 temp_die.sect_off = sig_type->type_offset_in_section;
24080 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
24081 to_underlying (temp_die.sect_off));
24082 if (die)
24083 {
24084 /* For .gdb_index version 7 keep track of included TUs.
24085 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
24086 if (per_objfile->per_bfd->index_table != NULL
24087 && per_objfile->per_bfd->index_table->version <= 7)
24088 {
24089 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
24090 }
24091
24092 *ref_cu = sig_cu;
24093 if (sig_cu != cu)
24094 sig_cu->ancestor = cu;
24095
24096 return die;
24097 }
24098
24099 return NULL;
24100 }
24101
24102 /* Follow signatured type referenced by ATTR in SRC_DIE.
24103 On entry *REF_CU is the CU of SRC_DIE.
24104 On exit *REF_CU is the CU of the result.
24105 The result is the DIE of the type.
24106 If the referenced type cannot be found an error is thrown. */
24107
24108 static struct die_info *
24109 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
24110 struct dwarf2_cu **ref_cu)
24111 {
24112 ULONGEST signature = attr->as_signature ();
24113 struct signatured_type *sig_type;
24114 struct die_info *die;
24115
24116 gdb_assert (attr->form == DW_FORM_ref_sig8);
24117
24118 sig_type = lookup_signatured_type (*ref_cu, signature);
24119 /* sig_type will be NULL if the signatured type is missing from
24120 the debug info. */
24121 if (sig_type == NULL)
24122 {
24123 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24124 " from DIE at %s [in module %s]"),
24125 hex_string (signature), sect_offset_str (src_die->sect_off),
24126 objfile_name ((*ref_cu)->per_objfile->objfile));
24127 }
24128
24129 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24130 if (die == NULL)
24131 {
24132 dump_die_for_error (src_die);
24133 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24134 " from DIE at %s [in module %s]"),
24135 hex_string (signature), sect_offset_str (src_die->sect_off),
24136 objfile_name ((*ref_cu)->per_objfile->objfile));
24137 }
24138
24139 return die;
24140 }
24141
24142 /* Get the type specified by SIGNATURE referenced in DIE/CU,
24143 reading in and processing the type unit if necessary. */
24144
24145 static struct type *
24146 get_signatured_type (struct die_info *die, ULONGEST signature,
24147 struct dwarf2_cu *cu)
24148 {
24149 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24150 struct signatured_type *sig_type;
24151 struct dwarf2_cu *type_cu;
24152 struct die_info *type_die;
24153 struct type *type;
24154
24155 sig_type = lookup_signatured_type (cu, signature);
24156 /* sig_type will be NULL if the signatured type is missing from
24157 the debug info. */
24158 if (sig_type == NULL)
24159 {
24160 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
24161 " from DIE at %s [in module %s]"),
24162 hex_string (signature), sect_offset_str (die->sect_off),
24163 objfile_name (per_objfile->objfile));
24164 return build_error_marker_type (cu, die);
24165 }
24166
24167 /* If we already know the type we're done. */
24168 type = per_objfile->get_type_for_signatured_type (sig_type);
24169 if (type != nullptr)
24170 return type;
24171
24172 type_cu = cu;
24173 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24174 if (type_die != NULL)
24175 {
24176 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24177 is created. This is important, for example, because for c++ classes
24178 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24179 type = read_type_die (type_die, type_cu);
24180 if (type == NULL)
24181 {
24182 complaint (_("Dwarf Error: Cannot build signatured type %s"
24183 " referenced from DIE at %s [in module %s]"),
24184 hex_string (signature), sect_offset_str (die->sect_off),
24185 objfile_name (per_objfile->objfile));
24186 type = build_error_marker_type (cu, die);
24187 }
24188 }
24189 else
24190 {
24191 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24192 " from DIE at %s [in module %s]"),
24193 hex_string (signature), sect_offset_str (die->sect_off),
24194 objfile_name (per_objfile->objfile));
24195 type = build_error_marker_type (cu, die);
24196 }
24197
24198 per_objfile->set_type_for_signatured_type (sig_type, type);
24199
24200 return type;
24201 }
24202
24203 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24204 reading in and processing the type unit if necessary. */
24205
24206 static struct type *
24207 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24208 struct dwarf2_cu *cu) /* ARI: editCase function */
24209 {
24210 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24211 if (attr->form_is_ref ())
24212 {
24213 struct dwarf2_cu *type_cu = cu;
24214 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24215
24216 return read_type_die (type_die, type_cu);
24217 }
24218 else if (attr->form == DW_FORM_ref_sig8)
24219 {
24220 return get_signatured_type (die, attr->as_signature (), cu);
24221 }
24222 else
24223 {
24224 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24225
24226 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24227 " at %s [in module %s]"),
24228 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24229 objfile_name (per_objfile->objfile));
24230 return build_error_marker_type (cu, die);
24231 }
24232 }
24233
24234 /* Load the DIEs associated with type unit PER_CU into memory. */
24235
24236 static void
24237 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24238 dwarf2_per_objfile *per_objfile)
24239 {
24240 struct signatured_type *sig_type;
24241
24242 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24243 gdb_assert (! per_cu->type_unit_group_p ());
24244
24245 /* We have the per_cu, but we need the signatured_type.
24246 Fortunately this is an easy translation. */
24247 gdb_assert (per_cu->is_debug_types);
24248 sig_type = (struct signatured_type *) per_cu;
24249
24250 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24251
24252 read_signatured_type (sig_type, per_objfile);
24253
24254 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24255 }
24256
24257 /* Read in a signatured type and build its CU and DIEs.
24258 If the type is a stub for the real type in a DWO file,
24259 read in the real type from the DWO file as well. */
24260
24261 static void
24262 read_signatured_type (signatured_type *sig_type,
24263 dwarf2_per_objfile *per_objfile)
24264 {
24265 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24266
24267 gdb_assert (per_cu->is_debug_types);
24268 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24269
24270 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24271
24272 if (!reader.dummy_p)
24273 {
24274 struct dwarf2_cu *cu = reader.cu;
24275 const gdb_byte *info_ptr = reader.info_ptr;
24276
24277 gdb_assert (cu->die_hash == NULL);
24278 cu->die_hash =
24279 htab_create_alloc_ex (cu->header.length / 12,
24280 die_hash,
24281 die_eq,
24282 NULL,
24283 &cu->comp_unit_obstack,
24284 hashtab_obstack_allocate,
24285 dummy_obstack_deallocate);
24286
24287 if (reader.comp_unit_die->has_children)
24288 reader.comp_unit_die->child
24289 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24290 reader.comp_unit_die);
24291 cu->dies = reader.comp_unit_die;
24292 /* comp_unit_die is not stored in die_hash, no need. */
24293
24294 /* We try not to read any attributes in this function, because
24295 not all CUs needed for references have been loaded yet, and
24296 symbol table processing isn't initialized. But we have to
24297 set the CU language, or we won't be able to build types
24298 correctly. Similarly, if we do not read the producer, we can
24299 not apply producer-specific interpretation. */
24300 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24301
24302 reader.keep ();
24303 }
24304
24305 sig_type->per_cu.tu_read = 1;
24306 }
24307
24308 /* Decode simple location descriptions.
24309 Given a pointer to a dwarf block that defines a location, compute
24310 the location and return the value. If COMPUTED is non-null, it is
24311 set to true to indicate that decoding was successful, and false
24312 otherwise. If COMPUTED is null, then this function may emit a
24313 complaint. */
24314
24315 static CORE_ADDR
24316 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24317 {
24318 struct objfile *objfile = cu->per_objfile->objfile;
24319 size_t i;
24320 size_t size = blk->size;
24321 const gdb_byte *data = blk->data;
24322 CORE_ADDR stack[64];
24323 int stacki;
24324 unsigned int bytes_read, unsnd;
24325 gdb_byte op;
24326
24327 if (computed != nullptr)
24328 *computed = false;
24329
24330 i = 0;
24331 stacki = 0;
24332 stack[stacki] = 0;
24333 stack[++stacki] = 0;
24334
24335 while (i < size)
24336 {
24337 op = data[i++];
24338 switch (op)
24339 {
24340 case DW_OP_lit0:
24341 case DW_OP_lit1:
24342 case DW_OP_lit2:
24343 case DW_OP_lit3:
24344 case DW_OP_lit4:
24345 case DW_OP_lit5:
24346 case DW_OP_lit6:
24347 case DW_OP_lit7:
24348 case DW_OP_lit8:
24349 case DW_OP_lit9:
24350 case DW_OP_lit10:
24351 case DW_OP_lit11:
24352 case DW_OP_lit12:
24353 case DW_OP_lit13:
24354 case DW_OP_lit14:
24355 case DW_OP_lit15:
24356 case DW_OP_lit16:
24357 case DW_OP_lit17:
24358 case DW_OP_lit18:
24359 case DW_OP_lit19:
24360 case DW_OP_lit20:
24361 case DW_OP_lit21:
24362 case DW_OP_lit22:
24363 case DW_OP_lit23:
24364 case DW_OP_lit24:
24365 case DW_OP_lit25:
24366 case DW_OP_lit26:
24367 case DW_OP_lit27:
24368 case DW_OP_lit28:
24369 case DW_OP_lit29:
24370 case DW_OP_lit30:
24371 case DW_OP_lit31:
24372 stack[++stacki] = op - DW_OP_lit0;
24373 break;
24374
24375 case DW_OP_reg0:
24376 case DW_OP_reg1:
24377 case DW_OP_reg2:
24378 case DW_OP_reg3:
24379 case DW_OP_reg4:
24380 case DW_OP_reg5:
24381 case DW_OP_reg6:
24382 case DW_OP_reg7:
24383 case DW_OP_reg8:
24384 case DW_OP_reg9:
24385 case DW_OP_reg10:
24386 case DW_OP_reg11:
24387 case DW_OP_reg12:
24388 case DW_OP_reg13:
24389 case DW_OP_reg14:
24390 case DW_OP_reg15:
24391 case DW_OP_reg16:
24392 case DW_OP_reg17:
24393 case DW_OP_reg18:
24394 case DW_OP_reg19:
24395 case DW_OP_reg20:
24396 case DW_OP_reg21:
24397 case DW_OP_reg22:
24398 case DW_OP_reg23:
24399 case DW_OP_reg24:
24400 case DW_OP_reg25:
24401 case DW_OP_reg26:
24402 case DW_OP_reg27:
24403 case DW_OP_reg28:
24404 case DW_OP_reg29:
24405 case DW_OP_reg30:
24406 case DW_OP_reg31:
24407 stack[++stacki] = op - DW_OP_reg0;
24408 if (i < size)
24409 {
24410 if (computed == nullptr)
24411 dwarf2_complex_location_expr_complaint ();
24412 else
24413 return 0;
24414 }
24415 break;
24416
24417 case DW_OP_regx:
24418 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24419 i += bytes_read;
24420 stack[++stacki] = unsnd;
24421 if (i < size)
24422 {
24423 if (computed == nullptr)
24424 dwarf2_complex_location_expr_complaint ();
24425 else
24426 return 0;
24427 }
24428 break;
24429
24430 case DW_OP_addr:
24431 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24432 &bytes_read);
24433 i += bytes_read;
24434 break;
24435
24436 case DW_OP_const1u:
24437 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24438 i += 1;
24439 break;
24440
24441 case DW_OP_const1s:
24442 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24443 i += 1;
24444 break;
24445
24446 case DW_OP_const2u:
24447 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24448 i += 2;
24449 break;
24450
24451 case DW_OP_const2s:
24452 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24453 i += 2;
24454 break;
24455
24456 case DW_OP_const4u:
24457 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24458 i += 4;
24459 break;
24460
24461 case DW_OP_const4s:
24462 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24463 i += 4;
24464 break;
24465
24466 case DW_OP_const8u:
24467 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24468 i += 8;
24469 break;
24470
24471 case DW_OP_constu:
24472 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24473 &bytes_read);
24474 i += bytes_read;
24475 break;
24476
24477 case DW_OP_consts:
24478 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24479 i += bytes_read;
24480 break;
24481
24482 case DW_OP_dup:
24483 stack[stacki + 1] = stack[stacki];
24484 stacki++;
24485 break;
24486
24487 case DW_OP_plus:
24488 stack[stacki - 1] += stack[stacki];
24489 stacki--;
24490 break;
24491
24492 case DW_OP_plus_uconst:
24493 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24494 &bytes_read);
24495 i += bytes_read;
24496 break;
24497
24498 case DW_OP_minus:
24499 stack[stacki - 1] -= stack[stacki];
24500 stacki--;
24501 break;
24502
24503 case DW_OP_deref:
24504 /* If we're not the last op, then we definitely can't encode
24505 this using GDB's address_class enum. This is valid for partial
24506 global symbols, although the variable's address will be bogus
24507 in the psymtab. */
24508 if (i < size)
24509 {
24510 if (computed == nullptr)
24511 dwarf2_complex_location_expr_complaint ();
24512 else
24513 return 0;
24514 }
24515 break;
24516
24517 case DW_OP_GNU_push_tls_address:
24518 case DW_OP_form_tls_address:
24519 /* The top of the stack has the offset from the beginning
24520 of the thread control block at which the variable is located. */
24521 /* Nothing should follow this operator, so the top of stack would
24522 be returned. */
24523 /* This is valid for partial global symbols, but the variable's
24524 address will be bogus in the psymtab. Make it always at least
24525 non-zero to not look as a variable garbage collected by linker
24526 which have DW_OP_addr 0. */
24527 if (i < size)
24528 {
24529 if (computed == nullptr)
24530 dwarf2_complex_location_expr_complaint ();
24531 else
24532 return 0;
24533 }
24534 stack[stacki]++;
24535 break;
24536
24537 case DW_OP_GNU_uninit:
24538 if (computed != nullptr)
24539 return 0;
24540 break;
24541
24542 case DW_OP_addrx:
24543 case DW_OP_GNU_addr_index:
24544 case DW_OP_GNU_const_index:
24545 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24546 &bytes_read);
24547 i += bytes_read;
24548 break;
24549
24550 default:
24551 if (computed == nullptr)
24552 {
24553 const char *name = get_DW_OP_name (op);
24554
24555 if (name)
24556 complaint (_("unsupported stack op: '%s'"),
24557 name);
24558 else
24559 complaint (_("unsupported stack op: '%02x'"),
24560 op);
24561 }
24562
24563 return (stack[stacki]);
24564 }
24565
24566 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24567 outside of the allocated space. Also enforce minimum>0. */
24568 if (stacki >= ARRAY_SIZE (stack) - 1)
24569 {
24570 if (computed == nullptr)
24571 complaint (_("location description stack overflow"));
24572 return 0;
24573 }
24574
24575 if (stacki <= 0)
24576 {
24577 if (computed == nullptr)
24578 complaint (_("location description stack underflow"));
24579 return 0;
24580 }
24581 }
24582
24583 if (computed != nullptr)
24584 *computed = true;
24585 return (stack[stacki]);
24586 }
24587
24588 /* memory allocation interface */
24589
24590 static struct dwarf_block *
24591 dwarf_alloc_block (struct dwarf2_cu *cu)
24592 {
24593 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24594 }
24595
24596 static struct die_info *
24597 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24598 {
24599 struct die_info *die;
24600 size_t size = sizeof (struct die_info);
24601
24602 if (num_attrs > 1)
24603 size += (num_attrs - 1) * sizeof (struct attribute);
24604
24605 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24606 memset (die, 0, sizeof (struct die_info));
24607 return (die);
24608 }
24609
24610 \f
24611
24612 /* Macro support. */
24613
24614 /* An overload of dwarf_decode_macros that finds the correct section
24615 and ensures it is read in before calling the other overload. */
24616
24617 static void
24618 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24619 int section_is_gnu)
24620 {
24621 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24622 struct objfile *objfile = per_objfile->objfile;
24623 const struct line_header *lh = cu->line_header;
24624 unsigned int offset_size = cu->header.offset_size;
24625 struct dwarf2_section_info *section;
24626 const char *section_name;
24627
24628 if (cu->dwo_unit != nullptr)
24629 {
24630 if (section_is_gnu)
24631 {
24632 section = &cu->dwo_unit->dwo_file->sections.macro;
24633 section_name = ".debug_macro.dwo";
24634 }
24635 else
24636 {
24637 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24638 section_name = ".debug_macinfo.dwo";
24639 }
24640 }
24641 else
24642 {
24643 if (section_is_gnu)
24644 {
24645 section = &per_objfile->per_bfd->macro;
24646 section_name = ".debug_macro";
24647 }
24648 else
24649 {
24650 section = &per_objfile->per_bfd->macinfo;
24651 section_name = ".debug_macinfo";
24652 }
24653 }
24654
24655 section->read (objfile);
24656 if (section->buffer == nullptr)
24657 {
24658 complaint (_("missing %s section"), section_name);
24659 return;
24660 }
24661
24662 buildsym_compunit *builder = cu->get_builder ();
24663
24664 struct dwarf2_section_info *str_offsets_section;
24665 struct dwarf2_section_info *str_section;
24666 ULONGEST str_offsets_base;
24667
24668 if (cu->dwo_unit != nullptr)
24669 {
24670 str_offsets_section = &cu->dwo_unit->dwo_file
24671 ->sections.str_offsets;
24672 str_section = &cu->dwo_unit->dwo_file->sections.str;
24673 str_offsets_base = cu->header.addr_size;
24674 }
24675 else
24676 {
24677 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24678 str_section = &per_objfile->per_bfd->str;
24679 str_offsets_base = *cu->str_offsets_base;
24680 }
24681
24682 dwarf_decode_macros (per_objfile, builder, section, lh,
24683 offset_size, offset, str_section, str_offsets_section,
24684 str_offsets_base, section_is_gnu);
24685 }
24686
24687 /* Return the .debug_loc section to use for CU.
24688 For DWO files use .debug_loc.dwo. */
24689
24690 static struct dwarf2_section_info *
24691 cu_debug_loc_section (struct dwarf2_cu *cu)
24692 {
24693 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24694
24695 if (cu->dwo_unit)
24696 {
24697 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24698
24699 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24700 }
24701 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24702 : &per_objfile->per_bfd->loc);
24703 }
24704
24705 /* Return the .debug_rnglists section to use for CU. */
24706 static struct dwarf2_section_info *
24707 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24708 {
24709 if (cu->header.version < 5)
24710 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24711 cu->header.version);
24712 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24713
24714 /* Make sure we read the .debug_rnglists section from the file that
24715 contains the DW_AT_ranges attribute we are reading. Normally that
24716 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24717 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24718 program. */
24719 if (cu->dwo_unit != nullptr
24720 && tag != DW_TAG_compile_unit
24721 && tag != DW_TAG_skeleton_unit)
24722 {
24723 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24724
24725 if (sections->rnglists.size > 0)
24726 return &sections->rnglists;
24727 else
24728 error (_(".debug_rnglists section is missing from .dwo file."));
24729 }
24730 return &dwarf2_per_objfile->per_bfd->rnglists;
24731 }
24732
24733 /* A helper function that fills in a dwarf2_loclist_baton. */
24734
24735 static void
24736 fill_in_loclist_baton (struct dwarf2_cu *cu,
24737 struct dwarf2_loclist_baton *baton,
24738 const struct attribute *attr)
24739 {
24740 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24741 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24742
24743 section->read (per_objfile->objfile);
24744
24745 baton->per_objfile = per_objfile;
24746 baton->per_cu = cu->per_cu;
24747 gdb_assert (baton->per_cu);
24748 /* We don't know how long the location list is, but make sure we
24749 don't run off the edge of the section. */
24750 baton->size = section->size - attr->as_unsigned ();
24751 baton->data = section->buffer + attr->as_unsigned ();
24752 if (cu->base_address.has_value ())
24753 baton->base_address = *cu->base_address;
24754 else
24755 baton->base_address = 0;
24756 baton->from_dwo = cu->dwo_unit != NULL;
24757 }
24758
24759 static void
24760 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24761 struct dwarf2_cu *cu, int is_block)
24762 {
24763 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24764 struct objfile *objfile = per_objfile->objfile;
24765 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24766
24767 if (attr->form_is_section_offset ()
24768 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24769 the section. If so, fall through to the complaint in the
24770 other branch. */
24771 && attr->as_unsigned () < section->get_size (objfile))
24772 {
24773 struct dwarf2_loclist_baton *baton;
24774
24775 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24776
24777 fill_in_loclist_baton (cu, baton, attr);
24778
24779 if (!cu->base_address.has_value ())
24780 complaint (_("Location list used without "
24781 "specifying the CU base address."));
24782
24783 SYMBOL_ACLASS_INDEX (sym) = (is_block
24784 ? dwarf2_loclist_block_index
24785 : dwarf2_loclist_index);
24786 SYMBOL_LOCATION_BATON (sym) = baton;
24787 }
24788 else
24789 {
24790 struct dwarf2_locexpr_baton *baton;
24791
24792 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24793 baton->per_objfile = per_objfile;
24794 baton->per_cu = cu->per_cu;
24795 gdb_assert (baton->per_cu);
24796
24797 if (attr->form_is_block ())
24798 {
24799 /* Note that we're just copying the block's data pointer
24800 here, not the actual data. We're still pointing into the
24801 info_buffer for SYM's objfile; right now we never release
24802 that buffer, but when we do clean up properly this may
24803 need to change. */
24804 struct dwarf_block *block = attr->as_block ();
24805 baton->size = block->size;
24806 baton->data = block->data;
24807 }
24808 else
24809 {
24810 dwarf2_invalid_attrib_class_complaint ("location description",
24811 sym->natural_name ());
24812 baton->size = 0;
24813 }
24814
24815 SYMBOL_ACLASS_INDEX (sym) = (is_block
24816 ? dwarf2_locexpr_block_index
24817 : dwarf2_locexpr_index);
24818 SYMBOL_LOCATION_BATON (sym) = baton;
24819 }
24820 }
24821
24822 /* See read.h. */
24823
24824 const comp_unit_head *
24825 dwarf2_per_cu_data::get_header () const
24826 {
24827 if (!m_header_read_in)
24828 {
24829 const gdb_byte *info_ptr
24830 = this->section->buffer + to_underlying (this->sect_off);
24831
24832 memset (&m_header, 0, sizeof (m_header));
24833
24834 read_comp_unit_head (&m_header, info_ptr, this->section,
24835 rcuh_kind::COMPILE);
24836 }
24837
24838 return &m_header;
24839 }
24840
24841 /* See read.h. */
24842
24843 int
24844 dwarf2_per_cu_data::addr_size () const
24845 {
24846 return this->get_header ()->addr_size;
24847 }
24848
24849 /* See read.h. */
24850
24851 int
24852 dwarf2_per_cu_data::offset_size () const
24853 {
24854 return this->get_header ()->offset_size;
24855 }
24856
24857 /* See read.h. */
24858
24859 int
24860 dwarf2_per_cu_data::ref_addr_size () const
24861 {
24862 const comp_unit_head *header = this->get_header ();
24863
24864 if (header->version == 2)
24865 return header->addr_size;
24866 else
24867 return header->offset_size;
24868 }
24869
24870 /* See read.h. */
24871
24872 struct type *
24873 dwarf2_cu::addr_type () const
24874 {
24875 struct objfile *objfile = this->per_objfile->objfile;
24876 struct type *void_type = objfile_type (objfile)->builtin_void;
24877 struct type *addr_type = lookup_pointer_type (void_type);
24878 int addr_size = this->per_cu->addr_size ();
24879
24880 if (TYPE_LENGTH (addr_type) == addr_size)
24881 return addr_type;
24882
24883 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24884 return addr_type;
24885 }
24886
24887 /* A helper function for dwarf2_find_containing_comp_unit that returns
24888 the index of the result, and that searches a vector. It will
24889 return a result even if the offset in question does not actually
24890 occur in any CU. This is separate so that it can be unit
24891 tested. */
24892
24893 static int
24894 dwarf2_find_containing_comp_unit
24895 (sect_offset sect_off,
24896 unsigned int offset_in_dwz,
24897 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24898 {
24899 int low, high;
24900
24901 low = 0;
24902 high = all_comp_units.size () - 1;
24903 while (high > low)
24904 {
24905 struct dwarf2_per_cu_data *mid_cu;
24906 int mid = low + (high - low) / 2;
24907
24908 mid_cu = all_comp_units[mid];
24909 if (mid_cu->is_dwz > offset_in_dwz
24910 || (mid_cu->is_dwz == offset_in_dwz
24911 && mid_cu->sect_off + mid_cu->length > sect_off))
24912 high = mid;
24913 else
24914 low = mid + 1;
24915 }
24916 gdb_assert (low == high);
24917 return low;
24918 }
24919
24920 /* Locate the .debug_info compilation unit from CU's objfile which contains
24921 the DIE at OFFSET. Raises an error on failure. */
24922
24923 static struct dwarf2_per_cu_data *
24924 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24925 unsigned int offset_in_dwz,
24926 dwarf2_per_objfile *per_objfile)
24927 {
24928 int low = dwarf2_find_containing_comp_unit
24929 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24930 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24931
24932 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24933 {
24934 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24935 error (_("Dwarf Error: could not find partial DIE containing "
24936 "offset %s [in module %s]"),
24937 sect_offset_str (sect_off),
24938 bfd_get_filename (per_objfile->objfile->obfd));
24939
24940 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24941 <= sect_off);
24942 return per_objfile->per_bfd->all_comp_units[low-1];
24943 }
24944 else
24945 {
24946 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24947 && sect_off >= this_cu->sect_off + this_cu->length)
24948 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24949 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24950 return this_cu;
24951 }
24952 }
24953
24954 #if GDB_SELF_TEST
24955
24956 namespace selftests {
24957 namespace find_containing_comp_unit {
24958
24959 static void
24960 run_test ()
24961 {
24962 struct dwarf2_per_cu_data one {};
24963 struct dwarf2_per_cu_data two {};
24964 struct dwarf2_per_cu_data three {};
24965 struct dwarf2_per_cu_data four {};
24966
24967 one.length = 5;
24968 two.sect_off = sect_offset (one.length);
24969 two.length = 7;
24970
24971 three.length = 5;
24972 three.is_dwz = 1;
24973 four.sect_off = sect_offset (three.length);
24974 four.length = 7;
24975 four.is_dwz = 1;
24976
24977 std::vector<dwarf2_per_cu_data *> units;
24978 units.push_back (&one);
24979 units.push_back (&two);
24980 units.push_back (&three);
24981 units.push_back (&four);
24982
24983 int result;
24984
24985 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24986 SELF_CHECK (units[result] == &one);
24987 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24988 SELF_CHECK (units[result] == &one);
24989 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24990 SELF_CHECK (units[result] == &two);
24991
24992 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24993 SELF_CHECK (units[result] == &three);
24994 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24995 SELF_CHECK (units[result] == &three);
24996 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24997 SELF_CHECK (units[result] == &four);
24998 }
24999
25000 }
25001 }
25002
25003 #endif /* GDB_SELF_TEST */
25004
25005 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
25006
25007 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
25008 dwarf2_per_objfile *per_objfile)
25009 : per_cu (per_cu),
25010 per_objfile (per_objfile),
25011 mark (false),
25012 has_loclist (false),
25013 checked_producer (false),
25014 producer_is_gxx_lt_4_6 (false),
25015 producer_is_gcc_lt_4_3 (false),
25016 producer_is_icc (false),
25017 producer_is_icc_lt_14 (false),
25018 producer_is_codewarrior (false),
25019 processing_has_namespace_info (false)
25020 {
25021 }
25022
25023 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25024
25025 static void
25026 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25027 enum language pretend_language)
25028 {
25029 struct attribute *attr;
25030
25031 /* Set the language we're debugging. */
25032 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25033 if (attr != nullptr)
25034 set_cu_language (attr->constant_value (0), cu);
25035 else
25036 {
25037 cu->language = pretend_language;
25038 cu->language_defn = language_def (cu->language);
25039 }
25040
25041 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
25042 }
25043
25044 /* See read.h. */
25045
25046 dwarf2_cu *
25047 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
25048 {
25049 auto it = m_dwarf2_cus.find (per_cu);
25050 if (it == m_dwarf2_cus.end ())
25051 return nullptr;
25052
25053 return it->second;
25054 }
25055
25056 /* See read.h. */
25057
25058 void
25059 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25060 {
25061 gdb_assert (this->get_cu (per_cu) == nullptr);
25062
25063 m_dwarf2_cus[per_cu] = cu;
25064 }
25065
25066 /* See read.h. */
25067
25068 void
25069 dwarf2_per_objfile::age_comp_units ()
25070 {
25071 dwarf_read_debug_printf_v ("running");
25072
25073 /* Start by clearing all marks. */
25074 for (auto pair : m_dwarf2_cus)
25075 pair.second->mark = false;
25076
25077 /* Traverse all CUs, mark them and their dependencies if used recently
25078 enough. */
25079 for (auto pair : m_dwarf2_cus)
25080 {
25081 dwarf2_cu *cu = pair.second;
25082
25083 cu->last_used++;
25084 if (cu->last_used <= dwarf_max_cache_age)
25085 dwarf2_mark (cu);
25086 }
25087
25088 /* Delete all CUs still not marked. */
25089 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
25090 {
25091 dwarf2_cu *cu = it->second;
25092
25093 if (!cu->mark)
25094 {
25095 dwarf_read_debug_printf_v ("deleting old CU %s",
25096 sect_offset_str (cu->per_cu->sect_off));
25097 delete cu;
25098 it = m_dwarf2_cus.erase (it);
25099 }
25100 else
25101 it++;
25102 }
25103 }
25104
25105 /* See read.h. */
25106
25107 void
25108 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
25109 {
25110 auto it = m_dwarf2_cus.find (per_cu);
25111 if (it == m_dwarf2_cus.end ())
25112 return;
25113
25114 delete it->second;
25115
25116 m_dwarf2_cus.erase (it);
25117 }
25118
25119 dwarf2_per_objfile::~dwarf2_per_objfile ()
25120 {
25121 remove_all_cus ();
25122 }
25123
25124 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25125 We store these in a hash table separate from the DIEs, and preserve them
25126 when the DIEs are flushed out of cache.
25127
25128 The CU "per_cu" pointer is needed because offset alone is not enough to
25129 uniquely identify the type. A file may have multiple .debug_types sections,
25130 or the type may come from a DWO file. Furthermore, while it's more logical
25131 to use per_cu->section+offset, with Fission the section with the data is in
25132 the DWO file but we don't know that section at the point we need it.
25133 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25134 because we can enter the lookup routine, get_die_type_at_offset, from
25135 outside this file, and thus won't necessarily have PER_CU->cu.
25136 Fortunately, PER_CU is stable for the life of the objfile. */
25137
25138 struct dwarf2_per_cu_offset_and_type
25139 {
25140 const struct dwarf2_per_cu_data *per_cu;
25141 sect_offset sect_off;
25142 struct type *type;
25143 };
25144
25145 /* Hash function for a dwarf2_per_cu_offset_and_type. */
25146
25147 static hashval_t
25148 per_cu_offset_and_type_hash (const void *item)
25149 {
25150 const struct dwarf2_per_cu_offset_and_type *ofs
25151 = (const struct dwarf2_per_cu_offset_and_type *) item;
25152
25153 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
25154 }
25155
25156 /* Equality function for a dwarf2_per_cu_offset_and_type. */
25157
25158 static int
25159 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
25160 {
25161 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25162 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25163 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25164 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
25165
25166 return (ofs_lhs->per_cu == ofs_rhs->per_cu
25167 && ofs_lhs->sect_off == ofs_rhs->sect_off);
25168 }
25169
25170 /* Set the type associated with DIE to TYPE. Save it in CU's hash
25171 table if necessary. For convenience, return TYPE.
25172
25173 The DIEs reading must have careful ordering to:
25174 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25175 reading current DIE.
25176 * Not trying to dereference contents of still incompletely read in types
25177 while reading in other DIEs.
25178 * Enable referencing still incompletely read in types just by a pointer to
25179 the type without accessing its fields.
25180
25181 Therefore caller should follow these rules:
25182 * Try to fetch any prerequisite types we may need to build this DIE type
25183 before building the type and calling set_die_type.
25184 * After building type call set_die_type for current DIE as soon as
25185 possible before fetching more types to complete the current type.
25186 * Make the type as complete as possible before fetching more types. */
25187
25188 static struct type *
25189 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25190 bool skip_data_location)
25191 {
25192 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25193 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25194 struct objfile *objfile = per_objfile->objfile;
25195 struct attribute *attr;
25196 struct dynamic_prop prop;
25197
25198 /* For Ada types, make sure that the gnat-specific data is always
25199 initialized (if not already set). There are a few types where
25200 we should not be doing so, because the type-specific area is
25201 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25202 where the type-specific area is used to store the floatformat).
25203 But this is not a problem, because the gnat-specific information
25204 is actually not needed for these types. */
25205 if (need_gnat_info (cu)
25206 && type->code () != TYPE_CODE_FUNC
25207 && type->code () != TYPE_CODE_FLT
25208 && type->code () != TYPE_CODE_METHODPTR
25209 && type->code () != TYPE_CODE_MEMBERPTR
25210 && type->code () != TYPE_CODE_METHOD
25211 && type->code () != TYPE_CODE_FIXED_POINT
25212 && !HAVE_GNAT_AUX_INFO (type))
25213 INIT_GNAT_SPECIFIC (type);
25214
25215 /* Read DW_AT_allocated and set in type. */
25216 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25217 if (attr != NULL)
25218 {
25219 struct type *prop_type = cu->addr_sized_int_type (false);
25220 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25221 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25222 }
25223
25224 /* Read DW_AT_associated and set in type. */
25225 attr = dwarf2_attr (die, DW_AT_associated, cu);
25226 if (attr != NULL)
25227 {
25228 struct type *prop_type = cu->addr_sized_int_type (false);
25229 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25230 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25231 }
25232
25233 /* Read DW_AT_data_location and set in type. */
25234 if (!skip_data_location)
25235 {
25236 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25237 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25238 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25239 }
25240
25241 if (per_objfile->die_type_hash == NULL)
25242 per_objfile->die_type_hash
25243 = htab_up (htab_create_alloc (127,
25244 per_cu_offset_and_type_hash,
25245 per_cu_offset_and_type_eq,
25246 NULL, xcalloc, xfree));
25247
25248 ofs.per_cu = cu->per_cu;
25249 ofs.sect_off = die->sect_off;
25250 ofs.type = type;
25251 slot = (struct dwarf2_per_cu_offset_and_type **)
25252 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25253 if (*slot)
25254 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25255 sect_offset_str (die->sect_off));
25256 *slot = XOBNEW (&objfile->objfile_obstack,
25257 struct dwarf2_per_cu_offset_and_type);
25258 **slot = ofs;
25259 return type;
25260 }
25261
25262 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25263 or return NULL if the die does not have a saved type. */
25264
25265 static struct type *
25266 get_die_type_at_offset (sect_offset sect_off,
25267 dwarf2_per_cu_data *per_cu,
25268 dwarf2_per_objfile *per_objfile)
25269 {
25270 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25271
25272 if (per_objfile->die_type_hash == NULL)
25273 return NULL;
25274
25275 ofs.per_cu = per_cu;
25276 ofs.sect_off = sect_off;
25277 slot = ((struct dwarf2_per_cu_offset_and_type *)
25278 htab_find (per_objfile->die_type_hash.get (), &ofs));
25279 if (slot)
25280 return slot->type;
25281 else
25282 return NULL;
25283 }
25284
25285 /* Look up the type for DIE in CU in die_type_hash,
25286 or return NULL if DIE does not have a saved type. */
25287
25288 static struct type *
25289 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25290 {
25291 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25292 }
25293
25294 /* Add a dependence relationship from CU to REF_PER_CU. */
25295
25296 static void
25297 dwarf2_add_dependence (struct dwarf2_cu *cu,
25298 struct dwarf2_per_cu_data *ref_per_cu)
25299 {
25300 void **slot;
25301
25302 if (cu->dependencies == NULL)
25303 cu->dependencies
25304 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25305 NULL, &cu->comp_unit_obstack,
25306 hashtab_obstack_allocate,
25307 dummy_obstack_deallocate);
25308
25309 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25310 if (*slot == NULL)
25311 *slot = ref_per_cu;
25312 }
25313
25314 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25315 Set the mark field in every compilation unit in the
25316 cache that we must keep because we are keeping CU.
25317
25318 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25319
25320 static int
25321 dwarf2_mark_helper (void **slot, void *data)
25322 {
25323 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25324 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25325 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25326
25327 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25328 reading of the chain. As such dependencies remain valid it is not much
25329 useful to track and undo them during QUIT cleanups. */
25330 if (cu == nullptr)
25331 return 1;
25332
25333 if (cu->mark)
25334 return 1;
25335
25336 cu->mark = true;
25337
25338 if (cu->dependencies != nullptr)
25339 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25340
25341 return 1;
25342 }
25343
25344 /* Set the mark field in CU and in every other compilation unit in the
25345 cache that we must keep because we are keeping CU. */
25346
25347 static void
25348 dwarf2_mark (struct dwarf2_cu *cu)
25349 {
25350 if (cu->mark)
25351 return;
25352
25353 cu->mark = true;
25354
25355 if (cu->dependencies != nullptr)
25356 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25357 }
25358
25359 /* Trivial hash function for partial_die_info: the hash value of a DIE
25360 is its offset in .debug_info for this objfile. */
25361
25362 static hashval_t
25363 partial_die_hash (const void *item)
25364 {
25365 const struct partial_die_info *part_die
25366 = (const struct partial_die_info *) item;
25367
25368 return to_underlying (part_die->sect_off);
25369 }
25370
25371 /* Trivial comparison function for partial_die_info structures: two DIEs
25372 are equal if they have the same offset. */
25373
25374 static int
25375 partial_die_eq (const void *item_lhs, const void *item_rhs)
25376 {
25377 const struct partial_die_info *part_die_lhs
25378 = (const struct partial_die_info *) item_lhs;
25379 const struct partial_die_info *part_die_rhs
25380 = (const struct partial_die_info *) item_rhs;
25381
25382 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25383 }
25384
25385 struct cmd_list_element *set_dwarf_cmdlist;
25386 struct cmd_list_element *show_dwarf_cmdlist;
25387
25388 static void
25389 show_check_physname (struct ui_file *file, int from_tty,
25390 struct cmd_list_element *c, const char *value)
25391 {
25392 fprintf_filtered (file,
25393 _("Whether to check \"physname\" is %s.\n"),
25394 value);
25395 }
25396
25397 void _initialize_dwarf2_read ();
25398 void
25399 _initialize_dwarf2_read ()
25400 {
25401 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25402 Set DWARF specific variables.\n\
25403 Configure DWARF variables such as the cache size."),
25404 &set_dwarf_cmdlist, "maintenance set dwarf ",
25405 0/*allow-unknown*/, &maintenance_set_cmdlist);
25406
25407 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25408 Show DWARF specific variables.\n\
25409 Show DWARF variables such as the cache size."),
25410 &show_dwarf_cmdlist, "maintenance show dwarf ",
25411 0/*allow-unknown*/, &maintenance_show_cmdlist);
25412
25413 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25414 &dwarf_max_cache_age, _("\
25415 Set the upper bound on the age of cached DWARF compilation units."), _("\
25416 Show the upper bound on the age of cached DWARF compilation units."), _("\
25417 A higher limit means that cached compilation units will be stored\n\
25418 in memory longer, and more total memory will be used. Zero disables\n\
25419 caching, which can slow down startup."),
25420 NULL,
25421 show_dwarf_max_cache_age,
25422 &set_dwarf_cmdlist,
25423 &show_dwarf_cmdlist);
25424
25425 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25426 Set debugging of the DWARF reader."), _("\
25427 Show debugging of the DWARF reader."), _("\
25428 When enabled (non-zero), debugging messages are printed during DWARF\n\
25429 reading and symtab expansion. A value of 1 (one) provides basic\n\
25430 information. A value greater than 1 provides more verbose information."),
25431 NULL,
25432 NULL,
25433 &setdebuglist, &showdebuglist);
25434
25435 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25436 Set debugging of the DWARF DIE reader."), _("\
25437 Show debugging of the DWARF DIE reader."), _("\
25438 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25439 The value is the maximum depth to print."),
25440 NULL,
25441 NULL,
25442 &setdebuglist, &showdebuglist);
25443
25444 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25445 Set debugging of the dwarf line reader."), _("\
25446 Show debugging of the dwarf line reader."), _("\
25447 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25448 A value of 1 (one) provides basic information.\n\
25449 A value greater than 1 provides more verbose information."),
25450 NULL,
25451 NULL,
25452 &setdebuglist, &showdebuglist);
25453
25454 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25455 Set cross-checking of \"physname\" code against demangler."), _("\
25456 Show cross-checking of \"physname\" code against demangler."), _("\
25457 When enabled, GDB's internal \"physname\" code is checked against\n\
25458 the demangler."),
25459 NULL, show_check_physname,
25460 &setdebuglist, &showdebuglist);
25461
25462 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25463 no_class, &use_deprecated_index_sections, _("\
25464 Set whether to use deprecated gdb_index sections."), _("\
25465 Show whether to use deprecated gdb_index sections."), _("\
25466 When enabled, deprecated .gdb_index sections are used anyway.\n\
25467 Normally they are ignored either because of a missing feature or\n\
25468 performance issue.\n\
25469 Warning: This option must be enabled before gdb reads the file."),
25470 NULL,
25471 NULL,
25472 &setlist, &showlist);
25473
25474 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25475 &dwarf2_locexpr_funcs);
25476 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25477 &dwarf2_loclist_funcs);
25478
25479 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25480 &dwarf2_block_frame_base_locexpr_funcs);
25481 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25482 &dwarf2_block_frame_base_loclist_funcs);
25483
25484 #if GDB_SELF_TEST
25485 selftests::register_test ("dw2_expand_symtabs_matching",
25486 selftests::dw2_expand_symtabs_matching::run_test);
25487 selftests::register_test ("dwarf2_find_containing_comp_unit",
25488 selftests::find_containing_comp_unit::run_test);
25489 #endif
25490 }
This page took 0.590302 seconds and 5 git commands to generate.