6befb77a2b6d8ee602333f90dea31b5d5010d6c1
[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/sect-names.h"
44 #include "dwarf2/stringify.h"
45 #include "dwarf2/public.h"
46 #include "bfd.h"
47 #include "elf-bfd.h"
48 #include "symtab.h"
49 #include "gdbtypes.h"
50 #include "objfiles.h"
51 #include "dwarf2.h"
52 #include "buildsym.h"
53 #include "demangle.h"
54 #include "gdb-demangle.h"
55 #include "filenames.h" /* for DOSish file names */
56 #include "language.h"
57 #include "complaints.h"
58 #include "dwarf2/expr.h"
59 #include "dwarf2/loc.h"
60 #include "cp-support.h"
61 #include "hashtab.h"
62 #include "command.h"
63 #include "gdbcmd.h"
64 #include "block.h"
65 #include "addrmap.h"
66 #include "typeprint.h"
67 #include "psympriv.h"
68 #include "c-lang.h"
69 #include "go-lang.h"
70 #include "valprint.h"
71 #include "gdbcore.h" /* for gnutarget */
72 #include "gdb/gdb-index.h"
73 #include "gdb_bfd.h"
74 #include "f-lang.h"
75 #include "source.h"
76 #include "build-id.h"
77 #include "namespace.h"
78 #include "gdbsupport/function-view.h"
79 #include "gdbsupport/gdb_optional.h"
80 #include "gdbsupport/underlying.h"
81 #include "gdbsupport/hash_enum.h"
82 #include "filename-seen-cache.h"
83 #include "producer.h"
84 #include <fcntl.h>
85 #include <algorithm>
86 #include <unordered_map>
87 #include "gdbsupport/selftest.h"
88 #include "rust-lang.h"
89 #include "gdbsupport/pathstuff.h"
90 #include "count-one-bits.h"
91
92 /* When == 1, print basic high level tracing messages.
93 When > 1, be more verbose.
94 This is in contrast to the low level DIE reading of dwarf_die_debug. */
95 static unsigned int dwarf_read_debug = 0;
96
97 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
98
99 #define dwarf_read_debug_printf(fmt, ...) \
100 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
101 ##__VA_ARGS__)
102
103 /* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
104
105 #define dwarf_read_debug_printf_v(fmt, ...) \
106 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
107 ##__VA_ARGS__)
108
109 /* When non-zero, dump DIEs after they are read in. */
110 static unsigned int dwarf_die_debug = 0;
111
112 /* When non-zero, dump line number entries as they are read in. */
113 unsigned int dwarf_line_debug = 0;
114
115 /* When true, cross-check physname against demangler. */
116 static bool check_physname = false;
117
118 /* When true, do not reject deprecated .gdb_index sections. */
119 static bool use_deprecated_index_sections = false;
120
121 /* This is used to store the data that is always per objfile. */
122 static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
123
124 /* These are used to store the dwarf2_per_bfd objects.
125
126 objfiles having the same BFD, which doesn't require relocations, are going to
127 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
128
129 Other objfiles are not going to share a dwarf2_per_bfd with any other
130 objfiles, so they'll have their own version kept in the _objfile_data_key
131 version. */
132 static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
133 static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
134
135 /* The "aclass" indices for various kinds of computed DWARF symbols. */
136
137 static int dwarf2_locexpr_index;
138 static int dwarf2_loclist_index;
139 static int dwarf2_locexpr_block_index;
140 static int dwarf2_loclist_block_index;
141
142 /* Size of .debug_loclists section header for 32-bit DWARF format. */
143 #define LOCLIST_HEADER_SIZE32 12
144
145 /* Size of .debug_loclists section header for 64-bit DWARF format. */
146 #define LOCLIST_HEADER_SIZE64 20
147
148 /* Size of .debug_rnglists section header for 32-bit DWARF format. */
149 #define RNGLIST_HEADER_SIZE32 12
150
151 /* Size of .debug_rnglists section header for 64-bit DWARF format. */
152 #define RNGLIST_HEADER_SIZE64 20
153
154 /* An index into a (C++) symbol name component in a symbol name as
155 recorded in the mapped_index's symbol table. For each C++ symbol
156 in the symbol table, we record one entry for the start of each
157 component in the symbol in a table of name components, and then
158 sort the table, in order to be able to binary search symbol names,
159 ignoring leading namespaces, both completion and regular look up.
160 For example, for symbol "A::B::C", we'll have an entry that points
161 to "A::B::C", another that points to "B::C", and another for "C".
162 Note that function symbols in GDB index have no parameter
163 information, just the function/method names. You can convert a
164 name_component to a "const char *" using the
165 'mapped_index::symbol_name_at(offset_type)' method. */
166
167 struct name_component
168 {
169 /* Offset in the symbol name where the component starts. Stored as
170 a (32-bit) offset instead of a pointer to save memory and improve
171 locality on 64-bit architectures. */
172 offset_type name_offset;
173
174 /* The symbol's index in the symbol and constant pool tables of a
175 mapped_index. */
176 offset_type idx;
177 };
178
179 /* Base class containing bits shared by both .gdb_index and
180 .debug_name indexes. */
181
182 struct mapped_index_base
183 {
184 mapped_index_base () = default;
185 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
186
187 /* The name_component table (a sorted vector). See name_component's
188 description above. */
189 std::vector<name_component> name_components;
190
191 /* How NAME_COMPONENTS is sorted. */
192 enum case_sensitivity name_components_casing;
193
194 /* Return the number of names in the symbol table. */
195 virtual size_t symbol_name_count () const = 0;
196
197 /* Get the name of the symbol at IDX in the symbol table. */
198 virtual const char *symbol_name_at
199 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
200
201 /* Return whether the name at IDX in the symbol table should be
202 ignored. */
203 virtual bool symbol_name_slot_invalid (offset_type idx) const
204 {
205 return false;
206 }
207
208 /* Build the symbol name component sorted vector, if we haven't
209 yet. */
210 void build_name_components (dwarf2_per_objfile *per_objfile);
211
212 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
213 possible matches for LN_NO_PARAMS in the name component
214 vector. */
215 std::pair<std::vector<name_component>::const_iterator,
216 std::vector<name_component>::const_iterator>
217 find_name_components_bounds (const lookup_name_info &ln_no_params,
218 enum language lang,
219 dwarf2_per_objfile *per_objfile) const;
220
221 /* Prevent deleting/destroying via a base class pointer. */
222 protected:
223 ~mapped_index_base() = default;
224 };
225
226 /* A description of the mapped index. The file format is described in
227 a comment by the code that writes the index. */
228 struct mapped_index final : public mapped_index_base
229 {
230 /* A slot/bucket in the symbol table hash. */
231 struct symbol_table_slot
232 {
233 const offset_type name;
234 const offset_type vec;
235 };
236
237 /* Index data format version. */
238 int version = 0;
239
240 /* The address table data. */
241 gdb::array_view<const gdb_byte> address_table;
242
243 /* The symbol table, implemented as a hash table. */
244 gdb::array_view<symbol_table_slot> symbol_table;
245
246 /* A pointer to the constant pool. */
247 const char *constant_pool = nullptr;
248
249 bool symbol_name_slot_invalid (offset_type idx) const override
250 {
251 const auto &bucket = this->symbol_table[idx];
252 return bucket.name == 0 && bucket.vec == 0;
253 }
254
255 /* Convenience method to get at the name of the symbol at IDX in the
256 symbol table. */
257 const char *symbol_name_at
258 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
259 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
260
261 size_t symbol_name_count () const override
262 { return this->symbol_table.size (); }
263 };
264
265 /* A description of the mapped .debug_names.
266 Uninitialized map has CU_COUNT 0. */
267 struct mapped_debug_names final : public mapped_index_base
268 {
269 bfd_endian dwarf5_byte_order;
270 bool dwarf5_is_dwarf64;
271 bool augmentation_is_gdb;
272 uint8_t offset_size;
273 uint32_t cu_count = 0;
274 uint32_t tu_count, bucket_count, name_count;
275 const gdb_byte *cu_table_reordered, *tu_table_reordered;
276 const uint32_t *bucket_table_reordered, *hash_table_reordered;
277 const gdb_byte *name_table_string_offs_reordered;
278 const gdb_byte *name_table_entry_offs_reordered;
279 const gdb_byte *entry_pool;
280
281 struct index_val
282 {
283 ULONGEST dwarf_tag;
284 struct attr
285 {
286 /* Attribute name DW_IDX_*. */
287 ULONGEST dw_idx;
288
289 /* Attribute form DW_FORM_*. */
290 ULONGEST form;
291
292 /* Value if FORM is DW_FORM_implicit_const. */
293 LONGEST implicit_const;
294 };
295 std::vector<attr> attr_vec;
296 };
297
298 std::unordered_map<ULONGEST, index_val> abbrev_map;
299
300 const char *namei_to_name
301 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
302
303 /* Implementation of the mapped_index_base virtual interface, for
304 the name_components cache. */
305
306 const char *symbol_name_at
307 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
308 { return namei_to_name (idx, per_objfile); }
309
310 size_t symbol_name_count () const override
311 { return this->name_count; }
312 };
313
314 /* See dwarf2read.h. */
315
316 dwarf2_per_objfile *
317 get_dwarf2_per_objfile (struct objfile *objfile)
318 {
319 return dwarf2_objfile_data_key.get (objfile);
320 }
321
322 /* Default names of the debugging sections. */
323
324 /* Note that if the debugging section has been compressed, it might
325 have a name like .zdebug_info. */
326
327 const struct dwarf2_debug_sections dwarf2_elf_names =
328 {
329 { ".debug_info", ".zdebug_info" },
330 { ".debug_abbrev", ".zdebug_abbrev" },
331 { ".debug_line", ".zdebug_line" },
332 { ".debug_loc", ".zdebug_loc" },
333 { ".debug_loclists", ".zdebug_loclists" },
334 { ".debug_macinfo", ".zdebug_macinfo" },
335 { ".debug_macro", ".zdebug_macro" },
336 { ".debug_str", ".zdebug_str" },
337 { ".debug_str_offsets", ".zdebug_str_offsets" },
338 { ".debug_line_str", ".zdebug_line_str" },
339 { ".debug_ranges", ".zdebug_ranges" },
340 { ".debug_rnglists", ".zdebug_rnglists" },
341 { ".debug_types", ".zdebug_types" },
342 { ".debug_addr", ".zdebug_addr" },
343 { ".debug_frame", ".zdebug_frame" },
344 { ".eh_frame", NULL },
345 { ".gdb_index", ".zgdb_index" },
346 { ".debug_names", ".zdebug_names" },
347 { ".debug_aranges", ".zdebug_aranges" },
348 23
349 };
350
351 /* List of DWO/DWP sections. */
352
353 static const struct dwop_section_names
354 {
355 struct dwarf2_section_names abbrev_dwo;
356 struct dwarf2_section_names info_dwo;
357 struct dwarf2_section_names line_dwo;
358 struct dwarf2_section_names loc_dwo;
359 struct dwarf2_section_names loclists_dwo;
360 struct dwarf2_section_names macinfo_dwo;
361 struct dwarf2_section_names macro_dwo;
362 struct dwarf2_section_names rnglists_dwo;
363 struct dwarf2_section_names str_dwo;
364 struct dwarf2_section_names str_offsets_dwo;
365 struct dwarf2_section_names types_dwo;
366 struct dwarf2_section_names cu_index;
367 struct dwarf2_section_names tu_index;
368 }
369 dwop_section_names =
370 {
371 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
372 { ".debug_info.dwo", ".zdebug_info.dwo" },
373 { ".debug_line.dwo", ".zdebug_line.dwo" },
374 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
375 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
376 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
377 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
378 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
379 { ".debug_str.dwo", ".zdebug_str.dwo" },
380 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
381 { ".debug_types.dwo", ".zdebug_types.dwo" },
382 { ".debug_cu_index", ".zdebug_cu_index" },
383 { ".debug_tu_index", ".zdebug_tu_index" },
384 };
385
386 /* local data types */
387
388 /* The location list and range list sections (.debug_loclists & .debug_rnglists)
389 begin with a header, which contains the following information. */
390 struct loclists_rnglists_header
391 {
392 /* A 4-byte or 12-byte length containing the length of the
393 set of entries for this compilation unit, not including the
394 length field itself. */
395 unsigned int length;
396
397 /* A 2-byte version identifier. */
398 short version;
399
400 /* A 1-byte unsigned integer containing the size in bytes of an address on
401 the target system. */
402 unsigned char addr_size;
403
404 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
405 on the target system. */
406 unsigned char segment_collector_size;
407
408 /* A 4-byte count of the number of offsets that follow the header. */
409 unsigned int offset_entry_count;
410 };
411
412 /* Type used for delaying computation of method physnames.
413 See comments for compute_delayed_physnames. */
414 struct delayed_method_info
415 {
416 /* The type to which the method is attached, i.e., its parent class. */
417 struct type *type;
418
419 /* The index of the method in the type's function fieldlists. */
420 int fnfield_index;
421
422 /* The index of the method in the fieldlist. */
423 int index;
424
425 /* The name of the DIE. */
426 const char *name;
427
428 /* The DIE associated with this method. */
429 struct die_info *die;
430 };
431
432 /* Internal state when decoding a particular compilation unit. */
433 struct dwarf2_cu
434 {
435 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
436 dwarf2_per_objfile *per_objfile);
437
438 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
439
440 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
441 Create the set of symtabs used by this TU, or if this TU is sharing
442 symtabs with another TU and the symtabs have already been created
443 then restore those symtabs in the line header.
444 We don't need the pc/line-number mapping for type units. */
445 void setup_type_unit_groups (struct die_info *die);
446
447 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
448 buildsym_compunit constructor. */
449 struct compunit_symtab *start_symtab (const char *name,
450 const char *comp_dir,
451 CORE_ADDR low_pc);
452
453 /* Reset the builder. */
454 void reset_builder () { m_builder.reset (); }
455
456 /* Return a type that is a generic pointer type, the size of which
457 matches the address size given in the compilation unit header for
458 this CU. */
459 struct type *addr_type () const;
460
461 /* Find an integer type the same size as the address size given in
462 the compilation unit header for this CU. UNSIGNED_P controls if
463 the integer is unsigned or not. */
464 struct type *addr_sized_int_type (bool unsigned_p) const;
465
466 /* The header of the compilation unit. */
467 struct comp_unit_head header {};
468
469 /* Base address of this compilation unit. */
470 gdb::optional<CORE_ADDR> base_address;
471
472 /* The language we are debugging. */
473 enum language language = language_unknown;
474 const struct language_defn *language_defn = nullptr;
475
476 const char *producer = nullptr;
477
478 private:
479 /* The symtab builder for this CU. This is only non-NULL when full
480 symbols are being read. */
481 std::unique_ptr<buildsym_compunit> m_builder;
482
483 public:
484 /* The generic symbol table building routines have separate lists for
485 file scope symbols and all all other scopes (local scopes). So
486 we need to select the right one to pass to add_symbol_to_list().
487 We do it by keeping a pointer to the correct list in list_in_scope.
488
489 FIXME: The original dwarf code just treated the file scope as the
490 first local scope, and all other local scopes as nested local
491 scopes, and worked fine. Check to see if we really need to
492 distinguish these in buildsym.c. */
493 struct pending **list_in_scope = nullptr;
494
495 /* Hash table holding all the loaded partial DIEs
496 with partial_die->offset.SECT_OFF as hash. */
497 htab_t partial_dies = nullptr;
498
499 /* Storage for things with the same lifetime as this read-in compilation
500 unit, including partial DIEs. */
501 auto_obstack comp_unit_obstack;
502
503 /* Backlink to our per_cu entry. */
504 struct dwarf2_per_cu_data *per_cu;
505
506 /* The dwarf2_per_objfile that owns this. */
507 dwarf2_per_objfile *per_objfile;
508
509 /* How many compilation units ago was this CU last referenced? */
510 int last_used = 0;
511
512 /* A hash table of DIE cu_offset for following references with
513 die_info->offset.sect_off as hash. */
514 htab_t die_hash = nullptr;
515
516 /* Full DIEs if read in. */
517 struct die_info *dies = nullptr;
518
519 /* A set of pointers to dwarf2_per_cu_data objects for compilation
520 units referenced by this one. Only set during full symbol processing;
521 partial symbol tables do not have dependencies. */
522 htab_t dependencies = nullptr;
523
524 /* Header data from the line table, during full symbol processing. */
525 struct line_header *line_header = nullptr;
526 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
527 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
528 this is the DW_TAG_compile_unit die for this CU. We'll hold on
529 to the line header as long as this DIE is being processed. See
530 process_die_scope. */
531 die_info *line_header_die_owner = nullptr;
532
533 /* A list of methods which need to have physnames computed
534 after all type information has been read. */
535 std::vector<delayed_method_info> method_list;
536
537 /* To be copied to symtab->call_site_htab. */
538 htab_t call_site_htab = nullptr;
539
540 /* Non-NULL if this CU came from a DWO file.
541 There is an invariant here that is important to remember:
542 Except for attributes copied from the top level DIE in the "main"
543 (or "stub") file in preparation for reading the DWO file
544 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
545 Either there isn't a DWO file (in which case this is NULL and the point
546 is moot), or there is and either we're not going to read it (in which
547 case this is NULL) or there is and we are reading it (in which case this
548 is non-NULL). */
549 struct dwo_unit *dwo_unit = nullptr;
550
551 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
552 Note this value comes from the Fission stub CU/TU's DIE. */
553 gdb::optional<ULONGEST> addr_base;
554
555 /* The DW_AT_GNU_ranges_base attribute, if present.
556
557 This is only relevant in the context of pre-DWARF 5 split units. In this
558 context, there is a .debug_ranges section in the linked executable,
559 containing all the ranges data for all the compilation units. Each
560 skeleton/stub unit has (if needed) a DW_AT_GNU_ranges_base attribute that
561 indicates the base of its contribution to that section. The DW_AT_ranges
562 attributes in the split-unit are of the form DW_FORM_sec_offset and point
563 into the .debug_ranges section of the linked file. However, they are not
564 "true" DW_FORM_sec_offset, because they are relative to the base of their
565 compilation unit's contribution, rather than relative to the beginning of
566 the section. The DW_AT_GNU_ranges_base value must be added to it to make
567 it relative to the beginning of the section.
568
569 Note that the value is zero when we are not in a pre-DWARF 5 split-unit
570 case, so this value can be added without needing to know whether we are in
571 this case or not.
572
573 N.B. If a DW_AT_ranges attribute is found on the DW_TAG_compile_unit in the
574 skeleton/stub, it must not have the base added, as it already points to the
575 right place. And since the DW_TAG_compile_unit DIE in the split-unit can't
576 have a DW_AT_ranges attribute, we can use the
577
578 die->tag != DW_AT_compile_unit
579
580 to determine whether the base should be added or not. */
581 ULONGEST gnu_ranges_base = 0;
582
583 /* The DW_AT_rnglists_base attribute, if present.
584
585 This is used when processing attributes of form DW_FORM_rnglistx in
586 non-split units. Attributes of this form found in a split unit don't
587 use it, as split-unit files have their own non-shared .debug_rnglists.dwo
588 section. */
589 ULONGEST rnglists_base = 0;
590
591 /* The DW_AT_loclists_base attribute if present. */
592 ULONGEST loclist_base = 0;
593
594 /* When reading debug info generated by older versions of rustc, we
595 have to rewrite some union types to be struct types with a
596 variant part. This rewriting must be done after the CU is fully
597 read in, because otherwise at the point of rewriting some struct
598 type might not have been fully processed. So, we keep a list of
599 all such types here and process them after expansion. */
600 std::vector<struct type *> rust_unions;
601
602 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
603 files, the value is implicitly zero. For DWARF 5 version DWO files, the
604 value is often implicit and is the size of the header of
605 .debug_str_offsets section (8 or 4, depending on the address size). */
606 gdb::optional<ULONGEST> str_offsets_base;
607
608 /* Mark used when releasing cached dies. */
609 bool mark : 1;
610
611 /* This CU references .debug_loc. See the symtab->locations_valid field.
612 This test is imperfect as there may exist optimized debug code not using
613 any location list and still facing inlining issues if handled as
614 unoptimized code. For a future better test see GCC PR other/32998. */
615 bool has_loclist : 1;
616
617 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
618 if all the producer_is_* fields are valid. This information is cached
619 because profiling CU expansion showed excessive time spent in
620 producer_is_gxx_lt_4_6. */
621 bool checked_producer : 1;
622 bool producer_is_gxx_lt_4_6 : 1;
623 bool producer_is_gcc_lt_4_3 : 1;
624 bool producer_is_icc : 1;
625 bool producer_is_icc_lt_14 : 1;
626 bool producer_is_codewarrior : 1;
627
628 /* When true, the file that we're processing is known to have
629 debugging info for C++ namespaces. GCC 3.3.x did not produce
630 this information, but later versions do. */
631
632 bool processing_has_namespace_info : 1;
633
634 struct partial_die_info *find_partial_die (sect_offset sect_off);
635
636 /* If this CU was inherited by another CU (via specification,
637 abstract_origin, etc), this is the ancestor CU. */
638 dwarf2_cu *ancestor;
639
640 /* Get the buildsym_compunit for this CU. */
641 buildsym_compunit *get_builder ()
642 {
643 /* If this CU has a builder associated with it, use that. */
644 if (m_builder != nullptr)
645 return m_builder.get ();
646
647 /* Otherwise, search ancestors for a valid builder. */
648 if (ancestor != nullptr)
649 return ancestor->get_builder ();
650
651 return nullptr;
652 }
653 };
654
655 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
656 This includes type_unit_group and quick_file_names. */
657
658 struct stmt_list_hash
659 {
660 /* The DWO unit this table is from or NULL if there is none. */
661 struct dwo_unit *dwo_unit;
662
663 /* Offset in .debug_line or .debug_line.dwo. */
664 sect_offset line_sect_off;
665 };
666
667 /* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
668 an object of this type. This contains elements of type unit groups
669 that can be shared across objfiles. The non-shareable parts are in
670 type_unit_group_unshareable. */
671
672 struct type_unit_group
673 {
674 /* dwarf2read.c's main "handle" on a TU symtab.
675 To simplify things we create an artificial CU that "includes" all the
676 type units using this stmt_list so that the rest of the code still has
677 a "per_cu" handle on the symtab. */
678 struct dwarf2_per_cu_data per_cu;
679
680 /* The TUs that share this DW_AT_stmt_list entry.
681 This is added to while parsing type units to build partial symtabs,
682 and is deleted afterwards and not used again. */
683 std::vector<signatured_type *> *tus;
684
685 /* The data used to construct the hash key. */
686 struct stmt_list_hash hash;
687 };
688
689 /* These sections are what may appear in a (real or virtual) DWO file. */
690
691 struct dwo_sections
692 {
693 struct dwarf2_section_info abbrev;
694 struct dwarf2_section_info line;
695 struct dwarf2_section_info loc;
696 struct dwarf2_section_info loclists;
697 struct dwarf2_section_info macinfo;
698 struct dwarf2_section_info macro;
699 struct dwarf2_section_info rnglists;
700 struct dwarf2_section_info str;
701 struct dwarf2_section_info str_offsets;
702 /* In the case of a virtual DWO file, these two are unused. */
703 struct dwarf2_section_info info;
704 std::vector<dwarf2_section_info> types;
705 };
706
707 /* CUs/TUs in DWP/DWO files. */
708
709 struct dwo_unit
710 {
711 /* Backlink to the containing struct dwo_file. */
712 struct dwo_file *dwo_file;
713
714 /* The "id" that distinguishes this CU/TU.
715 .debug_info calls this "dwo_id", .debug_types calls this "signature".
716 Since signatures came first, we stick with it for consistency. */
717 ULONGEST signature;
718
719 /* The section this CU/TU lives in, in the DWO file. */
720 struct dwarf2_section_info *section;
721
722 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
723 sect_offset sect_off;
724 unsigned int length;
725
726 /* For types, offset in the type's DIE of the type defined by this TU. */
727 cu_offset type_offset_in_tu;
728 };
729
730 /* include/dwarf2.h defines the DWP section codes.
731 It defines a max value but it doesn't define a min value, which we
732 use for error checking, so provide one. */
733
734 enum dwp_v2_section_ids
735 {
736 DW_SECT_MIN = 1
737 };
738
739 /* Data for one DWO file.
740
741 This includes virtual DWO files (a virtual DWO file is a DWO file as it
742 appears in a DWP file). DWP files don't really have DWO files per se -
743 comdat folding of types "loses" the DWO file they came from, and from
744 a high level view DWP files appear to contain a mass of random types.
745 However, to maintain consistency with the non-DWP case we pretend DWP
746 files contain virtual DWO files, and we assign each TU with one virtual
747 DWO file (generally based on the line and abbrev section offsets -
748 a heuristic that seems to work in practice). */
749
750 struct dwo_file
751 {
752 dwo_file () = default;
753 DISABLE_COPY_AND_ASSIGN (dwo_file);
754
755 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
756 For virtual DWO files the name is constructed from the section offsets
757 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
758 from related CU+TUs. */
759 const char *dwo_name = nullptr;
760
761 /* The DW_AT_comp_dir attribute. */
762 const char *comp_dir = nullptr;
763
764 /* The bfd, when the file is open. Otherwise this is NULL.
765 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
766 gdb_bfd_ref_ptr dbfd;
767
768 /* The sections that make up this DWO file.
769 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
770 sections (for lack of a better name). */
771 struct dwo_sections sections {};
772
773 /* The CUs in the file.
774 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
775 an extension to handle LLVM's Link Time Optimization output (where
776 multiple source files may be compiled into a single object/dwo pair). */
777 htab_up cus;
778
779 /* Table of TUs in the file.
780 Each element is a struct dwo_unit. */
781 htab_up tus;
782 };
783
784 /* These sections are what may appear in a DWP file. */
785
786 struct dwp_sections
787 {
788 /* These are used by all DWP versions (1, 2 and 5). */
789 struct dwarf2_section_info str;
790 struct dwarf2_section_info cu_index;
791 struct dwarf2_section_info tu_index;
792
793 /* These are only used by DWP version 2 and version 5 files.
794 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
795 sections are referenced by section number, and are not recorded here.
796 In DWP version 2 or 5 there is at most one copy of all these sections,
797 each section being (effectively) comprised of the concatenation of all of
798 the individual sections that exist in the version 1 format.
799 To keep the code simple we treat each of these concatenated pieces as a
800 section itself (a virtual section?). */
801 struct dwarf2_section_info abbrev;
802 struct dwarf2_section_info info;
803 struct dwarf2_section_info line;
804 struct dwarf2_section_info loc;
805 struct dwarf2_section_info loclists;
806 struct dwarf2_section_info macinfo;
807 struct dwarf2_section_info macro;
808 struct dwarf2_section_info rnglists;
809 struct dwarf2_section_info str_offsets;
810 struct dwarf2_section_info types;
811 };
812
813 /* These sections are what may appear in a virtual DWO file in DWP version 1.
814 A virtual DWO file is a DWO file as it appears in a DWP file. */
815
816 struct virtual_v1_dwo_sections
817 {
818 struct dwarf2_section_info abbrev;
819 struct dwarf2_section_info line;
820 struct dwarf2_section_info loc;
821 struct dwarf2_section_info macinfo;
822 struct dwarf2_section_info macro;
823 struct dwarf2_section_info str_offsets;
824 /* Each DWP hash table entry records one CU or one TU.
825 That is recorded here, and copied to dwo_unit.section. */
826 struct dwarf2_section_info info_or_types;
827 };
828
829 /* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
830 In version 2, the sections of the DWO files are concatenated together
831 and stored in one section of that name. Thus each ELF section contains
832 several "virtual" sections. */
833
834 struct virtual_v2_or_v5_dwo_sections
835 {
836 bfd_size_type abbrev_offset;
837 bfd_size_type abbrev_size;
838
839 bfd_size_type line_offset;
840 bfd_size_type line_size;
841
842 bfd_size_type loc_offset;
843 bfd_size_type loc_size;
844
845 bfd_size_type loclists_offset;
846 bfd_size_type loclists_size;
847
848 bfd_size_type macinfo_offset;
849 bfd_size_type macinfo_size;
850
851 bfd_size_type macro_offset;
852 bfd_size_type macro_size;
853
854 bfd_size_type rnglists_offset;
855 bfd_size_type rnglists_size;
856
857 bfd_size_type str_offsets_offset;
858 bfd_size_type str_offsets_size;
859
860 /* Each DWP hash table entry records one CU or one TU.
861 That is recorded here, and copied to dwo_unit.section. */
862 bfd_size_type info_or_types_offset;
863 bfd_size_type info_or_types_size;
864 };
865
866 /* Contents of DWP hash tables. */
867
868 struct dwp_hash_table
869 {
870 uint32_t version, nr_columns;
871 uint32_t nr_units, nr_slots;
872 const gdb_byte *hash_table, *unit_table;
873 union
874 {
875 struct
876 {
877 const gdb_byte *indices;
878 } v1;
879 struct
880 {
881 /* This is indexed by column number and gives the id of the section
882 in that column. */
883 #define MAX_NR_V2_DWO_SECTIONS \
884 (1 /* .debug_info or .debug_types */ \
885 + 1 /* .debug_abbrev */ \
886 + 1 /* .debug_line */ \
887 + 1 /* .debug_loc */ \
888 + 1 /* .debug_str_offsets */ \
889 + 1 /* .debug_macro or .debug_macinfo */)
890 int section_ids[MAX_NR_V2_DWO_SECTIONS];
891 const gdb_byte *offsets;
892 const gdb_byte *sizes;
893 } v2;
894 struct
895 {
896 /* This is indexed by column number and gives the id of the section
897 in that column. */
898 #define MAX_NR_V5_DWO_SECTIONS \
899 (1 /* .debug_info */ \
900 + 1 /* .debug_abbrev */ \
901 + 1 /* .debug_line */ \
902 + 1 /* .debug_loclists */ \
903 + 1 /* .debug_str_offsets */ \
904 + 1 /* .debug_macro */ \
905 + 1 /* .debug_rnglists */)
906 int section_ids[MAX_NR_V5_DWO_SECTIONS];
907 const gdb_byte *offsets;
908 const gdb_byte *sizes;
909 } v5;
910 } section_pool;
911 };
912
913 /* Data for one DWP file. */
914
915 struct dwp_file
916 {
917 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
918 : name (name_),
919 dbfd (std::move (abfd))
920 {
921 }
922
923 /* Name of the file. */
924 const char *name;
925
926 /* File format version. */
927 int version = 0;
928
929 /* The bfd. */
930 gdb_bfd_ref_ptr dbfd;
931
932 /* Section info for this file. */
933 struct dwp_sections sections {};
934
935 /* Table of CUs in the file. */
936 const struct dwp_hash_table *cus = nullptr;
937
938 /* Table of TUs in the file. */
939 const struct dwp_hash_table *tus = nullptr;
940
941 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
942 htab_up loaded_cus;
943 htab_up loaded_tus;
944
945 /* Table to map ELF section numbers to their sections.
946 This is only needed for the DWP V1 file format. */
947 unsigned int num_sections = 0;
948 asection **elf_sections = nullptr;
949 };
950
951 /* Struct used to pass misc. parameters to read_die_and_children, et
952 al. which are used for both .debug_info and .debug_types dies.
953 All parameters here are unchanging for the life of the call. This
954 struct exists to abstract away the constant parameters of die reading. */
955
956 struct die_reader_specs
957 {
958 /* The bfd of die_section. */
959 bfd *abfd;
960
961 /* The CU of the DIE we are parsing. */
962 struct dwarf2_cu *cu;
963
964 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
965 struct dwo_file *dwo_file;
966
967 /* The section the die comes from.
968 This is either .debug_info or .debug_types, or the .dwo variants. */
969 struct dwarf2_section_info *die_section;
970
971 /* die_section->buffer. */
972 const gdb_byte *buffer;
973
974 /* The end of the buffer. */
975 const gdb_byte *buffer_end;
976
977 /* The abbreviation table to use when reading the DIEs. */
978 struct abbrev_table *abbrev_table;
979 };
980
981 /* A subclass of die_reader_specs that holds storage and has complex
982 constructor and destructor behavior. */
983
984 class cutu_reader : public die_reader_specs
985 {
986 public:
987
988 cutu_reader (dwarf2_per_cu_data *this_cu,
989 dwarf2_per_objfile *per_objfile,
990 struct abbrev_table *abbrev_table,
991 dwarf2_cu *existing_cu,
992 bool skip_partial);
993
994 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
995 dwarf2_per_objfile *per_objfile,
996 struct dwarf2_cu *parent_cu = nullptr,
997 struct dwo_file *dwo_file = nullptr);
998
999 DISABLE_COPY_AND_ASSIGN (cutu_reader);
1000
1001 const gdb_byte *info_ptr = nullptr;
1002 struct die_info *comp_unit_die = nullptr;
1003 bool dummy_p = false;
1004
1005 /* Release the new CU, putting it on the chain. This cannot be done
1006 for dummy CUs. */
1007 void keep ();
1008
1009 private:
1010 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
1011 dwarf2_per_objfile *per_objfile,
1012 dwarf2_cu *existing_cu);
1013
1014 struct dwarf2_per_cu_data *m_this_cu;
1015 std::unique_ptr<dwarf2_cu> m_new_cu;
1016
1017 /* The ordinary abbreviation table. */
1018 abbrev_table_up m_abbrev_table_holder;
1019
1020 /* The DWO abbreviation table. */
1021 abbrev_table_up m_dwo_abbrev_table;
1022 };
1023
1024 /* When we construct a partial symbol table entry we only
1025 need this much information. */
1026 struct partial_die_info : public allocate_on_obstack
1027 {
1028 partial_die_info (sect_offset sect_off, const struct abbrev_info *abbrev);
1029
1030 /* Disable assign but still keep copy ctor, which is needed
1031 load_partial_dies. */
1032 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1033
1034 /* Adjust the partial die before generating a symbol for it. This
1035 function may set the is_external flag or change the DIE's
1036 name. */
1037 void fixup (struct dwarf2_cu *cu);
1038
1039 /* Read a minimal amount of information into the minimal die
1040 structure. */
1041 const gdb_byte *read (const struct die_reader_specs *reader,
1042 const struct abbrev_info &abbrev,
1043 const gdb_byte *info_ptr);
1044
1045 /* Compute the name of this partial DIE. This memoizes the
1046 result, so it is safe to call multiple times. */
1047 const char *name (dwarf2_cu *cu);
1048
1049 /* Offset of this DIE. */
1050 const sect_offset sect_off;
1051
1052 /* DWARF-2 tag for this DIE. */
1053 const ENUM_BITFIELD(dwarf_tag) tag : 16;
1054
1055 /* Assorted flags describing the data found in this DIE. */
1056 const unsigned int has_children : 1;
1057
1058 unsigned int is_external : 1;
1059 unsigned int is_declaration : 1;
1060 unsigned int has_type : 1;
1061 unsigned int has_specification : 1;
1062 unsigned int has_pc_info : 1;
1063 unsigned int may_be_inlined : 1;
1064
1065 /* This DIE has been marked DW_AT_main_subprogram. */
1066 unsigned int main_subprogram : 1;
1067
1068 /* Flag set if the SCOPE field of this structure has been
1069 computed. */
1070 unsigned int scope_set : 1;
1071
1072 /* Flag set if the DIE has a byte_size attribute. */
1073 unsigned int has_byte_size : 1;
1074
1075 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1076 unsigned int has_const_value : 1;
1077
1078 /* Flag set if any of the DIE's children are template arguments. */
1079 unsigned int has_template_arguments : 1;
1080
1081 /* Flag set if fixup has been called on this die. */
1082 unsigned int fixup_called : 1;
1083
1084 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1085 unsigned int is_dwz : 1;
1086
1087 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1088 unsigned int spec_is_dwz : 1;
1089
1090 unsigned int canonical_name : 1;
1091
1092 /* The name of this DIE. Normally the value of DW_AT_name, but
1093 sometimes a default name for unnamed DIEs. */
1094 const char *raw_name = nullptr;
1095
1096 /* The linkage name, if present. */
1097 const char *linkage_name = nullptr;
1098
1099 /* The scope to prepend to our children. This is generally
1100 allocated on the comp_unit_obstack, so will disappear
1101 when this compilation unit leaves the cache. */
1102 const char *scope = nullptr;
1103
1104 /* Some data associated with the partial DIE. The tag determines
1105 which field is live. */
1106 union
1107 {
1108 /* The location description associated with this DIE, if any. */
1109 struct dwarf_block *locdesc;
1110 /* The offset of an import, for DW_TAG_imported_unit. */
1111 sect_offset sect_off;
1112 } d {};
1113
1114 /* If HAS_PC_INFO, the PC range associated with this DIE. */
1115 CORE_ADDR lowpc = 0;
1116 CORE_ADDR highpc = 0;
1117
1118 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
1119 DW_AT_sibling, if any. */
1120 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1121 could return DW_AT_sibling values to its caller load_partial_dies. */
1122 const gdb_byte *sibling = nullptr;
1123
1124 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1125 DW_AT_specification (or DW_AT_abstract_origin or
1126 DW_AT_extension). */
1127 sect_offset spec_offset {};
1128
1129 /* Pointers to this DIE's parent, first child, and next sibling,
1130 if any. */
1131 struct partial_die_info *die_parent = nullptr;
1132 struct partial_die_info *die_child = nullptr;
1133 struct partial_die_info *die_sibling = nullptr;
1134
1135 friend struct partial_die_info *
1136 dwarf2_cu::find_partial_die (sect_offset sect_off);
1137
1138 private:
1139 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1140 partial_die_info (sect_offset sect_off)
1141 : partial_die_info (sect_off, DW_TAG_padding, 0)
1142 {
1143 }
1144
1145 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1146 int has_children_)
1147 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1148 {
1149 is_external = 0;
1150 is_declaration = 0;
1151 has_type = 0;
1152 has_specification = 0;
1153 has_pc_info = 0;
1154 may_be_inlined = 0;
1155 main_subprogram = 0;
1156 scope_set = 0;
1157 has_byte_size = 0;
1158 has_const_value = 0;
1159 has_template_arguments = 0;
1160 fixup_called = 0;
1161 is_dwz = 0;
1162 spec_is_dwz = 0;
1163 canonical_name = 0;
1164 }
1165 };
1166
1167 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1168 but this would require a corresponding change in unpack_field_as_long
1169 and friends. */
1170 static int bits_per_byte = 8;
1171
1172 struct variant_part_builder;
1173
1174 /* When reading a variant, we track a bit more information about the
1175 field, and store it in an object of this type. */
1176
1177 struct variant_field
1178 {
1179 int first_field = -1;
1180 int last_field = -1;
1181
1182 /* A variant can contain other variant parts. */
1183 std::vector<variant_part_builder> variant_parts;
1184
1185 /* If we see a DW_TAG_variant, then this will be set if this is the
1186 default branch. */
1187 bool default_branch = false;
1188 /* If we see a DW_AT_discr_value, then this will be the discriminant
1189 value. */
1190 ULONGEST discriminant_value = 0;
1191 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1192 data. */
1193 struct dwarf_block *discr_list_data = nullptr;
1194 };
1195
1196 /* This represents a DW_TAG_variant_part. */
1197
1198 struct variant_part_builder
1199 {
1200 /* The offset of the discriminant field. */
1201 sect_offset discriminant_offset {};
1202
1203 /* Variants that are direct children of this variant part. */
1204 std::vector<variant_field> variants;
1205
1206 /* True if we're currently reading a variant. */
1207 bool processing_variant = false;
1208 };
1209
1210 struct nextfield
1211 {
1212 int accessibility = 0;
1213 int virtuality = 0;
1214 /* Variant parts need to find the discriminant, which is a DIE
1215 reference. We track the section offset of each field to make
1216 this link. */
1217 sect_offset offset;
1218 struct field field {};
1219 };
1220
1221 struct fnfieldlist
1222 {
1223 const char *name = nullptr;
1224 std::vector<struct fn_field> fnfields;
1225 };
1226
1227 /* The routines that read and process dies for a C struct or C++ class
1228 pass lists of data member fields and lists of member function fields
1229 in an instance of a field_info structure, as defined below. */
1230 struct field_info
1231 {
1232 /* List of data member and baseclasses fields. */
1233 std::vector<struct nextfield> fields;
1234 std::vector<struct nextfield> baseclasses;
1235
1236 /* Set if the accessibility of one of the fields is not public. */
1237 bool non_public_fields = false;
1238
1239 /* Member function fieldlist array, contains name of possibly overloaded
1240 member function, number of overloaded member functions and a pointer
1241 to the head of the member function field chain. */
1242 std::vector<struct fnfieldlist> fnfieldlists;
1243
1244 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1245 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1246 std::vector<struct decl_field> typedef_field_list;
1247
1248 /* Nested types defined by this class and the number of elements in this
1249 list. */
1250 std::vector<struct decl_field> nested_types_list;
1251
1252 /* If non-null, this is the variant part we are currently
1253 reading. */
1254 variant_part_builder *current_variant_part = nullptr;
1255 /* This holds all the top-level variant parts attached to the type
1256 we're reading. */
1257 std::vector<variant_part_builder> variant_parts;
1258
1259 /* Return the total number of fields (including baseclasses). */
1260 int nfields () const
1261 {
1262 return fields.size () + baseclasses.size ();
1263 }
1264 };
1265
1266 /* Loaded secondary compilation units are kept in memory until they
1267 have not been referenced for the processing of this many
1268 compilation units. Set this to zero to disable caching. Cache
1269 sizes of up to at least twenty will improve startup time for
1270 typical inter-CU-reference binaries, at an obvious memory cost. */
1271 static int dwarf_max_cache_age = 5;
1272 static void
1273 show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1274 struct cmd_list_element *c, const char *value)
1275 {
1276 fprintf_filtered (file, _("The upper bound on the age of cached "
1277 "DWARF compilation units is %s.\n"),
1278 value);
1279 }
1280 \f
1281 /* local function prototypes */
1282
1283 static void dwarf2_find_base_address (struct die_info *die,
1284 struct dwarf2_cu *cu);
1285
1286 static dwarf2_psymtab *create_partial_symtab
1287 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1288 const char *name);
1289
1290 static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1291 const gdb_byte *info_ptr,
1292 struct die_info *type_unit_die);
1293
1294 static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
1295
1296 static void scan_partial_symbols (struct partial_die_info *,
1297 CORE_ADDR *, CORE_ADDR *,
1298 int, struct dwarf2_cu *);
1299
1300 static void add_partial_symbol (struct partial_die_info *,
1301 struct dwarf2_cu *);
1302
1303 static void add_partial_namespace (struct partial_die_info *pdi,
1304 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1305 int set_addrmap, struct dwarf2_cu *cu);
1306
1307 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
1308 CORE_ADDR *highpc, int set_addrmap,
1309 struct dwarf2_cu *cu);
1310
1311 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1312 struct dwarf2_cu *cu);
1313
1314 static void add_partial_subprogram (struct partial_die_info *pdi,
1315 CORE_ADDR *lowpc, CORE_ADDR *highpc,
1316 int need_pc, struct dwarf2_cu *cu);
1317
1318 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
1319
1320 static struct partial_die_info *load_partial_dies
1321 (const struct die_reader_specs *, const gdb_byte *, int);
1322
1323 /* A pair of partial_die_info and compilation unit. */
1324 struct cu_partial_die_info
1325 {
1326 /* The compilation unit of the partial_die_info. */
1327 struct dwarf2_cu *cu;
1328 /* A partial_die_info. */
1329 struct partial_die_info *pdi;
1330
1331 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1332 : cu (cu),
1333 pdi (pdi)
1334 { /* Nothing. */ }
1335
1336 private:
1337 cu_partial_die_info () = delete;
1338 };
1339
1340 static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1341 struct dwarf2_cu *);
1342
1343 static const gdb_byte *read_attribute (const struct die_reader_specs *,
1344 struct attribute *,
1345 const struct attr_abbrev *,
1346 const gdb_byte *);
1347
1348 static void read_attribute_reprocess (const struct die_reader_specs *reader,
1349 struct attribute *attr, dwarf_tag tag);
1350
1351 static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
1352
1353 static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1354 dwarf2_section_info *, sect_offset);
1355
1356 static const char *read_indirect_string
1357 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
1358 const struct comp_unit_head *, unsigned int *);
1359
1360 static const char *read_indirect_string_at_offset
1361 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
1362
1363 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1364 const gdb_byte *,
1365 unsigned int *);
1366
1367 static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1368 ULONGEST str_index);
1369
1370 static const char *read_stub_str_index (struct dwarf2_cu *cu,
1371 ULONGEST str_index);
1372
1373 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1374
1375 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1376 struct dwarf2_cu *);
1377
1378 static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1379 struct dwarf2_cu *cu);
1380
1381 static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1382
1383 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1384 struct dwarf2_cu *cu);
1385
1386 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1387
1388 static struct die_info *die_specification (struct die_info *die,
1389 struct dwarf2_cu **);
1390
1391 static line_header_up dwarf_decode_line_header (sect_offset sect_off,
1392 struct dwarf2_cu *cu);
1393
1394 static void dwarf_decode_lines (struct line_header *, const char *,
1395 struct dwarf2_cu *, dwarf2_psymtab *,
1396 CORE_ADDR, int decode_mapping);
1397
1398 static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1399 const char *);
1400
1401 static struct symbol *new_symbol (struct die_info *, struct type *,
1402 struct dwarf2_cu *, struct symbol * = NULL);
1403
1404 static void dwarf2_const_value (const struct attribute *, struct symbol *,
1405 struct dwarf2_cu *);
1406
1407 static void dwarf2_const_value_attr (const struct attribute *attr,
1408 struct type *type,
1409 const char *name,
1410 struct obstack *obstack,
1411 struct dwarf2_cu *cu, LONGEST *value,
1412 const gdb_byte **bytes,
1413 struct dwarf2_locexpr_baton **baton);
1414
1415 static struct type *read_subrange_index_type (struct die_info *die,
1416 struct dwarf2_cu *cu);
1417
1418 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1419
1420 static int need_gnat_info (struct dwarf2_cu *);
1421
1422 static struct type *die_descriptive_type (struct die_info *,
1423 struct dwarf2_cu *);
1424
1425 static void set_descriptive_type (struct type *, struct die_info *,
1426 struct dwarf2_cu *);
1427
1428 static struct type *die_containing_type (struct die_info *,
1429 struct dwarf2_cu *);
1430
1431 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
1432 struct dwarf2_cu *);
1433
1434 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1435
1436 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1437
1438 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1439
1440 static char *typename_concat (struct obstack *obs, const char *prefix,
1441 const char *suffix, int physname,
1442 struct dwarf2_cu *cu);
1443
1444 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1445
1446 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1447
1448 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1449
1450 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1451
1452 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1453
1454 static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1455
1456 /* Return the .debug_loclists section to use for cu. */
1457 static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1458
1459 /* Return the .debug_rnglists section to use for cu. */
1460 static struct dwarf2_section_info *cu_debug_rnglists_section
1461 (struct dwarf2_cu *cu, dwarf_tag tag);
1462
1463 /* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
1464 values. Keep the items ordered with increasing constraints compliance. */
1465 enum pc_bounds_kind
1466 {
1467 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
1468 PC_BOUNDS_NOT_PRESENT,
1469
1470 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1471 were present but they do not form a valid range of PC addresses. */
1472 PC_BOUNDS_INVALID,
1473
1474 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1475 PC_BOUNDS_RANGES,
1476
1477 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1478 PC_BOUNDS_HIGH_LOW,
1479 };
1480
1481 static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1482 CORE_ADDR *, CORE_ADDR *,
1483 struct dwarf2_cu *,
1484 dwarf2_psymtab *);
1485
1486 static void get_scope_pc_bounds (struct die_info *,
1487 CORE_ADDR *, CORE_ADDR *,
1488 struct dwarf2_cu *);
1489
1490 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1491 CORE_ADDR, struct dwarf2_cu *);
1492
1493 static void dwarf2_add_field (struct field_info *, struct die_info *,
1494 struct dwarf2_cu *);
1495
1496 static void dwarf2_attach_fields_to_type (struct field_info *,
1497 struct type *, struct dwarf2_cu *);
1498
1499 static void dwarf2_add_member_fn (struct field_info *,
1500 struct die_info *, struct type *,
1501 struct dwarf2_cu *);
1502
1503 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1504 struct type *,
1505 struct dwarf2_cu *);
1506
1507 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1508
1509 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1510
1511 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1512
1513 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1514
1515 static struct using_direct **using_directives (struct dwarf2_cu *cu);
1516
1517 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1518
1519 static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1520
1521 static struct type *read_module_type (struct die_info *die,
1522 struct dwarf2_cu *cu);
1523
1524 static const char *namespace_name (struct die_info *die,
1525 int *is_anonymous, struct dwarf2_cu *);
1526
1527 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1528
1529 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1530 bool * = nullptr);
1531
1532 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1533 struct dwarf2_cu *);
1534
1535 static struct die_info *read_die_and_siblings_1
1536 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
1537 struct die_info *);
1538
1539 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
1540 const gdb_byte *info_ptr,
1541 const gdb_byte **new_info_ptr,
1542 struct die_info *parent);
1543
1544 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1545 struct die_info **, const gdb_byte *,
1546 int);
1547
1548 static const gdb_byte *read_full_die (const struct die_reader_specs *,
1549 struct die_info **, const gdb_byte *);
1550
1551 static void process_die (struct die_info *, struct dwarf2_cu *);
1552
1553 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1554 struct objfile *);
1555
1556 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1557
1558 static const char *dwarf2_full_name (const char *name,
1559 struct die_info *die,
1560 struct dwarf2_cu *cu);
1561
1562 static const char *dwarf2_physname (const char *name, struct die_info *die,
1563 struct dwarf2_cu *cu);
1564
1565 static struct die_info *dwarf2_extension (struct die_info *die,
1566 struct dwarf2_cu **);
1567
1568 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1569
1570 static void dump_die_for_error (struct die_info *);
1571
1572 static void dump_die_1 (struct ui_file *, int level, int max_level,
1573 struct die_info *);
1574
1575 /*static*/ void dump_die (struct die_info *, int max_level);
1576
1577 static void store_in_ref_table (struct die_info *,
1578 struct dwarf2_cu *);
1579
1580 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1581 const struct attribute *,
1582 struct dwarf2_cu **);
1583
1584 static struct die_info *follow_die_ref (struct die_info *,
1585 const struct attribute *,
1586 struct dwarf2_cu **);
1587
1588 static struct die_info *follow_die_sig (struct die_info *,
1589 const struct attribute *,
1590 struct dwarf2_cu **);
1591
1592 static struct type *get_signatured_type (struct die_info *, ULONGEST,
1593 struct dwarf2_cu *);
1594
1595 static struct type *get_DW_AT_signature_type (struct die_info *,
1596 const struct attribute *,
1597 struct dwarf2_cu *);
1598
1599 static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1600 dwarf2_per_objfile *per_objfile);
1601
1602 static void read_signatured_type (signatured_type *sig_type,
1603 dwarf2_per_objfile *per_objfile);
1604
1605 static int attr_to_dynamic_prop (const struct attribute *attr,
1606 struct die_info *die, struct dwarf2_cu *cu,
1607 struct dynamic_prop *prop, struct type *type);
1608
1609 /* memory allocation interface */
1610
1611 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1612
1613 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1614
1615 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
1616
1617 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1618 struct dwarf2_loclist_baton *baton,
1619 const struct attribute *attr);
1620
1621 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
1622 struct symbol *sym,
1623 struct dwarf2_cu *cu,
1624 int is_block);
1625
1626 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1627 const gdb_byte *info_ptr,
1628 const struct abbrev_info *abbrev);
1629
1630 static hashval_t partial_die_hash (const void *item);
1631
1632 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1633
1634 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1635 (sect_offset sect_off, unsigned int offset_in_dwz,
1636 dwarf2_per_objfile *per_objfile);
1637
1638 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1639 struct die_info *comp_unit_die,
1640 enum language pretend_language);
1641
1642 static struct type *set_die_type (struct die_info *, struct type *,
1643 struct dwarf2_cu *, bool = false);
1644
1645 static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
1646
1647 static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1648
1649 static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1650 dwarf2_per_objfile *per_objfile,
1651 dwarf2_cu *existing_cu,
1652 bool skip_partial,
1653 enum language pretend_language);
1654
1655 static void process_full_comp_unit (dwarf2_cu *cu,
1656 enum language pretend_language);
1657
1658 static void process_full_type_unit (dwarf2_cu *cu,
1659 enum language pretend_language);
1660
1661 static void dwarf2_add_dependence (struct dwarf2_cu *,
1662 struct dwarf2_per_cu_data *);
1663
1664 static void dwarf2_mark (struct dwarf2_cu *);
1665
1666 static struct type *get_die_type_at_offset (sect_offset,
1667 dwarf2_per_cu_data *per_cu,
1668 dwarf2_per_objfile *per_objfile);
1669
1670 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1671
1672 static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1673 dwarf2_per_objfile *per_objfile,
1674 enum language pretend_language);
1675
1676 static void process_queue (dwarf2_per_objfile *per_objfile);
1677
1678 /* Class, the destructor of which frees all allocated queue entries. This
1679 will only have work to do if an error was thrown while processing the
1680 dwarf. If no error was thrown then the queue entries should have all
1681 been processed, and freed, as we went along. */
1682
1683 class dwarf2_queue_guard
1684 {
1685 public:
1686 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1687 : m_per_objfile (per_objfile)
1688 {
1689 gdb_assert (!m_per_objfile->per_bfd->queue.has_value ());
1690
1691 m_per_objfile->per_bfd->queue.emplace ();
1692 }
1693
1694 /* Free any entries remaining on the queue. There should only be
1695 entries left if we hit an error while processing the dwarf. */
1696 ~dwarf2_queue_guard ()
1697 {
1698 gdb_assert (m_per_objfile->per_bfd->queue.has_value ());
1699
1700 m_per_objfile->per_bfd->queue.reset ();
1701 }
1702
1703 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
1704
1705 private:
1706 dwarf2_per_objfile *m_per_objfile;
1707 };
1708
1709 dwarf2_queue_item::~dwarf2_queue_item ()
1710 {
1711 /* Anything still marked queued is likely to be in an
1712 inconsistent state, so discard it. */
1713 if (per_cu->queued)
1714 {
1715 per_objfile->remove_cu (per_cu);
1716 per_cu->queued = 0;
1717 }
1718 }
1719
1720 /* The return type of find_file_and_directory. Note, the enclosed
1721 string pointers are only valid while this object is valid. */
1722
1723 struct file_and_directory
1724 {
1725 /* The filename. This is never NULL. */
1726 const char *name;
1727
1728 /* The compilation directory. NULL if not known. If we needed to
1729 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1730 points directly to the DW_AT_comp_dir string attribute owned by
1731 the obstack that owns the DIE. */
1732 const char *comp_dir;
1733
1734 /* If we needed to build a new string for comp_dir, this is what
1735 owns the storage. */
1736 std::string comp_dir_storage;
1737 };
1738
1739 static file_and_directory find_file_and_directory (struct die_info *die,
1740 struct dwarf2_cu *cu);
1741
1742 static htab_up allocate_signatured_type_table ();
1743
1744 static htab_up allocate_dwo_unit_table ();
1745
1746 static struct dwo_unit *lookup_dwo_unit_in_dwp
1747 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1748 const char *comp_dir, ULONGEST signature, int is_debug_types);
1749
1750 static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
1751
1752 static struct dwo_unit *lookup_dwo_comp_unit
1753 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1754 ULONGEST signature);
1755
1756 static struct dwo_unit *lookup_dwo_type_unit
1757 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
1758
1759 static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
1760
1761 /* A unique pointer to a dwo_file. */
1762
1763 typedef std::unique_ptr<struct dwo_file> dwo_file_up;
1764
1765 static void process_cu_includes (dwarf2_per_objfile *per_objfile);
1766
1767 static void check_producer (struct dwarf2_cu *cu);
1768
1769 static void free_line_header_voidp (void *arg);
1770 \f
1771 /* Various complaints about symbol reading that don't abort the process. */
1772
1773 static void
1774 dwarf2_debug_line_missing_file_complaint (void)
1775 {
1776 complaint (_(".debug_line section has line data without a file"));
1777 }
1778
1779 static void
1780 dwarf2_debug_line_missing_end_sequence_complaint (void)
1781 {
1782 complaint (_(".debug_line section has line "
1783 "program sequence without an end"));
1784 }
1785
1786 static void
1787 dwarf2_complex_location_expr_complaint (void)
1788 {
1789 complaint (_("location expression too complex"));
1790 }
1791
1792 static void
1793 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1794 int arg3)
1795 {
1796 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
1797 arg1, arg2, arg3);
1798 }
1799
1800 static void
1801 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1802 {
1803 complaint (_("invalid attribute class or form for '%s' in '%s'"),
1804 arg1, arg2);
1805 }
1806
1807 /* Hash function for line_header_hash. */
1808
1809 static hashval_t
1810 line_header_hash (const struct line_header *ofs)
1811 {
1812 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
1813 }
1814
1815 /* Hash function for htab_create_alloc_ex for line_header_hash. */
1816
1817 static hashval_t
1818 line_header_hash_voidp (const void *item)
1819 {
1820 const struct line_header *ofs = (const struct line_header *) item;
1821
1822 return line_header_hash (ofs);
1823 }
1824
1825 /* Equality function for line_header_hash. */
1826
1827 static int
1828 line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1829 {
1830 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1831 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
1832
1833 return (ofs_lhs->sect_off == ofs_rhs->sect_off
1834 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1835 }
1836
1837 \f
1838
1839 /* See declaration. */
1840
1841 dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1842 bool can_copy_)
1843 : obfd (obfd),
1844 can_copy (can_copy_)
1845 {
1846 if (names == NULL)
1847 names = &dwarf2_elf_names;
1848
1849 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1850 locate_sections (obfd, sec, *names);
1851 }
1852
1853 dwarf2_per_bfd::~dwarf2_per_bfd ()
1854 {
1855 for (dwarf2_per_cu_data *per_cu : all_comp_units)
1856 per_cu->imported_symtabs_free ();
1857
1858 for (signatured_type *sig_type : all_type_units)
1859 sig_type->per_cu.imported_symtabs_free ();
1860
1861 /* Everything else should be on this->obstack. */
1862 }
1863
1864 /* See read.h. */
1865
1866 void
1867 dwarf2_per_objfile::remove_all_cus ()
1868 {
1869 gdb_assert (!this->per_bfd->queue.has_value ());
1870
1871 for (auto pair : m_dwarf2_cus)
1872 delete pair.second;
1873
1874 m_dwarf2_cus.clear ();
1875 }
1876
1877 /* A helper class that calls free_cached_comp_units on
1878 destruction. */
1879
1880 class free_cached_comp_units
1881 {
1882 public:
1883
1884 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1885 : m_per_objfile (per_objfile)
1886 {
1887 }
1888
1889 ~free_cached_comp_units ()
1890 {
1891 m_per_objfile->remove_all_cus ();
1892 }
1893
1894 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1895
1896 private:
1897
1898 dwarf2_per_objfile *m_per_objfile;
1899 };
1900
1901 /* See read.h. */
1902
1903 bool
1904 dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1905 {
1906 gdb_assert (per_cu->index < this->m_symtabs.size ());
1907
1908 return this->m_symtabs[per_cu->index] != nullptr;
1909 }
1910
1911 /* See read.h. */
1912
1913 compunit_symtab *
1914 dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1915 {
1916 gdb_assert (per_cu->index < this->m_symtabs.size ());
1917
1918 return this->m_symtabs[per_cu->index];
1919 }
1920
1921 /* See read.h. */
1922
1923 void
1924 dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1925 compunit_symtab *symtab)
1926 {
1927 gdb_assert (per_cu->index < this->m_symtabs.size ());
1928 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1929
1930 this->m_symtabs[per_cu->index] = symtab;
1931 }
1932
1933 /* Try to locate the sections we need for DWARF 2 debugging
1934 information and return true if we have enough to do something.
1935 NAMES points to the dwarf2 section names, or is NULL if the standard
1936 ELF names are used. CAN_COPY is true for formats where symbol
1937 interposition is possible and so symbol values must follow copy
1938 relocation rules. */
1939
1940 int
1941 dwarf2_has_info (struct objfile *objfile,
1942 const struct dwarf2_debug_sections *names,
1943 bool can_copy)
1944 {
1945 if (objfile->flags & OBJF_READNEVER)
1946 return 0;
1947
1948 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1949
1950 if (per_objfile == NULL)
1951 {
1952 dwarf2_per_bfd *per_bfd;
1953
1954 /* We can share a "dwarf2_per_bfd" with other objfiles if the
1955 BFD doesn't require relocations.
1956
1957 We don't share with objfiles for which -readnow was requested,
1958 because it would complicate things when loading the same BFD with
1959 -readnow and then without -readnow. */
1960 if (!gdb_bfd_requires_relocations (objfile->obfd)
1961 && (objfile->flags & OBJF_READNOW) == 0)
1962 {
1963 /* See if one has been created for this BFD yet. */
1964 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1965
1966 if (per_bfd == nullptr)
1967 {
1968 /* No, create it now. */
1969 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1970 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1971 }
1972 }
1973 else
1974 {
1975 /* No sharing possible, create one specifically for this objfile. */
1976 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1977 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1978 }
1979
1980 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1981 }
1982
1983 return (!per_objfile->per_bfd->info.is_virtual
1984 && per_objfile->per_bfd->info.s.section != NULL
1985 && !per_objfile->per_bfd->abbrev.is_virtual
1986 && per_objfile->per_bfd->abbrev.s.section != NULL);
1987 }
1988
1989 /* See declaration. */
1990
1991 void
1992 dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1993 const dwarf2_debug_sections &names)
1994 {
1995 flagword aflag = bfd_section_flags (sectp);
1996
1997 if ((aflag & SEC_HAS_CONTENTS) == 0)
1998 {
1999 }
2000 else if (elf_section_data (sectp)->this_hdr.sh_size
2001 > bfd_get_file_size (abfd))
2002 {
2003 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2004 warning (_("Discarding section %s which has a section size (%s"
2005 ") larger than the file size [in module %s]"),
2006 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2007 bfd_get_filename (abfd));
2008 }
2009 else if (names.info.matches (sectp->name))
2010 {
2011 this->info.s.section = sectp;
2012 this->info.size = bfd_section_size (sectp);
2013 }
2014 else if (names.abbrev.matches (sectp->name))
2015 {
2016 this->abbrev.s.section = sectp;
2017 this->abbrev.size = bfd_section_size (sectp);
2018 }
2019 else if (names.line.matches (sectp->name))
2020 {
2021 this->line.s.section = sectp;
2022 this->line.size = bfd_section_size (sectp);
2023 }
2024 else if (names.loc.matches (sectp->name))
2025 {
2026 this->loc.s.section = sectp;
2027 this->loc.size = bfd_section_size (sectp);
2028 }
2029 else if (names.loclists.matches (sectp->name))
2030 {
2031 this->loclists.s.section = sectp;
2032 this->loclists.size = bfd_section_size (sectp);
2033 }
2034 else if (names.macinfo.matches (sectp->name))
2035 {
2036 this->macinfo.s.section = sectp;
2037 this->macinfo.size = bfd_section_size (sectp);
2038 }
2039 else if (names.macro.matches (sectp->name))
2040 {
2041 this->macro.s.section = sectp;
2042 this->macro.size = bfd_section_size (sectp);
2043 }
2044 else if (names.str.matches (sectp->name))
2045 {
2046 this->str.s.section = sectp;
2047 this->str.size = bfd_section_size (sectp);
2048 }
2049 else if (names.str_offsets.matches (sectp->name))
2050 {
2051 this->str_offsets.s.section = sectp;
2052 this->str_offsets.size = bfd_section_size (sectp);
2053 }
2054 else if (names.line_str.matches (sectp->name))
2055 {
2056 this->line_str.s.section = sectp;
2057 this->line_str.size = bfd_section_size (sectp);
2058 }
2059 else if (names.addr.matches (sectp->name))
2060 {
2061 this->addr.s.section = sectp;
2062 this->addr.size = bfd_section_size (sectp);
2063 }
2064 else if (names.frame.matches (sectp->name))
2065 {
2066 this->frame.s.section = sectp;
2067 this->frame.size = bfd_section_size (sectp);
2068 }
2069 else if (names.eh_frame.matches (sectp->name))
2070 {
2071 this->eh_frame.s.section = sectp;
2072 this->eh_frame.size = bfd_section_size (sectp);
2073 }
2074 else if (names.ranges.matches (sectp->name))
2075 {
2076 this->ranges.s.section = sectp;
2077 this->ranges.size = bfd_section_size (sectp);
2078 }
2079 else if (names.rnglists.matches (sectp->name))
2080 {
2081 this->rnglists.s.section = sectp;
2082 this->rnglists.size = bfd_section_size (sectp);
2083 }
2084 else if (names.types.matches (sectp->name))
2085 {
2086 struct dwarf2_section_info type_section;
2087
2088 memset (&type_section, 0, sizeof (type_section));
2089 type_section.s.section = sectp;
2090 type_section.size = bfd_section_size (sectp);
2091
2092 this->types.push_back (type_section);
2093 }
2094 else if (names.gdb_index.matches (sectp->name))
2095 {
2096 this->gdb_index.s.section = sectp;
2097 this->gdb_index.size = bfd_section_size (sectp);
2098 }
2099 else if (names.debug_names.matches (sectp->name))
2100 {
2101 this->debug_names.s.section = sectp;
2102 this->debug_names.size = bfd_section_size (sectp);
2103 }
2104 else if (names.debug_aranges.matches (sectp->name))
2105 {
2106 this->debug_aranges.s.section = sectp;
2107 this->debug_aranges.size = bfd_section_size (sectp);
2108 }
2109
2110 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2111 && bfd_section_vma (sectp) == 0)
2112 this->has_section_at_zero = true;
2113 }
2114
2115 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
2116 SECTION_NAME. */
2117
2118 void
2119 dwarf2_get_section_info (struct objfile *objfile,
2120 enum dwarf2_section_enum sect,
2121 asection **sectp, const gdb_byte **bufp,
2122 bfd_size_type *sizep)
2123 {
2124 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
2125 struct dwarf2_section_info *info;
2126
2127 /* We may see an objfile without any DWARF, in which case we just
2128 return nothing. */
2129 if (per_objfile == NULL)
2130 {
2131 *sectp = NULL;
2132 *bufp = NULL;
2133 *sizep = 0;
2134 return;
2135 }
2136 switch (sect)
2137 {
2138 case DWARF2_DEBUG_FRAME:
2139 info = &per_objfile->per_bfd->frame;
2140 break;
2141 case DWARF2_EH_FRAME:
2142 info = &per_objfile->per_bfd->eh_frame;
2143 break;
2144 default:
2145 gdb_assert_not_reached ("unexpected section");
2146 }
2147
2148 info->read (objfile);
2149
2150 *sectp = info->get_bfd_section ();
2151 *bufp = info->buffer;
2152 *sizep = info->size;
2153 }
2154
2155 \f
2156 /* DWARF quick_symbol_functions support. */
2157
2158 /* TUs can share .debug_line entries, and there can be a lot more TUs than
2159 unique line tables, so we maintain a separate table of all .debug_line
2160 derived entries to support the sharing.
2161 All the quick functions need is the list of file names. We discard the
2162 line_header when we're done and don't need to record it here. */
2163 struct quick_file_names
2164 {
2165 /* The data used to construct the hash key. */
2166 struct stmt_list_hash hash;
2167
2168 /* The number of entries in file_names, real_names. */
2169 unsigned int num_file_names;
2170
2171 /* The file names from the line table, after being run through
2172 file_full_name. */
2173 const char **file_names;
2174
2175 /* The file names from the line table after being run through
2176 gdb_realpath. These are computed lazily. */
2177 const char **real_names;
2178 };
2179
2180 /* When using the index (and thus not using psymtabs), each CU has an
2181 object of this type. This is used to hold information needed by
2182 the various "quick" methods. */
2183 struct dwarf2_per_cu_quick_data
2184 {
2185 /* The file table. This can be NULL if there was no file table
2186 or it's currently not read in.
2187 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
2188 struct quick_file_names *file_names;
2189
2190 /* A temporary mark bit used when iterating over all CUs in
2191 expand_symtabs_matching. */
2192 unsigned int mark : 1;
2193
2194 /* True if we've tried to read the file table and found there isn't one.
2195 There will be no point in trying to read it again next time. */
2196 unsigned int no_file_data : 1;
2197 };
2198
2199 /* A subclass of psymbol_functions that arranges to read the DWARF
2200 partial symbols when needed. */
2201 struct lazy_dwarf_reader : public psymbol_functions
2202 {
2203 using psymbol_functions::psymbol_functions;
2204
2205 bool can_lazily_read_symbols () override
2206 {
2207 return true;
2208 }
2209
2210 void read_partial_symbols (struct objfile *objfile) override
2211 {
2212 if (dwarf2_has_info (objfile, nullptr))
2213 dwarf2_build_psymtabs (objfile, this);
2214 }
2215 };
2216
2217 static quick_symbol_functions_up
2218 make_lazy_dwarf_reader ()
2219 {
2220 return quick_symbol_functions_up (new lazy_dwarf_reader);
2221 }
2222
2223 struct dwarf2_base_index_functions : public quick_symbol_functions
2224 {
2225 bool has_symbols (struct objfile *objfile) override;
2226
2227 struct symtab *find_last_source_symtab (struct objfile *objfile) override;
2228
2229 void forget_cached_source_info (struct objfile *objfile) override;
2230
2231 enum language lookup_global_symbol_language (struct objfile *objfile,
2232 const char *name,
2233 domain_enum domain,
2234 bool *symbol_found_p) override
2235 {
2236 *symbol_found_p = false;
2237 return language_unknown;
2238 }
2239
2240 void print_stats (struct objfile *objfile, bool print_bcache) override;
2241
2242 void expand_all_symtabs (struct objfile *objfile) override;
2243
2244 struct compunit_symtab *find_pc_sect_compunit_symtab
2245 (struct objfile *objfile, struct bound_minimal_symbol msymbol,
2246 CORE_ADDR pc, struct obj_section *section, int warn_if_readin) override;
2247
2248 struct compunit_symtab *find_compunit_symtab_by_address
2249 (struct objfile *objfile, CORE_ADDR address) override
2250 {
2251 return nullptr;
2252 }
2253
2254 void map_symbol_filenames (struct objfile *objfile,
2255 gdb::function_view<symbol_filename_ftype> fun,
2256 bool need_fullname) override;
2257 };
2258
2259 struct dwarf2_gdb_index : public dwarf2_base_index_functions
2260 {
2261 void dump (struct objfile *objfile) override;
2262
2263 void map_matching_symbols
2264 (struct objfile *,
2265 const lookup_name_info &lookup_name,
2266 domain_enum domain,
2267 int global,
2268 gdb::function_view<symbol_found_callback_ftype> callback,
2269 symbol_compare_ftype *ordered_compare) override;
2270
2271 bool expand_symtabs_matching
2272 (struct objfile *objfile,
2273 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2274 const lookup_name_info *lookup_name,
2275 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2276 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2277 block_search_flags search_flags,
2278 domain_enum domain,
2279 enum search_domain kind) override;
2280 };
2281
2282 struct dwarf2_debug_names_index : public dwarf2_base_index_functions
2283 {
2284 void dump (struct objfile *objfile) override;
2285
2286 void map_matching_symbols
2287 (struct objfile *,
2288 const lookup_name_info &lookup_name,
2289 domain_enum domain,
2290 int global,
2291 gdb::function_view<symbol_found_callback_ftype> callback,
2292 symbol_compare_ftype *ordered_compare) override;
2293
2294 bool expand_symtabs_matching
2295 (struct objfile *objfile,
2296 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
2297 const lookup_name_info *lookup_name,
2298 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
2299 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
2300 block_search_flags search_flags,
2301 domain_enum domain,
2302 enum search_domain kind) override;
2303 };
2304
2305 static quick_symbol_functions_up
2306 make_dwarf_gdb_index ()
2307 {
2308 return quick_symbol_functions_up (new dwarf2_gdb_index);
2309 }
2310
2311 static quick_symbol_functions_up
2312 make_dwarf_debug_names ()
2313 {
2314 return quick_symbol_functions_up (new dwarf2_debug_names_index);
2315 }
2316
2317 /* Utility hash function for a stmt_list_hash. */
2318
2319 static hashval_t
2320 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2321 {
2322 hashval_t v = 0;
2323
2324 if (stmt_list_hash->dwo_unit != NULL)
2325 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
2326 v += to_underlying (stmt_list_hash->line_sect_off);
2327 return v;
2328 }
2329
2330 /* Utility equality function for a stmt_list_hash. */
2331
2332 static int
2333 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2334 const struct stmt_list_hash *rhs)
2335 {
2336 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2337 return 0;
2338 if (lhs->dwo_unit != NULL
2339 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2340 return 0;
2341
2342 return lhs->line_sect_off == rhs->line_sect_off;
2343 }
2344
2345 /* Hash function for a quick_file_names. */
2346
2347 static hashval_t
2348 hash_file_name_entry (const void *e)
2349 {
2350 const struct quick_file_names *file_data
2351 = (const struct quick_file_names *) e;
2352
2353 return hash_stmt_list_entry (&file_data->hash);
2354 }
2355
2356 /* Equality function for a quick_file_names. */
2357
2358 static int
2359 eq_file_name_entry (const void *a, const void *b)
2360 {
2361 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2362 const struct quick_file_names *eb = (const struct quick_file_names *) b;
2363
2364 return eq_stmt_list_entry (&ea->hash, &eb->hash);
2365 }
2366
2367 /* Delete function for a quick_file_names. */
2368
2369 static void
2370 delete_file_name_entry (void *e)
2371 {
2372 struct quick_file_names *file_data = (struct quick_file_names *) e;
2373 int i;
2374
2375 for (i = 0; i < file_data->num_file_names; ++i)
2376 {
2377 xfree ((void*) file_data->file_names[i]);
2378 if (file_data->real_names)
2379 xfree ((void*) file_data->real_names[i]);
2380 }
2381
2382 /* The space for the struct itself lives on the obstack, so we don't
2383 free it here. */
2384 }
2385
2386 /* Create a quick_file_names hash table. */
2387
2388 static htab_up
2389 create_quick_file_names_table (unsigned int nr_initial_entries)
2390 {
2391 return htab_up (htab_create_alloc (nr_initial_entries,
2392 hash_file_name_entry, eq_file_name_entry,
2393 delete_file_name_entry, xcalloc, xfree));
2394 }
2395
2396 /* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2397 function is unrelated to symtabs, symtab would have to be created afterwards.
2398 You should call age_cached_comp_units after processing the CU. */
2399
2400 static dwarf2_cu *
2401 load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2402 bool skip_partial)
2403 {
2404 if (per_cu->is_debug_types)
2405 load_full_type_unit (per_cu, per_objfile);
2406 else
2407 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2408 skip_partial, language_minimal);
2409
2410 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2411 if (cu == nullptr)
2412 return nullptr; /* Dummy CU. */
2413
2414 dwarf2_find_base_address (cu->dies, cu);
2415
2416 return cu;
2417 }
2418
2419 /* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2420
2421 static void
2422 dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2423 dwarf2_per_objfile *per_objfile, bool skip_partial)
2424 {
2425 /* Skip type_unit_groups, reading the type units they contain
2426 is handled elsewhere. */
2427 if (per_cu->type_unit_group_p ())
2428 return;
2429
2430 {
2431 /* The destructor of dwarf2_queue_guard frees any entries left on
2432 the queue. After this point we're guaranteed to leave this function
2433 with the dwarf queue empty. */
2434 dwarf2_queue_guard q_guard (per_objfile);
2435
2436 if (!per_objfile->symtab_set_p (per_cu))
2437 {
2438 queue_comp_unit (per_cu, per_objfile, language_minimal);
2439 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
2440
2441 /* If we just loaded a CU from a DWO, and we're working with an index
2442 that may badly handle TUs, load all the TUs in that DWO as well.
2443 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2444 if (!per_cu->is_debug_types
2445 && cu != NULL
2446 && cu->dwo_unit != NULL
2447 && per_objfile->per_bfd->index_table != NULL
2448 && per_objfile->per_bfd->index_table->version <= 7
2449 /* DWP files aren't supported yet. */
2450 && get_dwp_file (per_objfile) == NULL)
2451 queue_and_load_all_dwo_tus (cu);
2452 }
2453
2454 process_queue (per_objfile);
2455 }
2456
2457 /* Age the cache, releasing compilation units that have not
2458 been used recently. */
2459 per_objfile->age_comp_units ();
2460 }
2461
2462 /* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2463 the per-objfile for which this symtab is instantiated.
2464
2465 Returns the resulting symbol table. */
2466
2467 static struct compunit_symtab *
2468 dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2469 dwarf2_per_objfile *per_objfile,
2470 bool skip_partial)
2471 {
2472 gdb_assert (per_objfile->per_bfd->using_index);
2473
2474 if (!per_objfile->symtab_set_p (per_cu))
2475 {
2476 free_cached_comp_units freer (per_objfile);
2477 scoped_restore decrementer = increment_reading_symtab ();
2478 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2479 process_cu_includes (per_objfile);
2480 }
2481
2482 return per_objfile->get_symtab (per_cu);
2483 }
2484
2485 /* See declaration. */
2486
2487 dwarf2_per_cu_data *
2488 dwarf2_per_bfd::get_cutu (int index)
2489 {
2490 if (index >= this->all_comp_units.size ())
2491 {
2492 index -= this->all_comp_units.size ();
2493 gdb_assert (index < this->all_type_units.size ());
2494 return &this->all_type_units[index]->per_cu;
2495 }
2496
2497 return this->all_comp_units[index];
2498 }
2499
2500 /* See declaration. */
2501
2502 dwarf2_per_cu_data *
2503 dwarf2_per_bfd::get_cu (int index)
2504 {
2505 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
2506
2507 return this->all_comp_units[index];
2508 }
2509
2510 /* See declaration. */
2511
2512 signatured_type *
2513 dwarf2_per_bfd::get_tu (int index)
2514 {
2515 gdb_assert (index >= 0 && index < this->all_type_units.size ());
2516
2517 return this->all_type_units[index];
2518 }
2519
2520 /* See read.h. */
2521
2522 dwarf2_per_cu_data *
2523 dwarf2_per_bfd::allocate_per_cu ()
2524 {
2525 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
2526 result->per_bfd = this;
2527 result->index = m_num_psymtabs++;
2528 return result;
2529 }
2530
2531 /* See read.h. */
2532
2533 signatured_type *
2534 dwarf2_per_bfd::allocate_signatured_type ()
2535 {
2536 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
2537 result->per_cu.per_bfd = this;
2538 result->per_cu.index = m_num_psymtabs++;
2539 return result;
2540 }
2541
2542 /* Return a new dwarf2_per_cu_data allocated on the per-bfd
2543 obstack, and constructed with the specified field values. */
2544
2545 static dwarf2_per_cu_data *
2546 create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2547 struct dwarf2_section_info *section,
2548 int is_dwz,
2549 sect_offset sect_off, ULONGEST length)
2550 {
2551 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
2552 the_cu->sect_off = sect_off;
2553 the_cu->length = length;
2554 the_cu->section = section;
2555 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
2556 struct dwarf2_per_cu_quick_data);
2557 the_cu->is_dwz = is_dwz;
2558 return the_cu;
2559 }
2560
2561 /* A helper for create_cus_from_index that handles a given list of
2562 CUs. */
2563
2564 static void
2565 create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2566 const gdb_byte *cu_list, offset_type n_elements,
2567 struct dwarf2_section_info *section,
2568 int is_dwz)
2569 {
2570 for (offset_type i = 0; i < n_elements; i += 2)
2571 {
2572 gdb_static_assert (sizeof (ULONGEST) >= 8);
2573
2574 sect_offset sect_off
2575 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2576 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
2577 cu_list += 2 * 8;
2578
2579 dwarf2_per_cu_data *per_cu
2580 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2581 length);
2582 per_bfd->all_comp_units.push_back (per_cu);
2583 }
2584 }
2585
2586 /* Read the CU list from the mapped index, and use it to create all
2587 the CU objects for PER_BFD. */
2588
2589 static void
2590 create_cus_from_index (dwarf2_per_bfd *per_bfd,
2591 const gdb_byte *cu_list, offset_type cu_list_elements,
2592 const gdb_byte *dwz_list, offset_type dwz_elements)
2593 {
2594 gdb_assert (per_bfd->all_comp_units.empty ());
2595 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2596
2597 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2598 &per_bfd->info, 0);
2599
2600 if (dwz_elements == 0)
2601 return;
2602
2603 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2604 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
2605 &dwz->info, 1);
2606 }
2607
2608 /* Create the signatured type hash table from the index. */
2609
2610 static void
2611 create_signatured_type_table_from_index
2612 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2613 const gdb_byte *bytes, offset_type elements)
2614 {
2615 gdb_assert (per_bfd->all_type_units.empty ());
2616 per_bfd->all_type_units.reserve (elements / 3);
2617
2618 htab_up sig_types_hash = allocate_signatured_type_table ();
2619
2620 for (offset_type i = 0; i < elements; i += 3)
2621 {
2622 struct signatured_type *sig_type;
2623 ULONGEST signature;
2624 void **slot;
2625 cu_offset type_offset_in_tu;
2626
2627 gdb_static_assert (sizeof (ULONGEST) >= 8);
2628 sect_offset sect_off
2629 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2630 type_offset_in_tu
2631 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2632 BFD_ENDIAN_LITTLE);
2633 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2634 bytes += 3 * 8;
2635
2636 sig_type = per_bfd->allocate_signatured_type ();
2637 sig_type->signature = signature;
2638 sig_type->type_offset_in_tu = type_offset_in_tu;
2639 sig_type->per_cu.is_debug_types = 1;
2640 sig_type->per_cu.section = section;
2641 sig_type->per_cu.sect_off = sect_off;
2642 sig_type->per_cu.v.quick
2643 = OBSTACK_ZALLOC (&per_bfd->obstack,
2644 struct dwarf2_per_cu_quick_data);
2645
2646 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2647 *slot = sig_type;
2648
2649 per_bfd->all_type_units.push_back (sig_type);
2650 }
2651
2652 per_bfd->signatured_types = std::move (sig_types_hash);
2653 }
2654
2655 /* Create the signatured type hash table from .debug_names. */
2656
2657 static void
2658 create_signatured_type_table_from_debug_names
2659 (dwarf2_per_objfile *per_objfile,
2660 const mapped_debug_names &map,
2661 struct dwarf2_section_info *section,
2662 struct dwarf2_section_info *abbrev_section)
2663 {
2664 struct objfile *objfile = per_objfile->objfile;
2665
2666 section->read (objfile);
2667 abbrev_section->read (objfile);
2668
2669 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2670 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
2671
2672 htab_up sig_types_hash = allocate_signatured_type_table ();
2673
2674 for (uint32_t i = 0; i < map.tu_count; ++i)
2675 {
2676 struct signatured_type *sig_type;
2677 void **slot;
2678
2679 sect_offset sect_off
2680 = (sect_offset) (extract_unsigned_integer
2681 (map.tu_table_reordered + i * map.offset_size,
2682 map.offset_size,
2683 map.dwarf5_byte_order));
2684
2685 comp_unit_head cu_header;
2686 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
2687 abbrev_section,
2688 section->buffer + to_underlying (sect_off),
2689 rcuh_kind::TYPE);
2690
2691 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
2692 sig_type->signature = cu_header.signature;
2693 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2694 sig_type->per_cu.is_debug_types = 1;
2695 sig_type->per_cu.section = section;
2696 sig_type->per_cu.sect_off = sect_off;
2697 sig_type->per_cu.v.quick
2698 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
2699 struct dwarf2_per_cu_quick_data);
2700
2701 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
2702 *slot = sig_type;
2703
2704 per_objfile->per_bfd->all_type_units.push_back (sig_type);
2705 }
2706
2707 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
2708 }
2709
2710 /* Read the address map data from the mapped index, and use it to
2711 populate the psymtabs_addrmap. */
2712
2713 static void
2714 create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
2715 struct mapped_index *index)
2716 {
2717 struct objfile *objfile = per_objfile->objfile;
2718 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2719 struct gdbarch *gdbarch = objfile->arch ();
2720 const gdb_byte *iter, *end;
2721 struct addrmap *mutable_map;
2722 CORE_ADDR baseaddr;
2723
2724 auto_obstack temp_obstack;
2725
2726 mutable_map = addrmap_create_mutable (&temp_obstack);
2727
2728 iter = index->address_table.data ();
2729 end = iter + index->address_table.size ();
2730
2731 baseaddr = objfile->text_section_offset ();
2732
2733 while (iter < end)
2734 {
2735 ULONGEST hi, lo, cu_index;
2736 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2737 iter += 8;
2738 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2739 iter += 8;
2740 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2741 iter += 4;
2742
2743 if (lo > hi)
2744 {
2745 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
2746 hex_string (lo), hex_string (hi));
2747 continue;
2748 }
2749
2750 if (cu_index >= per_bfd->all_comp_units.size ())
2751 {
2752 complaint (_(".gdb_index address table has invalid CU number %u"),
2753 (unsigned) cu_index);
2754 continue;
2755 }
2756
2757 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2758 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
2759 addrmap_set_empty (mutable_map, lo, hi - 1,
2760 per_bfd->get_cu (cu_index));
2761 }
2762
2763 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2764 &per_bfd->obstack);
2765 }
2766
2767 /* Read the address map data from DWARF-5 .debug_aranges, and use it to
2768 populate the psymtabs_addrmap. */
2769
2770 static void
2771 create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
2772 struct dwarf2_section_info *section)
2773 {
2774 struct objfile *objfile = per_objfile->objfile;
2775 bfd *abfd = objfile->obfd;
2776 struct gdbarch *gdbarch = objfile->arch ();
2777 const CORE_ADDR baseaddr = objfile->text_section_offset ();
2778 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2779
2780 auto_obstack temp_obstack;
2781 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2782
2783 std::unordered_map<sect_offset,
2784 dwarf2_per_cu_data *,
2785 gdb::hash_enum<sect_offset>>
2786 debug_info_offset_to_per_cu;
2787 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
2788 {
2789 const auto insertpair
2790 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2791 if (!insertpair.second)
2792 {
2793 warning (_("Section .debug_aranges in %s has duplicate "
2794 "debug_info_offset %s, ignoring .debug_aranges."),
2795 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
2796 return;
2797 }
2798 }
2799
2800 section->read (objfile);
2801
2802 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2803
2804 const gdb_byte *addr = section->buffer;
2805
2806 while (addr < section->buffer + section->size)
2807 {
2808 const gdb_byte *const entry_addr = addr;
2809 unsigned int bytes_read;
2810
2811 const LONGEST entry_length = read_initial_length (abfd, addr,
2812 &bytes_read);
2813 addr += bytes_read;
2814
2815 const gdb_byte *const entry_end = addr + entry_length;
2816 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2817 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2818 if (addr + entry_length > section->buffer + section->size)
2819 {
2820 warning (_("Section .debug_aranges in %s entry at offset %s "
2821 "length %s exceeds section length %s, "
2822 "ignoring .debug_aranges."),
2823 objfile_name (objfile),
2824 plongest (entry_addr - section->buffer),
2825 plongest (bytes_read + entry_length),
2826 pulongest (section->size));
2827 return;
2828 }
2829
2830 /* The version number. */
2831 const uint16_t version = read_2_bytes (abfd, addr);
2832 addr += 2;
2833 if (version != 2)
2834 {
2835 warning (_("Section .debug_aranges in %s entry at offset %s "
2836 "has unsupported version %d, ignoring .debug_aranges."),
2837 objfile_name (objfile),
2838 plongest (entry_addr - section->buffer), version);
2839 return;
2840 }
2841
2842 const uint64_t debug_info_offset
2843 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2844 addr += offset_size;
2845 const auto per_cu_it
2846 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2847 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2848 {
2849 warning (_("Section .debug_aranges in %s entry at offset %s "
2850 "debug_info_offset %s does not exists, "
2851 "ignoring .debug_aranges."),
2852 objfile_name (objfile),
2853 plongest (entry_addr - section->buffer),
2854 pulongest (debug_info_offset));
2855 return;
2856 }
2857 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2858
2859 const uint8_t address_size = *addr++;
2860 if (address_size < 1 || address_size > 8)
2861 {
2862 warning (_("Section .debug_aranges in %s entry at offset %s "
2863 "address_size %u is invalid, ignoring .debug_aranges."),
2864 objfile_name (objfile),
2865 plongest (entry_addr - section->buffer), address_size);
2866 return;
2867 }
2868
2869 const uint8_t segment_selector_size = *addr++;
2870 if (segment_selector_size != 0)
2871 {
2872 warning (_("Section .debug_aranges in %s entry at offset %s "
2873 "segment_selector_size %u is not supported, "
2874 "ignoring .debug_aranges."),
2875 objfile_name (objfile),
2876 plongest (entry_addr - section->buffer),
2877 segment_selector_size);
2878 return;
2879 }
2880
2881 /* Must pad to an alignment boundary that is twice the address
2882 size. It is undocumented by the DWARF standard but GCC does
2883 use it. */
2884 for (size_t padding = ((-(addr - section->buffer))
2885 & (2 * address_size - 1));
2886 padding > 0; padding--)
2887 if (*addr++ != 0)
2888 {
2889 warning (_("Section .debug_aranges in %s entry at offset %s "
2890 "padding is not zero, ignoring .debug_aranges."),
2891 objfile_name (objfile),
2892 plongest (entry_addr - section->buffer));
2893 return;
2894 }
2895
2896 for (;;)
2897 {
2898 if (addr + 2 * address_size > entry_end)
2899 {
2900 warning (_("Section .debug_aranges in %s entry at offset %s "
2901 "address list is not properly terminated, "
2902 "ignoring .debug_aranges."),
2903 objfile_name (objfile),
2904 plongest (entry_addr - section->buffer));
2905 return;
2906 }
2907 ULONGEST start = extract_unsigned_integer (addr, address_size,
2908 dwarf5_byte_order);
2909 addr += address_size;
2910 ULONGEST length = extract_unsigned_integer (addr, address_size,
2911 dwarf5_byte_order);
2912 addr += address_size;
2913 if (start == 0 && length == 0)
2914 break;
2915 if (start == 0 && !per_bfd->has_section_at_zero)
2916 {
2917 /* Symbol was eliminated due to a COMDAT group. */
2918 continue;
2919 }
2920 ULONGEST end = start + length;
2921 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2922 - baseaddr);
2923 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2924 - baseaddr);
2925 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2926 }
2927 }
2928
2929 per_bfd->index_addrmap = addrmap_create_fixed (mutable_map,
2930 &per_bfd->obstack);
2931 }
2932
2933 /* A helper function that reads the .gdb_index from BUFFER and fills
2934 in MAP. FILENAME is the name of the file containing the data;
2935 it is used for error reporting. DEPRECATED_OK is true if it is
2936 ok to use deprecated sections.
2937
2938 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2939 out parameters that are filled in with information about the CU and
2940 TU lists in the section.
2941
2942 Returns true if all went well, false otherwise. */
2943
2944 static bool
2945 read_gdb_index_from_buffer (const char *filename,
2946 bool deprecated_ok,
2947 gdb::array_view<const gdb_byte> buffer,
2948 struct mapped_index *map,
2949 const gdb_byte **cu_list,
2950 offset_type *cu_list_elements,
2951 const gdb_byte **types_list,
2952 offset_type *types_list_elements)
2953 {
2954 const gdb_byte *addr = &buffer[0];
2955
2956 /* Version check. */
2957 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
2958 /* Versions earlier than 3 emitted every copy of a psymbol. This
2959 causes the index to behave very poorly for certain requests. Version 3
2960 contained incomplete addrmap. So, it seems better to just ignore such
2961 indices. */
2962 if (version < 4)
2963 {
2964 static int warning_printed = 0;
2965 if (!warning_printed)
2966 {
2967 warning (_("Skipping obsolete .gdb_index section in %s."),
2968 filename);
2969 warning_printed = 1;
2970 }
2971 return 0;
2972 }
2973 /* Index version 4 uses a different hash function than index version
2974 5 and later.
2975
2976 Versions earlier than 6 did not emit psymbols for inlined
2977 functions. Using these files will cause GDB not to be able to
2978 set breakpoints on inlined functions by name, so we ignore these
2979 indices unless the user has done
2980 "set use-deprecated-index-sections on". */
2981 if (version < 6 && !deprecated_ok)
2982 {
2983 static int warning_printed = 0;
2984 if (!warning_printed)
2985 {
2986 warning (_("\
2987 Skipping deprecated .gdb_index section in %s.\n\
2988 Do \"set use-deprecated-index-sections on\" before the file is read\n\
2989 to use the section anyway."),
2990 filename);
2991 warning_printed = 1;
2992 }
2993 return 0;
2994 }
2995 /* Version 7 indices generated by gold refer to the CU for a symbol instead
2996 of the TU (for symbols coming from TUs),
2997 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2998 Plus gold-generated indices can have duplicate entries for global symbols,
2999 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3000 These are just performance bugs, and we can't distinguish gdb-generated
3001 indices from gold-generated ones, so issue no warning here. */
3002
3003 /* Indexes with higher version than the one supported by GDB may be no
3004 longer backward compatible. */
3005 if (version > 8)
3006 return 0;
3007
3008 map->version = version;
3009
3010 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
3011
3012 int i = 0;
3013 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3014 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3015 / 8);
3016 ++i;
3017
3018 *types_list = addr + MAYBE_SWAP (metadata[i]);
3019 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3020 - MAYBE_SWAP (metadata[i]))
3021 / 8);
3022 ++i;
3023
3024 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3025 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3026 map->address_table
3027 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
3028 ++i;
3029
3030 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3031 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3032 map->symbol_table
3033 = gdb::array_view<mapped_index::symbol_table_slot>
3034 ((mapped_index::symbol_table_slot *) symbol_table,
3035 (mapped_index::symbol_table_slot *) symbol_table_end);
3036
3037 ++i;
3038 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
3039
3040 return 1;
3041 }
3042
3043 /* Callback types for dwarf2_read_gdb_index. */
3044
3045 typedef gdb::function_view
3046 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
3047 get_gdb_index_contents_ftype;
3048 typedef gdb::function_view
3049 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3050 get_gdb_index_contents_dwz_ftype;
3051
3052 /* Read .gdb_index. If everything went ok, initialize the "quick"
3053 elements of all the CUs and return 1. Otherwise, return 0. */
3054
3055 static int
3056 dwarf2_read_gdb_index
3057 (dwarf2_per_objfile *per_objfile,
3058 get_gdb_index_contents_ftype get_gdb_index_contents,
3059 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
3060 {
3061 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3062 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
3063 struct dwz_file *dwz;
3064 struct objfile *objfile = per_objfile->objfile;
3065 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
3066
3067 gdb::array_view<const gdb_byte> main_index_contents
3068 = get_gdb_index_contents (objfile, per_bfd);
3069
3070 if (main_index_contents.empty ())
3071 return 0;
3072
3073 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3074 if (!read_gdb_index_from_buffer (objfile_name (objfile),
3075 use_deprecated_index_sections,
3076 main_index_contents, map.get (), &cu_list,
3077 &cu_list_elements, &types_list,
3078 &types_list_elements))
3079 return 0;
3080
3081 /* Don't use the index if it's empty. */
3082 if (map->symbol_table.empty ())
3083 return 0;
3084
3085 /* If there is a .dwz file, read it so we can get its CU list as
3086 well. */
3087 dwz = dwarf2_get_dwz_file (per_bfd);
3088 if (dwz != NULL)
3089 {
3090 struct mapped_index dwz_map;
3091 const gdb_byte *dwz_types_ignore;
3092 offset_type dwz_types_elements_ignore;
3093
3094 gdb::array_view<const gdb_byte> dwz_index_content
3095 = get_gdb_index_contents_dwz (objfile, dwz);
3096
3097 if (dwz_index_content.empty ())
3098 return 0;
3099
3100 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
3101 1, dwz_index_content, &dwz_map,
3102 &dwz_list, &dwz_list_elements,
3103 &dwz_types_ignore,
3104 &dwz_types_elements_ignore))
3105 {
3106 warning (_("could not read '.gdb_index' section from %s; skipping"),
3107 bfd_get_filename (dwz->dwz_bfd.get ()));
3108 return 0;
3109 }
3110 }
3111
3112 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3113 dwz_list_elements);
3114
3115 if (types_list_elements)
3116 {
3117 /* We can only handle a single .debug_types when we have an
3118 index. */
3119 if (per_bfd->types.size () != 1)
3120 return 0;
3121
3122 dwarf2_section_info *section = &per_bfd->types[0];
3123
3124 create_signatured_type_table_from_index (per_bfd, section, types_list,
3125 types_list_elements);
3126 }
3127
3128 create_addrmap_from_index (per_objfile, map.get ());
3129
3130 per_bfd->index_table = std::move (map);
3131 per_bfd->using_index = 1;
3132 per_bfd->quick_file_names_table =
3133 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3134
3135 return 1;
3136 }
3137
3138 /* die_reader_func for dw2_get_file_names. */
3139
3140 static void
3141 dw2_get_file_names_reader (const struct die_reader_specs *reader,
3142 struct die_info *comp_unit_die)
3143 {
3144 struct dwarf2_cu *cu = reader->cu;
3145 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
3146 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3147 struct dwarf2_per_cu_data *lh_cu;
3148 struct attribute *attr;
3149 void **slot;
3150 struct quick_file_names *qfn;
3151
3152 gdb_assert (! this_cu->is_debug_types);
3153
3154 /* Our callers never want to match partial units -- instead they
3155 will match the enclosing full CU. */
3156 if (comp_unit_die->tag == DW_TAG_partial_unit)
3157 {
3158 this_cu->v.quick->no_file_data = 1;
3159 return;
3160 }
3161
3162 lh_cu = this_cu;
3163 slot = NULL;
3164
3165 line_header_up lh;
3166 sect_offset line_offset {};
3167
3168 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
3169 if (attr != nullptr && attr->form_is_unsigned ())
3170 {
3171 struct quick_file_names find_entry;
3172
3173 line_offset = (sect_offset) attr->as_unsigned ();
3174
3175 /* We may have already read in this line header (TU line header sharing).
3176 If we have we're done. */
3177 find_entry.hash.dwo_unit = cu->dwo_unit;
3178 find_entry.hash.line_sect_off = line_offset;
3179 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
3180 &find_entry, INSERT);
3181 if (*slot != NULL)
3182 {
3183 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
3184 return;
3185 }
3186
3187 lh = dwarf_decode_line_header (line_offset, cu);
3188 }
3189 if (lh == NULL)
3190 {
3191 lh_cu->v.quick->no_file_data = 1;
3192 return;
3193 }
3194
3195 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
3196 qfn->hash.dwo_unit = cu->dwo_unit;
3197 qfn->hash.line_sect_off = line_offset;
3198 gdb_assert (slot != NULL);
3199 *slot = qfn;
3200
3201 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
3202
3203 int offset = 0;
3204 if (strcmp (fnd.name, "<unknown>") != 0)
3205 ++offset;
3206
3207 qfn->num_file_names = offset + lh->file_names_size ();
3208 qfn->file_names =
3209 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
3210 qfn->num_file_names);
3211 if (offset != 0)
3212 qfn->file_names[0] = xstrdup (fnd.name);
3213 for (int i = 0; i < lh->file_names_size (); ++i)
3214 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3215 fnd.comp_dir).release ();
3216 qfn->real_names = NULL;
3217
3218 lh_cu->v.quick->file_names = qfn;
3219 }
3220
3221 /* A helper for the "quick" functions which attempts to read the line
3222 table for THIS_CU. */
3223
3224 static struct quick_file_names *
3225 dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3226 dwarf2_per_objfile *per_objfile)
3227 {
3228 /* This should never be called for TUs. */
3229 gdb_assert (! this_cu->is_debug_types);
3230 /* Nor type unit groups. */
3231 gdb_assert (! this_cu->type_unit_group_p ());
3232
3233 if (this_cu->v.quick->file_names != NULL)
3234 return this_cu->v.quick->file_names;
3235 /* If we know there is no line data, no point in looking again. */
3236 if (this_cu->v.quick->no_file_data)
3237 return NULL;
3238
3239 cutu_reader reader (this_cu, per_objfile);
3240 if (!reader.dummy_p)
3241 dw2_get_file_names_reader (&reader, reader.comp_unit_die);
3242
3243 if (this_cu->v.quick->no_file_data)
3244 return NULL;
3245 return this_cu->v.quick->file_names;
3246 }
3247
3248 /* A helper for the "quick" functions which computes and caches the
3249 real path for a given file name from the line table. */
3250
3251 static const char *
3252 dw2_get_real_path (dwarf2_per_objfile *per_objfile,
3253 struct quick_file_names *qfn, int index)
3254 {
3255 if (qfn->real_names == NULL)
3256 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
3257 qfn->num_file_names, const char *);
3258
3259 if (qfn->real_names[index] == NULL)
3260 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
3261
3262 return qfn->real_names[index];
3263 }
3264
3265 struct symtab *
3266 dwarf2_base_index_functions::find_last_source_symtab (struct objfile *objfile)
3267 {
3268 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3269 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3270 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
3271
3272 if (cust == NULL)
3273 return NULL;
3274
3275 return compunit_primary_filetab (cust);
3276 }
3277
3278 /* Traversal function for dw2_forget_cached_source_info. */
3279
3280 static int
3281 dw2_free_cached_file_names (void **slot, void *info)
3282 {
3283 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
3284
3285 if (file_data->real_names)
3286 {
3287 int i;
3288
3289 for (i = 0; i < file_data->num_file_names; ++i)
3290 {
3291 xfree ((void*) file_data->real_names[i]);
3292 file_data->real_names[i] = NULL;
3293 }
3294 }
3295
3296 return 1;
3297 }
3298
3299 void
3300 dwarf2_base_index_functions::forget_cached_source_info
3301 (struct objfile *objfile)
3302 {
3303 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3304
3305 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
3306 dw2_free_cached_file_names, NULL);
3307 }
3308
3309 /* Struct used to manage iterating over all CUs looking for a symbol. */
3310
3311 struct dw2_symtab_iterator
3312 {
3313 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3314 dwarf2_per_objfile *per_objfile;
3315 /* If set, only look for symbols that match that block. Valid values are
3316 GLOBAL_BLOCK and STATIC_BLOCK. */
3317 gdb::optional<block_enum> block_index;
3318 /* The kind of symbol we're looking for. */
3319 domain_enum domain;
3320 /* The list of CUs from the index entry of the symbol,
3321 or NULL if not found. */
3322 offset_type *vec;
3323 /* The next element in VEC to look at. */
3324 int next;
3325 /* The number of elements in VEC, or zero if there is no match. */
3326 int length;
3327 /* Have we seen a global version of the symbol?
3328 If so we can ignore all further global instances.
3329 This is to work around gold/15646, inefficient gold-generated
3330 indices. */
3331 int global_seen;
3332 };
3333
3334 /* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3335
3336 static void
3337 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3338 dwarf2_per_objfile *per_objfile,
3339 gdb::optional<block_enum> block_index,
3340 domain_enum domain, offset_type namei)
3341 {
3342 iter->per_objfile = per_objfile;
3343 iter->block_index = block_index;
3344 iter->domain = domain;
3345 iter->next = 0;
3346 iter->global_seen = 0;
3347 iter->vec = NULL;
3348 iter->length = 0;
3349
3350 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3351 /* index is NULL if OBJF_READNOW. */
3352 if (index == NULL)
3353 return;
3354
3355 gdb_assert (!index->symbol_name_slot_invalid (namei));
3356 const auto &bucket = index->symbol_table[namei];
3357
3358 iter->vec = (offset_type *) (index->constant_pool
3359 + MAYBE_SWAP (bucket.vec));
3360 iter->length = MAYBE_SWAP (*iter->vec);
3361 }
3362
3363 /* Return the next matching CU or NULL if there are no more. */
3364
3365 static struct dwarf2_per_cu_data *
3366 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3367 {
3368 dwarf2_per_objfile *per_objfile = iter->per_objfile;
3369
3370 for ( ; iter->next < iter->length; ++iter->next)
3371 {
3372 offset_type cu_index_and_attrs =
3373 MAYBE_SWAP (iter->vec[iter->next + 1]);
3374 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
3375 gdb_index_symbol_kind symbol_kind =
3376 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3377 /* Only check the symbol attributes if they're present.
3378 Indices prior to version 7 don't record them,
3379 and indices >= 7 may elide them for certain symbols
3380 (gold does this). */
3381 int attrs_valid =
3382 (per_objfile->per_bfd->index_table->version >= 7
3383 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3384
3385 /* Don't crash on bad data. */
3386 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3387 + per_objfile->per_bfd->all_type_units.size ()))
3388 {
3389 complaint (_(".gdb_index entry has bad CU index"
3390 " [in module %s]"), objfile_name (per_objfile->objfile));
3391 continue;
3392 }
3393
3394 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3395
3396 /* Skip if already read in. */
3397 if (per_objfile->symtab_set_p (per_cu))
3398 continue;
3399
3400 /* Check static vs global. */
3401 if (attrs_valid)
3402 {
3403 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3404
3405 if (iter->block_index.has_value ())
3406 {
3407 bool want_static = *iter->block_index == STATIC_BLOCK;
3408
3409 if (is_static != want_static)
3410 continue;
3411 }
3412
3413 /* Work around gold/15646. */
3414 if (!is_static
3415 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3416 {
3417 if (iter->global_seen)
3418 continue;
3419
3420 iter->global_seen = 1;
3421 }
3422 }
3423
3424 /* Only check the symbol's kind if it has one. */
3425 if (attrs_valid)
3426 {
3427 switch (iter->domain)
3428 {
3429 case VAR_DOMAIN:
3430 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3431 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3432 /* Some types are also in VAR_DOMAIN. */
3433 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3434 continue;
3435 break;
3436 case STRUCT_DOMAIN:
3437 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3438 continue;
3439 break;
3440 case LABEL_DOMAIN:
3441 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3442 continue;
3443 break;
3444 case MODULE_DOMAIN:
3445 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3446 continue;
3447 break;
3448 default:
3449 break;
3450 }
3451 }
3452
3453 ++iter->next;
3454 return per_cu;
3455 }
3456
3457 return NULL;
3458 }
3459
3460 void
3461 dwarf2_base_index_functions::print_stats (struct objfile *objfile,
3462 bool print_bcache)
3463 {
3464 if (print_bcache)
3465 return;
3466
3467 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3468 int total = (per_objfile->per_bfd->all_comp_units.size ()
3469 + per_objfile->per_bfd->all_type_units.size ());
3470 int count = 0;
3471
3472 for (int i = 0; i < total; ++i)
3473 {
3474 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3475
3476 if (!per_objfile->symtab_set_p (per_cu))
3477 ++count;
3478 }
3479 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
3480 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3481 }
3482
3483 /* This dumps minimal information about the index.
3484 It is called via "mt print objfiles".
3485 One use is to verify .gdb_index has been loaded by the
3486 gdb.dwarf2/gdb-index.exp testcase. */
3487
3488 void
3489 dwarf2_gdb_index::dump (struct objfile *objfile)
3490 {
3491 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3492
3493 gdb_assert (per_objfile->per_bfd->using_index);
3494 printf_filtered (".gdb_index:");
3495 if (per_objfile->per_bfd->index_table != NULL)
3496 {
3497 printf_filtered (" version %d\n",
3498 per_objfile->per_bfd->index_table->version);
3499 }
3500 else
3501 printf_filtered (" faked for \"readnow\"\n");
3502 printf_filtered ("\n");
3503 }
3504
3505 void
3506 dwarf2_base_index_functions::expand_all_symtabs (struct objfile *objfile)
3507 {
3508 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3509 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3510 + per_objfile->per_bfd->all_type_units.size ());
3511
3512 for (int i = 0; i < total_units; ++i)
3513 {
3514 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
3515
3516 /* We don't want to directly expand a partial CU, because if we
3517 read it with the wrong language, then assertion failures can
3518 be triggered later on. See PR symtab/23010. So, tell
3519 dw2_instantiate_symtab to skip partial CUs -- any important
3520 partial CU will be read via DW_TAG_imported_unit anyway. */
3521 dw2_instantiate_symtab (per_cu, per_objfile, true);
3522 }
3523 }
3524
3525 static bool
3526 dw2_expand_symtabs_matching_symbol
3527 (mapped_index_base &index,
3528 const lookup_name_info &lookup_name_in,
3529 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3530 gdb::function_view<bool (offset_type)> match_callback,
3531 dwarf2_per_objfile *per_objfile);
3532
3533 static bool
3534 dw2_expand_symtabs_matching_one
3535 (dwarf2_per_cu_data *per_cu,
3536 dwarf2_per_objfile *per_objfile,
3537 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3538 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3539
3540 void
3541 dwarf2_gdb_index::map_matching_symbols
3542 (struct objfile *objfile,
3543 const lookup_name_info &name, domain_enum domain,
3544 int global,
3545 gdb::function_view<symbol_found_callback_ftype> callback,
3546 symbol_compare_ftype *ordered_compare)
3547 {
3548 /* Used for Ada. */
3549 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3550
3551 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3552
3553 if (per_objfile->per_bfd->index_table != nullptr)
3554 {
3555 mapped_index &index = *per_objfile->per_bfd->index_table;
3556
3557 const char *match_name = name.ada ().lookup_name ().c_str ();
3558 auto matcher = [&] (const char *symname)
3559 {
3560 if (ordered_compare == nullptr)
3561 return true;
3562 return ordered_compare (symname, match_name) == 0;
3563 };
3564
3565 dw2_expand_symtabs_matching_symbol (index, name, matcher,
3566 [&] (offset_type namei)
3567 {
3568 struct dw2_symtab_iterator iter;
3569 struct dwarf2_per_cu_data *per_cu;
3570
3571 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
3572 namei);
3573 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3574 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
3575 nullptr);
3576 return true;
3577 }, per_objfile);
3578 }
3579 else
3580 {
3581 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3582 proceed assuming all symtabs have been read in. */
3583 }
3584
3585 for (compunit_symtab *cust : objfile->compunits ())
3586 {
3587 const struct block *block;
3588
3589 if (cust == NULL)
3590 continue;
3591 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3592 if (!iterate_over_symbols_terminated (block, name,
3593 domain, callback))
3594 return;
3595 }
3596 }
3597
3598 /* Starting from a search name, return the string that finds the upper
3599 bound of all strings that start with SEARCH_NAME in a sorted name
3600 list. Returns the empty string to indicate that the upper bound is
3601 the end of the list. */
3602
3603 static std::string
3604 make_sort_after_prefix_name (const char *search_name)
3605 {
3606 /* When looking to complete "func", we find the upper bound of all
3607 symbols that start with "func" by looking for where we'd insert
3608 the closest string that would follow "func" in lexicographical
3609 order. Usually, that's "func"-with-last-character-incremented,
3610 i.e. "fund". Mind non-ASCII characters, though. Usually those
3611 will be UTF-8 multi-byte sequences, but we can't be certain.
3612 Especially mind the 0xff character, which is a valid character in
3613 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3614 rule out compilers allowing it in identifiers. Note that
3615 conveniently, strcmp/strcasecmp are specified to compare
3616 characters interpreted as unsigned char. So what we do is treat
3617 the whole string as a base 256 number composed of a sequence of
3618 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3619 to 0, and carries 1 to the following more-significant position.
3620 If the very first character in SEARCH_NAME ends up incremented
3621 and carries/overflows, then the upper bound is the end of the
3622 list. The string after the empty string is also the empty
3623 string.
3624
3625 Some examples of this operation:
3626
3627 SEARCH_NAME => "+1" RESULT
3628
3629 "abc" => "abd"
3630 "ab\xff" => "ac"
3631 "\xff" "a" "\xff" => "\xff" "b"
3632 "\xff" => ""
3633 "\xff\xff" => ""
3634 "" => ""
3635
3636 Then, with these symbols for example:
3637
3638 func
3639 func1
3640 fund
3641
3642 completing "func" looks for symbols between "func" and
3643 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3644 which finds "func" and "func1", but not "fund".
3645
3646 And with:
3647
3648 funcÿ (Latin1 'ÿ' [0xff])
3649 funcÿ1
3650 fund
3651
3652 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3653 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3654
3655 And with:
3656
3657 ÿÿ (Latin1 'ÿ' [0xff])
3658 ÿÿ1
3659
3660 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3661 the end of the list.
3662 */
3663 std::string after = search_name;
3664 while (!after.empty () && (unsigned char) after.back () == 0xff)
3665 after.pop_back ();
3666 if (!after.empty ())
3667 after.back () = (unsigned char) after.back () + 1;
3668 return after;
3669 }
3670
3671 /* See declaration. */
3672
3673 std::pair<std::vector<name_component>::const_iterator,
3674 std::vector<name_component>::const_iterator>
3675 mapped_index_base::find_name_components_bounds
3676 (const lookup_name_info &lookup_name_without_params, language lang,
3677 dwarf2_per_objfile *per_objfile) const
3678 {
3679 auto *name_cmp
3680 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3681
3682 const char *lang_name
3683 = lookup_name_without_params.language_lookup_name (lang);
3684
3685 /* Comparison function object for lower_bound that matches against a
3686 given symbol name. */
3687 auto lookup_compare_lower = [&] (const name_component &elem,
3688 const char *name)
3689 {
3690 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3691 const char *elem_name = elem_qualified + elem.name_offset;
3692 return name_cmp (elem_name, name) < 0;
3693 };
3694
3695 /* Comparison function object for upper_bound that matches against a
3696 given symbol name. */
3697 auto lookup_compare_upper = [&] (const char *name,
3698 const name_component &elem)
3699 {
3700 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3701 const char *elem_name = elem_qualified + elem.name_offset;
3702 return name_cmp (name, elem_name) < 0;
3703 };
3704
3705 auto begin = this->name_components.begin ();
3706 auto end = this->name_components.end ();
3707
3708 /* Find the lower bound. */
3709 auto lower = [&] ()
3710 {
3711 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3712 return begin;
3713 else
3714 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3715 } ();
3716
3717 /* Find the upper bound. */
3718 auto upper = [&] ()
3719 {
3720 if (lookup_name_without_params.completion_mode ())
3721 {
3722 /* In completion mode, we want UPPER to point past all
3723 symbols names that have the same prefix. I.e., with
3724 these symbols, and completing "func":
3725
3726 function << lower bound
3727 function1
3728 other_function << upper bound
3729
3730 We find the upper bound by looking for the insertion
3731 point of "func"-with-last-character-incremented,
3732 i.e. "fund". */
3733 std::string after = make_sort_after_prefix_name (lang_name);
3734 if (after.empty ())
3735 return end;
3736 return std::lower_bound (lower, end, after.c_str (),
3737 lookup_compare_lower);
3738 }
3739 else
3740 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3741 } ();
3742
3743 return {lower, upper};
3744 }
3745
3746 /* See declaration. */
3747
3748 void
3749 mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
3750 {
3751 if (!this->name_components.empty ())
3752 return;
3753
3754 this->name_components_casing = case_sensitivity;
3755 auto *name_cmp
3756 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3757
3758 /* The code below only knows how to break apart components of C++
3759 symbol names (and other languages that use '::' as
3760 namespace/module separator) and Ada symbol names. */
3761 auto count = this->symbol_name_count ();
3762 for (offset_type idx = 0; idx < count; idx++)
3763 {
3764 if (this->symbol_name_slot_invalid (idx))
3765 continue;
3766
3767 const char *name = this->symbol_name_at (idx, per_objfile);
3768
3769 /* Add each name component to the name component table. */
3770 unsigned int previous_len = 0;
3771
3772 if (strstr (name, "::") != nullptr)
3773 {
3774 for (unsigned int current_len = cp_find_first_component (name);
3775 name[current_len] != '\0';
3776 current_len += cp_find_first_component (name + current_len))
3777 {
3778 gdb_assert (name[current_len] == ':');
3779 this->name_components.push_back ({previous_len, idx});
3780 /* Skip the '::'. */
3781 current_len += 2;
3782 previous_len = current_len;
3783 }
3784 }
3785 else
3786 {
3787 /* Handle the Ada encoded (aka mangled) form here. */
3788 for (const char *iter = strstr (name, "__");
3789 iter != nullptr;
3790 iter = strstr (iter, "__"))
3791 {
3792 this->name_components.push_back ({previous_len, idx});
3793 iter += 2;
3794 previous_len = iter - name;
3795 }
3796 }
3797
3798 this->name_components.push_back ({previous_len, idx});
3799 }
3800
3801 /* Sort name_components elements by name. */
3802 auto name_comp_compare = [&] (const name_component &left,
3803 const name_component &right)
3804 {
3805 const char *left_qualified
3806 = this->symbol_name_at (left.idx, per_objfile);
3807 const char *right_qualified
3808 = this->symbol_name_at (right.idx, per_objfile);
3809
3810 const char *left_name = left_qualified + left.name_offset;
3811 const char *right_name = right_qualified + right.name_offset;
3812
3813 return name_cmp (left_name, right_name) < 0;
3814 };
3815
3816 std::sort (this->name_components.begin (),
3817 this->name_components.end (),
3818 name_comp_compare);
3819 }
3820
3821 /* Helper for dw2_expand_symtabs_matching that works with a
3822 mapped_index_base instead of the containing objfile. This is split
3823 to a separate function in order to be able to unit test the
3824 name_components matching using a mock mapped_index_base. For each
3825 symbol name that matches, calls MATCH_CALLBACK, passing it the
3826 symbol's index in the mapped_index_base symbol table. */
3827
3828 static bool
3829 dw2_expand_symtabs_matching_symbol
3830 (mapped_index_base &index,
3831 const lookup_name_info &lookup_name_in,
3832 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3833 gdb::function_view<bool (offset_type)> match_callback,
3834 dwarf2_per_objfile *per_objfile)
3835 {
3836 lookup_name_info lookup_name_without_params
3837 = lookup_name_in.make_ignore_params ();
3838
3839 /* Build the symbol name component sorted vector, if we haven't
3840 yet. */
3841 index.build_name_components (per_objfile);
3842
3843 /* The same symbol may appear more than once in the range though.
3844 E.g., if we're looking for symbols that complete "w", and we have
3845 a symbol named "w1::w2", we'll find the two name components for
3846 that same symbol in the range. To be sure we only call the
3847 callback once per symbol, we first collect the symbol name
3848 indexes that matched in a temporary vector and ignore
3849 duplicates. */
3850 std::vector<offset_type> matches;
3851
3852 struct name_and_matcher
3853 {
3854 symbol_name_matcher_ftype *matcher;
3855 const char *name;
3856
3857 bool operator== (const name_and_matcher &other) const
3858 {
3859 return matcher == other.matcher && strcmp (name, other.name) == 0;
3860 }
3861 };
3862
3863 /* A vector holding all the different symbol name matchers, for all
3864 languages. */
3865 std::vector<name_and_matcher> matchers;
3866
3867 for (int i = 0; i < nr_languages; i++)
3868 {
3869 enum language lang_e = (enum language) i;
3870
3871 const language_defn *lang = language_def (lang_e);
3872 symbol_name_matcher_ftype *name_matcher
3873 = lang->get_symbol_name_matcher (lookup_name_without_params);
3874
3875 name_and_matcher key {
3876 name_matcher,
3877 lookup_name_without_params.language_lookup_name (lang_e)
3878 };
3879
3880 /* Don't insert the same comparison routine more than once.
3881 Note that we do this linear walk. This is not a problem in
3882 practice because the number of supported languages is
3883 low. */
3884 if (std::find (matchers.begin (), matchers.end (), key)
3885 != matchers.end ())
3886 continue;
3887 matchers.push_back (std::move (key));
3888
3889 auto bounds
3890 = index.find_name_components_bounds (lookup_name_without_params,
3891 lang_e, per_objfile);
3892
3893 /* Now for each symbol name in range, check to see if we have a name
3894 match, and if so, call the MATCH_CALLBACK callback. */
3895
3896 for (; bounds.first != bounds.second; ++bounds.first)
3897 {
3898 const char *qualified
3899 = index.symbol_name_at (bounds.first->idx, per_objfile);
3900
3901 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3902 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3903 continue;
3904
3905 matches.push_back (bounds.first->idx);
3906 }
3907 }
3908
3909 std::sort (matches.begin (), matches.end ());
3910
3911 /* Finally call the callback, once per match. */
3912 ULONGEST prev = -1;
3913 bool result = true;
3914 for (offset_type idx : matches)
3915 {
3916 if (prev != idx)
3917 {
3918 if (!match_callback (idx))
3919 {
3920 result = false;
3921 break;
3922 }
3923 prev = idx;
3924 }
3925 }
3926
3927 /* Above we use a type wider than idx's for 'prev', since 0 and
3928 (offset_type)-1 are both possible values. */
3929 static_assert (sizeof (prev) > sizeof (offset_type), "");
3930
3931 return result;
3932 }
3933
3934 #if GDB_SELF_TEST
3935
3936 namespace selftests { namespace dw2_expand_symtabs_matching {
3937
3938 /* A mock .gdb_index/.debug_names-like name index table, enough to
3939 exercise dw2_expand_symtabs_matching_symbol, which works with the
3940 mapped_index_base interface. Builds an index from the symbol list
3941 passed as parameter to the constructor. */
3942 class mock_mapped_index : public mapped_index_base
3943 {
3944 public:
3945 mock_mapped_index (gdb::array_view<const char *> symbols)
3946 : m_symbol_table (symbols)
3947 {}
3948
3949 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
3950
3951 /* Return the number of names in the symbol table. */
3952 size_t symbol_name_count () const override
3953 {
3954 return m_symbol_table.size ();
3955 }
3956
3957 /* Get the name of the symbol at IDX in the symbol table. */
3958 const char *symbol_name_at
3959 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
3960 {
3961 return m_symbol_table[idx];
3962 }
3963
3964 private:
3965 gdb::array_view<const char *> m_symbol_table;
3966 };
3967
3968 /* Convenience function that converts a NULL pointer to a "<null>"
3969 string, to pass to print routines. */
3970
3971 static const char *
3972 string_or_null (const char *str)
3973 {
3974 return str != NULL ? str : "<null>";
3975 }
3976
3977 /* Check if a lookup_name_info built from
3978 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3979 index. EXPECTED_LIST is the list of expected matches, in expected
3980 matching order. If no match expected, then an empty list is
3981 specified. Returns true on success. On failure prints a warning
3982 indicating the file:line that failed, and returns false. */
3983
3984 static bool
3985 check_match (const char *file, int line,
3986 mock_mapped_index &mock_index,
3987 const char *name, symbol_name_match_type match_type,
3988 bool completion_mode,
3989 std::initializer_list<const char *> expected_list,
3990 dwarf2_per_objfile *per_objfile)
3991 {
3992 lookup_name_info lookup_name (name, match_type, completion_mode);
3993
3994 bool matched = true;
3995
3996 auto mismatch = [&] (const char *expected_str,
3997 const char *got)
3998 {
3999 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4000 "expected=\"%s\", got=\"%s\"\n"),
4001 file, line,
4002 (match_type == symbol_name_match_type::FULL
4003 ? "FULL" : "WILD"),
4004 name, string_or_null (expected_str), string_or_null (got));
4005 matched = false;
4006 };
4007
4008 auto expected_it = expected_list.begin ();
4009 auto expected_end = expected_list.end ();
4010
4011 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
4012 nullptr,
4013 [&] (offset_type idx)
4014 {
4015 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
4016 const char *expected_str
4017 = expected_it == expected_end ? NULL : *expected_it++;
4018
4019 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4020 mismatch (expected_str, matched_name);
4021 return true;
4022 }, per_objfile);
4023
4024 const char *expected_str
4025 = expected_it == expected_end ? NULL : *expected_it++;
4026 if (expected_str != NULL)
4027 mismatch (expected_str, NULL);
4028
4029 return matched;
4030 }
4031
4032 /* The symbols added to the mock mapped_index for testing (in
4033 canonical form). */
4034 static const char *test_symbols[] = {
4035 "function",
4036 "std::bar",
4037 "std::zfunction",
4038 "std::zfunction2",
4039 "w1::w2",
4040 "ns::foo<char*>",
4041 "ns::foo<int>",
4042 "ns::foo<long>",
4043 "ns2::tmpl<int>::foo2",
4044 "(anonymous namespace)::A::B::C",
4045
4046 /* These are used to check that the increment-last-char in the
4047 matching algorithm for completion doesn't match "t1_fund" when
4048 completing "t1_func". */
4049 "t1_func",
4050 "t1_func1",
4051 "t1_fund",
4052 "t1_fund1",
4053
4054 /* A UTF-8 name with multi-byte sequences to make sure that
4055 cp-name-parser understands this as a single identifier ("função"
4056 is "function" in PT). */
4057 u8"u8função",
4058
4059 /* \377 (0xff) is Latin1 'ÿ'. */
4060 "yfunc\377",
4061
4062 /* \377 (0xff) is Latin1 'ÿ'. */
4063 "\377",
4064 "\377\377123",
4065
4066 /* A name with all sorts of complications. Starts with "z" to make
4067 it easier for the completion tests below. */
4068 #define Z_SYM_NAME \
4069 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4070 "::tuple<(anonymous namespace)::ui*, " \
4071 "std::default_delete<(anonymous namespace)::ui>, void>"
4072
4073 Z_SYM_NAME
4074 };
4075
4076 /* Returns true if the mapped_index_base::find_name_component_bounds
4077 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4078 in completion mode. */
4079
4080 static bool
4081 check_find_bounds_finds (mapped_index_base &index,
4082 const char *search_name,
4083 gdb::array_view<const char *> expected_syms,
4084 dwarf2_per_objfile *per_objfile)
4085 {
4086 lookup_name_info lookup_name (search_name,
4087 symbol_name_match_type::FULL, true);
4088
4089 auto bounds = index.find_name_components_bounds (lookup_name,
4090 language_cplus,
4091 per_objfile);
4092
4093 size_t distance = std::distance (bounds.first, bounds.second);
4094 if (distance != expected_syms.size ())
4095 return false;
4096
4097 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4098 {
4099 auto nc_elem = bounds.first + exp_elem;
4100 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
4101 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4102 return false;
4103 }
4104
4105 return true;
4106 }
4107
4108 /* Test the lower-level mapped_index::find_name_component_bounds
4109 method. */
4110
4111 static void
4112 test_mapped_index_find_name_component_bounds ()
4113 {
4114 mock_mapped_index mock_index (test_symbols);
4115
4116 mock_index.build_name_components (NULL /* per_objfile */);
4117
4118 /* Test the lower-level mapped_index::find_name_component_bounds
4119 method in completion mode. */
4120 {
4121 static const char *expected_syms[] = {
4122 "t1_func",
4123 "t1_func1",
4124 };
4125
4126 SELF_CHECK (check_find_bounds_finds
4127 (mock_index, "t1_func", expected_syms,
4128 NULL /* per_objfile */));
4129 }
4130
4131 /* Check that the increment-last-char in the name matching algorithm
4132 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4133 {
4134 static const char *expected_syms1[] = {
4135 "\377",
4136 "\377\377123",
4137 };
4138 SELF_CHECK (check_find_bounds_finds
4139 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
4140
4141 static const char *expected_syms2[] = {
4142 "\377\377123",
4143 };
4144 SELF_CHECK (check_find_bounds_finds
4145 (mock_index, "\377\377", expected_syms2,
4146 NULL /* per_objfile */));
4147 }
4148 }
4149
4150 /* Test dw2_expand_symtabs_matching_symbol. */
4151
4152 static void
4153 test_dw2_expand_symtabs_matching_symbol ()
4154 {
4155 mock_mapped_index mock_index (test_symbols);
4156
4157 /* We let all tests run until the end even if some fails, for debug
4158 convenience. */
4159 bool any_mismatch = false;
4160
4161 /* Create the expected symbols list (an initializer_list). Needed
4162 because lists have commas, and we need to pass them to CHECK,
4163 which is a macro. */
4164 #define EXPECT(...) { __VA_ARGS__ }
4165
4166 /* Wrapper for check_match that passes down the current
4167 __FILE__/__LINE__. */
4168 #define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4169 any_mismatch |= !check_match (__FILE__, __LINE__, \
4170 mock_index, \
4171 NAME, MATCH_TYPE, COMPLETION_MODE, \
4172 EXPECTED_LIST, NULL)
4173
4174 /* Identity checks. */
4175 for (const char *sym : test_symbols)
4176 {
4177 /* Should be able to match all existing symbols. */
4178 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4179 EXPECT (sym));
4180
4181 /* Should be able to match all existing symbols with
4182 parameters. */
4183 std::string with_params = std::string (sym) + "(int)";
4184 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4185 EXPECT (sym));
4186
4187 /* Should be able to match all existing symbols with
4188 parameters and qualifiers. */
4189 with_params = std::string (sym) + " ( int ) const";
4190 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4191 EXPECT (sym));
4192
4193 /* This should really find sym, but cp-name-parser.y doesn't
4194 know about lvalue/rvalue qualifiers yet. */
4195 with_params = std::string (sym) + " ( int ) &&";
4196 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4197 {});
4198 }
4199
4200 /* Check that the name matching algorithm for completion doesn't get
4201 confused with Latin1 'ÿ' / 0xff. */
4202 {
4203 static const char str[] = "\377";
4204 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4205 EXPECT ("\377", "\377\377123"));
4206 }
4207
4208 /* Check that the increment-last-char in the matching algorithm for
4209 completion doesn't match "t1_fund" when completing "t1_func". */
4210 {
4211 static const char str[] = "t1_func";
4212 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4213 EXPECT ("t1_func", "t1_func1"));
4214 }
4215
4216 /* Check that completion mode works at each prefix of the expected
4217 symbol name. */
4218 {
4219 static const char str[] = "function(int)";
4220 size_t len = strlen (str);
4221 std::string lookup;
4222
4223 for (size_t i = 1; i < len; i++)
4224 {
4225 lookup.assign (str, i);
4226 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4227 EXPECT ("function"));
4228 }
4229 }
4230
4231 /* While "w" is a prefix of both components, the match function
4232 should still only be called once. */
4233 {
4234 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4235 EXPECT ("w1::w2"));
4236 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4237 EXPECT ("w1::w2"));
4238 }
4239
4240 /* Same, with a "complicated" symbol. */
4241 {
4242 static const char str[] = Z_SYM_NAME;
4243 size_t len = strlen (str);
4244 std::string lookup;
4245
4246 for (size_t i = 1; i < len; i++)
4247 {
4248 lookup.assign (str, i);
4249 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4250 EXPECT (Z_SYM_NAME));
4251 }
4252 }
4253
4254 /* In FULL mode, an incomplete symbol doesn't match. */
4255 {
4256 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4257 {});
4258 }
4259
4260 /* A complete symbol with parameters matches any overload, since the
4261 index has no overload info. */
4262 {
4263 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4264 EXPECT ("std::zfunction", "std::zfunction2"));
4265 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4266 EXPECT ("std::zfunction", "std::zfunction2"));
4267 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4268 EXPECT ("std::zfunction", "std::zfunction2"));
4269 }
4270
4271 /* Check that whitespace is ignored appropriately. A symbol with a
4272 template argument list. */
4273 {
4274 static const char expected[] = "ns::foo<int>";
4275 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4276 EXPECT (expected));
4277 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4278 EXPECT (expected));
4279 }
4280
4281 /* Check that whitespace is ignored appropriately. A symbol with a
4282 template argument list that includes a pointer. */
4283 {
4284 static const char expected[] = "ns::foo<char*>";
4285 /* Try both completion and non-completion modes. */
4286 static const bool completion_mode[2] = {false, true};
4287 for (size_t i = 0; i < 2; i++)
4288 {
4289 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4290 completion_mode[i], EXPECT (expected));
4291 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4292 completion_mode[i], EXPECT (expected));
4293
4294 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4295 completion_mode[i], EXPECT (expected));
4296 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4297 completion_mode[i], EXPECT (expected));
4298 }
4299 }
4300
4301 {
4302 /* Check method qualifiers are ignored. */
4303 static const char expected[] = "ns::foo<char*>";
4304 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4305 symbol_name_match_type::FULL, true, EXPECT (expected));
4306 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4307 symbol_name_match_type::FULL, true, EXPECT (expected));
4308 CHECK_MATCH ("foo < char * > ( int ) const",
4309 symbol_name_match_type::WILD, true, EXPECT (expected));
4310 CHECK_MATCH ("foo < char * > ( int ) &&",
4311 symbol_name_match_type::WILD, true, EXPECT (expected));
4312 }
4313
4314 /* Test lookup names that don't match anything. */
4315 {
4316 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4317 {});
4318
4319 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4320 {});
4321 }
4322
4323 /* Some wild matching tests, exercising "(anonymous namespace)",
4324 which should not be confused with a parameter list. */
4325 {
4326 static const char *syms[] = {
4327 "A::B::C",
4328 "B::C",
4329 "C",
4330 "A :: B :: C ( int )",
4331 "B :: C ( int )",
4332 "C ( int )",
4333 };
4334
4335 for (const char *s : syms)
4336 {
4337 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4338 EXPECT ("(anonymous namespace)::A::B::C"));
4339 }
4340 }
4341
4342 {
4343 static const char expected[] = "ns2::tmpl<int>::foo2";
4344 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4345 EXPECT (expected));
4346 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4347 EXPECT (expected));
4348 }
4349
4350 SELF_CHECK (!any_mismatch);
4351
4352 #undef EXPECT
4353 #undef CHECK_MATCH
4354 }
4355
4356 static void
4357 run_test ()
4358 {
4359 test_mapped_index_find_name_component_bounds ();
4360 test_dw2_expand_symtabs_matching_symbol ();
4361 }
4362
4363 }} // namespace selftests::dw2_expand_symtabs_matching
4364
4365 #endif /* GDB_SELF_TEST */
4366
4367 /* If FILE_MATCHER is NULL or if PER_CU has
4368 dwarf2_per_cu_quick_data::MARK set (see
4369 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4370 EXPANSION_NOTIFY on it. */
4371
4372 static bool
4373 dw2_expand_symtabs_matching_one
4374 (dwarf2_per_cu_data *per_cu,
4375 dwarf2_per_objfile *per_objfile,
4376 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4377 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4378 {
4379 if (file_matcher == NULL || per_cu->v.quick->mark)
4380 {
4381 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4382
4383 compunit_symtab *symtab
4384 = dw2_instantiate_symtab (per_cu, per_objfile, false);
4385 gdb_assert (symtab != nullptr);
4386
4387 if (expansion_notify != NULL && symtab_was_null)
4388 return expansion_notify (symtab);
4389 }
4390 return true;
4391 }
4392
4393 /* Helper for dw2_expand_matching symtabs. Called on each symbol
4394 matched, to expand corresponding CUs that were marked. IDX is the
4395 index of the symbol name that matched. */
4396
4397 static bool
4398 dw2_expand_marked_cus
4399 (dwarf2_per_objfile *per_objfile, offset_type idx,
4400 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4401 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4402 block_search_flags search_flags,
4403 search_domain kind)
4404 {
4405 offset_type *vec, vec_len, vec_idx;
4406 bool global_seen = false;
4407 mapped_index &index = *per_objfile->per_bfd->index_table;
4408
4409 vec = (offset_type *) (index.constant_pool
4410 + MAYBE_SWAP (index.symbol_table[idx].vec));
4411 vec_len = MAYBE_SWAP (vec[0]);
4412 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4413 {
4414 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4415 /* This value is only valid for index versions >= 7. */
4416 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4417 gdb_index_symbol_kind symbol_kind =
4418 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4419 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4420 /* Only check the symbol attributes if they're present.
4421 Indices prior to version 7 don't record them,
4422 and indices >= 7 may elide them for certain symbols
4423 (gold does this). */
4424 int attrs_valid =
4425 (index.version >= 7
4426 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4427
4428 /* Work around gold/15646. */
4429 if (attrs_valid
4430 && !is_static
4431 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
4432 {
4433 if (global_seen)
4434 continue;
4435
4436 global_seen = true;
4437 }
4438
4439 /* Only check the symbol's kind if it has one. */
4440 if (attrs_valid)
4441 {
4442 if (is_static)
4443 {
4444 if ((search_flags & SEARCH_STATIC_BLOCK) == 0)
4445 continue;
4446 }
4447 else
4448 {
4449 if ((search_flags & SEARCH_GLOBAL_BLOCK) == 0)
4450 continue;
4451 }
4452
4453 switch (kind)
4454 {
4455 case VARIABLES_DOMAIN:
4456 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4457 continue;
4458 break;
4459 case FUNCTIONS_DOMAIN:
4460 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
4461 continue;
4462 break;
4463 case TYPES_DOMAIN:
4464 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4465 continue;
4466 break;
4467 case MODULES_DOMAIN:
4468 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4469 continue;
4470 break;
4471 default:
4472 break;
4473 }
4474 }
4475
4476 /* Don't crash on bad data. */
4477 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4478 + per_objfile->per_bfd->all_type_units.size ()))
4479 {
4480 complaint (_(".gdb_index entry has bad CU index"
4481 " [in module %s]"), objfile_name (per_objfile->objfile));
4482 continue;
4483 }
4484
4485 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4486 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4487 expansion_notify))
4488 return false;
4489 }
4490
4491 return true;
4492 }
4493
4494 /* If FILE_MATCHER is non-NULL, set all the
4495 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4496 that match FILE_MATCHER. */
4497
4498 static void
4499 dw_expand_symtabs_matching_file_matcher
4500 (dwarf2_per_objfile *per_objfile,
4501 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
4502 {
4503 if (file_matcher == NULL)
4504 return;
4505
4506 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4507 htab_eq_pointer,
4508 NULL, xcalloc, xfree));
4509 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
4510 htab_eq_pointer,
4511 NULL, xcalloc, xfree));
4512
4513 /* The rule is CUs specify all the files, including those used by
4514 any TU, so there's no need to scan TUs here. */
4515
4516 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4517 {
4518 QUIT;
4519
4520 per_cu->v.quick->mark = 0;
4521
4522 /* We only need to look at symtabs not already expanded. */
4523 if (per_objfile->symtab_set_p (per_cu))
4524 continue;
4525
4526 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
4527 if (file_data == NULL)
4528 continue;
4529
4530 if (htab_find (visited_not_found.get (), file_data) != NULL)
4531 continue;
4532 else if (htab_find (visited_found.get (), file_data) != NULL)
4533 {
4534 per_cu->v.quick->mark = 1;
4535 continue;
4536 }
4537
4538 for (int j = 0; j < file_data->num_file_names; ++j)
4539 {
4540 const char *this_real_name;
4541
4542 if (file_matcher (file_data->file_names[j], false))
4543 {
4544 per_cu->v.quick->mark = 1;
4545 break;
4546 }
4547
4548 /* Before we invoke realpath, which can get expensive when many
4549 files are involved, do a quick comparison of the basenames. */
4550 if (!basenames_may_differ
4551 && !file_matcher (lbasename (file_data->file_names[j]),
4552 true))
4553 continue;
4554
4555 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
4556 if (file_matcher (this_real_name, false))
4557 {
4558 per_cu->v.quick->mark = 1;
4559 break;
4560 }
4561 }
4562
4563 void **slot = htab_find_slot (per_cu->v.quick->mark
4564 ? visited_found.get ()
4565 : visited_not_found.get (),
4566 file_data, INSERT);
4567 *slot = file_data;
4568 }
4569 }
4570
4571 bool
4572 dwarf2_gdb_index::expand_symtabs_matching
4573 (struct objfile *objfile,
4574 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4575 const lookup_name_info *lookup_name,
4576 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4577 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4578 block_search_flags search_flags,
4579 domain_enum domain,
4580 enum search_domain kind)
4581 {
4582 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4583
4584 /* index_table is NULL if OBJF_READNOW. */
4585 if (!per_objfile->per_bfd->index_table)
4586 return true;
4587
4588 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
4589
4590 if (symbol_matcher == NULL && lookup_name == NULL)
4591 {
4592 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4593 {
4594 QUIT;
4595
4596 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
4597 file_matcher,
4598 expansion_notify))
4599 return false;
4600 }
4601 return true;
4602 }
4603
4604 mapped_index &index = *per_objfile->per_bfd->index_table;
4605
4606 bool result
4607 = dw2_expand_symtabs_matching_symbol (index, *lookup_name,
4608 symbol_matcher,
4609 [&] (offset_type idx)
4610 {
4611 if (!dw2_expand_marked_cus (per_objfile, idx, file_matcher,
4612 expansion_notify, search_flags, kind))
4613 return false;
4614 return true;
4615 }, per_objfile);
4616
4617 return result;
4618 }
4619
4620 /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4621 symtab. */
4622
4623 static struct compunit_symtab *
4624 recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4625 CORE_ADDR pc)
4626 {
4627 int i;
4628
4629 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4630 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4631 return cust;
4632
4633 if (cust->includes == NULL)
4634 return NULL;
4635
4636 for (i = 0; cust->includes[i]; ++i)
4637 {
4638 struct compunit_symtab *s = cust->includes[i];
4639
4640 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4641 if (s != NULL)
4642 return s;
4643 }
4644
4645 return NULL;
4646 }
4647
4648 struct compunit_symtab *
4649 dwarf2_base_index_functions::find_pc_sect_compunit_symtab
4650 (struct objfile *objfile,
4651 struct bound_minimal_symbol msymbol,
4652 CORE_ADDR pc,
4653 struct obj_section *section,
4654 int warn_if_readin)
4655 {
4656 struct dwarf2_per_cu_data *data;
4657 struct compunit_symtab *result;
4658
4659 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4660 if (per_objfile->per_bfd->index_addrmap == nullptr)
4661 return NULL;
4662
4663 CORE_ADDR baseaddr = objfile->text_section_offset ();
4664 data = ((struct dwarf2_per_cu_data *)
4665 addrmap_find (per_objfile->per_bfd->index_addrmap,
4666 pc - baseaddr));
4667 if (!data)
4668 return NULL;
4669
4670 if (warn_if_readin && per_objfile->symtab_set_p (data))
4671 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4672 paddress (objfile->arch (), pc));
4673
4674 result = recursively_find_pc_sect_compunit_symtab
4675 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4676
4677 gdb_assert (result != NULL);
4678 return result;
4679 }
4680
4681 void
4682 dwarf2_base_index_functions::map_symbol_filenames
4683 (struct objfile *objfile,
4684 gdb::function_view<symbol_filename_ftype> fun,
4685 bool need_fullname)
4686 {
4687 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4688
4689 if (!per_objfile->per_bfd->filenames_cache)
4690 {
4691 per_objfile->per_bfd->filenames_cache.emplace ();
4692
4693 htab_up visited (htab_create_alloc (10,
4694 htab_hash_pointer, htab_eq_pointer,
4695 NULL, xcalloc, xfree));
4696
4697 /* The rule is CUs specify all the files, including those used
4698 by any TU, so there's no need to scan TUs here. We can
4699 ignore file names coming from already-expanded CUs. */
4700
4701 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4702 {
4703 if (per_objfile->symtab_set_p (per_cu))
4704 {
4705 void **slot = htab_find_slot (visited.get (),
4706 per_cu->v.quick->file_names,
4707 INSERT);
4708
4709 *slot = per_cu->v.quick->file_names;
4710 }
4711 }
4712
4713 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
4714 {
4715 /* We only need to look at symtabs not already expanded. */
4716 if (per_objfile->symtab_set_p (per_cu))
4717 continue;
4718
4719 quick_file_names *file_data
4720 = dw2_get_file_names (per_cu, per_objfile);
4721 if (file_data == NULL)
4722 continue;
4723
4724 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
4725 if (*slot)
4726 {
4727 /* Already visited. */
4728 continue;
4729 }
4730 *slot = file_data;
4731
4732 for (int j = 0; j < file_data->num_file_names; ++j)
4733 {
4734 const char *filename = file_data->file_names[j];
4735 per_objfile->per_bfd->filenames_cache->seen (filename);
4736 }
4737 }
4738 }
4739
4740 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
4741 {
4742 gdb::unique_xmalloc_ptr<char> this_real_name;
4743
4744 if (need_fullname)
4745 this_real_name = gdb_realpath (filename);
4746 fun (filename, this_real_name.get ());
4747 });
4748 }
4749
4750 bool
4751 dwarf2_base_index_functions::has_symbols (struct objfile *objfile)
4752 {
4753 return true;
4754 }
4755
4756 /* DWARF-5 debug_names reader. */
4757
4758 /* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4759 static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4760
4761 /* A helper function that reads the .debug_names section in SECTION
4762 and fills in MAP. FILENAME is the name of the file containing the
4763 section; it is used for error reporting.
4764
4765 Returns true if all went well, false otherwise. */
4766
4767 static bool
4768 read_debug_names_from_section (struct objfile *objfile,
4769 const char *filename,
4770 struct dwarf2_section_info *section,
4771 mapped_debug_names &map)
4772 {
4773 if (section->empty ())
4774 return false;
4775
4776 /* Older elfutils strip versions could keep the section in the main
4777 executable while splitting it for the separate debug info file. */
4778 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4779 return false;
4780
4781 section->read (objfile);
4782
4783 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
4784
4785 const gdb_byte *addr = section->buffer;
4786
4787 bfd *const abfd = section->get_bfd_owner ();
4788
4789 unsigned int bytes_read;
4790 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4791 addr += bytes_read;
4792
4793 map.dwarf5_is_dwarf64 = bytes_read != 4;
4794 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4795 if (bytes_read + length != section->size)
4796 {
4797 /* There may be multiple per-CU indices. */
4798 warning (_("Section .debug_names in %s length %s does not match "
4799 "section length %s, ignoring .debug_names."),
4800 filename, plongest (bytes_read + length),
4801 pulongest (section->size));
4802 return false;
4803 }
4804
4805 /* The version number. */
4806 uint16_t version = read_2_bytes (abfd, addr);
4807 addr += 2;
4808 if (version != 5)
4809 {
4810 warning (_("Section .debug_names in %s has unsupported version %d, "
4811 "ignoring .debug_names."),
4812 filename, version);
4813 return false;
4814 }
4815
4816 /* Padding. */
4817 uint16_t padding = read_2_bytes (abfd, addr);
4818 addr += 2;
4819 if (padding != 0)
4820 {
4821 warning (_("Section .debug_names in %s has unsupported padding %d, "
4822 "ignoring .debug_names."),
4823 filename, padding);
4824 return false;
4825 }
4826
4827 /* comp_unit_count - The number of CUs in the CU list. */
4828 map.cu_count = read_4_bytes (abfd, addr);
4829 addr += 4;
4830
4831 /* local_type_unit_count - The number of TUs in the local TU
4832 list. */
4833 map.tu_count = read_4_bytes (abfd, addr);
4834 addr += 4;
4835
4836 /* foreign_type_unit_count - The number of TUs in the foreign TU
4837 list. */
4838 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4839 addr += 4;
4840 if (foreign_tu_count != 0)
4841 {
4842 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4843 "ignoring .debug_names."),
4844 filename, static_cast<unsigned long> (foreign_tu_count));
4845 return false;
4846 }
4847
4848 /* bucket_count - The number of hash buckets in the hash lookup
4849 table. */
4850 map.bucket_count = read_4_bytes (abfd, addr);
4851 addr += 4;
4852
4853 /* name_count - The number of unique names in the index. */
4854 map.name_count = read_4_bytes (abfd, addr);
4855 addr += 4;
4856
4857 /* abbrev_table_size - The size in bytes of the abbreviations
4858 table. */
4859 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4860 addr += 4;
4861
4862 /* augmentation_string_size - The size in bytes of the augmentation
4863 string. This value is rounded up to a multiple of 4. */
4864 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4865 addr += 4;
4866 map.augmentation_is_gdb = ((augmentation_string_size
4867 == sizeof (dwarf5_augmentation))
4868 && memcmp (addr, dwarf5_augmentation,
4869 sizeof (dwarf5_augmentation)) == 0);
4870 augmentation_string_size += (-augmentation_string_size) & 3;
4871 addr += augmentation_string_size;
4872
4873 /* List of CUs */
4874 map.cu_table_reordered = addr;
4875 addr += map.cu_count * map.offset_size;
4876
4877 /* List of Local TUs */
4878 map.tu_table_reordered = addr;
4879 addr += map.tu_count * map.offset_size;
4880
4881 /* Hash Lookup Table */
4882 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4883 addr += map.bucket_count * 4;
4884 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4885 addr += map.name_count * 4;
4886
4887 /* Name Table */
4888 map.name_table_string_offs_reordered = addr;
4889 addr += map.name_count * map.offset_size;
4890 map.name_table_entry_offs_reordered = addr;
4891 addr += map.name_count * map.offset_size;
4892
4893 const gdb_byte *abbrev_table_start = addr;
4894 for (;;)
4895 {
4896 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4897 addr += bytes_read;
4898 if (index_num == 0)
4899 break;
4900
4901 const auto insertpair
4902 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4903 if (!insertpair.second)
4904 {
4905 warning (_("Section .debug_names in %s has duplicate index %s, "
4906 "ignoring .debug_names."),
4907 filename, pulongest (index_num));
4908 return false;
4909 }
4910 mapped_debug_names::index_val &indexval = insertpair.first->second;
4911 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4912 addr += bytes_read;
4913
4914 for (;;)
4915 {
4916 mapped_debug_names::index_val::attr attr;
4917 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4918 addr += bytes_read;
4919 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4920 addr += bytes_read;
4921 if (attr.form == DW_FORM_implicit_const)
4922 {
4923 attr.implicit_const = read_signed_leb128 (abfd, addr,
4924 &bytes_read);
4925 addr += bytes_read;
4926 }
4927 if (attr.dw_idx == 0 && attr.form == 0)
4928 break;
4929 indexval.attr_vec.push_back (std::move (attr));
4930 }
4931 }
4932 if (addr != abbrev_table_start + abbrev_table_size)
4933 {
4934 warning (_("Section .debug_names in %s has abbreviation_table "
4935 "of size %s vs. written as %u, ignoring .debug_names."),
4936 filename, plongest (addr - abbrev_table_start),
4937 abbrev_table_size);
4938 return false;
4939 }
4940 map.entry_pool = addr;
4941
4942 return true;
4943 }
4944
4945 /* A helper for create_cus_from_debug_names that handles the MAP's CU
4946 list. */
4947
4948 static void
4949 create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
4950 const mapped_debug_names &map,
4951 dwarf2_section_info &section,
4952 bool is_dwz)
4953 {
4954 if (!map.augmentation_is_gdb)
4955 {
4956 for (uint32_t i = 0; i < map.cu_count; ++i)
4957 {
4958 sect_offset sect_off
4959 = (sect_offset) (extract_unsigned_integer
4960 (map.cu_table_reordered + i * map.offset_size,
4961 map.offset_size,
4962 map.dwarf5_byte_order));
4963 /* We don't know the length of the CU, because the CU list in a
4964 .debug_names index can be incomplete, so we can't use the start
4965 of the next CU as end of this CU. We create the CUs here with
4966 length 0, and in cutu_reader::cutu_reader we'll fill in the
4967 actual length. */
4968 dwarf2_per_cu_data *per_cu
4969 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4970 sect_off, 0);
4971 per_bfd->all_comp_units.push_back (per_cu);
4972 }
4973 return;
4974 }
4975
4976 sect_offset sect_off_prev;
4977 for (uint32_t i = 0; i <= map.cu_count; ++i)
4978 {
4979 sect_offset sect_off_next;
4980 if (i < map.cu_count)
4981 {
4982 sect_off_next
4983 = (sect_offset) (extract_unsigned_integer
4984 (map.cu_table_reordered + i * map.offset_size,
4985 map.offset_size,
4986 map.dwarf5_byte_order));
4987 }
4988 else
4989 sect_off_next = (sect_offset) section.size;
4990 if (i >= 1)
4991 {
4992 const ULONGEST length = sect_off_next - sect_off_prev;
4993 dwarf2_per_cu_data *per_cu
4994 = create_cu_from_index_list (per_bfd, &section, is_dwz,
4995 sect_off_prev, length);
4996 per_bfd->all_comp_units.push_back (per_cu);
4997 }
4998 sect_off_prev = sect_off_next;
4999 }
5000 }
5001
5002 /* Read the CU list from the mapped index, and use it to create all
5003 the CU objects for this dwarf2_per_objfile. */
5004
5005 static void
5006 create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
5007 const mapped_debug_names &map,
5008 const mapped_debug_names &dwz_map)
5009 {
5010 gdb_assert (per_bfd->all_comp_units.empty ());
5011 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
5012
5013 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
5014 false /* is_dwz */);
5015
5016 if (dwz_map.cu_count == 0)
5017 return;
5018
5019 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5020 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
5021 true /* is_dwz */);
5022 }
5023
5024 /* Read .debug_names. If everything went ok, initialize the "quick"
5025 elements of all the CUs and return true. Otherwise, return false. */
5026
5027 static bool
5028 dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
5029 {
5030 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5031 mapped_debug_names dwz_map;
5032 struct objfile *objfile = per_objfile->objfile;
5033 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5034
5035 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5036 &per_bfd->debug_names, *map))
5037 return false;
5038
5039 /* Don't use the index if it's empty. */
5040 if (map->name_count == 0)
5041 return false;
5042
5043 /* If there is a .dwz file, read it so we can get its CU list as
5044 well. */
5045 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5046 if (dwz != NULL)
5047 {
5048 if (!read_debug_names_from_section (objfile,
5049 bfd_get_filename (dwz->dwz_bfd.get ()),
5050 &dwz->debug_names, dwz_map))
5051 {
5052 warning (_("could not read '.debug_names' section from %s; skipping"),
5053 bfd_get_filename (dwz->dwz_bfd.get ()));
5054 return false;
5055 }
5056 }
5057
5058 create_cus_from_debug_names (per_bfd, *map, dwz_map);
5059
5060 if (map->tu_count != 0)
5061 {
5062 /* We can only handle a single .debug_types when we have an
5063 index. */
5064 if (per_bfd->types.size () != 1)
5065 return false;
5066
5067 dwarf2_section_info *section = &per_bfd->types[0];
5068
5069 create_signatured_type_table_from_debug_names
5070 (per_objfile, *map, section, &per_bfd->abbrev);
5071 }
5072
5073 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
5074
5075 per_bfd->debug_names_table = std::move (map);
5076 per_bfd->using_index = 1;
5077 per_bfd->quick_file_names_table =
5078 create_quick_file_names_table (per_bfd->all_comp_units.size ());
5079
5080 return true;
5081 }
5082
5083 /* Type used to manage iterating over all CUs looking for a symbol for
5084 .debug_names. */
5085
5086 class dw2_debug_names_iterator
5087 {
5088 public:
5089 dw2_debug_names_iterator (const mapped_debug_names &map,
5090 block_search_flags block_index,
5091 domain_enum domain,
5092 const char *name, dwarf2_per_objfile *per_objfile)
5093 : m_map (map), m_block_index (block_index), m_domain (domain),
5094 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5095 m_per_objfile (per_objfile)
5096 {}
5097
5098 dw2_debug_names_iterator (const mapped_debug_names &map,
5099 search_domain search, uint32_t namei,
5100 dwarf2_per_objfile *per_objfile,
5101 domain_enum domain = UNDEF_DOMAIN)
5102 : m_map (map),
5103 m_domain (domain),
5104 m_search (search),
5105 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5106 m_per_objfile (per_objfile)
5107 {}
5108
5109 dw2_debug_names_iterator (const mapped_debug_names &map,
5110 block_search_flags block_index, domain_enum domain,
5111 uint32_t namei, dwarf2_per_objfile *per_objfile)
5112 : m_map (map), m_block_index (block_index), m_domain (domain),
5113 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5114 m_per_objfile (per_objfile)
5115 {}
5116
5117 /* Return the next matching CU or NULL if there are no more. */
5118 dwarf2_per_cu_data *next ();
5119
5120 private:
5121 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5122 const char *name,
5123 dwarf2_per_objfile *per_objfile);
5124 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5125 uint32_t namei,
5126 dwarf2_per_objfile *per_objfile);
5127
5128 /* The internalized form of .debug_names. */
5129 const mapped_debug_names &m_map;
5130
5131 /* Restrict the search to these blocks. */
5132 block_search_flags m_block_index = (SEARCH_GLOBAL_BLOCK
5133 | SEARCH_STATIC_BLOCK);
5134
5135 /* The kind of symbol we're looking for. */
5136 const domain_enum m_domain = UNDEF_DOMAIN;
5137 const search_domain m_search = ALL_DOMAIN;
5138
5139 /* The list of CUs from the index entry of the symbol, or NULL if
5140 not found. */
5141 const gdb_byte *m_addr;
5142
5143 dwarf2_per_objfile *m_per_objfile;
5144 };
5145
5146 const char *
5147 mapped_debug_names::namei_to_name
5148 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
5149 {
5150 const ULONGEST namei_string_offs
5151 = extract_unsigned_integer ((name_table_string_offs_reordered
5152 + namei * offset_size),
5153 offset_size,
5154 dwarf5_byte_order);
5155 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
5156 }
5157
5158 /* Find a slot in .debug_names for the object named NAME. If NAME is
5159 found, return pointer to its pool data. If NAME cannot be found,
5160 return NULL. */
5161
5162 const gdb_byte *
5163 dw2_debug_names_iterator::find_vec_in_debug_names
5164 (const mapped_debug_names &map, const char *name,
5165 dwarf2_per_objfile *per_objfile)
5166 {
5167 int (*cmp) (const char *, const char *);
5168
5169 gdb::unique_xmalloc_ptr<char> without_params;
5170 if (current_language->la_language == language_cplus
5171 || current_language->la_language == language_fortran
5172 || current_language->la_language == language_d)
5173 {
5174 /* NAME is already canonical. Drop any qualifiers as
5175 .debug_names does not contain any. */
5176
5177 if (strchr (name, '(') != NULL)
5178 {
5179 without_params = cp_remove_params (name);
5180 if (without_params != NULL)
5181 name = without_params.get ();
5182 }
5183 }
5184
5185 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5186
5187 const uint32_t full_hash = dwarf5_djb_hash (name);
5188 uint32_t namei
5189 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5190 (map.bucket_table_reordered
5191 + (full_hash % map.bucket_count)), 4,
5192 map.dwarf5_byte_order);
5193 if (namei == 0)
5194 return NULL;
5195 --namei;
5196 if (namei >= map.name_count)
5197 {
5198 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5199 "[in module %s]"),
5200 namei, map.name_count,
5201 objfile_name (per_objfile->objfile));
5202 return NULL;
5203 }
5204
5205 for (;;)
5206 {
5207 const uint32_t namei_full_hash
5208 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5209 (map.hash_table_reordered + namei), 4,
5210 map.dwarf5_byte_order);
5211 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5212 return NULL;
5213
5214 if (full_hash == namei_full_hash)
5215 {
5216 const char *const namei_string = map.namei_to_name (namei, per_objfile);
5217
5218 #if 0 /* An expensive sanity check. */
5219 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5220 {
5221 complaint (_("Wrong .debug_names hash for string at index %u "
5222 "[in module %s]"),
5223 namei, objfile_name (dwarf2_per_objfile->objfile));
5224 return NULL;
5225 }
5226 #endif
5227
5228 if (cmp (namei_string, name) == 0)
5229 {
5230 const ULONGEST namei_entry_offs
5231 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5232 + namei * map.offset_size),
5233 map.offset_size, map.dwarf5_byte_order);
5234 return map.entry_pool + namei_entry_offs;
5235 }
5236 }
5237
5238 ++namei;
5239 if (namei >= map.name_count)
5240 return NULL;
5241 }
5242 }
5243
5244 const gdb_byte *
5245 dw2_debug_names_iterator::find_vec_in_debug_names
5246 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
5247 {
5248 if (namei >= map.name_count)
5249 {
5250 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
5251 "[in module %s]"),
5252 namei, map.name_count,
5253 objfile_name (per_objfile->objfile));
5254 return NULL;
5255 }
5256
5257 const ULONGEST namei_entry_offs
5258 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5259 + namei * map.offset_size),
5260 map.offset_size, map.dwarf5_byte_order);
5261 return map.entry_pool + namei_entry_offs;
5262 }
5263
5264 /* See dw2_debug_names_iterator. */
5265
5266 dwarf2_per_cu_data *
5267 dw2_debug_names_iterator::next ()
5268 {
5269 if (m_addr == NULL)
5270 return NULL;
5271
5272 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5273 struct objfile *objfile = m_per_objfile->objfile;
5274 bfd *const abfd = objfile->obfd;
5275
5276 again:
5277
5278 unsigned int bytes_read;
5279 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5280 m_addr += bytes_read;
5281 if (abbrev == 0)
5282 return NULL;
5283
5284 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5285 if (indexval_it == m_map.abbrev_map.cend ())
5286 {
5287 complaint (_("Wrong .debug_names undefined abbrev code %s "
5288 "[in module %s]"),
5289 pulongest (abbrev), objfile_name (objfile));
5290 return NULL;
5291 }
5292 const mapped_debug_names::index_val &indexval = indexval_it->second;
5293 enum class symbol_linkage {
5294 unknown,
5295 static_,
5296 extern_,
5297 } symbol_linkage_ = symbol_linkage::unknown;
5298 dwarf2_per_cu_data *per_cu = NULL;
5299 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5300 {
5301 ULONGEST ull;
5302 switch (attr.form)
5303 {
5304 case DW_FORM_implicit_const:
5305 ull = attr.implicit_const;
5306 break;
5307 case DW_FORM_flag_present:
5308 ull = 1;
5309 break;
5310 case DW_FORM_udata:
5311 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5312 m_addr += bytes_read;
5313 break;
5314 case DW_FORM_ref4:
5315 ull = read_4_bytes (abfd, m_addr);
5316 m_addr += 4;
5317 break;
5318 case DW_FORM_ref8:
5319 ull = read_8_bytes (abfd, m_addr);
5320 m_addr += 8;
5321 break;
5322 case DW_FORM_ref_sig8:
5323 ull = read_8_bytes (abfd, m_addr);
5324 m_addr += 8;
5325 break;
5326 default:
5327 complaint (_("Unsupported .debug_names form %s [in module %s]"),
5328 dwarf_form_name (attr.form),
5329 objfile_name (objfile));
5330 return NULL;
5331 }
5332 switch (attr.dw_idx)
5333 {
5334 case DW_IDX_compile_unit:
5335 /* Don't crash on bad data. */
5336 if (ull >= per_bfd->all_comp_units.size ())
5337 {
5338 complaint (_(".debug_names entry has bad CU index %s"
5339 " [in module %s]"),
5340 pulongest (ull),
5341 objfile_name (objfile));
5342 continue;
5343 }
5344 per_cu = per_bfd->get_cutu (ull);
5345 break;
5346 case DW_IDX_type_unit:
5347 /* Don't crash on bad data. */
5348 if (ull >= per_bfd->all_type_units.size ())
5349 {
5350 complaint (_(".debug_names entry has bad TU index %s"
5351 " [in module %s]"),
5352 pulongest (ull),
5353 objfile_name (objfile));
5354 continue;
5355 }
5356 per_cu = &per_bfd->get_tu (ull)->per_cu;
5357 break;
5358 case DW_IDX_die_offset:
5359 /* In a per-CU index (as opposed to a per-module index), index
5360 entries without CU attribute implicitly refer to the single CU. */
5361 if (per_cu == NULL)
5362 per_cu = per_bfd->get_cu (0);
5363 break;
5364 case DW_IDX_GNU_internal:
5365 if (!m_map.augmentation_is_gdb)
5366 break;
5367 symbol_linkage_ = symbol_linkage::static_;
5368 break;
5369 case DW_IDX_GNU_external:
5370 if (!m_map.augmentation_is_gdb)
5371 break;
5372 symbol_linkage_ = symbol_linkage::extern_;
5373 break;
5374 }
5375 }
5376
5377 /* Skip if already read in. */
5378 if (m_per_objfile->symtab_set_p (per_cu))
5379 goto again;
5380
5381 /* Check static vs global. */
5382 if (symbol_linkage_ != symbol_linkage::unknown)
5383 {
5384 if (symbol_linkage_ == symbol_linkage::static_)
5385 {
5386 if ((m_block_index & SEARCH_STATIC_BLOCK) == 0)
5387 goto again;
5388 }
5389 else
5390 {
5391 if ((m_block_index & SEARCH_GLOBAL_BLOCK) == 0)
5392 goto again;
5393 }
5394 }
5395
5396 /* Match dw2_symtab_iter_next, symbol_kind
5397 and debug_names::psymbol_tag. */
5398 switch (m_domain)
5399 {
5400 case VAR_DOMAIN:
5401 switch (indexval.dwarf_tag)
5402 {
5403 case DW_TAG_variable:
5404 case DW_TAG_subprogram:
5405 /* Some types are also in VAR_DOMAIN. */
5406 case DW_TAG_typedef:
5407 case DW_TAG_structure_type:
5408 break;
5409 default:
5410 goto again;
5411 }
5412 break;
5413 case STRUCT_DOMAIN:
5414 switch (indexval.dwarf_tag)
5415 {
5416 case DW_TAG_typedef:
5417 case DW_TAG_structure_type:
5418 break;
5419 default:
5420 goto again;
5421 }
5422 break;
5423 case LABEL_DOMAIN:
5424 switch (indexval.dwarf_tag)
5425 {
5426 case 0:
5427 case DW_TAG_variable:
5428 break;
5429 default:
5430 goto again;
5431 }
5432 break;
5433 case MODULE_DOMAIN:
5434 switch (indexval.dwarf_tag)
5435 {
5436 case DW_TAG_module:
5437 break;
5438 default:
5439 goto again;
5440 }
5441 break;
5442 default:
5443 break;
5444 }
5445
5446 /* Match dw2_expand_symtabs_matching, symbol_kind and
5447 debug_names::psymbol_tag. */
5448 switch (m_search)
5449 {
5450 case VARIABLES_DOMAIN:
5451 switch (indexval.dwarf_tag)
5452 {
5453 case DW_TAG_variable:
5454 break;
5455 default:
5456 goto again;
5457 }
5458 break;
5459 case FUNCTIONS_DOMAIN:
5460 switch (indexval.dwarf_tag)
5461 {
5462 case DW_TAG_subprogram:
5463 break;
5464 default:
5465 goto again;
5466 }
5467 break;
5468 case TYPES_DOMAIN:
5469 switch (indexval.dwarf_tag)
5470 {
5471 case DW_TAG_typedef:
5472 case DW_TAG_structure_type:
5473 break;
5474 default:
5475 goto again;
5476 }
5477 break;
5478 case MODULES_DOMAIN:
5479 switch (indexval.dwarf_tag)
5480 {
5481 case DW_TAG_module:
5482 break;
5483 default:
5484 goto again;
5485 }
5486 default:
5487 break;
5488 }
5489
5490 return per_cu;
5491 }
5492
5493 /* This dumps minimal information about .debug_names. It is called
5494 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5495 uses this to verify that .debug_names has been loaded. */
5496
5497 void
5498 dwarf2_debug_names_index::dump (struct objfile *objfile)
5499 {
5500 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5501
5502 gdb_assert (per_objfile->per_bfd->using_index);
5503 printf_filtered (".debug_names:");
5504 if (per_objfile->per_bfd->debug_names_table)
5505 printf_filtered (" exists\n");
5506 else
5507 printf_filtered (" faked for \"readnow\"\n");
5508 printf_filtered ("\n");
5509 }
5510
5511 void
5512 dwarf2_debug_names_index::map_matching_symbols
5513 (struct objfile *objfile,
5514 const lookup_name_info &name, domain_enum domain,
5515 int global,
5516 gdb::function_view<symbol_found_callback_ftype> callback,
5517 symbol_compare_ftype *ordered_compare)
5518 {
5519 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5520
5521 /* debug_names_table is NULL if OBJF_READNOW. */
5522 if (!per_objfile->per_bfd->debug_names_table)
5523 return;
5524
5525 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5526 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5527 const block_search_flags block_flags
5528 = global ? SEARCH_GLOBAL_BLOCK : SEARCH_STATIC_BLOCK;
5529
5530 const char *match_name = name.ada ().lookup_name ().c_str ();
5531 auto matcher = [&] (const char *symname)
5532 {
5533 if (ordered_compare == nullptr)
5534 return true;
5535 return ordered_compare (symname, match_name) == 0;
5536 };
5537
5538 dw2_expand_symtabs_matching_symbol (map, name, matcher,
5539 [&] (offset_type namei)
5540 {
5541 /* The name was matched, now expand corresponding CUs that were
5542 marked. */
5543 dw2_debug_names_iterator iter (map, block_flags, domain, namei,
5544 per_objfile);
5545
5546 struct dwarf2_per_cu_data *per_cu;
5547 while ((per_cu = iter.next ()) != NULL)
5548 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
5549 nullptr);
5550 return true;
5551 }, per_objfile);
5552
5553 /* It's a shame we couldn't do this inside the
5554 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5555 that have already been expanded. Instead, this loop matches what
5556 the psymtab code does. */
5557 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5558 {
5559 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
5560 if (symtab != nullptr)
5561 {
5562 const struct block *block
5563 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
5564 if (!iterate_over_symbols_terminated (block, name,
5565 domain, callback))
5566 break;
5567 }
5568 }
5569 }
5570
5571 bool
5572 dwarf2_debug_names_index::expand_symtabs_matching
5573 (struct objfile *objfile,
5574 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5575 const lookup_name_info *lookup_name,
5576 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5577 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5578 block_search_flags search_flags,
5579 domain_enum domain,
5580 enum search_domain kind)
5581 {
5582 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5583
5584 /* debug_names_table is NULL if OBJF_READNOW. */
5585 if (!per_objfile->per_bfd->debug_names_table)
5586 return true;
5587
5588 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
5589
5590 if (symbol_matcher == NULL && lookup_name == NULL)
5591 {
5592 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
5593 {
5594 QUIT;
5595
5596 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5597 file_matcher,
5598 expansion_notify))
5599 return false;
5600 }
5601 return true;
5602 }
5603
5604 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
5605
5606 bool result
5607 = dw2_expand_symtabs_matching_symbol (map, *lookup_name,
5608 symbol_matcher,
5609 [&] (offset_type namei)
5610 {
5611 /* The name was matched, now expand corresponding CUs that were
5612 marked. */
5613 dw2_debug_names_iterator iter (map, kind, namei, per_objfile, domain);
5614
5615 struct dwarf2_per_cu_data *per_cu;
5616 while ((per_cu = iter.next ()) != NULL)
5617 if (!dw2_expand_symtabs_matching_one (per_cu, per_objfile,
5618 file_matcher,
5619 expansion_notify))
5620 return false;
5621 return true;
5622 }, per_objfile);
5623
5624 return result;
5625 }
5626
5627 /* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5628 to either a dwarf2_per_bfd or dwz_file object. */
5629
5630 template <typename T>
5631 static gdb::array_view<const gdb_byte>
5632 get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5633 {
5634 dwarf2_section_info *section = &section_owner->gdb_index;
5635
5636 if (section->empty ())
5637 return {};
5638
5639 /* Older elfutils strip versions could keep the section in the main
5640 executable while splitting it for the separate debug info file. */
5641 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
5642 return {};
5643
5644 section->read (obj);
5645
5646 /* dwarf2_section_info::size is a bfd_size_type, while
5647 gdb::array_view works with size_t. On 32-bit hosts, with
5648 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5649 is 32-bit. So we need an explicit narrowing conversion here.
5650 This is fine, because it's impossible to allocate or mmap an
5651 array/buffer larger than what size_t can represent. */
5652 return gdb::make_array_view (section->buffer, section->size);
5653 }
5654
5655 /* Lookup the index cache for the contents of the index associated to
5656 DWARF2_OBJ. */
5657
5658 static gdb::array_view<const gdb_byte>
5659 get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
5660 {
5661 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5662 if (build_id == nullptr)
5663 return {};
5664
5665 return global_index_cache.lookup_gdb_index (build_id,
5666 &dwarf2_per_bfd->index_cache_res);
5667 }
5668
5669 /* Same as the above, but for DWZ. */
5670
5671 static gdb::array_view<const gdb_byte>
5672 get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5673 {
5674 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5675 if (build_id == nullptr)
5676 return {};
5677
5678 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5679 }
5680
5681 /* See dwarf2/public.h. */
5682
5683 void
5684 dwarf2_initialize_objfile (struct objfile *objfile)
5685 {
5686 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5687 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5688
5689 dwarf_read_debug_printf ("called");
5690
5691 /* If we're about to read full symbols, don't bother with the
5692 indices. In this case we also don't care if some other debug
5693 format is making psymtabs, because they are all about to be
5694 expanded anyway. */
5695 if ((objfile->flags & OBJF_READNOW))
5696 {
5697 dwarf_read_debug_printf ("readnow requested");
5698
5699 /* When using READNOW, the using_index flag (set below) indicates that
5700 PER_BFD was already initialized, when we loaded some other objfile. */
5701 if (per_bfd->using_index)
5702 {
5703 dwarf_read_debug_printf ("using_index already set");
5704 per_objfile->resize_symtabs ();
5705 objfile->qf.push_front (make_dwarf_gdb_index ());
5706 return;
5707 }
5708
5709 per_bfd->using_index = 1;
5710 create_all_comp_units (per_objfile);
5711 create_all_type_units (per_objfile);
5712 per_bfd->quick_file_names_table
5713 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
5714 per_objfile->resize_symtabs ();
5715
5716 for (int i = 0; i < (per_bfd->all_comp_units.size ()
5717 + per_bfd->all_type_units.size ()); ++i)
5718 {
5719 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
5720
5721 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
5722 struct dwarf2_per_cu_quick_data);
5723 }
5724
5725 /* Arrange for gdb to see the "quick" functions. However, these
5726 functions will be no-ops because we will have expanded all
5727 symtabs. */
5728 objfile->qf.push_front (make_dwarf_gdb_index ());
5729 return;
5730 }
5731
5732 /* Was a debug names index already read when we processed an objfile sharing
5733 PER_BFD? */
5734 if (per_bfd->debug_names_table != nullptr)
5735 {
5736 dwarf_read_debug_printf ("re-using shared debug names table");
5737 per_objfile->resize_symtabs ();
5738 objfile->qf.push_front (make_dwarf_debug_names ());
5739 return;
5740 }
5741
5742 /* Was a GDB index already read when we processed an objfile sharing
5743 PER_BFD? */
5744 if (per_bfd->index_table != nullptr)
5745 {
5746 dwarf_read_debug_printf ("re-using shared index table");
5747 per_objfile->resize_symtabs ();
5748 objfile->qf.push_front (make_dwarf_gdb_index ());
5749 return;
5750 }
5751
5752 /* There might already be partial symtabs built for this BFD. This happens
5753 when loading the same binary twice with the index-cache enabled. If so,
5754 don't try to read an index. The objfile / per_objfile initialization will
5755 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
5756 code path. */
5757 if (per_bfd->partial_symtabs != nullptr)
5758 {
5759 dwarf_read_debug_printf ("re-using shared partial symtabs");
5760 objfile->qf.push_front (make_lazy_dwarf_reader ());
5761 return;
5762 }
5763
5764 if (dwarf2_read_debug_names (per_objfile))
5765 {
5766 dwarf_read_debug_printf ("found debug names");
5767 per_objfile->resize_symtabs ();
5768 objfile->qf.push_front (make_dwarf_debug_names ());
5769 return;
5770 }
5771
5772 if (dwarf2_read_gdb_index (per_objfile,
5773 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
5774 get_gdb_index_contents_from_section<dwz_file>))
5775 {
5776 dwarf_read_debug_printf ("found gdb index from file");
5777 per_objfile->resize_symtabs ();
5778 objfile->qf.push_front (make_dwarf_gdb_index ());
5779 return;
5780 }
5781
5782 /* ... otherwise, try to find the index in the index cache. */
5783 if (dwarf2_read_gdb_index (per_objfile,
5784 get_gdb_index_contents_from_cache,
5785 get_gdb_index_contents_from_cache_dwz))
5786 {
5787 dwarf_read_debug_printf ("found gdb index from cache");
5788 global_index_cache.hit ();
5789 per_objfile->resize_symtabs ();
5790 objfile->qf.push_front (make_dwarf_gdb_index ());
5791 return;
5792 }
5793
5794 global_index_cache.miss ();
5795 objfile->qf.push_front (make_lazy_dwarf_reader ());
5796 }
5797
5798 \f
5799
5800 /* Build a partial symbol table. */
5801
5802 void
5803 dwarf2_build_psymtabs (struct objfile *objfile, psymbol_functions *psf)
5804 {
5805 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5806 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
5807
5808 if (per_bfd->partial_symtabs != nullptr)
5809 {
5810 /* Partial symbols were already read, so now we can simply
5811 attach them. */
5812 if (psf == nullptr)
5813 {
5814 psf = new psymbol_functions (per_bfd->partial_symtabs);
5815 objfile->qf.emplace_front (psf);
5816 }
5817 else
5818 psf->set_partial_symtabs (per_bfd->partial_symtabs);
5819 per_objfile->resize_symtabs ();
5820 return;
5821 }
5822
5823 if (psf == nullptr)
5824 {
5825 psf = new psymbol_functions;
5826 objfile->qf.emplace_front (psf);
5827 }
5828 const std::shared_ptr<psymtab_storage> &partial_symtabs
5829 = psf->get_partial_symtabs ();
5830
5831 /* Set the local reference to partial symtabs, so that we don't try
5832 to read them again if reading another objfile with the same BFD.
5833 If we can't in fact share, this won't make a difference anyway as
5834 the dwarf2_per_bfd object won't be shared. */
5835 per_bfd->partial_symtabs = partial_symtabs;
5836
5837 try
5838 {
5839 /* This isn't really ideal: all the data we allocate on the
5840 objfile's obstack is still uselessly kept around. However,
5841 freeing it seems unsafe. */
5842 psymtab_discarder psymtabs (partial_symtabs.get ());
5843 dwarf2_build_psymtabs_hard (per_objfile);
5844 psymtabs.keep ();
5845
5846 per_objfile->resize_symtabs ();
5847
5848 /* (maybe) store an index in the cache. */
5849 global_index_cache.store (per_objfile);
5850 }
5851 catch (const gdb_exception_error &except)
5852 {
5853 exception_print (gdb_stderr, except);
5854 }
5855 }
5856
5857 /* Find the base address of the compilation unit for range lists and
5858 location lists. It will normally be specified by DW_AT_low_pc.
5859 In DWARF-3 draft 4, the base address could be overridden by
5860 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5861 compilation units with discontinuous ranges. */
5862
5863 static void
5864 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5865 {
5866 struct attribute *attr;
5867
5868 cu->base_address.reset ();
5869
5870 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
5871 if (attr != nullptr)
5872 cu->base_address = attr->as_address ();
5873 else
5874 {
5875 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5876 if (attr != nullptr)
5877 cu->base_address = attr->as_address ();
5878 }
5879 }
5880
5881 /* Helper function that returns the proper abbrev section for
5882 THIS_CU. */
5883
5884 static struct dwarf2_section_info *
5885 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5886 {
5887 struct dwarf2_section_info *abbrev;
5888 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
5889
5890 if (this_cu->is_dwz)
5891 abbrev = &dwarf2_get_dwz_file (per_bfd, true)->abbrev;
5892 else
5893 abbrev = &per_bfd->abbrev;
5894
5895 return abbrev;
5896 }
5897
5898 /* Fetch the abbreviation table offset from a comp or type unit header. */
5899
5900 static sect_offset
5901 read_abbrev_offset (dwarf2_per_objfile *per_objfile,
5902 struct dwarf2_section_info *section,
5903 sect_offset sect_off)
5904 {
5905 bfd *abfd = section->get_bfd_owner ();
5906 const gdb_byte *info_ptr;
5907 unsigned int initial_length_size, offset_size;
5908 uint16_t version;
5909
5910 section->read (per_objfile->objfile);
5911 info_ptr = section->buffer + to_underlying (sect_off);
5912 read_initial_length (abfd, info_ptr, &initial_length_size);
5913 offset_size = initial_length_size == 4 ? 4 : 8;
5914 info_ptr += initial_length_size;
5915
5916 version = read_2_bytes (abfd, info_ptr);
5917 info_ptr += 2;
5918 if (version >= 5)
5919 {
5920 /* Skip unit type and address size. */
5921 info_ptr += 2;
5922 }
5923
5924 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
5925 }
5926
5927 /* A partial symtab that is used only for include files. */
5928 struct dwarf2_include_psymtab : public partial_symtab
5929 {
5930 dwarf2_include_psymtab (const char *filename,
5931 psymtab_storage *partial_symtabs,
5932 objfile_per_bfd_storage *objfile_per_bfd)
5933 : partial_symtab (filename, partial_symtabs, objfile_per_bfd)
5934 {
5935 }
5936
5937 void read_symtab (struct objfile *objfile) override
5938 {
5939 /* It's an include file, no symbols to read for it.
5940 Everything is in the includer symtab. */
5941
5942 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5943 expansion of the includer psymtab. We use the dependencies[0] field to
5944 model the includer. But if we go the regular route of calling
5945 expand_psymtab here, and having expand_psymtab call expand_dependencies
5946 to expand the includer, we'll only use expand_psymtab on the includer
5947 (making it a non-toplevel psymtab), while if we expand the includer via
5948 another path, we'll use read_symtab (making it a toplevel psymtab).
5949 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5950 psymtab, and trigger read_symtab on the includer here directly. */
5951 includer ()->read_symtab (objfile);
5952 }
5953
5954 void expand_psymtab (struct objfile *objfile) override
5955 {
5956 /* This is not called by read_symtab, and should not be called by any
5957 expand_dependencies. */
5958 gdb_assert (false);
5959 }
5960
5961 bool readin_p (struct objfile *objfile) const override
5962 {
5963 return includer ()->readin_p (objfile);
5964 }
5965
5966 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
5967 {
5968 return nullptr;
5969 }
5970
5971 private:
5972 partial_symtab *includer () const
5973 {
5974 /* An include psymtab has exactly one dependency: the psymtab that
5975 includes it. */
5976 gdb_assert (this->number_of_dependencies == 1);
5977 return this->dependencies[0];
5978 }
5979 };
5980
5981 /* Allocate a new partial symtab for file named NAME and mark this new
5982 partial symtab as being an include of PST. */
5983
5984 static void
5985 dwarf2_create_include_psymtab (dwarf2_per_bfd *per_bfd,
5986 const char *name,
5987 dwarf2_psymtab *pst,
5988 psymtab_storage *partial_symtabs,
5989 objfile_per_bfd_storage *objfile_per_bfd)
5990 {
5991 dwarf2_include_psymtab *subpst
5992 = new dwarf2_include_psymtab (name, partial_symtabs, objfile_per_bfd);
5993
5994 if (!IS_ABSOLUTE_PATH (subpst->filename))
5995 subpst->dirname = pst->dirname;
5996
5997 subpst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (1);
5998 subpst->dependencies[0] = pst;
5999 subpst->number_of_dependencies = 1;
6000 }
6001
6002 /* Read the Line Number Program data and extract the list of files
6003 included by the source file represented by PST. Build an include
6004 partial symtab for each of these included files. */
6005
6006 static void
6007 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
6008 struct die_info *die,
6009 dwarf2_psymtab *pst)
6010 {
6011 line_header_up lh;
6012 struct attribute *attr;
6013
6014 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6015 if (attr != nullptr && attr->form_is_unsigned ())
6016 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
6017 if (lh == NULL)
6018 return; /* No linetable, so no includes. */
6019
6020 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6021 that we pass in the raw text_low here; that is ok because we're
6022 only decoding the line table to make include partial symtabs, and
6023 so the addresses aren't really used. */
6024 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
6025 pst->raw_text_low (), 1);
6026 }
6027
6028 static hashval_t
6029 hash_signatured_type (const void *item)
6030 {
6031 const struct signatured_type *sig_type
6032 = (const struct signatured_type *) item;
6033
6034 /* This drops the top 32 bits of the signature, but is ok for a hash. */
6035 return sig_type->signature;
6036 }
6037
6038 static int
6039 eq_signatured_type (const void *item_lhs, const void *item_rhs)
6040 {
6041 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6042 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
6043
6044 return lhs->signature == rhs->signature;
6045 }
6046
6047 /* Allocate a hash table for signatured types. */
6048
6049 static htab_up
6050 allocate_signatured_type_table ()
6051 {
6052 return htab_up (htab_create_alloc (41,
6053 hash_signatured_type,
6054 eq_signatured_type,
6055 NULL, xcalloc, xfree));
6056 }
6057
6058 /* A helper function to add a signatured type CU to a table. */
6059
6060 static int
6061 add_signatured_type_cu_to_table (void **slot, void *datum)
6062 {
6063 struct signatured_type *sigt = (struct signatured_type *) *slot;
6064 std::vector<signatured_type *> *all_type_units
6065 = (std::vector<signatured_type *> *) datum;
6066
6067 all_type_units->push_back (sigt);
6068
6069 return 1;
6070 }
6071
6072 /* A helper for create_debug_types_hash_table. Read types from SECTION
6073 and fill them into TYPES_HTAB. It will process only type units,
6074 therefore DW_UT_type. */
6075
6076 static void
6077 create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
6078 struct dwo_file *dwo_file,
6079 dwarf2_section_info *section, htab_up &types_htab,
6080 rcuh_kind section_kind)
6081 {
6082 struct objfile *objfile = per_objfile->objfile;
6083 struct dwarf2_section_info *abbrev_section;
6084 bfd *abfd;
6085 const gdb_byte *info_ptr, *end_ptr;
6086
6087 abbrev_section = (dwo_file != NULL
6088 ? &dwo_file->sections.abbrev
6089 : &per_objfile->per_bfd->abbrev);
6090
6091 dwarf_read_debug_printf ("Reading %s for %s",
6092 section->get_name (),
6093 abbrev_section->get_file_name ());
6094
6095 section->read (objfile);
6096 info_ptr = section->buffer;
6097
6098 if (info_ptr == NULL)
6099 return;
6100
6101 /* We can't set abfd until now because the section may be empty or
6102 not present, in which case the bfd is unknown. */
6103 abfd = section->get_bfd_owner ();
6104
6105 /* We don't use cutu_reader here because we don't need to read
6106 any dies: the signature is in the header. */
6107
6108 end_ptr = info_ptr + section->size;
6109 while (info_ptr < end_ptr)
6110 {
6111 struct signatured_type *sig_type;
6112 struct dwo_unit *dwo_tu;
6113 void **slot;
6114 const gdb_byte *ptr = info_ptr;
6115 struct comp_unit_head header;
6116 unsigned int length;
6117
6118 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
6119
6120 /* Initialize it due to a false compiler warning. */
6121 header.signature = -1;
6122 header.type_cu_offset_in_tu = (cu_offset) -1;
6123
6124 /* We need to read the type's signature in order to build the hash
6125 table, but we don't need anything else just yet. */
6126
6127 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
6128 abbrev_section, ptr, section_kind);
6129
6130 length = header.get_length ();
6131
6132 /* Skip dummy type units. */
6133 if (ptr >= info_ptr + length
6134 || peek_abbrev_code (abfd, ptr) == 0
6135 || (header.unit_type != DW_UT_type
6136 && header.unit_type != DW_UT_split_type))
6137 {
6138 info_ptr += length;
6139 continue;
6140 }
6141
6142 if (types_htab == NULL)
6143 {
6144 if (dwo_file)
6145 types_htab = allocate_dwo_unit_table ();
6146 else
6147 types_htab = allocate_signatured_type_table ();
6148 }
6149
6150 if (dwo_file)
6151 {
6152 sig_type = NULL;
6153 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
6154 dwo_tu->dwo_file = dwo_file;
6155 dwo_tu->signature = header.signature;
6156 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
6157 dwo_tu->section = section;
6158 dwo_tu->sect_off = sect_off;
6159 dwo_tu->length = length;
6160 }
6161 else
6162 {
6163 /* N.B.: type_offset is not usable if this type uses a DWO file.
6164 The real type_offset is in the DWO file. */
6165 dwo_tu = NULL;
6166 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6167 sig_type->signature = header.signature;
6168 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
6169 sig_type->per_cu.is_debug_types = 1;
6170 sig_type->per_cu.section = section;
6171 sig_type->per_cu.sect_off = sect_off;
6172 sig_type->per_cu.length = length;
6173 }
6174
6175 slot = htab_find_slot (types_htab.get (),
6176 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6177 INSERT);
6178 gdb_assert (slot != NULL);
6179 if (*slot != NULL)
6180 {
6181 sect_offset dup_sect_off;
6182
6183 if (dwo_file)
6184 {
6185 const struct dwo_unit *dup_tu
6186 = (const struct dwo_unit *) *slot;
6187
6188 dup_sect_off = dup_tu->sect_off;
6189 }
6190 else
6191 {
6192 const struct signatured_type *dup_tu
6193 = (const struct signatured_type *) *slot;
6194
6195 dup_sect_off = dup_tu->per_cu.sect_off;
6196 }
6197
6198 complaint (_("debug type entry at offset %s is duplicate to"
6199 " the entry at offset %s, signature %s"),
6200 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
6201 hex_string (header.signature));
6202 }
6203 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
6204
6205 dwarf_read_debug_printf_v (" offset %s, signature %s",
6206 sect_offset_str (sect_off),
6207 hex_string (header.signature));
6208
6209 info_ptr += length;
6210 }
6211 }
6212
6213 /* Create the hash table of all entries in the .debug_types
6214 (or .debug_types.dwo) section(s).
6215 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6216 otherwise it is NULL.
6217
6218 The result is a pointer to the hash table or NULL if there are no types.
6219
6220 Note: This function processes DWO files only, not DWP files. */
6221
6222 static void
6223 create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
6224 struct dwo_file *dwo_file,
6225 gdb::array_view<dwarf2_section_info> type_sections,
6226 htab_up &types_htab)
6227 {
6228 for (dwarf2_section_info &section : type_sections)
6229 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6230 rcuh_kind::TYPE);
6231 }
6232
6233 /* Create the hash table of all entries in the .debug_types section,
6234 and initialize all_type_units.
6235 The result is zero if there is an error (e.g. missing .debug_types section),
6236 otherwise non-zero. */
6237
6238 static int
6239 create_all_type_units (dwarf2_per_objfile *per_objfile)
6240 {
6241 htab_up types_htab;
6242
6243 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6244 types_htab, rcuh_kind::COMPILE);
6245 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6246 types_htab);
6247 if (types_htab == NULL)
6248 {
6249 per_objfile->per_bfd->signatured_types = NULL;
6250 return 0;
6251 }
6252
6253 per_objfile->per_bfd->signatured_types = std::move (types_htab);
6254
6255 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6256 per_objfile->per_bfd->all_type_units.reserve
6257 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
6258
6259 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
6260 add_signatured_type_cu_to_table,
6261 &per_objfile->per_bfd->all_type_units);
6262
6263 return 1;
6264 }
6265
6266 /* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6267 If SLOT is non-NULL, it is the entry to use in the hash table.
6268 Otherwise we find one. */
6269
6270 static struct signatured_type *
6271 add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6272 {
6273 if (per_objfile->per_bfd->all_type_units.size ()
6274 == per_objfile->per_bfd->all_type_units.capacity ())
6275 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6276
6277 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
6278
6279 per_objfile->resize_symtabs ();
6280
6281 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6282 sig_type->signature = sig;
6283 sig_type->per_cu.is_debug_types = 1;
6284 if (per_objfile->per_bfd->using_index)
6285 {
6286 sig_type->per_cu.v.quick =
6287 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6288 struct dwarf2_per_cu_quick_data);
6289 }
6290
6291 if (slot == NULL)
6292 {
6293 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6294 sig_type, INSERT);
6295 }
6296 gdb_assert (*slot == NULL);
6297 *slot = sig_type;
6298 /* The rest of sig_type must be filled in by the caller. */
6299 return sig_type;
6300 }
6301
6302 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6303 Fill in SIG_ENTRY with DWO_ENTRY. */
6304
6305 static void
6306 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
6307 struct signatured_type *sig_entry,
6308 struct dwo_unit *dwo_entry)
6309 {
6310 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
6311
6312 /* Make sure we're not clobbering something we don't expect to. */
6313 gdb_assert (! sig_entry->per_cu.queued);
6314 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
6315 if (per_bfd->using_index)
6316 {
6317 gdb_assert (sig_entry->per_cu.v.quick != NULL);
6318 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6319 }
6320 else
6321 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
6322 gdb_assert (sig_entry->signature == dwo_entry->signature);
6323 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
6324 gdb_assert (sig_entry->type_unit_group == NULL);
6325 gdb_assert (sig_entry->dwo_unit == NULL);
6326
6327 sig_entry->per_cu.section = dwo_entry->section;
6328 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
6329 sig_entry->per_cu.length = dwo_entry->length;
6330 sig_entry->per_cu.reading_dwo_directly = 1;
6331 sig_entry->per_cu.per_bfd = per_bfd;
6332 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6333 sig_entry->dwo_unit = dwo_entry;
6334 }
6335
6336 /* Subroutine of lookup_signatured_type.
6337 If we haven't read the TU yet, create the signatured_type data structure
6338 for a TU to be read in directly from a DWO file, bypassing the stub.
6339 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6340 using .gdb_index, then when reading a CU we want to stay in the DWO file
6341 containing that CU. Otherwise we could end up reading several other DWO
6342 files (due to comdat folding) to process the transitive closure of all the
6343 mentioned TUs, and that can be slow. The current DWO file will have every
6344 type signature that it needs.
6345 We only do this for .gdb_index because in the psymtab case we already have
6346 to read all the DWOs to build the type unit groups. */
6347
6348 static struct signatured_type *
6349 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6350 {
6351 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6352 struct dwo_file *dwo_file;
6353 struct dwo_unit find_dwo_entry, *dwo_entry;
6354 struct signatured_type find_sig_entry, *sig_entry;
6355 void **slot;
6356
6357 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6358
6359 /* If TU skeletons have been removed then we may not have read in any
6360 TUs yet. */
6361 if (per_objfile->per_bfd->signatured_types == NULL)
6362 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6363
6364 /* We only ever need to read in one copy of a signatured type.
6365 Use the global signatured_types array to do our own comdat-folding
6366 of types. If this is the first time we're reading this TU, and
6367 the TU has an entry in .gdb_index, replace the recorded data from
6368 .gdb_index with this TU. */
6369
6370 find_sig_entry.signature = sig;
6371 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6372 &find_sig_entry, INSERT);
6373 sig_entry = (struct signatured_type *) *slot;
6374
6375 /* We can get here with the TU already read, *or* in the process of being
6376 read. Don't reassign the global entry to point to this DWO if that's
6377 the case. Also note that if the TU is already being read, it may not
6378 have come from a DWO, the program may be a mix of Fission-compiled
6379 code and non-Fission-compiled code. */
6380
6381 /* Have we already tried to read this TU?
6382 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6383 needn't exist in the global table yet). */
6384 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
6385 return sig_entry;
6386
6387 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6388 dwo_unit of the TU itself. */
6389 dwo_file = cu->dwo_unit->dwo_file;
6390
6391 /* Ok, this is the first time we're reading this TU. */
6392 if (dwo_file->tus == NULL)
6393 return NULL;
6394 find_dwo_entry.signature = sig;
6395 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6396 &find_dwo_entry);
6397 if (dwo_entry == NULL)
6398 return NULL;
6399
6400 /* If the global table doesn't have an entry for this TU, add one. */
6401 if (sig_entry == NULL)
6402 sig_entry = add_type_unit (per_objfile, sig, slot);
6403
6404 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6405 sig_entry->per_cu.tu_read = 1;
6406 return sig_entry;
6407 }
6408
6409 /* Subroutine of lookup_signatured_type.
6410 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6411 then try the DWP file. If the TU stub (skeleton) has been removed then
6412 it won't be in .gdb_index. */
6413
6414 static struct signatured_type *
6415 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6416 {
6417 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6418 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
6419 struct dwo_unit *dwo_entry;
6420 struct signatured_type find_sig_entry, *sig_entry;
6421 void **slot;
6422
6423 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
6424 gdb_assert (dwp_file != NULL);
6425
6426 /* If TU skeletons have been removed then we may not have read in any
6427 TUs yet. */
6428 if (per_objfile->per_bfd->signatured_types == NULL)
6429 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6430
6431 find_sig_entry.signature = sig;
6432 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6433 &find_sig_entry, INSERT);
6434 sig_entry = (struct signatured_type *) *slot;
6435
6436 /* Have we already tried to read this TU?
6437 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6438 needn't exist in the global table yet). */
6439 if (sig_entry != NULL)
6440 return sig_entry;
6441
6442 if (dwp_file->tus == NULL)
6443 return NULL;
6444 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6445 1 /* is_debug_types */);
6446 if (dwo_entry == NULL)
6447 return NULL;
6448
6449 sig_entry = add_type_unit (per_objfile, sig, slot);
6450 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
6451
6452 return sig_entry;
6453 }
6454
6455 /* Lookup a signature based type for DW_FORM_ref_sig8.
6456 Returns NULL if signature SIG is not present in the table.
6457 It is up to the caller to complain about this. */
6458
6459 static struct signatured_type *
6460 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6461 {
6462 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6463
6464 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
6465 {
6466 /* We're in a DWO/DWP file, and we're using .gdb_index.
6467 These cases require special processing. */
6468 if (get_dwp_file (per_objfile) == NULL)
6469 return lookup_dwo_signatured_type (cu, sig);
6470 else
6471 return lookup_dwp_signatured_type (cu, sig);
6472 }
6473 else
6474 {
6475 struct signatured_type find_entry, *entry;
6476
6477 if (per_objfile->per_bfd->signatured_types == NULL)
6478 return NULL;
6479 find_entry.signature = sig;
6480 entry = ((struct signatured_type *)
6481 htab_find (per_objfile->per_bfd->signatured_types.get (),
6482 &find_entry));
6483 return entry;
6484 }
6485 }
6486
6487 /* Low level DIE reading support. */
6488
6489 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6490
6491 static void
6492 init_cu_die_reader (struct die_reader_specs *reader,
6493 struct dwarf2_cu *cu,
6494 struct dwarf2_section_info *section,
6495 struct dwo_file *dwo_file,
6496 struct abbrev_table *abbrev_table)
6497 {
6498 gdb_assert (section->readin && section->buffer != NULL);
6499 reader->abfd = section->get_bfd_owner ();
6500 reader->cu = cu;
6501 reader->dwo_file = dwo_file;
6502 reader->die_section = section;
6503 reader->buffer = section->buffer;
6504 reader->buffer_end = section->buffer + section->size;
6505 reader->abbrev_table = abbrev_table;
6506 }
6507
6508 /* Subroutine of cutu_reader to simplify it.
6509 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
6510 There's just a lot of work to do, and cutu_reader is big enough
6511 already.
6512
6513 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6514 from it to the DIE in the DWO. If NULL we are skipping the stub.
6515 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6516 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
6517 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6518 STUB_COMP_DIR may be non-NULL.
6519 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
6520 are filled in with the info of the DIE from the DWO file.
6521 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6522 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6523 kept around for at least as long as *RESULT_READER.
6524
6525 The result is non-zero if a valid (non-dummy) DIE was found. */
6526
6527 static int
6528 read_cutu_die_from_dwo (dwarf2_cu *cu,
6529 struct dwo_unit *dwo_unit,
6530 struct die_info *stub_comp_unit_die,
6531 const char *stub_comp_dir,
6532 struct die_reader_specs *result_reader,
6533 const gdb_byte **result_info_ptr,
6534 struct die_info **result_comp_unit_die,
6535 abbrev_table_up *result_dwo_abbrev_table)
6536 {
6537 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6538 dwarf2_per_cu_data *per_cu = cu->per_cu;
6539 struct objfile *objfile = per_objfile->objfile;
6540 bfd *abfd;
6541 const gdb_byte *begin_info_ptr, *info_ptr;
6542 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6543 int i,num_extra_attrs;
6544 struct dwarf2_section_info *dwo_abbrev_section;
6545 struct die_info *comp_unit_die;
6546
6547 /* At most one of these may be provided. */
6548 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
6549
6550 /* These attributes aren't processed until later:
6551 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
6552 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6553 referenced later. However, these attributes are found in the stub
6554 which we won't have later. In order to not impose this complication
6555 on the rest of the code, we read them here and copy them to the
6556 DWO CU/TU die. */
6557
6558 stmt_list = NULL;
6559 low_pc = NULL;
6560 high_pc = NULL;
6561 ranges = NULL;
6562 comp_dir = NULL;
6563
6564 if (stub_comp_unit_die != NULL)
6565 {
6566 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6567 DWO file. */
6568 if (!per_cu->is_debug_types)
6569 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6570 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6571 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6572 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6573 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6574
6575 cu->addr_base = stub_comp_unit_die->addr_base ();
6576
6577 /* There should be a DW_AT_GNU_ranges_base attribute here (if needed).
6578 We need the value before we can process DW_AT_ranges values from the
6579 DWO. */
6580 cu->gnu_ranges_base = stub_comp_unit_die->gnu_ranges_base ();
6581
6582 /* For DWARF5: record the DW_AT_rnglists_base value from the skeleton. If
6583 there are attributes of form DW_FORM_rnglistx in the skeleton, they'll
6584 need the rnglists base. Attributes of form DW_FORM_rnglistx in the
6585 split unit don't use it, as the DWO has its own .debug_rnglists.dwo
6586 section. */
6587 cu->rnglists_base = stub_comp_unit_die->rnglists_base ();
6588 }
6589 else if (stub_comp_dir != NULL)
6590 {
6591 /* Reconstruct the comp_dir attribute to simplify the code below. */
6592 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
6593 comp_dir->name = DW_AT_comp_dir;
6594 comp_dir->form = DW_FORM_string;
6595 comp_dir->set_string_noncanonical (stub_comp_dir);
6596 }
6597
6598 /* Set up for reading the DWO CU/TU. */
6599 cu->dwo_unit = dwo_unit;
6600 dwarf2_section_info *section = dwo_unit->section;
6601 section->read (objfile);
6602 abfd = section->get_bfd_owner ();
6603 begin_info_ptr = info_ptr = (section->buffer
6604 + to_underlying (dwo_unit->sect_off));
6605 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
6606
6607 if (per_cu->is_debug_types)
6608 {
6609 signatured_type *sig_type = (struct signatured_type *) per_cu;
6610
6611 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6612 section, dwo_abbrev_section,
6613 info_ptr, rcuh_kind::TYPE);
6614 /* This is not an assert because it can be caused by bad debug info. */
6615 if (sig_type->signature != cu->header.signature)
6616 {
6617 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
6618 " TU at offset %s [in module %s]"),
6619 hex_string (sig_type->signature),
6620 hex_string (cu->header.signature),
6621 sect_offset_str (dwo_unit->sect_off),
6622 bfd_get_filename (abfd));
6623 }
6624 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6625 /* For DWOs coming from DWP files, we don't know the CU length
6626 nor the type's offset in the TU until now. */
6627 dwo_unit->length = cu->header.get_length ();
6628 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
6629
6630 /* Establish the type offset that can be used to lookup the type.
6631 For DWO files, we don't know it until now. */
6632 sig_type->type_offset_in_section
6633 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
6634 }
6635 else
6636 {
6637 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6638 section, dwo_abbrev_section,
6639 info_ptr, rcuh_kind::COMPILE);
6640 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
6641 /* For DWOs coming from DWP files, we don't know the CU length
6642 until now. */
6643 dwo_unit->length = cu->header.get_length ();
6644 }
6645
6646 dwo_abbrev_section->read (objfile);
6647 *result_dwo_abbrev_table
6648 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
6649 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6650 result_dwo_abbrev_table->get ());
6651
6652 /* Read in the die, but leave space to copy over the attributes
6653 from the stub. This has the benefit of simplifying the rest of
6654 the code - all the work to maintain the illusion of a single
6655 DW_TAG_{compile,type}_unit DIE is done here. */
6656 num_extra_attrs = ((stmt_list != NULL)
6657 + (low_pc != NULL)
6658 + (high_pc != NULL)
6659 + (ranges != NULL)
6660 + (comp_dir != NULL));
6661 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
6662 num_extra_attrs);
6663
6664 /* Copy over the attributes from the stub to the DIE we just read in. */
6665 comp_unit_die = *result_comp_unit_die;
6666 i = comp_unit_die->num_attrs;
6667 if (stmt_list != NULL)
6668 comp_unit_die->attrs[i++] = *stmt_list;
6669 if (low_pc != NULL)
6670 comp_unit_die->attrs[i++] = *low_pc;
6671 if (high_pc != NULL)
6672 comp_unit_die->attrs[i++] = *high_pc;
6673 if (ranges != NULL)
6674 comp_unit_die->attrs[i++] = *ranges;
6675 if (comp_dir != NULL)
6676 comp_unit_die->attrs[i++] = *comp_dir;
6677 comp_unit_die->num_attrs += num_extra_attrs;
6678
6679 if (dwarf_die_debug)
6680 {
6681 fprintf_unfiltered (gdb_stdlog,
6682 "Read die from %s@0x%x of %s:\n",
6683 section->get_name (),
6684 (unsigned) (begin_info_ptr - section->buffer),
6685 bfd_get_filename (abfd));
6686 dump_die (comp_unit_die, dwarf_die_debug);
6687 }
6688
6689 /* Skip dummy compilation units. */
6690 if (info_ptr >= begin_info_ptr + dwo_unit->length
6691 || peek_abbrev_code (abfd, info_ptr) == 0)
6692 return 0;
6693
6694 *result_info_ptr = info_ptr;
6695 return 1;
6696 }
6697
6698 /* Return the signature of the compile unit, if found. In DWARF 4 and before,
6699 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6700 signature is part of the header. */
6701 static gdb::optional<ULONGEST>
6702 lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6703 {
6704 if (cu->header.version >= 5)
6705 return cu->header.signature;
6706 struct attribute *attr;
6707 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6708 if (attr == nullptr || !attr->form_is_unsigned ())
6709 return gdb::optional<ULONGEST> ();
6710 return attr->as_unsigned ();
6711 }
6712
6713 /* Subroutine of cutu_reader to simplify it.
6714 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6715 Returns NULL if the specified DWO unit cannot be found. */
6716
6717 static struct dwo_unit *
6718 lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
6719 {
6720 dwarf2_per_cu_data *per_cu = cu->per_cu;
6721 struct dwo_unit *dwo_unit;
6722 const char *comp_dir;
6723
6724 gdb_assert (cu != NULL);
6725
6726 /* Yeah, we look dwo_name up again, but it simplifies the code. */
6727 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6728 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
6729
6730 if (per_cu->is_debug_types)
6731 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
6732 else
6733 {
6734 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6735
6736 if (!signature.has_value ())
6737 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6738 " [in module %s]"),
6739 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6740
6741 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
6742 }
6743
6744 return dwo_unit;
6745 }
6746
6747 /* Subroutine of cutu_reader to simplify it.
6748 See it for a description of the parameters.
6749 Read a TU directly from a DWO file, bypassing the stub. */
6750
6751 void
6752 cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6753 dwarf2_per_objfile *per_objfile,
6754 dwarf2_cu *existing_cu)
6755 {
6756 struct signatured_type *sig_type;
6757
6758 /* Verify we can do the following downcast, and that we have the
6759 data we need. */
6760 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6761 sig_type = (struct signatured_type *) this_cu;
6762 gdb_assert (sig_type->dwo_unit != NULL);
6763
6764 dwarf2_cu *cu;
6765
6766 if (existing_cu != nullptr)
6767 {
6768 cu = existing_cu;
6769 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6770 /* There's no need to do the rereading_dwo_cu handling that
6771 cutu_reader does since we don't read the stub. */
6772 }
6773 else
6774 {
6775 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6776 in per_objfile yet. */
6777 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6778 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6779 cu = m_new_cu.get ();
6780 }
6781
6782 /* A future optimization, if needed, would be to use an existing
6783 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6784 could share abbrev tables. */
6785
6786 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
6787 NULL /* stub_comp_unit_die */,
6788 sig_type->dwo_unit->dwo_file->comp_dir,
6789 this, &info_ptr,
6790 &comp_unit_die,
6791 &m_dwo_abbrev_table) == 0)
6792 {
6793 /* Dummy die. */
6794 dummy_p = true;
6795 }
6796 }
6797
6798 /* Initialize a CU (or TU) and read its DIEs.
6799 If the CU defers to a DWO file, read the DWO file as well.
6800
6801 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6802 Otherwise the table specified in the comp unit header is read in and used.
6803 This is an optimization for when we already have the abbrev table.
6804
6805 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6806 allocated. */
6807
6808 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6809 dwarf2_per_objfile *per_objfile,
6810 struct abbrev_table *abbrev_table,
6811 dwarf2_cu *existing_cu,
6812 bool skip_partial)
6813 : die_reader_specs {},
6814 m_this_cu (this_cu)
6815 {
6816 struct objfile *objfile = per_objfile->objfile;
6817 struct dwarf2_section_info *section = this_cu->section;
6818 bfd *abfd = section->get_bfd_owner ();
6819 const gdb_byte *begin_info_ptr;
6820 struct signatured_type *sig_type = NULL;
6821 struct dwarf2_section_info *abbrev_section;
6822 /* Non-zero if CU currently points to a DWO file and we need to
6823 reread it. When this happens we need to reread the skeleton die
6824 before we can reread the DWO file (this only applies to CUs, not TUs). */
6825 int rereading_dwo_cu = 0;
6826
6827 if (dwarf_die_debug)
6828 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
6829 this_cu->is_debug_types ? "type" : "comp",
6830 sect_offset_str (this_cu->sect_off));
6831
6832 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6833 file (instead of going through the stub), short-circuit all of this. */
6834 if (this_cu->reading_dwo_directly)
6835 {
6836 /* Narrow down the scope of possibilities to have to understand. */
6837 gdb_assert (this_cu->is_debug_types);
6838 gdb_assert (abbrev_table == NULL);
6839 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
6840 return;
6841 }
6842
6843 /* This is cheap if the section is already read in. */
6844 section->read (objfile);
6845
6846 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
6847
6848 abbrev_section = get_abbrev_section_for_cu (this_cu);
6849
6850 dwarf2_cu *cu;
6851
6852 if (existing_cu != nullptr)
6853 {
6854 cu = existing_cu;
6855 /* If this CU is from a DWO file we need to start over, we need to
6856 refetch the attributes from the skeleton CU.
6857 This could be optimized by retrieving those attributes from when we
6858 were here the first time: the previous comp_unit_die was stored in
6859 comp_unit_obstack. But there's no data yet that we need this
6860 optimization. */
6861 if (cu->dwo_unit != NULL)
6862 rereading_dwo_cu = 1;
6863 }
6864 else
6865 {
6866 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6867 in per_objfile yet. */
6868 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
6869 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
6870 cu = m_new_cu.get ();
6871 }
6872
6873 /* Get the header. */
6874 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
6875 {
6876 /* We already have the header, there's no need to read it in again. */
6877 info_ptr += to_underlying (cu->header.first_die_cu_offset);
6878 }
6879 else
6880 {
6881 if (this_cu->is_debug_types)
6882 {
6883 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6884 section, abbrev_section,
6885 info_ptr, rcuh_kind::TYPE);
6886
6887 /* Since per_cu is the first member of struct signatured_type,
6888 we can go from a pointer to one to a pointer to the other. */
6889 sig_type = (struct signatured_type *) this_cu;
6890 gdb_assert (sig_type->signature == cu->header.signature);
6891 gdb_assert (sig_type->type_offset_in_tu
6892 == cu->header.type_cu_offset_in_tu);
6893 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6894
6895 /* LENGTH has not been set yet for type units if we're
6896 using .gdb_index. */
6897 this_cu->length = cu->header.get_length ();
6898
6899 /* Establish the type offset that can be used to lookup the type. */
6900 sig_type->type_offset_in_section =
6901 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
6902
6903 this_cu->dwarf_version = cu->header.version;
6904 }
6905 else
6906 {
6907 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6908 section, abbrev_section,
6909 info_ptr,
6910 rcuh_kind::COMPILE);
6911
6912 gdb_assert (this_cu->sect_off == cu->header.sect_off);
6913 if (this_cu->length == 0)
6914 this_cu->length = cu->header.get_length ();
6915 else
6916 gdb_assert (this_cu->length == cu->header.get_length ());
6917 this_cu->dwarf_version = cu->header.version;
6918 }
6919 }
6920
6921 /* Skip dummy compilation units. */
6922 if (info_ptr >= begin_info_ptr + this_cu->length
6923 || peek_abbrev_code (abfd, info_ptr) == 0)
6924 {
6925 dummy_p = true;
6926 return;
6927 }
6928
6929 /* If we don't have them yet, read the abbrevs for this compilation unit.
6930 And if we need to read them now, make sure they're freed when we're
6931 done. */
6932 if (abbrev_table != NULL)
6933 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6934 else
6935 {
6936 abbrev_section->read (objfile);
6937 m_abbrev_table_holder
6938 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
6939 abbrev_table = m_abbrev_table_holder.get ();
6940 }
6941
6942 /* Read the top level CU/TU die. */
6943 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
6944 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
6945
6946 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
6947 {
6948 dummy_p = true;
6949 return;
6950 }
6951
6952 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
6953 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6954 table from the DWO file and pass the ownership over to us. It will be
6955 referenced from READER, so we must make sure to free it after we're done
6956 with READER.
6957
6958 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6959 DWO CU, that this test will fail (the attribute will not be present). */
6960 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
6961 if (dwo_name != nullptr)
6962 {
6963 struct dwo_unit *dwo_unit;
6964 struct die_info *dwo_comp_unit_die;
6965
6966 if (comp_unit_die->has_children)
6967 {
6968 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
6969 " has children (offset %s) [in module %s]"),
6970 sect_offset_str (this_cu->sect_off),
6971 bfd_get_filename (abfd));
6972 }
6973 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6974 if (dwo_unit != NULL)
6975 {
6976 if (read_cutu_die_from_dwo (cu, dwo_unit,
6977 comp_unit_die, NULL,
6978 this, &info_ptr,
6979 &dwo_comp_unit_die,
6980 &m_dwo_abbrev_table) == 0)
6981 {
6982 /* Dummy die. */
6983 dummy_p = true;
6984 return;
6985 }
6986 comp_unit_die = dwo_comp_unit_die;
6987 }
6988 else
6989 {
6990 /* Yikes, we couldn't find the rest of the DIE, we only have
6991 the stub. A complaint has already been logged. There's
6992 not much more we can do except pass on the stub DIE to
6993 die_reader_func. We don't want to throw an error on bad
6994 debug info. */
6995 }
6996 }
6997 }
6998
6999 void
7000 cutu_reader::keep ()
7001 {
7002 /* Done, clean up. */
7003 gdb_assert (!dummy_p);
7004 if (m_new_cu != NULL)
7005 {
7006 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7007 now. */
7008 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7009 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
7010 }
7011 }
7012
7013 /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7014 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7015 assumed to have already done the lookup to find the DWO file).
7016
7017 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
7018 THIS_CU->is_debug_types, but nothing else.
7019
7020 We fill in THIS_CU->length.
7021
7022 THIS_CU->cu is always freed when done.
7023 This is done in order to not leave THIS_CU->cu in a state where we have
7024 to care whether it refers to the "main" CU or the DWO CU.
7025
7026 When parent_cu is passed, it is used to provide a default value for
7027 str_offsets_base and addr_base from the parent. */
7028
7029 cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7030 dwarf2_per_objfile *per_objfile,
7031 struct dwarf2_cu *parent_cu,
7032 struct dwo_file *dwo_file)
7033 : die_reader_specs {},
7034 m_this_cu (this_cu)
7035 {
7036 struct objfile *objfile = per_objfile->objfile;
7037 struct dwarf2_section_info *section = this_cu->section;
7038 bfd *abfd = section->get_bfd_owner ();
7039 struct dwarf2_section_info *abbrev_section;
7040 const gdb_byte *begin_info_ptr, *info_ptr;
7041
7042 if (dwarf_die_debug)
7043 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
7044 this_cu->is_debug_types ? "type" : "comp",
7045 sect_offset_str (this_cu->sect_off));
7046
7047 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7048
7049 abbrev_section = (dwo_file != NULL
7050 ? &dwo_file->sections.abbrev
7051 : get_abbrev_section_for_cu (this_cu));
7052
7053 /* This is cheap if the section is already read in. */
7054 section->read (objfile);
7055
7056 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
7057
7058 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
7059 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7060 section, abbrev_section, info_ptr,
7061 (this_cu->is_debug_types
7062 ? rcuh_kind::TYPE
7063 : rcuh_kind::COMPILE));
7064
7065 if (parent_cu != nullptr)
7066 {
7067 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7068 m_new_cu->addr_base = parent_cu->addr_base;
7069 }
7070 this_cu->length = m_new_cu->header.get_length ();
7071
7072 /* Skip dummy compilation units. */
7073 if (info_ptr >= begin_info_ptr + this_cu->length
7074 || peek_abbrev_code (abfd, info_ptr) == 0)
7075 {
7076 dummy_p = true;
7077 return;
7078 }
7079
7080 abbrev_section->read (objfile);
7081 m_abbrev_table_holder
7082 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
7083
7084 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7085 m_abbrev_table_holder.get ());
7086 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
7087 }
7088
7089 \f
7090 /* Type Unit Groups.
7091
7092 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7093 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7094 so that all types coming from the same compilation (.o file) are grouped
7095 together. A future step could be to put the types in the same symtab as
7096 the CU the types ultimately came from. */
7097
7098 static hashval_t
7099 hash_type_unit_group (const void *item)
7100 {
7101 const struct type_unit_group *tu_group
7102 = (const struct type_unit_group *) item;
7103
7104 return hash_stmt_list_entry (&tu_group->hash);
7105 }
7106
7107 static int
7108 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
7109 {
7110 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7111 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
7112
7113 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
7114 }
7115
7116 /* Allocate a hash table for type unit groups. */
7117
7118 static htab_up
7119 allocate_type_unit_groups_table ()
7120 {
7121 return htab_up (htab_create_alloc (3,
7122 hash_type_unit_group,
7123 eq_type_unit_group,
7124 NULL, xcalloc, xfree));
7125 }
7126
7127 /* Type units that don't have DW_AT_stmt_list are grouped into their own
7128 partial symtabs. We combine several TUs per psymtab to not let the size
7129 of any one psymtab grow too big. */
7130 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7131 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
7132
7133 /* Helper routine for get_type_unit_group.
7134 Create the type_unit_group object used to hold one or more TUs. */
7135
7136 static struct type_unit_group *
7137 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
7138 {
7139 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7140 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7141 struct dwarf2_per_cu_data *per_cu;
7142 struct type_unit_group *tu_group;
7143
7144 tu_group = OBSTACK_ZALLOC (&per_bfd->obstack, type_unit_group);
7145 per_cu = &tu_group->per_cu;
7146 per_cu->per_bfd = per_bfd;
7147
7148 if (per_bfd->using_index)
7149 {
7150 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
7151 struct dwarf2_per_cu_quick_data);
7152 }
7153 else
7154 {
7155 unsigned int line_offset = to_underlying (line_offset_struct);
7156 dwarf2_psymtab *pst;
7157 std::string name;
7158
7159 /* Give the symtab a useful name for debug purposes. */
7160 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7161 name = string_printf ("<type_units_%d>",
7162 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7163 else
7164 name = string_printf ("<type_units_at_0x%x>", line_offset);
7165
7166 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
7167 pst->anonymous = true;
7168 }
7169
7170 tu_group->hash.dwo_unit = cu->dwo_unit;
7171 tu_group->hash.line_sect_off = line_offset_struct;
7172
7173 return tu_group;
7174 }
7175
7176 /* Look up the type_unit_group for type unit CU, and create it if necessary.
7177 STMT_LIST is a DW_AT_stmt_list attribute. */
7178
7179 static struct type_unit_group *
7180 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
7181 {
7182 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7183 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7184 struct type_unit_group *tu_group;
7185 void **slot;
7186 unsigned int line_offset;
7187 struct type_unit_group type_unit_group_for_lookup;
7188
7189 if (per_objfile->per_bfd->type_unit_groups == NULL)
7190 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
7191
7192 /* Do we need to create a new group, or can we use an existing one? */
7193
7194 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
7195 {
7196 line_offset = stmt_list->as_unsigned ();
7197 ++tu_stats->nr_symtab_sharers;
7198 }
7199 else
7200 {
7201 /* Ugh, no stmt_list. Rare, but we have to handle it.
7202 We can do various things here like create one group per TU or
7203 spread them over multiple groups to split up the expansion work.
7204 To avoid worst case scenarios (too many groups or too large groups)
7205 we, umm, group them in bunches. */
7206 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7207 | (tu_stats->nr_stmt_less_type_units
7208 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7209 ++tu_stats->nr_stmt_less_type_units;
7210 }
7211
7212 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
7213 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
7214 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
7215 &type_unit_group_for_lookup, INSERT);
7216 if (*slot != NULL)
7217 {
7218 tu_group = (struct type_unit_group *) *slot;
7219 gdb_assert (tu_group != NULL);
7220 }
7221 else
7222 {
7223 sect_offset line_offset_struct = (sect_offset) line_offset;
7224 tu_group = create_type_unit_group (cu, line_offset_struct);
7225 *slot = tu_group;
7226 ++tu_stats->nr_symtabs;
7227 }
7228
7229 return tu_group;
7230 }
7231 \f
7232 /* Partial symbol tables. */
7233
7234 /* Create a psymtab named NAME and assign it to PER_CU.
7235
7236 The caller must fill in the following details:
7237 dirname, textlow, texthigh. */
7238
7239 static dwarf2_psymtab *
7240 create_partial_symtab (dwarf2_per_cu_data *per_cu,
7241 dwarf2_per_objfile *per_objfile,
7242 const char *name)
7243 {
7244 dwarf2_psymtab *pst
7245 = new dwarf2_psymtab (name, per_objfile->per_bfd->partial_symtabs.get (),
7246 per_objfile->objfile->per_bfd, per_cu);
7247
7248 pst->psymtabs_addrmap_supported = true;
7249
7250 /* This is the glue that links PST into GDB's symbol API. */
7251 per_cu->v.psymtab = pst;
7252
7253 return pst;
7254 }
7255
7256 /* DIE reader function for process_psymtab_comp_unit. */
7257
7258 static void
7259 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
7260 const gdb_byte *info_ptr,
7261 struct die_info *comp_unit_die,
7262 enum language pretend_language)
7263 {
7264 struct dwarf2_cu *cu = reader->cu;
7265 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7266 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7267 struct objfile *objfile = per_objfile->objfile;
7268 struct gdbarch *gdbarch = objfile->arch ();
7269 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7270 CORE_ADDR baseaddr;
7271 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7272 dwarf2_psymtab *pst;
7273 enum pc_bounds_kind cu_bounds_kind;
7274 const char *filename;
7275
7276 gdb_assert (! per_cu->is_debug_types);
7277
7278 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
7279
7280 /* Allocate a new partial symbol table structure. */
7281 gdb::unique_xmalloc_ptr<char> debug_filename;
7282 static const char artificial[] = "<artificial>";
7283 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7284 if (filename == NULL)
7285 filename = "";
7286 else if (strcmp (filename, artificial) == 0)
7287 {
7288 debug_filename.reset (concat (artificial, "@",
7289 sect_offset_str (per_cu->sect_off),
7290 (char *) NULL));
7291 filename = debug_filename.get ();
7292 }
7293
7294 pst = create_partial_symtab (per_cu, per_objfile, filename);
7295
7296 /* This must be done before calling dwarf2_build_include_psymtabs. */
7297 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
7298
7299 baseaddr = objfile->text_section_offset ();
7300
7301 dwarf2_find_base_address (comp_unit_die, cu);
7302
7303 /* Possibly set the default values of LOWPC and HIGHPC from
7304 `DW_AT_ranges'. */
7305 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7306 &best_highpc, cu, pst);
7307 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
7308 {
7309 CORE_ADDR low
7310 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7311 - baseaddr);
7312 CORE_ADDR high
7313 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7314 - baseaddr - 1);
7315 /* Store the contiguous range if it is not empty; it can be
7316 empty for CUs with no code. */
7317 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
7318 low, high, pst);
7319 }
7320
7321 /* Check if comp unit has_children.
7322 If so, read the rest of the partial symbols from this comp unit.
7323 If not, there's no more debug_info for this comp unit. */
7324 if (comp_unit_die->has_children)
7325 {
7326 struct partial_die_info *first_die;
7327 CORE_ADDR lowpc, highpc;
7328
7329 lowpc = ((CORE_ADDR) -1);
7330 highpc = ((CORE_ADDR) 0);
7331
7332 first_die = load_partial_dies (reader, info_ptr, 1);
7333
7334 scan_partial_symbols (first_die, &lowpc, &highpc,
7335 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
7336
7337 /* If we didn't find a lowpc, set it to highpc to avoid
7338 complaints from `maint check'. */
7339 if (lowpc == ((CORE_ADDR) -1))
7340 lowpc = highpc;
7341
7342 /* If the compilation unit didn't have an explicit address range,
7343 then use the information extracted from its child dies. */
7344 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
7345 {
7346 best_lowpc = lowpc;
7347 best_highpc = highpc;
7348 }
7349 }
7350 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
7351 best_lowpc + baseaddr)
7352 - baseaddr);
7353 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
7354 best_highpc + baseaddr)
7355 - baseaddr);
7356
7357 pst->end ();
7358
7359 if (!cu->per_cu->imported_symtabs_empty ())
7360 {
7361 int i;
7362 int len = cu->per_cu->imported_symtabs_size ();
7363
7364 /* Fill in 'dependencies' here; we fill in 'users' in a
7365 post-pass. */
7366 pst->number_of_dependencies = len;
7367 pst->dependencies
7368 = per_bfd->partial_symtabs->allocate_dependencies (len);
7369 for (i = 0; i < len; ++i)
7370 {
7371 pst->dependencies[i]
7372 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7373 }
7374
7375 cu->per_cu->imported_symtabs_free ();
7376 }
7377
7378 /* Get the list of files included in the current compilation unit,
7379 and build a psymtab for each of them. */
7380 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7381
7382 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7383 ", %d global, %d static syms",
7384 per_cu->is_debug_types ? "type" : "comp",
7385 sect_offset_str (per_cu->sect_off),
7386 paddress (gdbarch, pst->text_low (objfile)),
7387 paddress (gdbarch, pst->text_high (objfile)),
7388 (int) pst->global_psymbols.size (),
7389 (int) pst->static_psymbols.size ());
7390 }
7391
7392 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7393 Process compilation unit THIS_CU for a psymtab. */
7394
7395 static void
7396 process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7397 dwarf2_per_objfile *per_objfile,
7398 bool want_partial_unit,
7399 enum language pretend_language)
7400 {
7401 /* If this compilation unit was already read in, free the
7402 cached copy in order to read it in again. This is
7403 necessary because we skipped some symbols when we first
7404 read in the compilation unit (see load_partial_dies).
7405 This problem could be avoided, but the benefit is unclear. */
7406 per_objfile->remove_cu (this_cu);
7407
7408 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
7409
7410 switch (reader.comp_unit_die->tag)
7411 {
7412 case DW_TAG_compile_unit:
7413 this_cu->unit_type = DW_UT_compile;
7414 break;
7415 case DW_TAG_partial_unit:
7416 this_cu->unit_type = DW_UT_partial;
7417 break;
7418 case DW_TAG_type_unit:
7419 this_cu->unit_type = DW_UT_type;
7420 break;
7421 default:
7422 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
7423 dwarf_tag_name (reader.comp_unit_die->tag),
7424 sect_offset_str (reader.cu->per_cu->sect_off),
7425 objfile_name (per_objfile->objfile));
7426 }
7427
7428 if (reader.dummy_p)
7429 {
7430 /* Nothing. */
7431 }
7432 else if (this_cu->is_debug_types)
7433 build_type_psymtabs_reader (&reader, reader.info_ptr,
7434 reader.comp_unit_die);
7435 else if (want_partial_unit
7436 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
7437 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7438 reader.comp_unit_die,
7439 pretend_language);
7440
7441 this_cu->lang = reader.cu->language;
7442
7443 /* Age out any secondary CUs. */
7444 per_objfile->age_comp_units ();
7445 }
7446
7447 /* Reader function for build_type_psymtabs. */
7448
7449 static void
7450 build_type_psymtabs_reader (const struct die_reader_specs *reader,
7451 const gdb_byte *info_ptr,
7452 struct die_info *type_unit_die)
7453 {
7454 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
7455 struct dwarf2_cu *cu = reader->cu;
7456 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
7457 struct signatured_type *sig_type;
7458 struct type_unit_group *tu_group;
7459 struct attribute *attr;
7460 struct partial_die_info *first_die;
7461 CORE_ADDR lowpc, highpc;
7462 dwarf2_psymtab *pst;
7463
7464 gdb_assert (per_cu->is_debug_types);
7465 sig_type = (struct signatured_type *) per_cu;
7466
7467 if (! type_unit_die->has_children)
7468 return;
7469
7470 attr = type_unit_die->attr (DW_AT_stmt_list);
7471 tu_group = get_type_unit_group (cu, attr);
7472
7473 if (tu_group->tus == nullptr)
7474 tu_group->tus = new std::vector<signatured_type *>;
7475 tu_group->tus->push_back (sig_type);
7476
7477 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7478 pst = create_partial_symtab (per_cu, per_objfile, "");
7479 pst->anonymous = true;
7480
7481 first_die = load_partial_dies (reader, info_ptr, 1);
7482
7483 lowpc = (CORE_ADDR) -1;
7484 highpc = (CORE_ADDR) 0;
7485 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7486
7487 pst->end ();
7488 }
7489
7490 /* Struct used to sort TUs by their abbreviation table offset. */
7491
7492 struct tu_abbrev_offset
7493 {
7494 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7495 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7496 {}
7497
7498 signatured_type *sig_type;
7499 sect_offset abbrev_offset;
7500 };
7501
7502 /* Helper routine for build_type_psymtabs_1, passed to std::sort. */
7503
7504 static bool
7505 sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7506 const struct tu_abbrev_offset &b)
7507 {
7508 return a.abbrev_offset < b.abbrev_offset;
7509 }
7510
7511 /* Efficiently read all the type units.
7512 This does the bulk of the work for build_type_psymtabs.
7513
7514 The efficiency is because we sort TUs by the abbrev table they use and
7515 only read each abbrev table once. In one program there are 200K TUs
7516 sharing 8K abbrev tables.
7517
7518 The main purpose of this function is to support building the
7519 dwarf2_per_objfile->per_bfd->type_unit_groups table.
7520 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7521 can collapse the search space by grouping them by stmt_list.
7522 The savings can be significant, in the same program from above the 200K TUs
7523 share 8K stmt_list tables.
7524
7525 FUNC is expected to call get_type_unit_group, which will create the
7526 struct type_unit_group if necessary and add it to
7527 dwarf2_per_objfile->per_bfd->type_unit_groups. */
7528
7529 static void
7530 build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
7531 {
7532 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7533 abbrev_table_up abbrev_table;
7534 sect_offset abbrev_offset;
7535
7536 /* It's up to the caller to not call us multiple times. */
7537 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
7538
7539 if (per_objfile->per_bfd->all_type_units.empty ())
7540 return;
7541
7542 /* TUs typically share abbrev tables, and there can be way more TUs than
7543 abbrev tables. Sort by abbrev table to reduce the number of times we
7544 read each abbrev table in.
7545 Alternatives are to punt or to maintain a cache of abbrev tables.
7546 This is simpler and efficient enough for now.
7547
7548 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7549 symtab to use). Typically TUs with the same abbrev offset have the same
7550 stmt_list value too so in practice this should work well.
7551
7552 The basic algorithm here is:
7553
7554 sort TUs by abbrev table
7555 for each TU with same abbrev table:
7556 read abbrev table if first user
7557 read TU top level DIE
7558 [IWBN if DWO skeletons had DW_AT_stmt_list]
7559 call FUNC */
7560
7561 dwarf_read_debug_printf ("Building type unit groups ...");
7562
7563 /* Sort in a separate table to maintain the order of all_type_units
7564 for .gdb_index: TU indices directly index all_type_units. */
7565 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7566 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
7567
7568 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
7569 sorted_by_abbrev.emplace_back
7570 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
7571 sig_type->per_cu.sect_off));
7572
7573 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7574 sort_tu_by_abbrev_offset);
7575
7576 abbrev_offset = (sect_offset) ~(unsigned) 0;
7577
7578 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
7579 {
7580 /* Switch to the next abbrev table if necessary. */
7581 if (abbrev_table == NULL
7582 || tu.abbrev_offset != abbrev_offset)
7583 {
7584 abbrev_offset = tu.abbrev_offset;
7585 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
7586 abbrev_table =
7587 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
7588 ++tu_stats->nr_uniq_abbrev_tables;
7589 }
7590
7591 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
7592 abbrev_table.get (), nullptr, false);
7593 if (!reader.dummy_p)
7594 build_type_psymtabs_reader (&reader, reader.info_ptr,
7595 reader.comp_unit_die);
7596 }
7597 }
7598
7599 /* Print collected type unit statistics. */
7600
7601 static void
7602 print_tu_stats (dwarf2_per_objfile *per_objfile)
7603 {
7604 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
7605
7606 dwarf_read_debug_printf ("Type unit statistics:");
7607 dwarf_read_debug_printf (" %zu TUs",
7608 per_objfile->per_bfd->all_type_units.size ());
7609 dwarf_read_debug_printf (" %d uniq abbrev tables",
7610 tu_stats->nr_uniq_abbrev_tables);
7611 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7612 tu_stats->nr_symtabs);
7613 dwarf_read_debug_printf (" %d symtab sharers",
7614 tu_stats->nr_symtab_sharers);
7615 dwarf_read_debug_printf (" %d type units without a stmt_list",
7616 tu_stats->nr_stmt_less_type_units);
7617 dwarf_read_debug_printf (" %d all_type_units reallocs",
7618 tu_stats->nr_all_type_units_reallocs);
7619 }
7620
7621 /* Traversal function for build_type_psymtabs. */
7622
7623 static int
7624 build_type_psymtab_dependencies (void **slot, void *info)
7625 {
7626 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7627 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7628 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
7629 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
7630 dwarf2_psymtab *pst = per_cu->v.psymtab;
7631 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
7632 int i;
7633
7634 gdb_assert (len > 0);
7635 gdb_assert (per_cu->type_unit_group_p ());
7636
7637 pst->number_of_dependencies = len;
7638 pst->dependencies = per_bfd->partial_symtabs->allocate_dependencies (len);
7639 for (i = 0; i < len; ++i)
7640 {
7641 struct signatured_type *iter = tu_group->tus->at (i);
7642 gdb_assert (iter->per_cu.is_debug_types);
7643 pst->dependencies[i] = iter->per_cu.v.psymtab;
7644 iter->type_unit_group = tu_group;
7645 }
7646
7647 delete tu_group->tus;
7648 tu_group->tus = nullptr;
7649
7650 return 1;
7651 }
7652
7653 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7654 Build partial symbol tables for the .debug_types comp-units. */
7655
7656 static void
7657 build_type_psymtabs (dwarf2_per_objfile *per_objfile)
7658 {
7659 if (! create_all_type_units (per_objfile))
7660 return;
7661
7662 build_type_psymtabs_1 (per_objfile);
7663 }
7664
7665 /* Traversal function for process_skeletonless_type_unit.
7666 Read a TU in a DWO file and build partial symbols for it. */
7667
7668 static int
7669 process_skeletonless_type_unit (void **slot, void *info)
7670 {
7671 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
7672 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
7673 struct signatured_type find_entry, *entry;
7674
7675 /* If this TU doesn't exist in the global table, add it and read it in. */
7676
7677 if (per_objfile->per_bfd->signatured_types == NULL)
7678 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
7679
7680 find_entry.signature = dwo_unit->signature;
7681 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
7682 &find_entry, INSERT);
7683 /* If we've already seen this type there's nothing to do. What's happening
7684 is we're doing our own version of comdat-folding here. */
7685 if (*slot != NULL)
7686 return 1;
7687
7688 /* This does the job that create_all_type_units would have done for
7689 this TU. */
7690 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
7691 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
7692 *slot = entry;
7693
7694 /* This does the job that build_type_psymtabs_1 would have done. */
7695 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
7696 if (!reader.dummy_p)
7697 build_type_psymtabs_reader (&reader, reader.info_ptr,
7698 reader.comp_unit_die);
7699
7700 return 1;
7701 }
7702
7703 /* Traversal function for process_skeletonless_type_units. */
7704
7705 static int
7706 process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7707 {
7708 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7709
7710 if (dwo_file->tus != NULL)
7711 htab_traverse_noresize (dwo_file->tus.get (),
7712 process_skeletonless_type_unit, info);
7713
7714 return 1;
7715 }
7716
7717 /* Scan all TUs of DWO files, verifying we've processed them.
7718 This is needed in case a TU was emitted without its skeleton.
7719 Note: This can't be done until we know what all the DWO files are. */
7720
7721 static void
7722 process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
7723 {
7724 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
7725 if (get_dwp_file (per_objfile) == NULL
7726 && per_objfile->per_bfd->dwo_files != NULL)
7727 {
7728 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
7729 process_dwo_file_for_skeletonless_type_units,
7730 per_objfile);
7731 }
7732 }
7733
7734 /* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
7735
7736 static void
7737 set_partial_user (dwarf2_per_objfile *per_objfile)
7738 {
7739 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
7740 {
7741 dwarf2_psymtab *pst = per_cu->v.psymtab;
7742
7743 if (pst == NULL)
7744 continue;
7745
7746 for (int j = 0; j < pst->number_of_dependencies; ++j)
7747 {
7748 /* Set the 'user' field only if it is not already set. */
7749 if (pst->dependencies[j]->user == NULL)
7750 pst->dependencies[j]->user = pst;
7751 }
7752 }
7753 }
7754
7755 /* Build the partial symbol table by doing a quick pass through the
7756 .debug_info and .debug_abbrev sections. */
7757
7758 static void
7759 dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
7760 {
7761 struct objfile *objfile = per_objfile->objfile;
7762 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
7763
7764 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
7765 objfile_name (objfile));
7766
7767 scoped_restore restore_reading_psyms
7768 = make_scoped_restore (&per_bfd->reading_partial_symbols, true);
7769
7770 per_bfd->info.read (objfile);
7771
7772 /* Any cached compilation units will be linked by the per-objfile
7773 read_in_chain. Make sure to free them when we're done. */
7774 free_cached_comp_units freer (per_objfile);
7775
7776 build_type_psymtabs (per_objfile);
7777
7778 create_all_comp_units (per_objfile);
7779
7780 /* Create a temporary address map on a temporary obstack. We later
7781 copy this to the final obstack. */
7782 auto_obstack temp_obstack;
7783
7784 scoped_restore save_psymtabs_addrmap
7785 = make_scoped_restore (&per_bfd->partial_symtabs->psymtabs_addrmap,
7786 addrmap_create_mutable (&temp_obstack));
7787
7788 for (dwarf2_per_cu_data *per_cu : per_bfd->all_comp_units)
7789 {
7790 if (per_cu->v.psymtab != NULL)
7791 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7792 continue;
7793 process_psymtab_comp_unit (per_cu, per_objfile, false,
7794 language_minimal);
7795 }
7796
7797 /* This has to wait until we read the CUs, we need the list of DWOs. */
7798 process_skeletonless_type_units (per_objfile);
7799
7800 /* Now that all TUs have been processed we can fill in the dependencies. */
7801 if (per_bfd->type_unit_groups != NULL)
7802 {
7803 htab_traverse_noresize (per_bfd->type_unit_groups.get (),
7804 build_type_psymtab_dependencies, per_objfile);
7805 }
7806
7807 if (dwarf_read_debug > 0)
7808 print_tu_stats (per_objfile);
7809
7810 set_partial_user (per_objfile);
7811
7812 per_bfd->partial_symtabs->psymtabs_addrmap
7813 = addrmap_create_fixed (per_bfd->partial_symtabs->psymtabs_addrmap,
7814 per_bfd->partial_symtabs->obstack ());
7815 /* At this point we want to keep the address map. */
7816 save_psymtabs_addrmap.release ();
7817
7818 dwarf_read_debug_printf ("Done building psymtabs of %s",
7819 objfile_name (objfile));
7820 }
7821
7822 /* Load the partial DIEs for a secondary CU into memory.
7823 This is also used when rereading a primary CU with load_all_dies. */
7824
7825 static void
7826 load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
7827 dwarf2_per_objfile *per_objfile,
7828 dwarf2_cu *existing_cu)
7829 {
7830 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
7831
7832 if (!reader.dummy_p)
7833 {
7834 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7835 language_minimal);
7836
7837 /* Check if comp unit has_children.
7838 If so, read the rest of the partial symbols from this comp unit.
7839 If not, there's no more debug_info for this comp unit. */
7840 if (reader.comp_unit_die->has_children)
7841 load_partial_dies (&reader, reader.info_ptr, 0);
7842
7843 reader.keep ();
7844 }
7845 }
7846
7847 static void
7848 read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
7849 struct dwarf2_section_info *section,
7850 struct dwarf2_section_info *abbrev_section,
7851 unsigned int is_dwz)
7852 {
7853 const gdb_byte *info_ptr;
7854 struct objfile *objfile = per_objfile->objfile;
7855
7856 dwarf_read_debug_printf ("Reading %s for %s",
7857 section->get_name (),
7858 section->get_file_name ());
7859
7860 section->read (objfile);
7861
7862 info_ptr = section->buffer;
7863
7864 while (info_ptr < section->buffer + section->size)
7865 {
7866 struct dwarf2_per_cu_data *this_cu;
7867
7868 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
7869
7870 comp_unit_head cu_header;
7871 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
7872 abbrev_section, info_ptr,
7873 rcuh_kind::COMPILE);
7874
7875 /* Save the compilation unit for later lookup. */
7876 if (cu_header.unit_type != DW_UT_type)
7877 this_cu = per_objfile->per_bfd->allocate_per_cu ();
7878 else
7879 {
7880 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
7881 sig_type->signature = cu_header.signature;
7882 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7883 this_cu = &sig_type->per_cu;
7884 }
7885 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
7886 this_cu->sect_off = sect_off;
7887 this_cu->length = cu_header.length + cu_header.initial_length_size;
7888 this_cu->is_dwz = is_dwz;
7889 this_cu->section = section;
7890
7891 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
7892
7893 info_ptr = info_ptr + this_cu->length;
7894 }
7895 }
7896
7897 /* Create a list of all compilation units in OBJFILE.
7898 This is only done for -readnow and building partial symtabs. */
7899
7900 static void
7901 create_all_comp_units (dwarf2_per_objfile *per_objfile)
7902 {
7903 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
7904 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
7905 &per_objfile->per_bfd->abbrev, 0);
7906
7907 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
7908 if (dwz != NULL)
7909 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
7910 }
7911
7912 /* Process all loaded DIEs for compilation unit CU, starting at
7913 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
7914 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
7915 DW_AT_ranges). See the comments of add_partial_subprogram on how
7916 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
7917
7918 static void
7919 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
7920 CORE_ADDR *highpc, int set_addrmap,
7921 struct dwarf2_cu *cu)
7922 {
7923 struct partial_die_info *pdi;
7924
7925 /* Now, march along the PDI's, descending into ones which have
7926 interesting children but skipping the children of the other ones,
7927 until we reach the end of the compilation unit. */
7928
7929 pdi = first_die;
7930
7931 while (pdi != NULL)
7932 {
7933 pdi->fixup (cu);
7934
7935 /* Anonymous namespaces or modules have no name but have interesting
7936 children, so we need to look at them. Ditto for anonymous
7937 enums. */
7938
7939 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
7940 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
7941 || pdi->tag == DW_TAG_imported_unit
7942 || pdi->tag == DW_TAG_inlined_subroutine)
7943 {
7944 switch (pdi->tag)
7945 {
7946 case DW_TAG_subprogram:
7947 case DW_TAG_inlined_subroutine:
7948 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
7949 if (cu->language == language_cplus)
7950 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7951 set_addrmap, cu);
7952 break;
7953 case DW_TAG_constant:
7954 case DW_TAG_variable:
7955 case DW_TAG_typedef:
7956 case DW_TAG_union_type:
7957 if (!pdi->is_declaration
7958 || (pdi->tag == DW_TAG_variable && pdi->is_external))
7959 {
7960 add_partial_symbol (pdi, cu);
7961 }
7962 break;
7963 case DW_TAG_class_type:
7964 case DW_TAG_interface_type:
7965 case DW_TAG_structure_type:
7966 if (!pdi->is_declaration)
7967 {
7968 add_partial_symbol (pdi, cu);
7969 }
7970 if ((cu->language == language_rust
7971 || cu->language == language_cplus) && pdi->has_children)
7972 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7973 set_addrmap, cu);
7974 break;
7975 case DW_TAG_enumeration_type:
7976 if (!pdi->is_declaration)
7977 add_partial_enumeration (pdi, cu);
7978 break;
7979 case DW_TAG_base_type:
7980 case DW_TAG_subrange_type:
7981 /* File scope base type definitions are added to the partial
7982 symbol table. */
7983 add_partial_symbol (pdi, cu);
7984 break;
7985 case DW_TAG_namespace:
7986 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
7987 break;
7988 case DW_TAG_module:
7989 if (!pdi->is_declaration)
7990 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
7991 break;
7992 case DW_TAG_imported_unit:
7993 {
7994 struct dwarf2_per_cu_data *per_cu;
7995
7996 /* For now we don't handle imported units in type units. */
7997 if (cu->per_cu->is_debug_types)
7998 {
7999 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8000 " supported in type units [in module %s]"),
8001 objfile_name (cu->per_objfile->objfile));
8002 }
8003
8004 per_cu = dwarf2_find_containing_comp_unit
8005 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
8006
8007 /* Go read the partial unit, if needed. */
8008 if (per_cu->v.psymtab == NULL)
8009 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8010 cu->language);
8011
8012 cu->per_cu->imported_symtabs_push (per_cu);
8013 }
8014 break;
8015 case DW_TAG_imported_declaration:
8016 add_partial_symbol (pdi, cu);
8017 break;
8018 default:
8019 break;
8020 }
8021 }
8022
8023 /* If the die has a sibling, skip to the sibling. */
8024
8025 pdi = pdi->die_sibling;
8026 }
8027 }
8028
8029 /* Functions used to compute the fully scoped name of a partial DIE.
8030
8031 Normally, this is simple. For C++, the parent DIE's fully scoped
8032 name is concatenated with "::" and the partial DIE's name.
8033 Enumerators are an exception; they use the scope of their parent
8034 enumeration type, i.e. the name of the enumeration type is not
8035 prepended to the enumerator.
8036
8037 There are two complexities. One is DW_AT_specification; in this
8038 case "parent" means the parent of the target of the specification,
8039 instead of the direct parent of the DIE. The other is compilers
8040 which do not emit DW_TAG_namespace; in this case we try to guess
8041 the fully qualified name of structure types from their members'
8042 linkage names. This must be done using the DIE's children rather
8043 than the children of any DW_AT_specification target. We only need
8044 to do this for structures at the top level, i.e. if the target of
8045 any DW_AT_specification (if any; otherwise the DIE itself) does not
8046 have a parent. */
8047
8048 /* Compute the scope prefix associated with PDI's parent, in
8049 compilation unit CU. The result will be allocated on CU's
8050 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8051 field. NULL is returned if no prefix is necessary. */
8052 static const char *
8053 partial_die_parent_scope (struct partial_die_info *pdi,
8054 struct dwarf2_cu *cu)
8055 {
8056 const char *grandparent_scope;
8057 struct partial_die_info *parent, *real_pdi;
8058
8059 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8060 then this means the parent of the specification DIE. */
8061
8062 real_pdi = pdi;
8063 while (real_pdi->has_specification)
8064 {
8065 auto res = find_partial_die (real_pdi->spec_offset,
8066 real_pdi->spec_is_dwz, cu);
8067 real_pdi = res.pdi;
8068 cu = res.cu;
8069 }
8070
8071 parent = real_pdi->die_parent;
8072 if (parent == NULL)
8073 return NULL;
8074
8075 if (parent->scope_set)
8076 return parent->scope;
8077
8078 parent->fixup (cu);
8079
8080 grandparent_scope = partial_die_parent_scope (parent, cu);
8081
8082 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8083 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8084 Work around this problem here. */
8085 if (cu->language == language_cplus
8086 && parent->tag == DW_TAG_namespace
8087 && strcmp (parent->name (cu), "::") == 0
8088 && grandparent_scope == NULL)
8089 {
8090 parent->scope = NULL;
8091 parent->scope_set = 1;
8092 return NULL;
8093 }
8094
8095 /* Nested subroutines in Fortran get a prefix. */
8096 if (pdi->tag == DW_TAG_enumerator)
8097 /* Enumerators should not get the name of the enumeration as a prefix. */
8098 parent->scope = grandparent_scope;
8099 else if (parent->tag == DW_TAG_namespace
8100 || parent->tag == DW_TAG_module
8101 || parent->tag == DW_TAG_structure_type
8102 || parent->tag == DW_TAG_class_type
8103 || parent->tag == DW_TAG_interface_type
8104 || parent->tag == DW_TAG_union_type
8105 || parent->tag == DW_TAG_enumeration_type
8106 || (cu->language == language_fortran
8107 && parent->tag == DW_TAG_subprogram
8108 && pdi->tag == DW_TAG_subprogram))
8109 {
8110 if (grandparent_scope == NULL)
8111 parent->scope = parent->name (cu);
8112 else
8113 parent->scope = typename_concat (&cu->comp_unit_obstack,
8114 grandparent_scope,
8115 parent->name (cu), 0, cu);
8116 }
8117 else
8118 {
8119 /* FIXME drow/2004-04-01: What should we be doing with
8120 function-local names? For partial symbols, we should probably be
8121 ignoring them. */
8122 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8123 dwarf_tag_name (parent->tag),
8124 sect_offset_str (pdi->sect_off));
8125 parent->scope = grandparent_scope;
8126 }
8127
8128 parent->scope_set = 1;
8129 return parent->scope;
8130 }
8131
8132 /* Return the fully scoped name associated with PDI, from compilation unit
8133 CU. The result will be allocated with malloc. */
8134
8135 static gdb::unique_xmalloc_ptr<char>
8136 partial_die_full_name (struct partial_die_info *pdi,
8137 struct dwarf2_cu *cu)
8138 {
8139 const char *parent_scope;
8140
8141 /* If this is a template instantiation, we can not work out the
8142 template arguments from partial DIEs. So, unfortunately, we have
8143 to go through the full DIEs. At least any work we do building
8144 types here will be reused if full symbols are loaded later. */
8145 if (pdi->has_template_arguments)
8146 {
8147 pdi->fixup (cu);
8148
8149 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
8150 {
8151 struct die_info *die;
8152 struct attribute attr;
8153 struct dwarf2_cu *ref_cu = cu;
8154
8155 /* DW_FORM_ref_addr is using section offset. */
8156 attr.name = (enum dwarf_attribute) 0;
8157 attr.form = DW_FORM_ref_addr;
8158 attr.u.unsnd = to_underlying (pdi->sect_off);
8159 die = follow_die_ref (NULL, &attr, &ref_cu);
8160
8161 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8162 }
8163 }
8164
8165 parent_scope = partial_die_parent_scope (pdi, cu);
8166 if (parent_scope == NULL)
8167 return NULL;
8168 else
8169 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8170 pdi->name (cu),
8171 0, cu));
8172 }
8173
8174 static void
8175 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
8176 {
8177 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8178 struct objfile *objfile = per_objfile->objfile;
8179 struct gdbarch *gdbarch = objfile->arch ();
8180 CORE_ADDR addr = 0;
8181 const char *actual_name = NULL;
8182 CORE_ADDR baseaddr;
8183
8184 baseaddr = objfile->text_section_offset ();
8185
8186 gdb::unique_xmalloc_ptr<char> built_actual_name
8187 = partial_die_full_name (pdi, cu);
8188 if (built_actual_name != NULL)
8189 actual_name = built_actual_name.get ();
8190
8191 if (actual_name == NULL)
8192 actual_name = pdi->name (cu);
8193
8194 partial_symbol psymbol;
8195 memset (&psymbol, 0, sizeof (psymbol));
8196 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8197 psymbol.ginfo.set_section_index (-1);
8198
8199 /* The code below indicates that the psymbol should be installed by
8200 setting this. */
8201 gdb::optional<psymbol_placement> where;
8202
8203 switch (pdi->tag)
8204 {
8205 case DW_TAG_inlined_subroutine:
8206 case DW_TAG_subprogram:
8207 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8208 - baseaddr);
8209 if (pdi->is_external
8210 || cu->language == language_ada
8211 || (cu->language == language_fortran
8212 && pdi->die_parent != NULL
8213 && pdi->die_parent->tag == DW_TAG_subprogram))
8214 {
8215 /* Normally, only "external" DIEs are part of the global scope.
8216 But in Ada and Fortran, we want to be able to access nested
8217 procedures globally. So all Ada and Fortran subprograms are
8218 stored in the global scope. */
8219 where = psymbol_placement::GLOBAL;
8220 }
8221 else
8222 where = psymbol_placement::STATIC;
8223
8224 psymbol.domain = VAR_DOMAIN;
8225 psymbol.aclass = LOC_BLOCK;
8226 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8227 psymbol.ginfo.value.address = addr;
8228
8229 if (pdi->main_subprogram && actual_name != NULL)
8230 set_objfile_main_name (objfile, actual_name, cu->language);
8231 break;
8232 case DW_TAG_constant:
8233 psymbol.domain = VAR_DOMAIN;
8234 psymbol.aclass = LOC_STATIC;
8235 where = (pdi->is_external
8236 ? psymbol_placement::GLOBAL
8237 : psymbol_placement::STATIC);
8238 break;
8239 case DW_TAG_variable:
8240 if (pdi->d.locdesc)
8241 addr = decode_locdesc (pdi->d.locdesc, cu);
8242
8243 if (pdi->d.locdesc
8244 && addr == 0
8245 && !per_objfile->per_bfd->has_section_at_zero)
8246 {
8247 /* A global or static variable may also have been stripped
8248 out by the linker if unused, in which case its address
8249 will be nullified; do not add such variables into partial
8250 symbol table then. */
8251 }
8252 else if (pdi->is_external)
8253 {
8254 /* Global Variable.
8255 Don't enter into the minimal symbol tables as there is
8256 a minimal symbol table entry from the ELF symbols already.
8257 Enter into partial symbol table if it has a location
8258 descriptor or a type.
8259 If the location descriptor is missing, new_symbol will create
8260 a LOC_UNRESOLVED symbol, the address of the variable will then
8261 be determined from the minimal symbol table whenever the variable
8262 is referenced.
8263 The address for the partial symbol table entry is not
8264 used by GDB, but it comes in handy for debugging partial symbol
8265 table building. */
8266
8267 if (pdi->d.locdesc || pdi->has_type)
8268 {
8269 psymbol.domain = VAR_DOMAIN;
8270 psymbol.aclass = LOC_STATIC;
8271 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8272 psymbol.ginfo.value.address = addr;
8273 where = psymbol_placement::GLOBAL;
8274 }
8275 }
8276 else
8277 {
8278 int has_loc = pdi->d.locdesc != NULL;
8279
8280 /* Static Variable. Skip symbols whose value we cannot know (those
8281 without location descriptors or constant values). */
8282 if (!has_loc && !pdi->has_const_value)
8283 return;
8284
8285 psymbol.domain = VAR_DOMAIN;
8286 psymbol.aclass = LOC_STATIC;
8287 psymbol.ginfo.set_section_index (SECT_OFF_TEXT (objfile));
8288 if (has_loc)
8289 psymbol.ginfo.value.address = addr;
8290 where = psymbol_placement::STATIC;
8291 }
8292 break;
8293 case DW_TAG_array_type:
8294 case DW_TAG_typedef:
8295 case DW_TAG_base_type:
8296 case DW_TAG_subrange_type:
8297 psymbol.domain = VAR_DOMAIN;
8298 psymbol.aclass = LOC_TYPEDEF;
8299 where = psymbol_placement::STATIC;
8300 break;
8301 case DW_TAG_imported_declaration:
8302 case DW_TAG_namespace:
8303 psymbol.domain = VAR_DOMAIN;
8304 psymbol.aclass = LOC_TYPEDEF;
8305 where = psymbol_placement::GLOBAL;
8306 break;
8307 case DW_TAG_module:
8308 /* With Fortran 77 there might be a "BLOCK DATA" module
8309 available without any name. If so, we skip the module as it
8310 doesn't bring any value. */
8311 if (actual_name != nullptr)
8312 {
8313 psymbol.domain = MODULE_DOMAIN;
8314 psymbol.aclass = LOC_TYPEDEF;
8315 where = psymbol_placement::GLOBAL;
8316 }
8317 break;
8318 case DW_TAG_class_type:
8319 case DW_TAG_interface_type:
8320 case DW_TAG_structure_type:
8321 case DW_TAG_union_type:
8322 case DW_TAG_enumeration_type:
8323 /* Skip external references. The DWARF standard says in the section
8324 about "Structure, Union, and Class Type Entries": "An incomplete
8325 structure, union or class type is represented by a structure,
8326 union or class entry that does not have a byte size attribute
8327 and that has a DW_AT_declaration attribute." */
8328 if (!pdi->has_byte_size && pdi->is_declaration)
8329 return;
8330
8331 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8332 static vs. global. */
8333 psymbol.domain = STRUCT_DOMAIN;
8334 psymbol.aclass = LOC_TYPEDEF;
8335 where = (cu->language == language_cplus
8336 ? psymbol_placement::GLOBAL
8337 : psymbol_placement::STATIC);
8338 break;
8339 case DW_TAG_enumerator:
8340 psymbol.domain = VAR_DOMAIN;
8341 psymbol.aclass = LOC_CONST;
8342 where = (cu->language == language_cplus
8343 ? psymbol_placement::GLOBAL
8344 : psymbol_placement::STATIC);
8345 break;
8346 default:
8347 break;
8348 }
8349
8350 if (where.has_value ())
8351 {
8352 if (built_actual_name != nullptr)
8353 actual_name = objfile->intern (actual_name);
8354 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8355 psymbol.ginfo.set_linkage_name (actual_name);
8356 else
8357 {
8358 psymbol.ginfo.set_demangled_name (actual_name,
8359 &objfile->objfile_obstack);
8360 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8361 }
8362 cu->per_cu->v.psymtab->add_psymbol
8363 (psymbol, *where, per_objfile->per_bfd->partial_symtabs.get (),
8364 objfile);
8365 }
8366 }
8367
8368 /* Read a partial die corresponding to a namespace; also, add a symbol
8369 corresponding to that namespace to the symbol table. NAMESPACE is
8370 the name of the enclosing namespace. */
8371
8372 static void
8373 add_partial_namespace (struct partial_die_info *pdi,
8374 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8375 int set_addrmap, struct dwarf2_cu *cu)
8376 {
8377 /* Add a symbol for the namespace. */
8378
8379 add_partial_symbol (pdi, cu);
8380
8381 /* Now scan partial symbols in that namespace. */
8382
8383 if (pdi->has_children)
8384 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8385 }
8386
8387 /* Read a partial die corresponding to a Fortran module. */
8388
8389 static void
8390 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
8391 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
8392 {
8393 /* Add a symbol for the namespace. */
8394
8395 add_partial_symbol (pdi, cu);
8396
8397 /* Now scan partial symbols in that module. */
8398
8399 if (pdi->has_children)
8400 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
8401 }
8402
8403 /* Read a partial die corresponding to a subprogram or an inlined
8404 subprogram and create a partial symbol for that subprogram.
8405 When the CU language allows it, this routine also defines a partial
8406 symbol for each nested subprogram that this subprogram contains.
8407 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8408 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
8409
8410 PDI may also be a lexical block, in which case we simply search
8411 recursively for subprograms defined inside that lexical block.
8412 Again, this is only performed when the CU language allows this
8413 type of definitions. */
8414
8415 static void
8416 add_partial_subprogram (struct partial_die_info *pdi,
8417 CORE_ADDR *lowpc, CORE_ADDR *highpc,
8418 int set_addrmap, struct dwarf2_cu *cu)
8419 {
8420 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
8421 {
8422 if (pdi->has_pc_info)
8423 {
8424 if (pdi->lowpc < *lowpc)
8425 *lowpc = pdi->lowpc;
8426 if (pdi->highpc > *highpc)
8427 *highpc = pdi->highpc;
8428 if (set_addrmap)
8429 {
8430 struct objfile *objfile = cu->per_objfile->objfile;
8431 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
8432 struct gdbarch *gdbarch = objfile->arch ();
8433 CORE_ADDR baseaddr;
8434 CORE_ADDR this_highpc;
8435 CORE_ADDR this_lowpc;
8436
8437 baseaddr = objfile->text_section_offset ();
8438 this_lowpc
8439 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8440 pdi->lowpc + baseaddr)
8441 - baseaddr);
8442 this_highpc
8443 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8444 pdi->highpc + baseaddr)
8445 - baseaddr);
8446 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
8447 this_lowpc, this_highpc - 1,
8448 cu->per_cu->v.psymtab);
8449 }
8450 }
8451
8452 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8453 {
8454 if (!pdi->is_declaration)
8455 /* Ignore subprogram DIEs that do not have a name, they are
8456 illegal. Do not emit a complaint at this point, we will
8457 do so when we convert this psymtab into a symtab. */
8458 if (pdi->name (cu))
8459 add_partial_symbol (pdi, cu);
8460 }
8461 }
8462
8463 if (! pdi->has_children)
8464 return;
8465
8466 if (cu->language == language_ada || cu->language == language_fortran)
8467 {
8468 pdi = pdi->die_child;
8469 while (pdi != NULL)
8470 {
8471 pdi->fixup (cu);
8472 if (pdi->tag == DW_TAG_subprogram
8473 || pdi->tag == DW_TAG_inlined_subroutine
8474 || pdi->tag == DW_TAG_lexical_block)
8475 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
8476 pdi = pdi->die_sibling;
8477 }
8478 }
8479 }
8480
8481 /* Read a partial die corresponding to an enumeration type. */
8482
8483 static void
8484 add_partial_enumeration (struct partial_die_info *enum_pdi,
8485 struct dwarf2_cu *cu)
8486 {
8487 struct partial_die_info *pdi;
8488
8489 if (enum_pdi->name (cu) != NULL)
8490 add_partial_symbol (enum_pdi, cu);
8491
8492 pdi = enum_pdi->die_child;
8493 while (pdi)
8494 {
8495 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
8496 complaint (_("malformed enumerator DIE ignored"));
8497 else
8498 add_partial_symbol (pdi, cu);
8499 pdi = pdi->die_sibling;
8500 }
8501 }
8502
8503 /* Return the initial uleb128 in the die at INFO_PTR. */
8504
8505 static unsigned int
8506 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
8507 {
8508 unsigned int bytes_read;
8509
8510 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8511 }
8512
8513 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8514 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8515
8516 Return the corresponding abbrev, or NULL if the number is zero (indicating
8517 an empty DIE). In either case *BYTES_READ will be set to the length of
8518 the initial number. */
8519
8520 static const struct abbrev_info *
8521 peek_die_abbrev (const die_reader_specs &reader,
8522 const gdb_byte *info_ptr, unsigned int *bytes_read)
8523 {
8524 dwarf2_cu *cu = reader.cu;
8525 bfd *abfd = reader.abfd;
8526 unsigned int abbrev_number
8527 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
8528
8529 if (abbrev_number == 0)
8530 return NULL;
8531
8532 const abbrev_info *abbrev
8533 = reader.abbrev_table->lookup_abbrev (abbrev_number);
8534 if (!abbrev)
8535 {
8536 error (_("Dwarf Error: Could not find abbrev number %d in %s"
8537 " at offset %s [in module %s]"),
8538 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
8539 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
8540 }
8541
8542 return abbrev;
8543 }
8544
8545 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8546 Returns a pointer to the end of a series of DIEs, terminated by an empty
8547 DIE. Any children of the skipped DIEs will also be skipped. */
8548
8549 static const gdb_byte *
8550 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
8551 {
8552 while (1)
8553 {
8554 unsigned int bytes_read;
8555 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
8556 &bytes_read);
8557
8558 if (abbrev == NULL)
8559 return info_ptr + bytes_read;
8560 else
8561 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
8562 }
8563 }
8564
8565 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8566 INFO_PTR should point just after the initial uleb128 of a DIE, and the
8567 abbrev corresponding to that skipped uleb128 should be passed in
8568 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8569 children. */
8570
8571 static const gdb_byte *
8572 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
8573 const struct abbrev_info *abbrev)
8574 {
8575 unsigned int bytes_read;
8576 struct attribute attr;
8577 bfd *abfd = reader->abfd;
8578 struct dwarf2_cu *cu = reader->cu;
8579 const gdb_byte *buffer = reader->buffer;
8580 const gdb_byte *buffer_end = reader->buffer_end;
8581 unsigned int form, i;
8582
8583 for (i = 0; i < abbrev->num_attrs; i++)
8584 {
8585 /* The only abbrev we care about is DW_AT_sibling. */
8586 if (abbrev->attrs[i].name == DW_AT_sibling)
8587 {
8588 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
8589 if (attr.form == DW_FORM_ref_addr)
8590 complaint (_("ignoring absolute DW_AT_sibling"));
8591 else
8592 {
8593 sect_offset off = attr.get_ref_die_offset ();
8594 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
8595
8596 if (sibling_ptr < info_ptr)
8597 complaint (_("DW_AT_sibling points backwards"));
8598 else if (sibling_ptr > reader->buffer_end)
8599 reader->die_section->overflow_complaint ();
8600 else
8601 return sibling_ptr;
8602 }
8603 }
8604
8605 /* If it isn't DW_AT_sibling, skip this attribute. */
8606 form = abbrev->attrs[i].form;
8607 skip_attribute:
8608 switch (form)
8609 {
8610 case DW_FORM_ref_addr:
8611 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8612 and later it is offset sized. */
8613 if (cu->header.version == 2)
8614 info_ptr += cu->header.addr_size;
8615 else
8616 info_ptr += cu->header.offset_size;
8617 break;
8618 case DW_FORM_GNU_ref_alt:
8619 info_ptr += cu->header.offset_size;
8620 break;
8621 case DW_FORM_addr:
8622 info_ptr += cu->header.addr_size;
8623 break;
8624 case DW_FORM_data1:
8625 case DW_FORM_ref1:
8626 case DW_FORM_flag:
8627 case DW_FORM_strx1:
8628 info_ptr += 1;
8629 break;
8630 case DW_FORM_flag_present:
8631 case DW_FORM_implicit_const:
8632 break;
8633 case DW_FORM_data2:
8634 case DW_FORM_ref2:
8635 case DW_FORM_strx2:
8636 info_ptr += 2;
8637 break;
8638 case DW_FORM_strx3:
8639 info_ptr += 3;
8640 break;
8641 case DW_FORM_data4:
8642 case DW_FORM_ref4:
8643 case DW_FORM_strx4:
8644 info_ptr += 4;
8645 break;
8646 case DW_FORM_data8:
8647 case DW_FORM_ref8:
8648 case DW_FORM_ref_sig8:
8649 info_ptr += 8;
8650 break;
8651 case DW_FORM_data16:
8652 info_ptr += 16;
8653 break;
8654 case DW_FORM_string:
8655 read_direct_string (abfd, info_ptr, &bytes_read);
8656 info_ptr += bytes_read;
8657 break;
8658 case DW_FORM_sec_offset:
8659 case DW_FORM_strp:
8660 case DW_FORM_GNU_strp_alt:
8661 info_ptr += cu->header.offset_size;
8662 break;
8663 case DW_FORM_exprloc:
8664 case DW_FORM_block:
8665 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8666 info_ptr += bytes_read;
8667 break;
8668 case DW_FORM_block1:
8669 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8670 break;
8671 case DW_FORM_block2:
8672 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8673 break;
8674 case DW_FORM_block4:
8675 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8676 break;
8677 case DW_FORM_addrx:
8678 case DW_FORM_strx:
8679 case DW_FORM_sdata:
8680 case DW_FORM_udata:
8681 case DW_FORM_ref_udata:
8682 case DW_FORM_GNU_addr_index:
8683 case DW_FORM_GNU_str_index:
8684 case DW_FORM_rnglistx:
8685 case DW_FORM_loclistx:
8686 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
8687 break;
8688 case DW_FORM_indirect:
8689 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8690 info_ptr += bytes_read;
8691 /* We need to continue parsing from here, so just go back to
8692 the top. */
8693 goto skip_attribute;
8694
8695 default:
8696 error (_("Dwarf Error: Cannot handle %s "
8697 "in DWARF reader [in module %s]"),
8698 dwarf_form_name (form),
8699 bfd_get_filename (abfd));
8700 }
8701 }
8702
8703 if (abbrev->has_children)
8704 return skip_children (reader, info_ptr);
8705 else
8706 return info_ptr;
8707 }
8708
8709 /* Locate ORIG_PDI's sibling.
8710 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
8711
8712 static const gdb_byte *
8713 locate_pdi_sibling (const struct die_reader_specs *reader,
8714 struct partial_die_info *orig_pdi,
8715 const gdb_byte *info_ptr)
8716 {
8717 /* Do we know the sibling already? */
8718
8719 if (orig_pdi->sibling)
8720 return orig_pdi->sibling;
8721
8722 /* Are there any children to deal with? */
8723
8724 if (!orig_pdi->has_children)
8725 return info_ptr;
8726
8727 /* Skip the children the long way. */
8728
8729 return skip_children (reader, info_ptr);
8730 }
8731
8732 /* Expand this partial symbol table into a full symbol table. SELF is
8733 not NULL. */
8734
8735 void
8736 dwarf2_psymtab::read_symtab (struct objfile *objfile)
8737 {
8738 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8739
8740 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
8741
8742 /* If this psymtab is constructed from a debug-only objfile, the
8743 has_section_at_zero flag will not necessarily be correct. We
8744 can get the correct value for this flag by looking at the data
8745 associated with the (presumably stripped) associated objfile. */
8746 if (objfile->separate_debug_objfile_backlink)
8747 {
8748 dwarf2_per_objfile *per_objfile_backlink
8749 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
8750
8751 per_objfile->per_bfd->has_section_at_zero
8752 = per_objfile_backlink->per_bfd->has_section_at_zero;
8753 }
8754
8755 expand_psymtab (objfile);
8756
8757 process_cu_includes (per_objfile);
8758 }
8759 \f
8760 /* Reading in full CUs. */
8761
8762 /* Add PER_CU to the queue. */
8763
8764 static void
8765 queue_comp_unit (dwarf2_per_cu_data *per_cu,
8766 dwarf2_per_objfile *per_objfile,
8767 enum language pretend_language)
8768 {
8769 per_cu->queued = 1;
8770
8771 gdb_assert (per_objfile->per_bfd->queue.has_value ());
8772 per_cu->per_bfd->queue->emplace (per_cu, per_objfile, pretend_language);
8773 }
8774
8775 /* If PER_CU is not yet expanded of queued for expansion, add it to the queue.
8776
8777 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8778 dependency.
8779
8780 Return true if maybe_queue_comp_unit requires the caller to load the CU's
8781 DIEs, false otherwise.
8782
8783 Explanation: there is an invariant that if a CU is queued for expansion
8784 (present in `dwarf2_per_bfd::queue`), then its DIEs are loaded
8785 (a dwarf2_cu object exists for this CU, and `dwarf2_per_objfile::get_cu`
8786 returns non-nullptr). If the CU gets enqueued by this function but its DIEs
8787 are not yet loaded, the the caller must load the CU's DIEs to ensure the
8788 invariant is respected.
8789
8790 The caller is therefore not required to load the CU's DIEs (we return false)
8791 if:
8792
8793 - the CU is already expanded, and therefore does not get enqueued
8794 - the CU gets enqueued for expansion, but its DIEs are already loaded
8795
8796 Note that the caller should not use this function's return value as an
8797 indicator of whether the CU's DIEs are loaded right now, it should check
8798 that by calling `dwarf2_per_objfile::get_cu` instead. */
8799
8800 static int
8801 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
8802 dwarf2_per_cu_data *per_cu,
8803 dwarf2_per_objfile *per_objfile,
8804 enum language pretend_language)
8805 {
8806 /* We may arrive here during partial symbol reading, if we need full
8807 DIEs to process an unusual case (e.g. template arguments). Do
8808 not queue PER_CU, just tell our caller to load its DIEs. */
8809 if (per_cu->per_bfd->reading_partial_symbols)
8810 {
8811 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8812
8813 if (cu == NULL || cu->dies == NULL)
8814 return 1;
8815 return 0;
8816 }
8817
8818 /* Mark the dependence relation so that we don't flush PER_CU
8819 too early. */
8820 if (dependent_cu != NULL)
8821 dwarf2_add_dependence (dependent_cu, per_cu);
8822
8823 /* If it's already on the queue, we have nothing to do. */
8824 if (per_cu->queued)
8825 {
8826 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
8827 loaded. */
8828 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
8829
8830 /* If the CU is queued for expansion, it should not already be
8831 expanded. */
8832 gdb_assert (!per_objfile->symtab_set_p (per_cu));
8833
8834 /* The DIEs are already loaded, the caller doesn't need to do it. */
8835 return 0;
8836 }
8837
8838 bool queued = false;
8839 if (!per_objfile->symtab_set_p (per_cu))
8840 {
8841 /* Add it to the queue. */
8842 queue_comp_unit (per_cu, per_objfile, pretend_language);
8843 queued = true;
8844 }
8845
8846 /* If the compilation unit is already loaded, just mark it as
8847 used. */
8848 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8849 if (cu != nullptr)
8850 cu->last_used = 0;
8851
8852 /* Ask the caller to load the CU's DIEs if the CU got enqueued for expansion
8853 and the DIEs are not already loaded. */
8854 return queued && cu == nullptr;
8855 }
8856
8857 /* Process the queue. */
8858
8859 static void
8860 process_queue (dwarf2_per_objfile *per_objfile)
8861 {
8862 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
8863 objfile_name (per_objfile->objfile));
8864
8865 /* The queue starts out with one item, but following a DIE reference
8866 may load a new CU, adding it to the end of the queue. */
8867 while (!per_objfile->per_bfd->queue->empty ())
8868 {
8869 dwarf2_queue_item &item = per_objfile->per_bfd->queue->front ();
8870 dwarf2_per_cu_data *per_cu = item.per_cu;
8871
8872 if (!per_objfile->symtab_set_p (per_cu))
8873 {
8874 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8875
8876 /* Skip dummy CUs. */
8877 if (cu != nullptr)
8878 {
8879 unsigned int debug_print_threshold;
8880 char buf[100];
8881
8882 if (per_cu->is_debug_types)
8883 {
8884 struct signatured_type *sig_type =
8885 (struct signatured_type *) per_cu;
8886
8887 sprintf (buf, "TU %s at offset %s",
8888 hex_string (sig_type->signature),
8889 sect_offset_str (per_cu->sect_off));
8890 /* There can be 100s of TUs.
8891 Only print them in verbose mode. */
8892 debug_print_threshold = 2;
8893 }
8894 else
8895 {
8896 sprintf (buf, "CU at offset %s",
8897 sect_offset_str (per_cu->sect_off));
8898 debug_print_threshold = 1;
8899 }
8900
8901 if (dwarf_read_debug >= debug_print_threshold)
8902 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
8903
8904 if (per_cu->is_debug_types)
8905 process_full_type_unit (cu, item.pretend_language);
8906 else
8907 process_full_comp_unit (cu, item.pretend_language);
8908
8909 if (dwarf_read_debug >= debug_print_threshold)
8910 dwarf_read_debug_printf ("Done expanding %s", buf);
8911 }
8912 }
8913
8914 per_cu->queued = 0;
8915 per_objfile->per_bfd->queue->pop ();
8916 }
8917
8918 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
8919 objfile_name (per_objfile->objfile));
8920 }
8921
8922 /* Read in full symbols for PST, and anything it depends on. */
8923
8924 void
8925 dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
8926 {
8927 gdb_assert (!readin_p (objfile));
8928
8929 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8930 free_cached_comp_units freer (per_objfile);
8931 expand_dependencies (objfile);
8932
8933 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
8934 gdb_assert (get_compunit_symtab (objfile) != nullptr);
8935 }
8936
8937 /* See psympriv.h. */
8938
8939 bool
8940 dwarf2_psymtab::readin_p (struct objfile *objfile) const
8941 {
8942 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8943 return per_objfile->symtab_set_p (per_cu_data);
8944 }
8945
8946 /* See psympriv.h. */
8947
8948 compunit_symtab *
8949 dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
8950 {
8951 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
8952 return per_objfile->get_symtab (per_cu_data);
8953 }
8954
8955 /* Trivial hash function for die_info: the hash value of a DIE
8956 is its offset in .debug_info for this objfile. */
8957
8958 static hashval_t
8959 die_hash (const void *item)
8960 {
8961 const struct die_info *die = (const struct die_info *) item;
8962
8963 return to_underlying (die->sect_off);
8964 }
8965
8966 /* Trivial comparison function for die_info structures: two DIEs
8967 are equal if they have the same offset. */
8968
8969 static int
8970 die_eq (const void *item_lhs, const void *item_rhs)
8971 {
8972 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8973 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
8974
8975 return die_lhs->sect_off == die_rhs->sect_off;
8976 }
8977
8978 /* Load the DIEs associated with PER_CU into memory.
8979
8980 In some cases, the caller, while reading partial symbols, will need to load
8981 the full symbols for the CU for some reason. It will already have a
8982 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
8983 rather than creating a new one. */
8984
8985 static void
8986 load_full_comp_unit (dwarf2_per_cu_data *this_cu,
8987 dwarf2_per_objfile *per_objfile,
8988 dwarf2_cu *existing_cu,
8989 bool skip_partial,
8990 enum language pretend_language)
8991 {
8992 gdb_assert (! this_cu->is_debug_types);
8993
8994 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
8995 if (reader.dummy_p)
8996 return;
8997
8998 struct dwarf2_cu *cu = reader.cu;
8999 const gdb_byte *info_ptr = reader.info_ptr;
9000
9001 gdb_assert (cu->die_hash == NULL);
9002 cu->die_hash =
9003 htab_create_alloc_ex (cu->header.length / 12,
9004 die_hash,
9005 die_eq,
9006 NULL,
9007 &cu->comp_unit_obstack,
9008 hashtab_obstack_allocate,
9009 dummy_obstack_deallocate);
9010
9011 if (reader.comp_unit_die->has_children)
9012 reader.comp_unit_die->child
9013 = read_die_and_siblings (&reader, reader.info_ptr,
9014 &info_ptr, reader.comp_unit_die);
9015 cu->dies = reader.comp_unit_die;
9016 /* comp_unit_die is not stored in die_hash, no need. */
9017
9018 /* We try not to read any attributes in this function, because not
9019 all CUs needed for references have been loaded yet, and symbol
9020 table processing isn't initialized. But we have to set the CU language,
9021 or we won't be able to build types correctly.
9022 Similarly, if we do not read the producer, we can not apply
9023 producer-specific interpretation. */
9024 prepare_one_comp_unit (cu, cu->dies, pretend_language);
9025
9026 reader.keep ();
9027 }
9028
9029 /* Add a DIE to the delayed physname list. */
9030
9031 static void
9032 add_to_method_list (struct type *type, int fnfield_index, int index,
9033 const char *name, struct die_info *die,
9034 struct dwarf2_cu *cu)
9035 {
9036 struct delayed_method_info mi;
9037 mi.type = type;
9038 mi.fnfield_index = fnfield_index;
9039 mi.index = index;
9040 mi.name = name;
9041 mi.die = die;
9042 cu->method_list.push_back (mi);
9043 }
9044
9045 /* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9046 "const" / "volatile". If so, decrements LEN by the length of the
9047 modifier and return true. Otherwise return false. */
9048
9049 template<size_t N>
9050 static bool
9051 check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9052 {
9053 size_t mod_len = sizeof (mod) - 1;
9054 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9055 {
9056 len -= mod_len;
9057 return true;
9058 }
9059 return false;
9060 }
9061
9062 /* Compute the physnames of any methods on the CU's method list.
9063
9064 The computation of method physnames is delayed in order to avoid the
9065 (bad) condition that one of the method's formal parameters is of an as yet
9066 incomplete type. */
9067
9068 static void
9069 compute_delayed_physnames (struct dwarf2_cu *cu)
9070 {
9071 /* Only C++ delays computing physnames. */
9072 if (cu->method_list.empty ())
9073 return;
9074 gdb_assert (cu->language == language_cplus);
9075
9076 for (const delayed_method_info &mi : cu->method_list)
9077 {
9078 const char *physname;
9079 struct fn_fieldlist *fn_flp
9080 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9081 physname = dwarf2_physname (mi.name, mi.die, cu);
9082 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
9083 = physname ? physname : "";
9084
9085 /* Since there's no tag to indicate whether a method is a
9086 const/volatile overload, extract that information out of the
9087 demangled name. */
9088 if (physname != NULL)
9089 {
9090 size_t len = strlen (physname);
9091
9092 while (1)
9093 {
9094 if (physname[len] == ')') /* shortcut */
9095 break;
9096 else if (check_modifier (physname, len, " const"))
9097 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
9098 else if (check_modifier (physname, len, " volatile"))
9099 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
9100 else
9101 break;
9102 }
9103 }
9104 }
9105
9106 /* The list is no longer needed. */
9107 cu->method_list.clear ();
9108 }
9109
9110 /* Go objects should be embedded in a DW_TAG_module DIE,
9111 and it's not clear if/how imported objects will appear.
9112 To keep Go support simple until that's worked out,
9113 go back through what we've read and create something usable.
9114 We could do this while processing each DIE, and feels kinda cleaner,
9115 but that way is more invasive.
9116 This is to, for example, allow the user to type "p var" or "b main"
9117 without having to specify the package name, and allow lookups
9118 of module.object to work in contexts that use the expression
9119 parser. */
9120
9121 static void
9122 fixup_go_packaging (struct dwarf2_cu *cu)
9123 {
9124 gdb::unique_xmalloc_ptr<char> package_name;
9125 struct pending *list;
9126 int i;
9127
9128 for (list = *cu->get_builder ()->get_global_symbols ();
9129 list != NULL;
9130 list = list->next)
9131 {
9132 for (i = 0; i < list->nsyms; ++i)
9133 {
9134 struct symbol *sym = list->symbol[i];
9135
9136 if (sym->language () == language_go
9137 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9138 {
9139 gdb::unique_xmalloc_ptr<char> this_package_name
9140 (go_symbol_package_name (sym));
9141
9142 if (this_package_name == NULL)
9143 continue;
9144 if (package_name == NULL)
9145 package_name = std::move (this_package_name);
9146 else
9147 {
9148 struct objfile *objfile = cu->per_objfile->objfile;
9149 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
9150 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
9151 (symbol_symtab (sym) != NULL
9152 ? symtab_to_filename_for_display
9153 (symbol_symtab (sym))
9154 : objfile_name (objfile)),
9155 this_package_name.get (), package_name.get ());
9156 }
9157 }
9158 }
9159 }
9160
9161 if (package_name != NULL)
9162 {
9163 struct objfile *objfile = cu->per_objfile->objfile;
9164 const char *saved_package_name = objfile->intern (package_name.get ());
9165 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9166 saved_package_name);
9167 struct symbol *sym;
9168
9169 sym = new (&objfile->objfile_obstack) symbol;
9170 sym->set_language (language_go, &objfile->objfile_obstack);
9171 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
9172 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9173 e.g., "main" finds the "main" module and not C's main(). */
9174 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
9175 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
9176 SYMBOL_TYPE (sym) = type;
9177
9178 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
9179 }
9180 }
9181
9182 /* Allocate a fully-qualified name consisting of the two parts on the
9183 obstack. */
9184
9185 static const char *
9186 rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9187 {
9188 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9189 }
9190
9191 /* A helper that allocates a variant part to attach to a Rust enum
9192 type. OBSTACK is where the results should be allocated. TYPE is
9193 the type we're processing. DISCRIMINANT_INDEX is the index of the
9194 discriminant. It must be the index of one of the fields of TYPE,
9195 or -1 to mean there is no discriminant (univariant enum).
9196 DEFAULT_INDEX is the index of the default field; or -1 if there is
9197 no default. RANGES is indexed by "effective" field number (the
9198 field index, but omitting the discriminant and default fields) and
9199 must hold the discriminant values used by the variants. Note that
9200 RANGES must have a lifetime at least as long as OBSTACK -- either
9201 already allocated on it, or static. */
9202
9203 static void
9204 alloc_rust_variant (struct obstack *obstack, struct type *type,
9205 int discriminant_index, int default_index,
9206 gdb::array_view<discriminant_range> ranges)
9207 {
9208 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9209 gdb_assert (discriminant_index == -1
9210 || (discriminant_index >= 0
9211 && discriminant_index < type->num_fields ()));
9212 gdb_assert (default_index == -1
9213 || (default_index >= 0 && default_index < type->num_fields ()));
9214
9215 /* We have one variant for each non-discriminant field. */
9216 int n_variants = type->num_fields ();
9217 if (discriminant_index != -1)
9218 --n_variants;
9219
9220 variant *variants = new (obstack) variant[n_variants];
9221 int var_idx = 0;
9222 int range_idx = 0;
9223 for (int i = 0; i < type->num_fields (); ++i)
9224 {
9225 if (i == discriminant_index)
9226 continue;
9227
9228 variants[var_idx].first_field = i;
9229 variants[var_idx].last_field = i + 1;
9230
9231 /* The default field does not need a range, but other fields do.
9232 We skipped the discriminant above. */
9233 if (i != default_index)
9234 {
9235 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9236 ++range_idx;
9237 }
9238
9239 ++var_idx;
9240 }
9241
9242 gdb_assert (range_idx == ranges.size ());
9243 gdb_assert (var_idx == n_variants);
9244
9245 variant_part *part = new (obstack) variant_part;
9246 part->discriminant_index = discriminant_index;
9247 /* If there is no discriminant, then whether it is signed is of no
9248 consequence. */
9249 part->is_unsigned
9250 = (discriminant_index == -1
9251 ? false
9252 : type->field (discriminant_index).type ()->is_unsigned ());
9253 part->variants = gdb::array_view<variant> (variants, n_variants);
9254
9255 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9256 gdb::array_view<variant_part> *prop_value
9257 = new (storage) gdb::array_view<variant_part> (part, 1);
9258
9259 struct dynamic_prop prop;
9260 prop.set_variant_parts (prop_value);
9261
9262 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9263 }
9264
9265 /* Some versions of rustc emitted enums in an unusual way.
9266
9267 Ordinary enums were emitted as unions. The first element of each
9268 structure in the union was named "RUST$ENUM$DISR". This element
9269 held the discriminant.
9270
9271 These versions of Rust also implemented the "non-zero"
9272 optimization. When the enum had two values, and one is empty and
9273 the other holds a pointer that cannot be zero, the pointer is used
9274 as the discriminant, with a zero value meaning the empty variant.
9275 Here, the union's first member is of the form
9276 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9277 where the fieldnos are the indices of the fields that should be
9278 traversed in order to find the field (which may be several fields deep)
9279 and the variantname is the name of the variant of the case when the
9280 field is zero.
9281
9282 This function recognizes whether TYPE is of one of these forms,
9283 and, if so, smashes it to be a variant type. */
9284
9285 static void
9286 quirk_rust_enum (struct type *type, struct objfile *objfile)
9287 {
9288 gdb_assert (type->code () == TYPE_CODE_UNION);
9289
9290 /* We don't need to deal with empty enums. */
9291 if (type->num_fields () == 0)
9292 return;
9293
9294 #define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9295 if (type->num_fields () == 1
9296 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9297 {
9298 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9299
9300 /* Decode the field name to find the offset of the
9301 discriminant. */
9302 ULONGEST bit_offset = 0;
9303 struct type *field_type = type->field (0).type ();
9304 while (name[0] >= '0' && name[0] <= '9')
9305 {
9306 char *tail;
9307 unsigned long index = strtoul (name, &tail, 10);
9308 name = tail;
9309 if (*name != '$'
9310 || index >= field_type->num_fields ()
9311 || (TYPE_FIELD_LOC_KIND (field_type, index)
9312 != FIELD_LOC_KIND_BITPOS))
9313 {
9314 complaint (_("Could not parse Rust enum encoding string \"%s\""
9315 "[in module %s]"),
9316 TYPE_FIELD_NAME (type, 0),
9317 objfile_name (objfile));
9318 return;
9319 }
9320 ++name;
9321
9322 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9323 field_type = field_type->field (index).type ();
9324 }
9325
9326 /* Smash this type to be a structure type. We have to do this
9327 because the type has already been recorded. */
9328 type->set_code (TYPE_CODE_STRUCT);
9329 type->set_num_fields (3);
9330 /* Save the field we care about. */
9331 struct field saved_field = type->field (0);
9332 type->set_fields
9333 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
9334
9335 /* Put the discriminant at index 0. */
9336 type->field (0).set_type (field_type);
9337 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9338 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9339 SET_FIELD_BITPOS (type->field (0), bit_offset);
9340
9341 /* The order of fields doesn't really matter, so put the real
9342 field at index 1 and the data-less field at index 2. */
9343 type->field (1) = saved_field;
9344 TYPE_FIELD_NAME (type, 1)
9345 = rust_last_path_segment (type->field (1).type ()->name ());
9346 type->field (1).type ()->set_name
9347 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9348 TYPE_FIELD_NAME (type, 1)));
9349
9350 const char *dataless_name
9351 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
9352 name);
9353 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9354 dataless_name);
9355 type->field (2).set_type (dataless_type);
9356 /* NAME points into the original discriminant name, which
9357 already has the correct lifetime. */
9358 TYPE_FIELD_NAME (type, 2) = name;
9359 SET_FIELD_BITPOS (type->field (2), 0);
9360
9361 /* Indicate that this is a variant type. */
9362 static discriminant_range ranges[1] = { { 0, 0 } };
9363 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
9364 }
9365 /* A union with a single anonymous field is probably an old-style
9366 univariant enum. */
9367 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
9368 {
9369 /* Smash this type to be a structure type. We have to do this
9370 because the type has already been recorded. */
9371 type->set_code (TYPE_CODE_STRUCT);
9372
9373 struct type *field_type = type->field (0).type ();
9374 const char *variant_name
9375 = rust_last_path_segment (field_type->name ());
9376 TYPE_FIELD_NAME (type, 0) = variant_name;
9377 field_type->set_name
9378 (rust_fully_qualify (&objfile->objfile_obstack,
9379 type->name (), variant_name));
9380
9381 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
9382 }
9383 else
9384 {
9385 struct type *disr_type = nullptr;
9386 for (int i = 0; i < type->num_fields (); ++i)
9387 {
9388 disr_type = type->field (i).type ();
9389
9390 if (disr_type->code () != TYPE_CODE_STRUCT)
9391 {
9392 /* All fields of a true enum will be structs. */
9393 return;
9394 }
9395 else if (disr_type->num_fields () == 0)
9396 {
9397 /* Could be data-less variant, so keep going. */
9398 disr_type = nullptr;
9399 }
9400 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9401 "RUST$ENUM$DISR") != 0)
9402 {
9403 /* Not a Rust enum. */
9404 return;
9405 }
9406 else
9407 {
9408 /* Found one. */
9409 break;
9410 }
9411 }
9412
9413 /* If we got here without a discriminant, then it's probably
9414 just a union. */
9415 if (disr_type == nullptr)
9416 return;
9417
9418 /* Smash this type to be a structure type. We have to do this
9419 because the type has already been recorded. */
9420 type->set_code (TYPE_CODE_STRUCT);
9421
9422 /* Make space for the discriminant field. */
9423 struct field *disr_field = &disr_type->field (0);
9424 field *new_fields
9425 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9426 * sizeof (struct field)));
9427 memcpy (new_fields + 1, type->fields (),
9428 type->num_fields () * sizeof (struct field));
9429 type->set_fields (new_fields);
9430 type->set_num_fields (type->num_fields () + 1);
9431
9432 /* Install the discriminant at index 0 in the union. */
9433 type->field (0) = *disr_field;
9434 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9435 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9436
9437 /* We need a way to find the correct discriminant given a
9438 variant name. For convenience we build a map here. */
9439 struct type *enum_type = disr_field->type ();
9440 std::unordered_map<std::string, ULONGEST> discriminant_map;
9441 for (int i = 0; i < enum_type->num_fields (); ++i)
9442 {
9443 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9444 {
9445 const char *name
9446 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9447 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9448 }
9449 }
9450
9451 int n_fields = type->num_fields ();
9452 /* We don't need a range entry for the discriminant, but we do
9453 need one for every other field, as there is no default
9454 variant. */
9455 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9456 discriminant_range,
9457 n_fields - 1);
9458 /* Skip the discriminant here. */
9459 for (int i = 1; i < n_fields; ++i)
9460 {
9461 /* Find the final word in the name of this variant's type.
9462 That name can be used to look up the correct
9463 discriminant. */
9464 const char *variant_name
9465 = rust_last_path_segment (type->field (i).type ()->name ());
9466
9467 auto iter = discriminant_map.find (variant_name);
9468 if (iter != discriminant_map.end ())
9469 {
9470 ranges[i - 1].low = iter->second;
9471 ranges[i - 1].high = iter->second;
9472 }
9473
9474 /* In Rust, each element should have the size of the
9475 enclosing enum. */
9476 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9477
9478 /* Remove the discriminant field, if it exists. */
9479 struct type *sub_type = type->field (i).type ();
9480 if (sub_type->num_fields () > 0)
9481 {
9482 sub_type->set_num_fields (sub_type->num_fields () - 1);
9483 sub_type->set_fields (sub_type->fields () + 1);
9484 }
9485 TYPE_FIELD_NAME (type, i) = variant_name;
9486 sub_type->set_name
9487 (rust_fully_qualify (&objfile->objfile_obstack,
9488 type->name (), variant_name));
9489 }
9490
9491 /* Indicate that this is a variant type. */
9492 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9493 gdb::array_view<discriminant_range> (ranges,
9494 n_fields - 1));
9495 }
9496 }
9497
9498 /* Rewrite some Rust unions to be structures with variants parts. */
9499
9500 static void
9501 rust_union_quirks (struct dwarf2_cu *cu)
9502 {
9503 gdb_assert (cu->language == language_rust);
9504 for (type *type_ : cu->rust_unions)
9505 quirk_rust_enum (type_, cu->per_objfile->objfile);
9506 /* We don't need this any more. */
9507 cu->rust_unions.clear ();
9508 }
9509
9510 /* See read.h. */
9511
9512 type_unit_group_unshareable *
9513 dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9514 {
9515 auto iter = this->m_type_units.find (tu_group);
9516 if (iter != this->m_type_units.end ())
9517 return iter->second.get ();
9518
9519 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9520 type_unit_group_unshareable *result = uniq.get ();
9521 this->m_type_units[tu_group] = std::move (uniq);
9522 return result;
9523 }
9524
9525 struct type *
9526 dwarf2_per_objfile::get_type_for_signatured_type
9527 (signatured_type *sig_type) const
9528 {
9529 auto iter = this->m_type_map.find (sig_type);
9530 if (iter == this->m_type_map.end ())
9531 return nullptr;
9532
9533 return iter->second;
9534 }
9535
9536 void dwarf2_per_objfile::set_type_for_signatured_type
9537 (signatured_type *sig_type, struct type *type)
9538 {
9539 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9540
9541 this->m_type_map[sig_type] = type;
9542 }
9543
9544 /* A helper function for computing the list of all symbol tables
9545 included by PER_CU. */
9546
9547 static void
9548 recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
9549 htab_t all_children, htab_t all_type_symtabs,
9550 dwarf2_per_cu_data *per_cu,
9551 dwarf2_per_objfile *per_objfile,
9552 struct compunit_symtab *immediate_parent)
9553 {
9554 void **slot = htab_find_slot (all_children, per_cu, INSERT);
9555 if (*slot != NULL)
9556 {
9557 /* This inclusion and its children have been processed. */
9558 return;
9559 }
9560
9561 *slot = per_cu;
9562
9563 /* Only add a CU if it has a symbol table. */
9564 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9565 if (cust != NULL)
9566 {
9567 /* If this is a type unit only add its symbol table if we haven't
9568 seen it yet (type unit per_cu's can share symtabs). */
9569 if (per_cu->is_debug_types)
9570 {
9571 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
9572 if (*slot == NULL)
9573 {
9574 *slot = cust;
9575 result->push_back (cust);
9576 if (cust->user == NULL)
9577 cust->user = immediate_parent;
9578 }
9579 }
9580 else
9581 {
9582 result->push_back (cust);
9583 if (cust->user == NULL)
9584 cust->user = immediate_parent;
9585 }
9586 }
9587
9588 if (!per_cu->imported_symtabs_empty ())
9589 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9590 {
9591 recursively_compute_inclusions (result, all_children,
9592 all_type_symtabs, ptr, per_objfile,
9593 cust);
9594 }
9595 }
9596
9597 /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
9598 PER_CU. */
9599
9600 static void
9601 compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9602 dwarf2_per_objfile *per_objfile)
9603 {
9604 gdb_assert (! per_cu->is_debug_types);
9605
9606 if (!per_cu->imported_symtabs_empty ())
9607 {
9608 int len;
9609 std::vector<compunit_symtab *> result_symtabs;
9610 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
9611
9612 /* If we don't have a symtab, we can just skip this case. */
9613 if (cust == NULL)
9614 return;
9615
9616 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9617 htab_eq_pointer,
9618 NULL, xcalloc, xfree));
9619 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9620 htab_eq_pointer,
9621 NULL, xcalloc, xfree));
9622
9623 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9624 {
9625 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9626 all_type_symtabs.get (), ptr,
9627 per_objfile, cust);
9628 }
9629
9630 /* Now we have a transitive closure of all the included symtabs. */
9631 len = result_symtabs.size ();
9632 cust->includes
9633 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
9634 struct compunit_symtab *, len + 1);
9635 memcpy (cust->includes, result_symtabs.data (),
9636 len * sizeof (compunit_symtab *));
9637 cust->includes[len] = NULL;
9638 }
9639 }
9640
9641 /* Compute the 'includes' field for the symtabs of all the CUs we just
9642 read. */
9643
9644 static void
9645 process_cu_includes (dwarf2_per_objfile *per_objfile)
9646 {
9647 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
9648 {
9649 if (! iter->is_debug_types)
9650 compute_compunit_symtab_includes (iter, per_objfile);
9651 }
9652
9653 per_objfile->per_bfd->just_read_cus.clear ();
9654 }
9655
9656 /* Generate full symbol information for CU, whose DIEs have
9657 already been loaded into memory. */
9658
9659 static void
9660 process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
9661 {
9662 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9663 struct objfile *objfile = per_objfile->objfile;
9664 struct gdbarch *gdbarch = objfile->arch ();
9665 CORE_ADDR lowpc, highpc;
9666 struct compunit_symtab *cust;
9667 CORE_ADDR baseaddr;
9668 struct block *static_block;
9669 CORE_ADDR addr;
9670
9671 baseaddr = objfile->text_section_offset ();
9672
9673 /* Clear the list here in case something was left over. */
9674 cu->method_list.clear ();
9675
9676 cu->language = pretend_language;
9677 cu->language_defn = language_def (cu->language);
9678
9679 dwarf2_find_base_address (cu->dies, cu);
9680
9681 /* Before we start reading the top-level DIE, ensure it has a valid tag
9682 type. */
9683 switch (cu->dies->tag)
9684 {
9685 case DW_TAG_compile_unit:
9686 case DW_TAG_partial_unit:
9687 case DW_TAG_type_unit:
9688 break;
9689 default:
9690 error (_("Dwarf Error: unexpected tag '%s' at offset %s [in module %s]"),
9691 dwarf_tag_name (cu->dies->tag),
9692 sect_offset_str (cu->per_cu->sect_off),
9693 objfile_name (per_objfile->objfile));
9694 }
9695
9696 /* Do line number decoding in read_file_scope () */
9697 process_die (cu->dies, cu);
9698
9699 /* For now fudge the Go package. */
9700 if (cu->language == language_go)
9701 fixup_go_packaging (cu);
9702
9703 /* Now that we have processed all the DIEs in the CU, all the types
9704 should be complete, and it should now be safe to compute all of the
9705 physnames. */
9706 compute_delayed_physnames (cu);
9707
9708 if (cu->language == language_rust)
9709 rust_union_quirks (cu);
9710
9711 /* Some compilers don't define a DW_AT_high_pc attribute for the
9712 compilation unit. If the DW_AT_high_pc is missing, synthesize
9713 it, by scanning the DIE's below the compilation unit. */
9714 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
9715
9716 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
9717 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
9718
9719 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9720 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9721 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9722 addrmap to help ensure it has an accurate map of pc values belonging to
9723 this comp unit. */
9724 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9725
9726 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
9727 SECT_OFF_TEXT (objfile),
9728 0);
9729
9730 if (cust != NULL)
9731 {
9732 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
9733
9734 /* Set symtab language to language from DW_AT_language. If the
9735 compilation is from a C file generated by language preprocessors, do
9736 not set the language if it was already deduced by start_subfile. */
9737 if (!(cu->language == language_c
9738 && COMPUNIT_FILETABS (cust)->language != language_unknown))
9739 COMPUNIT_FILETABS (cust)->language = cu->language;
9740
9741 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9742 produce DW_AT_location with location lists but it can be possibly
9743 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9744 there were bugs in prologue debug info, fixed later in GCC-4.5
9745 by "unwind info for epilogues" patch (which is not directly related).
9746
9747 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9748 needed, it would be wrong due to missing DW_AT_producer there.
9749
9750 Still one can confuse GDB by using non-standard GCC compilation
9751 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
9752 */
9753 if (cu->has_loclist && gcc_4_minor >= 5)
9754 cust->locations_valid = 1;
9755
9756 if (gcc_4_minor >= 5)
9757 cust->epilogue_unwind_valid = 1;
9758
9759 cust->call_site_htab = cu->call_site_htab;
9760 }
9761
9762 per_objfile->set_symtab (cu->per_cu, cust);
9763
9764 /* Push it for inclusion processing later. */
9765 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
9766
9767 /* Not needed any more. */
9768 cu->reset_builder ();
9769 }
9770
9771 /* Generate full symbol information for type unit CU, whose DIEs have
9772 already been loaded into memory. */
9773
9774 static void
9775 process_full_type_unit (dwarf2_cu *cu,
9776 enum language pretend_language)
9777 {
9778 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9779 struct objfile *objfile = per_objfile->objfile;
9780 struct compunit_symtab *cust;
9781 struct signatured_type *sig_type;
9782
9783 gdb_assert (cu->per_cu->is_debug_types);
9784 sig_type = (struct signatured_type *) cu->per_cu;
9785
9786 /* Clear the list here in case something was left over. */
9787 cu->method_list.clear ();
9788
9789 cu->language = pretend_language;
9790 cu->language_defn = language_def (cu->language);
9791
9792 /* The symbol tables are set up in read_type_unit_scope. */
9793 process_die (cu->dies, cu);
9794
9795 /* For now fudge the Go package. */
9796 if (cu->language == language_go)
9797 fixup_go_packaging (cu);
9798
9799 /* Now that we have processed all the DIEs in the CU, all the types
9800 should be complete, and it should now be safe to compute all of the
9801 physnames. */
9802 compute_delayed_physnames (cu);
9803
9804 if (cu->language == language_rust)
9805 rust_union_quirks (cu);
9806
9807 /* TUs share symbol tables.
9808 If this is the first TU to use this symtab, complete the construction
9809 of it with end_expandable_symtab. Otherwise, complete the addition of
9810 this TU's symbols to the existing symtab. */
9811 type_unit_group_unshareable *tug_unshare =
9812 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9813 if (tug_unshare->compunit_symtab == NULL)
9814 {
9815 buildsym_compunit *builder = cu->get_builder ();
9816 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
9817 tug_unshare->compunit_symtab = cust;
9818
9819 if (cust != NULL)
9820 {
9821 /* Set symtab language to language from DW_AT_language. If the
9822 compilation is from a C file generated by language preprocessors,
9823 do not set the language if it was already deduced by
9824 start_subfile. */
9825 if (!(cu->language == language_c
9826 && COMPUNIT_FILETABS (cust)->language != language_c))
9827 COMPUNIT_FILETABS (cust)->language = cu->language;
9828 }
9829 }
9830 else
9831 {
9832 cu->get_builder ()->augment_type_symtab ();
9833 cust = tug_unshare->compunit_symtab;
9834 }
9835
9836 per_objfile->set_symtab (cu->per_cu, cust);
9837
9838 /* Not needed any more. */
9839 cu->reset_builder ();
9840 }
9841
9842 /* Process an imported unit DIE. */
9843
9844 static void
9845 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9846 {
9847 struct attribute *attr;
9848
9849 /* For now we don't handle imported units in type units. */
9850 if (cu->per_cu->is_debug_types)
9851 {
9852 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9853 " supported in type units [in module %s]"),
9854 objfile_name (cu->per_objfile->objfile));
9855 }
9856
9857 attr = dwarf2_attr (die, DW_AT_import, cu);
9858 if (attr != NULL)
9859 {
9860 sect_offset sect_off = attr->get_ref_die_offset ();
9861 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9862 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9863 dwarf2_per_cu_data *per_cu
9864 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
9865
9866 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9867 into another compilation unit, at root level. Regard this as a hint,
9868 and ignore it. */
9869 if (die->parent && die->parent->parent == NULL
9870 && per_cu->unit_type == DW_UT_compile
9871 && per_cu->lang == language_cplus)
9872 return;
9873
9874 /* If necessary, add it to the queue and load its DIEs. */
9875 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
9876 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
9877 false, cu->language);
9878
9879 cu->per_cu->imported_symtabs_push (per_cu);
9880 }
9881 }
9882
9883 /* RAII object that represents a process_die scope: i.e.,
9884 starts/finishes processing a DIE. */
9885 class process_die_scope
9886 {
9887 public:
9888 process_die_scope (die_info *die, dwarf2_cu *cu)
9889 : m_die (die), m_cu (cu)
9890 {
9891 /* We should only be processing DIEs not already in process. */
9892 gdb_assert (!m_die->in_process);
9893 m_die->in_process = true;
9894 }
9895
9896 ~process_die_scope ()
9897 {
9898 m_die->in_process = false;
9899
9900 /* If we're done processing the DIE for the CU that owns the line
9901 header, we don't need the line header anymore. */
9902 if (m_cu->line_header_die_owner == m_die)
9903 {
9904 delete m_cu->line_header;
9905 m_cu->line_header = NULL;
9906 m_cu->line_header_die_owner = NULL;
9907 }
9908 }
9909
9910 private:
9911 die_info *m_die;
9912 dwarf2_cu *m_cu;
9913 };
9914
9915 /* Process a die and its children. */
9916
9917 static void
9918 process_die (struct die_info *die, struct dwarf2_cu *cu)
9919 {
9920 process_die_scope scope (die, cu);
9921
9922 switch (die->tag)
9923 {
9924 case DW_TAG_padding:
9925 break;
9926 case DW_TAG_compile_unit:
9927 case DW_TAG_partial_unit:
9928 read_file_scope (die, cu);
9929 break;
9930 case DW_TAG_type_unit:
9931 read_type_unit_scope (die, cu);
9932 break;
9933 case DW_TAG_subprogram:
9934 /* Nested subprograms in Fortran get a prefix. */
9935 if (cu->language == language_fortran
9936 && die->parent != NULL
9937 && die->parent->tag == DW_TAG_subprogram)
9938 cu->processing_has_namespace_info = true;
9939 /* Fall through. */
9940 case DW_TAG_inlined_subroutine:
9941 read_func_scope (die, cu);
9942 break;
9943 case DW_TAG_lexical_block:
9944 case DW_TAG_try_block:
9945 case DW_TAG_catch_block:
9946 read_lexical_block_scope (die, cu);
9947 break;
9948 case DW_TAG_call_site:
9949 case DW_TAG_GNU_call_site:
9950 read_call_site_scope (die, cu);
9951 break;
9952 case DW_TAG_class_type:
9953 case DW_TAG_interface_type:
9954 case DW_TAG_structure_type:
9955 case DW_TAG_union_type:
9956 process_structure_scope (die, cu);
9957 break;
9958 case DW_TAG_enumeration_type:
9959 process_enumeration_scope (die, cu);
9960 break;
9961
9962 /* These dies have a type, but processing them does not create
9963 a symbol or recurse to process the children. Therefore we can
9964 read them on-demand through read_type_die. */
9965 case DW_TAG_subroutine_type:
9966 case DW_TAG_set_type:
9967 case DW_TAG_pointer_type:
9968 case DW_TAG_ptr_to_member_type:
9969 case DW_TAG_reference_type:
9970 case DW_TAG_rvalue_reference_type:
9971 case DW_TAG_string_type:
9972 break;
9973
9974 case DW_TAG_array_type:
9975 /* We only need to handle this case for Ada -- in other
9976 languages, it's normal for the compiler to emit a typedef
9977 instead. */
9978 if (cu->language != language_ada)
9979 break;
9980 /* FALLTHROUGH */
9981 case DW_TAG_base_type:
9982 case DW_TAG_subrange_type:
9983 case DW_TAG_typedef:
9984 /* Add a typedef symbol for the type definition, if it has a
9985 DW_AT_name. */
9986 new_symbol (die, read_type_die (die, cu), cu);
9987 break;
9988 case DW_TAG_common_block:
9989 read_common_block (die, cu);
9990 break;
9991 case DW_TAG_common_inclusion:
9992 break;
9993 case DW_TAG_namespace:
9994 cu->processing_has_namespace_info = true;
9995 read_namespace (die, cu);
9996 break;
9997 case DW_TAG_module:
9998 cu->processing_has_namespace_info = true;
9999 read_module (die, cu);
10000 break;
10001 case DW_TAG_imported_declaration:
10002 cu->processing_has_namespace_info = true;
10003 if (read_namespace_alias (die, cu))
10004 break;
10005 /* The declaration is not a global namespace alias. */
10006 /* Fall through. */
10007 case DW_TAG_imported_module:
10008 cu->processing_has_namespace_info = true;
10009 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10010 || cu->language != language_fortran))
10011 complaint (_("Tag '%s' has unexpected children"),
10012 dwarf_tag_name (die->tag));
10013 read_import_statement (die, cu);
10014 break;
10015
10016 case DW_TAG_imported_unit:
10017 process_imported_unit_die (die, cu);
10018 break;
10019
10020 case DW_TAG_variable:
10021 read_variable (die, cu);
10022 break;
10023
10024 default:
10025 new_symbol (die, NULL, cu);
10026 break;
10027 }
10028 }
10029 \f
10030 /* DWARF name computation. */
10031
10032 /* A helper function for dwarf2_compute_name which determines whether DIE
10033 needs to have the name of the scope prepended to the name listed in the
10034 die. */
10035
10036 static int
10037 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10038 {
10039 struct attribute *attr;
10040
10041 switch (die->tag)
10042 {
10043 case DW_TAG_namespace:
10044 case DW_TAG_typedef:
10045 case DW_TAG_class_type:
10046 case DW_TAG_interface_type:
10047 case DW_TAG_structure_type:
10048 case DW_TAG_union_type:
10049 case DW_TAG_enumeration_type:
10050 case DW_TAG_enumerator:
10051 case DW_TAG_subprogram:
10052 case DW_TAG_inlined_subroutine:
10053 case DW_TAG_member:
10054 case DW_TAG_imported_declaration:
10055 return 1;
10056
10057 case DW_TAG_variable:
10058 case DW_TAG_constant:
10059 /* We only need to prefix "globally" visible variables. These include
10060 any variable marked with DW_AT_external or any variable that
10061 lives in a namespace. [Variables in anonymous namespaces
10062 require prefixing, but they are not DW_AT_external.] */
10063
10064 if (dwarf2_attr (die, DW_AT_specification, cu))
10065 {
10066 struct dwarf2_cu *spec_cu = cu;
10067
10068 return die_needs_namespace (die_specification (die, &spec_cu),
10069 spec_cu);
10070 }
10071
10072 attr = dwarf2_attr (die, DW_AT_external, cu);
10073 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10074 && die->parent->tag != DW_TAG_module)
10075 return 0;
10076 /* A variable in a lexical block of some kind does not need a
10077 namespace, even though in C++ such variables may be external
10078 and have a mangled name. */
10079 if (die->parent->tag == DW_TAG_lexical_block
10080 || die->parent->tag == DW_TAG_try_block
10081 || die->parent->tag == DW_TAG_catch_block
10082 || die->parent->tag == DW_TAG_subprogram)
10083 return 0;
10084 return 1;
10085
10086 default:
10087 return 0;
10088 }
10089 }
10090
10091 /* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10092 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10093 defined for the given DIE. */
10094
10095 static struct attribute *
10096 dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10097 {
10098 struct attribute *attr;
10099
10100 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10101 if (attr == NULL)
10102 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10103
10104 return attr;
10105 }
10106
10107 /* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10108 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10109 defined for the given DIE. */
10110
10111 static const char *
10112 dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10113 {
10114 const char *linkage_name;
10115
10116 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10117 if (linkage_name == NULL)
10118 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10119
10120 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10121 See https://github.com/rust-lang/rust/issues/32925. */
10122 if (cu->language == language_rust && linkage_name != NULL
10123 && strchr (linkage_name, '{') != NULL)
10124 linkage_name = NULL;
10125
10126 return linkage_name;
10127 }
10128
10129 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
10130 compute the physname for the object, which include a method's:
10131 - formal parameters (C++),
10132 - receiver type (Go),
10133
10134 The term "physname" is a bit confusing.
10135 For C++, for example, it is the demangled name.
10136 For Go, for example, it's the mangled name.
10137
10138 For Ada, return the DIE's linkage name rather than the fully qualified
10139 name. PHYSNAME is ignored..
10140
10141 The result is allocated on the objfile->per_bfd's obstack and
10142 canonicalized. */
10143
10144 static const char *
10145 dwarf2_compute_name (const char *name,
10146 struct die_info *die, struct dwarf2_cu *cu,
10147 int physname)
10148 {
10149 struct objfile *objfile = cu->per_objfile->objfile;
10150
10151 if (name == NULL)
10152 name = dwarf2_name (die, cu);
10153
10154 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10155 but otherwise compute it by typename_concat inside GDB.
10156 FIXME: Actually this is not really true, or at least not always true.
10157 It's all very confusing. compute_and_set_names doesn't try to demangle
10158 Fortran names because there is no mangling standard. So new_symbol
10159 will set the demangled name to the result of dwarf2_full_name, and it is
10160 the demangled name that GDB uses if it exists. */
10161 if (cu->language == language_ada
10162 || (cu->language == language_fortran && physname))
10163 {
10164 /* For Ada unit, we prefer the linkage name over the name, as
10165 the former contains the exported name, which the user expects
10166 to be able to reference. Ideally, we want the user to be able
10167 to reference this entity using either natural or linkage name,
10168 but we haven't started looking at this enhancement yet. */
10169 const char *linkage_name = dw2_linkage_name (die, cu);
10170
10171 if (linkage_name != NULL)
10172 return linkage_name;
10173 }
10174
10175 /* These are the only languages we know how to qualify names in. */
10176 if (name != NULL
10177 && (cu->language == language_cplus
10178 || cu->language == language_fortran || cu->language == language_d
10179 || cu->language == language_rust))
10180 {
10181 if (die_needs_namespace (die, cu))
10182 {
10183 const char *prefix;
10184 const char *canonical_name = NULL;
10185
10186 string_file buf;
10187
10188 prefix = determine_prefix (die, cu);
10189 if (*prefix != '\0')
10190 {
10191 gdb::unique_xmalloc_ptr<char> prefixed_name
10192 (typename_concat (NULL, prefix, name, physname, cu));
10193
10194 buf.puts (prefixed_name.get ());
10195 }
10196 else
10197 buf.puts (name);
10198
10199 /* Template parameters may be specified in the DIE's DW_AT_name, or
10200 as children with DW_TAG_template_type_param or
10201 DW_TAG_value_type_param. If the latter, add them to the name
10202 here. If the name already has template parameters, then
10203 skip this step; some versions of GCC emit both, and
10204 it is more efficient to use the pre-computed name.
10205
10206 Something to keep in mind about this process: it is very
10207 unlikely, or in some cases downright impossible, to produce
10208 something that will match the mangled name of a function.
10209 If the definition of the function has the same debug info,
10210 we should be able to match up with it anyway. But fallbacks
10211 using the minimal symbol, for instance to find a method
10212 implemented in a stripped copy of libstdc++, will not work.
10213 If we do not have debug info for the definition, we will have to
10214 match them up some other way.
10215
10216 When we do name matching there is a related problem with function
10217 templates; two instantiated function templates are allowed to
10218 differ only by their return types, which we do not add here. */
10219
10220 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10221 {
10222 struct attribute *attr;
10223 struct die_info *child;
10224 int first = 1;
10225 const language_defn *cplus_lang = language_def (cu->language);
10226
10227 die->building_fullname = 1;
10228
10229 for (child = die->child; child != NULL; child = child->sibling)
10230 {
10231 struct type *type;
10232 LONGEST value;
10233 const gdb_byte *bytes;
10234 struct dwarf2_locexpr_baton *baton;
10235 struct value *v;
10236
10237 if (child->tag != DW_TAG_template_type_param
10238 && child->tag != DW_TAG_template_value_param)
10239 continue;
10240
10241 if (first)
10242 {
10243 buf.puts ("<");
10244 first = 0;
10245 }
10246 else
10247 buf.puts (", ");
10248
10249 attr = dwarf2_attr (child, DW_AT_type, cu);
10250 if (attr == NULL)
10251 {
10252 complaint (_("template parameter missing DW_AT_type"));
10253 buf.puts ("UNKNOWN_TYPE");
10254 continue;
10255 }
10256 type = die_type (child, cu);
10257
10258 if (child->tag == DW_TAG_template_type_param)
10259 {
10260 cplus_lang->print_type (type, "", &buf, -1, 0,
10261 &type_print_raw_options);
10262 continue;
10263 }
10264
10265 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10266 if (attr == NULL)
10267 {
10268 complaint (_("template parameter missing "
10269 "DW_AT_const_value"));
10270 buf.puts ("UNKNOWN_VALUE");
10271 continue;
10272 }
10273
10274 dwarf2_const_value_attr (attr, type, name,
10275 &cu->comp_unit_obstack, cu,
10276 &value, &bytes, &baton);
10277
10278 if (type->has_no_signedness ())
10279 /* GDB prints characters as NUMBER 'CHAR'. If that's
10280 changed, this can use value_print instead. */
10281 cplus_lang->printchar (value, type, &buf);
10282 else
10283 {
10284 struct value_print_options opts;
10285
10286 if (baton != NULL)
10287 v = dwarf2_evaluate_loc_desc (type, NULL,
10288 baton->data,
10289 baton->size,
10290 baton->per_cu,
10291 baton->per_objfile);
10292 else if (bytes != NULL)
10293 {
10294 v = allocate_value (type);
10295 memcpy (value_contents_writeable (v), bytes,
10296 TYPE_LENGTH (type));
10297 }
10298 else
10299 v = value_from_longest (type, value);
10300
10301 /* Specify decimal so that we do not depend on
10302 the radix. */
10303 get_formatted_print_options (&opts, 'd');
10304 opts.raw = 1;
10305 value_print (v, &buf, &opts);
10306 release_value (v);
10307 }
10308 }
10309
10310 die->building_fullname = 0;
10311
10312 if (!first)
10313 {
10314 /* Close the argument list, with a space if necessary
10315 (nested templates). */
10316 if (!buf.empty () && buf.string ().back () == '>')
10317 buf.puts (" >");
10318 else
10319 buf.puts (">");
10320 }
10321 }
10322
10323 /* For C++ methods, append formal parameter type
10324 information, if PHYSNAME. */
10325
10326 if (physname && die->tag == DW_TAG_subprogram
10327 && cu->language == language_cplus)
10328 {
10329 struct type *type = read_type_die (die, cu);
10330
10331 c_type_print_args (type, &buf, 1, cu->language,
10332 &type_print_raw_options);
10333
10334 if (cu->language == language_cplus)
10335 {
10336 /* Assume that an artificial first parameter is
10337 "this", but do not crash if it is not. RealView
10338 marks unnamed (and thus unused) parameters as
10339 artificial; there is no way to differentiate
10340 the two cases. */
10341 if (type->num_fields () > 0
10342 && TYPE_FIELD_ARTIFICIAL (type, 0)
10343 && type->field (0).type ()->code () == TYPE_CODE_PTR
10344 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
10345 buf.puts (" const");
10346 }
10347 }
10348
10349 const std::string &intermediate_name = buf.string ();
10350
10351 if (cu->language == language_cplus)
10352 canonical_name
10353 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
10354 objfile);
10355
10356 /* If we only computed INTERMEDIATE_NAME, or if
10357 INTERMEDIATE_NAME is already canonical, then we need to
10358 intern it. */
10359 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
10360 name = objfile->intern (intermediate_name);
10361 else
10362 name = canonical_name;
10363 }
10364 }
10365
10366 return name;
10367 }
10368
10369 /* Return the fully qualified name of DIE, based on its DW_AT_name.
10370 If scope qualifiers are appropriate they will be added. The result
10371 will be allocated on the storage_obstack, or NULL if the DIE does
10372 not have a name. NAME may either be from a previous call to
10373 dwarf2_name or NULL.
10374
10375 The output string will be canonicalized (if C++). */
10376
10377 static const char *
10378 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10379 {
10380 return dwarf2_compute_name (name, die, cu, 0);
10381 }
10382
10383 /* Construct a physname for the given DIE in CU. NAME may either be
10384 from a previous call to dwarf2_name or NULL. The result will be
10385 allocated on the objfile_objstack or NULL if the DIE does not have a
10386 name.
10387
10388 The output string will be canonicalized (if C++). */
10389
10390 static const char *
10391 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
10392 {
10393 struct objfile *objfile = cu->per_objfile->objfile;
10394 const char *retval, *mangled = NULL, *canon = NULL;
10395 int need_copy = 1;
10396
10397 /* In this case dwarf2_compute_name is just a shortcut not building anything
10398 on its own. */
10399 if (!die_needs_namespace (die, cu))
10400 return dwarf2_compute_name (name, die, cu, 1);
10401
10402 if (cu->language != language_rust)
10403 mangled = dw2_linkage_name (die, cu);
10404
10405 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10406 has computed. */
10407 gdb::unique_xmalloc_ptr<char> demangled;
10408 if (mangled != NULL)
10409 {
10410
10411 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
10412 {
10413 /* Do nothing (do not demangle the symbol name). */
10414 }
10415 else
10416 {
10417 /* Use DMGL_RET_DROP for C++ template functions to suppress
10418 their return type. It is easier for GDB users to search
10419 for such functions as `name(params)' than `long name(params)'.
10420 In such case the minimal symbol names do not match the full
10421 symbol names but for template functions there is never a need
10422 to look up their definition from their declaration so
10423 the only disadvantage remains the minimal symbol variant
10424 `long name(params)' does not have the proper inferior type. */
10425 demangled.reset (gdb_demangle (mangled,
10426 (DMGL_PARAMS | DMGL_ANSI
10427 | DMGL_RET_DROP)));
10428 }
10429 if (demangled)
10430 canon = demangled.get ();
10431 else
10432 {
10433 canon = mangled;
10434 need_copy = 0;
10435 }
10436 }
10437
10438 if (canon == NULL || check_physname)
10439 {
10440 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10441
10442 if (canon != NULL && strcmp (physname, canon) != 0)
10443 {
10444 /* It may not mean a bug in GDB. The compiler could also
10445 compute DW_AT_linkage_name incorrectly. But in such case
10446 GDB would need to be bug-to-bug compatible. */
10447
10448 complaint (_("Computed physname <%s> does not match demangled <%s> "
10449 "(from linkage <%s>) - DIE at %s [in module %s]"),
10450 physname, canon, mangled, sect_offset_str (die->sect_off),
10451 objfile_name (objfile));
10452
10453 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10454 is available here - over computed PHYSNAME. It is safer
10455 against both buggy GDB and buggy compilers. */
10456
10457 retval = canon;
10458 }
10459 else
10460 {
10461 retval = physname;
10462 need_copy = 0;
10463 }
10464 }
10465 else
10466 retval = canon;
10467
10468 if (need_copy)
10469 retval = objfile->intern (retval);
10470
10471 return retval;
10472 }
10473
10474 /* Inspect DIE in CU for a namespace alias. If one exists, record
10475 a new symbol for it.
10476
10477 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10478
10479 static int
10480 read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10481 {
10482 struct attribute *attr;
10483
10484 /* If the die does not have a name, this is not a namespace
10485 alias. */
10486 attr = dwarf2_attr (die, DW_AT_name, cu);
10487 if (attr != NULL)
10488 {
10489 int num;
10490 struct die_info *d = die;
10491 struct dwarf2_cu *imported_cu = cu;
10492
10493 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10494 keep inspecting DIEs until we hit the underlying import. */
10495 #define MAX_NESTED_IMPORTED_DECLARATIONS 100
10496 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10497 {
10498 attr = dwarf2_attr (d, DW_AT_import, cu);
10499 if (attr == NULL)
10500 break;
10501
10502 d = follow_die_ref (d, attr, &imported_cu);
10503 if (d->tag != DW_TAG_imported_declaration)
10504 break;
10505 }
10506
10507 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10508 {
10509 complaint (_("DIE at %s has too many recursively imported "
10510 "declarations"), sect_offset_str (d->sect_off));
10511 return 0;
10512 }
10513
10514 if (attr != NULL)
10515 {
10516 struct type *type;
10517 sect_offset sect_off = attr->get_ref_die_offset ();
10518
10519 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
10520 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
10521 {
10522 /* This declaration is a global namespace alias. Add
10523 a symbol for it whose type is the aliased namespace. */
10524 new_symbol (die, type, cu);
10525 return 1;
10526 }
10527 }
10528 }
10529
10530 return 0;
10531 }
10532
10533 /* Return the using directives repository (global or local?) to use in the
10534 current context for CU.
10535
10536 For Ada, imported declarations can materialize renamings, which *may* be
10537 global. However it is impossible (for now?) in DWARF to distinguish
10538 "external" imported declarations and "static" ones. As all imported
10539 declarations seem to be static in all other languages, make them all CU-wide
10540 global only in Ada. */
10541
10542 static struct using_direct **
10543 using_directives (struct dwarf2_cu *cu)
10544 {
10545 if (cu->language == language_ada
10546 && cu->get_builder ()->outermost_context_p ())
10547 return cu->get_builder ()->get_global_using_directives ();
10548 else
10549 return cu->get_builder ()->get_local_using_directives ();
10550 }
10551
10552 /* Read the import statement specified by the given die and record it. */
10553
10554 static void
10555 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10556 {
10557 struct objfile *objfile = cu->per_objfile->objfile;
10558 struct attribute *import_attr;
10559 struct die_info *imported_die, *child_die;
10560 struct dwarf2_cu *imported_cu;
10561 const char *imported_name;
10562 const char *imported_name_prefix;
10563 const char *canonical_name;
10564 const char *import_alias;
10565 const char *imported_declaration = NULL;
10566 const char *import_prefix;
10567 std::vector<const char *> excludes;
10568
10569 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10570 if (import_attr == NULL)
10571 {
10572 complaint (_("Tag '%s' has no DW_AT_import"),
10573 dwarf_tag_name (die->tag));
10574 return;
10575 }
10576
10577 imported_cu = cu;
10578 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10579 imported_name = dwarf2_name (imported_die, imported_cu);
10580 if (imported_name == NULL)
10581 {
10582 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10583
10584 The import in the following code:
10585 namespace A
10586 {
10587 typedef int B;
10588 }
10589
10590 int main ()
10591 {
10592 using A::B;
10593 B b;
10594 return b;
10595 }
10596
10597 ...
10598 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10599 <52> DW_AT_decl_file : 1
10600 <53> DW_AT_decl_line : 6
10601 <54> DW_AT_import : <0x75>
10602 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10603 <59> DW_AT_name : B
10604 <5b> DW_AT_decl_file : 1
10605 <5c> DW_AT_decl_line : 2
10606 <5d> DW_AT_type : <0x6e>
10607 ...
10608 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10609 <76> DW_AT_byte_size : 4
10610 <77> DW_AT_encoding : 5 (signed)
10611
10612 imports the wrong die ( 0x75 instead of 0x58 ).
10613 This case will be ignored until the gcc bug is fixed. */
10614 return;
10615 }
10616
10617 /* Figure out the local name after import. */
10618 import_alias = dwarf2_name (die, cu);
10619
10620 /* Figure out where the statement is being imported to. */
10621 import_prefix = determine_prefix (die, cu);
10622
10623 /* Figure out what the scope of the imported die is and prepend it
10624 to the name of the imported die. */
10625 imported_name_prefix = determine_prefix (imported_die, imported_cu);
10626
10627 if (imported_die->tag != DW_TAG_namespace
10628 && imported_die->tag != DW_TAG_module)
10629 {
10630 imported_declaration = imported_name;
10631 canonical_name = imported_name_prefix;
10632 }
10633 else if (strlen (imported_name_prefix) > 0)
10634 canonical_name = obconcat (&objfile->objfile_obstack,
10635 imported_name_prefix,
10636 (cu->language == language_d ? "." : "::"),
10637 imported_name, (char *) NULL);
10638 else
10639 canonical_name = imported_name;
10640
10641 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10642 for (child_die = die->child; child_die && child_die->tag;
10643 child_die = child_die->sibling)
10644 {
10645 /* DWARF-4: A Fortran use statement with a “rename list” may be
10646 represented by an imported module entry with an import attribute
10647 referring to the module and owned entries corresponding to those
10648 entities that are renamed as part of being imported. */
10649
10650 if (child_die->tag != DW_TAG_imported_declaration)
10651 {
10652 complaint (_("child DW_TAG_imported_declaration expected "
10653 "- DIE at %s [in module %s]"),
10654 sect_offset_str (child_die->sect_off),
10655 objfile_name (objfile));
10656 continue;
10657 }
10658
10659 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10660 if (import_attr == NULL)
10661 {
10662 complaint (_("Tag '%s' has no DW_AT_import"),
10663 dwarf_tag_name (child_die->tag));
10664 continue;
10665 }
10666
10667 imported_cu = cu;
10668 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10669 &imported_cu);
10670 imported_name = dwarf2_name (imported_die, imported_cu);
10671 if (imported_name == NULL)
10672 {
10673 complaint (_("child DW_TAG_imported_declaration has unknown "
10674 "imported name - DIE at %s [in module %s]"),
10675 sect_offset_str (child_die->sect_off),
10676 objfile_name (objfile));
10677 continue;
10678 }
10679
10680 excludes.push_back (imported_name);
10681
10682 process_die (child_die, cu);
10683 }
10684
10685 add_using_directive (using_directives (cu),
10686 import_prefix,
10687 canonical_name,
10688 import_alias,
10689 imported_declaration,
10690 excludes,
10691 0,
10692 &objfile->objfile_obstack);
10693 }
10694
10695 /* ICC<14 does not output the required DW_AT_declaration on incomplete
10696 types, but gives them a size of zero. Starting with version 14,
10697 ICC is compatible with GCC. */
10698
10699 static bool
10700 producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10701 {
10702 if (!cu->checked_producer)
10703 check_producer (cu);
10704
10705 return cu->producer_is_icc_lt_14;
10706 }
10707
10708 /* ICC generates a DW_AT_type for C void functions. This was observed on
10709 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10710 which says that void functions should not have a DW_AT_type. */
10711
10712 static bool
10713 producer_is_icc (struct dwarf2_cu *cu)
10714 {
10715 if (!cu->checked_producer)
10716 check_producer (cu);
10717
10718 return cu->producer_is_icc;
10719 }
10720
10721 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10722 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10723 this, it was first present in GCC release 4.3.0. */
10724
10725 static bool
10726 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10727 {
10728 if (!cu->checked_producer)
10729 check_producer (cu);
10730
10731 return cu->producer_is_gcc_lt_4_3;
10732 }
10733
10734 static file_and_directory
10735 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
10736 {
10737 file_and_directory res;
10738
10739 /* Find the filename. Do not use dwarf2_name here, since the filename
10740 is not a source language identifier. */
10741 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10742 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
10743
10744 if (res.comp_dir == NULL
10745 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10746 && IS_ABSOLUTE_PATH (res.name))
10747 {
10748 res.comp_dir_storage = ldirname (res.name);
10749 if (!res.comp_dir_storage.empty ())
10750 res.comp_dir = res.comp_dir_storage.c_str ();
10751 }
10752 if (res.comp_dir != NULL)
10753 {
10754 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10755 directory, get rid of it. */
10756 const char *cp = strchr (res.comp_dir, ':');
10757
10758 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10759 res.comp_dir = cp + 1;
10760 }
10761
10762 if (res.name == NULL)
10763 res.name = "<unknown>";
10764
10765 return res;
10766 }
10767
10768 /* Handle DW_AT_stmt_list for a compilation unit.
10769 DIE is the DW_TAG_compile_unit die for CU.
10770 COMP_DIR is the compilation directory. LOWPC is passed to
10771 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
10772
10773 static void
10774 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
10775 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
10776 {
10777 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10778 struct attribute *attr;
10779 struct line_header line_header_local;
10780 hashval_t line_header_local_hash;
10781 void **slot;
10782 int decode_mapping;
10783
10784 gdb_assert (! cu->per_cu->is_debug_types);
10785
10786 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
10787 if (attr == NULL || !attr->form_is_unsigned ())
10788 return;
10789
10790 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10791
10792 /* The line header hash table is only created if needed (it exists to
10793 prevent redundant reading of the line table for partial_units).
10794 If we're given a partial_unit, we'll need it. If we're given a
10795 compile_unit, then use the line header hash table if it's already
10796 created, but don't create one just yet. */
10797
10798 if (per_objfile->line_header_hash == NULL
10799 && die->tag == DW_TAG_partial_unit)
10800 {
10801 per_objfile->line_header_hash
10802 .reset (htab_create_alloc (127, line_header_hash_voidp,
10803 line_header_eq_voidp,
10804 free_line_header_voidp,
10805 xcalloc, xfree));
10806 }
10807
10808 line_header_local.sect_off = line_offset;
10809 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10810 line_header_local_hash = line_header_hash (&line_header_local);
10811 if (per_objfile->line_header_hash != NULL)
10812 {
10813 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10814 &line_header_local,
10815 line_header_local_hash, NO_INSERT);
10816
10817 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10818 is not present in *SLOT (since if there is something in *SLOT then
10819 it will be for a partial_unit). */
10820 if (die->tag == DW_TAG_partial_unit && slot != NULL)
10821 {
10822 gdb_assert (*slot != NULL);
10823 cu->line_header = (struct line_header *) *slot;
10824 return;
10825 }
10826 }
10827
10828 /* dwarf_decode_line_header does not yet provide sufficient information.
10829 We always have to call also dwarf_decode_lines for it. */
10830 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10831 if (lh == NULL)
10832 return;
10833
10834 cu->line_header = lh.release ();
10835 cu->line_header_die_owner = die;
10836
10837 if (per_objfile->line_header_hash == NULL)
10838 slot = NULL;
10839 else
10840 {
10841 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
10842 &line_header_local,
10843 line_header_local_hash, INSERT);
10844 gdb_assert (slot != NULL);
10845 }
10846 if (slot != NULL && *slot == NULL)
10847 {
10848 /* This newly decoded line number information unit will be owned
10849 by line_header_hash hash table. */
10850 *slot = cu->line_header;
10851 cu->line_header_die_owner = NULL;
10852 }
10853 else
10854 {
10855 /* We cannot free any current entry in (*slot) as that struct line_header
10856 may be already used by multiple CUs. Create only temporary decoded
10857 line_header for this CU - it may happen at most once for each line
10858 number information unit. And if we're not using line_header_hash
10859 then this is what we want as well. */
10860 gdb_assert (die->tag != DW_TAG_partial_unit);
10861 }
10862 decode_mapping = (die->tag != DW_TAG_partial_unit);
10863 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10864 decode_mapping);
10865
10866 }
10867
10868 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
10869
10870 static void
10871 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
10872 {
10873 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10874 struct objfile *objfile = per_objfile->objfile;
10875 struct gdbarch *gdbarch = objfile->arch ();
10876 CORE_ADDR lowpc = ((CORE_ADDR) -1);
10877 CORE_ADDR highpc = ((CORE_ADDR) 0);
10878 struct attribute *attr;
10879 struct die_info *child_die;
10880 CORE_ADDR baseaddr;
10881
10882 prepare_one_comp_unit (cu, die, cu->language);
10883 baseaddr = objfile->text_section_offset ();
10884
10885 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
10886
10887 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10888 from finish_block. */
10889 if (lowpc == ((CORE_ADDR) -1))
10890 lowpc = highpc;
10891 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
10892
10893 file_and_directory fnd = find_file_and_directory (die, cu);
10894
10895 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10896 standardised yet. As a workaround for the language detection we fall
10897 back to the DW_AT_producer string. */
10898 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10899 cu->language = language_opencl;
10900
10901 /* Similar hack for Go. */
10902 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10903 set_cu_language (DW_LANG_Go, cu);
10904
10905 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
10906
10907 /* Decode line number information if present. We do this before
10908 processing child DIEs, so that the line header table is available
10909 for DW_AT_decl_file. */
10910 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
10911
10912 /* Process all dies in compilation unit. */
10913 if (die->child != NULL)
10914 {
10915 child_die = die->child;
10916 while (child_die && child_die->tag)
10917 {
10918 process_die (child_die, cu);
10919 child_die = child_die->sibling;
10920 }
10921 }
10922
10923 /* Decode macro information, if present. Dwarf 2 macro information
10924 refers to information in the line number info statement program
10925 header, so we can only read it if we've read the header
10926 successfully. */
10927 attr = dwarf2_attr (die, DW_AT_macros, cu);
10928 if (attr == NULL)
10929 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
10930 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10931 {
10932 if (dwarf2_attr (die, DW_AT_macro_info, cu))
10933 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
10934
10935 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
10936 }
10937 else
10938 {
10939 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10940 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
10941 {
10942 unsigned int macro_offset = attr->as_unsigned ();
10943
10944 dwarf_decode_macros (cu, macro_offset, 0);
10945 }
10946 }
10947 }
10948
10949 void
10950 dwarf2_cu::setup_type_unit_groups (struct die_info *die)
10951 {
10952 struct type_unit_group *tu_group;
10953 int first_time;
10954 struct attribute *attr;
10955 unsigned int i;
10956 struct signatured_type *sig_type;
10957
10958 gdb_assert (per_cu->is_debug_types);
10959 sig_type = (struct signatured_type *) per_cu;
10960
10961 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
10962
10963 /* If we're using .gdb_index (includes -readnow) then
10964 per_cu->type_unit_group may not have been set up yet. */
10965 if (sig_type->type_unit_group == NULL)
10966 sig_type->type_unit_group = get_type_unit_group (this, attr);
10967 tu_group = sig_type->type_unit_group;
10968
10969 /* If we've already processed this stmt_list there's no real need to
10970 do it again, we could fake it and just recreate the part we need
10971 (file name,index -> symtab mapping). If data shows this optimization
10972 is useful we can do it then. */
10973 type_unit_group_unshareable *tug_unshare
10974 = per_objfile->get_type_unit_group_unshareable (tu_group);
10975 first_time = tug_unshare->compunit_symtab == NULL;
10976
10977 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10978 debug info. */
10979 line_header_up lh;
10980 if (attr != NULL && attr->form_is_unsigned ())
10981 {
10982 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
10983 lh = dwarf_decode_line_header (line_offset, this);
10984 }
10985 if (lh == NULL)
10986 {
10987 if (first_time)
10988 start_symtab ("", NULL, 0);
10989 else
10990 {
10991 gdb_assert (tug_unshare->symtabs == NULL);
10992 gdb_assert (m_builder == nullptr);
10993 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
10994 m_builder.reset (new struct buildsym_compunit
10995 (COMPUNIT_OBJFILE (cust), "",
10996 COMPUNIT_DIRNAME (cust),
10997 compunit_language (cust),
10998 0, cust));
10999 list_in_scope = get_builder ()->get_file_symbols ();
11000 }
11001 return;
11002 }
11003
11004 line_header = lh.release ();
11005 line_header_die_owner = die;
11006
11007 if (first_time)
11008 {
11009 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
11010
11011 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11012 still initializing it, and our caller (a few levels up)
11013 process_full_type_unit still needs to know if this is the first
11014 time. */
11015
11016 tug_unshare->symtabs
11017 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11018 struct symtab *, line_header->file_names_size ());
11019
11020 auto &file_names = line_header->file_names ();
11021 for (i = 0; i < file_names.size (); ++i)
11022 {
11023 file_entry &fe = file_names[i];
11024 dwarf2_start_subfile (this, fe.name,
11025 fe.include_dir (line_header));
11026 buildsym_compunit *b = get_builder ();
11027 if (b->get_current_subfile ()->symtab == NULL)
11028 {
11029 /* NOTE: start_subfile will recognize when it's been
11030 passed a file it has already seen. So we can't
11031 assume there's a simple mapping from
11032 cu->line_header->file_names to subfiles, plus
11033 cu->line_header->file_names may contain dups. */
11034 b->get_current_subfile ()->symtab
11035 = allocate_symtab (cust, b->get_current_subfile ()->name);
11036 }
11037
11038 fe.symtab = b->get_current_subfile ()->symtab;
11039 tug_unshare->symtabs[i] = fe.symtab;
11040 }
11041 }
11042 else
11043 {
11044 gdb_assert (m_builder == nullptr);
11045 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
11046 m_builder.reset (new struct buildsym_compunit
11047 (COMPUNIT_OBJFILE (cust), "",
11048 COMPUNIT_DIRNAME (cust),
11049 compunit_language (cust),
11050 0, cust));
11051 list_in_scope = get_builder ()->get_file_symbols ();
11052
11053 auto &file_names = line_header->file_names ();
11054 for (i = 0; i < file_names.size (); ++i)
11055 {
11056 file_entry &fe = file_names[i];
11057 fe.symtab = tug_unshare->symtabs[i];
11058 }
11059 }
11060
11061 /* The main symtab is allocated last. Type units don't have DW_AT_name
11062 so they don't have a "real" (so to speak) symtab anyway.
11063 There is later code that will assign the main symtab to all symbols
11064 that don't have one. We need to handle the case of a symbol with a
11065 missing symtab (DW_AT_decl_file) anyway. */
11066 }
11067
11068 /* Process DW_TAG_type_unit.
11069 For TUs we want to skip the first top level sibling if it's not the
11070 actual type being defined by this TU. In this case the first top
11071 level sibling is there to provide context only. */
11072
11073 static void
11074 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11075 {
11076 struct die_info *child_die;
11077
11078 prepare_one_comp_unit (cu, die, language_minimal);
11079
11080 /* Initialize (or reinitialize) the machinery for building symtabs.
11081 We do this before processing child DIEs, so that the line header table
11082 is available for DW_AT_decl_file. */
11083 cu->setup_type_unit_groups (die);
11084
11085 if (die->child != NULL)
11086 {
11087 child_die = die->child;
11088 while (child_die && child_die->tag)
11089 {
11090 process_die (child_die, cu);
11091 child_die = child_die->sibling;
11092 }
11093 }
11094 }
11095 \f
11096 /* DWO/DWP files.
11097
11098 http://gcc.gnu.org/wiki/DebugFission
11099 http://gcc.gnu.org/wiki/DebugFissionDWP
11100
11101 To simplify handling of both DWO files ("object" files with the DWARF info)
11102 and DWP files (a file with the DWOs packaged up into one file), we treat
11103 DWP files as having a collection of virtual DWO files. */
11104
11105 static hashval_t
11106 hash_dwo_file (const void *item)
11107 {
11108 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
11109 hashval_t hash;
11110
11111 hash = htab_hash_string (dwo_file->dwo_name);
11112 if (dwo_file->comp_dir != NULL)
11113 hash += htab_hash_string (dwo_file->comp_dir);
11114 return hash;
11115 }
11116
11117 static int
11118 eq_dwo_file (const void *item_lhs, const void *item_rhs)
11119 {
11120 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11121 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
11122
11123 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11124 return 0;
11125 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11126 return lhs->comp_dir == rhs->comp_dir;
11127 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
11128 }
11129
11130 /* Allocate a hash table for DWO files. */
11131
11132 static htab_up
11133 allocate_dwo_file_hash_table ()
11134 {
11135 auto delete_dwo_file = [] (void *item)
11136 {
11137 struct dwo_file *dwo_file = (struct dwo_file *) item;
11138
11139 delete dwo_file;
11140 };
11141
11142 return htab_up (htab_create_alloc (41,
11143 hash_dwo_file,
11144 eq_dwo_file,
11145 delete_dwo_file,
11146 xcalloc, xfree));
11147 }
11148
11149 /* Lookup DWO file DWO_NAME. */
11150
11151 static void **
11152 lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
11153 const char *dwo_name,
11154 const char *comp_dir)
11155 {
11156 struct dwo_file find_entry;
11157 void **slot;
11158
11159 if (per_objfile->per_bfd->dwo_files == NULL)
11160 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
11161
11162 find_entry.dwo_name = dwo_name;
11163 find_entry.comp_dir = comp_dir;
11164 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
11165 INSERT);
11166
11167 return slot;
11168 }
11169
11170 static hashval_t
11171 hash_dwo_unit (const void *item)
11172 {
11173 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
11174
11175 /* This drops the top 32 bits of the id, but is ok for a hash. */
11176 return dwo_unit->signature;
11177 }
11178
11179 static int
11180 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11181 {
11182 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11183 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
11184
11185 /* The signature is assumed to be unique within the DWO file.
11186 So while object file CU dwo_id's always have the value zero,
11187 that's OK, assuming each object file DWO file has only one CU,
11188 and that's the rule for now. */
11189 return lhs->signature == rhs->signature;
11190 }
11191
11192 /* Allocate a hash table for DWO CUs,TUs.
11193 There is one of these tables for each of CUs,TUs for each DWO file. */
11194
11195 static htab_up
11196 allocate_dwo_unit_table ()
11197 {
11198 /* Start out with a pretty small number.
11199 Generally DWO files contain only one CU and maybe some TUs. */
11200 return htab_up (htab_create_alloc (3,
11201 hash_dwo_unit,
11202 eq_dwo_unit,
11203 NULL, xcalloc, xfree));
11204 }
11205
11206 /* die_reader_func for create_dwo_cu. */
11207
11208 static void
11209 create_dwo_cu_reader (const struct die_reader_specs *reader,
11210 const gdb_byte *info_ptr,
11211 struct die_info *comp_unit_die,
11212 struct dwo_file *dwo_file,
11213 struct dwo_unit *dwo_unit)
11214 {
11215 struct dwarf2_cu *cu = reader->cu;
11216 sect_offset sect_off = cu->per_cu->sect_off;
11217 struct dwarf2_section_info *section = cu->per_cu->section;
11218
11219 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11220 if (!signature.has_value ())
11221 {
11222 complaint (_("Dwarf Error: debug entry at offset %s is missing"
11223 " its dwo_id [in module %s]"),
11224 sect_offset_str (sect_off), dwo_file->dwo_name);
11225 return;
11226 }
11227
11228 dwo_unit->dwo_file = dwo_file;
11229 dwo_unit->signature = *signature;
11230 dwo_unit->section = section;
11231 dwo_unit->sect_off = sect_off;
11232 dwo_unit->length = cu->per_cu->length;
11233
11234 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11235 sect_offset_str (sect_off),
11236 hex_string (dwo_unit->signature));
11237 }
11238
11239 /* Create the dwo_units for the CUs in a DWO_FILE.
11240 Note: This function processes DWO files only, not DWP files. */
11241
11242 static void
11243 create_cus_hash_table (dwarf2_per_objfile *per_objfile,
11244 dwarf2_cu *cu, struct dwo_file &dwo_file,
11245 dwarf2_section_info &section, htab_up &cus_htab)
11246 {
11247 struct objfile *objfile = per_objfile->objfile;
11248 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
11249 const gdb_byte *info_ptr, *end_ptr;
11250
11251 section.read (objfile);
11252 info_ptr = section.buffer;
11253
11254 if (info_ptr == NULL)
11255 return;
11256
11257 dwarf_read_debug_printf ("Reading %s for %s:",
11258 section.get_name (),
11259 section.get_file_name ());
11260
11261 end_ptr = info_ptr + section.size;
11262 while (info_ptr < end_ptr)
11263 {
11264 struct dwarf2_per_cu_data per_cu;
11265 struct dwo_unit read_unit {};
11266 struct dwo_unit *dwo_unit;
11267 void **slot;
11268 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
11269
11270 memset (&per_cu, 0, sizeof (per_cu));
11271 per_cu.per_bfd = per_bfd;
11272 per_cu.is_debug_types = 0;
11273 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11274 per_cu.section = &section;
11275
11276 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
11277 if (!reader.dummy_p)
11278 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
11279 &dwo_file, &read_unit);
11280 info_ptr += per_cu.length;
11281
11282 // If the unit could not be parsed, skip it.
11283 if (read_unit.dwo_file == NULL)
11284 continue;
11285
11286 if (cus_htab == NULL)
11287 cus_htab = allocate_dwo_unit_table ();
11288
11289 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
11290 struct dwo_unit);
11291 *dwo_unit = read_unit;
11292 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
11293 gdb_assert (slot != NULL);
11294 if (*slot != NULL)
11295 {
11296 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11297 sect_offset dup_sect_off = dup_cu->sect_off;
11298
11299 complaint (_("debug cu entry at offset %s is duplicate to"
11300 " the entry at offset %s, signature %s"),
11301 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
11302 hex_string (dwo_unit->signature));
11303 }
11304 *slot = (void *)dwo_unit;
11305 }
11306 }
11307
11308 /* DWP file .debug_{cu,tu}_index section format:
11309 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11310 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11311
11312 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11313 officially standard DWP format was published with DWARF v5 and is called
11314 Version 5. There are no versions 3 or 4.
11315
11316 DWP Version 1:
11317
11318 Both index sections have the same format, and serve to map a 64-bit
11319 signature to a set of section numbers. Each section begins with a header,
11320 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11321 indexes, and a pool of 32-bit section numbers. The index sections will be
11322 aligned at 8-byte boundaries in the file.
11323
11324 The index section header consists of:
11325
11326 V, 32 bit version number
11327 -, 32 bits unused
11328 N, 32 bit number of compilation units or type units in the index
11329 M, 32 bit number of slots in the hash table
11330
11331 Numbers are recorded using the byte order of the application binary.
11332
11333 The hash table begins at offset 16 in the section, and consists of an array
11334 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11335 order of the application binary). Unused slots in the hash table are 0.
11336 (We rely on the extreme unlikeliness of a signature being exactly 0.)
11337
11338 The parallel table begins immediately after the hash table
11339 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11340 array of 32-bit indexes (using the byte order of the application binary),
11341 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11342 table contains a 32-bit index into the pool of section numbers. For unused
11343 hash table slots, the corresponding entry in the parallel table will be 0.
11344
11345 The pool of section numbers begins immediately following the hash table
11346 (at offset 16 + 12 * M from the beginning of the section). The pool of
11347 section numbers consists of an array of 32-bit words (using the byte order
11348 of the application binary). Each item in the array is indexed starting
11349 from 0. The hash table entry provides the index of the first section
11350 number in the set. Additional section numbers in the set follow, and the
11351 set is terminated by a 0 entry (section number 0 is not used in ELF).
11352
11353 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11354 section must be the first entry in the set, and the .debug_abbrev.dwo must
11355 be the second entry. Other members of the set may follow in any order.
11356
11357 ---
11358
11359 DWP Versions 2 and 5:
11360
11361 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
11362 and the entries in the index tables are now offsets into these sections.
11363 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11364 section.
11365
11366 Index Section Contents:
11367 Header
11368 Hash Table of Signatures dwp_hash_table.hash_table
11369 Parallel Table of Indices dwp_hash_table.unit_table
11370 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11371 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
11372
11373 The index section header consists of:
11374
11375 V, 32 bit version number
11376 L, 32 bit number of columns in the table of section offsets
11377 N, 32 bit number of compilation units or type units in the index
11378 M, 32 bit number of slots in the hash table
11379
11380 Numbers are recorded using the byte order of the application binary.
11381
11382 The hash table has the same format as version 1.
11383 The parallel table of indices has the same format as version 1,
11384 except that the entries are origin-1 indices into the table of sections
11385 offsets and the table of section sizes.
11386
11387 The table of offsets begins immediately following the parallel table
11388 (at offset 16 + 12 * M from the beginning of the section). The table is
11389 a two-dimensional array of 32-bit words (using the byte order of the
11390 application binary), with L columns and N+1 rows, in row-major order.
11391 Each row in the array is indexed starting from 0. The first row provides
11392 a key to the remaining rows: each column in this row provides an identifier
11393 for a debug section, and the offsets in the same column of subsequent rows
11394 refer to that section. The section identifiers for Version 2 are:
11395
11396 DW_SECT_INFO 1 .debug_info.dwo
11397 DW_SECT_TYPES 2 .debug_types.dwo
11398 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11399 DW_SECT_LINE 4 .debug_line.dwo
11400 DW_SECT_LOC 5 .debug_loc.dwo
11401 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11402 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11403 DW_SECT_MACRO 8 .debug_macro.dwo
11404
11405 The section identifiers for Version 5 are:
11406
11407 DW_SECT_INFO_V5 1 .debug_info.dwo
11408 DW_SECT_RESERVED_V5 2 --
11409 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11410 DW_SECT_LINE_V5 4 .debug_line.dwo
11411 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11412 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11413 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11414 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11415
11416 The offsets provided by the CU and TU index sections are the base offsets
11417 for the contributions made by each CU or TU to the corresponding section
11418 in the package file. Each CU and TU header contains an abbrev_offset
11419 field, used to find the abbreviations table for that CU or TU within the
11420 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11421 be interpreted as relative to the base offset given in the index section.
11422 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11423 should be interpreted as relative to the base offset for .debug_line.dwo,
11424 and offsets into other debug sections obtained from DWARF attributes should
11425 also be interpreted as relative to the corresponding base offset.
11426
11427 The table of sizes begins immediately following the table of offsets.
11428 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11429 with L columns and N rows, in row-major order. Each row in the array is
11430 indexed starting from 1 (row 0 is shared by the two tables).
11431
11432 ---
11433
11434 Hash table lookup is handled the same in version 1 and 2:
11435
11436 We assume that N and M will not exceed 2^32 - 1.
11437 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11438
11439 Given a 64-bit compilation unit signature or a type signature S, an entry
11440 in the hash table is located as follows:
11441
11442 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11443 the low-order k bits all set to 1.
11444
11445 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
11446
11447 3) If the hash table entry at index H matches the signature, use that
11448 entry. If the hash table entry at index H is unused (all zeroes),
11449 terminate the search: the signature is not present in the table.
11450
11451 4) Let H = (H + H') modulo M. Repeat at Step 3.
11452
11453 Because M > N and H' and M are relatively prime, the search is guaranteed
11454 to stop at an unused slot or find the match. */
11455
11456 /* Create a hash table to map DWO IDs to their CU/TU entry in
11457 .debug_{info,types}.dwo in DWP_FILE.
11458 Returns NULL if there isn't one.
11459 Note: This function processes DWP files only, not DWO files. */
11460
11461 static struct dwp_hash_table *
11462 create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
11463 struct dwp_file *dwp_file, int is_debug_types)
11464 {
11465 struct objfile *objfile = per_objfile->objfile;
11466 bfd *dbfd = dwp_file->dbfd.get ();
11467 const gdb_byte *index_ptr, *index_end;
11468 struct dwarf2_section_info *index;
11469 uint32_t version, nr_columns, nr_units, nr_slots;
11470 struct dwp_hash_table *htab;
11471
11472 if (is_debug_types)
11473 index = &dwp_file->sections.tu_index;
11474 else
11475 index = &dwp_file->sections.cu_index;
11476
11477 if (index->empty ())
11478 return NULL;
11479 index->read (objfile);
11480
11481 index_ptr = index->buffer;
11482 index_end = index_ptr + index->size;
11483
11484 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11485 For now it's safe to just read 4 bytes (particularly as it's difficult to
11486 tell if you're dealing with Version 5 before you've read the version). */
11487 version = read_4_bytes (dbfd, index_ptr);
11488 index_ptr += 4;
11489 if (version == 2 || version == 5)
11490 nr_columns = read_4_bytes (dbfd, index_ptr);
11491 else
11492 nr_columns = 0;
11493 index_ptr += 4;
11494 nr_units = read_4_bytes (dbfd, index_ptr);
11495 index_ptr += 4;
11496 nr_slots = read_4_bytes (dbfd, index_ptr);
11497 index_ptr += 4;
11498
11499 if (version != 1 && version != 2 && version != 5)
11500 {
11501 error (_("Dwarf Error: unsupported DWP file version (%s)"
11502 " [in module %s]"),
11503 pulongest (version), dwp_file->name);
11504 }
11505 if (nr_slots != (nr_slots & -nr_slots))
11506 {
11507 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
11508 " is not power of 2 [in module %s]"),
11509 pulongest (nr_slots), dwp_file->name);
11510 }
11511
11512 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
11513 htab->version = version;
11514 htab->nr_columns = nr_columns;
11515 htab->nr_units = nr_units;
11516 htab->nr_slots = nr_slots;
11517 htab->hash_table = index_ptr;
11518 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
11519
11520 /* Exit early if the table is empty. */
11521 if (nr_slots == 0 || nr_units == 0
11522 || (version == 2 && nr_columns == 0)
11523 || (version == 5 && nr_columns == 0))
11524 {
11525 /* All must be zero. */
11526 if (nr_slots != 0 || nr_units != 0
11527 || (version == 2 && nr_columns != 0)
11528 || (version == 5 && nr_columns != 0))
11529 {
11530 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
11531 " all zero [in modules %s]"),
11532 dwp_file->name);
11533 }
11534 return htab;
11535 }
11536
11537 if (version == 1)
11538 {
11539 htab->section_pool.v1.indices =
11540 htab->unit_table + sizeof (uint32_t) * nr_slots;
11541 /* It's harder to decide whether the section is too small in v1.
11542 V1 is deprecated anyway so we punt. */
11543 }
11544 else if (version == 2)
11545 {
11546 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11547 int *ids = htab->section_pool.v2.section_ids;
11548 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
11549 /* Reverse map for error checking. */
11550 int ids_seen[DW_SECT_MAX + 1];
11551 int i;
11552
11553 if (nr_columns < 2)
11554 {
11555 error (_("Dwarf Error: bad DWP hash table, too few columns"
11556 " in section table [in module %s]"),
11557 dwp_file->name);
11558 }
11559 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11560 {
11561 error (_("Dwarf Error: bad DWP hash table, too many columns"
11562 " in section table [in module %s]"),
11563 dwp_file->name);
11564 }
11565 memset (ids, 255, sizeof_ids);
11566 memset (ids_seen, 255, sizeof (ids_seen));
11567 for (i = 0; i < nr_columns; ++i)
11568 {
11569 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11570
11571 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11572 {
11573 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11574 " in section table [in module %s]"),
11575 id, dwp_file->name);
11576 }
11577 if (ids_seen[id] != -1)
11578 {
11579 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11580 " id %d in section table [in module %s]"),
11581 id, dwp_file->name);
11582 }
11583 ids_seen[id] = i;
11584 ids[i] = id;
11585 }
11586 /* Must have exactly one info or types section. */
11587 if (((ids_seen[DW_SECT_INFO] != -1)
11588 + (ids_seen[DW_SECT_TYPES] != -1))
11589 != 1)
11590 {
11591 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11592 " DWO info/types section [in module %s]"),
11593 dwp_file->name);
11594 }
11595 /* Must have an abbrev section. */
11596 if (ids_seen[DW_SECT_ABBREV] == -1)
11597 {
11598 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11599 " section [in module %s]"),
11600 dwp_file->name);
11601 }
11602 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11603 htab->section_pool.v2.sizes =
11604 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11605 * nr_units * nr_columns);
11606 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11607 * nr_units * nr_columns))
11608 > index_end)
11609 {
11610 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11611 " [in module %s]"),
11612 dwp_file->name);
11613 }
11614 }
11615 else /* version == 5 */
11616 {
11617 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11618 int *ids = htab->section_pool.v5.section_ids;
11619 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11620 /* Reverse map for error checking. */
11621 int ids_seen[DW_SECT_MAX_V5 + 1];
11622
11623 if (nr_columns < 2)
11624 {
11625 error (_("Dwarf Error: bad DWP hash table, too few columns"
11626 " in section table [in module %s]"),
11627 dwp_file->name);
11628 }
11629 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11630 {
11631 error (_("Dwarf Error: bad DWP hash table, too many columns"
11632 " in section table [in module %s]"),
11633 dwp_file->name);
11634 }
11635 memset (ids, 255, sizeof_ids);
11636 memset (ids_seen, 255, sizeof (ids_seen));
11637 for (int i = 0; i < nr_columns; ++i)
11638 {
11639 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11640
11641 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11642 {
11643 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11644 " in section table [in module %s]"),
11645 id, dwp_file->name);
11646 }
11647 if (ids_seen[id] != -1)
11648 {
11649 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11650 " id %d in section table [in module %s]"),
11651 id, dwp_file->name);
11652 }
11653 ids_seen[id] = i;
11654 ids[i] = id;
11655 }
11656 /* Must have seen an info section. */
11657 if (ids_seen[DW_SECT_INFO_V5] == -1)
11658 {
11659 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11660 " DWO info/types section [in module %s]"),
11661 dwp_file->name);
11662 }
11663 /* Must have an abbrev section. */
11664 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11665 {
11666 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11667 " section [in module %s]"),
11668 dwp_file->name);
11669 }
11670 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11671 htab->section_pool.v5.sizes
11672 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
11673 * nr_units * nr_columns);
11674 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
11675 * nr_units * nr_columns))
11676 > index_end)
11677 {
11678 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11679 " [in module %s]"),
11680 dwp_file->name);
11681 }
11682 }
11683
11684 return htab;
11685 }
11686
11687 /* Update SECTIONS with the data from SECTP.
11688
11689 This function is like the other "locate" section routines, but in
11690 this context the sections to read comes from the DWP V1 hash table,
11691 not the full ELF section table.
11692
11693 The result is non-zero for success, or zero if an error was found. */
11694
11695 static int
11696 locate_v1_virtual_dwo_sections (asection *sectp,
11697 struct virtual_v1_dwo_sections *sections)
11698 {
11699 const struct dwop_section_names *names = &dwop_section_names;
11700
11701 if (names->abbrev_dwo.matches (sectp->name))
11702 {
11703 /* There can be only one. */
11704 if (sections->abbrev.s.section != NULL)
11705 return 0;
11706 sections->abbrev.s.section = sectp;
11707 sections->abbrev.size = bfd_section_size (sectp);
11708 }
11709 else if (names->info_dwo.matches (sectp->name)
11710 || names->types_dwo.matches (sectp->name))
11711 {
11712 /* There can be only one. */
11713 if (sections->info_or_types.s.section != NULL)
11714 return 0;
11715 sections->info_or_types.s.section = sectp;
11716 sections->info_or_types.size = bfd_section_size (sectp);
11717 }
11718 else if (names->line_dwo.matches (sectp->name))
11719 {
11720 /* There can be only one. */
11721 if (sections->line.s.section != NULL)
11722 return 0;
11723 sections->line.s.section = sectp;
11724 sections->line.size = bfd_section_size (sectp);
11725 }
11726 else if (names->loc_dwo.matches (sectp->name))
11727 {
11728 /* There can be only one. */
11729 if (sections->loc.s.section != NULL)
11730 return 0;
11731 sections->loc.s.section = sectp;
11732 sections->loc.size = bfd_section_size (sectp);
11733 }
11734 else if (names->macinfo_dwo.matches (sectp->name))
11735 {
11736 /* There can be only one. */
11737 if (sections->macinfo.s.section != NULL)
11738 return 0;
11739 sections->macinfo.s.section = sectp;
11740 sections->macinfo.size = bfd_section_size (sectp);
11741 }
11742 else if (names->macro_dwo.matches (sectp->name))
11743 {
11744 /* There can be only one. */
11745 if (sections->macro.s.section != NULL)
11746 return 0;
11747 sections->macro.s.section = sectp;
11748 sections->macro.size = bfd_section_size (sectp);
11749 }
11750 else if (names->str_offsets_dwo.matches (sectp->name))
11751 {
11752 /* There can be only one. */
11753 if (sections->str_offsets.s.section != NULL)
11754 return 0;
11755 sections->str_offsets.s.section = sectp;
11756 sections->str_offsets.size = bfd_section_size (sectp);
11757 }
11758 else
11759 {
11760 /* No other kind of section is valid. */
11761 return 0;
11762 }
11763
11764 return 1;
11765 }
11766
11767 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11768 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11769 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11770 This is for DWP version 1 files. */
11771
11772 static struct dwo_unit *
11773 create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
11774 struct dwp_file *dwp_file,
11775 uint32_t unit_index,
11776 const char *comp_dir,
11777 ULONGEST signature, int is_debug_types)
11778 {
11779 const struct dwp_hash_table *dwp_htab =
11780 is_debug_types ? dwp_file->tus : dwp_file->cus;
11781 bfd *dbfd = dwp_file->dbfd.get ();
11782 const char *kind = is_debug_types ? "TU" : "CU";
11783 struct dwo_file *dwo_file;
11784 struct dwo_unit *dwo_unit;
11785 struct virtual_v1_dwo_sections sections;
11786 void **dwo_file_slot;
11787 int i;
11788
11789 gdb_assert (dwp_file->version == 1);
11790
11791 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
11792 kind, pulongest (unit_index), hex_string (signature),
11793 dwp_file->name);
11794
11795 /* Fetch the sections of this DWO unit.
11796 Put a limit on the number of sections we look for so that bad data
11797 doesn't cause us to loop forever. */
11798
11799 #define MAX_NR_V1_DWO_SECTIONS \
11800 (1 /* .debug_info or .debug_types */ \
11801 + 1 /* .debug_abbrev */ \
11802 + 1 /* .debug_line */ \
11803 + 1 /* .debug_loc */ \
11804 + 1 /* .debug_str_offsets */ \
11805 + 1 /* .debug_macro or .debug_macinfo */ \
11806 + 1 /* trailing zero */)
11807
11808 memset (&sections, 0, sizeof (sections));
11809
11810 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
11811 {
11812 asection *sectp;
11813 uint32_t section_nr =
11814 read_4_bytes (dbfd,
11815 dwp_htab->section_pool.v1.indices
11816 + (unit_index + i) * sizeof (uint32_t));
11817
11818 if (section_nr == 0)
11819 break;
11820 if (section_nr >= dwp_file->num_sections)
11821 {
11822 error (_("Dwarf Error: bad DWP hash table, section number too large"
11823 " [in module %s]"),
11824 dwp_file->name);
11825 }
11826
11827 sectp = dwp_file->elf_sections[section_nr];
11828 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
11829 {
11830 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11831 " [in module %s]"),
11832 dwp_file->name);
11833 }
11834 }
11835
11836 if (i < 2
11837 || sections.info_or_types.empty ()
11838 || sections.abbrev.empty ())
11839 {
11840 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11841 " [in module %s]"),
11842 dwp_file->name);
11843 }
11844 if (i == MAX_NR_V1_DWO_SECTIONS)
11845 {
11846 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11847 " [in module %s]"),
11848 dwp_file->name);
11849 }
11850
11851 /* It's easier for the rest of the code if we fake a struct dwo_file and
11852 have dwo_unit "live" in that. At least for now.
11853
11854 The DWP file can be made up of a random collection of CUs and TUs.
11855 However, for each CU + set of TUs that came from the same original DWO
11856 file, we can combine them back into a virtual DWO file to save space
11857 (fewer struct dwo_file objects to allocate). Remember that for really
11858 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11859
11860 std::string virtual_dwo_name =
11861 string_printf ("virtual-dwo/%d-%d-%d-%d",
11862 sections.abbrev.get_id (),
11863 sections.line.get_id (),
11864 sections.loc.get_id (),
11865 sections.str_offsets.get_id ());
11866 /* Can we use an existing virtual DWO file? */
11867 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
11868 comp_dir);
11869 /* Create one if necessary. */
11870 if (*dwo_file_slot == NULL)
11871 {
11872 dwarf_read_debug_printf ("Creating virtual DWO: %s",
11873 virtual_dwo_name.c_str ());
11874
11875 dwo_file = new struct dwo_file;
11876 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
11877 dwo_file->comp_dir = comp_dir;
11878 dwo_file->sections.abbrev = sections.abbrev;
11879 dwo_file->sections.line = sections.line;
11880 dwo_file->sections.loc = sections.loc;
11881 dwo_file->sections.macinfo = sections.macinfo;
11882 dwo_file->sections.macro = sections.macro;
11883 dwo_file->sections.str_offsets = sections.str_offsets;
11884 /* The "str" section is global to the entire DWP file. */
11885 dwo_file->sections.str = dwp_file->sections.str;
11886 /* The info or types section is assigned below to dwo_unit,
11887 there's no need to record it in dwo_file.
11888 Also, we can't simply record type sections in dwo_file because
11889 we record a pointer into the vector in dwo_unit. As we collect more
11890 types we'll grow the vector and eventually have to reallocate space
11891 for it, invalidating all copies of pointers into the previous
11892 contents. */
11893 *dwo_file_slot = dwo_file;
11894 }
11895 else
11896 {
11897 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
11898 virtual_dwo_name.c_str ());
11899
11900 dwo_file = (struct dwo_file *) *dwo_file_slot;
11901 }
11902
11903 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
11904 dwo_unit->dwo_file = dwo_file;
11905 dwo_unit->signature = signature;
11906 dwo_unit->section =
11907 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
11908 *dwo_unit->section = sections.info_or_types;
11909 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11910
11911 return dwo_unit;
11912 }
11913
11914 /* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
11915 simplify them. Given a pointer to the containing section SECTION, and
11916 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
11917 virtual section of just that piece. */
11918
11919 static struct dwarf2_section_info
11920 create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
11921 struct dwarf2_section_info *section,
11922 bfd_size_type offset, bfd_size_type size)
11923 {
11924 struct dwarf2_section_info result;
11925 asection *sectp;
11926
11927 gdb_assert (section != NULL);
11928 gdb_assert (!section->is_virtual);
11929
11930 memset (&result, 0, sizeof (result));
11931 result.s.containing_section = section;
11932 result.is_virtual = true;
11933
11934 if (size == 0)
11935 return result;
11936
11937 sectp = section->get_bfd_section ();
11938
11939 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11940 bounds of the real section. This is a pretty-rare event, so just
11941 flag an error (easier) instead of a warning and trying to cope. */
11942 if (sectp == NULL
11943 || offset + size > bfd_section_size (sectp))
11944 {
11945 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
11946 " in section %s [in module %s]"),
11947 sectp ? bfd_section_name (sectp) : "<unknown>",
11948 objfile_name (per_objfile->objfile));
11949 }
11950
11951 result.virtual_offset = offset;
11952 result.size = size;
11953 return result;
11954 }
11955
11956 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11957 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11958 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11959 This is for DWP version 2 files. */
11960
11961 static struct dwo_unit *
11962 create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
11963 struct dwp_file *dwp_file,
11964 uint32_t unit_index,
11965 const char *comp_dir,
11966 ULONGEST signature, int is_debug_types)
11967 {
11968 const struct dwp_hash_table *dwp_htab =
11969 is_debug_types ? dwp_file->tus : dwp_file->cus;
11970 bfd *dbfd = dwp_file->dbfd.get ();
11971 const char *kind = is_debug_types ? "TU" : "CU";
11972 struct dwo_file *dwo_file;
11973 struct dwo_unit *dwo_unit;
11974 struct virtual_v2_or_v5_dwo_sections sections;
11975 void **dwo_file_slot;
11976 int i;
11977
11978 gdb_assert (dwp_file->version == 2);
11979
11980 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
11981 kind, pulongest (unit_index), hex_string (signature),
11982 dwp_file->name);
11983
11984 /* Fetch the section offsets of this DWO unit. */
11985
11986 memset (&sections, 0, sizeof (sections));
11987
11988 for (i = 0; i < dwp_htab->nr_columns; ++i)
11989 {
11990 uint32_t offset = read_4_bytes (dbfd,
11991 dwp_htab->section_pool.v2.offsets
11992 + (((unit_index - 1) * dwp_htab->nr_columns
11993 + i)
11994 * sizeof (uint32_t)));
11995 uint32_t size = read_4_bytes (dbfd,
11996 dwp_htab->section_pool.v2.sizes
11997 + (((unit_index - 1) * dwp_htab->nr_columns
11998 + i)
11999 * sizeof (uint32_t)));
12000
12001 switch (dwp_htab->section_pool.v2.section_ids[i])
12002 {
12003 case DW_SECT_INFO:
12004 case DW_SECT_TYPES:
12005 sections.info_or_types_offset = offset;
12006 sections.info_or_types_size = size;
12007 break;
12008 case DW_SECT_ABBREV:
12009 sections.abbrev_offset = offset;
12010 sections.abbrev_size = size;
12011 break;
12012 case DW_SECT_LINE:
12013 sections.line_offset = offset;
12014 sections.line_size = size;
12015 break;
12016 case DW_SECT_LOC:
12017 sections.loc_offset = offset;
12018 sections.loc_size = size;
12019 break;
12020 case DW_SECT_STR_OFFSETS:
12021 sections.str_offsets_offset = offset;
12022 sections.str_offsets_size = size;
12023 break;
12024 case DW_SECT_MACINFO:
12025 sections.macinfo_offset = offset;
12026 sections.macinfo_size = size;
12027 break;
12028 case DW_SECT_MACRO:
12029 sections.macro_offset = offset;
12030 sections.macro_size = size;
12031 break;
12032 }
12033 }
12034
12035 /* It's easier for the rest of the code if we fake a struct dwo_file and
12036 have dwo_unit "live" in that. At least for now.
12037
12038 The DWP file can be made up of a random collection of CUs and TUs.
12039 However, for each CU + set of TUs that came from the same original DWO
12040 file, we can combine them back into a virtual DWO file to save space
12041 (fewer struct dwo_file objects to allocate). Remember that for really
12042 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12043
12044 std::string virtual_dwo_name =
12045 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12046 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12047 (long) (sections.line_size ? sections.line_offset : 0),
12048 (long) (sections.loc_size ? sections.loc_offset : 0),
12049 (long) (sections.str_offsets_size
12050 ? sections.str_offsets_offset : 0));
12051 /* Can we use an existing virtual DWO file? */
12052 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
12053 comp_dir);
12054 /* Create one if necessary. */
12055 if (*dwo_file_slot == NULL)
12056 {
12057 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12058 virtual_dwo_name.c_str ());
12059
12060 dwo_file = new struct dwo_file;
12061 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12062 dwo_file->comp_dir = comp_dir;
12063 dwo_file->sections.abbrev =
12064 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12065 sections.abbrev_offset,
12066 sections.abbrev_size);
12067 dwo_file->sections.line =
12068 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12069 sections.line_offset,
12070 sections.line_size);
12071 dwo_file->sections.loc =
12072 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12073 sections.loc_offset, sections.loc_size);
12074 dwo_file->sections.macinfo =
12075 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12076 sections.macinfo_offset,
12077 sections.macinfo_size);
12078 dwo_file->sections.macro =
12079 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12080 sections.macro_offset,
12081 sections.macro_size);
12082 dwo_file->sections.str_offsets =
12083 create_dwp_v2_or_v5_section (per_objfile,
12084 &dwp_file->sections.str_offsets,
12085 sections.str_offsets_offset,
12086 sections.str_offsets_size);
12087 /* The "str" section is global to the entire DWP file. */
12088 dwo_file->sections.str = dwp_file->sections.str;
12089 /* The info or types section is assigned below to dwo_unit,
12090 there's no need to record it in dwo_file.
12091 Also, we can't simply record type sections in dwo_file because
12092 we record a pointer into the vector in dwo_unit. As we collect more
12093 types we'll grow the vector and eventually have to reallocate space
12094 for it, invalidating all copies of pointers into the previous
12095 contents. */
12096 *dwo_file_slot = dwo_file;
12097 }
12098 else
12099 {
12100 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12101 virtual_dwo_name.c_str ());
12102
12103 dwo_file = (struct dwo_file *) *dwo_file_slot;
12104 }
12105
12106 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12107 dwo_unit->dwo_file = dwo_file;
12108 dwo_unit->signature = signature;
12109 dwo_unit->section =
12110 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12111 *dwo_unit->section = create_dwp_v2_or_v5_section
12112 (per_objfile,
12113 is_debug_types
12114 ? &dwp_file->sections.types
12115 : &dwp_file->sections.info,
12116 sections.info_or_types_offset,
12117 sections.info_or_types_size);
12118 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12119
12120 return dwo_unit;
12121 }
12122
12123 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12124 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12125 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12126 This is for DWP version 5 files. */
12127
12128 static struct dwo_unit *
12129 create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12130 struct dwp_file *dwp_file,
12131 uint32_t unit_index,
12132 const char *comp_dir,
12133 ULONGEST signature, int is_debug_types)
12134 {
12135 const struct dwp_hash_table *dwp_htab
12136 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12137 bfd *dbfd = dwp_file->dbfd.get ();
12138 const char *kind = is_debug_types ? "TU" : "CU";
12139 struct dwo_file *dwo_file;
12140 struct dwo_unit *dwo_unit;
12141 struct virtual_v2_or_v5_dwo_sections sections {};
12142 void **dwo_file_slot;
12143
12144 gdb_assert (dwp_file->version == 5);
12145
12146 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12147 kind, pulongest (unit_index), hex_string (signature),
12148 dwp_file->name);
12149
12150 /* Fetch the section offsets of this DWO unit. */
12151
12152 /* memset (&sections, 0, sizeof (sections)); */
12153
12154 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12155 {
12156 uint32_t offset = read_4_bytes (dbfd,
12157 dwp_htab->section_pool.v5.offsets
12158 + (((unit_index - 1)
12159 * dwp_htab->nr_columns
12160 + i)
12161 * sizeof (uint32_t)));
12162 uint32_t size = read_4_bytes (dbfd,
12163 dwp_htab->section_pool.v5.sizes
12164 + (((unit_index - 1) * dwp_htab->nr_columns
12165 + i)
12166 * sizeof (uint32_t)));
12167
12168 switch (dwp_htab->section_pool.v5.section_ids[i])
12169 {
12170 case DW_SECT_ABBREV_V5:
12171 sections.abbrev_offset = offset;
12172 sections.abbrev_size = size;
12173 break;
12174 case DW_SECT_INFO_V5:
12175 sections.info_or_types_offset = offset;
12176 sections.info_or_types_size = size;
12177 break;
12178 case DW_SECT_LINE_V5:
12179 sections.line_offset = offset;
12180 sections.line_size = size;
12181 break;
12182 case DW_SECT_LOCLISTS_V5:
12183 sections.loclists_offset = offset;
12184 sections.loclists_size = size;
12185 break;
12186 case DW_SECT_MACRO_V5:
12187 sections.macro_offset = offset;
12188 sections.macro_size = size;
12189 break;
12190 case DW_SECT_RNGLISTS_V5:
12191 sections.rnglists_offset = offset;
12192 sections.rnglists_size = size;
12193 break;
12194 case DW_SECT_STR_OFFSETS_V5:
12195 sections.str_offsets_offset = offset;
12196 sections.str_offsets_size = size;
12197 break;
12198 case DW_SECT_RESERVED_V5:
12199 default:
12200 break;
12201 }
12202 }
12203
12204 /* It's easier for the rest of the code if we fake a struct dwo_file and
12205 have dwo_unit "live" in that. At least for now.
12206
12207 The DWP file can be made up of a random collection of CUs and TUs.
12208 However, for each CU + set of TUs that came from the same original DWO
12209 file, we can combine them back into a virtual DWO file to save space
12210 (fewer struct dwo_file objects to allocate). Remember that for really
12211 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12212
12213 std::string virtual_dwo_name =
12214 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
12215 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12216 (long) (sections.line_size ? sections.line_offset : 0),
12217 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12218 (long) (sections.str_offsets_size
12219 ? sections.str_offsets_offset : 0),
12220 (long) (sections.macro_size ? sections.macro_offset : 0),
12221 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
12222 /* Can we use an existing virtual DWO file? */
12223 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
12224 virtual_dwo_name.c_str (),
12225 comp_dir);
12226 /* Create one if necessary. */
12227 if (*dwo_file_slot == NULL)
12228 {
12229 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12230 virtual_dwo_name.c_str ());
12231
12232 dwo_file = new struct dwo_file;
12233 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12234 dwo_file->comp_dir = comp_dir;
12235 dwo_file->sections.abbrev =
12236 create_dwp_v2_or_v5_section (per_objfile,
12237 &dwp_file->sections.abbrev,
12238 sections.abbrev_offset,
12239 sections.abbrev_size);
12240 dwo_file->sections.line =
12241 create_dwp_v2_or_v5_section (per_objfile,
12242 &dwp_file->sections.line,
12243 sections.line_offset, sections.line_size);
12244 dwo_file->sections.macro =
12245 create_dwp_v2_or_v5_section (per_objfile,
12246 &dwp_file->sections.macro,
12247 sections.macro_offset,
12248 sections.macro_size);
12249 dwo_file->sections.loclists =
12250 create_dwp_v2_or_v5_section (per_objfile,
12251 &dwp_file->sections.loclists,
12252 sections.loclists_offset,
12253 sections.loclists_size);
12254 dwo_file->sections.rnglists =
12255 create_dwp_v2_or_v5_section (per_objfile,
12256 &dwp_file->sections.rnglists,
12257 sections.rnglists_offset,
12258 sections.rnglists_size);
12259 dwo_file->sections.str_offsets =
12260 create_dwp_v2_or_v5_section (per_objfile,
12261 &dwp_file->sections.str_offsets,
12262 sections.str_offsets_offset,
12263 sections.str_offsets_size);
12264 /* The "str" section is global to the entire DWP file. */
12265 dwo_file->sections.str = dwp_file->sections.str;
12266 /* The info or types section is assigned below to dwo_unit,
12267 there's no need to record it in dwo_file.
12268 Also, we can't simply record type sections in dwo_file because
12269 we record a pointer into the vector in dwo_unit. As we collect more
12270 types we'll grow the vector and eventually have to reallocate space
12271 for it, invalidating all copies of pointers into the previous
12272 contents. */
12273 *dwo_file_slot = dwo_file;
12274 }
12275 else
12276 {
12277 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12278 virtual_dwo_name.c_str ());
12279
12280 dwo_file = (struct dwo_file *) *dwo_file_slot;
12281 }
12282
12283 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12284 dwo_unit->dwo_file = dwo_file;
12285 dwo_unit->signature = signature;
12286 dwo_unit->section
12287 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12288 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
12289 &dwp_file->sections.info,
12290 sections.info_or_types_offset,
12291 sections.info_or_types_size);
12292 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12293
12294 return dwo_unit;
12295 }
12296
12297 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12298 Returns NULL if the signature isn't found. */
12299
12300 static struct dwo_unit *
12301 lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
12302 struct dwp_file *dwp_file, const char *comp_dir,
12303 ULONGEST signature, int is_debug_types)
12304 {
12305 const struct dwp_hash_table *dwp_htab =
12306 is_debug_types ? dwp_file->tus : dwp_file->cus;
12307 bfd *dbfd = dwp_file->dbfd.get ();
12308 uint32_t mask = dwp_htab->nr_slots - 1;
12309 uint32_t hash = signature & mask;
12310 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12311 unsigned int i;
12312 void **slot;
12313 struct dwo_unit find_dwo_cu;
12314
12315 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12316 find_dwo_cu.signature = signature;
12317 slot = htab_find_slot (is_debug_types
12318 ? dwp_file->loaded_tus.get ()
12319 : dwp_file->loaded_cus.get (),
12320 &find_dwo_cu, INSERT);
12321
12322 if (*slot != NULL)
12323 return (struct dwo_unit *) *slot;
12324
12325 /* Use a for loop so that we don't loop forever on bad debug info. */
12326 for (i = 0; i < dwp_htab->nr_slots; ++i)
12327 {
12328 ULONGEST signature_in_table;
12329
12330 signature_in_table =
12331 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
12332 if (signature_in_table == signature)
12333 {
12334 uint32_t unit_index =
12335 read_4_bytes (dbfd,
12336 dwp_htab->unit_table + hash * sizeof (uint32_t));
12337
12338 if (dwp_file->version == 1)
12339 {
12340 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12341 unit_index, comp_dir,
12342 signature, is_debug_types);
12343 }
12344 else if (dwp_file->version == 2)
12345 {
12346 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12347 unit_index, comp_dir,
12348 signature, is_debug_types);
12349 }
12350 else /* version == 5 */
12351 {
12352 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12353 unit_index, comp_dir,
12354 signature, is_debug_types);
12355 }
12356 return (struct dwo_unit *) *slot;
12357 }
12358 if (signature_in_table == 0)
12359 return NULL;
12360 hash = (hash + hash2) & mask;
12361 }
12362
12363 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12364 " [in module %s]"),
12365 dwp_file->name);
12366 }
12367
12368 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
12369 Open the file specified by FILE_NAME and hand it off to BFD for
12370 preliminary analysis. Return a newly initialized bfd *, which
12371 includes a canonicalized copy of FILE_NAME.
12372 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
12373 SEARCH_CWD is true if the current directory is to be searched.
12374 It will be searched before debug-file-directory.
12375 If successful, the file is added to the bfd include table of the
12376 objfile's bfd (see gdb_bfd_record_inclusion).
12377 If unable to find/open the file, return NULL.
12378 NOTE: This function is derived from symfile_bfd_open. */
12379
12380 static gdb_bfd_ref_ptr
12381 try_open_dwop_file (dwarf2_per_objfile *per_objfile,
12382 const char *file_name, int is_dwp, int search_cwd)
12383 {
12384 int desc;
12385 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12386 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12387 to debug_file_directory. */
12388 const char *search_path;
12389 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12390
12391 gdb::unique_xmalloc_ptr<char> search_path_holder;
12392 if (search_cwd)
12393 {
12394 if (*debug_file_directory != '\0')
12395 {
12396 search_path_holder.reset (concat (".", dirname_separator_string,
12397 debug_file_directory,
12398 (char *) NULL));
12399 search_path = search_path_holder.get ();
12400 }
12401 else
12402 search_path = ".";
12403 }
12404 else
12405 search_path = debug_file_directory;
12406
12407 /* Add the path for the executable binary to the list of search paths. */
12408 std::string objfile_dir = ldirname (objfile_name (per_objfile->objfile));
12409 search_path_holder.reset (concat (objfile_dir.c_str (),
12410 dirname_separator_string,
12411 search_path, nullptr));
12412 search_path = search_path_holder.get ();
12413
12414 openp_flags flags = OPF_RETURN_REALPATH;
12415 if (is_dwp)
12416 flags |= OPF_SEARCH_IN_PATH;
12417
12418 gdb::unique_xmalloc_ptr<char> absolute_name;
12419 desc = openp (search_path, flags, file_name,
12420 O_RDONLY | O_BINARY, &absolute_name);
12421 if (desc < 0)
12422 return NULL;
12423
12424 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12425 gnutarget, desc));
12426 if (sym_bfd == NULL)
12427 return NULL;
12428 bfd_set_cacheable (sym_bfd.get (), 1);
12429
12430 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12431 return NULL;
12432
12433 /* Success. Record the bfd as having been included by the objfile's bfd.
12434 This is important because things like demangled_names_hash lives in the
12435 objfile's per_bfd space and may have references to things like symbol
12436 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
12437 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
12438
12439 return sym_bfd;
12440 }
12441
12442 /* Try to open DWO file FILE_NAME.
12443 COMP_DIR is the DW_AT_comp_dir attribute.
12444 The result is the bfd handle of the file.
12445 If there is a problem finding or opening the file, return NULL.
12446 Upon success, the canonicalized path of the file is stored in the bfd,
12447 same as symfile_bfd_open. */
12448
12449 static gdb_bfd_ref_ptr
12450 open_dwo_file (dwarf2_per_objfile *per_objfile,
12451 const char *file_name, const char *comp_dir)
12452 {
12453 if (IS_ABSOLUTE_PATH (file_name))
12454 return try_open_dwop_file (per_objfile, file_name,
12455 0 /*is_dwp*/, 0 /*search_cwd*/);
12456
12457 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12458
12459 if (comp_dir != NULL)
12460 {
12461 gdb::unique_xmalloc_ptr<char> path_to_try
12462 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
12463
12464 /* NOTE: If comp_dir is a relative path, this will also try the
12465 search path, which seems useful. */
12466 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
12467 0 /*is_dwp*/,
12468 1 /*search_cwd*/));
12469 if (abfd != NULL)
12470 return abfd;
12471 }
12472
12473 /* That didn't work, try debug-file-directory, which, despite its name,
12474 is a list of paths. */
12475
12476 if (*debug_file_directory == '\0')
12477 return NULL;
12478
12479 return try_open_dwop_file (per_objfile, file_name,
12480 0 /*is_dwp*/, 1 /*search_cwd*/);
12481 }
12482
12483 /* This function is mapped across the sections and remembers the offset and
12484 size of each of the DWO debugging sections we are interested in. */
12485
12486 static void
12487 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12488 dwo_sections *dwo_sections)
12489 {
12490 const struct dwop_section_names *names = &dwop_section_names;
12491
12492 if (names->abbrev_dwo.matches (sectp->name))
12493 {
12494 dwo_sections->abbrev.s.section = sectp;
12495 dwo_sections->abbrev.size = bfd_section_size (sectp);
12496 }
12497 else if (names->info_dwo.matches (sectp->name))
12498 {
12499 dwo_sections->info.s.section = sectp;
12500 dwo_sections->info.size = bfd_section_size (sectp);
12501 }
12502 else if (names->line_dwo.matches (sectp->name))
12503 {
12504 dwo_sections->line.s.section = sectp;
12505 dwo_sections->line.size = bfd_section_size (sectp);
12506 }
12507 else if (names->loc_dwo.matches (sectp->name))
12508 {
12509 dwo_sections->loc.s.section = sectp;
12510 dwo_sections->loc.size = bfd_section_size (sectp);
12511 }
12512 else if (names->loclists_dwo.matches (sectp->name))
12513 {
12514 dwo_sections->loclists.s.section = sectp;
12515 dwo_sections->loclists.size = bfd_section_size (sectp);
12516 }
12517 else if (names->macinfo_dwo.matches (sectp->name))
12518 {
12519 dwo_sections->macinfo.s.section = sectp;
12520 dwo_sections->macinfo.size = bfd_section_size (sectp);
12521 }
12522 else if (names->macro_dwo.matches (sectp->name))
12523 {
12524 dwo_sections->macro.s.section = sectp;
12525 dwo_sections->macro.size = bfd_section_size (sectp);
12526 }
12527 else if (names->rnglists_dwo.matches (sectp->name))
12528 {
12529 dwo_sections->rnglists.s.section = sectp;
12530 dwo_sections->rnglists.size = bfd_section_size (sectp);
12531 }
12532 else if (names->str_dwo.matches (sectp->name))
12533 {
12534 dwo_sections->str.s.section = sectp;
12535 dwo_sections->str.size = bfd_section_size (sectp);
12536 }
12537 else if (names->str_offsets_dwo.matches (sectp->name))
12538 {
12539 dwo_sections->str_offsets.s.section = sectp;
12540 dwo_sections->str_offsets.size = bfd_section_size (sectp);
12541 }
12542 else if (names->types_dwo.matches (sectp->name))
12543 {
12544 struct dwarf2_section_info type_section;
12545
12546 memset (&type_section, 0, sizeof (type_section));
12547 type_section.s.section = sectp;
12548 type_section.size = bfd_section_size (sectp);
12549 dwo_sections->types.push_back (type_section);
12550 }
12551 }
12552
12553 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
12554 by PER_CU. This is for the non-DWP case.
12555 The result is NULL if DWO_NAME can't be found. */
12556
12557 static struct dwo_file *
12558 open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12559 const char *comp_dir)
12560 {
12561 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12562
12563 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
12564 if (dbfd == NULL)
12565 {
12566 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12567
12568 return NULL;
12569 }
12570
12571 dwo_file_up dwo_file (new struct dwo_file);
12572 dwo_file->dwo_name = dwo_name;
12573 dwo_file->comp_dir = comp_dir;
12574 dwo_file->dbfd = std::move (dbfd);
12575
12576 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12577 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12578 &dwo_file->sections);
12579
12580 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12581 dwo_file->cus);
12582
12583 if (cu->per_cu->dwarf_version < 5)
12584 {
12585 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12586 dwo_file->sections.types, dwo_file->tus);
12587 }
12588 else
12589 {
12590 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12591 &dwo_file->sections.info, dwo_file->tus,
12592 rcuh_kind::COMPILE);
12593 }
12594
12595 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
12596
12597 return dwo_file.release ();
12598 }
12599
12600 /* This function is mapped across the sections and remembers the offset and
12601 size of each of the DWP debugging sections common to version 1 and 2 that
12602 we are interested in. */
12603
12604 static void
12605 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12606 dwp_file *dwp_file)
12607 {
12608 const struct dwop_section_names *names = &dwop_section_names;
12609 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12610
12611 /* Record the ELF section number for later lookup: this is what the
12612 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12613 gdb_assert (elf_section_nr < dwp_file->num_sections);
12614 dwp_file->elf_sections[elf_section_nr] = sectp;
12615
12616 /* Look for specific sections that we need. */
12617 if (names->str_dwo.matches (sectp->name))
12618 {
12619 dwp_file->sections.str.s.section = sectp;
12620 dwp_file->sections.str.size = bfd_section_size (sectp);
12621 }
12622 else if (names->cu_index.matches (sectp->name))
12623 {
12624 dwp_file->sections.cu_index.s.section = sectp;
12625 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
12626 }
12627 else if (names->tu_index.matches (sectp->name))
12628 {
12629 dwp_file->sections.tu_index.s.section = sectp;
12630 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
12631 }
12632 }
12633
12634 /* This function is mapped across the sections and remembers the offset and
12635 size of each of the DWP version 2 debugging sections that we are interested
12636 in. This is split into a separate function because we don't know if we
12637 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12638
12639 static void
12640 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12641 {
12642 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12643 const struct dwop_section_names *names = &dwop_section_names;
12644 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12645
12646 /* Record the ELF section number for later lookup: this is what the
12647 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12648 gdb_assert (elf_section_nr < dwp_file->num_sections);
12649 dwp_file->elf_sections[elf_section_nr] = sectp;
12650
12651 /* Look for specific sections that we need. */
12652 if (names->abbrev_dwo.matches (sectp->name))
12653 {
12654 dwp_file->sections.abbrev.s.section = sectp;
12655 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12656 }
12657 else if (names->info_dwo.matches (sectp->name))
12658 {
12659 dwp_file->sections.info.s.section = sectp;
12660 dwp_file->sections.info.size = bfd_section_size (sectp);
12661 }
12662 else if (names->line_dwo.matches (sectp->name))
12663 {
12664 dwp_file->sections.line.s.section = sectp;
12665 dwp_file->sections.line.size = bfd_section_size (sectp);
12666 }
12667 else if (names->loc_dwo.matches (sectp->name))
12668 {
12669 dwp_file->sections.loc.s.section = sectp;
12670 dwp_file->sections.loc.size = bfd_section_size (sectp);
12671 }
12672 else if (names->macinfo_dwo.matches (sectp->name))
12673 {
12674 dwp_file->sections.macinfo.s.section = sectp;
12675 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
12676 }
12677 else if (names->macro_dwo.matches (sectp->name))
12678 {
12679 dwp_file->sections.macro.s.section = sectp;
12680 dwp_file->sections.macro.size = bfd_section_size (sectp);
12681 }
12682 else if (names->str_offsets_dwo.matches (sectp->name))
12683 {
12684 dwp_file->sections.str_offsets.s.section = sectp;
12685 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12686 }
12687 else if (names->types_dwo.matches (sectp->name))
12688 {
12689 dwp_file->sections.types.s.section = sectp;
12690 dwp_file->sections.types.size = bfd_section_size (sectp);
12691 }
12692 }
12693
12694 /* This function is mapped across the sections and remembers the offset and
12695 size of each of the DWP version 5 debugging sections that we are interested
12696 in. This is split into a separate function because we don't know if we
12697 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
12698
12699 static void
12700 dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12701 {
12702 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
12703 const struct dwop_section_names *names = &dwop_section_names;
12704 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12705
12706 /* Record the ELF section number for later lookup: this is what the
12707 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12708 gdb_assert (elf_section_nr < dwp_file->num_sections);
12709 dwp_file->elf_sections[elf_section_nr] = sectp;
12710
12711 /* Look for specific sections that we need. */
12712 if (names->abbrev_dwo.matches (sectp->name))
12713 {
12714 dwp_file->sections.abbrev.s.section = sectp;
12715 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
12716 }
12717 else if (names->info_dwo.matches (sectp->name))
12718 {
12719 dwp_file->sections.info.s.section = sectp;
12720 dwp_file->sections.info.size = bfd_section_size (sectp);
12721 }
12722 else if (names->line_dwo.matches (sectp->name))
12723 {
12724 dwp_file->sections.line.s.section = sectp;
12725 dwp_file->sections.line.size = bfd_section_size (sectp);
12726 }
12727 else if (names->loclists_dwo.matches (sectp->name))
12728 {
12729 dwp_file->sections.loclists.s.section = sectp;
12730 dwp_file->sections.loclists.size = bfd_section_size (sectp);
12731 }
12732 else if (names->macro_dwo.matches (sectp->name))
12733 {
12734 dwp_file->sections.macro.s.section = sectp;
12735 dwp_file->sections.macro.size = bfd_section_size (sectp);
12736 }
12737 else if (names->rnglists_dwo.matches (sectp->name))
12738 {
12739 dwp_file->sections.rnglists.s.section = sectp;
12740 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
12741 }
12742 else if (names->str_offsets_dwo.matches (sectp->name))
12743 {
12744 dwp_file->sections.str_offsets.s.section = sectp;
12745 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
12746 }
12747 }
12748
12749 /* Hash function for dwp_file loaded CUs/TUs. */
12750
12751 static hashval_t
12752 hash_dwp_loaded_cutus (const void *item)
12753 {
12754 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
12755
12756 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12757 return dwo_unit->signature;
12758 }
12759
12760 /* Equality function for dwp_file loaded CUs/TUs. */
12761
12762 static int
12763 eq_dwp_loaded_cutus (const void *a, const void *b)
12764 {
12765 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12766 const struct dwo_unit *dub = (const struct dwo_unit *) b;
12767
12768 return dua->signature == dub->signature;
12769 }
12770
12771 /* Allocate a hash table for dwp_file loaded CUs/TUs. */
12772
12773 static htab_up
12774 allocate_dwp_loaded_cutus_table ()
12775 {
12776 return htab_up (htab_create_alloc (3,
12777 hash_dwp_loaded_cutus,
12778 eq_dwp_loaded_cutus,
12779 NULL, xcalloc, xfree));
12780 }
12781
12782 /* Try to open DWP file FILE_NAME.
12783 The result is the bfd handle of the file.
12784 If there is a problem finding or opening the file, return NULL.
12785 Upon success, the canonicalized path of the file is stored in the bfd,
12786 same as symfile_bfd_open. */
12787
12788 static gdb_bfd_ref_ptr
12789 open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
12790 {
12791 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
12792 1 /*is_dwp*/,
12793 1 /*search_cwd*/));
12794 if (abfd != NULL)
12795 return abfd;
12796
12797 /* Work around upstream bug 15652.
12798 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12799 [Whether that's a "bug" is debatable, but it is getting in our way.]
12800 We have no real idea where the dwp file is, because gdb's realpath-ing
12801 of the executable's path may have discarded the needed info.
12802 [IWBN if the dwp file name was recorded in the executable, akin to
12803 .gnu_debuglink, but that doesn't exist yet.]
12804 Strip the directory from FILE_NAME and search again. */
12805 if (*debug_file_directory != '\0')
12806 {
12807 /* Don't implicitly search the current directory here.
12808 If the user wants to search "." to handle this case,
12809 it must be added to debug-file-directory. */
12810 return try_open_dwop_file (per_objfile, lbasename (file_name),
12811 1 /*is_dwp*/,
12812 0 /*search_cwd*/);
12813 }
12814
12815 return NULL;
12816 }
12817
12818 /* Initialize the use of the DWP file for the current objfile.
12819 By convention the name of the DWP file is ${objfile}.dwp.
12820 The result is NULL if it can't be found. */
12821
12822 static std::unique_ptr<struct dwp_file>
12823 open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
12824 {
12825 struct objfile *objfile = per_objfile->objfile;
12826
12827 /* Try to find first .dwp for the binary file before any symbolic links
12828 resolving. */
12829
12830 /* If the objfile is a debug file, find the name of the real binary
12831 file and get the name of dwp file from there. */
12832 std::string dwp_name;
12833 if (objfile->separate_debug_objfile_backlink != NULL)
12834 {
12835 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12836 const char *backlink_basename = lbasename (backlink->original_name);
12837
12838 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
12839 }
12840 else
12841 dwp_name = objfile->original_name;
12842
12843 dwp_name += ".dwp";
12844
12845 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
12846 if (dbfd == NULL
12847 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12848 {
12849 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
12850 dwp_name = objfile_name (objfile);
12851 dwp_name += ".dwp";
12852 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
12853 }
12854
12855 if (dbfd == NULL)
12856 {
12857 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
12858
12859 return std::unique_ptr<dwp_file> ();
12860 }
12861
12862 const char *name = bfd_get_filename (dbfd.get ());
12863 std::unique_ptr<struct dwp_file> dwp_file
12864 (new struct dwp_file (name, std::move (dbfd)));
12865
12866 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
12867 dwp_file->elf_sections =
12868 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
12869 dwp_file->num_sections, asection *);
12870
12871 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12872 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
12873 dwp_file.get ());
12874
12875 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
12876
12877 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
12878
12879 /* The DWP file version is stored in the hash table. Oh well. */
12880 if (dwp_file->cus && dwp_file->tus
12881 && dwp_file->cus->version != dwp_file->tus->version)
12882 {
12883 /* Technically speaking, we should try to limp along, but this is
12884 pretty bizarre. We use pulongest here because that's the established
12885 portability solution (e.g, we cannot use %u for uint32_t). */
12886 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12887 " TU version %s [in DWP file %s]"),
12888 pulongest (dwp_file->cus->version),
12889 pulongest (dwp_file->tus->version), dwp_name.c_str ());
12890 }
12891
12892 if (dwp_file->cus)
12893 dwp_file->version = dwp_file->cus->version;
12894 else if (dwp_file->tus)
12895 dwp_file->version = dwp_file->tus->version;
12896 else
12897 dwp_file->version = 2;
12898
12899 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
12900 {
12901 if (dwp_file->version == 2)
12902 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
12903 dwp_file.get ());
12904 else
12905 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
12906 dwp_file.get ());
12907 }
12908
12909 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12910 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
12911
12912 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
12913 dwarf_read_debug_printf (" %s CUs, %s TUs",
12914 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12915 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
12916
12917 return dwp_file;
12918 }
12919
12920 /* Wrapper around open_and_init_dwp_file, only open it once. */
12921
12922 static struct dwp_file *
12923 get_dwp_file (dwarf2_per_objfile *per_objfile)
12924 {
12925 if (!per_objfile->per_bfd->dwp_checked)
12926 {
12927 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
12928 per_objfile->per_bfd->dwp_checked = 1;
12929 }
12930 return per_objfile->per_bfd->dwp_file.get ();
12931 }
12932
12933 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12934 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12935 or in the DWP file for the objfile, referenced by THIS_UNIT.
12936 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
12937 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12938
12939 This is called, for example, when wanting to read a variable with a
12940 complex location. Therefore we don't want to do file i/o for every call.
12941 Therefore we don't want to look for a DWO file on every call.
12942 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12943 then we check if we've already seen DWO_NAME, and only THEN do we check
12944 for a DWO file.
12945
12946 The result is a pointer to the dwo_unit object or NULL if we didn't find it
12947 (dwo_id mismatch or couldn't find the DWO/DWP file). */
12948
12949 static struct dwo_unit *
12950 lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
12951 ULONGEST signature, int is_debug_types)
12952 {
12953 dwarf2_per_objfile *per_objfile = cu->per_objfile;
12954 struct objfile *objfile = per_objfile->objfile;
12955 const char *kind = is_debug_types ? "TU" : "CU";
12956 void **dwo_file_slot;
12957 struct dwo_file *dwo_file;
12958 struct dwp_file *dwp_file;
12959
12960 /* First see if there's a DWP file.
12961 If we have a DWP file but didn't find the DWO inside it, don't
12962 look for the original DWO file. It makes gdb behave differently
12963 depending on whether one is debugging in the build tree. */
12964
12965 dwp_file = get_dwp_file (per_objfile);
12966 if (dwp_file != NULL)
12967 {
12968 const struct dwp_hash_table *dwp_htab =
12969 is_debug_types ? dwp_file->tus : dwp_file->cus;
12970
12971 if (dwp_htab != NULL)
12972 {
12973 struct dwo_unit *dwo_cutu =
12974 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
12975 is_debug_types);
12976
12977 if (dwo_cutu != NULL)
12978 {
12979 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
12980 kind, hex_string (signature),
12981 host_address_to_string (dwo_cutu));
12982
12983 return dwo_cutu;
12984 }
12985 }
12986 }
12987 else
12988 {
12989 /* No DWP file, look for the DWO file. */
12990
12991 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
12992 if (*dwo_file_slot == NULL)
12993 {
12994 /* Read in the file and build a table of the CUs/TUs it contains. */
12995 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
12996 }
12997 /* NOTE: This will be NULL if unable to open the file. */
12998 dwo_file = (struct dwo_file *) *dwo_file_slot;
12999
13000 if (dwo_file != NULL)
13001 {
13002 struct dwo_unit *dwo_cutu = NULL;
13003
13004 if (is_debug_types && dwo_file->tus)
13005 {
13006 struct dwo_unit find_dwo_cutu;
13007
13008 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13009 find_dwo_cutu.signature = signature;
13010 dwo_cutu
13011 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13012 &find_dwo_cutu);
13013 }
13014 else if (!is_debug_types && dwo_file->cus)
13015 {
13016 struct dwo_unit find_dwo_cutu;
13017
13018 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13019 find_dwo_cutu.signature = signature;
13020 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
13021 &find_dwo_cutu);
13022 }
13023
13024 if (dwo_cutu != NULL)
13025 {
13026 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13027 kind, dwo_name, hex_string (signature),
13028 host_address_to_string (dwo_cutu));
13029
13030 return dwo_cutu;
13031 }
13032 }
13033 }
13034
13035 /* We didn't find it. This could mean a dwo_id mismatch, or
13036 someone deleted the DWO/DWP file, or the search path isn't set up
13037 correctly to find the file. */
13038
13039 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13040 kind, dwo_name, hex_string (signature));
13041
13042 /* This is a warning and not a complaint because it can be caused by
13043 pilot error (e.g., user accidentally deleting the DWO). */
13044 {
13045 /* Print the name of the DWP file if we looked there, helps the user
13046 better diagnose the problem. */
13047 std::string dwp_text;
13048
13049 if (dwp_file != NULL)
13050 dwp_text = string_printf (" [in DWP file %s]",
13051 lbasename (dwp_file->name));
13052
13053 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
13054 " [in module %s]"),
13055 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13056 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
13057 }
13058 return NULL;
13059 }
13060
13061 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13062 See lookup_dwo_cutu_unit for details. */
13063
13064 static struct dwo_unit *
13065 lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
13066 ULONGEST signature)
13067 {
13068 gdb_assert (!cu->per_cu->is_debug_types);
13069
13070 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
13071 }
13072
13073 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13074 See lookup_dwo_cutu_unit for details. */
13075
13076 static struct dwo_unit *
13077 lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
13078 {
13079 gdb_assert (cu->per_cu->is_debug_types);
13080
13081 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13082
13083 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
13084 }
13085
13086 /* Traversal function for queue_and_load_all_dwo_tus. */
13087
13088 static int
13089 queue_and_load_dwo_tu (void **slot, void *info)
13090 {
13091 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13092 dwarf2_cu *cu = (dwarf2_cu *) info;
13093 ULONGEST signature = dwo_unit->signature;
13094 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
13095
13096 if (sig_type != NULL)
13097 {
13098 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13099
13100 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13101 a real dependency of PER_CU on SIG_TYPE. That is detected later
13102 while processing PER_CU. */
13103 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
13104 load_full_type_unit (sig_cu, cu->per_objfile);
13105 cu->per_cu->imported_symtabs_push (sig_cu);
13106 }
13107
13108 return 1;
13109 }
13110
13111 /* Queue all TUs contained in the DWO of CU to be read in.
13112 The DWO may have the only definition of the type, though it may not be
13113 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13114 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13115
13116 static void
13117 queue_and_load_all_dwo_tus (dwarf2_cu *cu)
13118 {
13119 struct dwo_unit *dwo_unit;
13120 struct dwo_file *dwo_file;
13121
13122 gdb_assert (cu != nullptr);
13123 gdb_assert (!cu->per_cu->is_debug_types);
13124 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
13125
13126 dwo_unit = cu->dwo_unit;
13127 gdb_assert (dwo_unit != NULL);
13128
13129 dwo_file = dwo_unit->dwo_file;
13130 if (dwo_file->tus != NULL)
13131 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
13132 }
13133
13134 /* Read in various DIEs. */
13135
13136 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
13137 Inherit only the children of the DW_AT_abstract_origin DIE not being
13138 already referenced by DW_AT_abstract_origin from the children of the
13139 current DIE. */
13140
13141 static void
13142 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13143 {
13144 struct die_info *child_die;
13145 sect_offset *offsetp;
13146 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13147 struct die_info *origin_die;
13148 /* Iterator of the ORIGIN_DIE children. */
13149 struct die_info *origin_child_die;
13150 struct attribute *attr;
13151 struct dwarf2_cu *origin_cu;
13152 struct pending **origin_previous_list_in_scope;
13153
13154 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13155 if (!attr)
13156 return;
13157
13158 /* Note that following die references may follow to a die in a
13159 different cu. */
13160
13161 origin_cu = cu;
13162 origin_die = follow_die_ref (die, attr, &origin_cu);
13163
13164 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13165 symbols in. */
13166 origin_previous_list_in_scope = origin_cu->list_in_scope;
13167 origin_cu->list_in_scope = cu->list_in_scope;
13168
13169 if (die->tag != origin_die->tag
13170 && !(die->tag == DW_TAG_inlined_subroutine
13171 && origin_die->tag == DW_TAG_subprogram))
13172 complaint (_("DIE %s and its abstract origin %s have different tags"),
13173 sect_offset_str (die->sect_off),
13174 sect_offset_str (origin_die->sect_off));
13175
13176 /* Find if the concrete and abstract trees are structurally the
13177 same. This is a shallow traversal and it is not bullet-proof;
13178 the compiler can trick the debugger into believing that the trees
13179 are isomorphic, whereas they actually are not. However, the
13180 likelyhood of this happening is pretty low, and a full-fledged
13181 check would be an overkill. */
13182 bool are_isomorphic = true;
13183 die_info *concrete_child = die->child;
13184 die_info *abstract_child = origin_die->child;
13185 while (concrete_child != nullptr || abstract_child != nullptr)
13186 {
13187 if (concrete_child == nullptr
13188 || abstract_child == nullptr
13189 || concrete_child->tag != abstract_child->tag)
13190 {
13191 are_isomorphic = false;
13192 break;
13193 }
13194
13195 concrete_child = concrete_child->sibling;
13196 abstract_child = abstract_child->sibling;
13197 }
13198
13199 /* Walk the origin's children in parallel to the concrete children.
13200 This helps match an origin child in case the debug info misses
13201 DW_AT_abstract_origin attributes. Keep in mind that the abstract
13202 origin tree may not have the same tree structure as the concrete
13203 DIE, though. */
13204 die_info *corresponding_abstract_child
13205 = are_isomorphic ? origin_die->child : nullptr;
13206
13207 std::vector<sect_offset> offsets;
13208
13209 for (child_die = die->child;
13210 child_die && child_die->tag;
13211 child_die = child_die->sibling)
13212 {
13213 struct die_info *child_origin_die;
13214 struct dwarf2_cu *child_origin_cu;
13215
13216 /* We are trying to process concrete instance entries:
13217 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
13218 it's not relevant to our analysis here. i.e. detecting DIEs that are
13219 present in the abstract instance but not referenced in the concrete
13220 one. */
13221 if (child_die->tag == DW_TAG_call_site
13222 || child_die->tag == DW_TAG_GNU_call_site)
13223 {
13224 if (are_isomorphic)
13225 corresponding_abstract_child
13226 = corresponding_abstract_child->sibling;
13227 continue;
13228 }
13229
13230 /* For each CHILD_DIE, find the corresponding child of
13231 ORIGIN_DIE. If there is more than one layer of
13232 DW_AT_abstract_origin, follow them all; there shouldn't be,
13233 but GCC versions at least through 4.4 generate this (GCC PR
13234 40573). */
13235 child_origin_die = child_die;
13236 child_origin_cu = cu;
13237 while (1)
13238 {
13239 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13240 child_origin_cu);
13241 if (attr == NULL)
13242 break;
13243 child_origin_die = follow_die_ref (child_origin_die, attr,
13244 &child_origin_cu);
13245 }
13246
13247 /* If missing DW_AT_abstract_origin, try the corresponding child
13248 of the origin. Clang emits such lexical scopes. */
13249 if (child_origin_die == child_die
13250 && dwarf2_attr (child_die, DW_AT_abstract_origin, cu) == nullptr
13251 && are_isomorphic
13252 && child_die->tag == DW_TAG_lexical_block)
13253 child_origin_die = corresponding_abstract_child;
13254
13255 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13256 counterpart may exist. */
13257 if (child_origin_die != child_die)
13258 {
13259 if (child_die->tag != child_origin_die->tag
13260 && !(child_die->tag == DW_TAG_inlined_subroutine
13261 && child_origin_die->tag == DW_TAG_subprogram))
13262 complaint (_("Child DIE %s and its abstract origin %s have "
13263 "different tags"),
13264 sect_offset_str (child_die->sect_off),
13265 sect_offset_str (child_origin_die->sect_off));
13266 if (child_origin_die->parent != origin_die)
13267 complaint (_("Child DIE %s and its abstract origin %s have "
13268 "different parents"),
13269 sect_offset_str (child_die->sect_off),
13270 sect_offset_str (child_origin_die->sect_off));
13271 else
13272 offsets.push_back (child_origin_die->sect_off);
13273 }
13274
13275 if (are_isomorphic)
13276 corresponding_abstract_child = corresponding_abstract_child->sibling;
13277 }
13278 std::sort (offsets.begin (), offsets.end ());
13279 sect_offset *offsets_end = offsets.data () + offsets.size ();
13280 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
13281 if (offsetp[-1] == *offsetp)
13282 complaint (_("Multiple children of DIE %s refer "
13283 "to DIE %s as their abstract origin"),
13284 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
13285
13286 offsetp = offsets.data ();
13287 origin_child_die = origin_die->child;
13288 while (origin_child_die && origin_child_die->tag)
13289 {
13290 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
13291 while (offsetp < offsets_end
13292 && *offsetp < origin_child_die->sect_off)
13293 offsetp++;
13294 if (offsetp >= offsets_end
13295 || *offsetp > origin_child_die->sect_off)
13296 {
13297 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13298 Check whether we're already processing ORIGIN_CHILD_DIE.
13299 This can happen with mutually referenced abstract_origins.
13300 PR 16581. */
13301 if (!origin_child_die->in_process)
13302 process_die (origin_child_die, origin_cu);
13303 }
13304 origin_child_die = origin_child_die->sibling;
13305 }
13306 origin_cu->list_in_scope = origin_previous_list_in_scope;
13307
13308 if (cu != origin_cu)
13309 compute_delayed_physnames (origin_cu);
13310 }
13311
13312 static void
13313 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
13314 {
13315 struct objfile *objfile = cu->per_objfile->objfile;
13316 struct gdbarch *gdbarch = objfile->arch ();
13317 struct context_stack *newobj;
13318 CORE_ADDR lowpc;
13319 CORE_ADDR highpc;
13320 struct die_info *child_die;
13321 struct attribute *attr, *call_line, *call_file;
13322 const char *name;
13323 CORE_ADDR baseaddr;
13324 struct block *block;
13325 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
13326 std::vector<struct symbol *> template_args;
13327 struct template_symbol *templ_func = NULL;
13328
13329 if (inlined_func)
13330 {
13331 /* If we do not have call site information, we can't show the
13332 caller of this inlined function. That's too confusing, so
13333 only use the scope for local variables. */
13334 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13335 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13336 if (call_line == NULL || call_file == NULL)
13337 {
13338 read_lexical_block_scope (die, cu);
13339 return;
13340 }
13341 }
13342
13343 baseaddr = objfile->text_section_offset ();
13344
13345 name = dwarf2_name (die, cu);
13346
13347 /* Ignore functions with missing or empty names. These are actually
13348 illegal according to the DWARF standard. */
13349 if (name == NULL)
13350 {
13351 complaint (_("missing name for subprogram DIE at %s"),
13352 sect_offset_str (die->sect_off));
13353 return;
13354 }
13355
13356 /* Ignore functions with missing or invalid low and high pc attributes. */
13357 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
13358 <= PC_BOUNDS_INVALID)
13359 {
13360 attr = dwarf2_attr (die, DW_AT_external, cu);
13361 if (attr == nullptr || !attr->as_boolean ())
13362 complaint (_("cannot get low and high bounds "
13363 "for subprogram DIE at %s"),
13364 sect_offset_str (die->sect_off));
13365 return;
13366 }
13367
13368 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13369 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13370
13371 /* If we have any template arguments, then we must allocate a
13372 different sort of symbol. */
13373 for (child_die = die->child; child_die; child_die = child_die->sibling)
13374 {
13375 if (child_die->tag == DW_TAG_template_type_param
13376 || child_die->tag == DW_TAG_template_value_param)
13377 {
13378 templ_func = new (&objfile->objfile_obstack) template_symbol;
13379 templ_func->subclass = SYMBOL_TEMPLATE;
13380 break;
13381 }
13382 }
13383
13384 gdb_assert (cu->get_builder () != nullptr);
13385 newobj = cu->get_builder ()->push_context (0, lowpc);
13386 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13387 (struct symbol *) templ_func);
13388
13389 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13390 set_objfile_main_name (objfile, newobj->name->linkage_name (),
13391 cu->language);
13392
13393 /* If there is a location expression for DW_AT_frame_base, record
13394 it. */
13395 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
13396 if (attr != nullptr)
13397 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
13398
13399 /* If there is a location for the static link, record it. */
13400 newobj->static_link = NULL;
13401 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13402 if (attr != nullptr)
13403 {
13404 newobj->static_link
13405 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
13406 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13407 cu->addr_type ());
13408 }
13409
13410 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
13411
13412 if (die->child != NULL)
13413 {
13414 child_die = die->child;
13415 while (child_die && child_die->tag)
13416 {
13417 if (child_die->tag == DW_TAG_template_type_param
13418 || child_die->tag == DW_TAG_template_value_param)
13419 {
13420 struct symbol *arg = new_symbol (child_die, NULL, cu);
13421
13422 if (arg != NULL)
13423 template_args.push_back (arg);
13424 }
13425 else
13426 process_die (child_die, cu);
13427 child_die = child_die->sibling;
13428 }
13429 }
13430
13431 inherit_abstract_dies (die, cu);
13432
13433 /* If we have a DW_AT_specification, we might need to import using
13434 directives from the context of the specification DIE. See the
13435 comment in determine_prefix. */
13436 if (cu->language == language_cplus
13437 && dwarf2_attr (die, DW_AT_specification, cu))
13438 {
13439 struct dwarf2_cu *spec_cu = cu;
13440 struct die_info *spec_die = die_specification (die, &spec_cu);
13441
13442 while (spec_die)
13443 {
13444 child_die = spec_die->child;
13445 while (child_die && child_die->tag)
13446 {
13447 if (child_die->tag == DW_TAG_imported_module)
13448 process_die (child_die, spec_cu);
13449 child_die = child_die->sibling;
13450 }
13451
13452 /* In some cases, GCC generates specification DIEs that
13453 themselves contain DW_AT_specification attributes. */
13454 spec_die = die_specification (spec_die, &spec_cu);
13455 }
13456 }
13457
13458 struct context_stack cstk = cu->get_builder ()->pop_context ();
13459 /* Make a block for the local symbols within. */
13460 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
13461 cstk.static_link, lowpc, highpc);
13462
13463 /* For C++, set the block's scope. */
13464 if ((cu->language == language_cplus
13465 || cu->language == language_fortran
13466 || cu->language == language_d
13467 || cu->language == language_rust)
13468 && cu->processing_has_namespace_info)
13469 block_set_scope (block, determine_prefix (die, cu),
13470 &objfile->objfile_obstack);
13471
13472 /* If we have address ranges, record them. */
13473 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13474
13475 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
13476
13477 /* Attach template arguments to function. */
13478 if (!template_args.empty ())
13479 {
13480 gdb_assert (templ_func != NULL);
13481
13482 templ_func->n_template_arguments = template_args.size ();
13483 templ_func->template_arguments
13484 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13485 templ_func->n_template_arguments);
13486 memcpy (templ_func->template_arguments,
13487 template_args.data (),
13488 (templ_func->n_template_arguments * sizeof (struct symbol *)));
13489
13490 /* Make sure that the symtab is set on the new symbols. Even
13491 though they don't appear in this symtab directly, other parts
13492 of gdb assume that symbols do, and this is reasonably
13493 true. */
13494 for (symbol *sym : template_args)
13495 symbol_set_symtab (sym, symbol_symtab (templ_func));
13496 }
13497
13498 /* In C++, we can have functions nested inside functions (e.g., when
13499 a function declares a class that has methods). This means that
13500 when we finish processing a function scope, we may need to go
13501 back to building a containing block's symbol lists. */
13502 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13503 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13504
13505 /* If we've finished processing a top-level function, subsequent
13506 symbols go in the file symbol list. */
13507 if (cu->get_builder ()->outermost_context_p ())
13508 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
13509 }
13510
13511 /* Process all the DIES contained within a lexical block scope. Start
13512 a new scope, process the dies, and then close the scope. */
13513
13514 static void
13515 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
13516 {
13517 struct objfile *objfile = cu->per_objfile->objfile;
13518 struct gdbarch *gdbarch = objfile->arch ();
13519 CORE_ADDR lowpc, highpc;
13520 struct die_info *child_die;
13521 CORE_ADDR baseaddr;
13522
13523 baseaddr = objfile->text_section_offset ();
13524
13525 /* Ignore blocks with missing or invalid low and high pc attributes. */
13526 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13527 as multiple lexical blocks? Handling children in a sane way would
13528 be nasty. Might be easier to properly extend generic blocks to
13529 describe ranges. */
13530 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13531 {
13532 case PC_BOUNDS_NOT_PRESENT:
13533 /* DW_TAG_lexical_block has no attributes, process its children as if
13534 there was no wrapping by that DW_TAG_lexical_block.
13535 GCC does no longer produces such DWARF since GCC r224161. */
13536 for (child_die = die->child;
13537 child_die != NULL && child_die->tag;
13538 child_die = child_die->sibling)
13539 {
13540 /* We might already be processing this DIE. This can happen
13541 in an unusual circumstance -- where a subroutine A
13542 appears lexically in another subroutine B, but A actually
13543 inlines B. The recursion is broken here, rather than in
13544 inherit_abstract_dies, because it seems better to simply
13545 drop concrete children here. */
13546 if (!child_die->in_process)
13547 process_die (child_die, cu);
13548 }
13549 return;
13550 case PC_BOUNDS_INVALID:
13551 return;
13552 }
13553 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13554 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
13555
13556 cu->get_builder ()->push_context (0, lowpc);
13557 if (die->child != NULL)
13558 {
13559 child_die = die->child;
13560 while (child_die && child_die->tag)
13561 {
13562 process_die (child_die, cu);
13563 child_die = child_die->sibling;
13564 }
13565 }
13566 inherit_abstract_dies (die, cu);
13567 struct context_stack cstk = cu->get_builder ()->pop_context ();
13568
13569 if (*cu->get_builder ()->get_local_symbols () != NULL
13570 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
13571 {
13572 struct block *block
13573 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
13574 cstk.start_addr, highpc);
13575
13576 /* Note that recording ranges after traversing children, as we
13577 do here, means that recording a parent's ranges entails
13578 walking across all its children's ranges as they appear in
13579 the address map, which is quadratic behavior.
13580
13581 It would be nicer to record the parent's ranges before
13582 traversing its children, simply overriding whatever you find
13583 there. But since we don't even decide whether to create a
13584 block until after we've traversed its children, that's hard
13585 to do. */
13586 dwarf2_record_block_ranges (die, block, baseaddr, cu);
13587 }
13588 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13589 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
13590 }
13591
13592 /* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
13593
13594 static void
13595 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13596 {
13597 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13598 struct objfile *objfile = per_objfile->objfile;
13599 struct gdbarch *gdbarch = objfile->arch ();
13600 CORE_ADDR pc, baseaddr;
13601 struct attribute *attr;
13602 struct call_site *call_site, call_site_local;
13603 void **slot;
13604 int nparams;
13605 struct die_info *child_die;
13606
13607 baseaddr = objfile->text_section_offset ();
13608
13609 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13610 if (attr == NULL)
13611 {
13612 /* This was a pre-DWARF-5 GNU extension alias
13613 for DW_AT_call_return_pc. */
13614 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13615 }
13616 if (!attr)
13617 {
13618 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
13619 "DIE %s [in module %s]"),
13620 sect_offset_str (die->sect_off), objfile_name (objfile));
13621 return;
13622 }
13623 pc = attr->as_address () + baseaddr;
13624 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
13625
13626 if (cu->call_site_htab == NULL)
13627 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13628 NULL, &objfile->objfile_obstack,
13629 hashtab_obstack_allocate, NULL);
13630 call_site_local.pc = pc;
13631 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13632 if (*slot != NULL)
13633 {
13634 complaint (_("Duplicate PC %s for DW_TAG_call_site "
13635 "DIE %s [in module %s]"),
13636 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
13637 objfile_name (objfile));
13638 return;
13639 }
13640
13641 /* Count parameters at the caller. */
13642
13643 nparams = 0;
13644 for (child_die = die->child; child_die && child_die->tag;
13645 child_die = child_die->sibling)
13646 {
13647 if (child_die->tag != DW_TAG_call_site_parameter
13648 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13649 {
13650 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
13651 "DW_TAG_call_site child DIE %s [in module %s]"),
13652 child_die->tag, sect_offset_str (child_die->sect_off),
13653 objfile_name (objfile));
13654 continue;
13655 }
13656
13657 nparams++;
13658 }
13659
13660 call_site
13661 = ((struct call_site *)
13662 obstack_alloc (&objfile->objfile_obstack,
13663 sizeof (*call_site)
13664 + (sizeof (*call_site->parameter) * (nparams - 1))));
13665 *slot = call_site;
13666 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13667 call_site->pc = pc;
13668
13669 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13670 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
13671 {
13672 struct die_info *func_die;
13673
13674 /* Skip also over DW_TAG_inlined_subroutine. */
13675 for (func_die = die->parent;
13676 func_die && func_die->tag != DW_TAG_subprogram
13677 && func_die->tag != DW_TAG_subroutine_type;
13678 func_die = func_die->parent);
13679
13680 /* DW_AT_call_all_calls is a superset
13681 of DW_AT_call_all_tail_calls. */
13682 if (func_die
13683 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13684 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
13685 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
13686 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13687 {
13688 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13689 not complete. But keep CALL_SITE for look ups via call_site_htab,
13690 both the initial caller containing the real return address PC and
13691 the final callee containing the current PC of a chain of tail
13692 calls do not need to have the tail call list complete. But any
13693 function candidate for a virtual tail call frame searched via
13694 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13695 determined unambiguously. */
13696 }
13697 else
13698 {
13699 struct type *func_type = NULL;
13700
13701 if (func_die)
13702 func_type = get_die_type (func_die, cu);
13703 if (func_type != NULL)
13704 {
13705 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
13706
13707 /* Enlist this call site to the function. */
13708 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13709 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13710 }
13711 else
13712 complaint (_("Cannot find function owning DW_TAG_call_site "
13713 "DIE %s [in module %s]"),
13714 sect_offset_str (die->sect_off), objfile_name (objfile));
13715 }
13716 }
13717
13718 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13719 if (attr == NULL)
13720 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13721 if (attr == NULL)
13722 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
13723 if (attr == NULL)
13724 {
13725 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13726 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13727 }
13728 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13729 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
13730 /* Keep NULL DWARF_BLOCK. */;
13731 else if (attr->form_is_block ())
13732 {
13733 struct dwarf2_locexpr_baton *dlbaton;
13734 struct dwarf_block *block = attr->as_block ();
13735
13736 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
13737 dlbaton->data = block->data;
13738 dlbaton->size = block->size;
13739 dlbaton->per_objfile = per_objfile;
13740 dlbaton->per_cu = cu->per_cu;
13741
13742 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13743 }
13744 else if (attr->form_is_ref ())
13745 {
13746 struct dwarf2_cu *target_cu = cu;
13747 struct die_info *target_die;
13748
13749 target_die = follow_die_ref (die, attr, &target_cu);
13750 gdb_assert (target_cu->per_objfile->objfile == objfile);
13751 if (die_is_declaration (target_die, target_cu))
13752 {
13753 const char *target_physname;
13754
13755 /* Prefer the mangled name; otherwise compute the demangled one. */
13756 target_physname = dw2_linkage_name (target_die, target_cu);
13757 if (target_physname == NULL)
13758 target_physname = dwarf2_physname (NULL, target_die, target_cu);
13759 if (target_physname == NULL)
13760 complaint (_("DW_AT_call_target target DIE has invalid "
13761 "physname, for referencing DIE %s [in module %s]"),
13762 sect_offset_str (die->sect_off), objfile_name (objfile));
13763 else
13764 SET_FIELD_PHYSNAME (call_site->target, target_physname);
13765 }
13766 else
13767 {
13768 CORE_ADDR lowpc;
13769
13770 /* DW_AT_entry_pc should be preferred. */
13771 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
13772 <= PC_BOUNDS_INVALID)
13773 complaint (_("DW_AT_call_target target DIE has invalid "
13774 "low pc, for referencing DIE %s [in module %s]"),
13775 sect_offset_str (die->sect_off), objfile_name (objfile));
13776 else
13777 {
13778 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13779 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13780 }
13781 }
13782 }
13783 else
13784 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
13785 "block nor reference, for DIE %s [in module %s]"),
13786 sect_offset_str (die->sect_off), objfile_name (objfile));
13787
13788 call_site->per_cu = cu->per_cu;
13789 call_site->per_objfile = per_objfile;
13790
13791 for (child_die = die->child;
13792 child_die && child_die->tag;
13793 child_die = child_die->sibling)
13794 {
13795 struct call_site_parameter *parameter;
13796 struct attribute *loc, *origin;
13797
13798 if (child_die->tag != DW_TAG_call_site_parameter
13799 && child_die->tag != DW_TAG_GNU_call_site_parameter)
13800 {
13801 /* Already printed the complaint above. */
13802 continue;
13803 }
13804
13805 gdb_assert (call_site->parameter_count < nparams);
13806 parameter = &call_site->parameter[call_site->parameter_count];
13807
13808 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13809 specifies DW_TAG_formal_parameter. Value of the data assumed for the
13810 register is contained in DW_AT_call_value. */
13811
13812 loc = dwarf2_attr (child_die, DW_AT_location, cu);
13813 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13814 if (origin == NULL)
13815 {
13816 /* This was a pre-DWARF-5 GNU extension alias
13817 for DW_AT_call_parameter. */
13818 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13819 }
13820 if (loc == NULL && origin != NULL && origin->form_is_ref ())
13821 {
13822 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
13823
13824 sect_offset sect_off = origin->get_ref_die_offset ();
13825 if (!cu->header.offset_in_cu_p (sect_off))
13826 {
13827 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13828 binding can be done only inside one CU. Such referenced DIE
13829 therefore cannot be even moved to DW_TAG_partial_unit. */
13830 complaint (_("DW_AT_call_parameter offset is not in CU for "
13831 "DW_TAG_call_site child DIE %s [in module %s]"),
13832 sect_offset_str (child_die->sect_off),
13833 objfile_name (objfile));
13834 continue;
13835 }
13836 parameter->u.param_cu_off
13837 = (cu_offset) (sect_off - cu->header.sect_off);
13838 }
13839 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
13840 {
13841 complaint (_("No DW_FORM_block* DW_AT_location for "
13842 "DW_TAG_call_site child DIE %s [in module %s]"),
13843 sect_offset_str (child_die->sect_off), objfile_name (objfile));
13844 continue;
13845 }
13846 else
13847 {
13848 struct dwarf_block *block = loc->as_block ();
13849
13850 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13851 (block->data, &block->data[block->size]);
13852 if (parameter->u.dwarf_reg != -1)
13853 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13854 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
13855 &block->data[block->size],
13856 &parameter->u.fb_offset))
13857 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13858 else
13859 {
13860 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
13861 "for DW_FORM_block* DW_AT_location is supported for "
13862 "DW_TAG_call_site child DIE %s "
13863 "[in module %s]"),
13864 sect_offset_str (child_die->sect_off),
13865 objfile_name (objfile));
13866 continue;
13867 }
13868 }
13869
13870 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13871 if (attr == NULL)
13872 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
13873 if (attr == NULL || !attr->form_is_block ())
13874 {
13875 complaint (_("No DW_FORM_block* DW_AT_call_value for "
13876 "DW_TAG_call_site child DIE %s [in module %s]"),
13877 sect_offset_str (child_die->sect_off),
13878 objfile_name (objfile));
13879 continue;
13880 }
13881
13882 struct dwarf_block *block = attr->as_block ();
13883 parameter->value = block->data;
13884 parameter->value_size = block->size;
13885
13886 /* Parameters are not pre-cleared by memset above. */
13887 parameter->data_value = NULL;
13888 parameter->data_value_size = 0;
13889 call_site->parameter_count++;
13890
13891 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13892 if (attr == NULL)
13893 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
13894 if (attr != nullptr)
13895 {
13896 if (!attr->form_is_block ())
13897 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
13898 "DW_TAG_call_site child DIE %s [in module %s]"),
13899 sect_offset_str (child_die->sect_off),
13900 objfile_name (objfile));
13901 else
13902 {
13903 block = attr->as_block ();
13904 parameter->data_value = block->data;
13905 parameter->data_value_size = block->size;
13906 }
13907 }
13908 }
13909 }
13910
13911 /* Helper function for read_variable. If DIE represents a virtual
13912 table, then return the type of the concrete object that is
13913 associated with the virtual table. Otherwise, return NULL. */
13914
13915 static struct type *
13916 rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13917 {
13918 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13919 if (attr == NULL)
13920 return NULL;
13921
13922 /* Find the type DIE. */
13923 struct die_info *type_die = NULL;
13924 struct dwarf2_cu *type_cu = cu;
13925
13926 if (attr->form_is_ref ())
13927 type_die = follow_die_ref (die, attr, &type_cu);
13928 if (type_die == NULL)
13929 return NULL;
13930
13931 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13932 return NULL;
13933 return die_containing_type (type_die, type_cu);
13934 }
13935
13936 /* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13937
13938 static void
13939 read_variable (struct die_info *die, struct dwarf2_cu *cu)
13940 {
13941 struct rust_vtable_symbol *storage = NULL;
13942
13943 if (cu->language == language_rust)
13944 {
13945 struct type *containing_type = rust_containing_type (die, cu);
13946
13947 if (containing_type != NULL)
13948 {
13949 struct objfile *objfile = cu->per_objfile->objfile;
13950
13951 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
13952 storage->concrete_type = containing_type;
13953 storage->subclass = SYMBOL_RUST_VTABLE;
13954 }
13955 }
13956
13957 struct symbol *res = new_symbol (die, NULL, cu, storage);
13958 struct attribute *abstract_origin
13959 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13960 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13961 if (res == NULL && loc && abstract_origin)
13962 {
13963 /* We have a variable without a name, but with a location and an abstract
13964 origin. This may be a concrete instance of an abstract variable
13965 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13966 later. */
13967 struct dwarf2_cu *origin_cu = cu;
13968 struct die_info *origin_die
13969 = follow_die_ref (die, abstract_origin, &origin_cu);
13970 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13971 per_objfile->per_bfd->abstract_to_concrete
13972 [origin_die->sect_off].push_back (die->sect_off);
13973 }
13974 }
13975
13976 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13977 reading .debug_rnglists.
13978 Callback's type should be:
13979 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13980 Return true if the attributes are present and valid, otherwise,
13981 return false. */
13982
13983 template <typename Callback>
13984 static bool
13985 dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13986 dwarf_tag tag, Callback &&callback)
13987 {
13988 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13989 struct objfile *objfile = per_objfile->objfile;
13990 bfd *obfd = objfile->obfd;
13991 /* Base address selection entry. */
13992 gdb::optional<CORE_ADDR> base;
13993 const gdb_byte *buffer;
13994 CORE_ADDR baseaddr;
13995 bool overflow = false;
13996 ULONGEST addr_index;
13997 struct dwarf2_section_info *rnglists_section;
13998
13999 base = cu->base_address;
14000 rnglists_section = cu_debug_rnglists_section (cu, tag);
14001 rnglists_section->read (objfile);
14002
14003 if (offset >= rnglists_section->size)
14004 {
14005 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14006 offset);
14007 return false;
14008 }
14009 buffer = rnglists_section->buffer + offset;
14010
14011 baseaddr = objfile->text_section_offset ();
14012
14013 while (1)
14014 {
14015 /* Initialize it due to a false compiler warning. */
14016 CORE_ADDR range_beginning = 0, range_end = 0;
14017 const gdb_byte *buf_end = (rnglists_section->buffer
14018 + rnglists_section->size);
14019 unsigned int bytes_read;
14020
14021 if (buffer == buf_end)
14022 {
14023 overflow = true;
14024 break;
14025 }
14026 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14027 switch (rlet)
14028 {
14029 case DW_RLE_end_of_list:
14030 break;
14031 case DW_RLE_base_address:
14032 if (buffer + cu->header.addr_size > buf_end)
14033 {
14034 overflow = true;
14035 break;
14036 }
14037 base = cu->header.read_address (obfd, buffer, &bytes_read);
14038 buffer += bytes_read;
14039 break;
14040 case DW_RLE_base_addressx:
14041 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14042 buffer += bytes_read;
14043 base = read_addr_index (cu, addr_index);
14044 break;
14045 case DW_RLE_start_length:
14046 if (buffer + cu->header.addr_size > buf_end)
14047 {
14048 overflow = true;
14049 break;
14050 }
14051 range_beginning = cu->header.read_address (obfd, buffer,
14052 &bytes_read);
14053 buffer += bytes_read;
14054 range_end = (range_beginning
14055 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14056 buffer += bytes_read;
14057 if (buffer > buf_end)
14058 {
14059 overflow = true;
14060 break;
14061 }
14062 break;
14063 case DW_RLE_startx_length:
14064 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14065 buffer += bytes_read;
14066 range_beginning = read_addr_index (cu, addr_index);
14067 if (buffer > buf_end)
14068 {
14069 overflow = true;
14070 break;
14071 }
14072 range_end = (range_beginning
14073 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14074 buffer += bytes_read;
14075 break;
14076 case DW_RLE_offset_pair:
14077 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14078 buffer += bytes_read;
14079 if (buffer > buf_end)
14080 {
14081 overflow = true;
14082 break;
14083 }
14084 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14085 buffer += bytes_read;
14086 if (buffer > buf_end)
14087 {
14088 overflow = true;
14089 break;
14090 }
14091 break;
14092 case DW_RLE_start_end:
14093 if (buffer + 2 * cu->header.addr_size > buf_end)
14094 {
14095 overflow = true;
14096 break;
14097 }
14098 range_beginning = cu->header.read_address (obfd, buffer,
14099 &bytes_read);
14100 buffer += bytes_read;
14101 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
14102 buffer += bytes_read;
14103 break;
14104 case DW_RLE_startx_endx:
14105 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14106 buffer += bytes_read;
14107 range_beginning = read_addr_index (cu, addr_index);
14108 if (buffer > buf_end)
14109 {
14110 overflow = true;
14111 break;
14112 }
14113 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14114 buffer += bytes_read;
14115 range_end = read_addr_index (cu, addr_index);
14116 break;
14117 default:
14118 complaint (_("Invalid .debug_rnglists data (no base address)"));
14119 return false;
14120 }
14121 if (rlet == DW_RLE_end_of_list || overflow)
14122 break;
14123 if (rlet == DW_RLE_base_address)
14124 continue;
14125
14126 if (range_beginning > range_end)
14127 {
14128 /* Inverted range entries are invalid. */
14129 complaint (_("Invalid .debug_rnglists data (inverted range)"));
14130 return false;
14131 }
14132
14133 /* Empty range entries have no effect. */
14134 if (range_beginning == range_end)
14135 continue;
14136
14137 /* Only DW_RLE_offset_pair needs the base address added. */
14138 if (rlet == DW_RLE_offset_pair)
14139 {
14140 if (!base.has_value ())
14141 {
14142 /* We have no valid base address for the DW_RLE_offset_pair. */
14143 complaint (_("Invalid .debug_rnglists data (no base address for "
14144 "DW_RLE_offset_pair)"));
14145 return false;
14146 }
14147
14148 range_beginning += *base;
14149 range_end += *base;
14150 }
14151
14152 /* A not-uncommon case of bad debug info.
14153 Don't pollute the addrmap with bad data. */
14154 if (range_beginning + baseaddr == 0
14155 && !per_objfile->per_bfd->has_section_at_zero)
14156 {
14157 complaint (_(".debug_rnglists entry has start address of zero"
14158 " [in module %s]"), objfile_name (objfile));
14159 continue;
14160 }
14161
14162 callback (range_beginning, range_end);
14163 }
14164
14165 if (overflow)
14166 {
14167 complaint (_("Offset %d is not terminated "
14168 "for DW_AT_ranges attribute"),
14169 offset);
14170 return false;
14171 }
14172
14173 return true;
14174 }
14175
14176 /* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14177 Callback's type should be:
14178 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14179 Return 1 if the attributes are present and valid, otherwise, return 0. */
14180
14181 template <typename Callback>
14182 static int
14183 dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
14184 Callback &&callback)
14185 {
14186 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14187 struct objfile *objfile = per_objfile->objfile;
14188 struct comp_unit_head *cu_header = &cu->header;
14189 bfd *obfd = objfile->obfd;
14190 unsigned int addr_size = cu_header->addr_size;
14191 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14192 /* Base address selection entry. */
14193 gdb::optional<CORE_ADDR> base;
14194 unsigned int dummy;
14195 const gdb_byte *buffer;
14196 CORE_ADDR baseaddr;
14197
14198 if (cu_header->version >= 5)
14199 return dwarf2_rnglists_process (offset, cu, tag, callback);
14200
14201 base = cu->base_address;
14202
14203 per_objfile->per_bfd->ranges.read (objfile);
14204 if (offset >= per_objfile->per_bfd->ranges.size)
14205 {
14206 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
14207 offset);
14208 return 0;
14209 }
14210 buffer = per_objfile->per_bfd->ranges.buffer + offset;
14211
14212 baseaddr = objfile->text_section_offset ();
14213
14214 while (1)
14215 {
14216 CORE_ADDR range_beginning, range_end;
14217
14218 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
14219 buffer += addr_size;
14220 range_end = cu->header.read_address (obfd, buffer, &dummy);
14221 buffer += addr_size;
14222 offset += 2 * addr_size;
14223
14224 /* An end of list marker is a pair of zero addresses. */
14225 if (range_beginning == 0 && range_end == 0)
14226 /* Found the end of list entry. */
14227 break;
14228
14229 /* Each base address selection entry is a pair of 2 values.
14230 The first is the largest possible address, the second is
14231 the base address. Check for a base address here. */
14232 if ((range_beginning & mask) == mask)
14233 {
14234 /* If we found the largest possible address, then we already
14235 have the base address in range_end. */
14236 base = range_end;
14237 continue;
14238 }
14239
14240 if (!base.has_value ())
14241 {
14242 /* We have no valid base address for the ranges
14243 data. */
14244 complaint (_("Invalid .debug_ranges data (no base address)"));
14245 return 0;
14246 }
14247
14248 if (range_beginning > range_end)
14249 {
14250 /* Inverted range entries are invalid. */
14251 complaint (_("Invalid .debug_ranges data (inverted range)"));
14252 return 0;
14253 }
14254
14255 /* Empty range entries have no effect. */
14256 if (range_beginning == range_end)
14257 continue;
14258
14259 range_beginning += *base;
14260 range_end += *base;
14261
14262 /* A not-uncommon case of bad debug info.
14263 Don't pollute the addrmap with bad data. */
14264 if (range_beginning + baseaddr == 0
14265 && !per_objfile->per_bfd->has_section_at_zero)
14266 {
14267 complaint (_(".debug_ranges entry has start address of zero"
14268 " [in module %s]"), objfile_name (objfile));
14269 continue;
14270 }
14271
14272 callback (range_beginning, range_end);
14273 }
14274
14275 return 1;
14276 }
14277
14278 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14279 Return 1 if the attributes are present and valid, otherwise, return 0.
14280 If RANGES_PST is not NULL we should set up the `psymtabs_addrmap'. */
14281
14282 static int
14283 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14284 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14285 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
14286 {
14287 struct objfile *objfile = cu->per_objfile->objfile;
14288 dwarf2_per_bfd *per_bfd = cu->per_objfile->per_bfd;
14289 struct gdbarch *gdbarch = objfile->arch ();
14290 const CORE_ADDR baseaddr = objfile->text_section_offset ();
14291 int low_set = 0;
14292 CORE_ADDR low = 0;
14293 CORE_ADDR high = 0;
14294 int retval;
14295
14296 retval = dwarf2_ranges_process (offset, cu, tag,
14297 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14298 {
14299 if (ranges_pst != NULL)
14300 {
14301 CORE_ADDR lowpc;
14302 CORE_ADDR highpc;
14303
14304 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14305 range_beginning + baseaddr)
14306 - baseaddr);
14307 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14308 range_end + baseaddr)
14309 - baseaddr);
14310 addrmap_set_empty (per_bfd->partial_symtabs->psymtabs_addrmap,
14311 lowpc, highpc - 1, ranges_pst);
14312 }
14313
14314 /* FIXME: This is recording everything as a low-high
14315 segment of consecutive addresses. We should have a
14316 data structure for discontiguous block ranges
14317 instead. */
14318 if (! low_set)
14319 {
14320 low = range_beginning;
14321 high = range_end;
14322 low_set = 1;
14323 }
14324 else
14325 {
14326 if (range_beginning < low)
14327 low = range_beginning;
14328 if (range_end > high)
14329 high = range_end;
14330 }
14331 });
14332 if (!retval)
14333 return 0;
14334
14335 if (! low_set)
14336 /* If the first entry is an end-of-list marker, the range
14337 describes an empty scope, i.e. no instructions. */
14338 return 0;
14339
14340 if (low_return)
14341 *low_return = low;
14342 if (high_return)
14343 *high_return = high;
14344 return 1;
14345 }
14346
14347 /* Get low and high pc attributes from a die. See enum pc_bounds_kind
14348 definition for the return value. *LOWPC and *HIGHPC are set iff
14349 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
14350
14351 static enum pc_bounds_kind
14352 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
14353 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14354 dwarf2_psymtab *pst)
14355 {
14356 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14357 struct attribute *attr;
14358 struct attribute *attr_high;
14359 CORE_ADDR low = 0;
14360 CORE_ADDR high = 0;
14361 enum pc_bounds_kind ret;
14362
14363 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14364 if (attr_high)
14365 {
14366 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14367 if (attr != nullptr)
14368 {
14369 low = attr->as_address ();
14370 high = attr_high->as_address ();
14371 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14372 high += low;
14373 }
14374 else
14375 /* Found high w/o low attribute. */
14376 return PC_BOUNDS_INVALID;
14377
14378 /* Found consecutive range of addresses. */
14379 ret = PC_BOUNDS_HIGH_LOW;
14380 }
14381 else
14382 {
14383 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14384 if (attr != nullptr && attr->form_is_unsigned ())
14385 {
14386 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14387 on DWARF version). */
14388 ULONGEST ranges_offset = attr->as_unsigned ();
14389
14390 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14391 this value. */
14392 if (die->tag != DW_TAG_compile_unit)
14393 ranges_offset += cu->gnu_ranges_base;
14394
14395 /* Value of the DW_AT_ranges attribute is the offset in the
14396 .debug_ranges section. */
14397 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14398 die->tag))
14399 return PC_BOUNDS_INVALID;
14400 /* Found discontinuous range of addresses. */
14401 ret = PC_BOUNDS_RANGES;
14402 }
14403 else
14404 return PC_BOUNDS_NOT_PRESENT;
14405 }
14406
14407 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
14408 if (high <= low)
14409 return PC_BOUNDS_INVALID;
14410
14411 /* When using the GNU linker, .gnu.linkonce. sections are used to
14412 eliminate duplicate copies of functions and vtables and such.
14413 The linker will arbitrarily choose one and discard the others.
14414 The AT_*_pc values for such functions refer to local labels in
14415 these sections. If the section from that file was discarded, the
14416 labels are not in the output, so the relocs get a value of 0.
14417 If this is a discarded function, mark the pc bounds as invalid,
14418 so that GDB will ignore it. */
14419 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
14420 return PC_BOUNDS_INVALID;
14421
14422 *lowpc = low;
14423 if (highpc)
14424 *highpc = high;
14425 return ret;
14426 }
14427
14428 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
14429 its low and high PC addresses. Do nothing if these addresses could not
14430 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14431 and HIGHPC to the high address if greater than HIGHPC. */
14432
14433 static void
14434 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14435 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14436 struct dwarf2_cu *cu)
14437 {
14438 CORE_ADDR low, high;
14439 struct die_info *child = die->child;
14440
14441 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
14442 {
14443 *lowpc = std::min (*lowpc, low);
14444 *highpc = std::max (*highpc, high);
14445 }
14446
14447 /* If the language does not allow nested subprograms (either inside
14448 subprograms or lexical blocks), we're done. */
14449 if (cu->language != language_ada)
14450 return;
14451
14452 /* Check all the children of the given DIE. If it contains nested
14453 subprograms, then check their pc bounds. Likewise, we need to
14454 check lexical blocks as well, as they may also contain subprogram
14455 definitions. */
14456 while (child && child->tag)
14457 {
14458 if (child->tag == DW_TAG_subprogram
14459 || child->tag == DW_TAG_lexical_block)
14460 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14461 child = child->sibling;
14462 }
14463 }
14464
14465 /* Get the low and high pc's represented by the scope DIE, and store
14466 them in *LOWPC and *HIGHPC. If the correct values can't be
14467 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14468
14469 static void
14470 get_scope_pc_bounds (struct die_info *die,
14471 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14472 struct dwarf2_cu *cu)
14473 {
14474 CORE_ADDR best_low = (CORE_ADDR) -1;
14475 CORE_ADDR best_high = (CORE_ADDR) 0;
14476 CORE_ADDR current_low, current_high;
14477
14478 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
14479 >= PC_BOUNDS_RANGES)
14480 {
14481 best_low = current_low;
14482 best_high = current_high;
14483 }
14484 else
14485 {
14486 struct die_info *child = die->child;
14487
14488 while (child && child->tag)
14489 {
14490 switch (child->tag) {
14491 case DW_TAG_subprogram:
14492 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
14493 break;
14494 case DW_TAG_namespace:
14495 case DW_TAG_module:
14496 /* FIXME: carlton/2004-01-16: Should we do this for
14497 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14498 that current GCC's always emit the DIEs corresponding
14499 to definitions of methods of classes as children of a
14500 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14501 the DIEs giving the declarations, which could be
14502 anywhere). But I don't see any reason why the
14503 standards says that they have to be there. */
14504 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14505
14506 if (current_low != ((CORE_ADDR) -1))
14507 {
14508 best_low = std::min (best_low, current_low);
14509 best_high = std::max (best_high, current_high);
14510 }
14511 break;
14512 default:
14513 /* Ignore. */
14514 break;
14515 }
14516
14517 child = child->sibling;
14518 }
14519 }
14520
14521 *lowpc = best_low;
14522 *highpc = best_high;
14523 }
14524
14525 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
14526 in DIE. */
14527
14528 static void
14529 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14530 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14531 {
14532 struct objfile *objfile = cu->per_objfile->objfile;
14533 struct gdbarch *gdbarch = objfile->arch ();
14534 struct attribute *attr;
14535 struct attribute *attr_high;
14536
14537 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14538 if (attr_high)
14539 {
14540 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14541 if (attr != nullptr)
14542 {
14543 CORE_ADDR low = attr->as_address ();
14544 CORE_ADDR high = attr_high->as_address ();
14545
14546 if (cu->header.version >= 4 && attr_high->form_is_constant ())
14547 high += low;
14548
14549 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14550 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14551 cu->get_builder ()->record_block_range (block, low, high - 1);
14552 }
14553 }
14554
14555 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14556 if (attr != nullptr && attr->form_is_unsigned ())
14557 {
14558 /* Offset in the .debug_ranges or .debug_rnglist section (depending
14559 on DWARF version). */
14560 ULONGEST ranges_offset = attr->as_unsigned ();
14561
14562 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
14563 this value. */
14564 if (die->tag != DW_TAG_compile_unit)
14565 ranges_offset += cu->gnu_ranges_base;
14566
14567 std::vector<blockrange> blockvec;
14568 dwarf2_ranges_process (ranges_offset, cu, die->tag,
14569 [&] (CORE_ADDR start, CORE_ADDR end)
14570 {
14571 start += baseaddr;
14572 end += baseaddr;
14573 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14574 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14575 cu->get_builder ()->record_block_range (block, start, end - 1);
14576 blockvec.emplace_back (start, end);
14577 });
14578
14579 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
14580 }
14581 }
14582
14583 /* Check whether the producer field indicates either of GCC < 4.6, or the
14584 Intel C/C++ compiler, and cache the result in CU. */
14585
14586 static void
14587 check_producer (struct dwarf2_cu *cu)
14588 {
14589 int major, minor;
14590
14591 if (cu->producer == NULL)
14592 {
14593 /* For unknown compilers expect their behavior is DWARF version
14594 compliant.
14595
14596 GCC started to support .debug_types sections by -gdwarf-4 since
14597 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14598 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14599 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14600 interpreted incorrectly by GDB now - GCC PR debug/48229. */
14601 }
14602 else if (producer_is_gcc (cu->producer, &major, &minor))
14603 {
14604 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14605 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
14606 }
14607 else if (producer_is_icc (cu->producer, &major, &minor))
14608 {
14609 cu->producer_is_icc = true;
14610 cu->producer_is_icc_lt_14 = major < 14;
14611 }
14612 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14613 cu->producer_is_codewarrior = true;
14614 else
14615 {
14616 /* For other non-GCC compilers, expect their behavior is DWARF version
14617 compliant. */
14618 }
14619
14620 cu->checked_producer = true;
14621 }
14622
14623 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14624 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14625 during 4.6.0 experimental. */
14626
14627 static bool
14628 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14629 {
14630 if (!cu->checked_producer)
14631 check_producer (cu);
14632
14633 return cu->producer_is_gxx_lt_4_6;
14634 }
14635
14636
14637 /* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14638 with incorrect is_stmt attributes. */
14639
14640 static bool
14641 producer_is_codewarrior (struct dwarf2_cu *cu)
14642 {
14643 if (!cu->checked_producer)
14644 check_producer (cu);
14645
14646 return cu->producer_is_codewarrior;
14647 }
14648
14649 /* Return the accessibility of DIE, as given by DW_AT_accessibility.
14650 If that attribute is not available, return the appropriate
14651 default. */
14652
14653 static enum dwarf_access_attribute
14654 dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14655 {
14656 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14657 if (attr != nullptr)
14658 {
14659 LONGEST value = attr->constant_value (-1);
14660 if (value == DW_ACCESS_public
14661 || value == DW_ACCESS_protected
14662 || value == DW_ACCESS_private)
14663 return (dwarf_access_attribute) value;
14664 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14665 plongest (value));
14666 }
14667
14668 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14669 {
14670 /* The default DWARF 2 accessibility for members is public, the default
14671 accessibility for inheritance is private. */
14672
14673 if (die->tag != DW_TAG_inheritance)
14674 return DW_ACCESS_public;
14675 else
14676 return DW_ACCESS_private;
14677 }
14678 else
14679 {
14680 /* DWARF 3+ defines the default accessibility a different way. The same
14681 rules apply now for DW_TAG_inheritance as for the members and it only
14682 depends on the container kind. */
14683
14684 if (die->parent->tag == DW_TAG_class_type)
14685 return DW_ACCESS_private;
14686 else
14687 return DW_ACCESS_public;
14688 }
14689 }
14690
14691 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14692 offset. If the attribute was not found return 0, otherwise return
14693 1. If it was found but could not properly be handled, set *OFFSET
14694 to 0. */
14695
14696 static int
14697 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14698 LONGEST *offset)
14699 {
14700 struct attribute *attr;
14701
14702 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14703 if (attr != NULL)
14704 {
14705 *offset = 0;
14706
14707 /* Note that we do not check for a section offset first here.
14708 This is because DW_AT_data_member_location is new in DWARF 4,
14709 so if we see it, we can assume that a constant form is really
14710 a constant and not a section offset. */
14711 if (attr->form_is_constant ())
14712 *offset = attr->constant_value (0);
14713 else if (attr->form_is_section_offset ())
14714 dwarf2_complex_location_expr_complaint ();
14715 else if (attr->form_is_block ())
14716 *offset = decode_locdesc (attr->as_block (), cu);
14717 else
14718 dwarf2_complex_location_expr_complaint ();
14719
14720 return 1;
14721 }
14722
14723 return 0;
14724 }
14725
14726 /* Look for DW_AT_data_member_location and store the results in FIELD. */
14727
14728 static void
14729 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14730 struct field *field)
14731 {
14732 struct attribute *attr;
14733
14734 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14735 if (attr != NULL)
14736 {
14737 if (attr->form_is_constant ())
14738 {
14739 LONGEST offset = attr->constant_value (0);
14740 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14741 }
14742 else if (attr->form_is_section_offset ())
14743 dwarf2_complex_location_expr_complaint ();
14744 else if (attr->form_is_block ())
14745 {
14746 bool handled;
14747 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
14748 if (handled)
14749 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14750 else
14751 {
14752 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14753 struct objfile *objfile = per_objfile->objfile;
14754 struct dwarf2_locexpr_baton *dlbaton
14755 = XOBNEW (&objfile->objfile_obstack,
14756 struct dwarf2_locexpr_baton);
14757 dlbaton->data = attr->as_block ()->data;
14758 dlbaton->size = attr->as_block ()->size;
14759 /* When using this baton, we want to compute the address
14760 of the field, not the value. This is why
14761 is_reference is set to false here. */
14762 dlbaton->is_reference = false;
14763 dlbaton->per_objfile = per_objfile;
14764 dlbaton->per_cu = cu->per_cu;
14765
14766 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14767 }
14768 }
14769 else
14770 dwarf2_complex_location_expr_complaint ();
14771 }
14772 }
14773
14774 /* Add an aggregate field to the field list. */
14775
14776 static void
14777 dwarf2_add_field (struct field_info *fip, struct die_info *die,
14778 struct dwarf2_cu *cu)
14779 {
14780 struct objfile *objfile = cu->per_objfile->objfile;
14781 struct gdbarch *gdbarch = objfile->arch ();
14782 struct nextfield *new_field;
14783 struct attribute *attr;
14784 struct field *fp;
14785 const char *fieldname = "";
14786
14787 if (die->tag == DW_TAG_inheritance)
14788 {
14789 fip->baseclasses.emplace_back ();
14790 new_field = &fip->baseclasses.back ();
14791 }
14792 else
14793 {
14794 fip->fields.emplace_back ();
14795 new_field = &fip->fields.back ();
14796 }
14797
14798 new_field->offset = die->sect_off;
14799
14800 new_field->accessibility = dwarf2_access_attribute (die, cu);
14801 if (new_field->accessibility != DW_ACCESS_public)
14802 fip->non_public_fields = true;
14803
14804 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14805 if (attr != nullptr)
14806 new_field->virtuality = attr->as_virtuality ();
14807 else
14808 new_field->virtuality = DW_VIRTUALITY_none;
14809
14810 fp = &new_field->field;
14811
14812 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
14813 {
14814 /* Data member other than a C++ static data member. */
14815
14816 /* Get type of field. */
14817 fp->set_type (die_type (die, cu));
14818
14819 SET_FIELD_BITPOS (*fp, 0);
14820
14821 /* Get bit size of field (zero if none). */
14822 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
14823 if (attr != nullptr)
14824 {
14825 FIELD_BITSIZE (*fp) = attr->constant_value (0);
14826 }
14827 else
14828 {
14829 FIELD_BITSIZE (*fp) = 0;
14830 }
14831
14832 /* Get bit offset of field. */
14833 handle_data_member_location (die, cu, fp);
14834 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
14835 if (attr != nullptr && attr->form_is_constant ())
14836 {
14837 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
14838 {
14839 /* For big endian bits, the DW_AT_bit_offset gives the
14840 additional bit offset from the MSB of the containing
14841 anonymous object to the MSB of the field. We don't
14842 have to do anything special since we don't need to
14843 know the size of the anonymous object. */
14844 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14845 + attr->constant_value (0)));
14846 }
14847 else
14848 {
14849 /* For little endian bits, compute the bit offset to the
14850 MSB of the anonymous object, subtract off the number of
14851 bits from the MSB of the field to the MSB of the
14852 object, and then subtract off the number of bits of
14853 the field itself. The result is the bit offset of
14854 the LSB of the field. */
14855 int anonymous_size;
14856 int bit_offset = attr->constant_value (0);
14857
14858 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14859 if (attr != nullptr && attr->form_is_constant ())
14860 {
14861 /* The size of the anonymous object containing
14862 the bit field is explicit, so use the
14863 indicated size (in bytes). */
14864 anonymous_size = attr->constant_value (0);
14865 }
14866 else
14867 {
14868 /* The size of the anonymous object containing
14869 the bit field must be inferred from the type
14870 attribute of the data member containing the
14871 bit field. */
14872 anonymous_size = TYPE_LENGTH (fp->type ());
14873 }
14874 SET_FIELD_BITPOS (*fp,
14875 (FIELD_BITPOS (*fp)
14876 + anonymous_size * bits_per_byte
14877 - bit_offset - FIELD_BITSIZE (*fp)));
14878 }
14879 }
14880 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14881 if (attr != NULL)
14882 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14883 + attr->constant_value (0)));
14884
14885 /* Get name of field. */
14886 fieldname = dwarf2_name (die, cu);
14887 if (fieldname == NULL)
14888 fieldname = "";
14889
14890 /* The name is already allocated along with this objfile, so we don't
14891 need to duplicate it for the type. */
14892 fp->name = fieldname;
14893
14894 /* Change accessibility for artificial fields (e.g. virtual table
14895 pointer or virtual base class pointer) to private. */
14896 if (dwarf2_attr (die, DW_AT_artificial, cu))
14897 {
14898 FIELD_ARTIFICIAL (*fp) = 1;
14899 new_field->accessibility = DW_ACCESS_private;
14900 fip->non_public_fields = true;
14901 }
14902 }
14903 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
14904 {
14905 /* C++ static member. */
14906
14907 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14908 is a declaration, but all versions of G++ as of this writing
14909 (so through at least 3.2.1) incorrectly generate
14910 DW_TAG_variable tags. */
14911
14912 const char *physname;
14913
14914 /* Get name of field. */
14915 fieldname = dwarf2_name (die, cu);
14916 if (fieldname == NULL)
14917 return;
14918
14919 attr = dwarf2_attr (die, DW_AT_const_value, cu);
14920 if (attr
14921 /* Only create a symbol if this is an external value.
14922 new_symbol checks this and puts the value in the global symbol
14923 table, which we want. If it is not external, new_symbol
14924 will try to put the value in cu->list_in_scope which is wrong. */
14925 && dwarf2_flag_true_p (die, DW_AT_external, cu))
14926 {
14927 /* A static const member, not much different than an enum as far as
14928 we're concerned, except that we can support more types. */
14929 new_symbol (die, NULL, cu);
14930 }
14931
14932 /* Get physical name. */
14933 physname = dwarf2_physname (fieldname, die, cu);
14934
14935 /* The name is already allocated along with this objfile, so we don't
14936 need to duplicate it for the type. */
14937 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
14938 fp->set_type (die_type (die, cu));
14939 FIELD_NAME (*fp) = fieldname;
14940 }
14941 else if (die->tag == DW_TAG_inheritance)
14942 {
14943 /* C++ base class field. */
14944 handle_data_member_location (die, cu, fp);
14945 FIELD_BITSIZE (*fp) = 0;
14946 fp->set_type (die_type (die, cu));
14947 FIELD_NAME (*fp) = fp->type ()->name ();
14948 }
14949 else
14950 gdb_assert_not_reached ("missing case in dwarf2_add_field");
14951 }
14952
14953 /* Can the type given by DIE define another type? */
14954
14955 static bool
14956 type_can_define_types (const struct die_info *die)
14957 {
14958 switch (die->tag)
14959 {
14960 case DW_TAG_typedef:
14961 case DW_TAG_class_type:
14962 case DW_TAG_structure_type:
14963 case DW_TAG_union_type:
14964 case DW_TAG_enumeration_type:
14965 return true;
14966
14967 default:
14968 return false;
14969 }
14970 }
14971
14972 /* Add a type definition defined in the scope of the FIP's class. */
14973
14974 static void
14975 dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14976 struct dwarf2_cu *cu)
14977 {
14978 struct decl_field fp;
14979 memset (&fp, 0, sizeof (fp));
14980
14981 gdb_assert (type_can_define_types (die));
14982
14983 /* Get name of field. NULL is okay here, meaning an anonymous type. */
14984 fp.name = dwarf2_name (die, cu);
14985 fp.type = read_type_die (die, cu);
14986
14987 /* Save accessibility. */
14988 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
14989 switch (accessibility)
14990 {
14991 case DW_ACCESS_public:
14992 /* The assumed value if neither private nor protected. */
14993 break;
14994 case DW_ACCESS_private:
14995 fp.is_private = 1;
14996 break;
14997 case DW_ACCESS_protected:
14998 fp.is_protected = 1;
14999 break;
15000 }
15001
15002 if (die->tag == DW_TAG_typedef)
15003 fip->typedef_field_list.push_back (fp);
15004 else
15005 fip->nested_types_list.push_back (fp);
15006 }
15007
15008 /* A convenience typedef that's used when finding the discriminant
15009 field for a variant part. */
15010 typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15011 offset_map_type;
15012
15013 /* Compute the discriminant range for a given variant. OBSTACK is
15014 where the results will be stored. VARIANT is the variant to
15015 process. IS_UNSIGNED indicates whether the discriminant is signed
15016 or unsigned. */
15017
15018 static const gdb::array_view<discriminant_range>
15019 convert_variant_range (struct obstack *obstack, const variant_field &variant,
15020 bool is_unsigned)
15021 {
15022 std::vector<discriminant_range> ranges;
15023
15024 if (variant.default_branch)
15025 return {};
15026
15027 if (variant.discr_list_data == nullptr)
15028 {
15029 discriminant_range r
15030 = {variant.discriminant_value, variant.discriminant_value};
15031 ranges.push_back (r);
15032 }
15033 else
15034 {
15035 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15036 variant.discr_list_data->size);
15037 while (!data.empty ())
15038 {
15039 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15040 {
15041 complaint (_("invalid discriminant marker: %d"), data[0]);
15042 break;
15043 }
15044 bool is_range = data[0] == DW_DSC_range;
15045 data = data.slice (1);
15046
15047 ULONGEST low, high;
15048 unsigned int bytes_read;
15049
15050 if (data.empty ())
15051 {
15052 complaint (_("DW_AT_discr_list missing low value"));
15053 break;
15054 }
15055 if (is_unsigned)
15056 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15057 else
15058 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15059 &bytes_read);
15060 data = data.slice (bytes_read);
15061
15062 if (is_range)
15063 {
15064 if (data.empty ())
15065 {
15066 complaint (_("DW_AT_discr_list missing high value"));
15067 break;
15068 }
15069 if (is_unsigned)
15070 high = read_unsigned_leb128 (nullptr, data.data (),
15071 &bytes_read);
15072 else
15073 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15074 &bytes_read);
15075 data = data.slice (bytes_read);
15076 }
15077 else
15078 high = low;
15079
15080 ranges.push_back ({ low, high });
15081 }
15082 }
15083
15084 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15085 ranges.size ());
15086 std::copy (ranges.begin (), ranges.end (), result);
15087 return gdb::array_view<discriminant_range> (result, ranges.size ());
15088 }
15089
15090 static const gdb::array_view<variant_part> create_variant_parts
15091 (struct obstack *obstack,
15092 const offset_map_type &offset_map,
15093 struct field_info *fi,
15094 const std::vector<variant_part_builder> &variant_parts);
15095
15096 /* Fill in a "struct variant" for a given variant field. RESULT is
15097 the variant to fill in. OBSTACK is where any needed allocations
15098 will be done. OFFSET_MAP holds the mapping from section offsets to
15099 fields for the type. FI describes the fields of the type we're
15100 processing. FIELD is the variant field we're converting. */
15101
15102 static void
15103 create_one_variant (variant &result, struct obstack *obstack,
15104 const offset_map_type &offset_map,
15105 struct field_info *fi, const variant_field &field)
15106 {
15107 result.discriminants = convert_variant_range (obstack, field, false);
15108 result.first_field = field.first_field + fi->baseclasses.size ();
15109 result.last_field = field.last_field + fi->baseclasses.size ();
15110 result.parts = create_variant_parts (obstack, offset_map, fi,
15111 field.variant_parts);
15112 }
15113
15114 /* Fill in a "struct variant_part" for a given variant part. RESULT
15115 is the variant part to fill in. OBSTACK is where any needed
15116 allocations will be done. OFFSET_MAP holds the mapping from
15117 section offsets to fields for the type. FI describes the fields of
15118 the type we're processing. BUILDER is the variant part to be
15119 converted. */
15120
15121 static void
15122 create_one_variant_part (variant_part &result,
15123 struct obstack *obstack,
15124 const offset_map_type &offset_map,
15125 struct field_info *fi,
15126 const variant_part_builder &builder)
15127 {
15128 auto iter = offset_map.find (builder.discriminant_offset);
15129 if (iter == offset_map.end ())
15130 {
15131 result.discriminant_index = -1;
15132 /* Doesn't matter. */
15133 result.is_unsigned = false;
15134 }
15135 else
15136 {
15137 result.discriminant_index = iter->second;
15138 result.is_unsigned
15139 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
15140 }
15141
15142 size_t n = builder.variants.size ();
15143 variant *output = new (obstack) variant[n];
15144 for (size_t i = 0; i < n; ++i)
15145 create_one_variant (output[i], obstack, offset_map, fi,
15146 builder.variants[i]);
15147
15148 result.variants = gdb::array_view<variant> (output, n);
15149 }
15150
15151 /* Create a vector of variant parts that can be attached to a type.
15152 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15153 holds the mapping from section offsets to fields for the type. FI
15154 describes the fields of the type we're processing. VARIANT_PARTS
15155 is the vector to convert. */
15156
15157 static const gdb::array_view<variant_part>
15158 create_variant_parts (struct obstack *obstack,
15159 const offset_map_type &offset_map,
15160 struct field_info *fi,
15161 const std::vector<variant_part_builder> &variant_parts)
15162 {
15163 if (variant_parts.empty ())
15164 return {};
15165
15166 size_t n = variant_parts.size ();
15167 variant_part *result = new (obstack) variant_part[n];
15168 for (size_t i = 0; i < n; ++i)
15169 create_one_variant_part (result[i], obstack, offset_map, fi,
15170 variant_parts[i]);
15171
15172 return gdb::array_view<variant_part> (result, n);
15173 }
15174
15175 /* Compute the variant part vector for FIP, attaching it to TYPE when
15176 done. */
15177
15178 static void
15179 add_variant_property (struct field_info *fip, struct type *type,
15180 struct dwarf2_cu *cu)
15181 {
15182 /* Map section offsets of fields to their field index. Note the
15183 field index here does not take the number of baseclasses into
15184 account. */
15185 offset_map_type offset_map;
15186 for (int i = 0; i < fip->fields.size (); ++i)
15187 offset_map[fip->fields[i].offset] = i;
15188
15189 struct objfile *objfile = cu->per_objfile->objfile;
15190 gdb::array_view<variant_part> parts
15191 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15192 fip->variant_parts);
15193
15194 struct dynamic_prop prop;
15195 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15196 obstack_copy (&objfile->objfile_obstack, &parts,
15197 sizeof (parts)));
15198
15199 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
15200 }
15201
15202 /* Create the vector of fields, and attach it to the type. */
15203
15204 static void
15205 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
15206 struct dwarf2_cu *cu)
15207 {
15208 int nfields = fip->nfields ();
15209
15210 /* Record the field count, allocate space for the array of fields,
15211 and create blank accessibility bitfields if necessary. */
15212 type->set_num_fields (nfields);
15213 type->set_fields
15214 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
15215
15216 if (fip->non_public_fields && cu->language != language_ada)
15217 {
15218 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15219
15220 TYPE_FIELD_PRIVATE_BITS (type) =
15221 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15222 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15223
15224 TYPE_FIELD_PROTECTED_BITS (type) =
15225 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15226 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15227
15228 TYPE_FIELD_IGNORE_BITS (type) =
15229 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15230 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
15231 }
15232
15233 /* If the type has baseclasses, allocate and clear a bit vector for
15234 TYPE_FIELD_VIRTUAL_BITS. */
15235 if (!fip->baseclasses.empty () && cu->language != language_ada)
15236 {
15237 int num_bytes = B_BYTES (fip->baseclasses.size ());
15238 unsigned char *pointer;
15239
15240 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15241 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
15242 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
15243 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15244 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
15245 }
15246
15247 if (!fip->variant_parts.empty ())
15248 add_variant_property (fip, type, cu);
15249
15250 /* Copy the saved-up fields into the field vector. */
15251 for (int i = 0; i < nfields; ++i)
15252 {
15253 struct nextfield &field
15254 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15255 : fip->fields[i - fip->baseclasses.size ()]);
15256
15257 type->field (i) = field.field;
15258 switch (field.accessibility)
15259 {
15260 case DW_ACCESS_private:
15261 if (cu->language != language_ada)
15262 SET_TYPE_FIELD_PRIVATE (type, i);
15263 break;
15264
15265 case DW_ACCESS_protected:
15266 if (cu->language != language_ada)
15267 SET_TYPE_FIELD_PROTECTED (type, i);
15268 break;
15269
15270 case DW_ACCESS_public:
15271 break;
15272
15273 default:
15274 /* Unknown accessibility. Complain and treat it as public. */
15275 {
15276 complaint (_("unsupported accessibility %d"),
15277 field.accessibility);
15278 }
15279 break;
15280 }
15281 if (i < fip->baseclasses.size ())
15282 {
15283 switch (field.virtuality)
15284 {
15285 case DW_VIRTUALITY_virtual:
15286 case DW_VIRTUALITY_pure_virtual:
15287 if (cu->language == language_ada)
15288 error (_("unexpected virtuality in component of Ada type"));
15289 SET_TYPE_FIELD_VIRTUAL (type, i);
15290 break;
15291 }
15292 }
15293 }
15294 }
15295
15296 /* Return true if this member function is a constructor, false
15297 otherwise. */
15298
15299 static int
15300 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15301 {
15302 const char *fieldname;
15303 const char *type_name;
15304 int len;
15305
15306 if (die->parent == NULL)
15307 return 0;
15308
15309 if (die->parent->tag != DW_TAG_structure_type
15310 && die->parent->tag != DW_TAG_union_type
15311 && die->parent->tag != DW_TAG_class_type)
15312 return 0;
15313
15314 fieldname = dwarf2_name (die, cu);
15315 type_name = dwarf2_name (die->parent, cu);
15316 if (fieldname == NULL || type_name == NULL)
15317 return 0;
15318
15319 len = strlen (fieldname);
15320 return (strncmp (fieldname, type_name, len) == 0
15321 && (type_name[len] == '\0' || type_name[len] == '<'));
15322 }
15323
15324 /* Add a member function to the proper fieldlist. */
15325
15326 static void
15327 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
15328 struct type *type, struct dwarf2_cu *cu)
15329 {
15330 struct objfile *objfile = cu->per_objfile->objfile;
15331 struct attribute *attr;
15332 int i;
15333 struct fnfieldlist *flp = nullptr;
15334 struct fn_field *fnp;
15335 const char *fieldname;
15336 struct type *this_type;
15337
15338 if (cu->language == language_ada)
15339 error (_("unexpected member function in Ada type"));
15340
15341 /* Get name of member function. */
15342 fieldname = dwarf2_name (die, cu);
15343 if (fieldname == NULL)
15344 return;
15345
15346 /* Look up member function name in fieldlist. */
15347 for (i = 0; i < fip->fnfieldlists.size (); i++)
15348 {
15349 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
15350 {
15351 flp = &fip->fnfieldlists[i];
15352 break;
15353 }
15354 }
15355
15356 /* Create a new fnfieldlist if necessary. */
15357 if (flp == nullptr)
15358 {
15359 fip->fnfieldlists.emplace_back ();
15360 flp = &fip->fnfieldlists.back ();
15361 flp->name = fieldname;
15362 i = fip->fnfieldlists.size () - 1;
15363 }
15364
15365 /* Create a new member function field and add it to the vector of
15366 fnfieldlists. */
15367 flp->fnfields.emplace_back ();
15368 fnp = &flp->fnfields.back ();
15369
15370 /* Delay processing of the physname until later. */
15371 if (cu->language == language_cplus)
15372 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15373 die, cu);
15374 else
15375 {
15376 const char *physname = dwarf2_physname (fieldname, die, cu);
15377 fnp->physname = physname ? physname : "";
15378 }
15379
15380 fnp->type = alloc_type (objfile);
15381 this_type = read_type_die (die, cu);
15382 if (this_type && this_type->code () == TYPE_CODE_FUNC)
15383 {
15384 int nparams = this_type->num_fields ();
15385
15386 /* TYPE is the domain of this method, and THIS_TYPE is the type
15387 of the method itself (TYPE_CODE_METHOD). */
15388 smash_to_method_type (fnp->type, type,
15389 TYPE_TARGET_TYPE (this_type),
15390 this_type->fields (),
15391 this_type->num_fields (),
15392 this_type->has_varargs ());
15393
15394 /* Handle static member functions.
15395 Dwarf2 has no clean way to discern C++ static and non-static
15396 member functions. G++ helps GDB by marking the first
15397 parameter for non-static member functions (which is the this
15398 pointer) as artificial. We obtain this information from
15399 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
15400 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
15401 fnp->voffset = VOFFSET_STATIC;
15402 }
15403 else
15404 complaint (_("member function type missing for '%s'"),
15405 dwarf2_full_name (fieldname, die, cu));
15406
15407 /* Get fcontext from DW_AT_containing_type if present. */
15408 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
15409 fnp->fcontext = die_containing_type (die, cu);
15410
15411 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15412 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
15413
15414 /* Get accessibility. */
15415 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
15416 switch (accessibility)
15417 {
15418 case DW_ACCESS_private:
15419 fnp->is_private = 1;
15420 break;
15421 case DW_ACCESS_protected:
15422 fnp->is_protected = 1;
15423 break;
15424 }
15425
15426 /* Check for artificial methods. */
15427 attr = dwarf2_attr (die, DW_AT_artificial, cu);
15428 if (attr && attr->as_boolean ())
15429 fnp->is_artificial = 1;
15430
15431 /* Check for defaulted methods. */
15432 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15433 if (attr != nullptr)
15434 fnp->defaulted = attr->defaulted ();
15435
15436 /* Check for deleted methods. */
15437 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15438 if (attr != nullptr && attr->as_boolean ())
15439 fnp->is_deleted = 1;
15440
15441 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15442
15443 /* Get index in virtual function table if it is a virtual member
15444 function. For older versions of GCC, this is an offset in the
15445 appropriate virtual table, as specified by DW_AT_containing_type.
15446 For everyone else, it is an expression to be evaluated relative
15447 to the object address. */
15448
15449 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
15450 if (attr != nullptr)
15451 {
15452 if (attr->form_is_block () && attr->as_block ()->size > 0)
15453 {
15454 struct dwarf_block *block = attr->as_block ();
15455
15456 if (block->data[0] == DW_OP_constu)
15457 {
15458 /* Old-style GCC. */
15459 fnp->voffset = decode_locdesc (block, cu) + 2;
15460 }
15461 else if (block->data[0] == DW_OP_deref
15462 || (block->size > 1
15463 && block->data[0] == DW_OP_deref_size
15464 && block->data[1] == cu->header.addr_size))
15465 {
15466 fnp->voffset = decode_locdesc (block, cu);
15467 if ((fnp->voffset % cu->header.addr_size) != 0)
15468 dwarf2_complex_location_expr_complaint ();
15469 else
15470 fnp->voffset /= cu->header.addr_size;
15471 fnp->voffset += 2;
15472 }
15473 else
15474 dwarf2_complex_location_expr_complaint ();
15475
15476 if (!fnp->fcontext)
15477 {
15478 /* If there is no `this' field and no DW_AT_containing_type,
15479 we cannot actually find a base class context for the
15480 vtable! */
15481 if (this_type->num_fields () == 0
15482 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15483 {
15484 complaint (_("cannot determine context for virtual member "
15485 "function \"%s\" (offset %s)"),
15486 fieldname, sect_offset_str (die->sect_off));
15487 }
15488 else
15489 {
15490 fnp->fcontext
15491 = TYPE_TARGET_TYPE (this_type->field (0).type ());
15492 }
15493 }
15494 }
15495 else if (attr->form_is_section_offset ())
15496 {
15497 dwarf2_complex_location_expr_complaint ();
15498 }
15499 else
15500 {
15501 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15502 fieldname);
15503 }
15504 }
15505 else
15506 {
15507 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15508 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
15509 {
15510 /* GCC does this, as of 2008-08-25; PR debug/37237. */
15511 complaint (_("Member function \"%s\" (offset %s) is virtual "
15512 "but the vtable offset is not specified"),
15513 fieldname, sect_offset_str (die->sect_off));
15514 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15515 TYPE_CPLUS_DYNAMIC (type) = 1;
15516 }
15517 }
15518 }
15519
15520 /* Create the vector of member function fields, and attach it to the type. */
15521
15522 static void
15523 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
15524 struct dwarf2_cu *cu)
15525 {
15526 if (cu->language == language_ada)
15527 error (_("unexpected member functions in Ada type"));
15528
15529 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15530 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
15531 TYPE_ALLOC (type,
15532 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
15533
15534 for (int i = 0; i < fip->fnfieldlists.size (); i++)
15535 {
15536 struct fnfieldlist &nf = fip->fnfieldlists[i];
15537 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
15538
15539 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15540 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
15541 fn_flp->fn_fields = (struct fn_field *)
15542 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15543
15544 for (int k = 0; k < nf.fnfields.size (); ++k)
15545 fn_flp->fn_fields[k] = nf.fnfields[k];
15546 }
15547
15548 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
15549 }
15550
15551 /* Returns non-zero if NAME is the name of a vtable member in CU's
15552 language, zero otherwise. */
15553 static int
15554 is_vtable_name (const char *name, struct dwarf2_cu *cu)
15555 {
15556 static const char vptr[] = "_vptr";
15557
15558 /* Look for the C++ form of the vtable. */
15559 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
15560 return 1;
15561
15562 return 0;
15563 }
15564
15565 /* GCC outputs unnamed structures that are really pointers to member
15566 functions, with the ABI-specified layout. If TYPE describes
15567 such a structure, smash it into a member function type.
15568
15569 GCC shouldn't do this; it should just output pointer to member DIEs.
15570 This is GCC PR debug/28767. */
15571
15572 static void
15573 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
15574 {
15575 struct type *pfn_type, *self_type, *new_type;
15576
15577 /* Check for a structure with no name and two children. */
15578 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15579 return;
15580
15581 /* Check for __pfn and __delta members. */
15582 if (TYPE_FIELD_NAME (type, 0) == NULL
15583 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15584 || TYPE_FIELD_NAME (type, 1) == NULL
15585 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15586 return;
15587
15588 /* Find the type of the method. */
15589 pfn_type = type->field (0).type ();
15590 if (pfn_type == NULL
15591 || pfn_type->code () != TYPE_CODE_PTR
15592 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
15593 return;
15594
15595 /* Look for the "this" argument. */
15596 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15597 if (pfn_type->num_fields () == 0
15598 /* || pfn_type->field (0).type () == NULL */
15599 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
15600 return;
15601
15602 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
15603 new_type = alloc_type (objfile);
15604 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
15605 pfn_type->fields (), pfn_type->num_fields (),
15606 pfn_type->has_varargs ());
15607 smash_to_methodptr_type (type, new_type);
15608 }
15609
15610 /* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15611 requires rewriting, then copy it and return the updated copy.
15612 Otherwise return nullptr. */
15613
15614 static struct type *
15615 rewrite_array_type (struct type *type)
15616 {
15617 if (type->code () != TYPE_CODE_ARRAY)
15618 return nullptr;
15619
15620 struct type *index_type = type->index_type ();
15621 range_bounds *current_bounds = index_type->bounds ();
15622
15623 /* Handle multi-dimensional arrays. */
15624 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15625 if (new_target == nullptr)
15626 {
15627 /* Maybe we don't need to rewrite this array. */
15628 if (current_bounds->low.kind () == PROP_CONST
15629 && current_bounds->high.kind () == PROP_CONST)
15630 return nullptr;
15631 }
15632
15633 /* Either the target type was rewritten, or the bounds have to be
15634 updated. Either way we want to copy the type and update
15635 everything. */
15636 struct type *copy = copy_type (type);
15637 int nfields = copy->num_fields ();
15638 field *new_fields
15639 = ((struct field *) TYPE_ZALLOC (copy,
15640 nfields * sizeof (struct field)));
15641 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15642 copy->set_fields (new_fields);
15643 if (new_target != nullptr)
15644 TYPE_TARGET_TYPE (copy) = new_target;
15645
15646 struct type *index_copy = copy_type (index_type);
15647 range_bounds *bounds
15648 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15649 sizeof (range_bounds));
15650 *bounds = *current_bounds;
15651 bounds->low.set_const_val (1);
15652 bounds->high.set_const_val (0);
15653 index_copy->set_bounds (bounds);
15654 copy->set_index_type (index_copy);
15655
15656 return copy;
15657 }
15658
15659 /* While some versions of GCC will generate complicated DWARF for an
15660 array (see quirk_ada_thick_pointer), more recent versions were
15661 modified to emit an explicit thick pointer structure. However, in
15662 this case, the array still has DWARF expressions for its ranges,
15663 and these must be ignored. */
15664
15665 static void
15666 quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15667 struct type *type)
15668 {
15669 gdb_assert (cu->language == language_ada);
15670
15671 /* Check for a structure with two children. */
15672 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15673 return;
15674
15675 /* Check for P_ARRAY and P_BOUNDS members. */
15676 if (TYPE_FIELD_NAME (type, 0) == NULL
15677 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15678 || TYPE_FIELD_NAME (type, 1) == NULL
15679 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15680 return;
15681
15682 /* Make sure we're looking at a pointer to an array. */
15683 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15684 return;
15685
15686 /* The Ada code already knows how to handle these types, so all that
15687 we need to do is turn the bounds into static bounds. However, we
15688 don't want to rewrite existing array or index types in-place,
15689 because those may be referenced in other contexts where this
15690 rewriting is undesirable. */
15691 struct type *new_ary_type
15692 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15693 if (new_ary_type != nullptr)
15694 type->field (0).set_type (lookup_pointer_type (new_ary_type));
15695 }
15696
15697 /* If the DIE has a DW_AT_alignment attribute, return its value, doing
15698 appropriate error checking and issuing complaints if there is a
15699 problem. */
15700
15701 static ULONGEST
15702 get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15703 {
15704 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15705
15706 if (attr == nullptr)
15707 return 0;
15708
15709 if (!attr->form_is_constant ())
15710 {
15711 complaint (_("DW_AT_alignment must have constant form"
15712 " - DIE at %s [in module %s]"),
15713 sect_offset_str (die->sect_off),
15714 objfile_name (cu->per_objfile->objfile));
15715 return 0;
15716 }
15717
15718 LONGEST val = attr->constant_value (0);
15719 if (val < 0)
15720 {
15721 complaint (_("DW_AT_alignment value must not be negative"
15722 " - DIE at %s [in module %s]"),
15723 sect_offset_str (die->sect_off),
15724 objfile_name (cu->per_objfile->objfile));
15725 return 0;
15726 }
15727 ULONGEST align = val;
15728
15729 if (align == 0)
15730 {
15731 complaint (_("DW_AT_alignment value must not be zero"
15732 " - DIE at %s [in module %s]"),
15733 sect_offset_str (die->sect_off),
15734 objfile_name (cu->per_objfile->objfile));
15735 return 0;
15736 }
15737 if ((align & (align - 1)) != 0)
15738 {
15739 complaint (_("DW_AT_alignment value must be a power of 2"
15740 " - DIE at %s [in module %s]"),
15741 sect_offset_str (die->sect_off),
15742 objfile_name (cu->per_objfile->objfile));
15743 return 0;
15744 }
15745
15746 return align;
15747 }
15748
15749 /* If the DIE has a DW_AT_alignment attribute, use its value to set
15750 the alignment for TYPE. */
15751
15752 static void
15753 maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15754 struct type *type)
15755 {
15756 if (!set_type_align (type, get_alignment (cu, die)))
15757 complaint (_("DW_AT_alignment value too large"
15758 " - DIE at %s [in module %s]"),
15759 sect_offset_str (die->sect_off),
15760 objfile_name (cu->per_objfile->objfile));
15761 }
15762
15763 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15764 constant for a type, according to DWARF5 spec, Table 5.5. */
15765
15766 static bool
15767 is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15768 {
15769 switch (value)
15770 {
15771 case DW_CC_normal:
15772 case DW_CC_pass_by_reference:
15773 case DW_CC_pass_by_value:
15774 return true;
15775
15776 default:
15777 complaint (_("unrecognized DW_AT_calling_convention value "
15778 "(%s) for a type"), pulongest (value));
15779 return false;
15780 }
15781 }
15782
15783 /* Check if the given VALUE is a valid enum dwarf_calling_convention
15784 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15785 also according to GNU-specific values (see include/dwarf2.h). */
15786
15787 static bool
15788 is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15789 {
15790 switch (value)
15791 {
15792 case DW_CC_normal:
15793 case DW_CC_program:
15794 case DW_CC_nocall:
15795 return true;
15796
15797 case DW_CC_GNU_renesas_sh:
15798 case DW_CC_GNU_borland_fastcall_i386:
15799 case DW_CC_GDB_IBM_OpenCL:
15800 return true;
15801
15802 default:
15803 complaint (_("unrecognized DW_AT_calling_convention value "
15804 "(%s) for a subroutine"), pulongest (value));
15805 return false;
15806 }
15807 }
15808
15809 /* Called when we find the DIE that starts a structure or union scope
15810 (definition) to create a type for the structure or union. Fill in
15811 the type's name and general properties; the members will not be
15812 processed until process_structure_scope. A symbol table entry for
15813 the type will also not be done until process_structure_scope (assuming
15814 the type has a name).
15815
15816 NOTE: we need to call these functions regardless of whether or not the
15817 DIE has a DW_AT_name attribute, since it might be an anonymous
15818 structure or union. This gets the type entered into our set of
15819 user defined types. */
15820
15821 static struct type *
15822 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
15823 {
15824 struct objfile *objfile = cu->per_objfile->objfile;
15825 struct type *type;
15826 struct attribute *attr;
15827 const char *name;
15828
15829 /* If the definition of this type lives in .debug_types, read that type.
15830 Don't follow DW_AT_specification though, that will take us back up
15831 the chain and we want to go down. */
15832 attr = die->attr (DW_AT_signature);
15833 if (attr != nullptr)
15834 {
15835 type = get_DW_AT_signature_type (die, attr, cu);
15836
15837 /* The type's CU may not be the same as CU.
15838 Ensure TYPE is recorded with CU in die_type_hash. */
15839 return set_die_type (die, type, cu);
15840 }
15841
15842 type = alloc_type (objfile);
15843 INIT_CPLUS_SPECIFIC (type);
15844
15845 name = dwarf2_name (die, cu);
15846 if (name != NULL)
15847 {
15848 if (cu->language == language_cplus
15849 || cu->language == language_d
15850 || cu->language == language_rust)
15851 {
15852 const char *full_name = dwarf2_full_name (name, die, cu);
15853
15854 /* dwarf2_full_name might have already finished building the DIE's
15855 type. If so, there is no need to continue. */
15856 if (get_die_type (die, cu) != NULL)
15857 return get_die_type (die, cu);
15858
15859 type->set_name (full_name);
15860 }
15861 else
15862 {
15863 /* The name is already allocated along with this objfile, so
15864 we don't need to duplicate it for the type. */
15865 type->set_name (name);
15866 }
15867 }
15868
15869 if (die->tag == DW_TAG_structure_type)
15870 {
15871 type->set_code (TYPE_CODE_STRUCT);
15872 }
15873 else if (die->tag == DW_TAG_union_type)
15874 {
15875 type->set_code (TYPE_CODE_UNION);
15876 }
15877 else
15878 {
15879 type->set_code (TYPE_CODE_STRUCT);
15880 }
15881
15882 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15883 type->set_is_declared_class (true);
15884
15885 /* Store the calling convention in the type if it's available in
15886 the die. Otherwise the calling convention remains set to
15887 the default value DW_CC_normal. */
15888 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15889 if (attr != nullptr
15890 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
15891 {
15892 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15893 TYPE_CPLUS_CALLING_CONVENTION (type)
15894 = (enum dwarf_calling_convention) (attr->constant_value (0));
15895 }
15896
15897 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
15898 if (attr != nullptr)
15899 {
15900 if (attr->form_is_constant ())
15901 TYPE_LENGTH (type) = attr->constant_value (0);
15902 else
15903 {
15904 struct dynamic_prop prop;
15905 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
15906 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
15907 TYPE_LENGTH (type) = 0;
15908 }
15909 }
15910 else
15911 {
15912 TYPE_LENGTH (type) = 0;
15913 }
15914
15915 maybe_set_alignment (cu, die, type);
15916
15917 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
15918 {
15919 /* ICC<14 does not output the required DW_AT_declaration on
15920 incomplete types, but gives them a size of zero. */
15921 type->set_is_stub (true);
15922 }
15923 else
15924 type->set_stub_is_supported (true);
15925
15926 if (die_is_declaration (die, cu))
15927 type->set_is_stub (true);
15928 else if (attr == NULL && die->child == NULL
15929 && producer_is_realview (cu->producer))
15930 /* RealView does not output the required DW_AT_declaration
15931 on incomplete types. */
15932 type->set_is_stub (true);
15933
15934 /* We need to add the type field to the die immediately so we don't
15935 infinitely recurse when dealing with pointers to the structure
15936 type within the structure itself. */
15937 set_die_type (die, type, cu);
15938
15939 /* set_die_type should be already done. */
15940 set_descriptive_type (type, die, cu);
15941
15942 return type;
15943 }
15944
15945 static void handle_struct_member_die
15946 (struct die_info *child_die,
15947 struct type *type,
15948 struct field_info *fi,
15949 std::vector<struct symbol *> *template_args,
15950 struct dwarf2_cu *cu);
15951
15952 /* A helper for handle_struct_member_die that handles
15953 DW_TAG_variant_part. */
15954
15955 static void
15956 handle_variant_part (struct die_info *die, struct type *type,
15957 struct field_info *fi,
15958 std::vector<struct symbol *> *template_args,
15959 struct dwarf2_cu *cu)
15960 {
15961 variant_part_builder *new_part;
15962 if (fi->current_variant_part == nullptr)
15963 {
15964 fi->variant_parts.emplace_back ();
15965 new_part = &fi->variant_parts.back ();
15966 }
15967 else if (!fi->current_variant_part->processing_variant)
15968 {
15969 complaint (_("nested DW_TAG_variant_part seen "
15970 "- DIE at %s [in module %s]"),
15971 sect_offset_str (die->sect_off),
15972 objfile_name (cu->per_objfile->objfile));
15973 return;
15974 }
15975 else
15976 {
15977 variant_field &current = fi->current_variant_part->variants.back ();
15978 current.variant_parts.emplace_back ();
15979 new_part = &current.variant_parts.back ();
15980 }
15981
15982 /* When we recurse, we want callees to add to this new variant
15983 part. */
15984 scoped_restore save_current_variant_part
15985 = make_scoped_restore (&fi->current_variant_part, new_part);
15986
15987 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15988 if (discr == NULL)
15989 {
15990 /* It's a univariant form, an extension we support. */
15991 }
15992 else if (discr->form_is_ref ())
15993 {
15994 struct dwarf2_cu *target_cu = cu;
15995 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15996
15997 new_part->discriminant_offset = target_die->sect_off;
15998 }
15999 else
16000 {
16001 complaint (_("DW_AT_discr does not have DIE reference form"
16002 " - DIE at %s [in module %s]"),
16003 sect_offset_str (die->sect_off),
16004 objfile_name (cu->per_objfile->objfile));
16005 }
16006
16007 for (die_info *child_die = die->child;
16008 child_die != NULL;
16009 child_die = child_die->sibling)
16010 handle_struct_member_die (child_die, type, fi, template_args, cu);
16011 }
16012
16013 /* A helper for handle_struct_member_die that handles
16014 DW_TAG_variant. */
16015
16016 static void
16017 handle_variant (struct die_info *die, struct type *type,
16018 struct field_info *fi,
16019 std::vector<struct symbol *> *template_args,
16020 struct dwarf2_cu *cu)
16021 {
16022 if (fi->current_variant_part == nullptr)
16023 {
16024 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16025 "- DIE at %s [in module %s]"),
16026 sect_offset_str (die->sect_off),
16027 objfile_name (cu->per_objfile->objfile));
16028 return;
16029 }
16030 if (fi->current_variant_part->processing_variant)
16031 {
16032 complaint (_("nested DW_TAG_variant seen "
16033 "- DIE at %s [in module %s]"),
16034 sect_offset_str (die->sect_off),
16035 objfile_name (cu->per_objfile->objfile));
16036 return;
16037 }
16038
16039 scoped_restore save_processing_variant
16040 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16041 true);
16042
16043 fi->current_variant_part->variants.emplace_back ();
16044 variant_field &variant = fi->current_variant_part->variants.back ();
16045 variant.first_field = fi->fields.size ();
16046
16047 /* In a variant we want to get the discriminant and also add a
16048 field for our sole member child. */
16049 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
16050 if (discr == nullptr || !discr->form_is_constant ())
16051 {
16052 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
16053 if (discr == nullptr || discr->as_block ()->size == 0)
16054 variant.default_branch = true;
16055 else
16056 variant.discr_list_data = discr->as_block ();
16057 }
16058 else
16059 variant.discriminant_value = discr->constant_value (0);
16060
16061 for (die_info *variant_child = die->child;
16062 variant_child != NULL;
16063 variant_child = variant_child->sibling)
16064 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16065
16066 variant.last_field = fi->fields.size ();
16067 }
16068
16069 /* A helper for process_structure_scope that handles a single member
16070 DIE. */
16071
16072 static void
16073 handle_struct_member_die (struct die_info *child_die, struct type *type,
16074 struct field_info *fi,
16075 std::vector<struct symbol *> *template_args,
16076 struct dwarf2_cu *cu)
16077 {
16078 if (child_die->tag == DW_TAG_member
16079 || child_die->tag == DW_TAG_variable)
16080 {
16081 /* NOTE: carlton/2002-11-05: A C++ static data member
16082 should be a DW_TAG_member that is a declaration, but
16083 all versions of G++ as of this writing (so through at
16084 least 3.2.1) incorrectly generate DW_TAG_variable
16085 tags for them instead. */
16086 dwarf2_add_field (fi, child_die, cu);
16087 }
16088 else if (child_die->tag == DW_TAG_subprogram)
16089 {
16090 /* Rust doesn't have member functions in the C++ sense.
16091 However, it does emit ordinary functions as children
16092 of a struct DIE. */
16093 if (cu->language == language_rust)
16094 read_func_scope (child_die, cu);
16095 else
16096 {
16097 /* C++ member function. */
16098 dwarf2_add_member_fn (fi, child_die, type, cu);
16099 }
16100 }
16101 else if (child_die->tag == DW_TAG_inheritance)
16102 {
16103 /* C++ base class field. */
16104 dwarf2_add_field (fi, child_die, cu);
16105 }
16106 else if (type_can_define_types (child_die))
16107 dwarf2_add_type_defn (fi, child_die, cu);
16108 else if (child_die->tag == DW_TAG_template_type_param
16109 || child_die->tag == DW_TAG_template_value_param)
16110 {
16111 struct symbol *arg = new_symbol (child_die, NULL, cu);
16112
16113 if (arg != NULL)
16114 template_args->push_back (arg);
16115 }
16116 else if (child_die->tag == DW_TAG_variant_part)
16117 handle_variant_part (child_die, type, fi, template_args, cu);
16118 else if (child_die->tag == DW_TAG_variant)
16119 handle_variant (child_die, type, fi, template_args, cu);
16120 }
16121
16122 /* Finish creating a structure or union type, including filling in
16123 its members and creating a symbol for it. */
16124
16125 static void
16126 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16127 {
16128 struct objfile *objfile = cu->per_objfile->objfile;
16129 struct die_info *child_die;
16130 struct type *type;
16131
16132 type = get_die_type (die, cu);
16133 if (type == NULL)
16134 type = read_structure_type (die, cu);
16135
16136 bool has_template_parameters = false;
16137 if (die->child != NULL && ! die_is_declaration (die, cu))
16138 {
16139 struct field_info fi;
16140 std::vector<struct symbol *> template_args;
16141
16142 child_die = die->child;
16143
16144 while (child_die && child_die->tag)
16145 {
16146 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
16147 child_die = child_die->sibling;
16148 }
16149
16150 /* Attach template arguments to type. */
16151 if (!template_args.empty ())
16152 {
16153 has_template_parameters = true;
16154 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16155 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
16156 TYPE_TEMPLATE_ARGUMENTS (type)
16157 = XOBNEWVEC (&objfile->objfile_obstack,
16158 struct symbol *,
16159 TYPE_N_TEMPLATE_ARGUMENTS (type));
16160 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
16161 template_args.data (),
16162 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16163 * sizeof (struct symbol *)));
16164 }
16165
16166 /* Attach fields and member functions to the type. */
16167 if (fi.nfields () > 0)
16168 dwarf2_attach_fields_to_type (&fi, type, cu);
16169 if (!fi.fnfieldlists.empty ())
16170 {
16171 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
16172
16173 /* Get the type which refers to the base class (possibly this
16174 class itself) which contains the vtable pointer for the current
16175 class from the DW_AT_containing_type attribute. This use of
16176 DW_AT_containing_type is a GNU extension. */
16177
16178 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
16179 {
16180 struct type *t = die_containing_type (die, cu);
16181
16182 set_type_vptr_basetype (type, t);
16183 if (type == t)
16184 {
16185 int i;
16186
16187 /* Our own class provides vtbl ptr. */
16188 for (i = t->num_fields () - 1;
16189 i >= TYPE_N_BASECLASSES (t);
16190 --i)
16191 {
16192 const char *fieldname = TYPE_FIELD_NAME (t, i);
16193
16194 if (is_vtable_name (fieldname, cu))
16195 {
16196 set_type_vptr_fieldno (type, i);
16197 break;
16198 }
16199 }
16200
16201 /* Complain if virtual function table field not found. */
16202 if (i < TYPE_N_BASECLASSES (t))
16203 complaint (_("virtual function table pointer "
16204 "not found when defining class '%s'"),
16205 type->name () ? type->name () : "");
16206 }
16207 else
16208 {
16209 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
16210 }
16211 }
16212 else if (cu->producer
16213 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
16214 {
16215 /* The IBM XLC compiler does not provide direct indication
16216 of the containing type, but the vtable pointer is
16217 always named __vfp. */
16218
16219 int i;
16220
16221 for (i = type->num_fields () - 1;
16222 i >= TYPE_N_BASECLASSES (type);
16223 --i)
16224 {
16225 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16226 {
16227 set_type_vptr_fieldno (type, i);
16228 set_type_vptr_basetype (type, type);
16229 break;
16230 }
16231 }
16232 }
16233 }
16234
16235 /* Copy fi.typedef_field_list linked list elements content into the
16236 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
16237 if (!fi.typedef_field_list.empty ())
16238 {
16239 int count = fi.typedef_field_list.size ();
16240
16241 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16242 TYPE_TYPEDEF_FIELD_ARRAY (type)
16243 = ((struct decl_field *)
16244 TYPE_ALLOC (type,
16245 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16246 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
16247
16248 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16249 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
16250 }
16251
16252 /* Copy fi.nested_types_list linked list elements content into the
16253 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
16254 if (!fi.nested_types_list.empty () && cu->language != language_ada)
16255 {
16256 int count = fi.nested_types_list.size ();
16257
16258 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16259 TYPE_NESTED_TYPES_ARRAY (type)
16260 = ((struct decl_field *)
16261 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16262 TYPE_NESTED_TYPES_COUNT (type) = count;
16263
16264 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16265 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
16266 }
16267 }
16268
16269 quirk_gcc_member_function_pointer (type, objfile);
16270 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16271 cu->rust_unions.push_back (type);
16272 else if (cu->language == language_ada)
16273 quirk_ada_thick_pointer_struct (die, cu, type);
16274
16275 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16276 snapshots) has been known to create a die giving a declaration
16277 for a class that has, as a child, a die giving a definition for a
16278 nested class. So we have to process our children even if the
16279 current die is a declaration. Normally, of course, a declaration
16280 won't have any children at all. */
16281
16282 child_die = die->child;
16283
16284 while (child_die != NULL && child_die->tag)
16285 {
16286 if (child_die->tag == DW_TAG_member
16287 || child_die->tag == DW_TAG_variable
16288 || child_die->tag == DW_TAG_inheritance
16289 || child_die->tag == DW_TAG_template_value_param
16290 || child_die->tag == DW_TAG_template_type_param)
16291 {
16292 /* Do nothing. */
16293 }
16294 else
16295 process_die (child_die, cu);
16296
16297 child_die = child_die->sibling;
16298 }
16299
16300 /* Do not consider external references. According to the DWARF standard,
16301 these DIEs are identified by the fact that they have no byte_size
16302 attribute, and a declaration attribute. */
16303 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16304 || !die_is_declaration (die, cu)
16305 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
16306 {
16307 struct symbol *sym = new_symbol (die, type, cu);
16308
16309 if (has_template_parameters)
16310 {
16311 struct symtab *symtab;
16312 if (sym != nullptr)
16313 symtab = symbol_symtab (sym);
16314 else if (cu->line_header != nullptr)
16315 {
16316 /* Any related symtab will do. */
16317 symtab
16318 = cu->line_header->file_names ()[0].symtab;
16319 }
16320 else
16321 {
16322 symtab = nullptr;
16323 complaint (_("could not find suitable "
16324 "symtab for template parameter"
16325 " - DIE at %s [in module %s]"),
16326 sect_offset_str (die->sect_off),
16327 objfile_name (objfile));
16328 }
16329
16330 if (symtab != nullptr)
16331 {
16332 /* Make sure that the symtab is set on the new symbols.
16333 Even though they don't appear in this symtab directly,
16334 other parts of gdb assume that symbols do, and this is
16335 reasonably true. */
16336 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16337 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16338 }
16339 }
16340 }
16341 }
16342
16343 /* Assuming DIE is an enumeration type, and TYPE is its associated
16344 type, update TYPE using some information only available in DIE's
16345 children. In particular, the fields are computed. */
16346
16347 static void
16348 update_enumeration_type_from_children (struct die_info *die,
16349 struct type *type,
16350 struct dwarf2_cu *cu)
16351 {
16352 struct die_info *child_die;
16353 int unsigned_enum = 1;
16354 int flag_enum = 1;
16355
16356 auto_obstack obstack;
16357 std::vector<struct field> fields;
16358
16359 for (child_die = die->child;
16360 child_die != NULL && child_die->tag;
16361 child_die = child_die->sibling)
16362 {
16363 struct attribute *attr;
16364 LONGEST value;
16365 const gdb_byte *bytes;
16366 struct dwarf2_locexpr_baton *baton;
16367 const char *name;
16368
16369 if (child_die->tag != DW_TAG_enumerator)
16370 continue;
16371
16372 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16373 if (attr == NULL)
16374 continue;
16375
16376 name = dwarf2_name (child_die, cu);
16377 if (name == NULL)
16378 name = "<anonymous enumerator>";
16379
16380 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16381 &value, &bytes, &baton);
16382 if (value < 0)
16383 {
16384 unsigned_enum = 0;
16385 flag_enum = 0;
16386 }
16387 else
16388 {
16389 if (count_one_bits_ll (value) >= 2)
16390 flag_enum = 0;
16391 }
16392
16393 fields.emplace_back ();
16394 struct field &field = fields.back ();
16395 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16396 SET_FIELD_ENUMVAL (field, value);
16397 }
16398
16399 if (!fields.empty ())
16400 {
16401 type->set_num_fields (fields.size ());
16402 type->set_fields
16403 ((struct field *)
16404 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
16405 memcpy (type->fields (), fields.data (),
16406 sizeof (struct field) * fields.size ());
16407 }
16408
16409 if (unsigned_enum)
16410 type->set_is_unsigned (true);
16411
16412 if (flag_enum)
16413 type->set_is_flag_enum (true);
16414 }
16415
16416 /* Given a DW_AT_enumeration_type die, set its type. We do not
16417 complete the type's fields yet, or create any symbols. */
16418
16419 static struct type *
16420 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
16421 {
16422 struct objfile *objfile = cu->per_objfile->objfile;
16423 struct type *type;
16424 struct attribute *attr;
16425 const char *name;
16426
16427 /* If the definition of this type lives in .debug_types, read that type.
16428 Don't follow DW_AT_specification though, that will take us back up
16429 the chain and we want to go down. */
16430 attr = die->attr (DW_AT_signature);
16431 if (attr != nullptr)
16432 {
16433 type = get_DW_AT_signature_type (die, attr, cu);
16434
16435 /* The type's CU may not be the same as CU.
16436 Ensure TYPE is recorded with CU in die_type_hash. */
16437 return set_die_type (die, type, cu);
16438 }
16439
16440 type = alloc_type (objfile);
16441
16442 type->set_code (TYPE_CODE_ENUM);
16443 name = dwarf2_full_name (NULL, die, cu);
16444 if (name != NULL)
16445 type->set_name (name);
16446
16447 attr = dwarf2_attr (die, DW_AT_type, cu);
16448 if (attr != NULL)
16449 {
16450 struct type *underlying_type = die_type (die, cu);
16451
16452 TYPE_TARGET_TYPE (type) = underlying_type;
16453 }
16454
16455 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16456 if (attr != nullptr)
16457 {
16458 TYPE_LENGTH (type) = attr->constant_value (0);
16459 }
16460 else
16461 {
16462 TYPE_LENGTH (type) = 0;
16463 }
16464
16465 maybe_set_alignment (cu, die, type);
16466
16467 /* The enumeration DIE can be incomplete. In Ada, any type can be
16468 declared as private in the package spec, and then defined only
16469 inside the package body. Such types are known as Taft Amendment
16470 Types. When another package uses such a type, an incomplete DIE
16471 may be generated by the compiler. */
16472 if (die_is_declaration (die, cu))
16473 type->set_is_stub (true);
16474
16475 /* If this type has an underlying type that is not a stub, then we
16476 may use its attributes. We always use the "unsigned" attribute
16477 in this situation, because ordinarily we guess whether the type
16478 is unsigned -- but the guess can be wrong and the underlying type
16479 can tell us the reality. However, we defer to a local size
16480 attribute if one exists, because this lets the compiler override
16481 the underlying type if needed. */
16482 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
16483 {
16484 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16485 underlying_type = check_typedef (underlying_type);
16486
16487 type->set_is_unsigned (underlying_type->is_unsigned ());
16488
16489 if (TYPE_LENGTH (type) == 0)
16490 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
16491
16492 if (TYPE_RAW_ALIGN (type) == 0
16493 && TYPE_RAW_ALIGN (underlying_type) != 0)
16494 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
16495 }
16496
16497 type->set_is_declared_class (dwarf2_flag_true_p (die, DW_AT_enum_class, cu));
16498
16499 set_die_type (die, type, cu);
16500
16501 /* Finish the creation of this type by using the enum's children.
16502 Note that, as usual, this must come after set_die_type to avoid
16503 infinite recursion when trying to compute the names of the
16504 enumerators. */
16505 update_enumeration_type_from_children (die, type, cu);
16506
16507 return type;
16508 }
16509
16510 /* Given a pointer to a die which begins an enumeration, process all
16511 the dies that define the members of the enumeration, and create the
16512 symbol for the enumeration type.
16513
16514 NOTE: We reverse the order of the element list. */
16515
16516 static void
16517 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16518 {
16519 struct type *this_type;
16520
16521 this_type = get_die_type (die, cu);
16522 if (this_type == NULL)
16523 this_type = read_enumeration_type (die, cu);
16524
16525 if (die->child != NULL)
16526 {
16527 struct die_info *child_die;
16528 const char *name;
16529
16530 child_die = die->child;
16531 while (child_die && child_die->tag)
16532 {
16533 if (child_die->tag != DW_TAG_enumerator)
16534 {
16535 process_die (child_die, cu);
16536 }
16537 else
16538 {
16539 name = dwarf2_name (child_die, cu);
16540 if (name)
16541 new_symbol (child_die, this_type, cu);
16542 }
16543
16544 child_die = child_die->sibling;
16545 }
16546 }
16547
16548 /* If we are reading an enum from a .debug_types unit, and the enum
16549 is a declaration, and the enum is not the signatured type in the
16550 unit, then we do not want to add a symbol for it. Adding a
16551 symbol would in some cases obscure the true definition of the
16552 enum, giving users an incomplete type when the definition is
16553 actually available. Note that we do not want to do this for all
16554 enums which are just declarations, because C++0x allows forward
16555 enum declarations. */
16556 if (cu->per_cu->is_debug_types
16557 && die_is_declaration (die, cu))
16558 {
16559 struct signatured_type *sig_type;
16560
16561 sig_type = (struct signatured_type *) cu->per_cu;
16562 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16563 if (sig_type->type_offset_in_section != die->sect_off)
16564 return;
16565 }
16566
16567 new_symbol (die, this_type, cu);
16568 }
16569
16570 /* Helper function for quirk_ada_thick_pointer that examines a bounds
16571 expression for an index type and finds the corresponding field
16572 offset in the hidden "P_BOUNDS" structure. Returns true on success
16573 and updates *FIELD, false if it fails to recognize an
16574 expression. */
16575
16576 static bool
16577 recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16578 int *bounds_offset, struct field *field,
16579 struct dwarf2_cu *cu)
16580 {
16581 struct attribute *attr = dwarf2_attr (die, name, cu);
16582 if (attr == nullptr || !attr->form_is_block ())
16583 return false;
16584
16585 const struct dwarf_block *block = attr->as_block ();
16586 const gdb_byte *start = block->data;
16587 const gdb_byte *end = block->data + block->size;
16588
16589 /* The expression to recognize generally looks like:
16590
16591 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16592 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16593
16594 However, the second "plus_uconst" may be missing:
16595
16596 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16597 DW_OP_deref_size: 4)
16598
16599 This happens when the field is at the start of the structure.
16600
16601 Also, the final deref may not be sized:
16602
16603 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16604 DW_OP_deref)
16605
16606 This happens when the size of the index type happens to be the
16607 same as the architecture's word size. This can occur with or
16608 without the second plus_uconst. */
16609
16610 if (end - start < 2)
16611 return false;
16612 if (*start++ != DW_OP_push_object_address)
16613 return false;
16614 if (*start++ != DW_OP_plus_uconst)
16615 return false;
16616
16617 uint64_t this_bound_off;
16618 start = gdb_read_uleb128 (start, end, &this_bound_off);
16619 if (start == nullptr || (int) this_bound_off != this_bound_off)
16620 return false;
16621 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16622 is consistent among all bounds. */
16623 if (*bounds_offset == -1)
16624 *bounds_offset = this_bound_off;
16625 else if (*bounds_offset != this_bound_off)
16626 return false;
16627
16628 if (start == end || *start++ != DW_OP_deref)
16629 return false;
16630
16631 int offset = 0;
16632 if (start ==end)
16633 return false;
16634 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16635 {
16636 /* This means an offset of 0. */
16637 }
16638 else if (*start++ != DW_OP_plus_uconst)
16639 return false;
16640 else
16641 {
16642 /* The size is the parameter to DW_OP_plus_uconst. */
16643 uint64_t val;
16644 start = gdb_read_uleb128 (start, end, &val);
16645 if (start == nullptr)
16646 return false;
16647 if ((int) val != val)
16648 return false;
16649 offset = val;
16650 }
16651
16652 if (start == end)
16653 return false;
16654
16655 uint64_t size;
16656 if (*start == DW_OP_deref_size)
16657 {
16658 start = gdb_read_uleb128 (start + 1, end, &size);
16659 if (start == nullptr)
16660 return false;
16661 }
16662 else if (*start == DW_OP_deref)
16663 {
16664 size = cu->header.addr_size;
16665 ++start;
16666 }
16667 else
16668 return false;
16669
16670 SET_FIELD_BITPOS (*field, 8 * offset);
16671 if (size != TYPE_LENGTH (field->type ()))
16672 FIELD_BITSIZE (*field) = 8 * size;
16673
16674 return true;
16675 }
16676
16677 /* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16678 some kinds of Ada arrays:
16679
16680 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16681 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16682 <11e0> DW_AT_data_location: 2 byte block: 97 6
16683 (DW_OP_push_object_address; DW_OP_deref)
16684 <11e3> DW_AT_type : <0x1173>
16685 <11e7> DW_AT_sibling : <0x1201>
16686 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16687 <11ec> DW_AT_type : <0x1206>
16688 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16689 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16690 DW_OP_deref_size: 4)
16691 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16692 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16693 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16694
16695 This actually represents a "thick pointer", which is a structure
16696 with two elements: one that is a pointer to the array data, and one
16697 that is a pointer to another structure; this second structure holds
16698 the array bounds.
16699
16700 This returns a new type on success, or nullptr if this didn't
16701 recognize the type. */
16702
16703 static struct type *
16704 quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16705 struct type *type)
16706 {
16707 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16708 /* So far we've only seen this with block form. */
16709 if (attr == nullptr || !attr->form_is_block ())
16710 return nullptr;
16711
16712 /* Note that this will fail if the structure layout is changed by
16713 the compiler. However, we have no good way to recognize some
16714 other layout, because we don't know what expression the compiler
16715 might choose to emit should this happen. */
16716 struct dwarf_block *blk = attr->as_block ();
16717 if (blk->size != 2
16718 || blk->data[0] != DW_OP_push_object_address
16719 || blk->data[1] != DW_OP_deref)
16720 return nullptr;
16721
16722 int bounds_offset = -1;
16723 int max_align = -1;
16724 std::vector<struct field> range_fields;
16725 for (struct die_info *child_die = die->child;
16726 child_die;
16727 child_die = child_die->sibling)
16728 {
16729 if (child_die->tag == DW_TAG_subrange_type)
16730 {
16731 struct type *underlying = read_subrange_index_type (child_die, cu);
16732
16733 int this_align = type_align (underlying);
16734 if (this_align > max_align)
16735 max_align = this_align;
16736
16737 range_fields.emplace_back ();
16738 range_fields.emplace_back ();
16739
16740 struct field &lower = range_fields[range_fields.size () - 2];
16741 struct field &upper = range_fields[range_fields.size () - 1];
16742
16743 lower.set_type (underlying);
16744 FIELD_ARTIFICIAL (lower) = 1;
16745
16746 upper.set_type (underlying);
16747 FIELD_ARTIFICIAL (upper) = 1;
16748
16749 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16750 &bounds_offset, &lower, cu)
16751 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16752 &bounds_offset, &upper, cu))
16753 return nullptr;
16754 }
16755 }
16756
16757 /* This shouldn't really happen, but double-check that we found
16758 where the bounds are stored. */
16759 if (bounds_offset == -1)
16760 return nullptr;
16761
16762 struct objfile *objfile = cu->per_objfile->objfile;
16763 for (int i = 0; i < range_fields.size (); i += 2)
16764 {
16765 char name[20];
16766
16767 /* Set the name of each field in the bounds. */
16768 xsnprintf (name, sizeof (name), "LB%d", i / 2);
16769 FIELD_NAME (range_fields[i]) = objfile->intern (name);
16770 xsnprintf (name, sizeof (name), "UB%d", i / 2);
16771 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
16772 }
16773
16774 struct type *bounds = alloc_type (objfile);
16775 bounds->set_code (TYPE_CODE_STRUCT);
16776
16777 bounds->set_num_fields (range_fields.size ());
16778 bounds->set_fields
16779 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
16780 * sizeof (struct field))));
16781 memcpy (bounds->fields (), range_fields.data (),
16782 bounds->num_fields () * sizeof (struct field));
16783
16784 int last_fieldno = range_fields.size () - 1;
16785 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
16786 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
16787 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
16788
16789 /* Rewrite the existing array type in place. Specifically, we
16790 remove any dynamic properties we might have read, and we replace
16791 the index types. */
16792 struct type *iter = type;
16793 for (int i = 0; i < range_fields.size (); i += 2)
16794 {
16795 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
16796 iter->main_type->dyn_prop_list = nullptr;
16797 iter->set_index_type
16798 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
16799 iter = TYPE_TARGET_TYPE (iter);
16800 }
16801
16802 struct type *result = alloc_type (objfile);
16803 result->set_code (TYPE_CODE_STRUCT);
16804
16805 result->set_num_fields (2);
16806 result->set_fields
16807 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
16808 * sizeof (struct field))));
16809
16810 /* The names are chosen to coincide with what the compiler does with
16811 -fgnat-encodings=all, which the Ada code in gdb already
16812 understands. */
16813 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
16814 result->field (0).set_type (lookup_pointer_type (type));
16815
16816 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
16817 result->field (1).set_type (lookup_pointer_type (bounds));
16818 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
16819
16820 result->set_name (type->name ());
16821 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
16822 + TYPE_LENGTH (result->field (1).type ()));
16823
16824 return result;
16825 }
16826
16827 /* Extract all information from a DW_TAG_array_type DIE and put it in
16828 the DIE's type field. For now, this only handles one dimensional
16829 arrays. */
16830
16831 static struct type *
16832 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
16833 {
16834 struct objfile *objfile = cu->per_objfile->objfile;
16835 struct die_info *child_die;
16836 struct type *type;
16837 struct type *element_type, *range_type, *index_type;
16838 struct attribute *attr;
16839 const char *name;
16840 struct dynamic_prop *byte_stride_prop = NULL;
16841 unsigned int bit_stride = 0;
16842
16843 element_type = die_type (die, cu);
16844
16845 /* The die_type call above may have already set the type for this DIE. */
16846 type = get_die_type (die, cu);
16847 if (type)
16848 return type;
16849
16850 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16851 if (attr != NULL)
16852 {
16853 int stride_ok;
16854 struct type *prop_type = cu->addr_sized_int_type (false);
16855
16856 byte_stride_prop
16857 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16858 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16859 prop_type);
16860 if (!stride_ok)
16861 {
16862 complaint (_("unable to read array DW_AT_byte_stride "
16863 " - DIE at %s [in module %s]"),
16864 sect_offset_str (die->sect_off),
16865 objfile_name (cu->per_objfile->objfile));
16866 /* Ignore this attribute. We will likely not be able to print
16867 arrays of this type correctly, but there is little we can do
16868 to help if we cannot read the attribute's value. */
16869 byte_stride_prop = NULL;
16870 }
16871 }
16872
16873 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16874 if (attr != NULL)
16875 bit_stride = attr->constant_value (0);
16876
16877 /* Irix 6.2 native cc creates array types without children for
16878 arrays with unspecified length. */
16879 if (die->child == NULL)
16880 {
16881 index_type = objfile_type (objfile)->builtin_int;
16882 range_type = create_static_range_type (NULL, index_type, 0, -1);
16883 type = create_array_type_with_stride (NULL, element_type, range_type,
16884 byte_stride_prop, bit_stride);
16885 return set_die_type (die, type, cu);
16886 }
16887
16888 std::vector<struct type *> range_types;
16889 child_die = die->child;
16890 while (child_die && child_die->tag)
16891 {
16892 if (child_die->tag == DW_TAG_subrange_type)
16893 {
16894 struct type *child_type = read_type_die (child_die, cu);
16895
16896 if (child_type != NULL)
16897 {
16898 /* The range type was succesfully read. Save it for the
16899 array type creation. */
16900 range_types.push_back (child_type);
16901 }
16902 }
16903 child_die = child_die->sibling;
16904 }
16905
16906 if (range_types.empty ())
16907 {
16908 complaint (_("unable to find array range - DIE at %s [in module %s]"),
16909 sect_offset_str (die->sect_off),
16910 objfile_name (cu->per_objfile->objfile));
16911 return NULL;
16912 }
16913
16914 /* Dwarf2 dimensions are output from left to right, create the
16915 necessary array types in backwards order. */
16916
16917 type = element_type;
16918
16919 if (read_array_order (die, cu) == DW_ORD_col_major)
16920 {
16921 int i = 0;
16922
16923 while (i < range_types.size ())
16924 {
16925 type = create_array_type_with_stride (NULL, type, range_types[i++],
16926 byte_stride_prop, bit_stride);
16927 bit_stride = 0;
16928 byte_stride_prop = nullptr;
16929 }
16930 }
16931 else
16932 {
16933 size_t ndim = range_types.size ();
16934 while (ndim-- > 0)
16935 {
16936 type = create_array_type_with_stride (NULL, type, range_types[ndim],
16937 byte_stride_prop, bit_stride);
16938 bit_stride = 0;
16939 byte_stride_prop = nullptr;
16940 }
16941 }
16942
16943 gdb_assert (type != element_type);
16944
16945 /* Understand Dwarf2 support for vector types (like they occur on
16946 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16947 array type. This is not part of the Dwarf2/3 standard yet, but a
16948 custom vendor extension. The main difference between a regular
16949 array and the vector variant is that vectors are passed by value
16950 to functions. */
16951 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
16952 if (attr != nullptr)
16953 make_vector_type (type);
16954
16955 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16956 implementation may choose to implement triple vectors using this
16957 attribute. */
16958 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16959 if (attr != nullptr && attr->form_is_unsigned ())
16960 {
16961 if (attr->as_unsigned () >= TYPE_LENGTH (type))
16962 TYPE_LENGTH (type) = attr->as_unsigned ();
16963 else
16964 complaint (_("DW_AT_byte_size for array type smaller "
16965 "than the total size of elements"));
16966 }
16967
16968 name = dwarf2_name (die, cu);
16969 if (name)
16970 type->set_name (name);
16971
16972 maybe_set_alignment (cu, die, type);
16973
16974 struct type *replacement_type = nullptr;
16975 if (cu->language == language_ada)
16976 {
16977 replacement_type = quirk_ada_thick_pointer (die, cu, type);
16978 if (replacement_type != nullptr)
16979 type = replacement_type;
16980 }
16981
16982 /* Install the type in the die. */
16983 set_die_type (die, type, cu, replacement_type != nullptr);
16984
16985 /* set_die_type should be already done. */
16986 set_descriptive_type (type, die, cu);
16987
16988 return type;
16989 }
16990
16991 static enum dwarf_array_dim_ordering
16992 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
16993 {
16994 struct attribute *attr;
16995
16996 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16997
16998 if (attr != nullptr)
16999 {
17000 LONGEST val = attr->constant_value (-1);
17001 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17002 return (enum dwarf_array_dim_ordering) val;
17003 }
17004
17005 /* GNU F77 is a special case, as at 08/2004 array type info is the
17006 opposite order to the dwarf2 specification, but data is still
17007 laid out as per normal fortran.
17008
17009 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17010 version checking. */
17011
17012 if (cu->language == language_fortran
17013 && cu->producer && strstr (cu->producer, "GNU F77"))
17014 {
17015 return DW_ORD_row_major;
17016 }
17017
17018 switch (cu->language_defn->array_ordering ())
17019 {
17020 case array_column_major:
17021 return DW_ORD_col_major;
17022 case array_row_major:
17023 default:
17024 return DW_ORD_row_major;
17025 };
17026 }
17027
17028 /* Extract all information from a DW_TAG_set_type DIE and put it in
17029 the DIE's type field. */
17030
17031 static struct type *
17032 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17033 {
17034 struct type *domain_type, *set_type;
17035 struct attribute *attr;
17036
17037 domain_type = die_type (die, cu);
17038
17039 /* The die_type call above may have already set the type for this DIE. */
17040 set_type = get_die_type (die, cu);
17041 if (set_type)
17042 return set_type;
17043
17044 set_type = create_set_type (NULL, domain_type);
17045
17046 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17047 if (attr != nullptr && attr->form_is_unsigned ())
17048 TYPE_LENGTH (set_type) = attr->as_unsigned ();
17049
17050 maybe_set_alignment (cu, die, set_type);
17051
17052 return set_die_type (die, set_type, cu);
17053 }
17054
17055 /* A helper for read_common_block that creates a locexpr baton.
17056 SYM is the symbol which we are marking as computed.
17057 COMMON_DIE is the DIE for the common block.
17058 COMMON_LOC is the location expression attribute for the common
17059 block itself.
17060 MEMBER_LOC is the location expression attribute for the particular
17061 member of the common block that we are processing.
17062 CU is the CU from which the above come. */
17063
17064 static void
17065 mark_common_block_symbol_computed (struct symbol *sym,
17066 struct die_info *common_die,
17067 struct attribute *common_loc,
17068 struct attribute *member_loc,
17069 struct dwarf2_cu *cu)
17070 {
17071 dwarf2_per_objfile *per_objfile = cu->per_objfile;
17072 struct objfile *objfile = per_objfile->objfile;
17073 struct dwarf2_locexpr_baton *baton;
17074 gdb_byte *ptr;
17075 unsigned int cu_off;
17076 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
17077 LONGEST offset = 0;
17078
17079 gdb_assert (common_loc && member_loc);
17080 gdb_assert (common_loc->form_is_block ());
17081 gdb_assert (member_loc->form_is_block ()
17082 || member_loc->form_is_constant ());
17083
17084 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
17085 baton->per_objfile = per_objfile;
17086 baton->per_cu = cu->per_cu;
17087 gdb_assert (baton->per_cu);
17088
17089 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17090
17091 if (member_loc->form_is_constant ())
17092 {
17093 offset = member_loc->constant_value (0);
17094 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17095 }
17096 else
17097 baton->size += member_loc->as_block ()->size;
17098
17099 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
17100 baton->data = ptr;
17101
17102 *ptr++ = DW_OP_call4;
17103 cu_off = common_die->sect_off - cu->per_cu->sect_off;
17104 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17105 ptr += 4;
17106
17107 if (member_loc->form_is_constant ())
17108 {
17109 *ptr++ = DW_OP_addr;
17110 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17111 ptr += cu->header.addr_size;
17112 }
17113 else
17114 {
17115 /* We have to copy the data here, because DW_OP_call4 will only
17116 use a DW_AT_location attribute. */
17117 struct dwarf_block *block = member_loc->as_block ();
17118 memcpy (ptr, block->data, block->size);
17119 ptr += block->size;
17120 }
17121
17122 *ptr++ = DW_OP_plus;
17123 gdb_assert (ptr - baton->data == baton->size);
17124
17125 SYMBOL_LOCATION_BATON (sym) = baton;
17126 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17127 }
17128
17129 /* Create appropriate locally-scoped variables for all the
17130 DW_TAG_common_block entries. Also create a struct common_block
17131 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
17132 is used to separate the common blocks name namespace from regular
17133 variable names. */
17134
17135 static void
17136 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
17137 {
17138 struct attribute *attr;
17139
17140 attr = dwarf2_attr (die, DW_AT_location, cu);
17141 if (attr != nullptr)
17142 {
17143 /* Support the .debug_loc offsets. */
17144 if (attr->form_is_block ())
17145 {
17146 /* Ok. */
17147 }
17148 else if (attr->form_is_section_offset ())
17149 {
17150 dwarf2_complex_location_expr_complaint ();
17151 attr = NULL;
17152 }
17153 else
17154 {
17155 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17156 "common block member");
17157 attr = NULL;
17158 }
17159 }
17160
17161 if (die->child != NULL)
17162 {
17163 struct objfile *objfile = cu->per_objfile->objfile;
17164 struct die_info *child_die;
17165 size_t n_entries = 0, size;
17166 struct common_block *common_block;
17167 struct symbol *sym;
17168
17169 for (child_die = die->child;
17170 child_die && child_die->tag;
17171 child_die = child_die->sibling)
17172 ++n_entries;
17173
17174 size = (sizeof (struct common_block)
17175 + (n_entries - 1) * sizeof (struct symbol *));
17176 common_block
17177 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17178 size);
17179 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17180 common_block->n_entries = 0;
17181
17182 for (child_die = die->child;
17183 child_die && child_die->tag;
17184 child_die = child_die->sibling)
17185 {
17186 /* Create the symbol in the DW_TAG_common_block block in the current
17187 symbol scope. */
17188 sym = new_symbol (child_die, NULL, cu);
17189 if (sym != NULL)
17190 {
17191 struct attribute *member_loc;
17192
17193 common_block->contents[common_block->n_entries++] = sym;
17194
17195 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17196 cu);
17197 if (member_loc)
17198 {
17199 /* GDB has handled this for a long time, but it is
17200 not specified by DWARF. It seems to have been
17201 emitted by gfortran at least as recently as:
17202 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
17203 complaint (_("Variable in common block has "
17204 "DW_AT_data_member_location "
17205 "- DIE at %s [in module %s]"),
17206 sect_offset_str (child_die->sect_off),
17207 objfile_name (objfile));
17208
17209 if (member_loc->form_is_section_offset ())
17210 dwarf2_complex_location_expr_complaint ();
17211 else if (member_loc->form_is_constant ()
17212 || member_loc->form_is_block ())
17213 {
17214 if (attr != nullptr)
17215 mark_common_block_symbol_computed (sym, die, attr,
17216 member_loc, cu);
17217 }
17218 else
17219 dwarf2_complex_location_expr_complaint ();
17220 }
17221 }
17222 }
17223
17224 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17225 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
17226 }
17227 }
17228
17229 /* Create a type for a C++ namespace. */
17230
17231 static struct type *
17232 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
17233 {
17234 struct objfile *objfile = cu->per_objfile->objfile;
17235 const char *previous_prefix, *name;
17236 int is_anonymous;
17237 struct type *type;
17238
17239 /* For extensions, reuse the type of the original namespace. */
17240 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17241 {
17242 struct die_info *ext_die;
17243 struct dwarf2_cu *ext_cu = cu;
17244
17245 ext_die = dwarf2_extension (die, &ext_cu);
17246 type = read_type_die (ext_die, ext_cu);
17247
17248 /* EXT_CU may not be the same as CU.
17249 Ensure TYPE is recorded with CU in die_type_hash. */
17250 return set_die_type (die, type, cu);
17251 }
17252
17253 name = namespace_name (die, &is_anonymous, cu);
17254
17255 /* Now build the name of the current namespace. */
17256
17257 previous_prefix = determine_prefix (die, cu);
17258 if (previous_prefix[0] != '\0')
17259 name = typename_concat (&objfile->objfile_obstack,
17260 previous_prefix, name, 0, cu);
17261
17262 /* Create the type. */
17263 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
17264
17265 return set_die_type (die, type, cu);
17266 }
17267
17268 /* Read a namespace scope. */
17269
17270 static void
17271 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17272 {
17273 struct objfile *objfile = cu->per_objfile->objfile;
17274 int is_anonymous;
17275
17276 /* Add a symbol associated to this if we haven't seen the namespace
17277 before. Also, add a using directive if it's an anonymous
17278 namespace. */
17279
17280 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
17281 {
17282 struct type *type;
17283
17284 type = read_type_die (die, cu);
17285 new_symbol (die, type, cu);
17286
17287 namespace_name (die, &is_anonymous, cu);
17288 if (is_anonymous)
17289 {
17290 const char *previous_prefix = determine_prefix (die, cu);
17291
17292 std::vector<const char *> excludes;
17293 add_using_directive (using_directives (cu),
17294 previous_prefix, type->name (), NULL,
17295 NULL, excludes, 0, &objfile->objfile_obstack);
17296 }
17297 }
17298
17299 if (die->child != NULL)
17300 {
17301 struct die_info *child_die = die->child;
17302
17303 while (child_die && child_die->tag)
17304 {
17305 process_die (child_die, cu);
17306 child_die = child_die->sibling;
17307 }
17308 }
17309 }
17310
17311 /* Read a Fortran module as type. This DIE can be only a declaration used for
17312 imported module. Still we need that type as local Fortran "use ... only"
17313 declaration imports depend on the created type in determine_prefix. */
17314
17315 static struct type *
17316 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17317 {
17318 struct objfile *objfile = cu->per_objfile->objfile;
17319 const char *module_name;
17320 struct type *type;
17321
17322 module_name = dwarf2_name (die, cu);
17323 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
17324
17325 return set_die_type (die, type, cu);
17326 }
17327
17328 /* Read a Fortran module. */
17329
17330 static void
17331 read_module (struct die_info *die, struct dwarf2_cu *cu)
17332 {
17333 struct die_info *child_die = die->child;
17334 struct type *type;
17335
17336 type = read_type_die (die, cu);
17337 new_symbol (die, type, cu);
17338
17339 while (child_die && child_die->tag)
17340 {
17341 process_die (child_die, cu);
17342 child_die = child_die->sibling;
17343 }
17344 }
17345
17346 /* Return the name of the namespace represented by DIE. Set
17347 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17348 namespace. */
17349
17350 static const char *
17351 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
17352 {
17353 struct die_info *current_die;
17354 const char *name = NULL;
17355
17356 /* Loop through the extensions until we find a name. */
17357
17358 for (current_die = die;
17359 current_die != NULL;
17360 current_die = dwarf2_extension (die, &cu))
17361 {
17362 /* We don't use dwarf2_name here so that we can detect the absence
17363 of a name -> anonymous namespace. */
17364 name = dwarf2_string_attr (die, DW_AT_name, cu);
17365
17366 if (name != NULL)
17367 break;
17368 }
17369
17370 /* Is it an anonymous namespace? */
17371
17372 *is_anonymous = (name == NULL);
17373 if (*is_anonymous)
17374 name = CP_ANONYMOUS_NAMESPACE_STR;
17375
17376 return name;
17377 }
17378
17379 /* Extract all information from a DW_TAG_pointer_type DIE and add to
17380 the user defined type vector. */
17381
17382 static struct type *
17383 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
17384 {
17385 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
17386 struct comp_unit_head *cu_header = &cu->header;
17387 struct type *type;
17388 struct attribute *attr_byte_size;
17389 struct attribute *attr_address_class;
17390 int byte_size, addr_class;
17391 struct type *target_type;
17392
17393 target_type = die_type (die, cu);
17394
17395 /* The die_type call above may have already set the type for this DIE. */
17396 type = get_die_type (die, cu);
17397 if (type)
17398 return type;
17399
17400 type = lookup_pointer_type (target_type);
17401
17402 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
17403 if (attr_byte_size)
17404 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
17405 else
17406 byte_size = cu_header->addr_size;
17407
17408 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
17409 if (attr_address_class)
17410 addr_class = attr_address_class->constant_value (DW_ADDR_none);
17411 else
17412 addr_class = DW_ADDR_none;
17413
17414 ULONGEST alignment = get_alignment (cu, die);
17415
17416 /* If the pointer size, alignment, or address class is different
17417 than the default, create a type variant marked as such and set
17418 the length accordingly. */
17419 if (TYPE_LENGTH (type) != byte_size
17420 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17421 && alignment != TYPE_RAW_ALIGN (type))
17422 || addr_class != DW_ADDR_none)
17423 {
17424 if (gdbarch_address_class_type_flags_p (gdbarch))
17425 {
17426 type_instance_flags type_flags
17427 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17428 addr_class);
17429 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17430 == 0);
17431 type = make_type_with_address_space (type, type_flags);
17432 }
17433 else if (TYPE_LENGTH (type) != byte_size)
17434 {
17435 complaint (_("invalid pointer size %d"), byte_size);
17436 }
17437 else if (TYPE_RAW_ALIGN (type) != alignment)
17438 {
17439 complaint (_("Invalid DW_AT_alignment"
17440 " - DIE at %s [in module %s]"),
17441 sect_offset_str (die->sect_off),
17442 objfile_name (cu->per_objfile->objfile));
17443 }
17444 else
17445 {
17446 /* Should we also complain about unhandled address classes? */
17447 }
17448 }
17449
17450 TYPE_LENGTH (type) = byte_size;
17451 set_type_align (type, alignment);
17452 return set_die_type (die, type, cu);
17453 }
17454
17455 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17456 the user defined type vector. */
17457
17458 static struct type *
17459 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
17460 {
17461 struct type *type;
17462 struct type *to_type;
17463 struct type *domain;
17464
17465 to_type = die_type (die, cu);
17466 domain = die_containing_type (die, cu);
17467
17468 /* The calls above may have already set the type for this DIE. */
17469 type = get_die_type (die, cu);
17470 if (type)
17471 return type;
17472
17473 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
17474 type = lookup_methodptr_type (to_type);
17475 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
17476 {
17477 struct type *new_type = alloc_type (cu->per_objfile->objfile);
17478
17479 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17480 to_type->fields (), to_type->num_fields (),
17481 to_type->has_varargs ());
17482 type = lookup_methodptr_type (new_type);
17483 }
17484 else
17485 type = lookup_memberptr_type (to_type, domain);
17486
17487 return set_die_type (die, type, cu);
17488 }
17489
17490 /* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
17491 the user defined type vector. */
17492
17493 static struct type *
17494 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17495 enum type_code refcode)
17496 {
17497 struct comp_unit_head *cu_header = &cu->header;
17498 struct type *type, *target_type;
17499 struct attribute *attr;
17500
17501 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17502
17503 target_type = die_type (die, cu);
17504
17505 /* The die_type call above may have already set the type for this DIE. */
17506 type = get_die_type (die, cu);
17507 if (type)
17508 return type;
17509
17510 type = lookup_reference_type (target_type, refcode);
17511 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
17512 if (attr != nullptr)
17513 {
17514 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
17515 }
17516 else
17517 {
17518 TYPE_LENGTH (type) = cu_header->addr_size;
17519 }
17520 maybe_set_alignment (cu, die, type);
17521 return set_die_type (die, type, cu);
17522 }
17523
17524 /* Add the given cv-qualifiers to the element type of the array. GCC
17525 outputs DWARF type qualifiers that apply to an array, not the
17526 element type. But GDB relies on the array element type to carry
17527 the cv-qualifiers. This mimics section 6.7.3 of the C99
17528 specification. */
17529
17530 static struct type *
17531 add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17532 struct type *base_type, int cnst, int voltl)
17533 {
17534 struct type *el_type, *inner_array;
17535
17536 base_type = copy_type (base_type);
17537 inner_array = base_type;
17538
17539 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
17540 {
17541 TYPE_TARGET_TYPE (inner_array) =
17542 copy_type (TYPE_TARGET_TYPE (inner_array));
17543 inner_array = TYPE_TARGET_TYPE (inner_array);
17544 }
17545
17546 el_type = TYPE_TARGET_TYPE (inner_array);
17547 cnst |= TYPE_CONST (el_type);
17548 voltl |= TYPE_VOLATILE (el_type);
17549 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17550
17551 return set_die_type (die, base_type, cu);
17552 }
17553
17554 static struct type *
17555 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
17556 {
17557 struct type *base_type, *cv_type;
17558
17559 base_type = die_type (die, cu);
17560
17561 /* The die_type call above may have already set the type for this DIE. */
17562 cv_type = get_die_type (die, cu);
17563 if (cv_type)
17564 return cv_type;
17565
17566 /* In case the const qualifier is applied to an array type, the element type
17567 is so qualified, not the array type (section 6.7.3 of C99). */
17568 if (base_type->code () == TYPE_CODE_ARRAY)
17569 return add_array_cv_type (die, cu, base_type, 1, 0);
17570
17571 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17572 return set_die_type (die, cv_type, cu);
17573 }
17574
17575 static struct type *
17576 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
17577 {
17578 struct type *base_type, *cv_type;
17579
17580 base_type = die_type (die, cu);
17581
17582 /* The die_type call above may have already set the type for this DIE. */
17583 cv_type = get_die_type (die, cu);
17584 if (cv_type)
17585 return cv_type;
17586
17587 /* In case the volatile qualifier is applied to an array type, the
17588 element type is so qualified, not the array type (section 6.7.3
17589 of C99). */
17590 if (base_type->code () == TYPE_CODE_ARRAY)
17591 return add_array_cv_type (die, cu, base_type, 0, 1);
17592
17593 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17594 return set_die_type (die, cv_type, cu);
17595 }
17596
17597 /* Handle DW_TAG_restrict_type. */
17598
17599 static struct type *
17600 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17601 {
17602 struct type *base_type, *cv_type;
17603
17604 base_type = die_type (die, cu);
17605
17606 /* The die_type call above may have already set the type for this DIE. */
17607 cv_type = get_die_type (die, cu);
17608 if (cv_type)
17609 return cv_type;
17610
17611 cv_type = make_restrict_type (base_type);
17612 return set_die_type (die, cv_type, cu);
17613 }
17614
17615 /* Handle DW_TAG_atomic_type. */
17616
17617 static struct type *
17618 read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17619 {
17620 struct type *base_type, *cv_type;
17621
17622 base_type = die_type (die, cu);
17623
17624 /* The die_type call above may have already set the type for this DIE. */
17625 cv_type = get_die_type (die, cu);
17626 if (cv_type)
17627 return cv_type;
17628
17629 cv_type = make_atomic_type (base_type);
17630 return set_die_type (die, cv_type, cu);
17631 }
17632
17633 /* Extract all information from a DW_TAG_string_type DIE and add to
17634 the user defined type vector. It isn't really a user defined type,
17635 but it behaves like one, with other DIE's using an AT_user_def_type
17636 attribute to reference it. */
17637
17638 static struct type *
17639 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
17640 {
17641 struct objfile *objfile = cu->per_objfile->objfile;
17642 struct gdbarch *gdbarch = objfile->arch ();
17643 struct type *type, *range_type, *index_type, *char_type;
17644 struct attribute *attr;
17645 struct dynamic_prop prop;
17646 bool length_is_constant = true;
17647 LONGEST length;
17648
17649 /* There are a couple of places where bit sizes might be made use of
17650 when parsing a DW_TAG_string_type, however, no producer that we know
17651 of make use of these. Handling bit sizes that are a multiple of the
17652 byte size is easy enough, but what about other bit sizes? Lets deal
17653 with that problem when we have to. Warn about these attributes being
17654 unsupported, then parse the type and ignore them like we always
17655 have. */
17656 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17657 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17658 {
17659 static bool warning_printed = false;
17660 if (!warning_printed)
17661 {
17662 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17663 "currently supported on DW_TAG_string_type."));
17664 warning_printed = true;
17665 }
17666 }
17667
17668 attr = dwarf2_attr (die, DW_AT_string_length, cu);
17669 if (attr != nullptr && !attr->form_is_constant ())
17670 {
17671 /* The string length describes the location at which the length of
17672 the string can be found. The size of the length field can be
17673 specified with one of the attributes below. */
17674 struct type *prop_type;
17675 struct attribute *len
17676 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17677 if (len == nullptr)
17678 len = dwarf2_attr (die, DW_AT_byte_size, cu);
17679 if (len != nullptr && len->form_is_constant ())
17680 {
17681 /* Pass 0 as the default as we know this attribute is constant
17682 and the default value will not be returned. */
17683 LONGEST sz = len->constant_value (0);
17684 prop_type = cu->per_objfile->int_type (sz, true);
17685 }
17686 else
17687 {
17688 /* If the size is not specified then we assume it is the size of
17689 an address on this target. */
17690 prop_type = cu->addr_sized_int_type (true);
17691 }
17692
17693 /* Convert the attribute into a dynamic property. */
17694 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17695 length = 1;
17696 else
17697 length_is_constant = false;
17698 }
17699 else if (attr != nullptr)
17700 {
17701 /* This DW_AT_string_length just contains the length with no
17702 indirection. There's no need to create a dynamic property in this
17703 case. Pass 0 for the default value as we know it will not be
17704 returned in this case. */
17705 length = attr->constant_value (0);
17706 }
17707 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
17708 {
17709 /* We don't currently support non-constant byte sizes for strings. */
17710 length = attr->constant_value (1);
17711 }
17712 else
17713 {
17714 /* Use 1 as a fallback length if we have nothing else. */
17715 length = 1;
17716 }
17717
17718 index_type = objfile_type (objfile)->builtin_int;
17719 if (length_is_constant)
17720 range_type = create_static_range_type (NULL, index_type, 1, length);
17721 else
17722 {
17723 struct dynamic_prop low_bound;
17724
17725 low_bound.set_const_val (1);
17726 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17727 }
17728 char_type = language_string_char_type (cu->language_defn, gdbarch);
17729 type = create_string_type (NULL, char_type, range_type);
17730
17731 return set_die_type (die, type, cu);
17732 }
17733
17734 /* Assuming that DIE corresponds to a function, returns nonzero
17735 if the function is prototyped. */
17736
17737 static int
17738 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17739 {
17740 struct attribute *attr;
17741
17742 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17743 if (attr && attr->as_boolean ())
17744 return 1;
17745
17746 /* The DWARF standard implies that the DW_AT_prototyped attribute
17747 is only meaningful for C, but the concept also extends to other
17748 languages that allow unprototyped functions (Eg: Objective C).
17749 For all other languages, assume that functions are always
17750 prototyped. */
17751 if (cu->language != language_c
17752 && cu->language != language_objc
17753 && cu->language != language_opencl)
17754 return 1;
17755
17756 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17757 prototyped and unprototyped functions; default to prototyped,
17758 since that is more common in modern code (and RealView warns
17759 about unprototyped functions). */
17760 if (producer_is_realview (cu->producer))
17761 return 1;
17762
17763 return 0;
17764 }
17765
17766 /* Handle DIES due to C code like:
17767
17768 struct foo
17769 {
17770 int (*funcp)(int a, long l);
17771 int b;
17772 };
17773
17774 ('funcp' generates a DW_TAG_subroutine_type DIE). */
17775
17776 static struct type *
17777 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
17778 {
17779 struct objfile *objfile = cu->per_objfile->objfile;
17780 struct type *type; /* Type that this function returns. */
17781 struct type *ftype; /* Function that returns above type. */
17782 struct attribute *attr;
17783
17784 type = die_type (die, cu);
17785
17786 /* The die_type call above may have already set the type for this DIE. */
17787 ftype = get_die_type (die, cu);
17788 if (ftype)
17789 return ftype;
17790
17791 ftype = lookup_function_type (type);
17792
17793 if (prototyped_function_p (die, cu))
17794 ftype->set_is_prototyped (true);
17795
17796 /* Store the calling convention in the type if it's available in
17797 the subroutine die. Otherwise set the calling convention to
17798 the default value DW_CC_normal. */
17799 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
17800 if (attr != nullptr
17801 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
17802 TYPE_CALLING_CONVENTION (ftype)
17803 = (enum dwarf_calling_convention) attr->constant_value (0);
17804 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17805 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17806 else
17807 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
17808
17809 /* Record whether the function returns normally to its caller or not
17810 if the DWARF producer set that information. */
17811 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17812 if (attr && attr->as_boolean ())
17813 TYPE_NO_RETURN (ftype) = 1;
17814
17815 /* We need to add the subroutine type to the die immediately so
17816 we don't infinitely recurse when dealing with parameters
17817 declared as the same subroutine type. */
17818 set_die_type (die, ftype, cu);
17819
17820 if (die->child != NULL)
17821 {
17822 struct type *void_type = objfile_type (objfile)->builtin_void;
17823 struct die_info *child_die;
17824 int nparams, iparams;
17825
17826 /* Count the number of parameters.
17827 FIXME: GDB currently ignores vararg functions, but knows about
17828 vararg member functions. */
17829 nparams = 0;
17830 child_die = die->child;
17831 while (child_die && child_die->tag)
17832 {
17833 if (child_die->tag == DW_TAG_formal_parameter)
17834 nparams++;
17835 else if (child_die->tag == DW_TAG_unspecified_parameters)
17836 ftype->set_has_varargs (true);
17837
17838 child_die = child_die->sibling;
17839 }
17840
17841 /* Allocate storage for parameters and fill them in. */
17842 ftype->set_num_fields (nparams);
17843 ftype->set_fields
17844 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
17845
17846 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17847 even if we error out during the parameters reading below. */
17848 for (iparams = 0; iparams < nparams; iparams++)
17849 ftype->field (iparams).set_type (void_type);
17850
17851 iparams = 0;
17852 child_die = die->child;
17853 while (child_die && child_die->tag)
17854 {
17855 if (child_die->tag == DW_TAG_formal_parameter)
17856 {
17857 struct type *arg_type;
17858
17859 /* DWARF version 2 has no clean way to discern C++
17860 static and non-static member functions. G++ helps
17861 GDB by marking the first parameter for non-static
17862 member functions (which is the this pointer) as
17863 artificial. We pass this information to
17864 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17865
17866 DWARF version 3 added DW_AT_object_pointer, which GCC
17867 4.5 does not yet generate. */
17868 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
17869 if (attr != nullptr)
17870 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
17871 else
17872 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
17873 arg_type = die_type (child_die, cu);
17874
17875 /* RealView does not mark THIS as const, which the testsuite
17876 expects. GCC marks THIS as const in method definitions,
17877 but not in the class specifications (GCC PR 43053). */
17878 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17879 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17880 {
17881 int is_this = 0;
17882 struct dwarf2_cu *arg_cu = cu;
17883 const char *name = dwarf2_name (child_die, cu);
17884
17885 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17886 if (attr != nullptr)
17887 {
17888 /* If the compiler emits this, use it. */
17889 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17890 is_this = 1;
17891 }
17892 else if (name && strcmp (name, "this") == 0)
17893 /* Function definitions will have the argument names. */
17894 is_this = 1;
17895 else if (name == NULL && iparams == 0)
17896 /* Declarations may not have the names, so like
17897 elsewhere in GDB, assume an artificial first
17898 argument is "this". */
17899 is_this = 1;
17900
17901 if (is_this)
17902 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17903 arg_type, 0);
17904 }
17905
17906 ftype->field (iparams).set_type (arg_type);
17907 iparams++;
17908 }
17909 child_die = child_die->sibling;
17910 }
17911 }
17912
17913 return ftype;
17914 }
17915
17916 static struct type *
17917 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
17918 {
17919 struct objfile *objfile = cu->per_objfile->objfile;
17920 const char *name = NULL;
17921 struct type *this_type, *target_type;
17922
17923 name = dwarf2_full_name (NULL, die, cu);
17924 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17925 this_type->set_target_is_stub (true);
17926 set_die_type (die, this_type, cu);
17927 target_type = die_type (die, cu);
17928 if (target_type != this_type)
17929 TYPE_TARGET_TYPE (this_type) = target_type;
17930 else
17931 {
17932 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17933 spec and cause infinite loops in GDB. */
17934 complaint (_("Self-referential DW_TAG_typedef "
17935 "- DIE at %s [in module %s]"),
17936 sect_offset_str (die->sect_off), objfile_name (objfile));
17937 TYPE_TARGET_TYPE (this_type) = NULL;
17938 }
17939 if (name == NULL)
17940 {
17941 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17942 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17943 Handle these by just returning the target type, rather than
17944 constructing an anonymous typedef type and trying to handle this
17945 elsewhere. */
17946 set_die_type (die, target_type, cu);
17947 return target_type;
17948 }
17949 return this_type;
17950 }
17951
17952 /* Helper for get_dwarf2_rational_constant that computes the value of
17953 a given gmp_mpz given an attribute. */
17954
17955 static void
17956 get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
17957 {
17958 /* GCC will sometimes emit a 16-byte constant value as a DWARF
17959 location expression that pushes an implicit value. */
17960 if (attr->form == DW_FORM_exprloc)
17961 {
17962 dwarf_block *blk = attr->as_block ();
17963 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
17964 {
17965 uint64_t len;
17966 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
17967 blk->data + blk->size,
17968 &len);
17969 if (ptr - blk->data + len <= blk->size)
17970 {
17971 mpz_import (value->val, len,
17972 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17973 1, 0, 0, ptr);
17974 return;
17975 }
17976 }
17977
17978 /* On failure set it to 1. */
17979 *value = gdb_mpz (1);
17980 }
17981 else if (attr->form_is_block ())
17982 {
17983 dwarf_block *blk = attr->as_block ();
17984 mpz_import (value->val, blk->size,
17985 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
17986 1, 0, 0, blk->data);
17987 }
17988 else
17989 *value = gdb_mpz (attr->constant_value (1));
17990 }
17991
17992 /* Assuming DIE is a rational DW_TAG_constant, read the DIE's
17993 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
17994
17995 If the numerator and/or numerator attribute is missing,
17996 a complaint is filed, and NUMERATOR and DENOMINATOR are left
17997 untouched. */
17998
17999 static void
18000 get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18001 gdb_mpz *numerator, gdb_mpz *denominator)
18002 {
18003 struct attribute *num_attr, *denom_attr;
18004
18005 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18006 if (num_attr == nullptr)
18007 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18008 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18009
18010 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18011 if (denom_attr == nullptr)
18012 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18013 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18014
18015 if (num_attr == nullptr || denom_attr == nullptr)
18016 return;
18017
18018 get_mpz (cu, numerator, num_attr);
18019 get_mpz (cu, denominator, denom_attr);
18020 }
18021
18022 /* Same as get_dwarf2_rational_constant, but extracting an unsigned
18023 rational constant, rather than a signed one.
18024
18025 If the rational constant has a negative value, a complaint
18026 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18027
18028 static void
18029 get_dwarf2_unsigned_rational_constant (struct die_info *die,
18030 struct dwarf2_cu *cu,
18031 gdb_mpz *numerator,
18032 gdb_mpz *denominator)
18033 {
18034 gdb_mpz num (1);
18035 gdb_mpz denom (1);
18036
18037 get_dwarf2_rational_constant (die, cu, &num, &denom);
18038 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
18039 {
18040 mpz_neg (num.val, num.val);
18041 mpz_neg (denom.val, denom.val);
18042 }
18043 else if (mpz_sgn (num.val) == -1)
18044 {
18045 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18046 " in DIE at %s"),
18047 sect_offset_str (die->sect_off));
18048 return;
18049 }
18050 else if (mpz_sgn (denom.val) == -1)
18051 {
18052 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18053 " in DIE at %s"),
18054 sect_offset_str (die->sect_off));
18055 return;
18056 }
18057
18058 *numerator = std::move (num);
18059 *denominator = std::move (denom);
18060 }
18061
18062 /* Assuming that ENCODING is a string whose contents starting at the
18063 K'th character is "_nn" where "nn" is a decimal number, scan that
18064 number and set RESULT to the value. K is updated to point to the
18065 character immediately following the number.
18066
18067 If the string does not conform to the format described above, false
18068 is returned, and K may or may not be changed. */
18069
18070 static bool
18071 ada_get_gnat_encoded_number (const char *encoding, int &k, gdb_mpz *result)
18072 {
18073 /* The next character should be an underscore ('_') followed
18074 by a digit. */
18075 if (encoding[k] != '_' || !isdigit (encoding[k + 1]))
18076 return false;
18077
18078 /* Skip the underscore. */
18079 k++;
18080 int start = k;
18081
18082 /* Determine the number of digits for our number. */
18083 while (isdigit (encoding[k]))
18084 k++;
18085 if (k == start)
18086 return false;
18087
18088 std::string copy (&encoding[start], k - start);
18089 if (mpz_set_str (result->val, copy.c_str (), 10) == -1)
18090 return false;
18091
18092 return true;
18093 }
18094
18095 /* Scan two numbers from ENCODING at OFFSET, assuming the string is of
18096 the form _NN_DD, where NN and DD are decimal numbers. Set NUM and
18097 DENOM, update OFFSET, and return true on success. Return false on
18098 failure. */
18099
18100 static bool
18101 ada_get_gnat_encoded_ratio (const char *encoding, int &offset,
18102 gdb_mpz *num, gdb_mpz *denom)
18103 {
18104 if (!ada_get_gnat_encoded_number (encoding, offset, num))
18105 return false;
18106 return ada_get_gnat_encoded_number (encoding, offset, denom);
18107 }
18108
18109 /* Assuming DIE corresponds to a fixed point type, finish the creation
18110 of the corresponding TYPE by setting its type-specific data. CU is
18111 the DIE's CU. SUFFIX is the "XF" type name suffix coming from GNAT
18112 encodings. It is nullptr if the GNAT encoding should be
18113 ignored. */
18114
18115 static void
18116 finish_fixed_point_type (struct type *type, const char *suffix,
18117 struct die_info *die, struct dwarf2_cu *cu)
18118 {
18119 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18120 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18121
18122 /* If GNAT encodings are preferred, don't examine the
18123 attributes. */
18124 struct attribute *attr = nullptr;
18125 if (suffix == nullptr)
18126 {
18127 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18128 if (attr == nullptr)
18129 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18130 if (attr == nullptr)
18131 attr = dwarf2_attr (die, DW_AT_small, cu);
18132 }
18133
18134 /* Numerator and denominator of our fixed-point type's scaling factor.
18135 The default is a scaling factor of 1, which we use as a fallback
18136 when we are not able to decode it (problem with the debugging info,
18137 unsupported forms, bug in GDB, etc...). Using that as the default
18138 allows us to at least print the unscaled value, which might still
18139 be useful to a user. */
18140 gdb_mpz scale_num (1);
18141 gdb_mpz scale_denom (1);
18142
18143 if (attr == nullptr)
18144 {
18145 int offset = 0;
18146 if (suffix != nullptr
18147 && ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18148 &scale_denom)
18149 /* The number might be encoded as _nn_dd_nn_dd, where the
18150 second ratio is the 'small value. In this situation, we
18151 want the second value. */
18152 && (suffix[offset] != '_'
18153 || ada_get_gnat_encoded_ratio (suffix, offset, &scale_num,
18154 &scale_denom)))
18155 {
18156 /* Found it. */
18157 }
18158 else
18159 {
18160 /* Scaling factor not found. Assume a scaling factor of 1,
18161 and hope for the best. At least the user will be able to
18162 see the encoded value. */
18163 scale_num = 1;
18164 scale_denom = 1;
18165 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18166 sect_offset_str (die->sect_off));
18167 }
18168 }
18169 else if (attr->name == DW_AT_binary_scale)
18170 {
18171 LONGEST scale_exp = attr->constant_value (0);
18172 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18173
18174 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
18175 }
18176 else if (attr->name == DW_AT_decimal_scale)
18177 {
18178 LONGEST scale_exp = attr->constant_value (0);
18179 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18180
18181 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
18182 }
18183 else if (attr->name == DW_AT_small)
18184 {
18185 struct die_info *scale_die;
18186 struct dwarf2_cu *scale_cu = cu;
18187
18188 scale_die = follow_die_ref (die, attr, &scale_cu);
18189 if (scale_die->tag == DW_TAG_constant)
18190 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18191 &scale_num, &scale_denom);
18192 else
18193 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18194 " (DIE at %s)"),
18195 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18196 }
18197 else
18198 {
18199 complaint (_("unsupported scale attribute %s for fixed-point type"
18200 " (DIE at %s)"),
18201 dwarf_attr_name (attr->name),
18202 sect_offset_str (die->sect_off));
18203 }
18204
18205 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
18206 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18207 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
18208 mpq_canonicalize (scaling_factor.val);
18209 }
18210
18211 /* The gnat-encoding suffix for fixed point. */
18212
18213 #define GNAT_FIXED_POINT_SUFFIX "___XF_"
18214
18215 /* If NAME encodes an Ada fixed-point type, return a pointer to the
18216 "XF" suffix of the name. The text after this is what encodes the
18217 'small and 'delta information. Otherwise, return nullptr. */
18218
18219 static const char *
18220 gnat_encoded_fixed_point_type_info (const char *name)
18221 {
18222 return strstr (name, GNAT_FIXED_POINT_SUFFIX);
18223 }
18224
18225 /* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18226 (which may be different from NAME) to the architecture back-end to allow
18227 it to guess the correct format if necessary. */
18228
18229 static struct type *
18230 dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
18231 const char *name_hint, enum bfd_endian byte_order)
18232 {
18233 struct gdbarch *gdbarch = objfile->arch ();
18234 const struct floatformat **format;
18235 struct type *type;
18236
18237 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18238 if (format)
18239 type = init_float_type (objfile, bits, name, format, byte_order);
18240 else
18241 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18242
18243 return type;
18244 }
18245
18246 /* Allocate an integer type of size BITS and name NAME. */
18247
18248 static struct type *
18249 dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18250 int bits, int unsigned_p, const char *name)
18251 {
18252 struct type *type;
18253
18254 /* Versions of Intel's C Compiler generate an integer type called "void"
18255 instead of using DW_TAG_unspecified_type. This has been seen on
18256 at least versions 14, 17, and 18. */
18257 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18258 && strcmp (name, "void") == 0)
18259 type = objfile_type (objfile)->builtin_void;
18260 else
18261 type = init_integer_type (objfile, bits, unsigned_p, name);
18262
18263 return type;
18264 }
18265
18266 /* Return true if DIE has a DW_AT_small attribute whose value is
18267 a constant rational, where both the numerator and denominator
18268 are equal to zero.
18269
18270 CU is the DIE's Compilation Unit. */
18271
18272 static bool
18273 has_zero_over_zero_small_attribute (struct die_info *die,
18274 struct dwarf2_cu *cu)
18275 {
18276 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18277 if (attr == nullptr)
18278 return false;
18279
18280 struct dwarf2_cu *scale_cu = cu;
18281 struct die_info *scale_die
18282 = follow_die_ref (die, attr, &scale_cu);
18283
18284 if (scale_die->tag != DW_TAG_constant)
18285 return false;
18286
18287 gdb_mpz num (1), denom (1);
18288 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18289 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
18290 }
18291
18292 /* Initialise and return a floating point type of size BITS suitable for
18293 use as a component of a complex number. The NAME_HINT is passed through
18294 when initialising the floating point type and is the name of the complex
18295 type.
18296
18297 As DWARF doesn't currently provide an explicit name for the components
18298 of a complex number, but it can be helpful to have these components
18299 named, we try to select a suitable name based on the size of the
18300 component. */
18301 static struct type *
18302 dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18303 struct objfile *objfile,
18304 int bits, const char *name_hint,
18305 enum bfd_endian byte_order)
18306 {
18307 gdbarch *gdbarch = objfile->arch ();
18308 struct type *tt = nullptr;
18309
18310 /* Try to find a suitable floating point builtin type of size BITS.
18311 We're going to use the name of this type as the name for the complex
18312 target type that we are about to create. */
18313 switch (cu->language)
18314 {
18315 case language_fortran:
18316 switch (bits)
18317 {
18318 case 32:
18319 tt = builtin_f_type (gdbarch)->builtin_real;
18320 break;
18321 case 64:
18322 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18323 break;
18324 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18325 case 128:
18326 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18327 break;
18328 }
18329 break;
18330 default:
18331 switch (bits)
18332 {
18333 case 32:
18334 tt = builtin_type (gdbarch)->builtin_float;
18335 break;
18336 case 64:
18337 tt = builtin_type (gdbarch)->builtin_double;
18338 break;
18339 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18340 case 128:
18341 tt = builtin_type (gdbarch)->builtin_long_double;
18342 break;
18343 }
18344 break;
18345 }
18346
18347 /* If the type we found doesn't match the size we were looking for, then
18348 pretend we didn't find a type at all, the complex target type we
18349 create will then be nameless. */
18350 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
18351 tt = nullptr;
18352
18353 const char *name = (tt == nullptr) ? nullptr : tt->name ();
18354 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
18355 }
18356
18357 /* Find a representation of a given base type and install
18358 it in the TYPE field of the die. */
18359
18360 static struct type *
18361 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
18362 {
18363 struct objfile *objfile = cu->per_objfile->objfile;
18364 struct type *type;
18365 struct attribute *attr;
18366 int encoding = 0, bits = 0;
18367 const char *name;
18368 gdbarch *arch;
18369
18370 attr = dwarf2_attr (die, DW_AT_encoding, cu);
18371 if (attr != nullptr && attr->form_is_constant ())
18372 encoding = attr->constant_value (0);
18373 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18374 if (attr != nullptr)
18375 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
18376 name = dwarf2_name (die, cu);
18377 if (!name)
18378 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
18379
18380 arch = objfile->arch ();
18381 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18382
18383 attr = dwarf2_attr (die, DW_AT_endianity, cu);
18384 if (attr != nullptr && attr->form_is_constant ())
18385 {
18386 int endianity = attr->constant_value (0);
18387
18388 switch (endianity)
18389 {
18390 case DW_END_big:
18391 byte_order = BFD_ENDIAN_BIG;
18392 break;
18393 case DW_END_little:
18394 byte_order = BFD_ENDIAN_LITTLE;
18395 break;
18396 default:
18397 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18398 break;
18399 }
18400 }
18401
18402 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18403 && cu->language == language_ada
18404 && has_zero_over_zero_small_attribute (die, cu))
18405 {
18406 /* brobecker/2018-02-24: This is a fixed point type for which
18407 the scaling factor is represented as fraction whose value
18408 does not make sense (zero divided by zero), so we should
18409 normally never see these. However, there is a small category
18410 of fixed point types for which GNAT is unable to provide
18411 the scaling factor via the standard DWARF mechanisms, and
18412 for which the info is provided via the GNAT encodings instead.
18413 This is likely what this DIE is about. */
18414 encoding = (encoding == DW_ATE_signed_fixed
18415 ? DW_ATE_signed
18416 : DW_ATE_unsigned);
18417 }
18418
18419 /* With GNAT encodings, fixed-point information will be encoded in
18420 the type name. Note that this can also occur with the above
18421 zero-over-zero case, which is why this is a separate "if" rather
18422 than an "else if". */
18423 const char *gnat_encoding_suffix = nullptr;
18424 if ((encoding == DW_ATE_signed || encoding == DW_ATE_unsigned)
18425 && cu->language == language_ada
18426 && name != nullptr)
18427 {
18428 gnat_encoding_suffix = gnat_encoded_fixed_point_type_info (name);
18429 if (gnat_encoding_suffix != nullptr)
18430 {
18431 gdb_assert (startswith (gnat_encoding_suffix,
18432 GNAT_FIXED_POINT_SUFFIX));
18433 name = obstack_strndup (&cu->per_objfile->objfile->objfile_obstack,
18434 name, gnat_encoding_suffix - name);
18435 /* Use -1 here so that SUFFIX points at the "_" after the
18436 "XF". */
18437 gnat_encoding_suffix += strlen (GNAT_FIXED_POINT_SUFFIX) - 1;
18438
18439 encoding = (encoding == DW_ATE_signed
18440 ? DW_ATE_signed_fixed
18441 : DW_ATE_unsigned_fixed);
18442 }
18443 }
18444
18445 switch (encoding)
18446 {
18447 case DW_ATE_address:
18448 /* Turn DW_ATE_address into a void * pointer. */
18449 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
18450 type = init_pointer_type (objfile, bits, name, type);
18451 break;
18452 case DW_ATE_boolean:
18453 type = init_boolean_type (objfile, bits, 1, name);
18454 break;
18455 case DW_ATE_complex_float:
18456 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18457 byte_order);
18458 if (type->code () == TYPE_CODE_ERROR)
18459 {
18460 if (name == nullptr)
18461 {
18462 struct obstack *obstack
18463 = &cu->per_objfile->objfile->objfile_obstack;
18464 name = obconcat (obstack, "_Complex ", type->name (),
18465 nullptr);
18466 }
18467 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18468 }
18469 else
18470 type = init_complex_type (name, type);
18471 break;
18472 case DW_ATE_decimal_float:
18473 type = init_decfloat_type (objfile, bits, name);
18474 break;
18475 case DW_ATE_float:
18476 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
18477 break;
18478 case DW_ATE_signed:
18479 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18480 break;
18481 case DW_ATE_unsigned:
18482 if (cu->language == language_fortran
18483 && name
18484 && startswith (name, "character("))
18485 type = init_character_type (objfile, bits, 1, name);
18486 else
18487 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18488 break;
18489 case DW_ATE_signed_char:
18490 if (cu->language == language_ada || cu->language == language_m2
18491 || cu->language == language_pascal
18492 || cu->language == language_fortran)
18493 type = init_character_type (objfile, bits, 0, name);
18494 else
18495 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
18496 break;
18497 case DW_ATE_unsigned_char:
18498 if (cu->language == language_ada || cu->language == language_m2
18499 || cu->language == language_pascal
18500 || cu->language == language_fortran
18501 || cu->language == language_rust)
18502 type = init_character_type (objfile, bits, 1, name);
18503 else
18504 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18505 break;
18506 case DW_ATE_UTF:
18507 {
18508 if (bits == 16)
18509 type = builtin_type (arch)->builtin_char16;
18510 else if (bits == 32)
18511 type = builtin_type (arch)->builtin_char32;
18512 else
18513 {
18514 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
18515 bits);
18516 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
18517 }
18518 return set_die_type (die, type, cu);
18519 }
18520 break;
18521 case DW_ATE_signed_fixed:
18522 type = init_fixed_point_type (objfile, bits, 0, name);
18523 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18524 break;
18525 case DW_ATE_unsigned_fixed:
18526 type = init_fixed_point_type (objfile, bits, 1, name);
18527 finish_fixed_point_type (type, gnat_encoding_suffix, die, cu);
18528 break;
18529
18530 default:
18531 complaint (_("unsupported DW_AT_encoding: '%s'"),
18532 dwarf_type_encoding_name (encoding));
18533 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18534 break;
18535 }
18536
18537 if (name && strcmp (name, "char") == 0)
18538 type->set_has_no_signedness (true);
18539
18540 maybe_set_alignment (cu, die, type);
18541
18542 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
18543
18544 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18545 {
18546 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
18547 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
18548 {
18549 unsigned real_bit_size = attr->as_unsigned ();
18550 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18551 /* Only use the attributes if they make sense together. */
18552 if (attr == nullptr
18553 || (attr->as_unsigned () + real_bit_size
18554 <= 8 * TYPE_LENGTH (type)))
18555 {
18556 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18557 = real_bit_size;
18558 if (attr != nullptr)
18559 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
18560 = attr->as_unsigned ();
18561 }
18562 }
18563 }
18564
18565 return set_die_type (die, type, cu);
18566 }
18567
18568 /* Parse dwarf attribute if it's a block, reference or constant and put the
18569 resulting value of the attribute into struct bound_prop.
18570 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18571
18572 static int
18573 attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
18574 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18575 struct type *default_type)
18576 {
18577 struct dwarf2_property_baton *baton;
18578 dwarf2_per_objfile *per_objfile = cu->per_objfile;
18579 struct objfile *objfile = per_objfile->objfile;
18580 struct obstack *obstack = &objfile->objfile_obstack;
18581
18582 gdb_assert (default_type != NULL);
18583
18584 if (attr == NULL || prop == NULL)
18585 return 0;
18586
18587 if (attr->form_is_block ())
18588 {
18589 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18590 baton->property_type = default_type;
18591 baton->locexpr.per_cu = cu->per_cu;
18592 baton->locexpr.per_objfile = per_objfile;
18593
18594 struct dwarf_block *block = attr->as_block ();
18595 baton->locexpr.size = block->size;
18596 baton->locexpr.data = block->data;
18597 switch (attr->name)
18598 {
18599 case DW_AT_string_length:
18600 baton->locexpr.is_reference = true;
18601 break;
18602 default:
18603 baton->locexpr.is_reference = false;
18604 break;
18605 }
18606
18607 prop->set_locexpr (baton);
18608 gdb_assert (prop->baton () != NULL);
18609 }
18610 else if (attr->form_is_ref ())
18611 {
18612 struct dwarf2_cu *target_cu = cu;
18613 struct die_info *target_die;
18614 struct attribute *target_attr;
18615
18616 target_die = follow_die_ref (die, attr, &target_cu);
18617 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
18618 if (target_attr == NULL)
18619 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18620 target_cu);
18621 if (target_attr == NULL)
18622 return 0;
18623
18624 switch (target_attr->name)
18625 {
18626 case DW_AT_location:
18627 if (target_attr->form_is_section_offset ())
18628 {
18629 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18630 baton->property_type = die_type (target_die, target_cu);
18631 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
18632 prop->set_loclist (baton);
18633 gdb_assert (prop->baton () != NULL);
18634 }
18635 else if (target_attr->form_is_block ())
18636 {
18637 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18638 baton->property_type = die_type (target_die, target_cu);
18639 baton->locexpr.per_cu = cu->per_cu;
18640 baton->locexpr.per_objfile = per_objfile;
18641 struct dwarf_block *block = target_attr->as_block ();
18642 baton->locexpr.size = block->size;
18643 baton->locexpr.data = block->data;
18644 baton->locexpr.is_reference = true;
18645 prop->set_locexpr (baton);
18646 gdb_assert (prop->baton () != NULL);
18647 }
18648 else
18649 {
18650 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18651 "dynamic property");
18652 return 0;
18653 }
18654 break;
18655 case DW_AT_data_member_location:
18656 {
18657 LONGEST offset;
18658
18659 if (!handle_data_member_location (target_die, target_cu,
18660 &offset))
18661 return 0;
18662
18663 baton = XOBNEW (obstack, struct dwarf2_property_baton);
18664 baton->property_type = read_type_die (target_die->parent,
18665 target_cu);
18666 baton->offset_info.offset = offset;
18667 baton->offset_info.type = die_type (target_die, target_cu);
18668 prop->set_addr_offset (baton);
18669 break;
18670 }
18671 }
18672 }
18673 else if (attr->form_is_constant ())
18674 prop->set_const_val (attr->constant_value (0));
18675 else
18676 {
18677 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18678 dwarf2_name (die, cu));
18679 return 0;
18680 }
18681
18682 return 1;
18683 }
18684
18685 /* See read.h. */
18686
18687 struct type *
18688 dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
18689 {
18690 struct type *int_type;
18691
18692 /* Helper macro to examine the various builtin types. */
18693 #define TRY_TYPE(F) \
18694 int_type = (unsigned_p \
18695 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18696 : objfile_type (objfile)->builtin_ ## F); \
18697 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
18698 return int_type
18699
18700 TRY_TYPE (char);
18701 TRY_TYPE (short);
18702 TRY_TYPE (int);
18703 TRY_TYPE (long);
18704 TRY_TYPE (long_long);
18705
18706 #undef TRY_TYPE
18707
18708 gdb_assert_not_reached ("unable to find suitable integer type");
18709 }
18710
18711 /* See read.h. */
18712
18713 struct type *
18714 dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
18715 {
18716 int addr_size = this->per_cu->addr_size ();
18717 return this->per_objfile->int_type (addr_size, unsigned_p);
18718 }
18719
18720 /* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18721 present (which is valid) then compute the default type based on the
18722 compilation units address size. */
18723
18724 static struct type *
18725 read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18726 {
18727 struct type *index_type = die_type (die, cu);
18728
18729 /* Dwarf-2 specifications explicitly allows to create subrange types
18730 without specifying a base type.
18731 In that case, the base type must be set to the type of
18732 the lower bound, upper bound or count, in that order, if any of these
18733 three attributes references an object that has a type.
18734 If no base type is found, the Dwarf-2 specifications say that
18735 a signed integer type of size equal to the size of an address should
18736 be used.
18737 For the following C code: `extern char gdb_int [];'
18738 GCC produces an empty range DIE.
18739 FIXME: muller/2010-05-28: Possible references to object for low bound,
18740 high bound or count are not yet handled by this code. */
18741 if (index_type->code () == TYPE_CODE_VOID)
18742 index_type = cu->addr_sized_int_type (false);
18743
18744 return index_type;
18745 }
18746
18747 /* Read the given DW_AT_subrange DIE. */
18748
18749 static struct type *
18750 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18751 {
18752 struct type *base_type, *orig_base_type;
18753 struct type *range_type;
18754 struct attribute *attr;
18755 struct dynamic_prop low, high;
18756 int low_default_is_valid;
18757 int high_bound_is_count = 0;
18758 const char *name;
18759 ULONGEST negative_mask;
18760
18761 orig_base_type = read_subrange_index_type (die, cu);
18762
18763 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18764 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18765 creating the range type, but we use the result of check_typedef
18766 when examining properties of the type. */
18767 base_type = check_typedef (orig_base_type);
18768
18769 /* The die_type call above may have already set the type for this DIE. */
18770 range_type = get_die_type (die, cu);
18771 if (range_type)
18772 return range_type;
18773
18774 high.set_const_val (0);
18775
18776 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18777 omitting DW_AT_lower_bound. */
18778 switch (cu->language)
18779 {
18780 case language_c:
18781 case language_cplus:
18782 low.set_const_val (0);
18783 low_default_is_valid = 1;
18784 break;
18785 case language_fortran:
18786 low.set_const_val (1);
18787 low_default_is_valid = 1;
18788 break;
18789 case language_d:
18790 case language_objc:
18791 case language_rust:
18792 low.set_const_val (0);
18793 low_default_is_valid = (cu->header.version >= 4);
18794 break;
18795 case language_ada:
18796 case language_m2:
18797 case language_pascal:
18798 low.set_const_val (1);
18799 low_default_is_valid = (cu->header.version >= 4);
18800 break;
18801 default:
18802 low.set_const_val (0);
18803 low_default_is_valid = 0;
18804 break;
18805 }
18806
18807 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
18808 if (attr != nullptr)
18809 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
18810 else if (!low_default_is_valid)
18811 complaint (_("Missing DW_AT_lower_bound "
18812 "- DIE at %s [in module %s]"),
18813 sect_offset_str (die->sect_off),
18814 objfile_name (cu->per_objfile->objfile));
18815
18816 struct attribute *attr_ub, *attr_count;
18817 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
18818 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18819 {
18820 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
18821 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
18822 {
18823 /* If bounds are constant do the final calculation here. */
18824 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18825 high.set_const_val (low.const_val () + high.const_val () - 1);
18826 else
18827 high_bound_is_count = 1;
18828 }
18829 else
18830 {
18831 if (attr_ub != NULL)
18832 complaint (_("Unresolved DW_AT_upper_bound "
18833 "- DIE at %s [in module %s]"),
18834 sect_offset_str (die->sect_off),
18835 objfile_name (cu->per_objfile->objfile));
18836 if (attr_count != NULL)
18837 complaint (_("Unresolved DW_AT_count "
18838 "- DIE at %s [in module %s]"),
18839 sect_offset_str (die->sect_off),
18840 objfile_name (cu->per_objfile->objfile));
18841 }
18842 }
18843
18844 LONGEST bias = 0;
18845 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18846 if (bias_attr != nullptr && bias_attr->form_is_constant ())
18847 bias = bias_attr->constant_value (0);
18848
18849 /* Normally, the DWARF producers are expected to use a signed
18850 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18851 But this is unfortunately not always the case, as witnessed
18852 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18853 is used instead. To work around that ambiguity, we treat
18854 the bounds as signed, and thus sign-extend their values, when
18855 the base type is signed. */
18856 negative_mask =
18857 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
18858 if (low.kind () == PROP_CONST
18859 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
18860 low.set_const_val (low.const_val () | negative_mask);
18861 if (high.kind () == PROP_CONST
18862 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
18863 high.set_const_val (high.const_val () | negative_mask);
18864
18865 /* Check for bit and byte strides. */
18866 struct dynamic_prop byte_stride_prop;
18867 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18868 if (attr_byte_stride != nullptr)
18869 {
18870 struct type *prop_type = cu->addr_sized_int_type (false);
18871 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18872 prop_type);
18873 }
18874
18875 struct dynamic_prop bit_stride_prop;
18876 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18877 if (attr_bit_stride != nullptr)
18878 {
18879 /* It only makes sense to have either a bit or byte stride. */
18880 if (attr_byte_stride != nullptr)
18881 {
18882 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18883 "- DIE at %s [in module %s]"),
18884 sect_offset_str (die->sect_off),
18885 objfile_name (cu->per_objfile->objfile));
18886 attr_bit_stride = nullptr;
18887 }
18888 else
18889 {
18890 struct type *prop_type = cu->addr_sized_int_type (false);
18891 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18892 prop_type);
18893 }
18894 }
18895
18896 if (attr_byte_stride != nullptr
18897 || attr_bit_stride != nullptr)
18898 {
18899 bool byte_stride_p = (attr_byte_stride != nullptr);
18900 struct dynamic_prop *stride
18901 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18902
18903 range_type
18904 = create_range_type_with_stride (NULL, orig_base_type, &low,
18905 &high, bias, stride, byte_stride_p);
18906 }
18907 else
18908 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
18909
18910 if (high_bound_is_count)
18911 range_type->bounds ()->flag_upper_bound_is_count = 1;
18912
18913 /* Ada expects an empty array on no boundary attributes. */
18914 if (attr == NULL && cu->language != language_ada)
18915 range_type->bounds ()->high.set_undefined ();
18916
18917 name = dwarf2_name (die, cu);
18918 if (name)
18919 range_type->set_name (name);
18920
18921 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
18922 if (attr != nullptr)
18923 TYPE_LENGTH (range_type) = attr->constant_value (0);
18924
18925 maybe_set_alignment (cu, die, range_type);
18926
18927 set_die_type (die, range_type, cu);
18928
18929 /* set_die_type should be already done. */
18930 set_descriptive_type (range_type, die, cu);
18931
18932 return range_type;
18933 }
18934
18935 static struct type *
18936 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18937 {
18938 struct type *type;
18939
18940 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
18941 type->set_name (dwarf2_name (die, cu));
18942
18943 /* In Ada, an unspecified type is typically used when the description
18944 of the type is deferred to a different unit. When encountering
18945 such a type, we treat it as a stub, and try to resolve it later on,
18946 when needed. */
18947 if (cu->language == language_ada)
18948 type->set_is_stub (true);
18949
18950 return set_die_type (die, type, cu);
18951 }
18952
18953 /* Read a single die and all its descendents. Set the die's sibling
18954 field to NULL; set other fields in the die correctly, and set all
18955 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18956 location of the info_ptr after reading all of those dies. PARENT
18957 is the parent of the die in question. */
18958
18959 static struct die_info *
18960 read_die_and_children (const struct die_reader_specs *reader,
18961 const gdb_byte *info_ptr,
18962 const gdb_byte **new_info_ptr,
18963 struct die_info *parent)
18964 {
18965 struct die_info *die;
18966 const gdb_byte *cur_ptr;
18967
18968 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
18969 if (die == NULL)
18970 {
18971 *new_info_ptr = cur_ptr;
18972 return NULL;
18973 }
18974 store_in_ref_table (die, reader->cu);
18975
18976 if (die->has_children)
18977 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
18978 else
18979 {
18980 die->child = NULL;
18981 *new_info_ptr = cur_ptr;
18982 }
18983
18984 die->sibling = NULL;
18985 die->parent = parent;
18986 return die;
18987 }
18988
18989 /* Read a die, all of its descendents, and all of its siblings; set
18990 all of the fields of all of the dies correctly. Arguments are as
18991 in read_die_and_children. */
18992
18993 static struct die_info *
18994 read_die_and_siblings_1 (const struct die_reader_specs *reader,
18995 const gdb_byte *info_ptr,
18996 const gdb_byte **new_info_ptr,
18997 struct die_info *parent)
18998 {
18999 struct die_info *first_die, *last_sibling;
19000 const gdb_byte *cur_ptr;
19001
19002 cur_ptr = info_ptr;
19003 first_die = last_sibling = NULL;
19004
19005 while (1)
19006 {
19007 struct die_info *die
19008 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
19009
19010 if (die == NULL)
19011 {
19012 *new_info_ptr = cur_ptr;
19013 return first_die;
19014 }
19015
19016 if (!first_die)
19017 first_die = die;
19018 else
19019 last_sibling->sibling = die;
19020
19021 last_sibling = die;
19022 }
19023 }
19024
19025 /* Read a die, all of its descendents, and all of its siblings; set
19026 all of the fields of all of the dies correctly. Arguments are as
19027 in read_die_and_children.
19028 This the main entry point for reading a DIE and all its children. */
19029
19030 static struct die_info *
19031 read_die_and_siblings (const struct die_reader_specs *reader,
19032 const gdb_byte *info_ptr,
19033 const gdb_byte **new_info_ptr,
19034 struct die_info *parent)
19035 {
19036 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19037 new_info_ptr, parent);
19038
19039 if (dwarf_die_debug)
19040 {
19041 fprintf_unfiltered (gdb_stdlog,
19042 "Read die from %s@0x%x of %s:\n",
19043 reader->die_section->get_name (),
19044 (unsigned) (info_ptr - reader->die_section->buffer),
19045 bfd_get_filename (reader->abfd));
19046 dump_die (die, dwarf_die_debug);
19047 }
19048
19049 return die;
19050 }
19051
19052 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19053 attributes.
19054 The caller is responsible for filling in the extra attributes
19055 and updating (*DIEP)->num_attrs.
19056 Set DIEP to point to a newly allocated die with its information,
19057 except for its child, sibling, and parent fields. */
19058
19059 static const gdb_byte *
19060 read_full_die_1 (const struct die_reader_specs *reader,
19061 struct die_info **diep, const gdb_byte *info_ptr,
19062 int num_extra_attrs)
19063 {
19064 unsigned int abbrev_number, bytes_read, i;
19065 const struct abbrev_info *abbrev;
19066 struct die_info *die;
19067 struct dwarf2_cu *cu = reader->cu;
19068 bfd *abfd = reader->abfd;
19069
19070 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
19071 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19072 info_ptr += bytes_read;
19073 if (!abbrev_number)
19074 {
19075 *diep = NULL;
19076 return info_ptr;
19077 }
19078
19079 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
19080 if (!abbrev)
19081 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19082 abbrev_number,
19083 bfd_get_filename (abfd));
19084
19085 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
19086 die->sect_off = sect_off;
19087 die->tag = abbrev->tag;
19088 die->abbrev = abbrev_number;
19089 die->has_children = abbrev->has_children;
19090
19091 /* Make the result usable.
19092 The caller needs to update num_attrs after adding the extra
19093 attributes. */
19094 die->num_attrs = abbrev->num_attrs;
19095
19096 bool any_need_reprocess = false;
19097 for (i = 0; i < abbrev->num_attrs; ++i)
19098 {
19099 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19100 info_ptr);
19101 if (die->attrs[i].requires_reprocessing_p ())
19102 any_need_reprocess = true;
19103 }
19104
19105 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
19106 if (attr != nullptr && attr->form_is_unsigned ())
19107 cu->str_offsets_base = attr->as_unsigned ();
19108
19109 attr = die->attr (DW_AT_loclists_base);
19110 if (attr != nullptr)
19111 cu->loclist_base = attr->as_unsigned ();
19112
19113 auto maybe_addr_base = die->addr_base ();
19114 if (maybe_addr_base.has_value ())
19115 cu->addr_base = *maybe_addr_base;
19116
19117 attr = die->attr (DW_AT_rnglists_base);
19118 if (attr != nullptr)
19119 cu->rnglists_base = attr->as_unsigned ();
19120
19121 if (any_need_reprocess)
19122 {
19123 for (i = 0; i < abbrev->num_attrs; ++i)
19124 {
19125 if (die->attrs[i].requires_reprocessing_p ())
19126 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19127 }
19128 }
19129 *diep = die;
19130 return info_ptr;
19131 }
19132
19133 /* Read a die and all its attributes.
19134 Set DIEP to point to a newly allocated die with its information,
19135 except for its child, sibling, and parent fields. */
19136
19137 static const gdb_byte *
19138 read_full_die (const struct die_reader_specs *reader,
19139 struct die_info **diep, const gdb_byte *info_ptr)
19140 {
19141 const gdb_byte *result;
19142
19143 result = read_full_die_1 (reader, diep, info_ptr, 0);
19144
19145 if (dwarf_die_debug)
19146 {
19147 fprintf_unfiltered (gdb_stdlog,
19148 "Read die from %s@0x%x of %s:\n",
19149 reader->die_section->get_name (),
19150 (unsigned) (info_ptr - reader->die_section->buffer),
19151 bfd_get_filename (reader->abfd));
19152 dump_die (*diep, dwarf_die_debug);
19153 }
19154
19155 return result;
19156 }
19157 \f
19158
19159 /* Returns nonzero if TAG represents a type that we might generate a partial
19160 symbol for. */
19161
19162 static int
19163 is_type_tag_for_partial (int tag, enum language lang)
19164 {
19165 switch (tag)
19166 {
19167 #if 0
19168 /* Some types that would be reasonable to generate partial symbols for,
19169 that we don't at present. Note that normally this does not
19170 matter, mainly because C compilers don't give names to these
19171 types, but instead emit DW_TAG_typedef. */
19172 case DW_TAG_file_type:
19173 case DW_TAG_ptr_to_member_type:
19174 case DW_TAG_set_type:
19175 case DW_TAG_string_type:
19176 case DW_TAG_subroutine_type:
19177 #endif
19178
19179 /* GNAT may emit an array with a name, but no typedef, so we
19180 need to make a symbol in this case. */
19181 case DW_TAG_array_type:
19182 return lang == language_ada;
19183
19184 case DW_TAG_base_type:
19185 case DW_TAG_class_type:
19186 case DW_TAG_interface_type:
19187 case DW_TAG_enumeration_type:
19188 case DW_TAG_structure_type:
19189 case DW_TAG_subrange_type:
19190 case DW_TAG_typedef:
19191 case DW_TAG_union_type:
19192 return 1;
19193 default:
19194 return 0;
19195 }
19196 }
19197
19198 /* Load all DIEs that are interesting for partial symbols into memory. */
19199
19200 static struct partial_die_info *
19201 load_partial_dies (const struct die_reader_specs *reader,
19202 const gdb_byte *info_ptr, int building_psymtab)
19203 {
19204 struct dwarf2_cu *cu = reader->cu;
19205 struct objfile *objfile = cu->per_objfile->objfile;
19206 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
19207 unsigned int bytes_read;
19208 unsigned int load_all = 0;
19209 int nesting_level = 1;
19210
19211 parent_die = NULL;
19212 last_die = NULL;
19213
19214 gdb_assert (cu->per_cu != NULL);
19215 if (cu->per_cu->load_all_dies)
19216 load_all = 1;
19217
19218 cu->partial_dies
19219 = htab_create_alloc_ex (cu->header.length / 12,
19220 partial_die_hash,
19221 partial_die_eq,
19222 NULL,
19223 &cu->comp_unit_obstack,
19224 hashtab_obstack_allocate,
19225 dummy_obstack_deallocate);
19226
19227 while (1)
19228 {
19229 const abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr,
19230 &bytes_read);
19231
19232 /* A NULL abbrev means the end of a series of children. */
19233 if (abbrev == NULL)
19234 {
19235 if (--nesting_level == 0)
19236 return first_die;
19237
19238 info_ptr += bytes_read;
19239 last_die = parent_die;
19240 parent_die = parent_die->die_parent;
19241 continue;
19242 }
19243
19244 /* Check for template arguments. We never save these; if
19245 they're seen, we just mark the parent, and go on our way. */
19246 if (parent_die != NULL
19247 && cu->language == language_cplus
19248 && (abbrev->tag == DW_TAG_template_type_param
19249 || abbrev->tag == DW_TAG_template_value_param))
19250 {
19251 parent_die->has_template_arguments = 1;
19252
19253 if (!load_all)
19254 {
19255 /* We don't need a partial DIE for the template argument. */
19256 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19257 continue;
19258 }
19259 }
19260
19261 /* We only recurse into c++ subprograms looking for template arguments.
19262 Skip their other children. */
19263 if (!load_all
19264 && cu->language == language_cplus
19265 && parent_die != NULL
19266 && parent_die->tag == DW_TAG_subprogram
19267 && abbrev->tag != DW_TAG_inlined_subroutine)
19268 {
19269 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19270 continue;
19271 }
19272
19273 /* Check whether this DIE is interesting enough to save. Normally
19274 we would not be interested in members here, but there may be
19275 later variables referencing them via DW_AT_specification (for
19276 static members). */
19277 if (!load_all
19278 && !is_type_tag_for_partial (abbrev->tag, cu->language)
19279 && abbrev->tag != DW_TAG_constant
19280 && abbrev->tag != DW_TAG_enumerator
19281 && abbrev->tag != DW_TAG_subprogram
19282 && abbrev->tag != DW_TAG_inlined_subroutine
19283 && abbrev->tag != DW_TAG_lexical_block
19284 && abbrev->tag != DW_TAG_variable
19285 && abbrev->tag != DW_TAG_namespace
19286 && abbrev->tag != DW_TAG_module
19287 && abbrev->tag != DW_TAG_member
19288 && abbrev->tag != DW_TAG_imported_unit
19289 && abbrev->tag != DW_TAG_imported_declaration)
19290 {
19291 /* Otherwise we skip to the next sibling, if any. */
19292 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
19293 continue;
19294 }
19295
19296 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19297 abbrev);
19298
19299 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
19300
19301 /* This two-pass algorithm for processing partial symbols has a
19302 high cost in cache pressure. Thus, handle some simple cases
19303 here which cover the majority of C partial symbols. DIEs
19304 which neither have specification tags in them, nor could have
19305 specification tags elsewhere pointing at them, can simply be
19306 processed and discarded.
19307
19308 This segment is also optional; scan_partial_symbols and
19309 add_partial_symbol will handle these DIEs if we chain
19310 them in normally. When compilers which do not emit large
19311 quantities of duplicate debug information are more common,
19312 this code can probably be removed. */
19313
19314 /* Any complete simple types at the top level (pretty much all
19315 of them, for a language without namespaces), can be processed
19316 directly. */
19317 if (parent_die == NULL
19318 && pdi.has_specification == 0
19319 && pdi.is_declaration == 0
19320 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19321 || pdi.tag == DW_TAG_base_type
19322 || pdi.tag == DW_TAG_array_type
19323 || pdi.tag == DW_TAG_subrange_type))
19324 {
19325 if (building_psymtab && pdi.raw_name != NULL)
19326 add_partial_symbol (&pdi, cu);
19327
19328 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19329 continue;
19330 }
19331
19332 /* The exception for DW_TAG_typedef with has_children above is
19333 a workaround of GCC PR debug/47510. In the case of this complaint
19334 type_name_or_error will error on such types later.
19335
19336 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19337 it could not find the child DIEs referenced later, this is checked
19338 above. In correct DWARF DW_TAG_typedef should have no children. */
19339
19340 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
19341 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
19342 "- DIE at %s [in module %s]"),
19343 sect_offset_str (pdi.sect_off), objfile_name (objfile));
19344
19345 /* If we're at the second level, and we're an enumerator, and
19346 our parent has no specification (meaning possibly lives in a
19347 namespace elsewhere), then we can add the partial symbol now
19348 instead of queueing it. */
19349 if (pdi.tag == DW_TAG_enumerator
19350 && parent_die != NULL
19351 && parent_die->die_parent == NULL
19352 && parent_die->tag == DW_TAG_enumeration_type
19353 && parent_die->has_specification == 0)
19354 {
19355 if (pdi.raw_name == NULL)
19356 complaint (_("malformed enumerator DIE ignored"));
19357 else if (building_psymtab)
19358 add_partial_symbol (&pdi, cu);
19359
19360 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
19361 continue;
19362 }
19363
19364 struct partial_die_info *part_die
19365 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
19366
19367 /* We'll save this DIE so link it in. */
19368 part_die->die_parent = parent_die;
19369 part_die->die_sibling = NULL;
19370 part_die->die_child = NULL;
19371
19372 if (last_die && last_die == parent_die)
19373 last_die->die_child = part_die;
19374 else if (last_die)
19375 last_die->die_sibling = part_die;
19376
19377 last_die = part_die;
19378
19379 if (first_die == NULL)
19380 first_die = part_die;
19381
19382 /* Maybe add the DIE to the hash table. Not all DIEs that we
19383 find interesting need to be in the hash table, because we
19384 also have the parent/sibling/child chains; only those that we
19385 might refer to by offset later during partial symbol reading.
19386
19387 For now this means things that might have be the target of a
19388 DW_AT_specification, DW_AT_abstract_origin, or
19389 DW_AT_extension. DW_AT_extension will refer only to
19390 namespaces; DW_AT_abstract_origin refers to functions (and
19391 many things under the function DIE, but we do not recurse
19392 into function DIEs during partial symbol reading) and
19393 possibly variables as well; DW_AT_specification refers to
19394 declarations. Declarations ought to have the DW_AT_declaration
19395 flag. It happens that GCC forgets to put it in sometimes, but
19396 only for functions, not for types.
19397
19398 Adding more things than necessary to the hash table is harmless
19399 except for the performance cost. Adding too few will result in
19400 wasted time in find_partial_die, when we reread the compilation
19401 unit with load_all_dies set. */
19402
19403 if (load_all
19404 || abbrev->tag == DW_TAG_constant
19405 || abbrev->tag == DW_TAG_subprogram
19406 || abbrev->tag == DW_TAG_variable
19407 || abbrev->tag == DW_TAG_namespace
19408 || part_die->is_declaration)
19409 {
19410 void **slot;
19411
19412 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
19413 to_underlying (part_die->sect_off),
19414 INSERT);
19415 *slot = part_die;
19416 }
19417
19418 /* For some DIEs we want to follow their children (if any). For C
19419 we have no reason to follow the children of structures; for other
19420 languages we have to, so that we can get at method physnames
19421 to infer fully qualified class names, for DW_AT_specification,
19422 and for C++ template arguments. For C++, we also look one level
19423 inside functions to find template arguments (if the name of the
19424 function does not already contain the template arguments).
19425
19426 For Ada and Fortran, we need to scan the children of subprograms
19427 and lexical blocks as well because these languages allow the
19428 definition of nested entities that could be interesting for the
19429 debugger, such as nested subprograms for instance. */
19430 if (last_die->has_children
19431 && (load_all
19432 || last_die->tag == DW_TAG_namespace
19433 || last_die->tag == DW_TAG_module
19434 || last_die->tag == DW_TAG_enumeration_type
19435 || (cu->language == language_cplus
19436 && last_die->tag == DW_TAG_subprogram
19437 && (last_die->raw_name == NULL
19438 || strchr (last_die->raw_name, '<') == NULL))
19439 || (cu->language != language_c
19440 && (last_die->tag == DW_TAG_class_type
19441 || last_die->tag == DW_TAG_interface_type
19442 || last_die->tag == DW_TAG_structure_type
19443 || last_die->tag == DW_TAG_union_type))
19444 || ((cu->language == language_ada
19445 || cu->language == language_fortran)
19446 && (last_die->tag == DW_TAG_subprogram
19447 || last_die->tag == DW_TAG_lexical_block))))
19448 {
19449 nesting_level++;
19450 parent_die = last_die;
19451 continue;
19452 }
19453
19454 /* Otherwise we skip to the next sibling, if any. */
19455 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
19456
19457 /* Back to the top, do it again. */
19458 }
19459 }
19460
19461 partial_die_info::partial_die_info (sect_offset sect_off_,
19462 const struct abbrev_info *abbrev)
19463 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19464 {
19465 }
19466
19467 /* See class definition. */
19468
19469 const char *
19470 partial_die_info::name (dwarf2_cu *cu)
19471 {
19472 if (!canonical_name && raw_name != nullptr)
19473 {
19474 struct objfile *objfile = cu->per_objfile->objfile;
19475 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19476 canonical_name = 1;
19477 }
19478
19479 return raw_name;
19480 }
19481
19482 /* Read a minimal amount of information into the minimal die structure.
19483 INFO_PTR should point just after the initial uleb128 of a DIE. */
19484
19485 const gdb_byte *
19486 partial_die_info::read (const struct die_reader_specs *reader,
19487 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
19488 {
19489 struct dwarf2_cu *cu = reader->cu;
19490 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19491 unsigned int i;
19492 int has_low_pc_attr = 0;
19493 int has_high_pc_attr = 0;
19494 int high_pc_relative = 0;
19495
19496 for (i = 0; i < abbrev.num_attrs; ++i)
19497 {
19498 attribute attr;
19499 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
19500 /* String and address offsets that need to do the reprocessing have
19501 already been read at this point, so there is no need to wait until
19502 the loop terminates to do the reprocessing. */
19503 if (attr.requires_reprocessing_p ())
19504 read_attribute_reprocess (reader, &attr, tag);
19505 /* Store the data if it is of an attribute we want to keep in a
19506 partial symbol table. */
19507 switch (attr.name)
19508 {
19509 case DW_AT_name:
19510 switch (tag)
19511 {
19512 case DW_TAG_compile_unit:
19513 case DW_TAG_partial_unit:
19514 case DW_TAG_type_unit:
19515 /* Compilation units have a DW_AT_name that is a filename, not
19516 a source language identifier. */
19517 case DW_TAG_enumeration_type:
19518 case DW_TAG_enumerator:
19519 /* These tags always have simple identifiers already; no need
19520 to canonicalize them. */
19521 canonical_name = 1;
19522 raw_name = attr.as_string ();
19523 break;
19524 default:
19525 canonical_name = 0;
19526 raw_name = attr.as_string ();
19527 break;
19528 }
19529 break;
19530 case DW_AT_linkage_name:
19531 case DW_AT_MIPS_linkage_name:
19532 /* Note that both forms of linkage name might appear. We
19533 assume they will be the same, and we only store the last
19534 one we see. */
19535 linkage_name = attr.as_string ();
19536 break;
19537 case DW_AT_low_pc:
19538 has_low_pc_attr = 1;
19539 lowpc = attr.as_address ();
19540 break;
19541 case DW_AT_high_pc:
19542 has_high_pc_attr = 1;
19543 highpc = attr.as_address ();
19544 if (cu->header.version >= 4 && attr.form_is_constant ())
19545 high_pc_relative = 1;
19546 break;
19547 case DW_AT_location:
19548 /* Support the .debug_loc offsets. */
19549 if (attr.form_is_block ())
19550 {
19551 d.locdesc = attr.as_block ();
19552 }
19553 else if (attr.form_is_section_offset ())
19554 {
19555 dwarf2_complex_location_expr_complaint ();
19556 }
19557 else
19558 {
19559 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19560 "partial symbol information");
19561 }
19562 break;
19563 case DW_AT_external:
19564 is_external = attr.as_boolean ();
19565 break;
19566 case DW_AT_declaration:
19567 is_declaration = attr.as_boolean ();
19568 break;
19569 case DW_AT_type:
19570 has_type = 1;
19571 break;
19572 case DW_AT_abstract_origin:
19573 case DW_AT_specification:
19574 case DW_AT_extension:
19575 has_specification = 1;
19576 spec_offset = attr.get_ref_die_offset ();
19577 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19578 || cu->per_cu->is_dwz);
19579 break;
19580 case DW_AT_sibling:
19581 /* Ignore absolute siblings, they might point outside of
19582 the current compile unit. */
19583 if (attr.form == DW_FORM_ref_addr)
19584 complaint (_("ignoring absolute DW_AT_sibling"));
19585 else
19586 {
19587 const gdb_byte *buffer = reader->buffer;
19588 sect_offset off = attr.get_ref_die_offset ();
19589 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
19590
19591 if (sibling_ptr < info_ptr)
19592 complaint (_("DW_AT_sibling points backwards"));
19593 else if (sibling_ptr > reader->buffer_end)
19594 reader->die_section->overflow_complaint ();
19595 else
19596 sibling = sibling_ptr;
19597 }
19598 break;
19599 case DW_AT_byte_size:
19600 has_byte_size = 1;
19601 break;
19602 case DW_AT_const_value:
19603 has_const_value = 1;
19604 break;
19605 case DW_AT_calling_convention:
19606 /* DWARF doesn't provide a way to identify a program's source-level
19607 entry point. DW_AT_calling_convention attributes are only meant
19608 to describe functions' calling conventions.
19609
19610 However, because it's a necessary piece of information in
19611 Fortran, and before DWARF 4 DW_CC_program was the only
19612 piece of debugging information whose definition refers to
19613 a 'main program' at all, several compilers marked Fortran
19614 main programs with DW_CC_program --- even when those
19615 functions use the standard calling conventions.
19616
19617 Although DWARF now specifies a way to provide this
19618 information, we support this practice for backward
19619 compatibility. */
19620 if (attr.constant_value (0) == DW_CC_program
19621 && cu->language == language_fortran)
19622 main_subprogram = 1;
19623 break;
19624 case DW_AT_inline:
19625 {
19626 LONGEST value = attr.constant_value (-1);
19627 if (value == DW_INL_inlined
19628 || value == DW_INL_declared_inlined)
19629 may_be_inlined = 1;
19630 }
19631 break;
19632
19633 case DW_AT_import:
19634 if (tag == DW_TAG_imported_unit)
19635 {
19636 d.sect_off = attr.get_ref_die_offset ();
19637 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
19638 || cu->per_cu->is_dwz);
19639 }
19640 break;
19641
19642 case DW_AT_main_subprogram:
19643 main_subprogram = attr.as_boolean ();
19644 break;
19645
19646 case DW_AT_ranges:
19647 {
19648 /* Offset in the .debug_ranges or .debug_rnglist section (depending
19649 on DWARF version). */
19650 ULONGEST ranges_offset = attr.as_unsigned ();
19651
19652 /* See dwarf2_cu::gnu_ranges_base's doc for why we might want to add
19653 this value. */
19654 if (tag != DW_TAG_compile_unit)
19655 ranges_offset += cu->gnu_ranges_base;
19656
19657 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
19658 nullptr, tag))
19659 has_pc_info = 1;
19660 }
19661 break;
19662
19663 default:
19664 break;
19665 }
19666 }
19667
19668 /* For Ada, if both the name and the linkage name appear, we prefer
19669 the latter. This lets "catch exception" work better, regardless
19670 of the order in which the name and linkage name were emitted.
19671 Really, though, this is just a workaround for the fact that gdb
19672 doesn't store both the name and the linkage name. */
19673 if (cu->language == language_ada && linkage_name != nullptr)
19674 raw_name = linkage_name;
19675
19676 if (high_pc_relative)
19677 highpc += lowpc;
19678
19679 if (has_low_pc_attr && has_high_pc_attr)
19680 {
19681 /* When using the GNU linker, .gnu.linkonce. sections are used to
19682 eliminate duplicate copies of functions and vtables and such.
19683 The linker will arbitrarily choose one and discard the others.
19684 The AT_*_pc values for such functions refer to local labels in
19685 these sections. If the section from that file was discarded, the
19686 labels are not in the output, so the relocs get a value of 0.
19687 If this is a discarded function, mark the pc bounds as invalid,
19688 so that GDB will ignore it. */
19689 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
19690 {
19691 struct objfile *objfile = per_objfile->objfile;
19692 struct gdbarch *gdbarch = objfile->arch ();
19693
19694 complaint (_("DW_AT_low_pc %s is zero "
19695 "for DIE at %s [in module %s]"),
19696 paddress (gdbarch, lowpc),
19697 sect_offset_str (sect_off),
19698 objfile_name (objfile));
19699 }
19700 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
19701 else if (lowpc >= highpc)
19702 {
19703 struct objfile *objfile = per_objfile->objfile;
19704 struct gdbarch *gdbarch = objfile->arch ();
19705
19706 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
19707 "for DIE at %s [in module %s]"),
19708 paddress (gdbarch, lowpc),
19709 paddress (gdbarch, highpc),
19710 sect_offset_str (sect_off),
19711 objfile_name (objfile));
19712 }
19713 else
19714 has_pc_info = 1;
19715 }
19716
19717 return info_ptr;
19718 }
19719
19720 /* Find a cached partial DIE at OFFSET in CU. */
19721
19722 struct partial_die_info *
19723 dwarf2_cu::find_partial_die (sect_offset sect_off)
19724 {
19725 struct partial_die_info *lookup_die = NULL;
19726 struct partial_die_info part_die (sect_off);
19727
19728 lookup_die = ((struct partial_die_info *)
19729 htab_find_with_hash (partial_dies, &part_die,
19730 to_underlying (sect_off)));
19731
19732 return lookup_die;
19733 }
19734
19735 /* Find a partial DIE at OFFSET, which may or may not be in CU,
19736 except in the case of .debug_types DIEs which do not reference
19737 outside their CU (they do however referencing other types via
19738 DW_FORM_ref_sig8). */
19739
19740 static const struct cu_partial_die_info
19741 find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
19742 {
19743 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19744 struct objfile *objfile = per_objfile->objfile;
19745 struct partial_die_info *pd = NULL;
19746
19747 if (offset_in_dwz == cu->per_cu->is_dwz
19748 && cu->header.offset_in_cu_p (sect_off))
19749 {
19750 pd = cu->find_partial_die (sect_off);
19751 if (pd != NULL)
19752 return { cu, pd };
19753 /* We missed recording what we needed.
19754 Load all dies and try again. */
19755 }
19756 else
19757 {
19758 /* TUs don't reference other CUs/TUs (except via type signatures). */
19759 if (cu->per_cu->is_debug_types)
19760 {
19761 error (_("Dwarf Error: Type Unit at offset %s contains"
19762 " external reference to offset %s [in module %s].\n"),
19763 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
19764 bfd_get_filename (objfile->obfd));
19765 }
19766 dwarf2_per_cu_data *per_cu
19767 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
19768 per_objfile);
19769
19770 cu = per_objfile->get_cu (per_cu);
19771 if (cu == NULL || cu->partial_dies == NULL)
19772 load_partial_comp_unit (per_cu, per_objfile, nullptr);
19773
19774 cu = per_objfile->get_cu (per_cu);
19775
19776 cu->last_used = 0;
19777 pd = cu->find_partial_die (sect_off);
19778 }
19779
19780 /* If we didn't find it, and not all dies have been loaded,
19781 load them all and try again. */
19782
19783 if (pd == NULL && cu->per_cu->load_all_dies == 0)
19784 {
19785 cu->per_cu->load_all_dies = 1;
19786
19787 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19788 THIS_CU->cu may already be in use. So we can't just free it and
19789 replace its DIEs with the ones we read in. Instead, we leave those
19790 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19791 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19792 set. */
19793 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
19794
19795 pd = cu->find_partial_die (sect_off);
19796 }
19797
19798 if (pd == NULL)
19799 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
19800 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
19801 return { cu, pd };
19802 }
19803
19804 /* See if we can figure out if the class lives in a namespace. We do
19805 this by looking for a member function; its demangled name will
19806 contain namespace info, if there is any. */
19807
19808 static void
19809 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19810 struct dwarf2_cu *cu)
19811 {
19812 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19813 what template types look like, because the demangler
19814 frequently doesn't give the same name as the debug info. We
19815 could fix this by only using the demangled name to get the
19816 prefix (but see comment in read_structure_type). */
19817
19818 struct partial_die_info *real_pdi;
19819 struct partial_die_info *child_pdi;
19820
19821 /* If this DIE (this DIE's specification, if any) has a parent, then
19822 we should not do this. We'll prepend the parent's fully qualified
19823 name when we create the partial symbol. */
19824
19825 real_pdi = struct_pdi;
19826 while (real_pdi->has_specification)
19827 {
19828 auto res = find_partial_die (real_pdi->spec_offset,
19829 real_pdi->spec_is_dwz, cu);
19830 real_pdi = res.pdi;
19831 cu = res.cu;
19832 }
19833
19834 if (real_pdi->die_parent != NULL)
19835 return;
19836
19837 for (child_pdi = struct_pdi->die_child;
19838 child_pdi != NULL;
19839 child_pdi = child_pdi->die_sibling)
19840 {
19841 if (child_pdi->tag == DW_TAG_subprogram
19842 && child_pdi->linkage_name != NULL)
19843 {
19844 gdb::unique_xmalloc_ptr<char> actual_class_name
19845 (cu->language_defn->class_name_from_physname
19846 (child_pdi->linkage_name));
19847 if (actual_class_name != NULL)
19848 {
19849 struct objfile *objfile = cu->per_objfile->objfile;
19850 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19851 struct_pdi->canonical_name = 1;
19852 }
19853 break;
19854 }
19855 }
19856 }
19857
19858 /* Return true if a DIE with TAG may have the DW_AT_const_value
19859 attribute. */
19860
19861 static bool
19862 can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19863 {
19864 switch (tag)
19865 {
19866 case DW_TAG_constant:
19867 case DW_TAG_enumerator:
19868 case DW_TAG_formal_parameter:
19869 case DW_TAG_template_value_param:
19870 case DW_TAG_variable:
19871 return true;
19872 }
19873
19874 return false;
19875 }
19876
19877 void
19878 partial_die_info::fixup (struct dwarf2_cu *cu)
19879 {
19880 /* Once we've fixed up a die, there's no point in doing so again.
19881 This also avoids a memory leak if we were to call
19882 guess_partial_die_structure_name multiple times. */
19883 if (fixup_called)
19884 return;
19885
19886 /* If we found a reference attribute and the DIE has no name, try
19887 to find a name in the referred to DIE. */
19888
19889 if (raw_name == NULL && has_specification)
19890 {
19891 struct partial_die_info *spec_die;
19892
19893 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19894 spec_die = res.pdi;
19895 cu = res.cu;
19896
19897 spec_die->fixup (cu);
19898
19899 if (spec_die->raw_name)
19900 {
19901 raw_name = spec_die->raw_name;
19902 canonical_name = spec_die->canonical_name;
19903
19904 /* Copy DW_AT_external attribute if it is set. */
19905 if (spec_die->is_external)
19906 is_external = spec_die->is_external;
19907 }
19908 }
19909
19910 if (!has_const_value && has_specification
19911 && can_have_DW_AT_const_value_p (tag))
19912 {
19913 struct partial_die_info *spec_die;
19914
19915 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
19916 spec_die = res.pdi;
19917 cu = res.cu;
19918
19919 spec_die->fixup (cu);
19920
19921 if (spec_die->has_const_value)
19922 {
19923 /* Copy DW_AT_const_value attribute if it is set. */
19924 has_const_value = spec_die->has_const_value;
19925 }
19926 }
19927
19928 /* Set default names for some unnamed DIEs. */
19929
19930 if (raw_name == NULL && tag == DW_TAG_namespace)
19931 {
19932 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
19933 canonical_name = 1;
19934 }
19935
19936 /* If there is no parent die to provide a namespace, and there are
19937 children, see if we can determine the namespace from their linkage
19938 name. */
19939 if (cu->language == language_cplus
19940 && !cu->per_objfile->per_bfd->types.empty ()
19941 && die_parent == NULL
19942 && has_children
19943 && (tag == DW_TAG_class_type
19944 || tag == DW_TAG_structure_type
19945 || tag == DW_TAG_union_type))
19946 guess_partial_die_structure_name (this, cu);
19947
19948 /* GCC might emit a nameless struct or union that has a linkage
19949 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
19950 if (raw_name == NULL
19951 && (tag == DW_TAG_class_type
19952 || tag == DW_TAG_interface_type
19953 || tag == DW_TAG_structure_type
19954 || tag == DW_TAG_union_type)
19955 && linkage_name != NULL)
19956 {
19957 gdb::unique_xmalloc_ptr<char> demangled
19958 (gdb_demangle (linkage_name, DMGL_TYPES));
19959 if (demangled != nullptr)
19960 {
19961 const char *base;
19962
19963 /* Strip any leading namespaces/classes, keep only the base name.
19964 DW_AT_name for named DIEs does not contain the prefixes. */
19965 base = strrchr (demangled.get (), ':');
19966 if (base && base > demangled.get () && base[-1] == ':')
19967 base++;
19968 else
19969 base = demangled.get ();
19970
19971 struct objfile *objfile = cu->per_objfile->objfile;
19972 raw_name = objfile->intern (base);
19973 canonical_name = 1;
19974 }
19975 }
19976
19977 fixup_called = 1;
19978 }
19979
19980 /* Read the .debug_loclists or .debug_rnglists header (they are the same format)
19981 contents from the given SECTION in the HEADER.
19982
19983 HEADER_OFFSET is the offset of the header in the section. */
19984 static void
19985 read_loclists_rnglists_header (struct loclists_rnglists_header *header,
19986 struct dwarf2_section_info *section,
19987 sect_offset header_offset)
19988 {
19989 unsigned int bytes_read;
19990 bfd *abfd = section->get_bfd_owner ();
19991 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
19992
19993 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
19994 info_ptr += bytes_read;
19995
19996 header->version = read_2_bytes (abfd, info_ptr);
19997 info_ptr += 2;
19998
19999 header->addr_size = read_1_byte (abfd, info_ptr);
20000 info_ptr += 1;
20001
20002 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20003 info_ptr += 1;
20004
20005 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20006 }
20007
20008 /* Return the DW_AT_loclists_base value for the CU. */
20009 static ULONGEST
20010 lookup_loclist_base (struct dwarf2_cu *cu)
20011 {
20012 /* For the .dwo unit, the loclist_base points to the first offset following
20013 the header. The header consists of the following entities-
20014 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20015 bit format)
20016 2. version (2 bytes)
20017 3. address size (1 byte)
20018 4. segment selector size (1 byte)
20019 5. offset entry count (4 bytes)
20020 These sizes are derived as per the DWARFv5 standard. */
20021 if (cu->dwo_unit != nullptr)
20022 {
20023 if (cu->header.initial_length_size == 4)
20024 return LOCLIST_HEADER_SIZE32;
20025 return LOCLIST_HEADER_SIZE64;
20026 }
20027 return cu->loclist_base;
20028 }
20029
20030 /* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20031 array of offsets in the .debug_loclists section. */
20032
20033 static sect_offset
20034 read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20035 {
20036 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20037 struct objfile *objfile = per_objfile->objfile;
20038 bfd *abfd = objfile->obfd;
20039 ULONGEST loclist_header_size =
20040 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20041 : LOCLIST_HEADER_SIZE64);
20042 ULONGEST loclist_base = lookup_loclist_base (cu);
20043
20044 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20045 ULONGEST start_offset =
20046 loclist_base + loclist_index * cu->header.offset_size;
20047
20048 /* Get loclists section. */
20049 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20050
20051 /* Read the loclists section content. */
20052 section->read (objfile);
20053 if (section->buffer == NULL)
20054 error (_("DW_FORM_loclistx used without .debug_loclists "
20055 "section [in module %s]"), objfile_name (objfile));
20056
20057 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20058 so if loclist_base is smaller than the header size, we have a problem. */
20059 if (loclist_base < loclist_header_size)
20060 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20061 objfile_name (objfile));
20062
20063 /* Read the header of the loclists contribution. */
20064 struct loclists_rnglists_header header;
20065 read_loclists_rnglists_header (&header, section,
20066 (sect_offset) (loclist_base - loclist_header_size));
20067
20068 /* Verify the loclist index is valid. */
20069 if (loclist_index >= header.offset_entry_count)
20070 error (_("DW_FORM_loclistx pointing outside of "
20071 ".debug_loclists offset array [in module %s]"),
20072 objfile_name (objfile));
20073
20074 /* Validate that reading won't go beyond the end of the section. */
20075 if (start_offset + cu->header.offset_size > section->size)
20076 error (_("Reading DW_FORM_loclistx index beyond end of"
20077 ".debug_loclists section [in module %s]"),
20078 objfile_name (objfile));
20079
20080 const gdb_byte *info_ptr = section->buffer + start_offset;
20081
20082 if (cu->header.offset_size == 4)
20083 return (sect_offset) (bfd_get_32 (abfd, info_ptr) + loclist_base);
20084 else
20085 return (sect_offset) (bfd_get_64 (abfd, info_ptr) + loclist_base);
20086 }
20087
20088 /* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20089 array of offsets in the .debug_rnglists section. */
20090
20091 static sect_offset
20092 read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20093 dwarf_tag tag)
20094 {
20095 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20096 struct objfile *objfile = dwarf2_per_objfile->objfile;
20097 bfd *abfd = objfile->obfd;
20098 ULONGEST rnglist_header_size =
20099 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20100 : RNGLIST_HEADER_SIZE64);
20101
20102 /* When reading a DW_FORM_rnglistx from a DWO, we read from the DWO's
20103 .debug_rnglists.dwo section. The rnglists base given in the skeleton
20104 doesn't apply. */
20105 ULONGEST rnglist_base =
20106 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->rnglists_base;
20107
20108 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
20109 ULONGEST start_offset =
20110 rnglist_base + rnglist_index * cu->header.offset_size;
20111
20112 /* Get rnglists section. */
20113 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20114
20115 /* Read the rnglists section content. */
20116 section->read (objfile);
20117 if (section->buffer == nullptr)
20118 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20119 "[in module %s]"),
20120 objfile_name (objfile));
20121
20122 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20123 so if rnglist_base is smaller than the header size, we have a problem. */
20124 if (rnglist_base < rnglist_header_size)
20125 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20126 objfile_name (objfile));
20127
20128 /* Read the header of the rnglists contribution. */
20129 struct loclists_rnglists_header header;
20130 read_loclists_rnglists_header (&header, section,
20131 (sect_offset) (rnglist_base - rnglist_header_size));
20132
20133 /* Verify the rnglist index is valid. */
20134 if (rnglist_index >= header.offset_entry_count)
20135 error (_("DW_FORM_rnglistx index pointing outside of "
20136 ".debug_rnglists offset array [in module %s]"),
20137 objfile_name (objfile));
20138
20139 /* Validate that reading won't go beyond the end of the section. */
20140 if (start_offset + cu->header.offset_size > section->size)
20141 error (_("Reading DW_FORM_rnglistx index beyond end of"
20142 ".debug_rnglists section [in module %s]"),
20143 objfile_name (objfile));
20144
20145 const gdb_byte *info_ptr = section->buffer + start_offset;
20146
20147 if (cu->header.offset_size == 4)
20148 return (sect_offset) (read_4_bytes (abfd, info_ptr) + rnglist_base);
20149 else
20150 return (sect_offset) (read_8_bytes (abfd, info_ptr) + rnglist_base);
20151 }
20152
20153 /* Process the attributes that had to be skipped in the first round. These
20154 attributes are the ones that need str_offsets_base or addr_base attributes.
20155 They could not have been processed in the first round, because at the time
20156 the values of str_offsets_base or addr_base may not have been known. */
20157 static void
20158 read_attribute_reprocess (const struct die_reader_specs *reader,
20159 struct attribute *attr, dwarf_tag tag)
20160 {
20161 struct dwarf2_cu *cu = reader->cu;
20162 switch (attr->form)
20163 {
20164 case DW_FORM_addrx:
20165 case DW_FORM_GNU_addr_index:
20166 attr->set_address (read_addr_index (cu,
20167 attr->as_unsigned_reprocess ()));
20168 break;
20169 case DW_FORM_loclistx:
20170 {
20171 sect_offset loclists_sect_off
20172 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20173
20174 attr->set_unsigned (to_underlying (loclists_sect_off));
20175 }
20176 break;
20177 case DW_FORM_rnglistx:
20178 {
20179 sect_offset rnglists_sect_off
20180 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20181
20182 attr->set_unsigned (to_underlying (rnglists_sect_off));
20183 }
20184 break;
20185 case DW_FORM_strx:
20186 case DW_FORM_strx1:
20187 case DW_FORM_strx2:
20188 case DW_FORM_strx3:
20189 case DW_FORM_strx4:
20190 case DW_FORM_GNU_str_index:
20191 {
20192 unsigned int str_index = attr->as_unsigned_reprocess ();
20193 gdb_assert (!attr->canonical_string_p ());
20194 if (reader->dwo_file != NULL)
20195 attr->set_string_noncanonical (read_dwo_str_index (reader,
20196 str_index));
20197 else
20198 attr->set_string_noncanonical (read_stub_str_index (cu,
20199 str_index));
20200 break;
20201 }
20202 default:
20203 gdb_assert_not_reached (_("Unexpected DWARF form."));
20204 }
20205 }
20206
20207 /* Read an attribute value described by an attribute form. */
20208
20209 static const gdb_byte *
20210 read_attribute_value (const struct die_reader_specs *reader,
20211 struct attribute *attr, unsigned form,
20212 LONGEST implicit_const, const gdb_byte *info_ptr)
20213 {
20214 struct dwarf2_cu *cu = reader->cu;
20215 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20216 struct objfile *objfile = per_objfile->objfile;
20217 bfd *abfd = reader->abfd;
20218 struct comp_unit_head *cu_header = &cu->header;
20219 unsigned int bytes_read;
20220 struct dwarf_block *blk;
20221
20222 attr->form = (enum dwarf_form) form;
20223 switch (form)
20224 {
20225 case DW_FORM_ref_addr:
20226 if (cu_header->version == 2)
20227 attr->set_unsigned (cu_header->read_address (abfd, info_ptr,
20228 &bytes_read));
20229 else
20230 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20231 &bytes_read));
20232 info_ptr += bytes_read;
20233 break;
20234 case DW_FORM_GNU_ref_alt:
20235 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20236 &bytes_read));
20237 info_ptr += bytes_read;
20238 break;
20239 case DW_FORM_addr:
20240 {
20241 struct gdbarch *gdbarch = objfile->arch ();
20242 CORE_ADDR addr = cu_header->read_address (abfd, info_ptr, &bytes_read);
20243 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20244 attr->set_address (addr);
20245 info_ptr += bytes_read;
20246 }
20247 break;
20248 case DW_FORM_block2:
20249 blk = dwarf_alloc_block (cu);
20250 blk->size = read_2_bytes (abfd, info_ptr);
20251 info_ptr += 2;
20252 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20253 info_ptr += blk->size;
20254 attr->set_block (blk);
20255 break;
20256 case DW_FORM_block4:
20257 blk = dwarf_alloc_block (cu);
20258 blk->size = read_4_bytes (abfd, info_ptr);
20259 info_ptr += 4;
20260 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20261 info_ptr += blk->size;
20262 attr->set_block (blk);
20263 break;
20264 case DW_FORM_data2:
20265 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
20266 info_ptr += 2;
20267 break;
20268 case DW_FORM_data4:
20269 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
20270 info_ptr += 4;
20271 break;
20272 case DW_FORM_data8:
20273 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
20274 info_ptr += 8;
20275 break;
20276 case DW_FORM_data16:
20277 blk = dwarf_alloc_block (cu);
20278 blk->size = 16;
20279 blk->data = read_n_bytes (abfd, info_ptr, 16);
20280 info_ptr += 16;
20281 attr->set_block (blk);
20282 break;
20283 case DW_FORM_sec_offset:
20284 attr->set_unsigned (cu_header->read_offset (abfd, info_ptr,
20285 &bytes_read));
20286 info_ptr += bytes_read;
20287 break;
20288 case DW_FORM_loclistx:
20289 {
20290 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20291 &bytes_read));
20292 info_ptr += bytes_read;
20293 }
20294 break;
20295 case DW_FORM_string:
20296 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20297 &bytes_read));
20298 info_ptr += bytes_read;
20299 break;
20300 case DW_FORM_strp:
20301 if (!cu->per_cu->is_dwz)
20302 {
20303 attr->set_string_noncanonical
20304 (read_indirect_string (per_objfile,
20305 abfd, info_ptr, cu_header,
20306 &bytes_read));
20307 info_ptr += bytes_read;
20308 break;
20309 }
20310 /* FALLTHROUGH */
20311 case DW_FORM_line_strp:
20312 if (!cu->per_cu->is_dwz)
20313 {
20314 attr->set_string_noncanonical
20315 (per_objfile->read_line_string (info_ptr, cu_header,
20316 &bytes_read));
20317 info_ptr += bytes_read;
20318 break;
20319 }
20320 /* FALLTHROUGH */
20321 case DW_FORM_GNU_strp_alt:
20322 {
20323 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20324 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20325 &bytes_read);
20326
20327 attr->set_string_noncanonical
20328 (dwz->read_string (objfile, str_offset));
20329 info_ptr += bytes_read;
20330 }
20331 break;
20332 case DW_FORM_exprloc:
20333 case DW_FORM_block:
20334 blk = dwarf_alloc_block (cu);
20335 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20336 info_ptr += bytes_read;
20337 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20338 info_ptr += blk->size;
20339 attr->set_block (blk);
20340 break;
20341 case DW_FORM_block1:
20342 blk = dwarf_alloc_block (cu);
20343 blk->size = read_1_byte (abfd, info_ptr);
20344 info_ptr += 1;
20345 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20346 info_ptr += blk->size;
20347 attr->set_block (blk);
20348 break;
20349 case DW_FORM_data1:
20350 case DW_FORM_flag:
20351 attr->set_unsigned (read_1_byte (abfd, info_ptr));
20352 info_ptr += 1;
20353 break;
20354 case DW_FORM_flag_present:
20355 attr->set_unsigned (1);
20356 break;
20357 case DW_FORM_sdata:
20358 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
20359 info_ptr += bytes_read;
20360 break;
20361 case DW_FORM_rnglistx:
20362 {
20363 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20364 &bytes_read));
20365 info_ptr += bytes_read;
20366 }
20367 break;
20368 case DW_FORM_udata:
20369 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
20370 info_ptr += bytes_read;
20371 break;
20372 case DW_FORM_ref1:
20373 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20374 + read_1_byte (abfd, info_ptr)));
20375 info_ptr += 1;
20376 break;
20377 case DW_FORM_ref2:
20378 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20379 + read_2_bytes (abfd, info_ptr)));
20380 info_ptr += 2;
20381 break;
20382 case DW_FORM_ref4:
20383 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20384 + read_4_bytes (abfd, info_ptr)));
20385 info_ptr += 4;
20386 break;
20387 case DW_FORM_ref8:
20388 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20389 + read_8_bytes (abfd, info_ptr)));
20390 info_ptr += 8;
20391 break;
20392 case DW_FORM_ref_sig8:
20393 attr->set_signature (read_8_bytes (abfd, info_ptr));
20394 info_ptr += 8;
20395 break;
20396 case DW_FORM_ref_udata:
20397 attr->set_unsigned ((to_underlying (cu_header->sect_off)
20398 + read_unsigned_leb128 (abfd, info_ptr,
20399 &bytes_read)));
20400 info_ptr += bytes_read;
20401 break;
20402 case DW_FORM_indirect:
20403 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20404 info_ptr += bytes_read;
20405 if (form == DW_FORM_implicit_const)
20406 {
20407 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20408 info_ptr += bytes_read;
20409 }
20410 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
20411 info_ptr);
20412 break;
20413 case DW_FORM_implicit_const:
20414 attr->set_signed (implicit_const);
20415 break;
20416 case DW_FORM_addrx:
20417 case DW_FORM_GNU_addr_index:
20418 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20419 &bytes_read));
20420 info_ptr += bytes_read;
20421 break;
20422 case DW_FORM_strx:
20423 case DW_FORM_strx1:
20424 case DW_FORM_strx2:
20425 case DW_FORM_strx3:
20426 case DW_FORM_strx4:
20427 case DW_FORM_GNU_str_index:
20428 {
20429 ULONGEST str_index;
20430 if (form == DW_FORM_strx1)
20431 {
20432 str_index = read_1_byte (abfd, info_ptr);
20433 info_ptr += 1;
20434 }
20435 else if (form == DW_FORM_strx2)
20436 {
20437 str_index = read_2_bytes (abfd, info_ptr);
20438 info_ptr += 2;
20439 }
20440 else if (form == DW_FORM_strx3)
20441 {
20442 str_index = read_3_bytes (abfd, info_ptr);
20443 info_ptr += 3;
20444 }
20445 else if (form == DW_FORM_strx4)
20446 {
20447 str_index = read_4_bytes (abfd, info_ptr);
20448 info_ptr += 4;
20449 }
20450 else
20451 {
20452 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20453 info_ptr += bytes_read;
20454 }
20455 attr->set_unsigned_reprocess (str_index);
20456 }
20457 break;
20458 default:
20459 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
20460 dwarf_form_name (form),
20461 bfd_get_filename (abfd));
20462 }
20463
20464 /* Super hack. */
20465 if (cu->per_cu->is_dwz && attr->form_is_ref ())
20466 attr->form = DW_FORM_GNU_ref_alt;
20467
20468 /* We have seen instances where the compiler tried to emit a byte
20469 size attribute of -1 which ended up being encoded as an unsigned
20470 0xffffffff. Although 0xffffffff is technically a valid size value,
20471 an object of this size seems pretty unlikely so we can relatively
20472 safely treat these cases as if the size attribute was invalid and
20473 treat them as zero by default. */
20474 if (attr->name == DW_AT_byte_size
20475 && form == DW_FORM_data4
20476 && attr->as_unsigned () >= 0xffffffff)
20477 {
20478 complaint
20479 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20480 hex_string (attr->as_unsigned ()));
20481 attr->set_unsigned (0);
20482 }
20483
20484 return info_ptr;
20485 }
20486
20487 /* Read an attribute described by an abbreviated attribute. */
20488
20489 static const gdb_byte *
20490 read_attribute (const struct die_reader_specs *reader,
20491 struct attribute *attr, const struct attr_abbrev *abbrev,
20492 const gdb_byte *info_ptr)
20493 {
20494 attr->name = abbrev->name;
20495 attr->string_is_canonical = 0;
20496 attr->requires_reprocessing = 0;
20497 return read_attribute_value (reader, attr, abbrev->form,
20498 abbrev->implicit_const, info_ptr);
20499 }
20500
20501 /* Return pointer to string at .debug_str offset STR_OFFSET. */
20502
20503 static const char *
20504 read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
20505 LONGEST str_offset)
20506 {
20507 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20508 str_offset, "DW_FORM_strp");
20509 }
20510
20511 /* Return pointer to string at .debug_str offset as read from BUF.
20512 BUF is assumed to be in a compilation unit described by CU_HEADER.
20513 Return *BYTES_READ_PTR count of bytes read from BUF. */
20514
20515 static const char *
20516 read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
20517 const gdb_byte *buf,
20518 const struct comp_unit_head *cu_header,
20519 unsigned int *bytes_read_ptr)
20520 {
20521 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20522
20523 return read_indirect_string_at_offset (per_objfile, str_offset);
20524 }
20525
20526 /* See read.h. */
20527
20528 const char *
20529 dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
20530 const struct comp_unit_head *cu_header,
20531 unsigned int *bytes_read_ptr)
20532 {
20533 bfd *abfd = objfile->obfd;
20534 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
20535
20536 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
20537 }
20538
20539 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
20540 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
20541 ADDR_SIZE is the size of addresses from the CU header. */
20542
20543 static CORE_ADDR
20544 read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20545 gdb::optional<ULONGEST> addr_base, int addr_size)
20546 {
20547 struct objfile *objfile = per_objfile->objfile;
20548 bfd *abfd = objfile->obfd;
20549 const gdb_byte *info_ptr;
20550 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
20551
20552 per_objfile->per_bfd->addr.read (objfile);
20553 if (per_objfile->per_bfd->addr.buffer == NULL)
20554 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
20555 objfile_name (objfile));
20556 if (addr_base_or_zero + addr_index * addr_size
20557 >= per_objfile->per_bfd->addr.size)
20558 error (_("DW_FORM_addr_index pointing outside of "
20559 ".debug_addr section [in module %s]"),
20560 objfile_name (objfile));
20561 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20562 + addr_index * addr_size);
20563 if (addr_size == 4)
20564 return bfd_get_32 (abfd, info_ptr);
20565 else
20566 return bfd_get_64 (abfd, info_ptr);
20567 }
20568
20569 /* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20570
20571 static CORE_ADDR
20572 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20573 {
20574 return read_addr_index_1 (cu->per_objfile, addr_index,
20575 cu->addr_base, cu->header.addr_size);
20576 }
20577
20578 /* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20579
20580 static CORE_ADDR
20581 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
20582 unsigned int *bytes_read)
20583 {
20584 bfd *abfd = cu->per_objfile->objfile->obfd;
20585 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20586
20587 return read_addr_index (cu, addr_index);
20588 }
20589
20590 /* See read.h. */
20591
20592 CORE_ADDR
20593 dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
20594 dwarf2_per_objfile *per_objfile,
20595 unsigned int addr_index)
20596 {
20597 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
20598 gdb::optional<ULONGEST> addr_base;
20599 int addr_size;
20600
20601 /* We need addr_base and addr_size.
20602 If we don't have PER_CU->cu, we have to get it.
20603 Nasty, but the alternative is storing the needed info in PER_CU,
20604 which at this point doesn't seem justified: it's not clear how frequently
20605 it would get used and it would increase the size of every PER_CU.
20606 Entry points like dwarf2_per_cu_addr_size do a similar thing
20607 so we're not in uncharted territory here.
20608 Alas we need to be a bit more complicated as addr_base is contained
20609 in the DIE.
20610
20611 We don't need to read the entire CU(/TU).
20612 We just need the header and top level die.
20613
20614 IWBN to use the aging mechanism to let us lazily later discard the CU.
20615 For now we skip this optimization. */
20616
20617 if (cu != NULL)
20618 {
20619 addr_base = cu->addr_base;
20620 addr_size = cu->header.addr_size;
20621 }
20622 else
20623 {
20624 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
20625 addr_base = reader.cu->addr_base;
20626 addr_size = reader.cu->header.addr_size;
20627 }
20628
20629 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
20630 }
20631
20632 /* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20633 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20634 DWO file. */
20635
20636 static const char *
20637 read_str_index (struct dwarf2_cu *cu,
20638 struct dwarf2_section_info *str_section,
20639 struct dwarf2_section_info *str_offsets_section,
20640 ULONGEST str_offsets_base, ULONGEST str_index)
20641 {
20642 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20643 struct objfile *objfile = per_objfile->objfile;
20644 const char *objf_name = objfile_name (objfile);
20645 bfd *abfd = objfile->obfd;
20646 const gdb_byte *info_ptr;
20647 ULONGEST str_offset;
20648 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
20649
20650 str_section->read (objfile);
20651 str_offsets_section->read (objfile);
20652 if (str_section->buffer == NULL)
20653 error (_("%s used without %s section"
20654 " in CU at offset %s [in module %s]"),
20655 form_name, str_section->get_name (),
20656 sect_offset_str (cu->header.sect_off), objf_name);
20657 if (str_offsets_section->buffer == NULL)
20658 error (_("%s used without %s section"
20659 " in CU at offset %s [in module %s]"),
20660 form_name, str_section->get_name (),
20661 sect_offset_str (cu->header.sect_off), objf_name);
20662 info_ptr = (str_offsets_section->buffer
20663 + str_offsets_base
20664 + str_index * cu->header.offset_size);
20665 if (cu->header.offset_size == 4)
20666 str_offset = bfd_get_32 (abfd, info_ptr);
20667 else
20668 str_offset = bfd_get_64 (abfd, info_ptr);
20669 if (str_offset >= str_section->size)
20670 error (_("Offset from %s pointing outside of"
20671 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20672 form_name, sect_offset_str (cu->header.sect_off), objf_name);
20673 return (const char *) (str_section->buffer + str_offset);
20674 }
20675
20676 /* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20677
20678 static const char *
20679 read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20680 {
20681 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20682 ? reader->cu->header.addr_size : 0;
20683 return read_str_index (reader->cu,
20684 &reader->dwo_file->sections.str,
20685 &reader->dwo_file->sections.str_offsets,
20686 str_offsets_base, str_index);
20687 }
20688
20689 /* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20690
20691 static const char *
20692 read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20693 {
20694 struct objfile *objfile = cu->per_objfile->objfile;
20695 const char *objf_name = objfile_name (objfile);
20696 static const char form_name[] = "DW_FORM_GNU_str_index";
20697 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20698
20699 if (!cu->str_offsets_base.has_value ())
20700 error (_("%s used in Fission stub without %s"
20701 " in CU at offset 0x%lx [in module %s]"),
20702 form_name, str_offsets_attr_name,
20703 (long) cu->header.offset_size, objf_name);
20704
20705 return read_str_index (cu,
20706 &cu->per_objfile->per_bfd->str,
20707 &cu->per_objfile->per_bfd->str_offsets,
20708 *cu->str_offsets_base, str_index);
20709 }
20710
20711 /* Return the length of an LEB128 number in BUF. */
20712
20713 static int
20714 leb128_size (const gdb_byte *buf)
20715 {
20716 const gdb_byte *begin = buf;
20717 gdb_byte byte;
20718
20719 while (1)
20720 {
20721 byte = *buf++;
20722 if ((byte & 128) == 0)
20723 return buf - begin;
20724 }
20725 }
20726
20727 static void
20728 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
20729 {
20730 switch (lang)
20731 {
20732 case DW_LANG_C89:
20733 case DW_LANG_C99:
20734 case DW_LANG_C11:
20735 case DW_LANG_C:
20736 case DW_LANG_UPC:
20737 cu->language = language_c;
20738 break;
20739 case DW_LANG_Java:
20740 case DW_LANG_C_plus_plus:
20741 case DW_LANG_C_plus_plus_11:
20742 case DW_LANG_C_plus_plus_14:
20743 cu->language = language_cplus;
20744 break;
20745 case DW_LANG_D:
20746 cu->language = language_d;
20747 break;
20748 case DW_LANG_Fortran77:
20749 case DW_LANG_Fortran90:
20750 case DW_LANG_Fortran95:
20751 case DW_LANG_Fortran03:
20752 case DW_LANG_Fortran08:
20753 cu->language = language_fortran;
20754 break;
20755 case DW_LANG_Go:
20756 cu->language = language_go;
20757 break;
20758 case DW_LANG_Mips_Assembler:
20759 cu->language = language_asm;
20760 break;
20761 case DW_LANG_Ada83:
20762 case DW_LANG_Ada95:
20763 cu->language = language_ada;
20764 break;
20765 case DW_LANG_Modula2:
20766 cu->language = language_m2;
20767 break;
20768 case DW_LANG_Pascal83:
20769 cu->language = language_pascal;
20770 break;
20771 case DW_LANG_ObjC:
20772 cu->language = language_objc;
20773 break;
20774 case DW_LANG_Rust:
20775 case DW_LANG_Rust_old:
20776 cu->language = language_rust;
20777 break;
20778 case DW_LANG_Cobol74:
20779 case DW_LANG_Cobol85:
20780 default:
20781 cu->language = language_minimal;
20782 break;
20783 }
20784 cu->language_defn = language_def (cu->language);
20785 }
20786
20787 /* Return the named attribute or NULL if not there. */
20788
20789 static struct attribute *
20790 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20791 {
20792 for (;;)
20793 {
20794 unsigned int i;
20795 struct attribute *spec = NULL;
20796
20797 for (i = 0; i < die->num_attrs; ++i)
20798 {
20799 if (die->attrs[i].name == name)
20800 return &die->attrs[i];
20801 if (die->attrs[i].name == DW_AT_specification
20802 || die->attrs[i].name == DW_AT_abstract_origin)
20803 spec = &die->attrs[i];
20804 }
20805
20806 if (!spec)
20807 break;
20808
20809 die = follow_die_ref (die, spec, &cu);
20810 }
20811
20812 return NULL;
20813 }
20814
20815 /* Return the string associated with a string-typed attribute, or NULL if it
20816 is either not found or is of an incorrect type. */
20817
20818 static const char *
20819 dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20820 {
20821 struct attribute *attr;
20822 const char *str = NULL;
20823
20824 attr = dwarf2_attr (die, name, cu);
20825
20826 if (attr != NULL)
20827 {
20828 str = attr->as_string ();
20829 if (str == nullptr)
20830 complaint (_("string type expected for attribute %s for "
20831 "DIE at %s in module %s"),
20832 dwarf_attr_name (name), sect_offset_str (die->sect_off),
20833 objfile_name (cu->per_objfile->objfile));
20834 }
20835
20836 return str;
20837 }
20838
20839 /* Return the dwo name or NULL if not present. If present, it is in either
20840 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
20841 static const char *
20842 dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20843 {
20844 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20845 if (dwo_name == nullptr)
20846 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20847 return dwo_name;
20848 }
20849
20850 /* Return non-zero iff the attribute NAME is defined for the given DIE,
20851 and holds a non-zero value. This function should only be used for
20852 DW_FORM_flag or DW_FORM_flag_present attributes. */
20853
20854 static int
20855 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20856 {
20857 struct attribute *attr = dwarf2_attr (die, name, cu);
20858
20859 return attr != nullptr && attr->as_boolean ();
20860 }
20861
20862 static int
20863 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
20864 {
20865 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20866 which value is non-zero. However, we have to be careful with
20867 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20868 (via dwarf2_flag_true_p) follows this attribute. So we may
20869 end up accidently finding a declaration attribute that belongs
20870 to a different DIE referenced by the specification attribute,
20871 even though the given DIE does not have a declaration attribute. */
20872 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20873 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
20874 }
20875
20876 /* Return the die giving the specification for DIE, if there is
20877 one. *SPEC_CU is the CU containing DIE on input, and the CU
20878 containing the return value on output. If there is no
20879 specification, but there is an abstract origin, that is
20880 returned. */
20881
20882 static struct die_info *
20883 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
20884 {
20885 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20886 *spec_cu);
20887
20888 if (spec_attr == NULL)
20889 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20890
20891 if (spec_attr == NULL)
20892 return NULL;
20893 else
20894 return follow_die_ref (die, spec_attr, spec_cu);
20895 }
20896
20897 /* Stub for free_line_header to match void * callback types. */
20898
20899 static void
20900 free_line_header_voidp (void *arg)
20901 {
20902 struct line_header *lh = (struct line_header *) arg;
20903
20904 delete lh;
20905 }
20906
20907 /* A convenience function to find the proper .debug_line section for a CU. */
20908
20909 static struct dwarf2_section_info *
20910 get_debug_line_section (struct dwarf2_cu *cu)
20911 {
20912 struct dwarf2_section_info *section;
20913 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20914
20915 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20916 DWO file. */
20917 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20918 section = &cu->dwo_unit->dwo_file->sections.line;
20919 else if (cu->per_cu->is_dwz)
20920 {
20921 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd, true);
20922
20923 section = &dwz->line;
20924 }
20925 else
20926 section = &per_objfile->per_bfd->line;
20927
20928 return section;
20929 }
20930
20931 /* Read the statement program header starting at OFFSET in
20932 .debug_line, or .debug_line.dwo. Return a pointer
20933 to a struct line_header, allocated using xmalloc.
20934 Returns NULL if there is a problem reading the header, e.g., if it
20935 has a version we don't understand.
20936
20937 NOTE: the strings in the include directory and file name tables of
20938 the returned object point into the dwarf line section buffer,
20939 and must not be freed. */
20940
20941 static line_header_up
20942 dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
20943 {
20944 struct dwarf2_section_info *section;
20945 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20946
20947 section = get_debug_line_section (cu);
20948 section->read (per_objfile->objfile);
20949 if (section->buffer == NULL)
20950 {
20951 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20952 complaint (_("missing .debug_line.dwo section"));
20953 else
20954 complaint (_("missing .debug_line section"));
20955 return 0;
20956 }
20957
20958 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
20959 per_objfile, section, &cu->header);
20960 }
20961
20962 /* Subroutine of dwarf_decode_lines to simplify it.
20963 Return the file name of the psymtab for the given file_entry.
20964 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20965 If space for the result is malloc'd, *NAME_HOLDER will be set.
20966 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
20967
20968 static const char *
20969 psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
20970 const dwarf2_psymtab *pst,
20971 const char *comp_dir,
20972 gdb::unique_xmalloc_ptr<char> *name_holder)
20973 {
20974 const char *include_name = fe.name;
20975 const char *include_name_to_compare = include_name;
20976 const char *pst_filename;
20977 int file_is_pst;
20978
20979 const char *dir_name = fe.include_dir (lh);
20980
20981 gdb::unique_xmalloc_ptr<char> hold_compare;
20982 if (!IS_ABSOLUTE_PATH (include_name)
20983 && (dir_name != NULL || comp_dir != NULL))
20984 {
20985 /* Avoid creating a duplicate psymtab for PST.
20986 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20987 Before we do the comparison, however, we need to account
20988 for DIR_NAME and COMP_DIR.
20989 First prepend dir_name (if non-NULL). If we still don't
20990 have an absolute path prepend comp_dir (if non-NULL).
20991 However, the directory we record in the include-file's
20992 psymtab does not contain COMP_DIR (to match the
20993 corresponding symtab(s)).
20994
20995 Example:
20996
20997 bash$ cd /tmp
20998 bash$ gcc -g ./hello.c
20999 include_name = "hello.c"
21000 dir_name = "."
21001 DW_AT_comp_dir = comp_dir = "/tmp"
21002 DW_AT_name = "./hello.c"
21003
21004 */
21005
21006 if (dir_name != NULL)
21007 {
21008 name_holder->reset (concat (dir_name, SLASH_STRING,
21009 include_name, (char *) NULL));
21010 include_name = name_holder->get ();
21011 include_name_to_compare = include_name;
21012 }
21013 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21014 {
21015 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21016 include_name, (char *) NULL));
21017 include_name_to_compare = hold_compare.get ();
21018 }
21019 }
21020
21021 pst_filename = pst->filename;
21022 gdb::unique_xmalloc_ptr<char> copied_name;
21023 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21024 {
21025 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21026 pst_filename, (char *) NULL));
21027 pst_filename = copied_name.get ();
21028 }
21029
21030 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
21031
21032 if (file_is_pst)
21033 return NULL;
21034 return include_name;
21035 }
21036
21037 /* State machine to track the state of the line number program. */
21038
21039 class lnp_state_machine
21040 {
21041 public:
21042 /* Initialize a machine state for the start of a line number
21043 program. */
21044 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21045 bool record_lines_p);
21046
21047 file_entry *current_file ()
21048 {
21049 /* lh->file_names is 0-based, but the file name numbers in the
21050 statement program are 1-based. */
21051 return m_line_header->file_name_at (m_file);
21052 }
21053
21054 /* Record the line in the state machine. END_SEQUENCE is true if
21055 we're processing the end of a sequence. */
21056 void record_line (bool end_sequence);
21057
21058 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
21059 nop-out rest of the lines in this sequence. */
21060 void check_line_address (struct dwarf2_cu *cu,
21061 const gdb_byte *line_ptr,
21062 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
21063
21064 void handle_set_discriminator (unsigned int discriminator)
21065 {
21066 m_discriminator = discriminator;
21067 m_line_has_non_zero_discriminator |= discriminator != 0;
21068 }
21069
21070 /* Handle DW_LNE_set_address. */
21071 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21072 {
21073 m_op_index = 0;
21074 address += baseaddr;
21075 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21076 }
21077
21078 /* Handle DW_LNS_advance_pc. */
21079 void handle_advance_pc (CORE_ADDR adjust);
21080
21081 /* Handle a special opcode. */
21082 void handle_special_opcode (unsigned char op_code);
21083
21084 /* Handle DW_LNS_advance_line. */
21085 void handle_advance_line (int line_delta)
21086 {
21087 advance_line (line_delta);
21088 }
21089
21090 /* Handle DW_LNS_set_file. */
21091 void handle_set_file (file_name_index file);
21092
21093 /* Handle DW_LNS_negate_stmt. */
21094 void handle_negate_stmt ()
21095 {
21096 m_is_stmt = !m_is_stmt;
21097 }
21098
21099 /* Handle DW_LNS_const_add_pc. */
21100 void handle_const_add_pc ();
21101
21102 /* Handle DW_LNS_fixed_advance_pc. */
21103 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21104 {
21105 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21106 m_op_index = 0;
21107 }
21108
21109 /* Handle DW_LNS_copy. */
21110 void handle_copy ()
21111 {
21112 record_line (false);
21113 m_discriminator = 0;
21114 }
21115
21116 /* Handle DW_LNE_end_sequence. */
21117 void handle_end_sequence ()
21118 {
21119 m_currently_recording_lines = true;
21120 }
21121
21122 private:
21123 /* Advance the line by LINE_DELTA. */
21124 void advance_line (int line_delta)
21125 {
21126 m_line += line_delta;
21127
21128 if (line_delta != 0)
21129 m_line_has_non_zero_discriminator = m_discriminator != 0;
21130 }
21131
21132 struct dwarf2_cu *m_cu;
21133
21134 gdbarch *m_gdbarch;
21135
21136 /* True if we're recording lines.
21137 Otherwise we're building partial symtabs and are just interested in
21138 finding include files mentioned by the line number program. */
21139 bool m_record_lines_p;
21140
21141 /* The line number header. */
21142 line_header *m_line_header;
21143
21144 /* These are part of the standard DWARF line number state machine,
21145 and initialized according to the DWARF spec. */
21146
21147 unsigned char m_op_index = 0;
21148 /* The line table index of the current file. */
21149 file_name_index m_file = 1;
21150 unsigned int m_line = 1;
21151
21152 /* These are initialized in the constructor. */
21153
21154 CORE_ADDR m_address;
21155 bool m_is_stmt;
21156 unsigned int m_discriminator;
21157
21158 /* Additional bits of state we need to track. */
21159
21160 /* The last file that we called dwarf2_start_subfile for.
21161 This is only used for TLLs. */
21162 unsigned int m_last_file = 0;
21163 /* The last file a line number was recorded for. */
21164 struct subfile *m_last_subfile = NULL;
21165
21166 /* The address of the last line entry. */
21167 CORE_ADDR m_last_address;
21168
21169 /* Set to true when a previous line at the same address (using
21170 m_last_address) had m_is_stmt true. This is reset to false when a
21171 line entry at a new address (m_address different to m_last_address) is
21172 processed. */
21173 bool m_stmt_at_address = false;
21174
21175 /* When true, record the lines we decode. */
21176 bool m_currently_recording_lines = false;
21177
21178 /* The last line number that was recorded, used to coalesce
21179 consecutive entries for the same line. This can happen, for
21180 example, when discriminators are present. PR 17276. */
21181 unsigned int m_last_line = 0;
21182 bool m_line_has_non_zero_discriminator = false;
21183 };
21184
21185 void
21186 lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21187 {
21188 CORE_ADDR addr_adj = (((m_op_index + adjust)
21189 / m_line_header->maximum_ops_per_instruction)
21190 * m_line_header->minimum_instruction_length);
21191 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21192 m_op_index = ((m_op_index + adjust)
21193 % m_line_header->maximum_ops_per_instruction);
21194 }
21195
21196 void
21197 lnp_state_machine::handle_special_opcode (unsigned char op_code)
21198 {
21199 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
21200 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21201 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21202 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
21203 / m_line_header->maximum_ops_per_instruction)
21204 * m_line_header->minimum_instruction_length);
21205 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21206 m_op_index = ((m_op_index + adj_opcode_d)
21207 % m_line_header->maximum_ops_per_instruction);
21208
21209 int line_delta = m_line_header->line_base + adj_opcode_r;
21210 advance_line (line_delta);
21211 record_line (false);
21212 m_discriminator = 0;
21213 }
21214
21215 void
21216 lnp_state_machine::handle_set_file (file_name_index file)
21217 {
21218 m_file = file;
21219
21220 const file_entry *fe = current_file ();
21221 if (fe == NULL)
21222 dwarf2_debug_line_missing_file_complaint ();
21223 else if (m_record_lines_p)
21224 {
21225 const char *dir = fe->include_dir (m_line_header);
21226
21227 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21228 m_line_has_non_zero_discriminator = m_discriminator != 0;
21229 dwarf2_start_subfile (m_cu, fe->name, dir);
21230 }
21231 }
21232
21233 void
21234 lnp_state_machine::handle_const_add_pc ()
21235 {
21236 CORE_ADDR adjust
21237 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21238
21239 CORE_ADDR addr_adj
21240 = (((m_op_index + adjust)
21241 / m_line_header->maximum_ops_per_instruction)
21242 * m_line_header->minimum_instruction_length);
21243
21244 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21245 m_op_index = ((m_op_index + adjust)
21246 % m_line_header->maximum_ops_per_instruction);
21247 }
21248
21249 /* Return non-zero if we should add LINE to the line number table.
21250 LINE is the line to add, LAST_LINE is the last line that was added,
21251 LAST_SUBFILE is the subfile for LAST_LINE.
21252 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21253 had a non-zero discriminator.
21254
21255 We have to be careful in the presence of discriminators.
21256 E.g., for this line:
21257
21258 for (i = 0; i < 100000; i++);
21259
21260 clang can emit four line number entries for that one line,
21261 each with a different discriminator.
21262 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21263
21264 However, we want gdb to coalesce all four entries into one.
21265 Otherwise the user could stepi into the middle of the line and
21266 gdb would get confused about whether the pc really was in the
21267 middle of the line.
21268
21269 Things are further complicated by the fact that two consecutive
21270 line number entries for the same line is a heuristic used by gcc
21271 to denote the end of the prologue. So we can't just discard duplicate
21272 entries, we have to be selective about it. The heuristic we use is
21273 that we only collapse consecutive entries for the same line if at least
21274 one of those entries has a non-zero discriminator. PR 17276.
21275
21276 Note: Addresses in the line number state machine can never go backwards
21277 within one sequence, thus this coalescing is ok. */
21278
21279 static int
21280 dwarf_record_line_p (struct dwarf2_cu *cu,
21281 unsigned int line, unsigned int last_line,
21282 int line_has_non_zero_discriminator,
21283 struct subfile *last_subfile)
21284 {
21285 if (cu->get_builder ()->get_current_subfile () != last_subfile)
21286 return 1;
21287 if (line != last_line)
21288 return 1;
21289 /* Same line for the same file that we've seen already.
21290 As a last check, for pr 17276, only record the line if the line
21291 has never had a non-zero discriminator. */
21292 if (!line_has_non_zero_discriminator)
21293 return 1;
21294 return 0;
21295 }
21296
21297 /* Use the CU's builder to record line number LINE beginning at
21298 address ADDRESS in the line table of subfile SUBFILE. */
21299
21300 static void
21301 dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21302 unsigned int line, CORE_ADDR address, bool is_stmt,
21303 struct dwarf2_cu *cu)
21304 {
21305 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21306
21307 if (dwarf_line_debug)
21308 {
21309 fprintf_unfiltered (gdb_stdlog,
21310 "Recording line %u, file %s, address %s\n",
21311 line, lbasename (subfile->name),
21312 paddress (gdbarch, address));
21313 }
21314
21315 if (cu != nullptr)
21316 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
21317 }
21318
21319 /* Subroutine of dwarf_decode_lines_1 to simplify it.
21320 Mark the end of a set of line number records.
21321 The arguments are the same as for dwarf_record_line_1.
21322 If SUBFILE is NULL the request is ignored. */
21323
21324 static void
21325 dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
21326 CORE_ADDR address, struct dwarf2_cu *cu)
21327 {
21328 if (subfile == NULL)
21329 return;
21330
21331 if (dwarf_line_debug)
21332 {
21333 fprintf_unfiltered (gdb_stdlog,
21334 "Finishing current line, file %s, address %s\n",
21335 lbasename (subfile->name),
21336 paddress (gdbarch, address));
21337 }
21338
21339 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
21340 }
21341
21342 void
21343 lnp_state_machine::record_line (bool end_sequence)
21344 {
21345 if (dwarf_line_debug)
21346 {
21347 fprintf_unfiltered (gdb_stdlog,
21348 "Processing actual line %u: file %u,"
21349 " address %s, is_stmt %u, discrim %u%s\n",
21350 m_line, m_file,
21351 paddress (m_gdbarch, m_address),
21352 m_is_stmt, m_discriminator,
21353 (end_sequence ? "\t(end sequence)" : ""));
21354 }
21355
21356 file_entry *fe = current_file ();
21357
21358 if (fe == NULL)
21359 dwarf2_debug_line_missing_file_complaint ();
21360 /* For now we ignore lines not starting on an instruction boundary.
21361 But not when processing end_sequence for compatibility with the
21362 previous version of the code. */
21363 else if (m_op_index == 0 || end_sequence)
21364 {
21365 fe->included_p = 1;
21366 if (m_record_lines_p)
21367 {
21368 /* When we switch files we insert an end maker in the first file,
21369 switch to the second file and add a new line entry. The
21370 problem is that the end marker inserted in the first file will
21371 discard any previous line entries at the same address. If the
21372 line entries in the first file are marked as is-stmt, while
21373 the new line in the second file is non-stmt, then this means
21374 the end marker will discard is-stmt lines so we can have a
21375 non-stmt line. This means that there are less addresses at
21376 which the user can insert a breakpoint.
21377
21378 To improve this we track the last address in m_last_address,
21379 and whether we have seen an is-stmt at this address. Then
21380 when switching files, if we have seen a stmt at the current
21381 address, and we are switching to create a non-stmt line, then
21382 discard the new line. */
21383 bool file_changed
21384 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21385 bool ignore_this_line
21386 = ((file_changed && !end_sequence && m_last_address == m_address
21387 && !m_is_stmt && m_stmt_at_address)
21388 || (!end_sequence && m_line == 0));
21389
21390 if ((file_changed && !ignore_this_line) || end_sequence)
21391 {
21392 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21393 m_currently_recording_lines ? m_cu : nullptr);
21394 }
21395
21396 if (!end_sequence && !ignore_this_line)
21397 {
21398 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21399
21400 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
21401 m_line_has_non_zero_discriminator,
21402 m_last_subfile))
21403 {
21404 buildsym_compunit *builder = m_cu->get_builder ();
21405 dwarf_record_line_1 (m_gdbarch,
21406 builder->get_current_subfile (),
21407 m_line, m_address, is_stmt,
21408 m_currently_recording_lines ? m_cu : nullptr);
21409 }
21410 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
21411 m_last_line = m_line;
21412 }
21413 }
21414 }
21415
21416 /* Track whether we have seen any m_is_stmt true at m_address in case we
21417 have multiple line table entries all at m_address. */
21418 if (m_last_address != m_address)
21419 {
21420 m_stmt_at_address = false;
21421 m_last_address = m_address;
21422 }
21423 m_stmt_at_address |= m_is_stmt;
21424 }
21425
21426 lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21427 line_header *lh, bool record_lines_p)
21428 {
21429 m_cu = cu;
21430 m_gdbarch = arch;
21431 m_record_lines_p = record_lines_p;
21432 m_line_header = lh;
21433
21434 m_currently_recording_lines = true;
21435
21436 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21437 was a line entry for it so that the backend has a chance to adjust it
21438 and also record it in case it needs it. This is currently used by MIPS
21439 code, cf. `mips_adjust_dwarf2_line'. */
21440 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21441 m_is_stmt = lh->default_is_stmt;
21442 m_discriminator = 0;
21443
21444 m_last_address = m_address;
21445 m_stmt_at_address = false;
21446 }
21447
21448 void
21449 lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21450 const gdb_byte *line_ptr,
21451 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
21452 {
21453 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21454 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21455 located at 0x0. In this case, additionally check that if
21456 ADDRESS < UNRELOCATED_LOWPC. */
21457
21458 if ((address == 0 && address < unrelocated_lowpc)
21459 || address == (CORE_ADDR) -1)
21460 {
21461 /* This line table is for a function which has been
21462 GCd by the linker. Ignore it. PR gdb/12528 */
21463
21464 struct objfile *objfile = cu->per_objfile->objfile;
21465 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21466
21467 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
21468 line_offset, objfile_name (objfile));
21469 m_currently_recording_lines = false;
21470 /* Note: m_currently_recording_lines is left as false until we see
21471 DW_LNE_end_sequence. */
21472 }
21473 }
21474
21475 /* Subroutine of dwarf_decode_lines to simplify it.
21476 Process the line number information in LH.
21477 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21478 program in order to set included_p for every referenced header. */
21479
21480 static void
21481 dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21482 const int decode_for_pst_p, CORE_ADDR lowpc)
21483 {
21484 const gdb_byte *line_ptr, *extended_end;
21485 const gdb_byte *line_end;
21486 unsigned int bytes_read, extended_len;
21487 unsigned char op_code, extended_op;
21488 CORE_ADDR baseaddr;
21489 struct objfile *objfile = cu->per_objfile->objfile;
21490 bfd *abfd = objfile->obfd;
21491 struct gdbarch *gdbarch = objfile->arch ();
21492 /* True if we're recording line info (as opposed to building partial
21493 symtabs and just interested in finding include files mentioned by
21494 the line number program). */
21495 bool record_lines_p = !decode_for_pst_p;
21496
21497 baseaddr = objfile->text_section_offset ();
21498
21499 line_ptr = lh->statement_program_start;
21500 line_end = lh->statement_program_end;
21501
21502 /* Read the statement sequences until there's nothing left. */
21503 while (line_ptr < line_end)
21504 {
21505 /* The DWARF line number program state machine. Reset the state
21506 machine at the start of each sequence. */
21507 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
21508 bool end_sequence = false;
21509
21510 if (record_lines_p)
21511 {
21512 /* Start a subfile for the current file of the state
21513 machine. */
21514 const file_entry *fe = state_machine.current_file ();
21515
21516 if (fe != NULL)
21517 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
21518 }
21519
21520 /* Decode the table. */
21521 while (line_ptr < line_end && !end_sequence)
21522 {
21523 op_code = read_1_byte (abfd, line_ptr);
21524 line_ptr += 1;
21525
21526 if (op_code >= lh->opcode_base)
21527 {
21528 /* Special opcode. */
21529 state_machine.handle_special_opcode (op_code);
21530 }
21531 else switch (op_code)
21532 {
21533 case DW_LNS_extended_op:
21534 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21535 &bytes_read);
21536 line_ptr += bytes_read;
21537 extended_end = line_ptr + extended_len;
21538 extended_op = read_1_byte (abfd, line_ptr);
21539 line_ptr += 1;
21540 if (DW_LNE_lo_user <= extended_op
21541 && extended_op <= DW_LNE_hi_user)
21542 {
21543 /* Vendor extension, ignore. */
21544 line_ptr = extended_end;
21545 break;
21546 }
21547 switch (extended_op)
21548 {
21549 case DW_LNE_end_sequence:
21550 state_machine.handle_end_sequence ();
21551 end_sequence = true;
21552 break;
21553 case DW_LNE_set_address:
21554 {
21555 CORE_ADDR address
21556 = cu->header.read_address (abfd, line_ptr, &bytes_read);
21557 line_ptr += bytes_read;
21558
21559 state_machine.check_line_address (cu, line_ptr,
21560 lowpc - baseaddr, address);
21561 state_machine.handle_set_address (baseaddr, address);
21562 }
21563 break;
21564 case DW_LNE_define_file:
21565 {
21566 const char *cur_file;
21567 unsigned int mod_time, length;
21568 dir_index dindex;
21569
21570 cur_file = read_direct_string (abfd, line_ptr,
21571 &bytes_read);
21572 line_ptr += bytes_read;
21573 dindex = (dir_index)
21574 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21575 line_ptr += bytes_read;
21576 mod_time =
21577 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21578 line_ptr += bytes_read;
21579 length =
21580 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21581 line_ptr += bytes_read;
21582 lh->add_file_name (cur_file, dindex, mod_time, length);
21583 }
21584 break;
21585 case DW_LNE_set_discriminator:
21586 {
21587 /* The discriminator is not interesting to the
21588 debugger; just ignore it. We still need to
21589 check its value though:
21590 if there are consecutive entries for the same
21591 (non-prologue) line we want to coalesce them.
21592 PR 17276. */
21593 unsigned int discr
21594 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21595 line_ptr += bytes_read;
21596
21597 state_machine.handle_set_discriminator (discr);
21598 }
21599 break;
21600 default:
21601 complaint (_("mangled .debug_line section"));
21602 return;
21603 }
21604 /* Make sure that we parsed the extended op correctly. If e.g.
21605 we expected a different address size than the producer used,
21606 we may have read the wrong number of bytes. */
21607 if (line_ptr != extended_end)
21608 {
21609 complaint (_("mangled .debug_line section"));
21610 return;
21611 }
21612 break;
21613 case DW_LNS_copy:
21614 state_machine.handle_copy ();
21615 break;
21616 case DW_LNS_advance_pc:
21617 {
21618 CORE_ADDR adjust
21619 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21620 line_ptr += bytes_read;
21621
21622 state_machine.handle_advance_pc (adjust);
21623 }
21624 break;
21625 case DW_LNS_advance_line:
21626 {
21627 int line_delta
21628 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
21629 line_ptr += bytes_read;
21630
21631 state_machine.handle_advance_line (line_delta);
21632 }
21633 break;
21634 case DW_LNS_set_file:
21635 {
21636 file_name_index file
21637 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21638 &bytes_read);
21639 line_ptr += bytes_read;
21640
21641 state_machine.handle_set_file (file);
21642 }
21643 break;
21644 case DW_LNS_set_column:
21645 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21646 line_ptr += bytes_read;
21647 break;
21648 case DW_LNS_negate_stmt:
21649 state_machine.handle_negate_stmt ();
21650 break;
21651 case DW_LNS_set_basic_block:
21652 break;
21653 /* Add to the address register of the state machine the
21654 address increment value corresponding to special opcode
21655 255. I.e., this value is scaled by the minimum
21656 instruction length since special opcode 255 would have
21657 scaled the increment. */
21658 case DW_LNS_const_add_pc:
21659 state_machine.handle_const_add_pc ();
21660 break;
21661 case DW_LNS_fixed_advance_pc:
21662 {
21663 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
21664 line_ptr += 2;
21665
21666 state_machine.handle_fixed_advance_pc (addr_adj);
21667 }
21668 break;
21669 default:
21670 {
21671 /* Unknown standard opcode, ignore it. */
21672 int i;
21673
21674 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
21675 {
21676 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21677 line_ptr += bytes_read;
21678 }
21679 }
21680 }
21681 }
21682
21683 if (!end_sequence)
21684 dwarf2_debug_line_missing_end_sequence_complaint ();
21685
21686 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21687 in which case we still finish recording the last line). */
21688 state_machine.record_line (true);
21689 }
21690 }
21691
21692 /* Decode the Line Number Program (LNP) for the given line_header
21693 structure and CU. The actual information extracted and the type
21694 of structures created from the LNP depends on the value of PST.
21695
21696 1. If PST is NULL, then this procedure uses the data from the program
21697 to create all necessary symbol tables, and their linetables.
21698
21699 2. If PST is not NULL, this procedure reads the program to determine
21700 the list of files included by the unit represented by PST, and
21701 builds all the associated partial symbol tables.
21702
21703 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21704 It is used for relative paths in the line table.
21705 NOTE: When processing partial symtabs (pst != NULL),
21706 comp_dir == pst->dirname.
21707
21708 NOTE: It is important that psymtabs have the same file name (via strcmp)
21709 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21710 symtab we don't use it in the name of the psymtabs we create.
21711 E.g. expand_line_sal requires this when finding psymtabs to expand.
21712 A good testcase for this is mb-inline.exp.
21713
21714 LOWPC is the lowest address in CU (or 0 if not known).
21715
21716 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21717 for its PC<->lines mapping information. Otherwise only the filename
21718 table is read in. */
21719
21720 static void
21721 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
21722 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
21723 CORE_ADDR lowpc, int decode_mapping)
21724 {
21725 struct objfile *objfile = cu->per_objfile->objfile;
21726 const int decode_for_pst_p = (pst != NULL);
21727
21728 if (decode_mapping)
21729 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
21730
21731 if (decode_for_pst_p)
21732 {
21733 /* Now that we're done scanning the Line Header Program, we can
21734 create the psymtab of each included file. */
21735 for (auto &file_entry : lh->file_names ())
21736 if (file_entry.included_p == 1)
21737 {
21738 gdb::unique_xmalloc_ptr<char> name_holder;
21739 const char *include_name =
21740 psymtab_include_file_name (lh, file_entry, pst,
21741 comp_dir, &name_holder);
21742 if (include_name != NULL)
21743 dwarf2_create_include_psymtab
21744 (cu->per_objfile->per_bfd, include_name, pst,
21745 cu->per_objfile->per_bfd->partial_symtabs.get (),
21746 objfile->per_bfd);
21747 }
21748 }
21749 else
21750 {
21751 /* Make sure a symtab is created for every file, even files
21752 which contain only variables (i.e. no code with associated
21753 line numbers). */
21754 buildsym_compunit *builder = cu->get_builder ();
21755 struct compunit_symtab *cust = builder->get_compunit_symtab ();
21756
21757 for (auto &fe : lh->file_names ())
21758 {
21759 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
21760 if (builder->get_current_subfile ()->symtab == NULL)
21761 {
21762 builder->get_current_subfile ()->symtab
21763 = allocate_symtab (cust,
21764 builder->get_current_subfile ()->name);
21765 }
21766 fe.symtab = builder->get_current_subfile ()->symtab;
21767 }
21768 }
21769 }
21770
21771 /* Start a subfile for DWARF. FILENAME is the name of the file and
21772 DIRNAME the name of the source directory which contains FILENAME
21773 or NULL if not known.
21774 This routine tries to keep line numbers from identical absolute and
21775 relative file names in a common subfile.
21776
21777 Using the `list' example from the GDB testsuite, which resides in
21778 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21779 of /srcdir/list0.c yields the following debugging information for list0.c:
21780
21781 DW_AT_name: /srcdir/list0.c
21782 DW_AT_comp_dir: /compdir
21783 files.files[0].name: list0.h
21784 files.files[0].dir: /srcdir
21785 files.files[1].name: list0.c
21786 files.files[1].dir: /srcdir
21787
21788 The line number information for list0.c has to end up in a single
21789 subfile, so that `break /srcdir/list0.c:1' works as expected.
21790 start_subfile will ensure that this happens provided that we pass the
21791 concatenation of files.files[1].dir and files.files[1].name as the
21792 subfile's name. */
21793
21794 static void
21795 dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21796 const char *dirname)
21797 {
21798 gdb::unique_xmalloc_ptr<char> copy;
21799
21800 /* In order not to lose the line information directory,
21801 we concatenate it to the filename when it makes sense.
21802 Note that the Dwarf3 standard says (speaking of filenames in line
21803 information): ``The directory index is ignored for file names
21804 that represent full path names''. Thus ignoring dirname in the
21805 `else' branch below isn't an issue. */
21806
21807 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
21808 {
21809 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21810 filename = copy.get ();
21811 }
21812
21813 cu->get_builder ()->start_subfile (filename);
21814 }
21815
21816 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21817 buildsym_compunit constructor. */
21818
21819 struct compunit_symtab *
21820 dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21821 CORE_ADDR low_pc)
21822 {
21823 gdb_assert (m_builder == nullptr);
21824
21825 m_builder.reset (new struct buildsym_compunit
21826 (this->per_objfile->objfile,
21827 name, comp_dir, language, low_pc));
21828
21829 list_in_scope = get_builder ()->get_file_symbols ();
21830
21831 get_builder ()->record_debugformat ("DWARF 2");
21832 get_builder ()->record_producer (producer);
21833
21834 processing_has_namespace_info = false;
21835
21836 return get_builder ()->get_compunit_symtab ();
21837 }
21838
21839 static void
21840 var_decode_location (struct attribute *attr, struct symbol *sym,
21841 struct dwarf2_cu *cu)
21842 {
21843 struct objfile *objfile = cu->per_objfile->objfile;
21844 struct comp_unit_head *cu_header = &cu->header;
21845
21846 /* NOTE drow/2003-01-30: There used to be a comment and some special
21847 code here to turn a symbol with DW_AT_external and a
21848 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21849 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21850 with some versions of binutils) where shared libraries could have
21851 relocations against symbols in their debug information - the
21852 minimal symbol would have the right address, but the debug info
21853 would not. It's no longer necessary, because we will explicitly
21854 apply relocations when we read in the debug information now. */
21855
21856 /* A DW_AT_location attribute with no contents indicates that a
21857 variable has been optimized away. */
21858 if (attr->form_is_block () && attr->as_block ()->size == 0)
21859 {
21860 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21861 return;
21862 }
21863
21864 /* Handle one degenerate form of location expression specially, to
21865 preserve GDB's previous behavior when section offsets are
21866 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21867 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
21868
21869 if (attr->form_is_block ())
21870 {
21871 struct dwarf_block *block = attr->as_block ();
21872
21873 if ((block->data[0] == DW_OP_addr
21874 && block->size == 1 + cu_header->addr_size)
21875 || ((block->data[0] == DW_OP_GNU_addr_index
21876 || block->data[0] == DW_OP_addrx)
21877 && (block->size
21878 == 1 + leb128_size (&block->data[1]))))
21879 {
21880 unsigned int dummy;
21881
21882 if (block->data[0] == DW_OP_addr)
21883 SET_SYMBOL_VALUE_ADDRESS
21884 (sym, cu->header.read_address (objfile->obfd,
21885 block->data + 1,
21886 &dummy));
21887 else
21888 SET_SYMBOL_VALUE_ADDRESS
21889 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21890 &dummy));
21891 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21892 fixup_symbol_section (sym, objfile);
21893 SET_SYMBOL_VALUE_ADDRESS
21894 (sym,
21895 SYMBOL_VALUE_ADDRESS (sym)
21896 + objfile->section_offsets[sym->section_index ()]);
21897 return;
21898 }
21899 }
21900
21901 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21902 expression evaluator, and use LOC_COMPUTED only when necessary
21903 (i.e. when the value of a register or memory location is
21904 referenced, or a thread-local block, etc.). Then again, it might
21905 not be worthwhile. I'm assuming that it isn't unless performance
21906 or memory numbers show me otherwise. */
21907
21908 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
21909
21910 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
21911 cu->has_loclist = true;
21912 }
21913
21914 /* Given a pointer to a DWARF information entry, figure out if we need
21915 to make a symbol table entry for it, and if so, create a new entry
21916 and return a pointer to it.
21917 If TYPE is NULL, determine symbol type from the die, otherwise
21918 used the passed type.
21919 If SPACE is not NULL, use it to hold the new symbol. If it is
21920 NULL, allocate a new symbol on the objfile's obstack. */
21921
21922 static struct symbol *
21923 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21924 struct symbol *space)
21925 {
21926 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21927 struct objfile *objfile = per_objfile->objfile;
21928 struct gdbarch *gdbarch = objfile->arch ();
21929 struct symbol *sym = NULL;
21930 const char *name;
21931 struct attribute *attr = NULL;
21932 struct attribute *attr2 = NULL;
21933 CORE_ADDR baseaddr;
21934 struct pending **list_to_add = NULL;
21935
21936 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
21937
21938 baseaddr = objfile->text_section_offset ();
21939
21940 name = dwarf2_name (die, cu);
21941 if (name)
21942 {
21943 int suppress_add = 0;
21944
21945 if (space)
21946 sym = space;
21947 else
21948 sym = new (&objfile->objfile_obstack) symbol;
21949 OBJSTAT (objfile, n_syms++);
21950
21951 /* Cache this symbol's name and the name's demangled form (if any). */
21952 sym->set_language (cu->language, &objfile->objfile_obstack);
21953 /* Fortran does not have mangling standard and the mangling does differ
21954 between gfortran, iFort etc. */
21955 const char *physname
21956 = (cu->language == language_fortran
21957 ? dwarf2_full_name (name, die, cu)
21958 : dwarf2_physname (name, die, cu));
21959 const char *linkagename = dw2_linkage_name (die, cu);
21960
21961 if (linkagename == nullptr || cu->language == language_ada)
21962 sym->set_linkage_name (physname);
21963 else
21964 {
21965 sym->set_demangled_name (physname, &objfile->objfile_obstack);
21966 sym->set_linkage_name (linkagename);
21967 }
21968
21969 /* Default assumptions.
21970 Use the passed type or decode it from the die. */
21971 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
21972 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
21973 if (type != NULL)
21974 SYMBOL_TYPE (sym) = type;
21975 else
21976 SYMBOL_TYPE (sym) = die_type (die, cu);
21977 attr = dwarf2_attr (die,
21978 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21979 cu);
21980 if (attr != nullptr)
21981 SYMBOL_LINE (sym) = attr->constant_value (0);
21982
21983 attr = dwarf2_attr (die,
21984 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21985 cu);
21986 if (attr != nullptr && attr->is_nonnegative ())
21987 {
21988 file_name_index file_index
21989 = (file_name_index) attr->as_nonnegative ();
21990 struct file_entry *fe;
21991
21992 if (cu->line_header != NULL)
21993 fe = cu->line_header->file_name_at (file_index);
21994 else
21995 fe = NULL;
21996
21997 if (fe == NULL)
21998 complaint (_("file index out of range"));
21999 else
22000 symbol_set_symtab (sym, fe->symtab);
22001 }
22002
22003 switch (die->tag)
22004 {
22005 case DW_TAG_label:
22006 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
22007 if (attr != nullptr)
22008 {
22009 CORE_ADDR addr;
22010
22011 addr = attr->as_address ();
22012 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
22013 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
22014 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
22015 }
22016 else
22017 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
22018 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22019 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
22020 add_symbol_to_list (sym, cu->list_in_scope);
22021 break;
22022 case DW_TAG_subprogram:
22023 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22024 finish_block. */
22025 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22026 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22027 if ((attr2 != nullptr && attr2->as_boolean ())
22028 || cu->language == language_ada
22029 || cu->language == language_fortran)
22030 {
22031 /* Subprograms marked external are stored as a global symbol.
22032 Ada and Fortran subprograms, whether marked external or
22033 not, are always stored as a global symbol, because we want
22034 to be able to access them globally. For instance, we want
22035 to be able to break on a nested subprogram without having
22036 to specify the context. */
22037 list_to_add = cu->get_builder ()->get_global_symbols ();
22038 }
22039 else
22040 {
22041 list_to_add = cu->list_in_scope;
22042 }
22043 break;
22044 case DW_TAG_inlined_subroutine:
22045 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22046 finish_block. */
22047 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
22048 SYMBOL_INLINED (sym) = 1;
22049 list_to_add = cu->list_in_scope;
22050 break;
22051 case DW_TAG_template_value_param:
22052 suppress_add = 1;
22053 /* Fall through. */
22054 case DW_TAG_constant:
22055 case DW_TAG_variable:
22056 case DW_TAG_member:
22057 /* Compilation with minimal debug info may result in
22058 variables with missing type entries. Change the
22059 misleading `void' type to something sensible. */
22060 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
22061 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
22062
22063 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22064 /* In the case of DW_TAG_member, we should only be called for
22065 static const members. */
22066 if (die->tag == DW_TAG_member)
22067 {
22068 /* dwarf2_add_field uses die_is_declaration,
22069 so we do the same. */
22070 gdb_assert (die_is_declaration (die, cu));
22071 gdb_assert (attr);
22072 }
22073 if (attr != nullptr)
22074 {
22075 dwarf2_const_value (attr, sym, cu);
22076 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22077 if (!suppress_add)
22078 {
22079 if (attr2 != nullptr && attr2->as_boolean ())
22080 list_to_add = cu->get_builder ()->get_global_symbols ();
22081 else
22082 list_to_add = cu->list_in_scope;
22083 }
22084 break;
22085 }
22086 attr = dwarf2_attr (die, DW_AT_location, cu);
22087 if (attr != nullptr)
22088 {
22089 var_decode_location (attr, sym, cu);
22090 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22091
22092 /* Fortran explicitly imports any global symbols to the local
22093 scope by DW_TAG_common_block. */
22094 if (cu->language == language_fortran && die->parent
22095 && die->parent->tag == DW_TAG_common_block)
22096 attr2 = NULL;
22097
22098 if (SYMBOL_CLASS (sym) == LOC_STATIC
22099 && SYMBOL_VALUE_ADDRESS (sym) == 0
22100 && !per_objfile->per_bfd->has_section_at_zero)
22101 {
22102 /* When a static variable is eliminated by the linker,
22103 the corresponding debug information is not stripped
22104 out, but the variable address is set to null;
22105 do not add such variables into symbol table. */
22106 }
22107 else if (attr2 != nullptr && attr2->as_boolean ())
22108 {
22109 if (SYMBOL_CLASS (sym) == LOC_STATIC
22110 && (objfile->flags & OBJF_MAINLINE) == 0
22111 && per_objfile->per_bfd->can_copy)
22112 {
22113 /* A global static variable might be subject to
22114 copy relocation. We first check for a local
22115 minsym, though, because maybe the symbol was
22116 marked hidden, in which case this would not
22117 apply. */
22118 bound_minimal_symbol found
22119 = (lookup_minimal_symbol_linkage
22120 (sym->linkage_name (), objfile));
22121 if (found.minsym != nullptr)
22122 sym->maybe_copied = 1;
22123 }
22124
22125 /* A variable with DW_AT_external is never static,
22126 but it may be block-scoped. */
22127 list_to_add
22128 = ((cu->list_in_scope
22129 == cu->get_builder ()->get_file_symbols ())
22130 ? cu->get_builder ()->get_global_symbols ()
22131 : cu->list_in_scope);
22132 }
22133 else
22134 list_to_add = cu->list_in_scope;
22135 }
22136 else
22137 {
22138 /* We do not know the address of this symbol.
22139 If it is an external symbol and we have type information
22140 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22141 The address of the variable will then be determined from
22142 the minimal symbol table whenever the variable is
22143 referenced. */
22144 attr2 = dwarf2_attr (die, DW_AT_external, cu);
22145
22146 /* Fortran explicitly imports any global symbols to the local
22147 scope by DW_TAG_common_block. */
22148 if (cu->language == language_fortran && die->parent
22149 && die->parent->tag == DW_TAG_common_block)
22150 {
22151 /* SYMBOL_CLASS doesn't matter here because
22152 read_common_block is going to reset it. */
22153 if (!suppress_add)
22154 list_to_add = cu->list_in_scope;
22155 }
22156 else if (attr2 != nullptr && attr2->as_boolean ()
22157 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
22158 {
22159 /* A variable with DW_AT_external is never static, but it
22160 may be block-scoped. */
22161 list_to_add
22162 = ((cu->list_in_scope
22163 == cu->get_builder ()->get_file_symbols ())
22164 ? cu->get_builder ()->get_global_symbols ()
22165 : cu->list_in_scope);
22166
22167 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
22168 }
22169 else if (!die_is_declaration (die, cu))
22170 {
22171 /* Use the default LOC_OPTIMIZED_OUT class. */
22172 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
22173 if (!suppress_add)
22174 list_to_add = cu->list_in_scope;
22175 }
22176 }
22177 break;
22178 case DW_TAG_formal_parameter:
22179 {
22180 /* If we are inside a function, mark this as an argument. If
22181 not, we might be looking at an argument to an inlined function
22182 when we do not have enough information to show inlined frames;
22183 pretend it's a local variable in that case so that the user can
22184 still see it. */
22185 struct context_stack *curr
22186 = cu->get_builder ()->get_current_context_stack ();
22187 if (curr != nullptr && curr->name != nullptr)
22188 SYMBOL_IS_ARGUMENT (sym) = 1;
22189 attr = dwarf2_attr (die, DW_AT_location, cu);
22190 if (attr != nullptr)
22191 {
22192 var_decode_location (attr, sym, cu);
22193 }
22194 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22195 if (attr != nullptr)
22196 {
22197 dwarf2_const_value (attr, sym, cu);
22198 }
22199
22200 list_to_add = cu->list_in_scope;
22201 }
22202 break;
22203 case DW_TAG_unspecified_parameters:
22204 /* From varargs functions; gdb doesn't seem to have any
22205 interest in this information, so just ignore it for now.
22206 (FIXME?) */
22207 break;
22208 case DW_TAG_template_type_param:
22209 suppress_add = 1;
22210 /* Fall through. */
22211 case DW_TAG_class_type:
22212 case DW_TAG_interface_type:
22213 case DW_TAG_structure_type:
22214 case DW_TAG_union_type:
22215 case DW_TAG_set_type:
22216 case DW_TAG_enumeration_type:
22217 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22218 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
22219
22220 {
22221 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
22222 really ever be static objects: otherwise, if you try
22223 to, say, break of a class's method and you're in a file
22224 which doesn't mention that class, it won't work unless
22225 the check for all static symbols in lookup_symbol_aux
22226 saves you. See the OtherFileClass tests in
22227 gdb.c++/namespace.exp. */
22228
22229 if (!suppress_add)
22230 {
22231 buildsym_compunit *builder = cu->get_builder ();
22232 list_to_add
22233 = (cu->list_in_scope == builder->get_file_symbols ()
22234 && cu->language == language_cplus
22235 ? builder->get_global_symbols ()
22236 : cu->list_in_scope);
22237
22238 /* The semantics of C++ state that "struct foo {
22239 ... }" also defines a typedef for "foo". */
22240 if (cu->language == language_cplus
22241 || cu->language == language_ada
22242 || cu->language == language_d
22243 || cu->language == language_rust)
22244 {
22245 /* The symbol's name is already allocated along
22246 with this objfile, so we don't need to
22247 duplicate it for the type. */
22248 if (SYMBOL_TYPE (sym)->name () == 0)
22249 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
22250 }
22251 }
22252 }
22253 break;
22254 case DW_TAG_typedef:
22255 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22256 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22257 list_to_add = cu->list_in_scope;
22258 break;
22259 case DW_TAG_array_type:
22260 case DW_TAG_base_type:
22261 case DW_TAG_subrange_type:
22262 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22263 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
22264 list_to_add = cu->list_in_scope;
22265 break;
22266 case DW_TAG_enumerator:
22267 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22268 if (attr != nullptr)
22269 {
22270 dwarf2_const_value (attr, sym, cu);
22271 }
22272 {
22273 /* NOTE: carlton/2003-11-10: See comment above in the
22274 DW_TAG_class_type, etc. block. */
22275
22276 list_to_add
22277 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
22278 && cu->language == language_cplus
22279 ? cu->get_builder ()->get_global_symbols ()
22280 : cu->list_in_scope);
22281 }
22282 break;
22283 case DW_TAG_imported_declaration:
22284 case DW_TAG_namespace:
22285 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22286 list_to_add = cu->get_builder ()->get_global_symbols ();
22287 break;
22288 case DW_TAG_module:
22289 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22290 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
22291 list_to_add = cu->get_builder ()->get_global_symbols ();
22292 break;
22293 case DW_TAG_common_block:
22294 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
22295 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
22296 add_symbol_to_list (sym, cu->list_in_scope);
22297 break;
22298 default:
22299 /* Not a tag we recognize. Hopefully we aren't processing
22300 trash data, but since we must specifically ignore things
22301 we don't recognize, there is nothing else we should do at
22302 this point. */
22303 complaint (_("unsupported tag: '%s'"),
22304 dwarf_tag_name (die->tag));
22305 break;
22306 }
22307
22308 if (suppress_add)
22309 {
22310 sym->hash_next = objfile->template_symbols;
22311 objfile->template_symbols = sym;
22312 list_to_add = NULL;
22313 }
22314
22315 if (list_to_add != NULL)
22316 add_symbol_to_list (sym, list_to_add);
22317
22318 /* For the benefit of old versions of GCC, check for anonymous
22319 namespaces based on the demangled name. */
22320 if (!cu->processing_has_namespace_info
22321 && cu->language == language_cplus)
22322 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
22323 }
22324 return (sym);
22325 }
22326
22327 /* Given an attr with a DW_FORM_dataN value in host byte order,
22328 zero-extend it as appropriate for the symbol's type. The DWARF
22329 standard (v4) is not entirely clear about the meaning of using
22330 DW_FORM_dataN for a constant with a signed type, where the type is
22331 wider than the data. The conclusion of a discussion on the DWARF
22332 list was that this is unspecified. We choose to always zero-extend
22333 because that is the interpretation long in use by GCC. */
22334
22335 static gdb_byte *
22336 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
22337 struct dwarf2_cu *cu, LONGEST *value, int bits)
22338 {
22339 struct objfile *objfile = cu->per_objfile->objfile;
22340 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22341 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
22342 LONGEST l = attr->constant_value (0);
22343
22344 if (bits < sizeof (*value) * 8)
22345 {
22346 l &= ((LONGEST) 1 << bits) - 1;
22347 *value = l;
22348 }
22349 else if (bits == sizeof (*value) * 8)
22350 *value = l;
22351 else
22352 {
22353 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
22354 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22355 return bytes;
22356 }
22357
22358 return NULL;
22359 }
22360
22361 /* Read a constant value from an attribute. Either set *VALUE, or if
22362 the value does not fit in *VALUE, set *BYTES - either already
22363 allocated on the objfile obstack, or newly allocated on OBSTACK,
22364 or, set *BATON, if we translated the constant to a location
22365 expression. */
22366
22367 static void
22368 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
22369 const char *name, struct obstack *obstack,
22370 struct dwarf2_cu *cu,
22371 LONGEST *value, const gdb_byte **bytes,
22372 struct dwarf2_locexpr_baton **baton)
22373 {
22374 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22375 struct objfile *objfile = per_objfile->objfile;
22376 struct comp_unit_head *cu_header = &cu->header;
22377 struct dwarf_block *blk;
22378 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22379 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22380
22381 *value = 0;
22382 *bytes = NULL;
22383 *baton = NULL;
22384
22385 switch (attr->form)
22386 {
22387 case DW_FORM_addr:
22388 case DW_FORM_addrx:
22389 case DW_FORM_GNU_addr_index:
22390 {
22391 gdb_byte *data;
22392
22393 if (TYPE_LENGTH (type) != cu_header->addr_size)
22394 dwarf2_const_value_length_mismatch_complaint (name,
22395 cu_header->addr_size,
22396 TYPE_LENGTH (type));
22397 /* Symbols of this form are reasonably rare, so we just
22398 piggyback on the existing location code rather than writing
22399 a new implementation of symbol_computed_ops. */
22400 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
22401 (*baton)->per_objfile = per_objfile;
22402 (*baton)->per_cu = cu->per_cu;
22403 gdb_assert ((*baton)->per_cu);
22404
22405 (*baton)->size = 2 + cu_header->addr_size;
22406 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
22407 (*baton)->data = data;
22408
22409 data[0] = DW_OP_addr;
22410 store_unsigned_integer (&data[1], cu_header->addr_size,
22411 byte_order, attr->as_address ());
22412 data[cu_header->addr_size + 1] = DW_OP_stack_value;
22413 }
22414 break;
22415 case DW_FORM_string:
22416 case DW_FORM_strp:
22417 case DW_FORM_strx:
22418 case DW_FORM_GNU_str_index:
22419 case DW_FORM_GNU_strp_alt:
22420 /* The string is already allocated on the objfile obstack, point
22421 directly to it. */
22422 *bytes = (const gdb_byte *) attr->as_string ();
22423 break;
22424 case DW_FORM_block1:
22425 case DW_FORM_block2:
22426 case DW_FORM_block4:
22427 case DW_FORM_block:
22428 case DW_FORM_exprloc:
22429 case DW_FORM_data16:
22430 blk = attr->as_block ();
22431 if (TYPE_LENGTH (type) != blk->size)
22432 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22433 TYPE_LENGTH (type));
22434 *bytes = blk->data;
22435 break;
22436
22437 /* The DW_AT_const_value attributes are supposed to carry the
22438 symbol's value "represented as it would be on the target
22439 architecture." By the time we get here, it's already been
22440 converted to host endianness, so we just need to sign- or
22441 zero-extend it as appropriate. */
22442 case DW_FORM_data1:
22443 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
22444 break;
22445 case DW_FORM_data2:
22446 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
22447 break;
22448 case DW_FORM_data4:
22449 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
22450 break;
22451 case DW_FORM_data8:
22452 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
22453 break;
22454
22455 case DW_FORM_sdata:
22456 case DW_FORM_implicit_const:
22457 *value = attr->as_signed ();
22458 break;
22459
22460 case DW_FORM_udata:
22461 *value = attr->as_unsigned ();
22462 break;
22463
22464 default:
22465 complaint (_("unsupported const value attribute form: '%s'"),
22466 dwarf_form_name (attr->form));
22467 *value = 0;
22468 break;
22469 }
22470 }
22471
22472
22473 /* Copy constant value from an attribute to a symbol. */
22474
22475 static void
22476 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
22477 struct dwarf2_cu *cu)
22478 {
22479 struct objfile *objfile = cu->per_objfile->objfile;
22480 LONGEST value;
22481 const gdb_byte *bytes;
22482 struct dwarf2_locexpr_baton *baton;
22483
22484 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22485 sym->print_name (),
22486 &objfile->objfile_obstack, cu,
22487 &value, &bytes, &baton);
22488
22489 if (baton != NULL)
22490 {
22491 SYMBOL_LOCATION_BATON (sym) = baton;
22492 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
22493 }
22494 else if (bytes != NULL)
22495 {
22496 SYMBOL_VALUE_BYTES (sym) = bytes;
22497 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
22498 }
22499 else
22500 {
22501 SYMBOL_VALUE (sym) = value;
22502 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
22503 }
22504 }
22505
22506 /* Return the type of the die in question using its DW_AT_type attribute. */
22507
22508 static struct type *
22509 die_type (struct die_info *die, struct dwarf2_cu *cu)
22510 {
22511 struct attribute *type_attr;
22512
22513 type_attr = dwarf2_attr (die, DW_AT_type, cu);
22514 if (!type_attr)
22515 {
22516 struct objfile *objfile = cu->per_objfile->objfile;
22517 /* A missing DW_AT_type represents a void type. */
22518 return objfile_type (objfile)->builtin_void;
22519 }
22520
22521 return lookup_die_type (die, type_attr, cu);
22522 }
22523
22524 /* True iff CU's producer generates GNAT Ada auxiliary information
22525 that allows to find parallel types through that information instead
22526 of having to do expensive parallel lookups by type name. */
22527
22528 static int
22529 need_gnat_info (struct dwarf2_cu *cu)
22530 {
22531 /* Assume that the Ada compiler was GNAT, which always produces
22532 the auxiliary information. */
22533 return (cu->language == language_ada);
22534 }
22535
22536 /* Return the auxiliary type of the die in question using its
22537 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22538 attribute is not present. */
22539
22540 static struct type *
22541 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22542 {
22543 struct attribute *type_attr;
22544
22545 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22546 if (!type_attr)
22547 return NULL;
22548
22549 return lookup_die_type (die, type_attr, cu);
22550 }
22551
22552 /* If DIE has a descriptive_type attribute, then set the TYPE's
22553 descriptive type accordingly. */
22554
22555 static void
22556 set_descriptive_type (struct type *type, struct die_info *die,
22557 struct dwarf2_cu *cu)
22558 {
22559 struct type *descriptive_type = die_descriptive_type (die, cu);
22560
22561 if (descriptive_type)
22562 {
22563 ALLOCATE_GNAT_AUX_TYPE (type);
22564 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22565 }
22566 }
22567
22568 /* Return the containing type of the die in question using its
22569 DW_AT_containing_type attribute. */
22570
22571 static struct type *
22572 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
22573 {
22574 struct attribute *type_attr;
22575 struct objfile *objfile = cu->per_objfile->objfile;
22576
22577 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
22578 if (!type_attr)
22579 error (_("Dwarf Error: Problem turning containing type into gdb type "
22580 "[in module %s]"), objfile_name (objfile));
22581
22582 return lookup_die_type (die, type_attr, cu);
22583 }
22584
22585 /* Return an error marker type to use for the ill formed type in DIE/CU. */
22586
22587 static struct type *
22588 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22589 {
22590 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22591 struct objfile *objfile = per_objfile->objfile;
22592 char *saved;
22593
22594 std::string message
22595 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22596 objfile_name (objfile),
22597 sect_offset_str (cu->header.sect_off),
22598 sect_offset_str (die->sect_off));
22599 saved = obstack_strdup (&objfile->objfile_obstack, message);
22600
22601 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
22602 }
22603
22604 /* Look up the type of DIE in CU using its type attribute ATTR.
22605 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22606 DW_AT_containing_type.
22607 If there is no type substitute an error marker. */
22608
22609 static struct type *
22610 lookup_die_type (struct die_info *die, const struct attribute *attr,
22611 struct dwarf2_cu *cu)
22612 {
22613 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22614 struct objfile *objfile = per_objfile->objfile;
22615 struct type *this_type;
22616
22617 gdb_assert (attr->name == DW_AT_type
22618 || attr->name == DW_AT_GNAT_descriptive_type
22619 || attr->name == DW_AT_containing_type);
22620
22621 /* First see if we have it cached. */
22622
22623 if (attr->form == DW_FORM_GNU_ref_alt)
22624 {
22625 struct dwarf2_per_cu_data *per_cu;
22626 sect_offset sect_off = attr->get_ref_die_offset ();
22627
22628 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22629 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
22630 }
22631 else if (attr->form_is_ref ())
22632 {
22633 sect_offset sect_off = attr->get_ref_die_offset ();
22634
22635 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
22636 }
22637 else if (attr->form == DW_FORM_ref_sig8)
22638 {
22639 ULONGEST signature = attr->as_signature ();
22640
22641 return get_signatured_type (die, signature, cu);
22642 }
22643 else
22644 {
22645 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
22646 " at %s [in module %s]"),
22647 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
22648 objfile_name (objfile));
22649 return build_error_marker_type (cu, die);
22650 }
22651
22652 /* If not cached we need to read it in. */
22653
22654 if (this_type == NULL)
22655 {
22656 struct die_info *type_die = NULL;
22657 struct dwarf2_cu *type_cu = cu;
22658
22659 if (attr->form_is_ref ())
22660 type_die = follow_die_ref (die, attr, &type_cu);
22661 if (type_die == NULL)
22662 return build_error_marker_type (cu, die);
22663 /* If we find the type now, it's probably because the type came
22664 from an inter-CU reference and the type's CU got expanded before
22665 ours. */
22666 this_type = read_type_die (type_die, type_cu);
22667 }
22668
22669 /* If we still don't have a type use an error marker. */
22670
22671 if (this_type == NULL)
22672 return build_error_marker_type (cu, die);
22673
22674 return this_type;
22675 }
22676
22677 /* Return the type in DIE, CU.
22678 Returns NULL for invalid types.
22679
22680 This first does a lookup in die_type_hash,
22681 and only reads the die in if necessary.
22682
22683 NOTE: This can be called when reading in partial or full symbols. */
22684
22685 static struct type *
22686 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
22687 {
22688 struct type *this_type;
22689
22690 this_type = get_die_type (die, cu);
22691 if (this_type)
22692 return this_type;
22693
22694 return read_type_die_1 (die, cu);
22695 }
22696
22697 /* Read the type in DIE, CU.
22698 Returns NULL for invalid types. */
22699
22700 static struct type *
22701 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22702 {
22703 struct type *this_type = NULL;
22704
22705 switch (die->tag)
22706 {
22707 case DW_TAG_class_type:
22708 case DW_TAG_interface_type:
22709 case DW_TAG_structure_type:
22710 case DW_TAG_union_type:
22711 this_type = read_structure_type (die, cu);
22712 break;
22713 case DW_TAG_enumeration_type:
22714 this_type = read_enumeration_type (die, cu);
22715 break;
22716 case DW_TAG_subprogram:
22717 case DW_TAG_subroutine_type:
22718 case DW_TAG_inlined_subroutine:
22719 this_type = read_subroutine_type (die, cu);
22720 break;
22721 case DW_TAG_array_type:
22722 this_type = read_array_type (die, cu);
22723 break;
22724 case DW_TAG_set_type:
22725 this_type = read_set_type (die, cu);
22726 break;
22727 case DW_TAG_pointer_type:
22728 this_type = read_tag_pointer_type (die, cu);
22729 break;
22730 case DW_TAG_ptr_to_member_type:
22731 this_type = read_tag_ptr_to_member_type (die, cu);
22732 break;
22733 case DW_TAG_reference_type:
22734 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22735 break;
22736 case DW_TAG_rvalue_reference_type:
22737 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
22738 break;
22739 case DW_TAG_const_type:
22740 this_type = read_tag_const_type (die, cu);
22741 break;
22742 case DW_TAG_volatile_type:
22743 this_type = read_tag_volatile_type (die, cu);
22744 break;
22745 case DW_TAG_restrict_type:
22746 this_type = read_tag_restrict_type (die, cu);
22747 break;
22748 case DW_TAG_string_type:
22749 this_type = read_tag_string_type (die, cu);
22750 break;
22751 case DW_TAG_typedef:
22752 this_type = read_typedef (die, cu);
22753 break;
22754 case DW_TAG_subrange_type:
22755 this_type = read_subrange_type (die, cu);
22756 break;
22757 case DW_TAG_base_type:
22758 this_type = read_base_type (die, cu);
22759 break;
22760 case DW_TAG_unspecified_type:
22761 this_type = read_unspecified_type (die, cu);
22762 break;
22763 case DW_TAG_namespace:
22764 this_type = read_namespace_type (die, cu);
22765 break;
22766 case DW_TAG_module:
22767 this_type = read_module_type (die, cu);
22768 break;
22769 case DW_TAG_atomic_type:
22770 this_type = read_tag_atomic_type (die, cu);
22771 break;
22772 default:
22773 complaint (_("unexpected tag in read_type_die: '%s'"),
22774 dwarf_tag_name (die->tag));
22775 break;
22776 }
22777
22778 return this_type;
22779 }
22780
22781 /* See if we can figure out if the class lives in a namespace. We do
22782 this by looking for a member function; its demangled name will
22783 contain namespace info, if there is any.
22784 Return the computed name or NULL.
22785 Space for the result is allocated on the objfile's obstack.
22786 This is the full-die version of guess_partial_die_structure_name.
22787 In this case we know DIE has no useful parent. */
22788
22789 static const char *
22790 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22791 {
22792 struct die_info *spec_die;
22793 struct dwarf2_cu *spec_cu;
22794 struct die_info *child;
22795 struct objfile *objfile = cu->per_objfile->objfile;
22796
22797 spec_cu = cu;
22798 spec_die = die_specification (die, &spec_cu);
22799 if (spec_die != NULL)
22800 {
22801 die = spec_die;
22802 cu = spec_cu;
22803 }
22804
22805 for (child = die->child;
22806 child != NULL;
22807 child = child->sibling)
22808 {
22809 if (child->tag == DW_TAG_subprogram)
22810 {
22811 const char *linkage_name = dw2_linkage_name (child, cu);
22812
22813 if (linkage_name != NULL)
22814 {
22815 gdb::unique_xmalloc_ptr<char> actual_name
22816 (cu->language_defn->class_name_from_physname (linkage_name));
22817 const char *name = NULL;
22818
22819 if (actual_name != NULL)
22820 {
22821 const char *die_name = dwarf2_name (die, cu);
22822
22823 if (die_name != NULL
22824 && strcmp (die_name, actual_name.get ()) != 0)
22825 {
22826 /* Strip off the class name from the full name.
22827 We want the prefix. */
22828 int die_name_len = strlen (die_name);
22829 int actual_name_len = strlen (actual_name.get ());
22830 const char *ptr = actual_name.get ();
22831
22832 /* Test for '::' as a sanity check. */
22833 if (actual_name_len > die_name_len + 2
22834 && ptr[actual_name_len - die_name_len - 1] == ':')
22835 name = obstack_strndup (
22836 &objfile->per_bfd->storage_obstack,
22837 ptr, actual_name_len - die_name_len - 2);
22838 }
22839 }
22840 return name;
22841 }
22842 }
22843 }
22844
22845 return NULL;
22846 }
22847
22848 /* GCC might emit a nameless typedef that has a linkage name. Determine the
22849 prefix part in such case. See
22850 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22851
22852 static const char *
22853 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22854 {
22855 struct attribute *attr;
22856 const char *base;
22857
22858 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22859 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22860 return NULL;
22861
22862 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
22863 return NULL;
22864
22865 attr = dw2_linkage_name_attr (die, cu);
22866 const char *attr_name = attr->as_string ();
22867 if (attr == NULL || attr_name == NULL)
22868 return NULL;
22869
22870 /* dwarf2_name had to be already called. */
22871 gdb_assert (attr->canonical_string_p ());
22872
22873 /* Strip the base name, keep any leading namespaces/classes. */
22874 base = strrchr (attr_name, ':');
22875 if (base == NULL || base == attr_name || base[-1] != ':')
22876 return "";
22877
22878 struct objfile *objfile = cu->per_objfile->objfile;
22879 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22880 attr_name,
22881 &base[-1] - attr_name);
22882 }
22883
22884 /* Return the name of the namespace/class that DIE is defined within,
22885 or "" if we can't tell. The caller should not xfree the result.
22886
22887 For example, if we're within the method foo() in the following
22888 code:
22889
22890 namespace N {
22891 class C {
22892 void foo () {
22893 }
22894 };
22895 }
22896
22897 then determine_prefix on foo's die will return "N::C". */
22898
22899 static const char *
22900 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
22901 {
22902 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22903 struct die_info *parent, *spec_die;
22904 struct dwarf2_cu *spec_cu;
22905 struct type *parent_type;
22906 const char *retval;
22907
22908 if (cu->language != language_cplus
22909 && cu->language != language_fortran && cu->language != language_d
22910 && cu->language != language_rust)
22911 return "";
22912
22913 retval = anonymous_struct_prefix (die, cu);
22914 if (retval)
22915 return retval;
22916
22917 /* We have to be careful in the presence of DW_AT_specification.
22918 For example, with GCC 3.4, given the code
22919
22920 namespace N {
22921 void foo() {
22922 // Definition of N::foo.
22923 }
22924 }
22925
22926 then we'll have a tree of DIEs like this:
22927
22928 1: DW_TAG_compile_unit
22929 2: DW_TAG_namespace // N
22930 3: DW_TAG_subprogram // declaration of N::foo
22931 4: DW_TAG_subprogram // definition of N::foo
22932 DW_AT_specification // refers to die #3
22933
22934 Thus, when processing die #4, we have to pretend that we're in
22935 the context of its DW_AT_specification, namely the contex of die
22936 #3. */
22937 spec_cu = cu;
22938 spec_die = die_specification (die, &spec_cu);
22939 if (spec_die == NULL)
22940 parent = die->parent;
22941 else
22942 {
22943 parent = spec_die->parent;
22944 cu = spec_cu;
22945 }
22946
22947 if (parent == NULL)
22948 return "";
22949 else if (parent->building_fullname)
22950 {
22951 const char *name;
22952 const char *parent_name;
22953
22954 /* It has been seen on RealView 2.2 built binaries,
22955 DW_TAG_template_type_param types actually _defined_ as
22956 children of the parent class:
22957
22958 enum E {};
22959 template class <class Enum> Class{};
22960 Class<enum E> class_e;
22961
22962 1: DW_TAG_class_type (Class)
22963 2: DW_TAG_enumeration_type (E)
22964 3: DW_TAG_enumerator (enum1:0)
22965 3: DW_TAG_enumerator (enum2:1)
22966 ...
22967 2: DW_TAG_template_type_param
22968 DW_AT_type DW_FORM_ref_udata (E)
22969
22970 Besides being broken debug info, it can put GDB into an
22971 infinite loop. Consider:
22972
22973 When we're building the full name for Class<E>, we'll start
22974 at Class, and go look over its template type parameters,
22975 finding E. We'll then try to build the full name of E, and
22976 reach here. We're now trying to build the full name of E,
22977 and look over the parent DIE for containing scope. In the
22978 broken case, if we followed the parent DIE of E, we'd again
22979 find Class, and once again go look at its template type
22980 arguments, etc., etc. Simply don't consider such parent die
22981 as source-level parent of this die (it can't be, the language
22982 doesn't allow it), and break the loop here. */
22983 name = dwarf2_name (die, cu);
22984 parent_name = dwarf2_name (parent, cu);
22985 complaint (_("template param type '%s' defined within parent '%s'"),
22986 name ? name : "<unknown>",
22987 parent_name ? parent_name : "<unknown>");
22988 return "";
22989 }
22990 else
22991 switch (parent->tag)
22992 {
22993 case DW_TAG_namespace:
22994 parent_type = read_type_die (parent, cu);
22995 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22996 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22997 Work around this problem here. */
22998 if (cu->language == language_cplus
22999 && strcmp (parent_type->name (), "::") == 0)
23000 return "";
23001 /* We give a name to even anonymous namespaces. */
23002 return parent_type->name ();
23003 case DW_TAG_class_type:
23004 case DW_TAG_interface_type:
23005 case DW_TAG_structure_type:
23006 case DW_TAG_union_type:
23007 case DW_TAG_module:
23008 parent_type = read_type_die (parent, cu);
23009 if (parent_type->name () != NULL)
23010 return parent_type->name ();
23011 else
23012 /* An anonymous structure is only allowed non-static data
23013 members; no typedefs, no member functions, et cetera.
23014 So it does not need a prefix. */
23015 return "";
23016 case DW_TAG_compile_unit:
23017 case DW_TAG_partial_unit:
23018 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23019 if (cu->language == language_cplus
23020 && !per_objfile->per_bfd->types.empty ()
23021 && die->child != NULL
23022 && (die->tag == DW_TAG_class_type
23023 || die->tag == DW_TAG_structure_type
23024 || die->tag == DW_TAG_union_type))
23025 {
23026 const char *name = guess_full_die_structure_name (die, cu);
23027 if (name != NULL)
23028 return name;
23029 }
23030 return "";
23031 case DW_TAG_subprogram:
23032 /* Nested subroutines in Fortran get a prefix with the name
23033 of the parent's subroutine. */
23034 if (cu->language == language_fortran)
23035 {
23036 if ((die->tag == DW_TAG_subprogram)
23037 && (dwarf2_name (parent, cu) != NULL))
23038 return dwarf2_name (parent, cu);
23039 }
23040 return determine_prefix (parent, cu);
23041 case DW_TAG_enumeration_type:
23042 parent_type = read_type_die (parent, cu);
23043 if (parent_type->is_declared_class ())
23044 {
23045 if (parent_type->name () != NULL)
23046 return parent_type->name ();
23047 return "";
23048 }
23049 /* Fall through. */
23050 default:
23051 return determine_prefix (parent, cu);
23052 }
23053 }
23054
23055 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23056 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23057 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23058 an obconcat, otherwise allocate storage for the result. The CU argument is
23059 used to determine the language and hence, the appropriate separator. */
23060
23061 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
23062
23063 static char *
23064 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
23065 int physname, struct dwarf2_cu *cu)
23066 {
23067 const char *lead = "";
23068 const char *sep;
23069
23070 if (suffix == NULL || suffix[0] == '\0'
23071 || prefix == NULL || prefix[0] == '\0')
23072 sep = "";
23073 else if (cu->language == language_d)
23074 {
23075 /* For D, the 'main' function could be defined in any module, but it
23076 should never be prefixed. */
23077 if (strcmp (suffix, "D main") == 0)
23078 {
23079 prefix = "";
23080 sep = "";
23081 }
23082 else
23083 sep = ".";
23084 }
23085 else if (cu->language == language_fortran && physname)
23086 {
23087 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23088 DW_AT_MIPS_linkage_name is preferred and used instead. */
23089
23090 lead = "__";
23091 sep = "_MOD_";
23092 }
23093 else
23094 sep = "::";
23095
23096 if (prefix == NULL)
23097 prefix = "";
23098 if (suffix == NULL)
23099 suffix = "";
23100
23101 if (obs == NULL)
23102 {
23103 char *retval
23104 = ((char *)
23105 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
23106
23107 strcpy (retval, lead);
23108 strcat (retval, prefix);
23109 strcat (retval, sep);
23110 strcat (retval, suffix);
23111 return retval;
23112 }
23113 else
23114 {
23115 /* We have an obstack. */
23116 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
23117 }
23118 }
23119
23120 /* Get name of a die, return NULL if not found. */
23121
23122 static const char *
23123 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
23124 struct objfile *objfile)
23125 {
23126 if (name && cu->language == language_cplus)
23127 {
23128 gdb::unique_xmalloc_ptr<char> canon_name
23129 = cp_canonicalize_string (name);
23130
23131 if (canon_name != nullptr)
23132 name = objfile->intern (canon_name.get ());
23133 }
23134
23135 return name;
23136 }
23137
23138 /* Get name of a die, return NULL if not found.
23139 Anonymous namespaces are converted to their magic string. */
23140
23141 static const char *
23142 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
23143 {
23144 struct attribute *attr;
23145 struct objfile *objfile = cu->per_objfile->objfile;
23146
23147 attr = dwarf2_attr (die, DW_AT_name, cu);
23148 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23149 if (attr_name == nullptr
23150 && die->tag != DW_TAG_namespace
23151 && die->tag != DW_TAG_class_type
23152 && die->tag != DW_TAG_interface_type
23153 && die->tag != DW_TAG_structure_type
23154 && die->tag != DW_TAG_union_type)
23155 return NULL;
23156
23157 switch (die->tag)
23158 {
23159 case DW_TAG_compile_unit:
23160 case DW_TAG_partial_unit:
23161 /* Compilation units have a DW_AT_name that is a filename, not
23162 a source language identifier. */
23163 case DW_TAG_enumeration_type:
23164 case DW_TAG_enumerator:
23165 /* These tags always have simple identifiers already; no need
23166 to canonicalize them. */
23167 return attr_name;
23168
23169 case DW_TAG_namespace:
23170 if (attr_name != nullptr)
23171 return attr_name;
23172 return CP_ANONYMOUS_NAMESPACE_STR;
23173
23174 case DW_TAG_class_type:
23175 case DW_TAG_interface_type:
23176 case DW_TAG_structure_type:
23177 case DW_TAG_union_type:
23178 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23179 structures or unions. These were of the form "._%d" in GCC 4.1,
23180 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23181 and GCC 4.4. We work around this problem by ignoring these. */
23182 if (attr_name != nullptr
23183 && (startswith (attr_name, "._")
23184 || startswith (attr_name, "<anonymous")))
23185 return NULL;
23186
23187 /* GCC might emit a nameless typedef that has a linkage name. See
23188 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23189 if (!attr || attr_name == NULL)
23190 {
23191 attr = dw2_linkage_name_attr (die, cu);
23192 attr_name = attr == nullptr ? nullptr : attr->as_string ();
23193 if (attr == NULL || attr_name == NULL)
23194 return NULL;
23195
23196 /* Avoid demangling attr_name the second time on a second
23197 call for the same DIE. */
23198 if (!attr->canonical_string_p ())
23199 {
23200 gdb::unique_xmalloc_ptr<char> demangled
23201 (gdb_demangle (attr_name, DMGL_TYPES));
23202 if (demangled == nullptr)
23203 return nullptr;
23204
23205 attr->set_string_canonical (objfile->intern (demangled.get ()));
23206 attr_name = attr->as_string ();
23207 }
23208
23209 /* Strip any leading namespaces/classes, keep only the
23210 base name. DW_AT_name for named DIEs does not
23211 contain the prefixes. */
23212 const char *base = strrchr (attr_name, ':');
23213 if (base && base > attr_name && base[-1] == ':')
23214 return &base[1];
23215 else
23216 return attr_name;
23217 }
23218 break;
23219
23220 default:
23221 break;
23222 }
23223
23224 if (!attr->canonical_string_p ())
23225 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23226 objfile));
23227 return attr->as_string ();
23228 }
23229
23230 /* Return the die that this die in an extension of, or NULL if there
23231 is none. *EXT_CU is the CU containing DIE on input, and the CU
23232 containing the return value on output. */
23233
23234 static struct die_info *
23235 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
23236 {
23237 struct attribute *attr;
23238
23239 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
23240 if (attr == NULL)
23241 return NULL;
23242
23243 return follow_die_ref (die, attr, ext_cu);
23244 }
23245
23246 static void
23247 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
23248 {
23249 unsigned int i;
23250
23251 print_spaces (indent, f);
23252 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
23253 dwarf_tag_name (die->tag), die->abbrev,
23254 sect_offset_str (die->sect_off));
23255
23256 if (die->parent != NULL)
23257 {
23258 print_spaces (indent, f);
23259 fprintf_unfiltered (f, " parent at offset: %s\n",
23260 sect_offset_str (die->parent->sect_off));
23261 }
23262
23263 print_spaces (indent, f);
23264 fprintf_unfiltered (f, " has children: %s\n",
23265 dwarf_bool_name (die->child != NULL));
23266
23267 print_spaces (indent, f);
23268 fprintf_unfiltered (f, " attributes:\n");
23269
23270 for (i = 0; i < die->num_attrs; ++i)
23271 {
23272 print_spaces (indent, f);
23273 fprintf_unfiltered (f, " %s (%s) ",
23274 dwarf_attr_name (die->attrs[i].name),
23275 dwarf_form_name (die->attrs[i].form));
23276
23277 switch (die->attrs[i].form)
23278 {
23279 case DW_FORM_addr:
23280 case DW_FORM_addrx:
23281 case DW_FORM_GNU_addr_index:
23282 fprintf_unfiltered (f, "address: ");
23283 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
23284 break;
23285 case DW_FORM_block2:
23286 case DW_FORM_block4:
23287 case DW_FORM_block:
23288 case DW_FORM_block1:
23289 fprintf_unfiltered (f, "block: size %s",
23290 pulongest (die->attrs[i].as_block ()->size));
23291 break;
23292 case DW_FORM_exprloc:
23293 fprintf_unfiltered (f, "expression: size %s",
23294 pulongest (die->attrs[i].as_block ()->size));
23295 break;
23296 case DW_FORM_data16:
23297 fprintf_unfiltered (f, "constant of 16 bytes");
23298 break;
23299 case DW_FORM_ref_addr:
23300 fprintf_unfiltered (f, "ref address: ");
23301 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23302 break;
23303 case DW_FORM_GNU_ref_alt:
23304 fprintf_unfiltered (f, "alt ref address: ");
23305 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
23306 break;
23307 case DW_FORM_ref1:
23308 case DW_FORM_ref2:
23309 case DW_FORM_ref4:
23310 case DW_FORM_ref8:
23311 case DW_FORM_ref_udata:
23312 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
23313 (long) (die->attrs[i].as_unsigned ()));
23314 break;
23315 case DW_FORM_data1:
23316 case DW_FORM_data2:
23317 case DW_FORM_data4:
23318 case DW_FORM_data8:
23319 case DW_FORM_udata:
23320 fprintf_unfiltered (f, "constant: %s",
23321 pulongest (die->attrs[i].as_unsigned ()));
23322 break;
23323 case DW_FORM_sec_offset:
23324 fprintf_unfiltered (f, "section offset: %s",
23325 pulongest (die->attrs[i].as_unsigned ()));
23326 break;
23327 case DW_FORM_ref_sig8:
23328 fprintf_unfiltered (f, "signature: %s",
23329 hex_string (die->attrs[i].as_signature ()));
23330 break;
23331 case DW_FORM_string:
23332 case DW_FORM_strp:
23333 case DW_FORM_line_strp:
23334 case DW_FORM_strx:
23335 case DW_FORM_GNU_str_index:
23336 case DW_FORM_GNU_strp_alt:
23337 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
23338 die->attrs[i].as_string ()
23339 ? die->attrs[i].as_string () : "",
23340 die->attrs[i].canonical_string_p () ? "is" : "not");
23341 break;
23342 case DW_FORM_flag:
23343 if (die->attrs[i].as_boolean ())
23344 fprintf_unfiltered (f, "flag: TRUE");
23345 else
23346 fprintf_unfiltered (f, "flag: FALSE");
23347 break;
23348 case DW_FORM_flag_present:
23349 fprintf_unfiltered (f, "flag: TRUE");
23350 break;
23351 case DW_FORM_indirect:
23352 /* The reader will have reduced the indirect form to
23353 the "base form" so this form should not occur. */
23354 fprintf_unfiltered (f,
23355 "unexpected attribute form: DW_FORM_indirect");
23356 break;
23357 case DW_FORM_sdata:
23358 case DW_FORM_implicit_const:
23359 fprintf_unfiltered (f, "constant: %s",
23360 plongest (die->attrs[i].as_signed ()));
23361 break;
23362 default:
23363 fprintf_unfiltered (f, "unsupported attribute form: %d.",
23364 die->attrs[i].form);
23365 break;
23366 }
23367 fprintf_unfiltered (f, "\n");
23368 }
23369 }
23370
23371 static void
23372 dump_die_for_error (struct die_info *die)
23373 {
23374 dump_die_shallow (gdb_stderr, 0, die);
23375 }
23376
23377 static void
23378 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23379 {
23380 int indent = level * 4;
23381
23382 gdb_assert (die != NULL);
23383
23384 if (level >= max_level)
23385 return;
23386
23387 dump_die_shallow (f, indent, die);
23388
23389 if (die->child != NULL)
23390 {
23391 print_spaces (indent, f);
23392 fprintf_unfiltered (f, " Children:");
23393 if (level + 1 < max_level)
23394 {
23395 fprintf_unfiltered (f, "\n");
23396 dump_die_1 (f, level + 1, max_level, die->child);
23397 }
23398 else
23399 {
23400 fprintf_unfiltered (f,
23401 " [not printed, max nesting level reached]\n");
23402 }
23403 }
23404
23405 if (die->sibling != NULL && level > 0)
23406 {
23407 dump_die_1 (f, level, max_level, die->sibling);
23408 }
23409 }
23410
23411 /* This is called from the pdie macro in gdbinit.in.
23412 It's not static so gcc will keep a copy callable from gdb. */
23413
23414 void
23415 dump_die (struct die_info *die, int max_level)
23416 {
23417 dump_die_1 (gdb_stdlog, 0, max_level, die);
23418 }
23419
23420 static void
23421 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
23422 {
23423 void **slot;
23424
23425 slot = htab_find_slot_with_hash (cu->die_hash, die,
23426 to_underlying (die->sect_off),
23427 INSERT);
23428
23429 *slot = die;
23430 }
23431
23432 /* Follow reference or signature attribute ATTR of SRC_DIE.
23433 On entry *REF_CU is the CU of SRC_DIE.
23434 On exit *REF_CU is the CU of the result. */
23435
23436 static struct die_info *
23437 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
23438 struct dwarf2_cu **ref_cu)
23439 {
23440 struct die_info *die;
23441
23442 if (attr->form_is_ref ())
23443 die = follow_die_ref (src_die, attr, ref_cu);
23444 else if (attr->form == DW_FORM_ref_sig8)
23445 die = follow_die_sig (src_die, attr, ref_cu);
23446 else
23447 {
23448 dump_die_for_error (src_die);
23449 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
23450 objfile_name ((*ref_cu)->per_objfile->objfile));
23451 }
23452
23453 return die;
23454 }
23455
23456 /* Follow reference OFFSET.
23457 On entry *REF_CU is the CU of the source die referencing OFFSET.
23458 On exit *REF_CU is the CU of the result.
23459 Returns NULL if OFFSET is invalid. */
23460
23461 static struct die_info *
23462 follow_die_offset (sect_offset sect_off, int offset_in_dwz,
23463 struct dwarf2_cu **ref_cu)
23464 {
23465 struct die_info temp_die;
23466 struct dwarf2_cu *target_cu, *cu = *ref_cu;
23467 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23468
23469 gdb_assert (cu->per_cu != NULL);
23470
23471 target_cu = cu;
23472
23473 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23474 "source CU contains target offset: %d",
23475 sect_offset_str (cu->per_cu->sect_off),
23476 sect_offset_str (sect_off),
23477 cu->header.offset_in_cu_p (sect_off));
23478
23479 if (cu->per_cu->is_debug_types)
23480 {
23481 /* .debug_types CUs cannot reference anything outside their CU.
23482 If they need to, they have to reference a signatured type via
23483 DW_FORM_ref_sig8. */
23484 if (!cu->header.offset_in_cu_p (sect_off))
23485 return NULL;
23486 }
23487 else if (offset_in_dwz != cu->per_cu->is_dwz
23488 || !cu->header.offset_in_cu_p (sect_off))
23489 {
23490 struct dwarf2_per_cu_data *per_cu;
23491
23492 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23493 per_objfile);
23494
23495 dwarf_read_debug_printf_v ("target CU offset: %s, "
23496 "target CU DIEs loaded: %d",
23497 sect_offset_str (per_cu->sect_off),
23498 per_objfile->get_cu (per_cu) != nullptr);
23499
23500 /* If necessary, add it to the queue and load its DIEs.
23501
23502 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23503 it doesn't mean they are currently loaded. Since we require them
23504 to be loaded, we must check for ourselves. */
23505 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language)
23506 || per_objfile->get_cu (per_cu) == nullptr)
23507 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23508 false, cu->language);
23509
23510 target_cu = per_objfile->get_cu (per_cu);
23511 gdb_assert (target_cu != nullptr);
23512 }
23513 else if (cu->dies == NULL)
23514 {
23515 /* We're loading full DIEs during partial symbol reading. */
23516 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
23517 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23518 language_minimal);
23519 }
23520
23521 *ref_cu = target_cu;
23522 temp_die.sect_off = sect_off;
23523
23524 if (target_cu != cu)
23525 target_cu->ancestor = cu;
23526
23527 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
23528 &temp_die,
23529 to_underlying (sect_off));
23530 }
23531
23532 /* Follow reference attribute ATTR of SRC_DIE.
23533 On entry *REF_CU is the CU of SRC_DIE.
23534 On exit *REF_CU is the CU of the result. */
23535
23536 static struct die_info *
23537 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
23538 struct dwarf2_cu **ref_cu)
23539 {
23540 sect_offset sect_off = attr->get_ref_die_offset ();
23541 struct dwarf2_cu *cu = *ref_cu;
23542 struct die_info *die;
23543
23544 die = follow_die_offset (sect_off,
23545 (attr->form == DW_FORM_GNU_ref_alt
23546 || cu->per_cu->is_dwz),
23547 ref_cu);
23548 if (!die)
23549 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23550 "at %s [in module %s]"),
23551 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
23552 objfile_name (cu->per_objfile->objfile));
23553
23554 return die;
23555 }
23556
23557 /* See read.h. */
23558
23559 struct dwarf2_locexpr_baton
23560 dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
23561 dwarf2_per_cu_data *per_cu,
23562 dwarf2_per_objfile *per_objfile,
23563 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23564 bool resolve_abstract_p)
23565 {
23566 struct die_info *die;
23567 struct attribute *attr;
23568 struct dwarf2_locexpr_baton retval;
23569 struct objfile *objfile = per_objfile->objfile;
23570
23571 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23572 if (cu == nullptr)
23573 cu = load_cu (per_cu, per_objfile, false);
23574
23575 if (cu == nullptr)
23576 {
23577 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23578 Instead just throw an error, not much else we can do. */
23579 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23580 sect_offset_str (sect_off), objfile_name (objfile));
23581 }
23582
23583 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23584 if (!die)
23585 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23586 sect_offset_str (sect_off), objfile_name (objfile));
23587
23588 attr = dwarf2_attr (die, DW_AT_location, cu);
23589 if (!attr && resolve_abstract_p
23590 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23591 != per_objfile->per_bfd->abstract_to_concrete.end ()))
23592 {
23593 CORE_ADDR pc = get_frame_pc ();
23594 CORE_ADDR baseaddr = objfile->text_section_offset ();
23595 struct gdbarch *gdbarch = objfile->arch ();
23596
23597 for (const auto &cand_off
23598 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
23599 {
23600 struct dwarf2_cu *cand_cu = cu;
23601 struct die_info *cand
23602 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23603 if (!cand
23604 || !cand->parent
23605 || cand->parent->tag != DW_TAG_subprogram)
23606 continue;
23607
23608 CORE_ADDR pc_low, pc_high;
23609 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
23610 if (pc_low == ((CORE_ADDR) -1))
23611 continue;
23612 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23613 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23614 if (!(pc_low <= pc && pc < pc_high))
23615 continue;
23616
23617 die = cand;
23618 attr = dwarf2_attr (die, DW_AT_location, cu);
23619 break;
23620 }
23621 }
23622
23623 if (!attr)
23624 {
23625 /* DWARF: "If there is no such attribute, then there is no effect.".
23626 DATA is ignored if SIZE is 0. */
23627
23628 retval.data = NULL;
23629 retval.size = 0;
23630 }
23631 else if (attr->form_is_section_offset ())
23632 {
23633 struct dwarf2_loclist_baton loclist_baton;
23634 CORE_ADDR pc = get_frame_pc ();
23635 size_t size;
23636
23637 fill_in_loclist_baton (cu, &loclist_baton, attr);
23638
23639 retval.data = dwarf2_find_location_expression (&loclist_baton,
23640 &size, pc);
23641 retval.size = size;
23642 }
23643 else
23644 {
23645 if (!attr->form_is_block ())
23646 error (_("Dwarf Error: DIE at %s referenced in module %s "
23647 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
23648 sect_offset_str (sect_off), objfile_name (objfile));
23649
23650 struct dwarf_block *block = attr->as_block ();
23651 retval.data = block->data;
23652 retval.size = block->size;
23653 }
23654 retval.per_objfile = per_objfile;
23655 retval.per_cu = cu->per_cu;
23656
23657 per_objfile->age_comp_units ();
23658
23659 return retval;
23660 }
23661
23662 /* See read.h. */
23663
23664 struct dwarf2_locexpr_baton
23665 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23666 dwarf2_per_cu_data *per_cu,
23667 dwarf2_per_objfile *per_objfile,
23668 gdb::function_view<CORE_ADDR ()> get_frame_pc)
23669 {
23670 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
23671
23672 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
23673 get_frame_pc);
23674 }
23675
23676 /* Write a constant of a given type as target-ordered bytes into
23677 OBSTACK. */
23678
23679 static const gdb_byte *
23680 write_constant_as_bytes (struct obstack *obstack,
23681 enum bfd_endian byte_order,
23682 struct type *type,
23683 ULONGEST value,
23684 LONGEST *len)
23685 {
23686 gdb_byte *result;
23687
23688 *len = TYPE_LENGTH (type);
23689 result = (gdb_byte *) obstack_alloc (obstack, *len);
23690 store_unsigned_integer (result, *len, byte_order, value);
23691
23692 return result;
23693 }
23694
23695 /* See read.h. */
23696
23697 const gdb_byte *
23698 dwarf2_fetch_constant_bytes (sect_offset sect_off,
23699 dwarf2_per_cu_data *per_cu,
23700 dwarf2_per_objfile *per_objfile,
23701 obstack *obstack,
23702 LONGEST *len)
23703 {
23704 struct die_info *die;
23705 struct attribute *attr;
23706 const gdb_byte *result = NULL;
23707 struct type *type;
23708 LONGEST value;
23709 enum bfd_endian byte_order;
23710 struct objfile *objfile = per_objfile->objfile;
23711
23712 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23713 if (cu == nullptr)
23714 cu = load_cu (per_cu, per_objfile, false);
23715
23716 if (cu == nullptr)
23717 {
23718 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23719 Instead just throw an error, not much else we can do. */
23720 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23721 sect_offset_str (sect_off), objfile_name (objfile));
23722 }
23723
23724 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23725 if (!die)
23726 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23727 sect_offset_str (sect_off), objfile_name (objfile));
23728
23729 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23730 if (attr == NULL)
23731 return NULL;
23732
23733 byte_order = (bfd_big_endian (objfile->obfd)
23734 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23735
23736 switch (attr->form)
23737 {
23738 case DW_FORM_addr:
23739 case DW_FORM_addrx:
23740 case DW_FORM_GNU_addr_index:
23741 {
23742 gdb_byte *tem;
23743
23744 *len = cu->header.addr_size;
23745 tem = (gdb_byte *) obstack_alloc (obstack, *len);
23746 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
23747 result = tem;
23748 }
23749 break;
23750 case DW_FORM_string:
23751 case DW_FORM_strp:
23752 case DW_FORM_strx:
23753 case DW_FORM_GNU_str_index:
23754 case DW_FORM_GNU_strp_alt:
23755 /* The string is already allocated on the objfile obstack, point
23756 directly to it. */
23757 {
23758 const char *attr_name = attr->as_string ();
23759 result = (const gdb_byte *) attr_name;
23760 *len = strlen (attr_name);
23761 }
23762 break;
23763 case DW_FORM_block1:
23764 case DW_FORM_block2:
23765 case DW_FORM_block4:
23766 case DW_FORM_block:
23767 case DW_FORM_exprloc:
23768 case DW_FORM_data16:
23769 {
23770 struct dwarf_block *block = attr->as_block ();
23771 result = block->data;
23772 *len = block->size;
23773 }
23774 break;
23775
23776 /* The DW_AT_const_value attributes are supposed to carry the
23777 symbol's value "represented as it would be on the target
23778 architecture." By the time we get here, it's already been
23779 converted to host endianness, so we just need to sign- or
23780 zero-extend it as appropriate. */
23781 case DW_FORM_data1:
23782 type = die_type (die, cu);
23783 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23784 if (result == NULL)
23785 result = write_constant_as_bytes (obstack, byte_order,
23786 type, value, len);
23787 break;
23788 case DW_FORM_data2:
23789 type = die_type (die, cu);
23790 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23791 if (result == NULL)
23792 result = write_constant_as_bytes (obstack, byte_order,
23793 type, value, len);
23794 break;
23795 case DW_FORM_data4:
23796 type = die_type (die, cu);
23797 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23798 if (result == NULL)
23799 result = write_constant_as_bytes (obstack, byte_order,
23800 type, value, len);
23801 break;
23802 case DW_FORM_data8:
23803 type = die_type (die, cu);
23804 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23805 if (result == NULL)
23806 result = write_constant_as_bytes (obstack, byte_order,
23807 type, value, len);
23808 break;
23809
23810 case DW_FORM_sdata:
23811 case DW_FORM_implicit_const:
23812 type = die_type (die, cu);
23813 result = write_constant_as_bytes (obstack, byte_order,
23814 type, attr->as_signed (), len);
23815 break;
23816
23817 case DW_FORM_udata:
23818 type = die_type (die, cu);
23819 result = write_constant_as_bytes (obstack, byte_order,
23820 type, attr->as_unsigned (), len);
23821 break;
23822
23823 default:
23824 complaint (_("unsupported const value attribute form: '%s'"),
23825 dwarf_form_name (attr->form));
23826 break;
23827 }
23828
23829 return result;
23830 }
23831
23832 /* See read.h. */
23833
23834 struct type *
23835 dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
23836 dwarf2_per_cu_data *per_cu,
23837 dwarf2_per_objfile *per_objfile)
23838 {
23839 struct die_info *die;
23840
23841 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
23842 if (cu == nullptr)
23843 cu = load_cu (per_cu, per_objfile, false);
23844
23845 if (cu == nullptr)
23846 return nullptr;
23847
23848 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
23849 if (!die)
23850 return NULL;
23851
23852 return die_type (die, cu);
23853 }
23854
23855 /* See read.h. */
23856
23857 struct type *
23858 dwarf2_get_die_type (cu_offset die_offset,
23859 dwarf2_per_cu_data *per_cu,
23860 dwarf2_per_objfile *per_objfile)
23861 {
23862 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
23863 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
23864 }
23865
23866 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
23867 On entry *REF_CU is the CU of SRC_DIE.
23868 On exit *REF_CU is the CU of the result.
23869 Returns NULL if the referenced DIE isn't found. */
23870
23871 static struct die_info *
23872 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23873 struct dwarf2_cu **ref_cu)
23874 {
23875 struct die_info temp_die;
23876 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
23877 struct die_info *die;
23878 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
23879
23880
23881 /* While it might be nice to assert sig_type->type == NULL here,
23882 we can get here for DW_AT_imported_declaration where we need
23883 the DIE not the type. */
23884
23885 /* If necessary, add it to the queue and load its DIEs.
23886
23887 Even if maybe_queue_comp_unit doesn't require us to load the CU's DIEs,
23888 it doesn't mean they are currently loaded. Since we require them
23889 to be loaded, we must check for ourselves. */
23890 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
23891 language_minimal)
23892 || per_objfile->get_cu (&sig_type->per_cu) == nullptr)
23893 read_signatured_type (sig_type, per_objfile);
23894
23895 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
23896 gdb_assert (sig_cu != NULL);
23897 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23898 temp_die.sect_off = sig_type->type_offset_in_section;
23899 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
23900 to_underlying (temp_die.sect_off));
23901 if (die)
23902 {
23903 /* For .gdb_index version 7 keep track of included TUs.
23904 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23905 if (per_objfile->per_bfd->index_table != NULL
23906 && per_objfile->per_bfd->index_table->version <= 7)
23907 {
23908 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
23909 }
23910
23911 *ref_cu = sig_cu;
23912 if (sig_cu != cu)
23913 sig_cu->ancestor = cu;
23914
23915 return die;
23916 }
23917
23918 return NULL;
23919 }
23920
23921 /* Follow signatured type referenced by ATTR in SRC_DIE.
23922 On entry *REF_CU is the CU of SRC_DIE.
23923 On exit *REF_CU is the CU of the result.
23924 The result is the DIE of the type.
23925 If the referenced type cannot be found an error is thrown. */
23926
23927 static struct die_info *
23928 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
23929 struct dwarf2_cu **ref_cu)
23930 {
23931 ULONGEST signature = attr->as_signature ();
23932 struct signatured_type *sig_type;
23933 struct die_info *die;
23934
23935 gdb_assert (attr->form == DW_FORM_ref_sig8);
23936
23937 sig_type = lookup_signatured_type (*ref_cu, signature);
23938 /* sig_type will be NULL if the signatured type is missing from
23939 the debug info. */
23940 if (sig_type == NULL)
23941 {
23942 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23943 " from DIE at %s [in module %s]"),
23944 hex_string (signature), sect_offset_str (src_die->sect_off),
23945 objfile_name ((*ref_cu)->per_objfile->objfile));
23946 }
23947
23948 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23949 if (die == NULL)
23950 {
23951 dump_die_for_error (src_die);
23952 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
23953 " from DIE at %s [in module %s]"),
23954 hex_string (signature), sect_offset_str (src_die->sect_off),
23955 objfile_name ((*ref_cu)->per_objfile->objfile));
23956 }
23957
23958 return die;
23959 }
23960
23961 /* Get the type specified by SIGNATURE referenced in DIE/CU,
23962 reading in and processing the type unit if necessary. */
23963
23964 static struct type *
23965 get_signatured_type (struct die_info *die, ULONGEST signature,
23966 struct dwarf2_cu *cu)
23967 {
23968 dwarf2_per_objfile *per_objfile = cu->per_objfile;
23969 struct signatured_type *sig_type;
23970 struct dwarf2_cu *type_cu;
23971 struct die_info *type_die;
23972 struct type *type;
23973
23974 sig_type = lookup_signatured_type (cu, signature);
23975 /* sig_type will be NULL if the signatured type is missing from
23976 the debug info. */
23977 if (sig_type == NULL)
23978 {
23979 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
23980 " from DIE at %s [in module %s]"),
23981 hex_string (signature), sect_offset_str (die->sect_off),
23982 objfile_name (per_objfile->objfile));
23983 return build_error_marker_type (cu, die);
23984 }
23985
23986 /* If we already know the type we're done. */
23987 type = per_objfile->get_type_for_signatured_type (sig_type);
23988 if (type != nullptr)
23989 return type;
23990
23991 type_cu = cu;
23992 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23993 if (type_die != NULL)
23994 {
23995 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23996 is created. This is important, for example, because for c++ classes
23997 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23998 type = read_type_die (type_die, type_cu);
23999 if (type == NULL)
24000 {
24001 complaint (_("Dwarf Error: Cannot build signatured type %s"
24002 " referenced from DIE at %s [in module %s]"),
24003 hex_string (signature), sect_offset_str (die->sect_off),
24004 objfile_name (per_objfile->objfile));
24005 type = build_error_marker_type (cu, die);
24006 }
24007 }
24008 else
24009 {
24010 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
24011 " from DIE at %s [in module %s]"),
24012 hex_string (signature), sect_offset_str (die->sect_off),
24013 objfile_name (per_objfile->objfile));
24014 type = build_error_marker_type (cu, die);
24015 }
24016
24017 per_objfile->set_type_for_signatured_type (sig_type, type);
24018
24019 return type;
24020 }
24021
24022 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24023 reading in and processing the type unit if necessary. */
24024
24025 static struct type *
24026 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
24027 struct dwarf2_cu *cu) /* ARI: editCase function */
24028 {
24029 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
24030 if (attr->form_is_ref ())
24031 {
24032 struct dwarf2_cu *type_cu = cu;
24033 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24034
24035 return read_type_die (type_die, type_cu);
24036 }
24037 else if (attr->form == DW_FORM_ref_sig8)
24038 {
24039 return get_signatured_type (die, attr->as_signature (), cu);
24040 }
24041 else
24042 {
24043 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24044
24045 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
24046 " at %s [in module %s]"),
24047 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
24048 objfile_name (per_objfile->objfile));
24049 return build_error_marker_type (cu, die);
24050 }
24051 }
24052
24053 /* Load the DIEs associated with type unit PER_CU into memory. */
24054
24055 static void
24056 load_full_type_unit (dwarf2_per_cu_data *per_cu,
24057 dwarf2_per_objfile *per_objfile)
24058 {
24059 struct signatured_type *sig_type;
24060
24061 /* Caller is responsible for ensuring type_unit_groups don't get here. */
24062 gdb_assert (! per_cu->type_unit_group_p ());
24063
24064 /* We have the per_cu, but we need the signatured_type.
24065 Fortunately this is an easy translation. */
24066 gdb_assert (per_cu->is_debug_types);
24067 sig_type = (struct signatured_type *) per_cu;
24068
24069 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24070
24071 read_signatured_type (sig_type, per_objfile);
24072
24073 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
24074 }
24075
24076 /* Read in a signatured type and build its CU and DIEs.
24077 If the type is a stub for the real type in a DWO file,
24078 read in the real type from the DWO file as well. */
24079
24080 static void
24081 read_signatured_type (signatured_type *sig_type,
24082 dwarf2_per_objfile *per_objfile)
24083 {
24084 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
24085
24086 gdb_assert (per_cu->is_debug_types);
24087 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
24088
24089 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
24090
24091 if (!reader.dummy_p)
24092 {
24093 struct dwarf2_cu *cu = reader.cu;
24094 const gdb_byte *info_ptr = reader.info_ptr;
24095
24096 gdb_assert (cu->die_hash == NULL);
24097 cu->die_hash =
24098 htab_create_alloc_ex (cu->header.length / 12,
24099 die_hash,
24100 die_eq,
24101 NULL,
24102 &cu->comp_unit_obstack,
24103 hashtab_obstack_allocate,
24104 dummy_obstack_deallocate);
24105
24106 if (reader.comp_unit_die->has_children)
24107 reader.comp_unit_die->child
24108 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24109 reader.comp_unit_die);
24110 cu->dies = reader.comp_unit_die;
24111 /* comp_unit_die is not stored in die_hash, no need. */
24112
24113 /* We try not to read any attributes in this function, because
24114 not all CUs needed for references have been loaded yet, and
24115 symbol table processing isn't initialized. But we have to
24116 set the CU language, or we won't be able to build types
24117 correctly. Similarly, if we do not read the producer, we can
24118 not apply producer-specific interpretation. */
24119 prepare_one_comp_unit (cu, cu->dies, language_minimal);
24120
24121 reader.keep ();
24122 }
24123
24124 sig_type->per_cu.tu_read = 1;
24125 }
24126
24127 /* Decode simple location descriptions.
24128 Given a pointer to a dwarf block that defines a location, compute
24129 the location and return the value. If COMPUTED is non-null, it is
24130 set to true to indicate that decoding was successful, and false
24131 otherwise. If COMPUTED is null, then this function may emit a
24132 complaint. */
24133
24134 static CORE_ADDR
24135 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
24136 {
24137 struct objfile *objfile = cu->per_objfile->objfile;
24138 size_t i;
24139 size_t size = blk->size;
24140 const gdb_byte *data = blk->data;
24141 CORE_ADDR stack[64];
24142 int stacki;
24143 unsigned int bytes_read, unsnd;
24144 gdb_byte op;
24145
24146 if (computed != nullptr)
24147 *computed = false;
24148
24149 i = 0;
24150 stacki = 0;
24151 stack[stacki] = 0;
24152 stack[++stacki] = 0;
24153
24154 while (i < size)
24155 {
24156 op = data[i++];
24157 switch (op)
24158 {
24159 case DW_OP_lit0:
24160 case DW_OP_lit1:
24161 case DW_OP_lit2:
24162 case DW_OP_lit3:
24163 case DW_OP_lit4:
24164 case DW_OP_lit5:
24165 case DW_OP_lit6:
24166 case DW_OP_lit7:
24167 case DW_OP_lit8:
24168 case DW_OP_lit9:
24169 case DW_OP_lit10:
24170 case DW_OP_lit11:
24171 case DW_OP_lit12:
24172 case DW_OP_lit13:
24173 case DW_OP_lit14:
24174 case DW_OP_lit15:
24175 case DW_OP_lit16:
24176 case DW_OP_lit17:
24177 case DW_OP_lit18:
24178 case DW_OP_lit19:
24179 case DW_OP_lit20:
24180 case DW_OP_lit21:
24181 case DW_OP_lit22:
24182 case DW_OP_lit23:
24183 case DW_OP_lit24:
24184 case DW_OP_lit25:
24185 case DW_OP_lit26:
24186 case DW_OP_lit27:
24187 case DW_OP_lit28:
24188 case DW_OP_lit29:
24189 case DW_OP_lit30:
24190 case DW_OP_lit31:
24191 stack[++stacki] = op - DW_OP_lit0;
24192 break;
24193
24194 case DW_OP_reg0:
24195 case DW_OP_reg1:
24196 case DW_OP_reg2:
24197 case DW_OP_reg3:
24198 case DW_OP_reg4:
24199 case DW_OP_reg5:
24200 case DW_OP_reg6:
24201 case DW_OP_reg7:
24202 case DW_OP_reg8:
24203 case DW_OP_reg9:
24204 case DW_OP_reg10:
24205 case DW_OP_reg11:
24206 case DW_OP_reg12:
24207 case DW_OP_reg13:
24208 case DW_OP_reg14:
24209 case DW_OP_reg15:
24210 case DW_OP_reg16:
24211 case DW_OP_reg17:
24212 case DW_OP_reg18:
24213 case DW_OP_reg19:
24214 case DW_OP_reg20:
24215 case DW_OP_reg21:
24216 case DW_OP_reg22:
24217 case DW_OP_reg23:
24218 case DW_OP_reg24:
24219 case DW_OP_reg25:
24220 case DW_OP_reg26:
24221 case DW_OP_reg27:
24222 case DW_OP_reg28:
24223 case DW_OP_reg29:
24224 case DW_OP_reg30:
24225 case DW_OP_reg31:
24226 stack[++stacki] = op - DW_OP_reg0;
24227 if (i < size)
24228 {
24229 if (computed == nullptr)
24230 dwarf2_complex_location_expr_complaint ();
24231 else
24232 return 0;
24233 }
24234 break;
24235
24236 case DW_OP_regx:
24237 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24238 i += bytes_read;
24239 stack[++stacki] = unsnd;
24240 if (i < size)
24241 {
24242 if (computed == nullptr)
24243 dwarf2_complex_location_expr_complaint ();
24244 else
24245 return 0;
24246 }
24247 break;
24248
24249 case DW_OP_addr:
24250 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24251 &bytes_read);
24252 i += bytes_read;
24253 break;
24254
24255 case DW_OP_const1u:
24256 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24257 i += 1;
24258 break;
24259
24260 case DW_OP_const1s:
24261 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24262 i += 1;
24263 break;
24264
24265 case DW_OP_const2u:
24266 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24267 i += 2;
24268 break;
24269
24270 case DW_OP_const2s:
24271 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24272 i += 2;
24273 break;
24274
24275 case DW_OP_const4u:
24276 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24277 i += 4;
24278 break;
24279
24280 case DW_OP_const4s:
24281 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24282 i += 4;
24283 break;
24284
24285 case DW_OP_const8u:
24286 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24287 i += 8;
24288 break;
24289
24290 case DW_OP_constu:
24291 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24292 &bytes_read);
24293 i += bytes_read;
24294 break;
24295
24296 case DW_OP_consts:
24297 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24298 i += bytes_read;
24299 break;
24300
24301 case DW_OP_dup:
24302 stack[stacki + 1] = stack[stacki];
24303 stacki++;
24304 break;
24305
24306 case DW_OP_plus:
24307 stack[stacki - 1] += stack[stacki];
24308 stacki--;
24309 break;
24310
24311 case DW_OP_plus_uconst:
24312 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24313 &bytes_read);
24314 i += bytes_read;
24315 break;
24316
24317 case DW_OP_minus:
24318 stack[stacki - 1] -= stack[stacki];
24319 stacki--;
24320 break;
24321
24322 case DW_OP_deref:
24323 /* If we're not the last op, then we definitely can't encode
24324 this using GDB's address_class enum. This is valid for partial
24325 global symbols, although the variable's address will be bogus
24326 in the psymtab. */
24327 if (i < size)
24328 {
24329 if (computed == nullptr)
24330 dwarf2_complex_location_expr_complaint ();
24331 else
24332 return 0;
24333 }
24334 break;
24335
24336 case DW_OP_GNU_push_tls_address:
24337 case DW_OP_form_tls_address:
24338 /* The top of the stack has the offset from the beginning
24339 of the thread control block at which the variable is located. */
24340 /* Nothing should follow this operator, so the top of stack would
24341 be returned. */
24342 /* This is valid for partial global symbols, but the variable's
24343 address will be bogus in the psymtab. Make it always at least
24344 non-zero to not look as a variable garbage collected by linker
24345 which have DW_OP_addr 0. */
24346 if (i < size)
24347 {
24348 if (computed == nullptr)
24349 dwarf2_complex_location_expr_complaint ();
24350 else
24351 return 0;
24352 }
24353 stack[stacki]++;
24354 break;
24355
24356 case DW_OP_GNU_uninit:
24357 if (computed != nullptr)
24358 return 0;
24359 break;
24360
24361 case DW_OP_addrx:
24362 case DW_OP_GNU_addr_index:
24363 case DW_OP_GNU_const_index:
24364 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24365 &bytes_read);
24366 i += bytes_read;
24367 break;
24368
24369 default:
24370 if (computed == nullptr)
24371 {
24372 const char *name = get_DW_OP_name (op);
24373
24374 if (name)
24375 complaint (_("unsupported stack op: '%s'"),
24376 name);
24377 else
24378 complaint (_("unsupported stack op: '%02x'"),
24379 op);
24380 }
24381
24382 return (stack[stacki]);
24383 }
24384
24385 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24386 outside of the allocated space. Also enforce minimum>0. */
24387 if (stacki >= ARRAY_SIZE (stack) - 1)
24388 {
24389 if (computed == nullptr)
24390 complaint (_("location description stack overflow"));
24391 return 0;
24392 }
24393
24394 if (stacki <= 0)
24395 {
24396 if (computed == nullptr)
24397 complaint (_("location description stack underflow"));
24398 return 0;
24399 }
24400 }
24401
24402 if (computed != nullptr)
24403 *computed = true;
24404 return (stack[stacki]);
24405 }
24406
24407 /* memory allocation interface */
24408
24409 static struct dwarf_block *
24410 dwarf_alloc_block (struct dwarf2_cu *cu)
24411 {
24412 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
24413 }
24414
24415 static struct die_info *
24416 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
24417 {
24418 struct die_info *die;
24419 size_t size = sizeof (struct die_info);
24420
24421 if (num_attrs > 1)
24422 size += (num_attrs - 1) * sizeof (struct attribute);
24423
24424 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
24425 memset (die, 0, sizeof (struct die_info));
24426 return (die);
24427 }
24428
24429 \f
24430
24431 /* Macro support. */
24432
24433 /* An overload of dwarf_decode_macros that finds the correct section
24434 and ensures it is read in before calling the other overload. */
24435
24436 static void
24437 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24438 int section_is_gnu)
24439 {
24440 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24441 struct objfile *objfile = per_objfile->objfile;
24442 const struct line_header *lh = cu->line_header;
24443 unsigned int offset_size = cu->header.offset_size;
24444 struct dwarf2_section_info *section;
24445 const char *section_name;
24446
24447 if (cu->dwo_unit != nullptr)
24448 {
24449 if (section_is_gnu)
24450 {
24451 section = &cu->dwo_unit->dwo_file->sections.macro;
24452 section_name = ".debug_macro.dwo";
24453 }
24454 else
24455 {
24456 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24457 section_name = ".debug_macinfo.dwo";
24458 }
24459 }
24460 else
24461 {
24462 if (section_is_gnu)
24463 {
24464 section = &per_objfile->per_bfd->macro;
24465 section_name = ".debug_macro";
24466 }
24467 else
24468 {
24469 section = &per_objfile->per_bfd->macinfo;
24470 section_name = ".debug_macinfo";
24471 }
24472 }
24473
24474 section->read (objfile);
24475 if (section->buffer == nullptr)
24476 {
24477 complaint (_("missing %s section"), section_name);
24478 return;
24479 }
24480
24481 buildsym_compunit *builder = cu->get_builder ();
24482
24483 struct dwarf2_section_info *str_offsets_section;
24484 struct dwarf2_section_info *str_section;
24485 ULONGEST str_offsets_base;
24486
24487 if (cu->dwo_unit != nullptr)
24488 {
24489 str_offsets_section = &cu->dwo_unit->dwo_file
24490 ->sections.str_offsets;
24491 str_section = &cu->dwo_unit->dwo_file->sections.str;
24492 str_offsets_base = cu->header.addr_size;
24493 }
24494 else
24495 {
24496 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24497 str_section = &per_objfile->per_bfd->str;
24498 str_offsets_base = *cu->str_offsets_base;
24499 }
24500
24501 dwarf_decode_macros (per_objfile, builder, section, lh,
24502 offset_size, offset, str_section, str_offsets_section,
24503 str_offsets_base, section_is_gnu);
24504 }
24505
24506 /* Return the .debug_loc section to use for CU.
24507 For DWO files use .debug_loc.dwo. */
24508
24509 static struct dwarf2_section_info *
24510 cu_debug_loc_section (struct dwarf2_cu *cu)
24511 {
24512 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24513
24514 if (cu->dwo_unit)
24515 {
24516 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24517
24518 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24519 }
24520 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24521 : &per_objfile->per_bfd->loc);
24522 }
24523
24524 /* Return the .debug_rnglists section to use for CU. */
24525 static struct dwarf2_section_info *
24526 cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24527 {
24528 if (cu->header.version < 5)
24529 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24530 cu->header.version);
24531 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24532
24533 /* Make sure we read the .debug_rnglists section from the file that
24534 contains the DW_AT_ranges attribute we are reading. Normally that
24535 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24536 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24537 program. */
24538 if (cu->dwo_unit != nullptr
24539 && tag != DW_TAG_compile_unit
24540 && tag != DW_TAG_skeleton_unit)
24541 {
24542 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24543
24544 if (sections->rnglists.size > 0)
24545 return &sections->rnglists;
24546 else
24547 error (_(".debug_rnglists section is missing from .dwo file."));
24548 }
24549 return &dwarf2_per_objfile->per_bfd->rnglists;
24550 }
24551
24552 /* A helper function that fills in a dwarf2_loclist_baton. */
24553
24554 static void
24555 fill_in_loclist_baton (struct dwarf2_cu *cu,
24556 struct dwarf2_loclist_baton *baton,
24557 const struct attribute *attr)
24558 {
24559 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24560 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24561
24562 section->read (per_objfile->objfile);
24563
24564 baton->per_objfile = per_objfile;
24565 baton->per_cu = cu->per_cu;
24566 gdb_assert (baton->per_cu);
24567 /* We don't know how long the location list is, but make sure we
24568 don't run off the edge of the section. */
24569 baton->size = section->size - attr->as_unsigned ();
24570 baton->data = section->buffer + attr->as_unsigned ();
24571 if (cu->base_address.has_value ())
24572 baton->base_address = *cu->base_address;
24573 else
24574 baton->base_address = 0;
24575 baton->from_dwo = cu->dwo_unit != NULL;
24576 }
24577
24578 static void
24579 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
24580 struct dwarf2_cu *cu, int is_block)
24581 {
24582 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24583 struct objfile *objfile = per_objfile->objfile;
24584 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24585
24586 if (attr->form_is_section_offset ()
24587 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
24588 the section. If so, fall through to the complaint in the
24589 other branch. */
24590 && attr->as_unsigned () < section->get_size (objfile))
24591 {
24592 struct dwarf2_loclist_baton *baton;
24593
24594 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
24595
24596 fill_in_loclist_baton (cu, baton, attr);
24597
24598 if (!cu->base_address.has_value ())
24599 complaint (_("Location list used without "
24600 "specifying the CU base address."));
24601
24602 SYMBOL_ACLASS_INDEX (sym) = (is_block
24603 ? dwarf2_loclist_block_index
24604 : dwarf2_loclist_index);
24605 SYMBOL_LOCATION_BATON (sym) = baton;
24606 }
24607 else
24608 {
24609 struct dwarf2_locexpr_baton *baton;
24610
24611 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
24612 baton->per_objfile = per_objfile;
24613 baton->per_cu = cu->per_cu;
24614 gdb_assert (baton->per_cu);
24615
24616 if (attr->form_is_block ())
24617 {
24618 /* Note that we're just copying the block's data pointer
24619 here, not the actual data. We're still pointing into the
24620 info_buffer for SYM's objfile; right now we never release
24621 that buffer, but when we do clean up properly this may
24622 need to change. */
24623 struct dwarf_block *block = attr->as_block ();
24624 baton->size = block->size;
24625 baton->data = block->data;
24626 }
24627 else
24628 {
24629 dwarf2_invalid_attrib_class_complaint ("location description",
24630 sym->natural_name ());
24631 baton->size = 0;
24632 }
24633
24634 SYMBOL_ACLASS_INDEX (sym) = (is_block
24635 ? dwarf2_locexpr_block_index
24636 : dwarf2_locexpr_index);
24637 SYMBOL_LOCATION_BATON (sym) = baton;
24638 }
24639 }
24640
24641 /* See read.h. */
24642
24643 const comp_unit_head *
24644 dwarf2_per_cu_data::get_header () const
24645 {
24646 if (!m_header_read_in)
24647 {
24648 const gdb_byte *info_ptr
24649 = this->section->buffer + to_underlying (this->sect_off);
24650
24651 memset (&m_header, 0, sizeof (m_header));
24652
24653 read_comp_unit_head (&m_header, info_ptr, this->section,
24654 rcuh_kind::COMPILE);
24655
24656 m_header_read_in = true;
24657 }
24658
24659 return &m_header;
24660 }
24661
24662 /* See read.h. */
24663
24664 int
24665 dwarf2_per_cu_data::addr_size () const
24666 {
24667 return this->get_header ()->addr_size;
24668 }
24669
24670 /* See read.h. */
24671
24672 int
24673 dwarf2_per_cu_data::offset_size () const
24674 {
24675 return this->get_header ()->offset_size;
24676 }
24677
24678 /* See read.h. */
24679
24680 int
24681 dwarf2_per_cu_data::ref_addr_size () const
24682 {
24683 const comp_unit_head *header = this->get_header ();
24684
24685 if (header->version == 2)
24686 return header->addr_size;
24687 else
24688 return header->offset_size;
24689 }
24690
24691 /* See read.h. */
24692
24693 struct type *
24694 dwarf2_cu::addr_type () const
24695 {
24696 struct objfile *objfile = this->per_objfile->objfile;
24697 struct type *void_type = objfile_type (objfile)->builtin_void;
24698 struct type *addr_type = lookup_pointer_type (void_type);
24699 int addr_size = this->per_cu->addr_size ();
24700
24701 if (TYPE_LENGTH (addr_type) == addr_size)
24702 return addr_type;
24703
24704 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
24705 return addr_type;
24706 }
24707
24708 /* A helper function for dwarf2_find_containing_comp_unit that returns
24709 the index of the result, and that searches a vector. It will
24710 return a result even if the offset in question does not actually
24711 occur in any CU. This is separate so that it can be unit
24712 tested. */
24713
24714 static int
24715 dwarf2_find_containing_comp_unit
24716 (sect_offset sect_off,
24717 unsigned int offset_in_dwz,
24718 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
24719 {
24720 int low, high;
24721
24722 low = 0;
24723 high = all_comp_units.size () - 1;
24724 while (high > low)
24725 {
24726 struct dwarf2_per_cu_data *mid_cu;
24727 int mid = low + (high - low) / 2;
24728
24729 mid_cu = all_comp_units[mid];
24730 if (mid_cu->is_dwz > offset_in_dwz
24731 || (mid_cu->is_dwz == offset_in_dwz
24732 && mid_cu->sect_off + mid_cu->length > sect_off))
24733 high = mid;
24734 else
24735 low = mid + 1;
24736 }
24737 gdb_assert (low == high);
24738 return low;
24739 }
24740
24741 /* Locate the .debug_info compilation unit from CU's objfile which contains
24742 the DIE at OFFSET. Raises an error on failure. */
24743
24744 static struct dwarf2_per_cu_data *
24745 dwarf2_find_containing_comp_unit (sect_offset sect_off,
24746 unsigned int offset_in_dwz,
24747 dwarf2_per_objfile *per_objfile)
24748 {
24749 int low = dwarf2_find_containing_comp_unit
24750 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24751 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
24752
24753 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
24754 {
24755 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
24756 error (_("Dwarf Error: could not find partial DIE containing "
24757 "offset %s [in module %s]"),
24758 sect_offset_str (sect_off),
24759 bfd_get_filename (per_objfile->objfile->obfd));
24760
24761 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
24762 <= sect_off);
24763 return per_objfile->per_bfd->all_comp_units[low-1];
24764 }
24765 else
24766 {
24767 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
24768 && sect_off >= this_cu->sect_off + this_cu->length)
24769 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
24770 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
24771 return this_cu;
24772 }
24773 }
24774
24775 #if GDB_SELF_TEST
24776
24777 namespace selftests {
24778 namespace find_containing_comp_unit {
24779
24780 static void
24781 run_test ()
24782 {
24783 struct dwarf2_per_cu_data one {};
24784 struct dwarf2_per_cu_data two {};
24785 struct dwarf2_per_cu_data three {};
24786 struct dwarf2_per_cu_data four {};
24787
24788 one.length = 5;
24789 two.sect_off = sect_offset (one.length);
24790 two.length = 7;
24791
24792 three.length = 5;
24793 three.is_dwz = 1;
24794 four.sect_off = sect_offset (three.length);
24795 four.length = 7;
24796 four.is_dwz = 1;
24797
24798 std::vector<dwarf2_per_cu_data *> units;
24799 units.push_back (&one);
24800 units.push_back (&two);
24801 units.push_back (&three);
24802 units.push_back (&four);
24803
24804 int result;
24805
24806 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24807 SELF_CHECK (units[result] == &one);
24808 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24809 SELF_CHECK (units[result] == &one);
24810 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24811 SELF_CHECK (units[result] == &two);
24812
24813 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24814 SELF_CHECK (units[result] == &three);
24815 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24816 SELF_CHECK (units[result] == &three);
24817 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24818 SELF_CHECK (units[result] == &four);
24819 }
24820
24821 }
24822 }
24823
24824 #endif /* GDB_SELF_TEST */
24825
24826 /* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
24827
24828 dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24829 dwarf2_per_objfile *per_objfile)
24830 : per_cu (per_cu),
24831 per_objfile (per_objfile),
24832 mark (false),
24833 has_loclist (false),
24834 checked_producer (false),
24835 producer_is_gxx_lt_4_6 (false),
24836 producer_is_gcc_lt_4_3 (false),
24837 producer_is_icc (false),
24838 producer_is_icc_lt_14 (false),
24839 producer_is_codewarrior (false),
24840 processing_has_namespace_info (false)
24841 {
24842 }
24843
24844 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24845
24846 static void
24847 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24848 enum language pretend_language)
24849 {
24850 struct attribute *attr;
24851
24852 /* Set the language we're debugging. */
24853 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24854 if (attr != nullptr)
24855 set_cu_language (attr->constant_value (0), cu);
24856 else
24857 {
24858 cu->language = pretend_language;
24859 cu->language_defn = language_def (cu->language);
24860 }
24861
24862 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
24863 }
24864
24865 /* See read.h. */
24866
24867 dwarf2_cu *
24868 dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
24869 {
24870 auto it = m_dwarf2_cus.find (per_cu);
24871 if (it == m_dwarf2_cus.end ())
24872 return nullptr;
24873
24874 return it->second;
24875 }
24876
24877 /* See read.h. */
24878
24879 void
24880 dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24881 {
24882 gdb_assert (this->get_cu (per_cu) == nullptr);
24883
24884 m_dwarf2_cus[per_cu] = cu;
24885 }
24886
24887 /* See read.h. */
24888
24889 void
24890 dwarf2_per_objfile::age_comp_units ()
24891 {
24892 dwarf_read_debug_printf_v ("running");
24893
24894 /* This is not expected to be called in the middle of CU expansion. There is
24895 an invariant that if a CU is in the CUs-to-expand queue, its DIEs are
24896 loaded in memory. Calling age_comp_units while the queue is in use could
24897 make us free the DIEs for a CU that is in the queue and therefore break
24898 that invariant. */
24899 gdb_assert (!this->per_bfd->queue.has_value ());
24900
24901 /* Start by clearing all marks. */
24902 for (auto pair : m_dwarf2_cus)
24903 pair.second->mark = false;
24904
24905 /* Traverse all CUs, mark them and their dependencies if used recently
24906 enough. */
24907 for (auto pair : m_dwarf2_cus)
24908 {
24909 dwarf2_cu *cu = pair.second;
24910
24911 cu->last_used++;
24912 if (cu->last_used <= dwarf_max_cache_age)
24913 dwarf2_mark (cu);
24914 }
24915
24916 /* Delete all CUs still not marked. */
24917 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
24918 {
24919 dwarf2_cu *cu = it->second;
24920
24921 if (!cu->mark)
24922 {
24923 dwarf_read_debug_printf_v ("deleting old CU %s",
24924 sect_offset_str (cu->per_cu->sect_off));
24925 delete cu;
24926 it = m_dwarf2_cus.erase (it);
24927 }
24928 else
24929 it++;
24930 }
24931 }
24932
24933 /* See read.h. */
24934
24935 void
24936 dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
24937 {
24938 auto it = m_dwarf2_cus.find (per_cu);
24939 if (it == m_dwarf2_cus.end ())
24940 return;
24941
24942 delete it->second;
24943
24944 m_dwarf2_cus.erase (it);
24945 }
24946
24947 dwarf2_per_objfile::~dwarf2_per_objfile ()
24948 {
24949 remove_all_cus ();
24950 }
24951
24952 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24953 We store these in a hash table separate from the DIEs, and preserve them
24954 when the DIEs are flushed out of cache.
24955
24956 The CU "per_cu" pointer is needed because offset alone is not enough to
24957 uniquely identify the type. A file may have multiple .debug_types sections,
24958 or the type may come from a DWO file. Furthermore, while it's more logical
24959 to use per_cu->section+offset, with Fission the section with the data is in
24960 the DWO file but we don't know that section at the point we need it.
24961 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24962 because we can enter the lookup routine, get_die_type_at_offset, from
24963 outside this file, and thus won't necessarily have PER_CU->cu.
24964 Fortunately, PER_CU is stable for the life of the objfile. */
24965
24966 struct dwarf2_per_cu_offset_and_type
24967 {
24968 const struct dwarf2_per_cu_data *per_cu;
24969 sect_offset sect_off;
24970 struct type *type;
24971 };
24972
24973 /* Hash function for a dwarf2_per_cu_offset_and_type. */
24974
24975 static hashval_t
24976 per_cu_offset_and_type_hash (const void *item)
24977 {
24978 const struct dwarf2_per_cu_offset_and_type *ofs
24979 = (const struct dwarf2_per_cu_offset_and_type *) item;
24980
24981 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
24982 }
24983
24984 /* Equality function for a dwarf2_per_cu_offset_and_type. */
24985
24986 static int
24987 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
24988 {
24989 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
24990 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
24991 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
24992 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
24993
24994 return (ofs_lhs->per_cu == ofs_rhs->per_cu
24995 && ofs_lhs->sect_off == ofs_rhs->sect_off);
24996 }
24997
24998 /* Set the type associated with DIE to TYPE. Save it in CU's hash
24999 table if necessary. For convenience, return TYPE.
25000
25001 The DIEs reading must have careful ordering to:
25002 * Not cause infinite loops trying to read in DIEs as a prerequisite for
25003 reading current DIE.
25004 * Not trying to dereference contents of still incompletely read in types
25005 while reading in other DIEs.
25006 * Enable referencing still incompletely read in types just by a pointer to
25007 the type without accessing its fields.
25008
25009 Therefore caller should follow these rules:
25010 * Try to fetch any prerequisite types we may need to build this DIE type
25011 before building the type and calling set_die_type.
25012 * After building type call set_die_type for current DIE as soon as
25013 possible before fetching more types to complete the current type.
25014 * Make the type as complete as possible before fetching more types. */
25015
25016 static struct type *
25017 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25018 bool skip_data_location)
25019 {
25020 dwarf2_per_objfile *per_objfile = cu->per_objfile;
25021 struct dwarf2_per_cu_offset_and_type **slot, ofs;
25022 struct objfile *objfile = per_objfile->objfile;
25023 struct attribute *attr;
25024 struct dynamic_prop prop;
25025
25026 /* For Ada types, make sure that the gnat-specific data is always
25027 initialized (if not already set). There are a few types where
25028 we should not be doing so, because the type-specific area is
25029 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25030 where the type-specific area is used to store the floatformat).
25031 But this is not a problem, because the gnat-specific information
25032 is actually not needed for these types. */
25033 if (need_gnat_info (cu)
25034 && type->code () != TYPE_CODE_FUNC
25035 && type->code () != TYPE_CODE_FLT
25036 && type->code () != TYPE_CODE_METHODPTR
25037 && type->code () != TYPE_CODE_MEMBERPTR
25038 && type->code () != TYPE_CODE_METHOD
25039 && type->code () != TYPE_CODE_FIXED_POINT
25040 && !HAVE_GNAT_AUX_INFO (type))
25041 INIT_GNAT_SPECIFIC (type);
25042
25043 /* Read DW_AT_allocated and set in type. */
25044 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25045 if (attr != NULL)
25046 {
25047 struct type *prop_type = cu->addr_sized_int_type (false);
25048 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25049 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
25050 }
25051
25052 /* Read DW_AT_associated and set in type. */
25053 attr = dwarf2_attr (die, DW_AT_associated, cu);
25054 if (attr != NULL)
25055 {
25056 struct type *prop_type = cu->addr_sized_int_type (false);
25057 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
25058 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
25059 }
25060
25061 /* Read DW_AT_data_location and set in type. */
25062 if (!skip_data_location)
25063 {
25064 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25065 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25066 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25067 }
25068
25069 if (per_objfile->die_type_hash == NULL)
25070 per_objfile->die_type_hash
25071 = htab_up (htab_create_alloc (127,
25072 per_cu_offset_and_type_hash,
25073 per_cu_offset_and_type_eq,
25074 NULL, xcalloc, xfree));
25075
25076 ofs.per_cu = cu->per_cu;
25077 ofs.sect_off = die->sect_off;
25078 ofs.type = type;
25079 slot = (struct dwarf2_per_cu_offset_and_type **)
25080 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
25081 if (*slot)
25082 complaint (_("A problem internal to GDB: DIE %s has type already set"),
25083 sect_offset_str (die->sect_off));
25084 *slot = XOBNEW (&objfile->objfile_obstack,
25085 struct dwarf2_per_cu_offset_and_type);
25086 **slot = ofs;
25087 return type;
25088 }
25089
25090 /* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
25091 or return NULL if the die does not have a saved type. */
25092
25093 static struct type *
25094 get_die_type_at_offset (sect_offset sect_off,
25095 dwarf2_per_cu_data *per_cu,
25096 dwarf2_per_objfile *per_objfile)
25097 {
25098 struct dwarf2_per_cu_offset_and_type *slot, ofs;
25099
25100 if (per_objfile->die_type_hash == NULL)
25101 return NULL;
25102
25103 ofs.per_cu = per_cu;
25104 ofs.sect_off = sect_off;
25105 slot = ((struct dwarf2_per_cu_offset_and_type *)
25106 htab_find (per_objfile->die_type_hash.get (), &ofs));
25107 if (slot)
25108 return slot->type;
25109 else
25110 return NULL;
25111 }
25112
25113 /* Look up the type for DIE in CU in die_type_hash,
25114 or return NULL if DIE does not have a saved type. */
25115
25116 static struct type *
25117 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25118 {
25119 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
25120 }
25121
25122 /* Add a dependence relationship from CU to REF_PER_CU. */
25123
25124 static void
25125 dwarf2_add_dependence (struct dwarf2_cu *cu,
25126 struct dwarf2_per_cu_data *ref_per_cu)
25127 {
25128 void **slot;
25129
25130 if (cu->dependencies == NULL)
25131 cu->dependencies
25132 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25133 NULL, &cu->comp_unit_obstack,
25134 hashtab_obstack_allocate,
25135 dummy_obstack_deallocate);
25136
25137 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25138 if (*slot == NULL)
25139 *slot = ref_per_cu;
25140 }
25141
25142 /* Subroutine of dwarf2_mark to pass to htab_traverse.
25143 Set the mark field in every compilation unit in the
25144 cache that we must keep because we are keeping CU.
25145
25146 DATA is the dwarf2_per_objfile object in which to look up CUs. */
25147
25148 static int
25149 dwarf2_mark_helper (void **slot, void *data)
25150 {
25151 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25152 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25153 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
25154
25155 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25156 reading of the chain. As such dependencies remain valid it is not much
25157 useful to track and undo them during QUIT cleanups. */
25158 if (cu == nullptr)
25159 return 1;
25160
25161 if (cu->mark)
25162 return 1;
25163
25164 cu->mark = true;
25165
25166 if (cu->dependencies != nullptr)
25167 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
25168
25169 return 1;
25170 }
25171
25172 /* Set the mark field in CU and in every other compilation unit in the
25173 cache that we must keep because we are keeping CU. */
25174
25175 static void
25176 dwarf2_mark (struct dwarf2_cu *cu)
25177 {
25178 if (cu->mark)
25179 return;
25180
25181 cu->mark = true;
25182
25183 if (cu->dependencies != nullptr)
25184 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
25185 }
25186
25187 /* Trivial hash function for partial_die_info: the hash value of a DIE
25188 is its offset in .debug_info for this objfile. */
25189
25190 static hashval_t
25191 partial_die_hash (const void *item)
25192 {
25193 const struct partial_die_info *part_die
25194 = (const struct partial_die_info *) item;
25195
25196 return to_underlying (part_die->sect_off);
25197 }
25198
25199 /* Trivial comparison function for partial_die_info structures: two DIEs
25200 are equal if they have the same offset. */
25201
25202 static int
25203 partial_die_eq (const void *item_lhs, const void *item_rhs)
25204 {
25205 const struct partial_die_info *part_die_lhs
25206 = (const struct partial_die_info *) item_lhs;
25207 const struct partial_die_info *part_die_rhs
25208 = (const struct partial_die_info *) item_rhs;
25209
25210 return part_die_lhs->sect_off == part_die_rhs->sect_off;
25211 }
25212
25213 struct cmd_list_element *set_dwarf_cmdlist;
25214 struct cmd_list_element *show_dwarf_cmdlist;
25215
25216 static void
25217 show_check_physname (struct ui_file *file, int from_tty,
25218 struct cmd_list_element *c, const char *value)
25219 {
25220 fprintf_filtered (file,
25221 _("Whether to check \"physname\" is %s.\n"),
25222 value);
25223 }
25224
25225 void _initialize_dwarf2_read ();
25226 void
25227 _initialize_dwarf2_read ()
25228 {
25229 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
25230 Set DWARF specific variables.\n\
25231 Configure DWARF variables such as the cache size."),
25232 &set_dwarf_cmdlist, "maintenance set dwarf ",
25233 0/*allow-unknown*/, &maintenance_set_cmdlist);
25234
25235 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
25236 Show DWARF specific variables.\n\
25237 Show DWARF variables such as the cache size."),
25238 &show_dwarf_cmdlist, "maintenance show dwarf ",
25239 0/*allow-unknown*/, &maintenance_show_cmdlist);
25240
25241 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25242 &dwarf_max_cache_age, _("\
25243 Set the upper bound on the age of cached DWARF compilation units."), _("\
25244 Show the upper bound on the age of cached DWARF compilation units."), _("\
25245 A higher limit means that cached compilation units will be stored\n\
25246 in memory longer, and more total memory will be used. Zero disables\n\
25247 caching, which can slow down startup."),
25248 NULL,
25249 show_dwarf_max_cache_age,
25250 &set_dwarf_cmdlist,
25251 &show_dwarf_cmdlist);
25252
25253 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25254 Set debugging of the DWARF reader."), _("\
25255 Show debugging of the DWARF reader."), _("\
25256 When enabled (non-zero), debugging messages are printed during DWARF\n\
25257 reading and symtab expansion. A value of 1 (one) provides basic\n\
25258 information. A value greater than 1 provides more verbose information."),
25259 NULL,
25260 NULL,
25261 &setdebuglist, &showdebuglist);
25262
25263 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25264 Set debugging of the DWARF DIE reader."), _("\
25265 Show debugging of the DWARF DIE reader."), _("\
25266 When enabled (non-zero), DIEs are dumped after they are read in.\n\
25267 The value is the maximum depth to print."),
25268 NULL,
25269 NULL,
25270 &setdebuglist, &showdebuglist);
25271
25272 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25273 Set debugging of the dwarf line reader."), _("\
25274 Show debugging of the dwarf line reader."), _("\
25275 When enabled (non-zero), line number entries are dumped as they are read in.\n\
25276 A value of 1 (one) provides basic information.\n\
25277 A value greater than 1 provides more verbose information."),
25278 NULL,
25279 NULL,
25280 &setdebuglist, &showdebuglist);
25281
25282 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25283 Set cross-checking of \"physname\" code against demangler."), _("\
25284 Show cross-checking of \"physname\" code against demangler."), _("\
25285 When enabled, GDB's internal \"physname\" code is checked against\n\
25286 the demangler."),
25287 NULL, show_check_physname,
25288 &setdebuglist, &showdebuglist);
25289
25290 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25291 no_class, &use_deprecated_index_sections, _("\
25292 Set whether to use deprecated gdb_index sections."), _("\
25293 Show whether to use deprecated gdb_index sections."), _("\
25294 When enabled, deprecated .gdb_index sections are used anyway.\n\
25295 Normally they are ignored either because of a missing feature or\n\
25296 performance issue.\n\
25297 Warning: This option must be enabled before gdb reads the file."),
25298 NULL,
25299 NULL,
25300 &setlist, &showlist);
25301
25302 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25303 &dwarf2_locexpr_funcs);
25304 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25305 &dwarf2_loclist_funcs);
25306
25307 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25308 &dwarf2_block_frame_base_locexpr_funcs);
25309 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25310 &dwarf2_block_frame_base_loclist_funcs);
25311
25312 #if GDB_SELF_TEST
25313 selftests::register_test ("dw2_expand_symtabs_matching",
25314 selftests::dw2_expand_symtabs_matching::run_test);
25315 selftests::register_test ("dwarf2_find_containing_comp_unit",
25316 selftests::find_containing_comp_unit::run_test);
25317 #endif
25318 }
This page took 0.550224 seconds and 3 git commands to generate.